Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /** |
2 | ****************************************************************************** |
||
3 | * @file system_stm32f0xx.c |
||
4 | * @author MCD Application Team |
||
5 | * @brief CMSIS Cortex-M0 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(): This function is called at startup just after reset and |
||
10 | * before branch to main program. This call is made inside |
||
11 | * the "startup_stm32f0xx.s" file. |
||
12 | * |
||
13 | * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used |
||
14 | * by the user application to setup the SysTick |
||
15 | * timer or configure other parameters. |
||
16 | * |
||
17 | * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must |
||
18 | * be called whenever the core clock is changed |
||
19 | * during program execution. |
||
20 | * |
||
21 | * |
||
22 | ****************************************************************************** |
||
23 | * @attention |
||
24 | * |
||
25 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
26 | * All rights reserved.</center></h2> |
||
27 | * |
||
28 | * This software component is licensed by ST under BSD 3-Clause license, |
||
29 | * the "License"; You may not use this file except in compliance with the |
||
30 | * License. You may obtain a copy of the License at: |
||
31 | * opensource.org/licenses/BSD-3-Clause |
||
32 | * |
||
33 | ****************************************************************************** |
||
34 | */ |
||
35 | |||
36 | /** @addtogroup CMSIS |
||
37 | * @{ |
||
38 | */ |
||
39 | |||
40 | /** @addtogroup stm32f0xx_system |
||
41 | * @{ |
||
42 | */ |
||
43 | |||
44 | /** @addtogroup STM32F0xx_System_Private_Includes |
||
45 | * @{ |
||
46 | */ |
||
47 | |||
48 | #include "stm32f0xx.h" |
||
49 | |||
50 | /** |
||
51 | * @} |
||
52 | */ |
||
53 | |||
54 | /** @addtogroup STM32F0xx_System_Private_TypesDefinitions |
||
55 | * @{ |
||
56 | */ |
||
57 | |||
58 | /** |
||
59 | * @} |
||
60 | */ |
||
61 | |||
62 | /** @addtogroup STM32F0xx_System_Private_Defines |
||
63 | * @{ |
||
64 | */ |
||
65 | #if !defined (HSE_VALUE) |
||
66 | #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz. |
||
67 | This value can be provided and adapted by the user application. */ |
||
68 | #endif /* HSE_VALUE */ |
||
69 | |||
70 | #if !defined (HSI_VALUE) |
||
71 | #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz. |
||
72 | This value can be provided and adapted by the user application. */ |
||
73 | #endif /* HSI_VALUE */ |
||
74 | |||
75 | #if !defined (HSI48_VALUE) |
||
76 | #define HSI48_VALUE ((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz. |
||
77 | This value can be provided and adapted by the user application. */ |
||
78 | #endif /* HSI48_VALUE */ |
||
79 | /** |
||
80 | * @} |
||
81 | */ |
||
82 | |||
83 | /** @addtogroup STM32F0xx_System_Private_Macros |
||
84 | * @{ |
||
85 | */ |
||
86 | |||
87 | /** |
||
88 | * @} |
||
89 | */ |
||
90 | |||
91 | /** @addtogroup STM32F0xx_System_Private_Variables |
||
92 | * @{ |
||
93 | */ |
||
94 | /* This variable is updated in three ways: |
||
95 | 1) by calling CMSIS function SystemCoreClockUpdate() |
||
96 | 2) by calling HAL API function HAL_RCC_GetHCLKFreq() |
||
97 | 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency |
||
98 | Note: If you use this function to configure the system clock; then there |
||
99 | is no need to call the 2 first functions listed above, since SystemCoreClock |
||
100 | variable is updated automatically. |
||
101 | */ |
||
102 | uint32_t SystemCoreClock = 8000000; |
||
103 | |||
104 | const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; |
||
105 | const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4}; |
||
106 | |||
107 | /** |
||
108 | * @} |
||
109 | */ |
||
110 | |||
111 | /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes |
||
112 | * @{ |
||
113 | */ |
||
114 | |||
115 | /** |
||
116 | * @} |
||
117 | */ |
||
118 | |||
119 | /** @addtogroup STM32F0xx_System_Private_Functions |
||
120 | * @{ |
||
121 | */ |
||
122 | |||
123 | /** |
||
124 | * @brief Setup the microcontroller system |
||
125 | * @param None |
||
126 | * @retval None |
||
127 | */ |
||
128 | void SystemInit(void) |
||
129 | { |
||
130 | /* NOTE :SystemInit(): This function is called at startup just after reset and |
||
131 | before branch to main program. This call is made inside |
||
132 | the "startup_stm32f0xx.s" file. |
||
133 | User can setups the default system clock (System clock source, PLL Multiplier |
||
134 | and Divider factors, AHB/APBx prescalers and Flash settings). |
||
135 | */ |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @brief Update SystemCoreClock variable according to Clock Register Values. |
||
140 | * The SystemCoreClock variable contains the core clock (HCLK), it can |
||
141 | * be used by the user application to setup the SysTick timer or configure |
||
142 | * other parameters. |
||
143 | * |
||
144 | * @note Each time the core clock (HCLK) changes, this function must be called |
||
145 | * to update SystemCoreClock variable value. Otherwise, any configuration |
||
146 | * based on this variable will be incorrect. |
||
147 | * |
||
148 | * @note - The system frequency computed by this function is not the real |
||
149 | * frequency in the chip. It is calculated based on the predefined |
||
150 | * constant and the selected clock source: |
||
151 | * |
||
152 | * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) |
||
153 | * |
||
154 | * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) |
||
155 | * |
||
156 | * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) |
||
157 | * or HSI_VALUE(*) multiplied/divided by the PLL factors. |
||
158 | * |
||
159 | * (*) HSI_VALUE is a constant defined in stm32f0xx_hal_conf.h file (default value |
||
160 | * 8 MHz) but the real value may vary depending on the variations |
||
161 | * in voltage and temperature. |
||
162 | * |
||
163 | * (**) HSE_VALUE is a constant defined in stm32f0xx_hal_conf.h file (its value |
||
164 | * depends on the application requirements), user has to ensure that HSE_VALUE |
||
165 | * is same as the real frequency of the crystal used. Otherwise, this function |
||
166 | * may have wrong result. |
||
167 | * |
||
168 | * - The result of this function could be not correct when using fractional |
||
169 | * value for HSE crystal. |
||
170 | * |
||
171 | * @param None |
||
172 | * @retval None |
||
173 | */ |
||
174 | void SystemCoreClockUpdate (void) |
||
175 | { |
||
176 | uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0; |
||
177 | |||
178 | /* Get SYSCLK source -------------------------------------------------------*/ |
||
179 | tmp = RCC->CFGR & RCC_CFGR_SWS; |
||
180 | |||
181 | switch (tmp) |
||
182 | { |
||
183 | case RCC_CFGR_SWS_HSI: /* HSI used as system clock */ |
||
184 | SystemCoreClock = HSI_VALUE; |
||
185 | break; |
||
186 | case RCC_CFGR_SWS_HSE: /* HSE used as system clock */ |
||
187 | SystemCoreClock = HSE_VALUE; |
||
188 | break; |
||
189 | case RCC_CFGR_SWS_PLL: /* PLL used as system clock */ |
||
190 | /* Get PLL clock source and multiplication factor ----------------------*/ |
||
191 | pllmull = RCC->CFGR & RCC_CFGR_PLLMUL; |
||
192 | pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; |
||
193 | pllmull = ( pllmull >> 18) + 2; |
||
194 | predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1; |
||
195 | |||
196 | if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV) |
||
197 | { |
||
198 | /* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */ |
||
199 | SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull; |
||
200 | } |
||
201 | #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx) |
||
202 | else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV) |
||
203 | { |
||
204 | /* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */ |
||
205 | SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull; |
||
206 | } |
||
207 | #endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx */ |
||
208 | else |
||
209 | { |
||
210 | #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6) \ |
||
211 | || defined(STM32F078xx) || defined(STM32F071xB) || defined(STM32F072xB) \ |
||
212 | || defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC) |
||
213 | /* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */ |
||
214 | SystemCoreClock = (HSI_VALUE/predivfactor) * pllmull; |
||
215 | #else |
||
216 | /* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */ |
||
217 | SystemCoreClock = (HSI_VALUE >> 1) * pllmull; |
||
218 | #endif /* STM32F042x6 || STM32F048xx || STM32F070x6 || |
||
219 | STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB || |
||
220 | STM32F091xC || STM32F098xx || STM32F030xC */ |
||
221 | } |
||
222 | break; |
||
223 | default: /* HSI used as system clock */ |
||
224 | SystemCoreClock = HSI_VALUE; |
||
225 | break; |
||
226 | } |
||
227 | /* Compute HCLK clock frequency ----------------*/ |
||
228 | /* Get HCLK prescaler */ |
||
229 | tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; |
||
230 | /* HCLK clock frequency */ |
||
231 | SystemCoreClock >>= tmp; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @} |
||
236 | */ |
||
237 | |||
238 | /** |
||
239 | * @} |
||
240 | */ |
||
241 | |||
242 | /** |
||
243 | * @} |
||
244 | */ |
||
245 | |||
246 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
||
247 |