Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file system_stm32f1xx.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File. |
||
6 | * |
||
7 | * 1. This file provides two functions and one global variable to be called from |
||
8 | * user application: |
||
9 | * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier |
||
10 | * factors, AHB/APBx prescalers and Flash settings). |
||
11 | * This function is called at startup just after reset and |
||
12 | * before branch to main program. This call is made inside |
||
13 | * the "startup_stm32f1xx_xx.s" file. |
||
14 | * |
||
15 | * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
||
16 | * by the user application to setup the SysTick |
||
17 | * timer or configure other parameters. |
||
18 | * |
||
19 | * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
||
20 | * be called whenever the core clock is changed |
||
21 | * during program execution. |
||
22 | * |
||
23 | * 2. After each device reset the HSI (8 MHz) is used as system clock source. |
||
24 | * Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to |
||
25 | * configure the system clock before to branch to main program. |
||
26 | * |
||
27 | * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on |
||
28 | * the product used), refer to "HSE_VALUE". |
||
29 | * When HSE is used as system clock source, directly or through PLL, and you |
||
30 | * are using different crystal you have to adapt the HSE value to your own |
||
31 | * configuration. |
||
32 | * |
||
33 | ****************************************************************************** |
||
34 | * @attention |
||
35 | * |
||
36 | * <h2><center>© Copyright (c) 2017 STMicroelectronics. |
||
37 | * All rights reserved.</center></h2> |
||
38 | * |
||
39 | * This software component is licensed by ST under BSD 3-Clause license, |
||
40 | * the "License"; You may not use this file except in compliance with the |
||
41 | * License. You may obtain a copy of the License at: |
||
42 | * opensource.org/licenses/BSD-3-Clause |
||
43 | * |
||
44 | ****************************************************************************** |
||
45 | */ |
||
46 | |||
47 | /** @addtogroup CMSIS |
||
48 | * @{ |
||
49 | */ |
||
50 | |||
51 | /** @addtogroup stm32f1xx_system |
||
52 | * @{ |
||
53 | */ |
||
54 | |||
55 | /** @addtogroup STM32F1xx_System_Private_Includes |
||
56 | * @{ |
||
57 | */ |
||
58 | |||
59 | #include "stm32f1xx.h" |
||
60 | |||
61 | /** |
||
62 | * @} |
||
63 | */ |
||
64 | |||
65 | /** @addtogroup STM32F1xx_System_Private_TypesDefinitions |
||
66 | * @{ |
||
67 | */ |
||
68 | |||
69 | /** |
||
70 | * @} |
||
71 | */ |
||
72 | |||
73 | /** @addtogroup STM32F1xx_System_Private_Defines |
||
74 | * @{ |
||
75 | */ |
||
76 | |||
77 | #if !defined (HSE_VALUE) |
||
78 | #define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz. |
||
79 | This value can be provided and adapted by the user application. */ |
||
80 | #endif /* HSE_VALUE */ |
||
81 | |||
82 | #if !defined (HSI_VALUE) |
||
83 | #define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz. |
||
84 | This value can be provided and adapted by the user application. */ |
||
85 | #endif /* HSI_VALUE */ |
||
86 | |||
87 | /*!< Uncomment the following line if you need to use external SRAM */ |
||
88 | #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) |
||
89 | /* #define DATA_IN_ExtSRAM */ |
||
90 | #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */ |
||
91 | |||
92 | /*!< Uncomment the following line if you need to relocate your vector Table in |
||
93 | Internal SRAM. */ |
||
94 | /* #define VECT_TAB_SRAM */ |
||
95 | #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. |
||
96 | This value must be a multiple of 0x200. */ |
||
97 | |||
98 | |||
99 | /** |
||
100 | * @} |
||
101 | */ |
||
102 | |||
103 | /** @addtogroup STM32F1xx_System_Private_Macros |
||
104 | * @{ |
||
105 | */ |
||
106 | |||
107 | /** |
||
108 | * @} |
||
109 | */ |
||
110 | |||
111 | /** @addtogroup STM32F1xx_System_Private_Variables |
||
112 | * @{ |
||
113 | */ |
||
114 | |||
115 | /* This variable is updated in three ways: |
||
116 | 1) by calling CMSIS function SystemCoreClockUpdate() |
||
117 | 2) by calling HAL API function HAL_RCC_GetHCLKFreq() |
||
118 | 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency |
||
119 | Note: If you use this function to configure the system clock; then there |
||
120 | is no need to call the 2 first functions listed above, since SystemCoreClock |
||
121 | variable is updated automatically. |
||
122 | */ |
||
123 | uint32_t SystemCoreClock = 16000000; |
||
124 | const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; |
||
125 | const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4}; |
||
126 | |||
127 | /** |
||
128 | * @} |
||
129 | */ |
||
130 | |||
131 | /** @addtogroup STM32F1xx_System_Private_FunctionPrototypes |
||
132 | * @{ |
||
133 | */ |
||
134 | |||
135 | #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) |
||
136 | #ifdef DATA_IN_ExtSRAM |
||
137 | static void SystemInit_ExtMemCtl(void); |
||
138 | #endif /* DATA_IN_ExtSRAM */ |
||
139 | #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */ |
||
140 | |||
141 | /** |
||
142 | * @} |
||
143 | */ |
||
144 | |||
145 | /** @addtogroup STM32F1xx_System_Private_Functions |
||
146 | * @{ |
||
147 | */ |
||
148 | |||
149 | /** |
||
150 | * @brief Setup the microcontroller system |
||
151 | * Initialize the Embedded Flash Interface, the PLL and update the |
||
152 | * SystemCoreClock variable. |
||
153 | * @note This function should be used only after reset. |
||
154 | * @param None |
||
155 | * @retval None |
||
156 | */ |
||
157 | void SystemInit (void) |
||
158 | { |
||
159 | /* Reset the RCC clock configuration to the default reset state(for debug purpose) */ |
||
160 | /* Set HSION bit */ |
||
161 | RCC->CR |= 0x00000001U; |
||
162 | |||
163 | /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */ |
||
164 | #if !defined(STM32F105xC) && !defined(STM32F107xC) |
||
165 | RCC->CFGR &= 0xF8FF0000U; |
||
166 | #else |
||
167 | RCC->CFGR &= 0xF0FF0000U; |
||
168 | #endif /* STM32F105xC */ |
||
169 | |||
170 | /* Reset HSEON, CSSON and PLLON bits */ |
||
171 | RCC->CR &= 0xFEF6FFFFU; |
||
172 | |||
173 | /* Reset HSEBYP bit */ |
||
174 | RCC->CR &= 0xFFFBFFFFU; |
||
175 | |||
176 | /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */ |
||
177 | RCC->CFGR &= 0xFF80FFFFU; |
||
178 | |||
179 | #if defined(STM32F105xC) || defined(STM32F107xC) |
||
180 | /* Reset PLL2ON and PLL3ON bits */ |
||
181 | RCC->CR &= 0xEBFFFFFFU; |
||
182 | |||
183 | /* Disable all interrupts and clear pending bits */ |
||
184 | RCC->CIR = 0x00FF0000U; |
||
185 | |||
186 | /* Reset CFGR2 register */ |
||
187 | RCC->CFGR2 = 0x00000000U; |
||
188 | #elif defined(STM32F100xB) || defined(STM32F100xE) |
||
189 | /* Disable all interrupts and clear pending bits */ |
||
190 | RCC->CIR = 0x009F0000U; |
||
191 | |||
192 | /* Reset CFGR2 register */ |
||
193 | RCC->CFGR2 = 0x00000000U; |
||
194 | #else |
||
195 | /* Disable all interrupts and clear pending bits */ |
||
196 | RCC->CIR = 0x009F0000U; |
||
197 | #endif /* STM32F105xC */ |
||
198 | |||
199 | #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) |
||
200 | #ifdef DATA_IN_ExtSRAM |
||
201 | SystemInit_ExtMemCtl(); |
||
202 | #endif /* DATA_IN_ExtSRAM */ |
||
203 | #endif |
||
204 | |||
205 | #ifdef VECT_TAB_SRAM |
||
206 | SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ |
||
207 | #else |
||
208 | SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ |
||
209 | #endif |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @brief Update SystemCoreClock variable according to Clock Register Values. |
||
214 | * The SystemCoreClock variable contains the core clock (HCLK), it can |
||
215 | * be used by the user application to setup the SysTick timer or configure |
||
216 | * other parameters. |
||
217 | * |
||
218 | * @note Each time the core clock (HCLK) changes, this function must be called |
||
219 | * to update SystemCoreClock variable value. Otherwise, any configuration |
||
220 | * based on this variable will be incorrect. |
||
221 | * |
||
222 | * @note - The system frequency computed by this function is not the real |
||
223 | * frequency in the chip. It is calculated based on the predefined |
||
224 | * constant and the selected clock source: |
||
225 | * |
||
226 | * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) |
||
227 | * |
||
228 | * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) |
||
229 | * |
||
230 | * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) |
||
231 | * or HSI_VALUE(*) multiplied by the PLL factors. |
||
232 | * |
||
233 | * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value |
||
234 | * 8 MHz) but the real value may vary depending on the variations |
||
235 | * in voltage and temperature. |
||
236 | * |
||
237 | * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value |
||
238 | * 8 MHz or 25 MHz, depending on the product used), user has to ensure |
||
239 | * that HSE_VALUE is same as the real frequency of the crystal used. |
||
240 | * Otherwise, this function may have wrong result. |
||
241 | * |
||
242 | * - The result of this function could be not correct when using fractional |
||
243 | * value for HSE crystal. |
||
244 | * @param None |
||
245 | * @retval None |
||
246 | */ |
||
247 | void SystemCoreClockUpdate (void) |
||
248 | { |
||
249 | uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U; |
||
250 | |||
251 | #if defined(STM32F105xC) || defined(STM32F107xC) |
||
252 | uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U; |
||
253 | #endif /* STM32F105xC */ |
||
254 | |||
255 | #if defined(STM32F100xB) || defined(STM32F100xE) |
||
256 | uint32_t prediv1factor = 0U; |
||
257 | #endif /* STM32F100xB or STM32F100xE */ |
||
258 | |||
259 | /* Get SYSCLK source -------------------------------------------------------*/ |
||
260 | tmp = RCC->CFGR & RCC_CFGR_SWS; |
||
261 | |||
262 | switch (tmp) |
||
263 | { |
||
264 | case 0x00U: /* HSI used as system clock */ |
||
265 | SystemCoreClock = HSI_VALUE; |
||
266 | break; |
||
267 | case 0x04U: /* HSE used as system clock */ |
||
268 | SystemCoreClock = HSE_VALUE; |
||
269 | break; |
||
270 | case 0x08U: /* PLL used as system clock */ |
||
271 | |||
272 | /* Get PLL clock source and multiplication factor ----------------------*/ |
||
273 | pllmull = RCC->CFGR & RCC_CFGR_PLLMULL; |
||
274 | pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; |
||
275 | |||
276 | #if !defined(STM32F105xC) && !defined(STM32F107xC) |
||
277 | pllmull = ( pllmull >> 18U) + 2U; |
||
278 | |||
279 | if (pllsource == 0x00U) |
||
280 | { |
||
281 | /* HSI oscillator clock divided by 2 selected as PLL clock entry */ |
||
282 | SystemCoreClock = (HSI_VALUE >> 1U) * pllmull; |
||
283 | } |
||
284 | else |
||
285 | { |
||
286 | #if defined(STM32F100xB) || defined(STM32F100xE) |
||
287 | prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U; |
||
288 | /* HSE oscillator clock selected as PREDIV1 clock entry */ |
||
289 | SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; |
||
290 | #else |
||
291 | /* HSE selected as PLL clock entry */ |
||
292 | if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET) |
||
293 | {/* HSE oscillator clock divided by 2 */ |
||
294 | SystemCoreClock = (HSE_VALUE >> 1U) * pllmull; |
||
295 | } |
||
296 | else |
||
297 | { |
||
298 | SystemCoreClock = HSE_VALUE * pllmull; |
||
299 | } |
||
300 | #endif |
||
301 | } |
||
302 | #else |
||
303 | pllmull = pllmull >> 18U; |
||
304 | |||
305 | if (pllmull != 0x0DU) |
||
306 | { |
||
307 | pllmull += 2U; |
||
308 | } |
||
309 | else |
||
310 | { /* PLL multiplication factor = PLL input clock * 6.5 */ |
||
311 | pllmull = 13U / 2U; |
||
312 | } |
||
313 | |||
314 | if (pllsource == 0x00U) |
||
315 | { |
||
316 | /* HSI oscillator clock divided by 2 selected as PLL clock entry */ |
||
317 | SystemCoreClock = (HSI_VALUE >> 1U) * pllmull; |
||
318 | } |
||
319 | else |
||
320 | {/* PREDIV1 selected as PLL clock entry */ |
||
321 | |||
322 | /* Get PREDIV1 clock source and division factor */ |
||
323 | prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC; |
||
324 | prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U; |
||
325 | |||
326 | if (prediv1source == 0U) |
||
327 | { |
||
328 | /* HSE oscillator clock selected as PREDIV1 clock entry */ |
||
329 | SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; |
||
330 | } |
||
331 | else |
||
332 | {/* PLL2 clock selected as PREDIV1 clock entry */ |
||
333 | |||
334 | /* Get PREDIV2 division factor and PLL2 multiplication factor */ |
||
335 | prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U; |
||
336 | pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U; |
||
337 | SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull; |
||
338 | } |
||
339 | } |
||
340 | #endif /* STM32F105xC */ |
||
341 | break; |
||
342 | |||
343 | default: |
||
344 | SystemCoreClock = HSI_VALUE; |
||
345 | break; |
||
346 | } |
||
347 | |||
348 | /* Compute HCLK clock frequency ----------------*/ |
||
349 | /* Get HCLK prescaler */ |
||
350 | tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)]; |
||
351 | /* HCLK clock frequency */ |
||
352 | SystemCoreClock >>= tmp; |
||
353 | } |
||
354 | |||
355 | #if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) |
||
356 | /** |
||
357 | * @brief Setup the external memory controller. Called in startup_stm32f1xx.s |
||
358 | * before jump to __main |
||
359 | * @param None |
||
360 | * @retval None |
||
361 | */ |
||
362 | #ifdef DATA_IN_ExtSRAM |
||
363 | /** |
||
364 | * @brief Setup the external memory controller. |
||
365 | * Called in startup_stm32f1xx_xx.s/.c before jump to main. |
||
366 | * This function configures the external SRAM mounted on STM3210E-EVAL |
||
367 | * board (STM32 High density devices). This SRAM will be used as program |
||
368 | * data memory (including heap and stack). |
||
369 | * @param None |
||
370 | * @retval None |
||
371 | */ |
||
372 | void SystemInit_ExtMemCtl(void) |
||
373 | { |
||
374 | __IO uint32_t tmpreg; |
||
375 | /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is |
||
376 | required, then adjust the Register Addresses */ |
||
377 | |||
378 | /* Enable FSMC clock */ |
||
379 | RCC->AHBENR = 0x00000114U; |
||
380 | |||
381 | /* Delay after an RCC peripheral clock enabling */ |
||
382 | tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN); |
||
383 | |||
384 | /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */ |
||
385 | RCC->APB2ENR = 0x000001E0U; |
||
386 | |||
387 | /* Delay after an RCC peripheral clock enabling */ |
||
388 | tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN); |
||
389 | |||
390 | (void)(tmpreg); |
||
391 | |||
392 | /* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/ |
||
393 | /*---------------- SRAM Address lines configuration -------------------------*/ |
||
394 | /*---------------- NOE and NWE configuration --------------------------------*/ |
||
395 | /*---------------- NE3 configuration ----------------------------------------*/ |
||
396 | /*---------------- NBL0, NBL1 configuration ---------------------------------*/ |
||
397 | |||
398 | GPIOD->CRL = 0x44BB44BBU; |
||
399 | GPIOD->CRH = 0xBBBBBBBBU; |
||
400 | |||
401 | GPIOE->CRL = 0xB44444BBU; |
||
402 | GPIOE->CRH = 0xBBBBBBBBU; |
||
403 | |||
404 | GPIOF->CRL = 0x44BBBBBBU; |
||
405 | GPIOF->CRH = 0xBBBB4444U; |
||
406 | |||
407 | GPIOG->CRL = 0x44BBBBBBU; |
||
408 | GPIOG->CRH = 0x444B4B44U; |
||
409 | |||
410 | /*---------------- FSMC Configuration ---------------------------------------*/ |
||
411 | /*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/ |
||
412 | |||
413 | FSMC_Bank1->BTCR[4U] = 0x00001091U; |
||
414 | FSMC_Bank1->BTCR[5U] = 0x00110212U; |
||
415 | } |
||
416 | #endif /* DATA_IN_ExtSRAM */ |
||
417 | #endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */ |
||
418 | |||
419 | /** |
||
420 | * @} |
||
421 | */ |
||
422 | |||
423 | /** |
||
424 | * @} |
||
425 | */ |
||
426 | |||
427 | /** |
||
428 | * @} |
||
429 | */ |
||
430 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |