system_stm32u5xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. * Copyright (c) 2021 STMicroelectronics.
  82. * All rights reserved.
  83. *
  84. * This software is licensed under terms that can be found in the LICENSE file
  85. * in the root directory of this software component.
  86. * If no LICENSE file comes with this software, it is provided AS-IS.
  87. *
  88. ******************************************************************************
  89. */
  90. /** @addtogroup CMSIS
  91. * @{
  92. */
  93. /** @addtogroup STM32U5xx_system
  94. * @{
  95. */
  96. /** @addtogroup STM32U5xx_System_Private_Includes
  97. * @{
  98. */
  99. #include "stm32u5xx.h"
  100. #include <math.h>
  101. /**
  102. * @}
  103. */
  104. /** @addtogroup STM32U5xx_System_Private_TypesDefinitions
  105. * @{
  106. */
  107. /**
  108. * @}
  109. */
  110. /** @addtogroup STM32U5xx_System_Private_Defines
  111. * @{
  112. */
  113. #if !defined (HSE_VALUE)
  114. #define HSE_VALUE 16000000U /*!< Value of the External oscillator in Hz */
  115. #endif /* HSE_VALUE */
  116. #if !defined (MSI_VALUE)
  117. #define MSI_VALUE 4000000U /*!< Value of the Internal oscillator in Hz*/
  118. #endif /* MSI_VALUE */
  119. #if !defined (HSI_VALUE)
  120. #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/
  121. #endif /* HSI_VALUE */
  122. /************************* Miscellaneous Configuration ************************/
  123. /*!< Uncomment the following line if you need to relocate your vector Table in
  124. Internal SRAM. */
  125. /* #define VECT_TAB_SRAM */
  126. #define VECT_TAB_OFFSET 0x00000000UL /*!< Vector Table base offset field.
  127. This value must be a multiple of 0x200. */
  128. /******************************************************************************/
  129. /**
  130. * @}
  131. */
  132. /** @addtogroup STM32U5xx_System_Private_Macros
  133. * @{
  134. */
  135. /**
  136. * @}
  137. */
  138. /** @addtogroup STM32U5xx_System_Private_Variables
  139. * @{
  140. */
  141. /* The SystemCoreClock variable is updated in three ways:
  142. 1) by calling CMSIS function SystemCoreClockUpdate()
  143. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  144. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  145. Note: If you use this function to configure the system clock; then there
  146. is no need to call the 2 first functions listed above, since SystemCoreClock
  147. variable is updated automatically.
  148. */
  149. uint32_t SystemCoreClock = 4000000U;
  150. const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  151. const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
  152. const uint32_t MSIRangeTable[16] = {48000000U,24000000U,16000000U,12000000U, 4000000U, 2000000U, 1500000U,\
  153. 1000000U, 3072000U, 1536000U,1024000U, 768000U, 400000U, 200000U, 150000U, 100000U};
  154. /**
  155. * @}
  156. */
  157. /** @addtogroup STM32U5xx_System_Private_FunctionPrototypes
  158. * @{
  159. */
  160. /**
  161. * @}
  162. */
  163. /** @addtogroup STM32U5xx_System_Private_Functions
  164. * @{
  165. */
  166. /**
  167. * @brief Setup the microcontroller system.
  168. * @param None
  169. * @retval None
  170. */
  171. void SystemInit(void)
  172. {
  173. /* FPU settings ------------------------------------------------------------*/
  174. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  175. SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
  176. #endif
  177. /* Reset the RCC clock configuration to the default reset state ------------*/
  178. /* Set MSION bit */
  179. RCC->CR = RCC_CR_MSISON;
  180. /* Reset CFGR register */
  181. RCC->CFGR1 = 0U;
  182. RCC->CFGR2 = 0U;
  183. RCC->CFGR3 = 0U;
  184. /* Reset HSEON, CSSON , HSION, PLLxON bits */
  185. RCC->CR &= ~(RCC_CR_HSEON | RCC_CR_CSSON | RCC_CR_PLL1ON | RCC_CR_PLL2ON | RCC_CR_PLL3ON);
  186. /* Reset PLLCFGR register */
  187. RCC->PLL1CFGR = 0U;
  188. /* Reset HSEBYP bit */
  189. RCC->CR &= ~(RCC_CR_HSEBYP);
  190. /* Disable all interrupts */
  191. RCC->CIER = 0U;
  192. /* Configure the Vector Table location add offset address ------------------*/
  193. #ifdef VECT_TAB_SRAM
  194. SCB->VTOR = SRAM1_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  195. #else
  196. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  197. #endif
  198. }
  199. /**
  200. * @brief Update SystemCoreClock variable according to Clock Register Values.
  201. * The SystemCoreClock variable contains the core clock (HCLK), it can
  202. * be used by the user application to setup the SysTick timer or configure
  203. * other parameters.
  204. *
  205. * @note Each time the core clock (HCLK) changes, this function must be called
  206. * to update SystemCoreClock variable value. Otherwise, any configuration
  207. * based on this variable will be incorrect.
  208. *
  209. * @note - The system frequency computed by this function is not the real
  210. * frequency in the chip. It is calculated based on the predefined
  211. * constant and the selected clock source:
  212. *
  213. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
  214. *
  215. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
  216. *
  217. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  218. *
  219. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
  220. * or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
  221. *
  222. * (*) MSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
  223. * 4 MHz) but the real value may vary depending on the variations
  224. * in voltage and temperature.
  225. *
  226. * (**) HSI_VALUE is a constant defined in stm32u5xx_hal.h file (default value
  227. * 16 MHz) but the real value may vary depending on the variations
  228. * in voltage and temperature.
  229. *
  230. * (***) HSE_VALUE is a constant defined in stm32u5xx_hal.h file (default value
  231. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  232. * frequency of the crystal used. Otherwise, this function may
  233. * have wrong result.
  234. *
  235. * - The result of this function could be not correct when using fractional
  236. * value for HSE crystal.
  237. *
  238. * @param None
  239. * @retval None
  240. */
  241. void SystemCoreClockUpdate(void)
  242. {
  243. uint32_t pllr, pllsource, pllm , tmp, pllfracen, msirange;
  244. float_t fracn1, pllvco;
  245. /* Get MSI Range frequency--------------------------------------------------*/
  246. if(READ_BIT(RCC->ICSCR1, RCC_ICSCR1_MSIRGSEL) == 0U)
  247. {
  248. /* MSISRANGE from RCC_CSR applies */
  249. msirange = (RCC->CSR & RCC_CSR_MSISSRANGE) >> RCC_CSR_MSISSRANGE_Pos;
  250. }
  251. else
  252. {
  253. /* MSIRANGE from RCC_CR applies */
  254. msirange = (RCC->ICSCR1 & RCC_ICSCR1_MSISRANGE) >> RCC_ICSCR1_MSISRANGE_Pos;
  255. }
  256. /*MSI frequency range in HZ*/
  257. msirange = MSIRangeTable[msirange];
  258. /* Get SYSCLK source -------------------------------------------------------*/
  259. switch (RCC->CFGR1 & RCC_CFGR1_SWS)
  260. {
  261. case 0x00: /* MSI used as system clock source */
  262. SystemCoreClock = msirange;
  263. break;
  264. case 0x04: /* HSI used as system clock source */
  265. SystemCoreClock = HSI_VALUE;
  266. break;
  267. case 0x08: /* HSE used as system clock source */
  268. SystemCoreClock = HSE_VALUE;
  269. break;
  270. case 0x0C: /* PLL used as system clock source */
  271. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  272. SYSCLK = PLL_VCO / PLLR
  273. */
  274. pllsource = (RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1SRC);
  275. pllm = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1M)>> RCC_PLL1CFGR_PLL1M_Pos) + 1U;
  276. pllfracen = ((RCC->PLL1CFGR & RCC_PLL1CFGR_PLL1FRACEN)>>RCC_PLL1CFGR_PLL1FRACEN_Pos);
  277. fracn1 = (float_t)(uint32_t)(pllfracen* ((RCC->PLL1FRACR & RCC_PLL1FRACR_PLL1FRACN)>> RCC_PLL1FRACR_PLL1FRACN_Pos));
  278. switch (pllsource)
  279. {
  280. case 0x00: /* No clock sent to PLL*/
  281. pllvco = (float_t)0U;
  282. break;
  283. case 0x02: /* HSI used as PLL clock source */
  284. pllvco = ((float_t)HSI_VALUE / (float_t)pllm);
  285. break;
  286. case 0x03: /* HSE used as PLL clock source */
  287. pllvco = ((float_t)HSE_VALUE / (float_t)pllm);
  288. break;
  289. default: /* MSI used as PLL clock source */
  290. pllvco = ((float_t)msirange / (float_t)pllm);
  291. break;
  292. }
  293. pllvco = pllvco * ((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1N) + (fracn1/(float_t)0x2000) + (float_t)1U);
  294. pllr = (((RCC->PLL1DIVR & RCC_PLL1DIVR_PLL1R) >> RCC_PLL1DIVR_PLL1R_Pos) + 1U );
  295. SystemCoreClock = (uint32_t)((uint32_t)pllvco/pllr);
  296. break;
  297. default:
  298. SystemCoreClock = msirange;
  299. break;
  300. }
  301. /* Compute HCLK clock frequency --------------------------------------------*/
  302. /* Get HCLK prescaler */
  303. tmp = AHBPrescTable[((RCC->CFGR2 & RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos)];
  304. /* HCLK clock frequency */
  305. SystemCoreClock >>= tmp;
  306. }
  307. /**
  308. * @}
  309. */
  310. /**
  311. * @}
  312. */
  313. /**
  314. * @}
  315. */