system_stm32wbxx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32wbxx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex 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_stm32wbxx.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_stm32wbxx.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. * PLL_M | 1
  41. *-----------------------------------------------------------------------------
  42. * PLL_N | 8
  43. *-----------------------------------------------------------------------------
  44. * PLL_P | 7
  45. *-----------------------------------------------------------------------------
  46. * PLL_Q | 2
  47. *-----------------------------------------------------------------------------
  48. * PLL_R | 2
  49. *-----------------------------------------------------------------------------
  50. * PLLSAI1_P | NA
  51. *-----------------------------------------------------------------------------
  52. * PLLSAI1_Q | NA
  53. *-----------------------------------------------------------------------------
  54. * PLLSAI1_R | NA
  55. *-----------------------------------------------------------------------------
  56. * Require 48MHz for USB OTG FS, | Disabled
  57. * SDIO and RNG clock |
  58. *-----------------------------------------------------------------------------
  59. *=============================================================================
  60. ******************************************************************************
  61. * @attention
  62. *
  63. * Copyright (c) 2019-2021 STMicroelectronics.
  64. * All rights reserved.
  65. *
  66. * This software is licensed under terms that can be found in the LICENSE file
  67. * in the root directory of this software component.
  68. * If no LICENSE file comes with this software, it is provided AS-IS.
  69. *
  70. ******************************************************************************
  71. */
  72. /** @addtogroup CMSIS
  73. * @{
  74. */
  75. /** @addtogroup stm32WBxx_system
  76. * @{
  77. */
  78. /** @addtogroup stm32WBxx_System_Private_Includes
  79. * @{
  80. */
  81. #include "stm32wbxx.h"
  82. #if !defined (HSE_VALUE)
  83. #define HSE_VALUE (32000000UL) /*!< Value of the External oscillator in Hz */
  84. #endif /* HSE_VALUE */
  85. #if !defined (MSI_VALUE)
  86. #define MSI_VALUE (4000000UL) /*!< Value of the Internal oscillator in Hz*/
  87. #endif /* MSI_VALUE */
  88. #if !defined (HSI_VALUE)
  89. #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/
  90. #endif /* HSI_VALUE */
  91. #if !defined (LSI_VALUE)
  92. #define LSI_VALUE (32000UL) /*!< Value of LSI in Hz*/
  93. #endif /* LSI_VALUE */
  94. #if !defined (LSE_VALUE)
  95. #define LSE_VALUE (32768UL) /*!< Value of LSE in Hz*/
  96. #endif /* LSE_VALUE */
  97. /**
  98. * @}
  99. */
  100. /** @addtogroup STM32WBxx_System_Private_TypesDefinitions
  101. * @{
  102. */
  103. /**
  104. * @}
  105. */
  106. /** @addtogroup STM32WBxx_System_Private_Defines
  107. * @{
  108. */
  109. /* Note: Following vector table addresses must be defined in line with linker
  110. configuration. */
  111. /*!< Uncomment the following line if you need to relocate CPU1 CM4 and/or CPU2
  112. CM0+ vector table anywhere in Sram or Flash. Else vector table will be kept
  113. at address 0x00 which correspond to automatic remap of boot address selected */
  114. /* #define USER_VECT_TAB_ADDRESS */
  115. #if defined(USER_VECT_TAB_ADDRESS)
  116. /*!< Uncomment this line for user vector table remap in Sram else user remap
  117. will be done in Flash. */
  118. /* #define VECT_TAB_SRAM */
  119. #if defined(VECT_TAB_SRAM)
  120. #define VECT_TAB_BASE_ADDRESS SRAM1_BASE /*!< Vector Table base address field.
  121. This value must be a multiple of 0x200. */
  122. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  123. This value must be a multiple of 0x200. */
  124. #else
  125. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  126. This value must be a multiple of 0x200. */
  127. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  128. This value must be a multiple of 0x200. */
  129. #endif
  130. #endif
  131. /**
  132. * @}
  133. */
  134. /** @addtogroup STM32WBxx_System_Private_Macros
  135. * @{
  136. */
  137. /**
  138. * @}
  139. */
  140. /** @addtogroup STM32WBxx_System_Private_Variables
  141. * @{
  142. */
  143. /* The SystemCoreClock variable is updated in three ways:
  144. 1) by calling CMSIS function SystemCoreClockUpdate()
  145. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  146. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  147. Note: If you use this function to configure the system clock; then there
  148. is no need to call the 2 first functions listed above, since SystemCoreClock
  149. variable is updated automatically.
  150. */
  151. uint32_t SystemCoreClock = 4000000UL ; /*CPU1: M4 on MSI clock after startup (4MHz)*/
  152. const uint32_t AHBPrescTable[16UL] = {1UL, 3UL, 5UL, 1UL, 1UL, 6UL, 10UL, 32UL, 2UL, 4UL, 8UL, 16UL, 64UL, 128UL, 256UL, 512UL};
  153. const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
  154. const uint32_t MSIRangeTable[16UL] = {100000UL, 200000UL, 400000UL, 800000UL, 1000000UL, 2000000UL, \
  155. 4000000UL, 8000000UL, 16000000UL, 24000000UL, 32000000UL, 48000000UL, 0UL, 0UL, 0UL, 0UL}; /* 0UL values are incorrect cases */
  156. #if defined(STM32WB55xx) || defined(STM32WB5Mxx) || defined(STM32WB35xx) || defined (STM32WB15xx) || defined (STM32WB10xx)
  157. const uint32_t SmpsPrescalerTable[4UL][6UL]={{1UL,3UL,2UL,2UL,1UL,2UL}, \
  158. {2UL,6UL,4UL,3UL,2UL,4UL}, \
  159. {4UL,12UL,8UL,6UL,4UL,8UL}, \
  160. {4UL,12UL,8UL,6UL,4UL,8UL}};
  161. #endif
  162. /**
  163. * @}
  164. */
  165. /** @addtogroup STM32WBxx_System_Private_FunctionPrototypes
  166. * @{
  167. */
  168. /**
  169. * @}
  170. */
  171. /** @addtogroup STM32WBxx_System_Private_Functions
  172. * @{
  173. */
  174. /**
  175. * @brief Setup the microcontroller system.
  176. * @param None
  177. * @retval None
  178. */
  179. void SystemInit(void)
  180. {
  181. #if defined(USER_VECT_TAB_ADDRESS)
  182. /* Configure the Vector Table location add offset address ------------------*/
  183. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
  184. #endif
  185. /* FPU settings ------------------------------------------------------------*/
  186. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  187. SCB->CPACR |= ((3UL << (10UL*2UL))|(3UL << (11UL*2UL))); /* set CP10 and CP11 Full Access */
  188. #endif
  189. /* Reset the RCC clock configuration to the default reset state ------------*/
  190. /* Set MSION bit */
  191. RCC->CR |= RCC_CR_MSION;
  192. /* Reset CFGR register */
  193. RCC->CFGR = 0x00070000U;
  194. /* Reset PLLSAI1ON, PLLON, HSECSSON, HSEON, HSION, and MSIPLLON bits */
  195. RCC->CR &= (uint32_t)0xFAF6FEFBU;
  196. /*!< Reset LSI1 and LSI2 bits */
  197. RCC->CSR &= (uint32_t)0xFFFFFFFAU;
  198. /*!< Reset HSI48ON bit */
  199. RCC->CRRCR &= (uint32_t)0xFFFFFFFEU;
  200. /* Reset PLLCFGR register */
  201. RCC->PLLCFGR = 0x22041000U;
  202. #if defined(STM32WB55xx) || defined(STM32WB5Mxx)
  203. /* Reset PLLSAI1CFGR register */
  204. RCC->PLLSAI1CFGR = 0x22041000U;
  205. #endif
  206. /* Reset HSEBYP bit */
  207. RCC->CR &= 0xFFFBFFFFU;
  208. /* Disable all interrupts */
  209. RCC->CIER = 0x00000000;
  210. }
  211. /**
  212. * @brief Update SystemCoreClock variable according to Clock Register Values.
  213. * The SystemCoreClock variable contains the core clock (HCLK), it can
  214. * be used by the user application to setup the SysTick timer or configure
  215. * other parameters.
  216. *
  217. * @note Each time the core clock (HCLK) changes, this function must be called
  218. * to update SystemCoreClock variable value. Otherwise, any configuration
  219. * based on this variable will be incorrect.
  220. *
  221. * @note - The system frequency computed by this function is not the real
  222. * frequency in the chip. It is calculated based on the predefined
  223. * constant and the selected clock source:
  224. *
  225. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
  226. *
  227. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
  228. *
  229. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  230. *
  231. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
  232. * or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
  233. *
  234. * (*) MSI_VALUE is a constant defined in stm32wbxx_hal.h file (default value
  235. * 4 MHz) but the real value may vary depending on the variations
  236. * in voltage and temperature.
  237. *
  238. * (**) HSI_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value
  239. * 16 MHz) but the real value may vary depending on the variations
  240. * in voltage and temperature.
  241. *
  242. * (***) HSE_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value
  243. * 32 MHz), user has to ensure that HSE_VALUE is same as the real
  244. * frequency of the crystal used. Otherwise, this function may
  245. * have wrong result.
  246. *
  247. * - The result of this function could be not correct when using fractional
  248. * value for HSE crystal.
  249. *
  250. * @param None
  251. * @retval None
  252. */
  253. void SystemCoreClockUpdate(void)
  254. {
  255. uint32_t tmp, msirange, pllvco, pllr, pllsource , pllm;
  256. /* Get MSI Range frequency--------------------------------------------------*/
  257. /*MSI frequency range in Hz*/
  258. msirange = MSIRangeTable[(RCC->CR & RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos];
  259. /* Get SYSCLK source -------------------------------------------------------*/
  260. switch (RCC->CFGR & RCC_CFGR_SWS)
  261. {
  262. case 0x00: /* MSI used as system clock source */
  263. SystemCoreClock = msirange;
  264. break;
  265. case 0x04: /* HSI used as system clock source */
  266. /* HSI used as system clock source */
  267. SystemCoreClock = HSI_VALUE;
  268. break;
  269. case 0x08: /* HSE used as system clock source */
  270. SystemCoreClock = HSE_VALUE;
  271. break;
  272. case 0x0C: /* PLL used as system clock source */
  273. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  274. SYSCLK = PLL_VCO / PLLR
  275. */
  276. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  277. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1UL ;
  278. if(pllsource == 0x02UL) /* HSI used as PLL clock source */
  279. {
  280. pllvco = (HSI_VALUE / pllm);
  281. }
  282. else if(pllsource == 0x03UL) /* HSE used as PLL clock source */
  283. {
  284. pllvco = (HSE_VALUE / pllm);
  285. }
  286. else /* MSI used as PLL clock source */
  287. {
  288. pllvco = (msirange / pllm);
  289. }
  290. pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  291. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1UL);
  292. SystemCoreClock = pllvco/pllr;
  293. break;
  294. default:
  295. SystemCoreClock = msirange;
  296. break;
  297. }
  298. /* Compute HCLK clock frequency --------------------------------------------*/
  299. /* Get HCLK1 prescaler */
  300. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  301. /* HCLK clock frequency */
  302. SystemCoreClock = SystemCoreClock / tmp;
  303. }
  304. /**
  305. * @}
  306. */
  307. /**
  308. * @}
  309. */
  310. /**
  311. * @}
  312. */
  313. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/