stm32f0x2_hal_0.1.0_85eaea48/src/devices/STM32F0X2/stm32-device.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
with System;        use System;
with System.Machine_Code;
with STM32_SVD.RCC; use STM32_SVD.RCC;

package body STM32.Device is

   --  FIXME  ADL_Config.High_Speed_External_Clock;
   --  No board, so no ADL_Config
   HSE_VALUE : constant := 0;
   --  External oscillator in Hz

   HSI_VALUE : constant := 8_000_000;
   --  Internal oscillator in Hz

   HSI48_VALUE : constant := 48_000_000;
   --  internal oscillator in Hz

   HPRE_Presc_Table : constant array (UInt4) of UInt32 :=
     [1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 8, 16, 64, 128, 256, 512];

   PPRE_Presc_Table : constant array (UInt3) of UInt32 :=
     [1, 1, 1, 1, 2, 4, 8, 16];

   procedure Delay_Cycles (Cycles : UInt32) is
      use System.Machine_Code;
      use ASCII;
      Tmp : constant UInt32 := 1 + Cycles / 2;
   begin
      Asm
        ("1:" & LF & HT & "sub %0, #1" & LF & HT & "bne 1b" & LF & HT,
         Inputs   => (UInt32'Asm_Input ("r", Tmp)), Clobber => "r3",
         Volatile => True);
   end Delay_Cycles;

   ------------------------------
   -- GPIO_Port_Representation --
   ------------------------------

   function GPIO_Port_Representation (Port : GPIO_Port) return UInt4 is
   begin
      --  TODO: rather ugly to have this board-specific range here
      if Port'Address = GPIOA_Base then
         return 0;
      elsif Port'Address = GPIOB_Base then
         return 1;
      elsif Port'Address = GPIOC_Base then
         return 2;
      elsif Port'Address = GPIOD_Base then
         return 3;
      elsif Port'Address = GPIOE_Base then
         return 4;
      elsif Port'Address = GPIOF_Base then
         return 5;
      else
         raise Program_Error;
      end if;
   end GPIO_Port_Representation;

   ------------------
   -- Enable_Clock --
   ------------------

   procedure Enable_Clock (This : aliased in out GPIO_Port) is
   begin
      if This'Address = GPIOA_Base then
         RCC_Periph.AHBENR.IOPAEN := True;
      elsif This'Address = GPIOB_Base then
         RCC_Periph.AHBENR.IOPBEN := True;
      elsif This'Address = GPIOC_Base then
         RCC_Periph.AHBENR.IOPCEN := True;
      elsif This'Address = GPIOD_Base then
         RCC_Periph.AHBENR.IOPDEN := True;
         --  There is an error in the SVD file where this BIT (21 in
         --  RCC_AHBENR) is missing.
         --  elsif This'Address = GPIOE_Base then
         --    RCC_Periph.AHBENR.IOPEEN := True;
      elsif This'Address = GPIOF_Base then
         RCC_Periph.AHBENR.IOPFEN := True;
      else
         raise Unknown_Device;
      end if;
   end Enable_Clock;

   ------------------
   -- Enable_Clock --
   ------------------

   procedure Enable_Clock (Point : GPIO_Point) is
   begin
      Enable_Clock (Point.Periph.all);
   end Enable_Clock;

   ------------------
   -- Enable_Clock --
   ------------------

   procedure Enable_Clock (Points : GPIO_Points) is
   begin
      for Point of Points loop
         Enable_Clock (Point.Periph.all);
      end loop;
   end Enable_Clock;

   -----------
   -- Reset --
   -----------

   procedure Reset (This : aliased in out GPIO_Port) is
   begin
      if This'Address = GPIOA_Base then
         RCC_Periph.AHBRSTR.IOPARST := True;
         RCC_Periph.AHBRSTR.IOPARST := False;
      elsif This'Address = GPIOB_Base then
         RCC_Periph.AHBRSTR.IOPBRST := True;
         RCC_Periph.AHBRSTR.IOPBRST := False;
      elsif This'Address = GPIOC_Base then
         RCC_Periph.AHBRSTR.IOPCRST := True;
         RCC_Periph.AHBRSTR.IOPCRST := False;
      elsif This'Address = GPIOD_Base then
         RCC_Periph.AHBRSTR.IOPDRST := True;
         RCC_Periph.AHBRSTR.IOPDRST := False;

      --  There is an error in the SVD file where this BIT (21 in RCC_AHBRSTR) is
      --  missing.
      --  elsif This'Address = GPIOE_Base then
         --     RCC_Periph.AHBRSTR.IOPERST := True;
         --     RCC_Periph.AHBRSTR.IOPERST := False;
      elsif This'Address = GPIOF_Base then
         RCC_Periph.AHBRSTR.IOPFRST := True;
         RCC_Periph.AHBRSTR.IOPFRST := False;
      else
         raise Unknown_Device;
      end if;
   end Reset;

   -----------
   -- Reset --
   -----------

   procedure Reset (Point : GPIO_Point) is
   begin
      Reset (Point.Periph.all);
   end Reset;

   -----------
   -- Reset --
   -----------

   procedure Reset (Points : GPIO_Points) is
      Do_Reset : Boolean;
   begin
      for J in Points'Range loop
         Do_Reset := True;
         for K in Points'First .. J - 1 loop
            if Points (K).Periph = Points (J).Periph then
               Do_Reset := False;

               exit;
            end if;
         end loop;

         if Do_Reset then
            Reset (Points (J).Periph.all);
         end if;
      end loop;
   end Reset;

   ------------------
   -- Enable_Clock --
   ------------------

   procedure Enable_Clock (This : aliased in out USART) is
   begin
      if This.Periph.all'Address = USART1_Base then
         RCC_Periph.APB2ENR.USART1EN := True;
      elsif This.Periph.all'Address = USART2_Base then
         RCC_Periph.APB1ENR.USART2EN := True;
      elsif This.Periph.all'Address = USART3_Base then
         RCC_Periph.APB1ENR.USART3EN := True;
      elsif This.Periph.all'Address = USART4_Base then
         RCC_Periph.APB1ENR.USART4EN := True;
      else
         raise Unknown_Device;
      end if;
   end Enable_Clock;

   -----------
   -- Reset --
   -----------

   procedure Reset (This : aliased in out USART) is
   begin
      if This.Periph.all'Address = USART1_Base then
         RCC_Periph.APB2RSTR.USART1RST := True;
         RCC_Periph.APB2RSTR.USART1RST := False;
      elsif This.Periph.all'Address = USART2_Base then
         RCC_Periph.APB1RSTR.USART2RST := True;
         RCC_Periph.APB1RSTR.USART2RST := False;
      elsif This.Periph.all'Address = USART3_Base then
         RCC_Periph.APB1RSTR.USART3RST := True;
         RCC_Periph.APB1RSTR.USART3RST := False;
      elsif This.Periph.all'Address = USART4_Base then
         RCC_Periph.APB1RSTR.USART4RST := True;
         RCC_Periph.APB1RSTR.USART4RST := False;
      else
         raise Unknown_Device;
      end if;
   end Reset;

   ------------------------------
   -- System_Clock_Frequencies --
   ------------------------------

   function System_Clock_Frequencies return RCC_System_Clocks is
      Source : constant UInt2 := RCC_Periph.CFGR.SWS;
      Result : RCC_System_Clocks;
   begin

      case Source is
         when 0 =>
            --  HSI as source
            Result.SYSCLK := HSI_VALUE;
         when 1 =>
            --  HSE as source
            Result.SYSCLK := HSE_VALUE;
         when 2 =>
            --  PLL as source
            declare
               Input_Source : constant Uint2  := RCC_Periph.CFGR.PLLSRC;
               Prediv : constant UInt32 := UInt32 (RCC_Periph.CFGR2.PREDIV);
               Pllmul : constant UInt32 := UInt32 (RCC_Periph.CFGR.PLLMUL);
               Pllval       : UInt32          := HSI_VALUE;
            begin
               case Input_Source is
                  when 0 =>
                     Pllval := HSI_VALUE / 2;
                  when 1 =>
                     Pllval := HSI_VALUE / Prediv;
                  when 2 =>
                     Pllval := HSE_VALUE / Prediv;
                  when 3 =>
                     Pllval := HSI48_VALUE / Prediv;
               end case;

               Pllval := Pllval * Pllmul;

               Result.SYSCLK := Pllval;
            end;
         when 3 =>
            Result.SYSCLK := HSI48_VALUE;
      end case;

      declare
         HPRE : constant UInt4 := RCC_Periph.CFGR.HPRE;
         PPRE : constant UInt3 := RCC_Periph.CFGR.PPRE;
      begin
         Result.HCLK := Result.SYSCLK / HPRE_Presc_Table (HPRE);
         Result.PCLK := Result.HCLK / PPRE_Presc_Table (PPRE);
      end;

      return Result;
   end System_Clock_Frequencies;

   procedure Enable_Clock (This : in out Timer) is
   begin
      if This'Address = TIM1_Base then
         RCC_Periph.APB2ENR.TIM1EN := True;
      elsif This'Address = TIM2_Base then
         RCC_Periph.APB1ENR.TIM2EN := True;
      elsif This'Address = TIM3_Base then
         RCC_Periph.APB1ENR.TIM3EN := True;

      elsif This'Address = TIM14_Base then
         RCC_Periph.APB1ENR.TIM14EN := True;
      elsif This'Address = TIM15_Base then
         RCC_Periph.APB2ENR.TIM15EN := True;
      elsif This'Address = TIM16_Base then
         RCC_Periph.APB2ENR.TIM16EN := True;
      elsif This'Address = TIM17_Base then
         RCC_Periph.APB2ENR.TIM17EN := True;

      elsif This'Address = TIM6_Base then
         RCC_Periph.APB1ENR.TIM6EN := True;
      elsif This'Address = TIM7_Base then
         RCC_Periph.APB1ENR.TIM7EN := True;

      else
         raise Unknown_Device;
      end if;
   end Enable_Clock;

   -----------
   -- Reset --
   -----------

   procedure Reset (This : in out Timer) is
   begin
      if This'Address = TIM1_Base then
         RCC_Periph.APB2RSTR.TIM1RST := True;
         RCC_Periph.APB2RSTR.TIM1RST := False;
      elsif This'Address = TIM2_Base then
         RCC_Periph.APB1RSTR.TIM2RST := True;
         RCC_Periph.APB1RSTR.TIM2RST := False;
      elsif This'Address = TIM3_Base then
         RCC_Periph.APB1RSTR.TIM3RST := True;
         RCC_Periph.APB1RSTR.TIM3RST := False;

      elsif This'Address = TIM14_Base then
         RCC_Periph.APB1RSTR.TIM14RST := True;
         RCC_Periph.APB1RSTR.TIM14RST := False;
      elsif This'Address = TIM15_Base then
         RCC_Periph.APB2RSTR.TIM15RST := True;
         RCC_Periph.APB2RSTR.TIM15RST := False;
      elsif This'Address = TIM16_Base then
         RCC_Periph.APB2RSTR.TIM16RST := True;
         RCC_Periph.APB2RSTR.TIM16RST := False;
      elsif This'Address = TIM17_Base then
         RCC_Periph.APB2RSTR.TIM17RST := True;
         RCC_Periph.APB2RSTR.TIM17RST := False;

      elsif This'Address = TIM6_Base then
         RCC_Periph.APB1RSTR.TIM6RST := True;
         RCC_Periph.APB1RSTR.TIM6RST := False;
      elsif This'Address = TIM7_Base then
         RCC_Periph.APB1RSTR.TIM7RST := True;
         RCC_Periph.APB1RSTR.TIM7RST := False;

      else
         raise Unknown_Device;
      end if;
   end Reset;
end STM32.Device;