File : s-stm32.ads


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT RUN-TIME COMPONENTS                         --
   4 --                                                                          --
   5 --          Copyright (C) 2012-2016, Free Software Foundation, Inc.         --
   6 --                                                                          --
   7 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
   8 -- terms of the  GNU General Public License as published  by the Free Soft- --
   9 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  10 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  11 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  12 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  13 --                                                                          --
  14 --                                                                          --
  15 --                                                                          --
  16 --                                                                          --
  17 --                                                                          --
  18 -- You should have received a copy of the GNU General Public License and    --
  19 -- a copy of the GCC Runtime Library Exception along with this program;     --
  20 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
  21 -- <http://www.gnu.org/licenses/>.                                          --
  22 --                                                                          --
  23 -- GNAT was originally developed  by the GNAT team at  New York University. --
  24 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  25 --                                                                          --
  26 ------------------------------------------------------------------------------
  27 
  28 --  This file provides register definitions for the STM32Fx (ARM Cortex M4/7)
  29 --  microcontrollers from ST Microelectronics.
  30 
  31 pragma Restrictions (No_Elaboration_Code);
  32 
  33 with Interfaces.Bit_Types;
  34 
  35 package System.STM32 is
  36    pragma Preelaborate (System.STM32);
  37 
  38    subtype Frequency is Interfaces.Bit_Types.Word;
  39 
  40    type RCC_System_Clocks is record
  41       SYSCLK  : Frequency;
  42       HCLK    : Frequency;
  43       PCLK1   : Frequency;
  44       PCLK2   : Frequency;
  45       TIMCLK1 : Frequency;
  46       TIMCLK2 : Frequency;
  47    end record;
  48 
  49    function System_Clocks return RCC_System_Clocks;
  50 
  51    --  MODER constants
  52    subtype GPIO_MODER_Values is Interfaces.Bit_Types.UInt2;
  53    Mode_IN  : constant GPIO_MODER_Values := 0;
  54    Mode_OUT : constant GPIO_MODER_Values := 1;
  55    Mode_AF  : constant GPIO_MODER_Values := 2;
  56    Mode_AN  : constant GPIO_MODER_Values := 3;
  57 
  58    --  OTYPER constants
  59    subtype GPIO_OTYPER_Values is Interfaces.Bit_Types.Bit;
  60    Push_Pull  : constant GPIO_OTYPER_Values := 0;
  61    Open_Drain : constant GPIO_OTYPER_Values := 1;
  62 
  63    --  OSPEEDR constants
  64    subtype GPIO_OSPEEDR_Values is Interfaces.Bit_Types.UInt2;
  65    Speed_2MHz   : constant GPIO_OSPEEDR_Values := 0; -- Low speed
  66    Speed_25MHz  : constant GPIO_OSPEEDR_Values := 1; -- Medium speed
  67    Speed_50MHz  : constant GPIO_OSPEEDR_Values := 2; -- Fast speed
  68    Speed_100MHz : constant GPIO_OSPEEDR_Values := 3; -- High speed
  69 
  70    --  PUPDR constants
  71    subtype GPIO_PUPDR_Values is Interfaces.Bit_Types.UInt2;
  72    No_Pull   : constant GPIO_PUPDR_Values := 0;
  73    Pull_Up   : constant GPIO_PUPDR_Values := 1;
  74    Pull_Down : constant GPIO_PUPDR_Values := 2;
  75 
  76    --  AFL constants
  77    AF_USART1  : constant Interfaces.Bit_Types.UInt4 := 7;
  78    AF_USART6  : constant Interfaces.Bit_Types.UInt4 := 8;
  79 
  80    type MCU_ID_Register is record
  81       DEV_ID   : Interfaces.Bit_Types.UInt12;
  82       Reserved : Interfaces.Bit_Types.UInt4;
  83       REV_ID   : Interfaces.Bit_Types.Short;
  84    end record with Pack, Size => 32;
  85 
  86    --  RCC constants
  87 
  88    type PLL_Source is
  89      (PLL_SRC_HSI,
  90       PLL_SRC_HSE)
  91      with Size => 1;
  92 
  93    type SYSCLK_Source is
  94      (SYSCLK_SRC_HSI,
  95       SYSCLK_SRC_HSE,
  96       SYSCLK_SRC_PLL)
  97      with Size => 2;
  98 
  99    type AHB_Prescaler_Enum is
 100      (DIV2,  DIV4,   DIV8,   DIV16,
 101       DIV64, DIV128, DIV256, DIV512)
 102      with Size => 3;
 103 
 104    type AHB_Prescaler is record
 105       Enabled : Boolean := False;
 106       Value   : AHB_Prescaler_Enum := AHB_Prescaler_Enum'First;
 107    end record with Size => 4;
 108 
 109    for AHB_Prescaler use record
 110       Enabled at 0 range 3 .. 3;
 111       Value   at 0 range 0 .. 2;
 112    end record;
 113 
 114    AHBPRE_DIV1 : constant AHB_Prescaler := (Enabled => False, Value => DIV2);
 115 
 116    type APB_Prescaler_Enum is
 117      (DIV2,  DIV4,  DIV8,  DIV16)
 118      with Size => 2;
 119 
 120    type APB_Prescaler is record
 121       Enabled : Boolean;
 122       Value   : APB_Prescaler_Enum;
 123    end record with Size => 3;
 124 
 125    for APB_Prescaler use record
 126       Enabled at 0 range 2 .. 2;
 127       Value   at 0 range 0 .. 1;
 128    end record;
 129 
 130    type I2S_Clock_Selection is
 131      (I2SSEL_PLL,
 132       I2SSEL_CKIN)
 133      with Size => 1;
 134 
 135    type MC01_Clock_Selection is
 136      (MC01SEL_HSI,
 137       MC01SEL_LSE,
 138       MC01SEL_HSE,
 139       MC01SEL_PLL)
 140      with Size => 2;
 141 
 142    type MC02_Clock_Selection is
 143      (MC02SEL_SYSCLK,
 144       MC02SEL_PLLI2S,
 145       MC02SEL_HSE,
 146       MC02SEL_PLL)
 147      with Size => 2;
 148 
 149    type MC0x_Prescaler is
 150      (MC0xPRE_DIV1,
 151       MC0xPRE_DIV2,
 152       MC0xPRE_DIV3,
 153       MC0xPRE_DIV4,
 154       MC0xPRE_DIV5)
 155      with Size => 3;
 156    for MC0x_Prescaler use
 157      (MC0xPRE_DIV1 => 0,
 158       MC0xPRE_DIV2 => 2#100#,
 159       MC0xPRE_DIV3 => 2#101#,
 160       MC0xPRE_DIV4 => 2#110#,
 161       MC0xPRE_DIV5 => 2#111#);
 162 
 163    --  Constants for RCC CR register
 164 
 165    subtype PLLM_Range is Integer range 2 .. 63;
 166    subtype PLLN_Range is Integer range 50 .. 432;
 167    subtype PLLP_Range is Integer range 2 .. 8
 168      with Static_Predicate => (case PLLP_Range is
 169                                  when 2 | 4 | 6 | 8 => True,
 170                                  when others => False);
 171    subtype PLLQ_Range is Integer range 2 .. 15;
 172 
 173    subtype HSECLK_Range is Integer range   1_000_000 ..  26_000_000;
 174    subtype PLLIN_Range  is Integer range     950_000 ..   2_000_000;
 175    subtype PLLVC0_Range is Integer range 192_000_000 .. 432_000_000;
 176    subtype PLLOUT_Range is Integer range  24_000_000 .. 216_000_000;
 177    subtype SYSCLK_Range is Integer range           1 .. 216_000_000;
 178    subtype HCLK_Range   is Integer range           1 .. 216_000_000;
 179    subtype PCLK1_Range  is Integer range           1 ..  54_000_000;
 180    subtype PCLK2_Range  is Integer range           1 .. 108_000_000;
 181    subtype SPII2S_Range is Integer range           1 ..  37_500_000;
 182    pragma Unreferenced (SPII2S_Range);
 183 
 184    --  These internal low and high speed clocks are fixed (do not modify)
 185 
 186    HSICLK : constant := 16_000_000;
 187    LSICLK : constant :=     32_000;
 188 
 189    MCU_ID : MCU_ID_Register with Volatile,
 190                                  Address => System'To_Address (16#E004_2000#);
 191    --  Only 32-bits access supported (read-only)
 192 
 193    DEV_ID_STM32F40xxx : constant := 16#413#; --  STM32F40xxx/41xxx
 194    DEV_ID_STM32F42xxx : constant := 16#419#; --  STM32F42xxx/43xxx
 195    DEV_ID_STM32F46xxx : constant := 16#434#; --  STM32F469xx/479xx
 196    DEV_ID_STM32F74xxx : constant := 16#449#; --  STM32F74xxx/75xxx
 197 
 198 end System.STM32;