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 static void SetSysClock(void);
180 
181 /**
182   * @}
183   */
184 
185 /** @addtogroup STM32U5xx_System_Private_Functions
186   * @{
187   */
188 
189 /**
190   * @brief  Setup the microcontroller system.
191   * @param  None
192   * @retval None
193   */
194 
SystemInit(void)195 void SystemInit(void)
196 {
197   /* FPU settings ------------------------------------------------------------*/
198   #if ((__FPU_PRESENT == 1) && (__FPU_USED == 1U)) \
199     || ((__FPU_PRESENT == 1) && defined(__CC_ARM)) /*  Fix Me : armclang build using vfp instruction  */
200    SCB->CPACR |= ((3UL << 20U)|(3UL << 22U));  /* set CP10 and CP11 Full Access */
201   #endif
202 
203   /* Configure the System clock source, PLL Multiplier and Divider factors,
204      AHB/APBx prescalers and Flash settings for System clock 160 MHz ---------*/
205   SetSysClock();
206 }
207 
208 /**
209   * @brief  Update SystemCoreClock variable according to Clock Register Values.
210   *         The SystemCoreClock variable contains the core clock (HCLK), it can
211   *         be used by the user application to setup the SysTick timer or configure
212   *         other parameters.
213   *
214   * @note   Each time the core clock (HCLK) changes, this function must be called
215   *         to update SystemCoreClock variable value. Otherwise, any configuration
216   *         based on this variable will be incorrect.
217   *
218   * @note   - The system frequency computed by this function is not the real
219   *           frequency in the chip. It is calculated based on the predefined
220   *           constant and the selected clock source:
221   *
222   *           - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
223   *
224   *           - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
225   *
226   *           - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
227   *
228   *           - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
229   *             or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
230   *
231   *         (*) MSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
232   *             4 MHz) but the real value may vary depending on the variations
233   *             in voltage and temperature.
234   *
235   *         (**) HSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
236   *              16 MHz) but the real value may vary depending on the variations
237   *              in voltage and temperature.
238   *
239   *         (***) HSE_VALUE is a constant defined in stm32u5xx_hal.h file (default value
240   *              8 MHz), user has to ensure that HSE_VALUE is same as the real
241   *              frequency of the crystal used. Otherwise, this function may
242   *              have wrong result.
243   *
244   *         - The result of this function could be not correct when using fractional
245   *           value for HSE crystal.
246   *
247   * @param  None
248   * @retval None
249   */
SystemCoreClockUpdate(void)250 void SystemCoreClockUpdate(void)
251 {
252   uint32_t pllr, pllsource, pllm , tmp, pllfracen, msirange;
253   float_t fracn1, pllvco;
254 
255   /* Get MSI Range frequency--------------------------------------------------*/
256   if(READ_BIT(RCC->ICSCR1, RCC_ICSCR1_MSIRGSEL) == 0U)
257   {
258     /* MSISRANGE from RCC_CSR applies */
259     msirange = (RCC->CSR & RCC_CSR_MSISSRANGE) >> RCC_CSR_MSISSRANGE_Pos;
260   }
261   else
262   {
263     /* MSIRANGE from RCC_CR applies */
264     msirange = (RCC->ICSCR1 & RCC_ICSCR1_MSISRANGE) >> RCC_ICSCR1_MSISRANGE_Pos;
265   }
266 
267   /*MSI frequency range in HZ*/
268   msirange = MSIRangeTable[msirange];
269 
270   /* Get SYSCLK source -------------------------------------------------------*/
271   switch (RCC->CFGR1 & RCC_CFGR1_SWS)
272   {
273   case 0x00:  /* MSI used as system clock source */
274     SystemCoreClock = msirange;
275     break;
276 
277   case 0x04:  /* HSI used as system clock source */
278     SystemCoreClock = HSI_VALUE;
279     break;
280 
281   case 0x08:  /* HSE used as system clock source */
282     SystemCoreClock = HSE_VALUE;
283     break;
284 
285   case 0x0C:  /* PLL used as system clock source */
286     /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
287     SYSCLK = PLL_VCO / PLLR
288     */
289     pllsource = (RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1SRC);
290     pllm = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1M)>> RCC_PLL1CFGR_PLL1M_Pos) + 1U;
291     pllfracen = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1FRACEN)>>RCC_PLL1CFGR_PLL1FRACEN_Pos);
292     fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_PLL1FRACN)>> RCC_PLL1FRACR_PLL1FRACN_Pos));
293 
294       switch (pllsource)
295       {
296       case 0x00:  /* No clock sent to PLL*/
297         pllvco = (float_t)0U;
298         break;
299 
300       case 0x02:  /* HSI used as PLL clock source */
301         pllvco = ((float_t)HSI_VALUE / (float_t)pllm);
302         break;
303 
304       case 0x03:  /* HSE used as PLL clock source */
305         pllvco = ((float_t)HSE_VALUE / (float_t)pllm);
306         break;
307 
308       default:    /* MSI used as PLL clock source */
309         pllvco = ((float_t)msirange / (float_t)pllm);
310         break;
311       }
312 
313       pllvco = pllvco * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1N) + (fracn1/(float_t)0x2000) + (float_t)1U);
314       pllr = (((RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1R) >> RCC_PLL1DIVR_PLL1R_Pos) + 1U );
315       SystemCoreClock = (uint32_t)((uint32_t)pllvco/pllr);
316       break;
317 
318   default:
319     SystemCoreClock = msirange;
320     break;
321   }
322   /* Compute HCLK clock frequency --------------------------------------------*/
323   /* Get HCLK prescaler */
324   tmp = AHBPrescTable[((RCC->CFGR2 & RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos)];
325   /* HCLK clock frequency */
326   SystemCoreClock >>= tmp;
327 }
328 
329 /**
330   * @brief  Configures the System clock source, PLL Multiplier and Divider factors,
331   *         AHB/APBx prescalers and Flash settings for System clock 160MHz
332   * @retval None
333   */
SetSysClock(void)334 static void SetSysClock(void)
335 {
336   __IO uint32_t tmp;
337 
338   /* Enable PWR clock */
339   RCC->AHB3ENR |= RCC_AHB3ENR_PWREN;
340   tmp = RCC->AHB3ENR;
341   UNUSED(tmp);
342 
343   /* Set the regulator supply output voltage to range 1 for frequency above 100 Mhz */
344   PWR->VOSR |= PWR_VOSR_VOS;  /* voltage range 1 */
345   while ((PWR->VOSR & PWR_VOSR_VOSRDY) != PWR_VOSR_VOSRDY)
346   {
347   }
348 
349   /* Set Flash latency prior to system clock change */
350   FLASH->ACR = FLASH_ACR_LATENCY_4WS;
351   while ((FLASH->ACR & FLASH_ACR_LATENCY) != FLASH_ACR_LATENCY_4WS)
352   {
353   }
354 
355   /* Configure PLL clock source (MSI) */
356   RCC->PLL1CFGR = RCC_PLL1CFGR_PLL1SRC_0;
357 
358   /* Enable the EPOD to reach max frequency */
359   PWR->VOSR |= PWR_VOSR_BOOSTEN;
360   while ((PWR->VOSR & PWR_VOSR_BOOSTRDY) != PWR_VOSR_BOOSTRDY)
361   {
362   }
363 
364   /* Main PLL configuration and activation */
365   RCC->PLL1CFGR |= RCC_PLL1CFGR_PLL1REN;
366   RCC->PLL1DIVR = ((2U-1U)  << RCC_PLL1DIVR_PLL1R_Pos) |
367                   ((2U-1U)  << RCC_PLL1DIVR_PLL1Q_Pos) |
368                   ((2U-1U)  << RCC_PLL1DIVR_PLL1P_Pos) |
369                   ((80U-1U) << RCC_PLL1DIVR_PLL1N_Pos);
370   RCC->CR |= RCC_CR_PLL1ON;
371   while ((RCC->CR & RCC_CR_PLL1RDY) != RCC_CR_PLL1RDY)
372   {
373   }
374 
375   /* Set PLL1 as System Clock Source */
376   RCC->CFGR1 = RCC_CFGR1_SW;
377   while ((RCC->CFGR1 & RCC_CFGR1_SWS) != RCC_CFGR1_SWS)
378   {
379   }
380 
381   /* Enable the secure Internal High Speed oscillator (SHSI) for HW crypto peripherals */
382   RCC->CR |= RCC_CR_SHSION;
383 }
384 
385 /**
386   * @}
387   */
388 
389 /**
390   * @}
391   */
392 
393 /**
394   * @}
395   */
396 
397 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
398