2
2
******************************************************************************
3
3
* @file system_stm32f2xx.c
4
4
* @author MCD Application Team
5
- * @version V2.1.2
6
- * @date 29-June-2016
5
+ * @version V2.2.0
6
+ * @date 17-March-2017
7
7
* @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File.
8
8
*
9
9
* This file provides two functions and one global variable to be called from
39
39
******************************************************************************
40
40
* @attention
41
41
*
42
- * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
42
+ * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
43
43
*
44
44
* Redistribution and use in source and binary forms, with or without modification,
45
45
* are permitted provided that the following conditions are met:
127
127
/** @addtogroup STM32F2xx_System_Private_Variables
128
128
* @{
129
129
*/
130
-
131
- /* This variable can be updated in Three ways :
132
- 1) by calling CMSIS function SystemCoreClockUpdate()
133
- 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
134
- 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
135
- Note: If you use this function to configure the system clock; then there
136
- is no need to call the 2 first functions listed above, since SystemCoreClock
137
- variable is updated automatically.
138
- */
139
- uint32_t SystemCoreClock = 120000000 ;
140
- const uint8_t AHBPrescTable [16 ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 };
141
-
130
+
131
+ /* This variable can be updated in Three ways :
132
+ 1) by calling CMSIS function SystemCoreClockUpdate()
133
+ 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
134
+ 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
135
+ Note: If you use this function to configure the system clock; then there
136
+ is no need to call the 2 first functions listed above, since SystemCoreClock
137
+ variable is updated automatically.
138
+ */
139
+ uint32_t SystemCoreClock = 16000000 ;
140
+ const uint8_t AHBPrescTable [16 ] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 };
141
+ const uint8_t APBPrescTable [ 8 ] = { 0 , 0 , 0 , 0 , 1 , 2 , 3 , 4 };
142
142
/**
143
143
* @}
144
144
*/
@@ -148,7 +148,7 @@ const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8,
148
148
*/
149
149
150
150
#ifdef DATA_IN_ExtSRAM
151
- static void SystemInit_ExtMemCtl (void );
151
+ static void SystemInit_ExtMemCtl (void );
152
152
#endif /* DATA_IN_ExtSRAM */
153
153
154
154
#if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
@@ -166,41 +166,41 @@ uint8_t SetSysClock_PLL_HSI(void);
166
166
167
167
/**
168
168
* @brief Setup the microcontroller system
169
- * Initialize the Embedded Flash Interface, the PLL and update the
169
+ * Initialize the Embedded Flash Interface, the PLL and update the
170
170
* SystemFrequency variable.
171
171
* @param None
172
172
* @retval None
173
173
*/
174
174
void SystemInit (void )
175
175
{
176
- /* Reset the RCC clock configuration to the default reset state ------------*/
177
- /* Set HSION bit */
178
- RCC -> CR |= (uint32_t )0x00000001 ;
176
+ /* Reset the RCC clock configuration to the default reset state ------------*/
177
+ /* Set HSION bit */
178
+ RCC -> CR |= (uint32_t )0x00000001 ;
179
179
180
- /* Reset CFGR register */
181
- RCC -> CFGR = 0x00000000 ;
180
+ /* Reset CFGR register */
181
+ RCC -> CFGR = 0x00000000 ;
182
182
183
- /* Reset HSEON, CSSON and PLLON bits */
184
- RCC -> CR &= (uint32_t )0xFEF6FFFF ;
183
+ /* Reset HSEON, CSSON and PLLON bits */
184
+ RCC -> CR &= (uint32_t )0xFEF6FFFF ;
185
185
186
- /* Reset PLLCFGR register */
187
- RCC -> PLLCFGR = 0x24003010 ;
186
+ /* Reset PLLCFGR register */
187
+ RCC -> PLLCFGR = 0x24003010 ;
188
188
189
- /* Reset HSEBYP bit */
190
- RCC -> CR &= (uint32_t )0xFFFBFFFF ;
189
+ /* Reset HSEBYP bit */
190
+ RCC -> CR &= (uint32_t )0xFFFBFFFF ;
191
191
192
- /* Disable all interrupts */
193
- RCC -> CIR = 0x00000000 ;
192
+ /* Disable all interrupts */
193
+ RCC -> CIR = 0x00000000 ;
194
194
195
195
#ifdef DATA_IN_ExtSRAM
196
- SystemInit_ExtMemCtl ();
196
+ SystemInit_ExtMemCtl ();
197
197
#endif /* DATA_IN_ExtSRAM */
198
198
199
- /* Configure the Vector Table location add offset address ------------------*/
199
+ /* Configure the Vector Table location add offset address ------------------*/
200
200
#ifdef VECT_TAB_SRAM
201
- SCB -> VTOR = SRAM_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal SRAM */
201
+ SCB -> VTOR = SRAM_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal SRAM */
202
202
#else
203
- SCB -> VTOR = FLASH_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal FLASH */
203
+ SCB -> VTOR = FLASH_BASE | VECT_TAB_OFFSET ; /* Vector Table Relocation in Internal FLASH */
204
204
#endif
205
205
206
206
#if (USE_PLL_HSE_XTAL != 0 ) || (USE_PLL_HSE_EXTC != 0 )
@@ -224,24 +224,83 @@ void SystemInit(void)
224
224
* The SystemCoreClock variable contains the core clock (HCLK), it can
225
225
* be used by the user application to setup the SysTick timer or configure
226
226
* other parameters.
227
- *
227
+ *
228
228
* @note Each time the core clock (HCLK) changes, this function must be called
229
229
* to update SystemCoreClock variable value. Otherwise, any configuration
230
- * based on this variable will be incorrect.
231
- *
232
- * @note - The system frequency computed by this function is not the real
233
- * frequency in the chip. It is calculated based on the predefined
230
+ * based on this variable will be incorrect.
231
+ *
232
+ * @note - The system frequency computed by this function is not the real
233
+ * frequency in the chip. It is calculated based on the predefined
234
234
* constant and the selected clock source:
235
- *
235
+ *
236
+ * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
237
+ *
238
+ * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
239
+ *
240
+ * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
241
+ * or HSI_VALUE(*) multiplied/divided by the PLL factors.
242
+ *
243
+ * (*) HSI_VALUE is a constant defined in stm32f2xx_hal_conf.h file (default value
244
+ * 16 MHz) but the real value may vary depending on the variations
245
+ * in voltage and temperature.
246
+ *
247
+ * (**) HSE_VALUE is a constant defined in stm32f2xx_hal_conf.h file (its value
248
+ * depends on the application requirements), user has to ensure that HSE_VALUE
249
+ * is same as the real frequency of the crystal used. Otherwise, this function
250
+ * may have wrong result.
251
+ *
236
252
* - The result of this function could be not correct when using fractional
237
253
* value for HSE crystal.
238
- *
254
+ *
239
255
* @param None
240
256
* @retval None
241
257
*/
242
258
void SystemCoreClockUpdate (void )
243
259
{
244
- SystemCoreClock = HAL_RCC_GetSysClockFreq ();
260
+ uint32_t tmp = 0 , pllvco = 0 , pllp = 2 , pllsource = 0 , pllm = 2 ;
261
+
262
+ /* Get SYSCLK source -------------------------------------------------------*/
263
+ tmp = RCC -> CFGR & RCC_CFGR_SWS ;
264
+
265
+ switch (tmp )
266
+ {
267
+ case 0x00 : /* HSI used as system clock source */
268
+ SystemCoreClock = HSI_VALUE ;
269
+ break ;
270
+ case 0x04 : /* HSE used as system clock source */
271
+ SystemCoreClock = HSE_VALUE ;
272
+ break ;
273
+ case 0x08 : /* PLL used as system clock source */
274
+
275
+ /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
276
+ SYSCLK = PLL_VCO / PLL_P
277
+ */
278
+ pllsource = (RCC -> PLLCFGR & RCC_PLLCFGR_PLLSRC ) >> 22 ;
279
+ pllm = RCC -> PLLCFGR & RCC_PLLCFGR_PLLM ;
280
+
281
+ if (pllsource != 0 )
282
+ {
283
+ /* HSE used as PLL clock source */
284
+ pllvco = (HSE_VALUE / pllm ) * ((RCC -> PLLCFGR & RCC_PLLCFGR_PLLN ) >> 6 );
285
+ }
286
+ else
287
+ {
288
+ /* HSI used as PLL clock source */
289
+ pllvco = (HSI_VALUE / pllm ) * ((RCC -> PLLCFGR & RCC_PLLCFGR_PLLN ) >> 6 );
290
+ }
291
+
292
+ pllp = (((RCC -> PLLCFGR & RCC_PLLCFGR_PLLP ) >>16 ) + 1 ) * 2 ;
293
+ SystemCoreClock = pllvco /pllp ;
294
+ break ;
295
+ default :
296
+ SystemCoreClock = HSI_VALUE ;
297
+ break ;
298
+ }
299
+ /* Compute HCLK frequency --------------------------------------------------*/
300
+ /* Get HCLK prescaler */
301
+ tmp = AHBPrescTable [((RCC -> CFGR & RCC_CFGR_HPRE ) >> 4 )];
302
+ /* HCLK frequency */
303
+ SystemCoreClock >>= tmp ;
245
304
}
246
305
247
306
#ifdef DATA_IN_ExtSRAM
@@ -255,71 +314,71 @@ void SystemCoreClockUpdate(void)
255
314
*/
256
315
void SystemInit_ExtMemCtl (void )
257
316
{
258
- __IO uint32_t tmp = 0x00 ;
259
-
260
- /*-- GPIOs Configuration -----------------------------------------------------*/
261
- /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
262
- RCC -> AHB1ENR |= 0x00000078 ;
263
- /* Delay after an RCC peripheral clock enabling */
264
- tmp = READ_BIT (RCC -> AHB1ENR , RCC_AHB1ENR_GPIODEN );
265
- (void )(tmp );
266
-
267
- /* Connect PDx pins to FSMC Alternate function */
268
- GPIOD -> AFR [0 ] = 0x00CCC0CC ;
269
- GPIOD -> AFR [1 ] = 0xCCCCCCCC ;
270
- /* Configure PDx pins in Alternate function mode */
271
- GPIOD -> MODER = 0xAAAA0A8A ;
272
- /* Configure PDx pins speed to 100 MHz */
273
- GPIOD -> OSPEEDR = 0xFFFF0FCF ;
274
- /* Configure PDx pins Output type to push-pull */
275
- GPIOD -> OTYPER = 0x00000000 ;
276
- /* No pull-up, pull-down for PDx pins */
277
- GPIOD -> PUPDR = 0x00000000 ;
278
-
279
- /* Connect PEx pins to FSMC Alternate function */
280
- GPIOE -> AFR [0 ] = 0xC00CC0CC ;
281
- GPIOE -> AFR [1 ] = 0xCCCCCCCC ;
282
- /* Configure PEx pins in Alternate function mode */
283
- GPIOE -> MODER = 0xAAAA828A ;
284
- /* Configure PEx pins speed to 100 MHz */
285
- GPIOE -> OSPEEDR = 0xFFFFC3CF ;
286
- /* Configure PEx pins Output type to push-pull */
287
- GPIOE -> OTYPER = 0x00000000 ;
288
- /* No pull-up, pull-down for PEx pins */
289
- GPIOE -> PUPDR = 0x00000000 ;
290
-
291
- /* Connect PFx pins to FSMC Alternate function */
292
- GPIOF -> AFR [0 ] = 0x00CCCCCC ;
293
- GPIOF -> AFR [1 ] = 0xCCCC0000 ;
294
- /* Configure PFx pins in Alternate function mode */
295
- GPIOF -> MODER = 0xAA000AAA ;
296
- /* Configure PFx pins speed to 100 MHz */
297
- GPIOF -> OSPEEDR = 0xFF000FFF ;
298
- /* Configure PFx pins Output type to push-pull */
299
- GPIOF -> OTYPER = 0x00000000 ;
300
- /* No pull-up, pull-down for PFx pins */
301
- GPIOF -> PUPDR = 0x00000000 ;
302
-
303
- /* Connect PGx pins to FSMC Alternate function */
304
- GPIOG -> AFR [0 ] = 0x00CCCCCC ;
305
- GPIOG -> AFR [1 ] = 0x000000C0 ;
306
- /* Configure PGx pins in Alternate function mode */
307
- GPIOG -> MODER = 0x00085AAA ;
308
- /* Configure PGx pins speed to 100 MHz */
309
- GPIOG -> OSPEEDR = 0x000CAFFF ;
310
- /* Configure PGx pins Output type to push-pull */
311
- GPIOG -> OTYPER = 0x00000000 ;
312
- /* No pull-up, pull-down for PGx pins */
313
- GPIOG -> PUPDR = 0x00000000 ;
314
-
315
- /*--FSMC Configuration -------------------------------------------------------*/
316
- /* Enable the FSMC interface clock */
317
- RCC -> AHB3ENR |= 0x00000001 ;
318
-
319
- /* Configure and enable Bank1_SRAM2 */
320
- FSMC_Bank1 -> BTCR [2 ] = 0x00001011 ;
321
- FSMC_Bank1 -> BTCR [3 ] = 0x00000201 ;
322
- FSMC_Bank1E -> BWTR [2 ] = 0x0FFFFFFF ;
317
+ __IO uint32_t tmp = 0x00 ;
318
+
319
+ /*-- GPIOs Configuration -----------------------------------------------------*/
320
+ /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */
321
+ RCC -> AHB1ENR |= 0x00000078 ;
322
+ /* Delay after an RCC peripheral clock enabling */
323
+ tmp = READ_BIT (RCC -> AHB1ENR , RCC_AHB1ENR_GPIODEN );
324
+ (void )(tmp );
325
+
326
+ /* Connect PDx pins to FSMC Alternate function */
327
+ GPIOD -> AFR [0 ] = 0x00CCC0CC ;
328
+ GPIOD -> AFR [1 ] = 0xCCCCCCCC ;
329
+ /* Configure PDx pins in Alternate function mode */
330
+ GPIOD -> MODER = 0xAAAA0A8A ;
331
+ /* Configure PDx pins speed to 100 MHz */
332
+ GPIOD -> OSPEEDR = 0xFFFF0FCF ;
333
+ /* Configure PDx pins Output type to push-pull */
334
+ GPIOD -> OTYPER = 0x00000000 ;
335
+ /* No pull-up, pull-down for PDx pins */
336
+ GPIOD -> PUPDR = 0x00000000 ;
337
+
338
+ /* Connect PEx pins to FSMC Alternate function */
339
+ GPIOE -> AFR [0 ] = 0xC00CC0CC ;
340
+ GPIOE -> AFR [1 ] = 0xCCCCCCCC ;
341
+ /* Configure PEx pins in Alternate function mode */
342
+ GPIOE -> MODER = 0xAAAA828A ;
343
+ /* Configure PEx pins speed to 100 MHz */
344
+ GPIOE -> OSPEEDR = 0xFFFFC3CF ;
345
+ /* Configure PEx pins Output type to push-pull */
346
+ GPIOE -> OTYPER = 0x00000000 ;
347
+ /* No pull-up, pull-down for PEx pins */
348
+ GPIOE -> PUPDR = 0x00000000 ;
349
+
350
+ /* Connect PFx pins to FSMC Alternate function */
351
+ GPIOF -> AFR [0 ] = 0x00CCCCCC ;
352
+ GPIOF -> AFR [1 ] = 0xCCCC0000 ;
353
+ /* Configure PFx pins in Alternate function mode */
354
+ GPIOF -> MODER = 0xAA000AAA ;
355
+ /* Configure PFx pins speed to 100 MHz */
356
+ GPIOF -> OSPEEDR = 0xFF000FFF ;
357
+ /* Configure PFx pins Output type to push-pull */
358
+ GPIOF -> OTYPER = 0x00000000 ;
359
+ /* No pull-up, pull-down for PFx pins */
360
+ GPIOF -> PUPDR = 0x00000000 ;
361
+
362
+ /* Connect PGx pins to FSMC Alternate function */
363
+ GPIOG -> AFR [0 ] = 0x00CCCCCC ;
364
+ GPIOG -> AFR [1 ] = 0x000000C0 ;
365
+ /* Configure PGx pins in Alternate function mode */
366
+ GPIOG -> MODER = 0x00085AAA ;
367
+ /* Configure PGx pins speed to 100 MHz */
368
+ GPIOG -> OSPEEDR = 0x000CAFFF ;
369
+ /* Configure PGx pins Output type to push-pull */
370
+ GPIOG -> OTYPER = 0x00000000 ;
371
+ /* No pull-up, pull-down for PGx pins */
372
+ GPIOG -> PUPDR = 0x00000000 ;
373
+
374
+ /*--FSMC Configuration -------------------------------------------------------*/
375
+ /* Enable the FSMC interface clock */
376
+ RCC -> AHB3ENR |= 0x00000001 ;
377
+
378
+ /* Configure and enable Bank1_SRAM2 */
379
+ FSMC_Bank1 -> BTCR [2 ] = 0x00001011 ;
380
+ FSMC_Bank1 -> BTCR [3 ] = 0x00000201 ;
381
+ FSMC_Bank1E -> BWTR [2 ] = 0x0FFFFFFF ;
323
382
}
324
383
#endif /* DATA_IN_ExtSRAM */
325
384
0 commit comments