]> mj.ucw.cz Git - home-hw.git/blob - Src/system_stm32f0xx.c
36b46525297170296ee24e8ea3075a6761a88d74
[home-hw.git] / Src / system_stm32f0xx.c
1 /**\r
2   ******************************************************************************\r
3   * @file    system_stm32f0xx.c\r
4   * @author  MCD Application Team\r
5   * @brief   CMSIS Cortex-M0 Device Peripheral Access Layer System Source File.\r
6   *\r
7   * 1. This file provides two functions and one global variable to be called from\r
8   *    user application:\r
9   *      - SystemInit(): This function is called at startup just after reset and \r
10   *                      before branch to main program. This call is made inside\r
11   *                      the "startup_stm32f0xx.s" file.\r
12   *\r
13   *      - SystemCoreClock variable: Contains the core clock (HCLK), it can be used\r
14   *                                  by the user application to setup the SysTick\r
15   *                                  timer or configure other parameters.\r
16   *\r
17   *      - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must\r
18   *                                 be called whenever the core clock is changed\r
19   *                                 during program execution.\r
20   *\r
21   * 2. After each device reset the HSI (8 MHz) is used as system clock source.\r
22   *    Then SystemInit() function is called, in "startup_stm32f0xx.s" file, to\r
23   *    configure the system clock before to branch to main program.\r
24   *\r
25   * 3. This file configures the system clock as follows:\r
26   *=============================================================================\r
27   *                         Supported STM32F0xx device\r
28   *-----------------------------------------------------------------------------\r
29   *        System Clock source                    | HSI\r
30   *-----------------------------------------------------------------------------\r
31   *        SYSCLK(Hz)                             | 8000000\r
32   *-----------------------------------------------------------------------------\r
33   *        HCLK(Hz)                               | 8000000\r
34   *-----------------------------------------------------------------------------\r
35   *        AHB Prescaler                          | 1\r
36   *-----------------------------------------------------------------------------\r
37   *        APB1 Prescaler                         | 1\r
38   *-----------------------------------------------------------------------------\r
39   *=============================================================================\r
40   ******************************************************************************\r
41   * @attention\r
42   *\r
43   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>\r
44   *\r
45   * Redistribution and use in source and binary forms, with or without modification,\r
46   * are permitted provided that the following conditions are met:\r
47   *   1. Redistributions of source code must retain the above copyright notice,\r
48   *      this list of conditions and the following disclaimer.\r
49   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
50   *      this list of conditions and the following disclaimer in the documentation\r
51   *      and/or other materials provided with the distribution.\r
52   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
53   *      may be used to endorse or promote products derived from this software\r
54   *      without specific prior written permission.\r
55   *\r
56   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
57   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
58   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
59   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
60   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
61   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
62   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
63   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
64   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
65   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
66   *\r
67   ******************************************************************************\r
68   */\r
69 \r
70 /** @addtogroup CMSIS\r
71   * @{\r
72   */\r
73 \r
74 /** @addtogroup stm32f0xx_system\r
75   * @{\r
76   */\r
77 \r
78 /** @addtogroup STM32F0xx_System_Private_Includes\r
79   * @{\r
80   */\r
81 \r
82 #include "stm32f0xx.h"\r
83 \r
84 /**\r
85   * @}\r
86   */\r
87 \r
88 /** @addtogroup STM32F0xx_System_Private_TypesDefinitions\r
89   * @{\r
90   */\r
91 \r
92 /**\r
93   * @}\r
94   */\r
95 \r
96 /** @addtogroup STM32F0xx_System_Private_Defines\r
97   * @{\r
98   */\r
99 #if !defined  (HSE_VALUE) \r
100   #define HSE_VALUE    ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.\r
101                                                 This value can be provided and adapted by the user application. */\r
102 #endif /* HSE_VALUE */\r
103 \r
104 #if !defined  (HSI_VALUE)\r
105   #define HSI_VALUE    ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.\r
106                                                 This value can be provided and adapted by the user application. */\r
107 #endif /* HSI_VALUE */\r
108 \r
109 #if !defined (HSI48_VALUE)\r
110 #define HSI48_VALUE    ((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz.\r
111                                                  This value can be provided and adapted by the user application. */\r
112 #endif /* HSI48_VALUE */\r
113 /**\r
114   * @}\r
115   */\r
116 \r
117 /** @addtogroup STM32F0xx_System_Private_Macros\r
118   * @{\r
119   */\r
120 \r
121 /**\r
122   * @}\r
123   */\r
124 \r
125 /** @addtogroup STM32F0xx_System_Private_Variables\r
126   * @{\r
127   */\r
128   /* This variable is updated in three ways:\r
129       1) by calling CMSIS function SystemCoreClockUpdate()\r
130       2) by calling HAL API function HAL_RCC_GetHCLKFreq()\r
131       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency\r
132          Note: If you use this function to configure the system clock there is no need to\r
133                call the 2 first functions listed above, since SystemCoreClock variable is \r
134                updated automatically.\r
135   */\r
136 uint32_t SystemCoreClock = 8000000;\r
137 \r
138 const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};\r
139 const uint8_t APBPrescTable[8]  = {0, 0, 0, 0, 1, 2, 3, 4};\r
140 \r
141 /**\r
142   * @}\r
143   */\r
144 \r
145 /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes\r
146   * @{\r
147   */\r
148 \r
149 /**\r
150   * @}\r
151   */\r
152 \r
153 /** @addtogroup STM32F0xx_System_Private_Functions\r
154   * @{\r
155   */\r
156 \r
157 /**\r
158   * @brief  Setup the microcontroller system.\r
159   *         Initialize the default HSI clock source, vector table location and the PLL configuration is reset.\r
160   * @param  None\r
161   * @retval None\r
162   */\r
163 void SystemInit(void)\r
164 {\r
165   /* Reset the RCC clock configuration to the default reset state ------------*/\r
166   /* Set HSION bit */\r
167   RCC->CR |= (uint32_t)0x00000001U;\r
168 \r
169 #if defined (STM32F051x8) || defined (STM32F058x8)\r
170   /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */\r
171   RCC->CFGR &= (uint32_t)0xF8FFB80CU;\r
172 #else\r
173   /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE, MCOSEL[2:0], MCOPRE[2:0] and PLLNODIV bits */\r
174   RCC->CFGR &= (uint32_t)0x08FFB80CU;\r
175 #endif /* STM32F051x8 or STM32F058x8 */\r
176   \r
177   /* Reset HSEON, CSSON and PLLON bits */\r
178   RCC->CR &= (uint32_t)0xFEF6FFFFU;\r
179 \r
180   /* Reset HSEBYP bit */\r
181   RCC->CR &= (uint32_t)0xFFFBFFFFU;\r
182 \r
183   /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */\r
184   RCC->CFGR &= (uint32_t)0xFFC0FFFFU;\r
185 \r
186   /* Reset PREDIV[3:0] bits */\r
187   RCC->CFGR2 &= (uint32_t)0xFFFFFFF0U;\r
188 \r
189 #if defined (STM32F072xB) || defined (STM32F078xx)\r
190   /* Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits */\r
191   RCC->CFGR3 &= (uint32_t)0xFFFCFE2CU;\r
192 #elif defined (STM32F071xB)\r
193   /* Reset USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */\r
194   RCC->CFGR3 &= (uint32_t)0xFFFFCEACU;\r
195 #elif defined (STM32F091xC) || defined (STM32F098xx)\r
196   /* Reset USART3SW[1:0], USART2SW[1:0], USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */\r
197   RCC->CFGR3 &= (uint32_t)0xFFF0FEACU;\r
198 #elif defined (STM32F030x6) || defined (STM32F030x8) || defined (STM32F031x6) || defined (STM32F038xx) || defined (STM32F030xC)\r
199   /* Reset USART1SW[1:0], I2C1SW and ADCSW bits */\r
200   RCC->CFGR3 &= (uint32_t)0xFFFFFEECU;\r
201 #elif defined (STM32F051x8) || defined (STM32F058xx)\r
202   /* Reset USART1SW[1:0], I2C1SW, CECSW and ADCSW bits */\r
203   RCC->CFGR3 &= (uint32_t)0xFFFFFEACU;\r
204 #elif defined (STM32F042x6) || defined (STM32F048xx)\r
205   /* Reset USART1SW[1:0], I2C1SW, CECSW, USBSW and ADCSW bits */\r
206   RCC->CFGR3 &= (uint32_t)0xFFFFFE2CU;\r
207 #elif defined (STM32F070x6) || defined (STM32F070xB)\r
208   /* Reset USART1SW[1:0], I2C1SW, USBSW and ADCSW bits */\r
209   RCC->CFGR3 &= (uint32_t)0xFFFFFE6CU;\r
210   /* Set default USB clock to PLLCLK, since there is no HSI48 */\r
211   RCC->CFGR3 |= (uint32_t)0x00000080U;  \r
212 #else\r
213  #warning "No target selected"\r
214 #endif\r
215 \r
216   /* Reset HSI14 bit */\r
217   RCC->CR2 &= (uint32_t)0xFFFFFFFEU;\r
218 \r
219   /* Disable all interrupts */\r
220   RCC->CIR = 0x00000000U;\r
221 \r
222 }\r
223 \r
224 /**\r
225    * @brief  Update SystemCoreClock variable according to Clock Register Values.\r
226   *         The SystemCoreClock variable contains the core clock (HCLK), it can\r
227   *         be used by the user application to setup the SysTick timer or configure\r
228   *         other parameters.\r
229   *\r
230   * @note   Each time the core clock (HCLK) changes, this function must be called\r
231   *         to update SystemCoreClock variable value. Otherwise, any configuration\r
232   *         based on this variable will be incorrect.\r
233   *\r
234   * @note   - The system frequency computed by this function is not the real\r
235   *           frequency in the chip. It is calculated based on the predefined\r
236   *           constant and the selected clock source:\r
237   *\r
238   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)\r
239   *\r
240   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)\r
241   *\r
242   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)\r
243   *             or HSI_VALUE(*) multiplied/divided by the PLL factors.\r
244   *\r
245   *         (*) HSI_VALUE is a constant defined in stm32f0xx_hal.h file (default value\r
246   *             8 MHz) but the real value may vary depending on the variations\r
247   *             in voltage and temperature.\r
248   *\r
249   *         (**) HSE_VALUE is a constant defined in stm32f0xx_hal.h file (default value\r
250   *              8 MHz), user has to ensure that HSE_VALUE is same as the real\r
251   *              frequency of the crystal used. Otherwise, this function may\r
252   *              have wrong result.\r
253   *\r
254   *         - The result of this function could be not correct when using fractional\r
255   *           value for HSE crystal.\r
256   *\r
257   * @param  None\r
258   * @retval None\r
259   */\r
260 void SystemCoreClockUpdate (void)\r
261 {\r
262   uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;\r
263 \r
264   /* Get SYSCLK source -------------------------------------------------------*/\r
265   tmp = RCC->CFGR & RCC_CFGR_SWS;\r
266 \r
267   switch (tmp)\r
268   {\r
269     case RCC_CFGR_SWS_HSI:  /* HSI used as system clock */\r
270       SystemCoreClock = HSI_VALUE;\r
271       break;\r
272     case RCC_CFGR_SWS_HSE:  /* HSE used as system clock */\r
273       SystemCoreClock = HSE_VALUE;\r
274       break;\r
275     case RCC_CFGR_SWS_PLL:  /* PLL used as system clock */\r
276       /* Get PLL clock source and multiplication factor ----------------------*/\r
277       pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;\r
278       pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;\r
279       pllmull = ( pllmull >> 18) + 2;\r
280       predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;\r
281 \r
282       if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)\r
283       {\r
284         /* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */\r
285         SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull;\r
286       }\r
287 #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx)\r
288       else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV)\r
289       {\r
290         /* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */\r
291         SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull;\r
292       }\r
293 #endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx */\r
294       else\r
295       {\r
296 #if defined(STM32F042x6) || defined(STM32F048xx)  || defined(STM32F070x6) \\r
297  || defined(STM32F078xx) || defined(STM32F071xB)  || defined(STM32F072xB) \\r
298  || defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx)  || defined(STM32F030xC)\r
299         /* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */\r
300         SystemCoreClock = (HSI_VALUE/predivfactor) * pllmull;\r
301 #else\r
302         /* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */\r
303         SystemCoreClock = (HSI_VALUE >> 1) * pllmull;\r
304 #endif /* STM32F042x6 || STM32F048xx || STM32F070x6 || \r
305           STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB ||\r
306           STM32F091xC || STM32F098xx || STM32F030xC */\r
307       }\r
308       break;\r
309     default: /* HSI used as system clock */\r
310       SystemCoreClock = HSI_VALUE;\r
311       break;\r
312   }\r
313   /* Compute HCLK clock frequency ----------------*/\r
314   /* Get HCLK prescaler */\r
315   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];\r
316   /* HCLK clock frequency */\r
317   SystemCoreClock >>= tmp;\r
318 }\r
319 \r
320 /**\r
321   * @}\r
322   */\r
323 \r
324 /**\r
325   * @}\r
326   */\r
327 \r
328 /**\r
329   * @}\r
330   */\r
331 \r
332 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r
333 \r