1 /**
2   ******************************************************************************
3   * @file    system_stm32u5xx.c
4   * @author  MCD Application Team
5   * @brief   CMSIS Cortex-M33 Device Peripheral Access Layer System Source File
6   *
7   *   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_stm32u5xx.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   *   After each device reset the MSI (4 MHz) is used as system clock source.
22   *   Then SystemInit() function is called, in "startup_stm32u5xx.s" file, to
23   *   configure the system clock before to branch to main program.
24   *
25   *   This file configures the system clock as follows:
26   *=============================================================================
27   *-----------------------------------------------------------------------------
28   *        System Clock source                     | MSI
29   *-----------------------------------------------------------------------------
30   *        SYSCLK(Hz)                              | 4000000
31   *-----------------------------------------------------------------------------
32   *        HCLK(Hz)                                | 4000000
33   *-----------------------------------------------------------------------------
34   *        AHB Prescaler                           | 1
35   *-----------------------------------------------------------------------------
36   *        APB1 Prescaler                          | 1
37   *-----------------------------------------------------------------------------
38   *        APB2 Prescaler                          | 1
39   *-----------------------------------------------------------------------------
40   *        APB3 Prescaler                          | 1
41   *-----------------------------------------------------------------------------
42   *        PLL1_SRC                                | No clock
43   *-----------------------------------------------------------------------------
44   *        PLL1_M                                  | 1
45   *-----------------------------------------------------------------------------
46   *        PLL1_N                                  | 8
47   *-----------------------------------------------------------------------------
48   *        PLL1_P                                  | 7
49   *-----------------------------------------------------------------------------
50   *        PLL1_Q                                  | 2
51   *-----------------------------------------------------------------------------
52   *        PLL1_R                                  | 2
53   *-----------------------------------------------------------------------------
54   *        PLL2_SRC                                | NA
55   *-----------------------------------------------------------------------------
56   *        PLL2_M                                  | NA
57   *-----------------------------------------------------------------------------
58   *        PLL2_N                                  | NA
59   *-----------------------------------------------------------------------------
60   *        PLL2_P                                  | NA
61   *-----------------------------------------------------------------------------
62   *        PLL2_Q                                  | NA
63   *-----------------------------------------------------------------------------
64   *        PLL2_R                                  | NA
65   *-----------------------------------------------------------------------------
66   *        PLL3_SRC                                | NA
67   *-----------------------------------------------------------------------------
68   *        PLL3_M                                  | NA
69   *-----------------------------------------------------------------------------
70   *        PLL3_N                                  | NA
71   *-----------------------------------------------------------------------------
72   *        PLL3_P                                  | NA
73   *-----------------------------------------------------------------------------
74   *        Require 48MHz for USB FS,               | Disabled
75   *        SDIO and RNG clock                      |
76   *-----------------------------------------------------------------------------
77   *=============================================================================
78   ******************************************************************************
79   * @attention
80   *
81   * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
82   * All rights reserved.</center></h2>
83   *
84   * This software component is licensed by ST under BSD 3-Clause license,
85   * the "License"; You may not use this file except in compliance with the
86   * License. You may obtain a copy of the License at:
87   *                        opensource.org/licenses/BSD-3-Clause
88   *
89   ******************************************************************************
90   */
91 
92 /** @addtogroup CMSIS
93   * @{
94   */
95 
96 /** @addtogroup STM32U5xx_system
97   * @{
98   */
99 
100 /** @addtogroup STM32U5xx_System_Private_Includes
101   * @{
102   */
103 
104 #include "stm32u5xx.h"
105 #include <math.h>
106 
107 /**
108   * @}
109   */
110 
111 /** @addtogroup STM32U5xx_System_Private_TypesDefinitions
112   * @{
113   */
114 
115 /**
116   * @}
117   */
118 
119 /** @addtogroup STM32U5xx_System_Private_Defines
120   * @{
121   */
122 
123 #if !defined  (HSE_VALUE)
124   #define HSE_VALUE    16000000U /*!< Value of the External oscillator in Hz */
125 #endif /* HSE_VALUE */
126 
127 #if !defined  (MSI_VALUE)
128   #define MSI_VALUE    4000000U  /*!< Value of the Internal oscillator in Hz*/
129 #endif /* MSI_VALUE */
130 
131 #if !defined  (HSI_VALUE)
132   #define HSI_VALUE    16000000U /*!< Value of the Internal oscillator in Hz*/
133 #endif /* HSI_VALUE */
134 
135 /************************* Miscellaneous Configuration ************************/
136 /*!< Uncomment the following line if you need to relocate your vector Table in
137      Internal SRAM. */
138 /* #define VECT_TAB_SRAM */
139 #define VECT_TAB_OFFSET  0x00000000UL /*!< Vector Table base offset field.
140                                    This value must be a multiple of 0x200. */
141 /******************************************************************************/
142 
143 /**
144   * @}
145   */
146 
147 /** @addtogroup STM32U5xx_System_Private_Macros
148   * @{
149   */
150 
151 /**
152   * @}
153   */
154 
155 /** @addtogroup STM32U5xx_System_Private_Variables
156   * @{
157   */
158   /* The SystemCoreClock variable is updated in three ways:
159       1) by calling CMSIS function SystemCoreClockUpdate()
160       2) by calling HAL API function HAL_RCC_GetHCLKFreq()
161       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
162          Note: If you use this function to configure the system clock; then there
163                is no need to call the 2 first functions listed above, since SystemCoreClock
164                variable is updated automatically.
165   */
166   uint32_t SystemCoreClock = 4000000U;
167 
168   const uint8_t  AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
169   const uint8_t  APBPrescTable[8] =  {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
170   const uint32_t MSIRangeTable[16] = {48000000U,24000000U,16000000U,12000000U, 4000000U, 2000000U, 1500000U,\
171                                       1000000U, 3072000U, 1536000U,1024000U, 768000U, 400000U, 200000U, 150000U, 100000U};
172 /**
173   * @}
174   */
175 
176 /** @addtogroup STM32U5xx_System_Private_FunctionPrototypes
177   * @{
178   */
179 
180 /**
181   * @}
182   */
183 
184 /** @addtogroup STM32U5xx_System_Private_Functions
185   * @{
186   */
187 
188 /**
189   * @brief  Setup the microcontroller system.
190   * @param  None
191   * @retval None
192   */
193 
SystemInit(void)194 void SystemInit(void)
195 {
196   /* FPU settings ------------------------------------------------------------*/
197   #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
198    SCB->CPACR |= ((3UL << 20U)|(3UL << 22U));  /* set CP10 and CP11 Full Access */
199   #endif
200 }
201 
202 /**
203   * @brief  Update SystemCoreClock variable according to Clock Register Values.
204   *         The SystemCoreClock variable contains the core clock (HCLK), it can
205   *         be used by the user application to setup the SysTick timer or configure
206   *         other parameters.
207   *
208   * @note   Each time the core clock (HCLK) changes, this function must be called
209   *         to update SystemCoreClock variable value. Otherwise, any configuration
210   *         based on this variable will be incorrect.
211   *
212   * @note   - The system frequency computed by this function is not the real
213   *           frequency in the chip. It is calculated based on the predefined
214   *           constant and the selected clock source:
215   *
216   *           - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
217   *
218   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
219   *
220   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
221   *
222   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
223   *             or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
224   *
225   *         (*) MSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
226   *             4 MHz) but the real value may vary depending on the variations
227   *             in voltage and temperature.
228   *
229   *         (**) HSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
230   *              16 MHz) but the real value may vary depending on the variations
231   *              in voltage and temperature.
232   *
233   *         (***) HSE_VALUE is a constant defined in stm32u5xx_hal.h file (default value
234   *              8 MHz), user has to ensure that HSE_VALUE is same as the real
235   *              frequency of the crystal used. Otherwise, this function may
236   *              have wrong result.
237   *
238   *         - The result of this function could be not correct when using fractional
239   *           value for HSE crystal.
240   *
241   * @param  None
242   * @retval None
243   */
SystemCoreClockUpdate(void)244 void SystemCoreClockUpdate(void)
245 {
246   uint32_t pllr, pllsource, pllm , tmp, pllfracen, msirange;
247   float_t fracn1, pllvco;
248 
249   /* Get MSI Range frequency--------------------------------------------------*/
250   if(READ_BIT(RCC->ICSCR1, RCC_ICSCR1_MSIRGSEL) == 0U)
251   {
252     /* MSISRANGE from RCC_CSR applies */
253     msirange = (RCC->CSR & RCC_CSR_MSISSRANGE) >> RCC_CSR_MSISSRANGE_Pos;
254   }
255   else
256   {
257     /* MSIRANGE from RCC_CR applies */
258     msirange = (RCC->ICSCR1 & RCC_ICSCR1_MSISRANGE) >> RCC_ICSCR1_MSISRANGE_Pos;
259   }
260 
261   /*MSI frequency range in HZ*/
262   msirange = MSIRangeTable[msirange];
263 
264   /* Get SYSCLK source -------------------------------------------------------*/
265   switch (RCC->CFGR1 & RCC_CFGR1_SWS)
266   {
267   case 0x00:  /* MSI used as system clock source */
268     SystemCoreClock = msirange;
269     break;
270 
271   case 0x04:  /* HSI used as system clock source */
272     SystemCoreClock = HSI_VALUE;
273     break;
274 
275   case 0x08:  /* HSE used as system clock source */
276     SystemCoreClock = HSE_VALUE;
277     break;
278 
279   case 0x0C:  /* PLL used as system clock source */
280     /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
281     SYSCLK = PLL_VCO / PLLR
282     */
283     pllsource = (RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1SRC);
284     pllm = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1M)>> RCC_PLL1CFGR_PLL1M_Pos) + 1U;
285     pllfracen = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1FRACEN)>>RCC_PLL1CFGR_PLL1FRACEN_Pos);
286     fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_PLL1FRACN)>> RCC_PLL1FRACR_PLL1FRACN_Pos));
287 
288       switch (pllsource)
289       {
290       case 0x00:  /* No clock sent to PLL*/
291         pllvco = (float_t)0U;
292         break;
293 
294       case 0x02:  /* HSI used as PLL clock source */
295         pllvco = ((float_t)HSI_VALUE / (float_t)pllm);
296         break;
297 
298       case 0x03:  /* HSE used as PLL clock source */
299         pllvco = ((float_t)HSE_VALUE / (float_t)pllm);
300         break;
301 
302       default:    /* MSI used as PLL clock source */
303         pllvco = ((float_t)msirange / (float_t)pllm);
304         break;
305       }
306 
307       pllvco = pllvco * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1N) + (fracn1/(float_t)0x2000) + (float_t)1U);
308       pllr = (((RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1R) >> RCC_PLL1DIVR_PLL1R_Pos) + 1U );
309       SystemCoreClock = (uint32_t)((uint32_t)pllvco/pllr);
310       break;
311 
312   default:
313     SystemCoreClock = msirange;
314     break;
315   }
316   /* Compute HCLK clock frequency --------------------------------------------*/
317   /* Get HCLK prescaler */
318   tmp = AHBPrescTable[((RCC->CFGR2 & RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos)];
319   /* HCLK clock frequency */
320   SystemCoreClock >>= tmp;
321 }
322 
323 
324 /**
325   * @}
326   */
327 
328 /**
329   * @}
330   */
331 
332 /**
333   * @}
334   */
335 
336 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
337