]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/ST_Code/Core/Src/system_stm32l4xx.c
Rename STM32Cube to GCC for STM32L4 Discovery projects as GCC is
[freertos] / FreeRTOS / Demo / CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil / ST_Code / Core / Src / system_stm32l4xx.c
1 /**\r
2   ******************************************************************************\r
3   * @file    system_stm32l4xx.c\r
4   * @author  MCD Application Team\r
5   * @brief   CMSIS Cortex-M4 Device Peripheral Access Layer System Source File\r
6   *\r
7   *   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_stm32l4xx.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   *   After each device reset the MSI (4 MHz) is used as system clock source.\r
22   *   Then SystemInit() function is called, in "startup_stm32l4xx.s" file, to\r
23   *   configure the system clock before to branch to main program.\r
24   *\r
25   *   This file configures the system clock as follows:\r
26   *=============================================================================\r
27   *-----------------------------------------------------------------------------\r
28   *        System Clock source                    | MSI\r
29   *-----------------------------------------------------------------------------\r
30   *        SYSCLK(Hz)                             | 4000000\r
31   *-----------------------------------------------------------------------------\r
32   *        HCLK(Hz)                               | 4000000\r
33   *-----------------------------------------------------------------------------\r
34   *        AHB Prescaler                          | 1\r
35   *-----------------------------------------------------------------------------\r
36   *        APB1 Prescaler                         | 1\r
37   *-----------------------------------------------------------------------------\r
38   *        APB2 Prescaler                         | 1\r
39   *-----------------------------------------------------------------------------\r
40   *        PLL_M                                  | 1\r
41   *-----------------------------------------------------------------------------\r
42   *        PLL_N                                  | 8\r
43   *-----------------------------------------------------------------------------\r
44   *        PLL_P                                  | 7\r
45   *-----------------------------------------------------------------------------\r
46   *        PLL_Q                                  | 2\r
47   *-----------------------------------------------------------------------------\r
48   *        PLL_R                                  | 2\r
49   *-----------------------------------------------------------------------------\r
50   *        PLLSAI1_P                              | NA\r
51   *-----------------------------------------------------------------------------\r
52   *        PLLSAI1_Q                              | NA\r
53   *-----------------------------------------------------------------------------\r
54   *        PLLSAI1_R                              | NA\r
55   *-----------------------------------------------------------------------------\r
56   *        PLLSAI2_P                              | NA\r
57   *-----------------------------------------------------------------------------\r
58   *        PLLSAI2_Q                              | NA\r
59   *-----------------------------------------------------------------------------\r
60   *        PLLSAI2_R                              | NA\r
61   *-----------------------------------------------------------------------------\r
62   *        Require 48MHz for USB OTG FS,          | Disabled\r
63   *        SDIO and RNG clock                     |\r
64   *-----------------------------------------------------------------------------\r
65   *=============================================================================\r
66   ******************************************************************************\r
67   * @attention\r
68   *\r
69   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.\r
70   * All rights reserved.</center></h2>\r
71   *\r
72   * This software component is licensed by ST under BSD 3-Clause license,\r
73   * the "License"; You may not use this file except in compliance with the\r
74   * License. You may obtain a copy of the License at:\r
75   *                        opensource.org/licenses/BSD-3-Clause\r
76   *\r
77   ******************************************************************************\r
78   */\r
79 \r
80 /** @addtogroup CMSIS\r
81   * @{\r
82   */\r
83 \r
84 /** @addtogroup stm32l4xx_system\r
85   * @{\r
86   */\r
87 \r
88 /** @addtogroup STM32L4xx_System_Private_Includes\r
89   * @{\r
90   */\r
91 \r
92 #include "stm32l4xx.h"\r
93 \r
94 #if !defined  (HSE_VALUE)\r
95   #define HSE_VALUE    8000000U  /*!< Value of the External oscillator in Hz */\r
96 #endif /* HSE_VALUE */\r
97 \r
98 #if !defined  (MSI_VALUE)\r
99   #define MSI_VALUE    4000000U  /*!< Value of the Internal oscillator in Hz*/\r
100 #endif /* MSI_VALUE */\r
101 \r
102 #if !defined  (HSI_VALUE)\r
103   #define HSI_VALUE    16000000U /*!< Value of the Internal oscillator in Hz*/\r
104 #endif /* HSI_VALUE */\r
105 \r
106 /**\r
107   * @}\r
108   */\r
109 \r
110 /** @addtogroup STM32L4xx_System_Private_TypesDefinitions\r
111   * @{\r
112   */\r
113 \r
114 /**\r
115   * @}\r
116   */\r
117 \r
118 /** @addtogroup STM32L4xx_System_Private_Defines\r
119   * @{\r
120   */\r
121 \r
122 /************************* Miscellaneous Configuration ************************/\r
123 /*!< Uncomment the following line if you need to relocate your vector Table in\r
124      Internal SRAM. */\r
125 /* #define VECT_TAB_SRAM */\r
126 #define VECT_TAB_OFFSET  0x00 /*!< Vector Table base offset field.\r
127                                    This value must be a multiple of 0x200. */\r
128 /******************************************************************************/\r
129 /**\r
130   * @}\r
131   */\r
132 \r
133 /** @addtogroup STM32L4xx_System_Private_Macros\r
134   * @{\r
135   */\r
136 \r
137 /**\r
138   * @}\r
139   */\r
140 \r
141 /** @addtogroup STM32L4xx_System_Private_Variables\r
142   * @{\r
143   */\r
144   /* The SystemCoreClock variable is updated in three ways:\r
145       1) by calling CMSIS function SystemCoreClockUpdate()\r
146       2) by calling HAL API function HAL_RCC_GetHCLKFreq()\r
147       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency\r
148          Note: If you use this function to configure the system clock; then there\r
149                is no need to call the 2 first functions listed above, since SystemCoreClock\r
150                variable is updated automatically.\r
151   */\r
152   uint32_t SystemCoreClock = 4000000U;\r
153 \r
154   const uint8_t  AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};\r
155   const uint8_t  APBPrescTable[8] =  {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};\r
156   const uint32_t MSIRangeTable[12] = {100000U,   200000U,   400000U,   800000U,  1000000U,  2000000U, \\r
157                                       4000000U, 8000000U, 16000000U, 24000000U, 32000000U, 48000000U};\r
158 /**\r
159   * @}\r
160   */\r
161 \r
162 /** @addtogroup STM32L4xx_System_Private_FunctionPrototypes\r
163   * @{\r
164   */\r
165 \r
166 /**\r
167   * @}\r
168   */\r
169 \r
170 /** @addtogroup STM32L4xx_System_Private_Functions\r
171   * @{\r
172   */\r
173 \r
174 /**\r
175   * @brief  Setup the microcontroller system.\r
176   * @param  None\r
177   * @retval None\r
178   */\r
179 \r
180 void SystemInit(void)\r
181 {\r
182   /* FPU settings ------------------------------------------------------------*/\r
183   #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)\r
184     SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */\r
185   #endif\r
186 \r
187   /* Reset the RCC clock configuration to the default reset state ------------*/\r
188   /* Set MSION bit */\r
189   RCC->CR |= RCC_CR_MSION;\r
190 \r
191   /* Reset CFGR register */\r
192   RCC->CFGR = 0x00000000U;\r
193 \r
194   /* Reset HSEON, CSSON , HSION, and PLLON bits */\r
195   RCC->CR &= 0xEAF6FFFFU;\r
196 \r
197   /* Reset PLLCFGR register */\r
198   RCC->PLLCFGR = 0x00001000U;\r
199 \r
200   /* Reset HSEBYP bit */\r
201   RCC->CR &= 0xFFFBFFFFU;\r
202 \r
203   /* Disable all interrupts */\r
204   RCC->CIER = 0x00000000U;\r
205 \r
206   /* Configure the Vector Table location add offset address ------------------*/\r
207 #ifdef VECT_TAB_SRAM\r
208   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */\r
209 #else\r
210   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */\r
211 #endif\r
212 }\r
213 \r
214 /**\r
215   * @brief  Update SystemCoreClock variable according to Clock Register Values.\r
216   *         The SystemCoreClock variable contains the core clock (HCLK), it can\r
217   *         be used by the user application to setup the SysTick timer or configure\r
218   *         other parameters.\r
219   *\r
220   * @note   Each time the core clock (HCLK) changes, this function must be called\r
221   *         to update SystemCoreClock variable value. Otherwise, any configuration\r
222   *         based on this variable will be incorrect.\r
223   *\r
224   * @note   - The system frequency computed by this function is not the real\r
225   *           frequency in the chip. It is calculated based on the predefined\r
226   *           constant and the selected clock source:\r
227   *\r
228   *           - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)\r
229   *\r
230   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)\r
231   *\r
232   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)\r
233   *\r
234   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)\r
235   *             or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.\r
236   *\r
237   *         (*) MSI_VALUE is a constant defined in stm32l4xx_hal.h file (default value\r
238   *             4 MHz) but the real value may vary depending on the variations\r
239   *             in voltage and temperature.\r
240   *\r
241   *         (**) HSI_VALUE is a constant defined in stm32l4xx_hal.h file (default value\r
242   *              16 MHz) but the real value may vary depending on the variations\r
243   *              in voltage and temperature.\r
244   *\r
245   *         (***) HSE_VALUE is a constant defined in stm32l4xx_hal.h file (default value\r
246   *              8 MHz), user has to ensure that HSE_VALUE is same as the real\r
247   *              frequency of the crystal used. Otherwise, this function may\r
248   *              have wrong result.\r
249   *\r
250   *         - The result of this function could be not correct when using fractional\r
251   *           value for HSE crystal.\r
252   *\r
253   * @param  None\r
254   * @retval None\r
255   */\r
256 void SystemCoreClockUpdate(void)\r
257 {\r
258   uint32_t tmp = 0U, msirange = 0U, pllvco = 0U, pllr = 2U, pllsource = 0U, pllm = 2U;\r
259 \r
260   /* Get MSI Range frequency--------------------------------------------------*/\r
261   if((RCC->CR & RCC_CR_MSIRGSEL) == RESET)\r
262   { /* MSISRANGE from RCC_CSR applies */\r
263     msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> 8U;\r
264   }\r
265   else\r
266   { /* MSIRANGE from RCC_CR applies */\r
267     msirange = (RCC->CR & RCC_CR_MSIRANGE) >> 4U;\r
268   }\r
269   /*MSI frequency range in HZ*/\r
270   msirange = MSIRangeTable[msirange];\r
271 \r
272   /* Get SYSCLK source -------------------------------------------------------*/\r
273   switch (RCC->CFGR & RCC_CFGR_SWS)\r
274   {\r
275     case 0x00:  /* MSI used as system clock source */\r
276       SystemCoreClock = msirange;\r
277       break;\r
278 \r
279     case 0x04:  /* HSI used as system clock source */\r
280       SystemCoreClock = HSI_VALUE;\r
281       break;\r
282 \r
283     case 0x08:  /* HSE used as system clock source */\r
284       SystemCoreClock = HSE_VALUE;\r
285       break;\r
286 \r
287     case 0x0C:  /* PLL used as system clock  source */\r
288       /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN\r
289          SYSCLK = PLL_VCO / PLLR\r
290          */\r
291       pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);\r
292       pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4U) + 1U ;\r
293 \r
294       switch (pllsource)\r
295       {\r
296         case 0x02:  /* HSI used as PLL clock source */\r
297           pllvco = (HSI_VALUE / pllm);\r
298           break;\r
299 \r
300         case 0x03:  /* HSE used as PLL clock source */\r
301           pllvco = (HSE_VALUE / pllm);\r
302           break;\r
303 \r
304         default:    /* MSI used as PLL clock source */\r
305           pllvco = (msirange / pllm);\r
306           break;\r
307       }\r
308       pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8U);\r
309       pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25U) + 1U) * 2U;\r
310       SystemCoreClock = pllvco/pllr;\r
311       break;\r
312 \r
313     default:\r
314       SystemCoreClock = msirange;\r
315       break;\r
316   }\r
317   /* Compute HCLK clock frequency --------------------------------------------*/\r
318   /* Get HCLK prescaler */\r
319   tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];\r
320   /* HCLK clock frequency */\r
321   SystemCoreClock >>= tmp;\r
322 }\r
323 \r
324 \r
325 /**\r
326   * @}\r
327   */\r
328 \r
329 /**\r
330   * @}\r
331   */\r
332 \r
333 /**\r
334   * @}\r
335   */\r
336 \r
337 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r