system_stm32wbxx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 \
  121. SRAM1_BASE /*!< Vector Table base address field.
  122. This value must be a multiple of 0x200. */
  123. #define VECT_TAB_OFFSET \
  124. 0x00000000U /*!< Vector Table base offset field.
  125. This value must be a multiple of 0x200. */
  126. #else
  127. #define VECT_TAB_BASE_ADDRESS \
  128. FLASH_BASE /*!< Vector Table base address field.
  129. This value must be a multiple of 0x200. */
  130. #define VECT_TAB_OFFSET \
  131. 0x00000000U /*!< Vector Table base offset field.
  132. This value must be a multiple of 0x200. */
  133. #endif
  134. #endif
  135. /**
  136. * @}
  137. */
  138. /** @addtogroup STM32WBxx_System_Private_Macros
  139. * @{
  140. */
  141. /**
  142. * @}
  143. */
  144. /** @addtogroup STM32WBxx_System_Private_Variables
  145. * @{
  146. */
  147. /* The SystemCoreClock variable is updated in three ways:
  148. 1) by calling CMSIS function SystemCoreClockUpdate()
  149. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  150. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  151. Note: If you use this function to configure the system clock; then there
  152. is no need to call the 2 first functions listed above, since SystemCoreClock
  153. variable is updated automatically.
  154. */
  155. uint32_t SystemCoreClock = 4000000UL; /*CPU1: M4 on MSI clock after startup (4MHz)*/
  156. const uint32_t AHBPrescTable[16UL] =
  157. {1UL, 3UL, 5UL, 1UL, 1UL, 6UL, 10UL, 32UL, 2UL, 4UL, 8UL, 16UL, 64UL, 128UL, 256UL, 512UL};
  158. const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
  159. const uint32_t MSIRangeTable[16UL] = {
  160. 100000UL,
  161. 200000UL,
  162. 400000UL,
  163. 800000UL,
  164. 1000000UL,
  165. 2000000UL,
  166. 4000000UL,
  167. 8000000UL,
  168. 16000000UL,
  169. 24000000UL,
  170. 32000000UL,
  171. 48000000UL,
  172. 0UL,
  173. 0UL,
  174. 0UL,
  175. 0UL}; /* 0UL values are incorrect cases */
  176. #if defined(STM32WB55xx) || defined(STM32WB5Mxx) || defined(STM32WB35xx) || \
  177. defined(STM32WB15xx) || defined(STM32WB10xx)
  178. const uint32_t SmpsPrescalerTable[4UL][6UL] = {
  179. {1UL, 3UL, 2UL, 2UL, 1UL, 2UL},
  180. {2UL, 6UL, 4UL, 3UL, 2UL, 4UL},
  181. {4UL, 12UL, 8UL, 6UL, 4UL, 8UL},
  182. {4UL, 12UL, 8UL, 6UL, 4UL, 8UL}};
  183. #endif
  184. /**
  185. * @}
  186. */
  187. /** @addtogroup STM32WBxx_System_Private_FunctionPrototypes
  188. * @{
  189. */
  190. /**
  191. * @}
  192. */
  193. /** @addtogroup STM32WBxx_System_Private_Functions
  194. * @{
  195. */
  196. /**
  197. * @brief Setup the microcontroller system.
  198. * @param None
  199. * @retval None
  200. */
  201. void SystemInit(void) {
  202. #if defined(USER_VECT_TAB_ADDRESS)
  203. /* Configure the Vector Table location add offset address ------------------*/
  204. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
  205. #endif
  206. /* FPU settings ------------------------------------------------------------*/
  207. #if(__FPU_PRESENT == 1) && (__FPU_USED == 1)
  208. SCB->CPACR |=
  209. ((3UL << (10UL * 2UL)) | (3UL << (11UL * 2UL))); /* set CP10 and CP11 Full Access */
  210. #endif
  211. /* Reset the RCC clock configuration to the default reset state ------------*/
  212. /* Set MSION bit */
  213. RCC->CR |= RCC_CR_MSION;
  214. /* Reset CFGR register */
  215. RCC->CFGR = 0x00070000U;
  216. /* Reset PLLSAI1ON, PLLON, HSECSSON, HSEON, HSION, and MSIPLLON bits */
  217. RCC->CR &= (uint32_t)0xFAF6FEFBU;
  218. /*!< Reset LSI1 and LSI2 bits */
  219. RCC->CSR &= (uint32_t)0xFFFFFFFAU;
  220. /*!< Reset HSI48ON bit */
  221. RCC->CRRCR &= (uint32_t)0xFFFFFFFEU;
  222. /* Reset PLLCFGR register */
  223. RCC->PLLCFGR = 0x22041000U;
  224. #if defined(STM32WB55xx) || defined(STM32WB5Mxx)
  225. /* Reset PLLSAI1CFGR register */
  226. RCC->PLLSAI1CFGR = 0x22041000U;
  227. #endif
  228. /* Reset HSEBYP bit */
  229. RCC->CR &= 0xFFFBFFFFU;
  230. /* Disable all interrupts */
  231. RCC->CIER = 0x00000000;
  232. }
  233. /**
  234. * @brief Update SystemCoreClock variable according to Clock Register Values.
  235. * The SystemCoreClock variable contains the core clock (HCLK), it can
  236. * be used by the user application to setup the SysTick timer or configure
  237. * other parameters.
  238. *
  239. * @note Each time the core clock (HCLK) changes, this function must be called
  240. * to update SystemCoreClock variable value. Otherwise, any configuration
  241. * based on this variable will be incorrect.
  242. *
  243. * @note - The system frequency computed by this function is not the real
  244. * frequency in the chip. It is calculated based on the predefined
  245. * constant and the selected clock source:
  246. *
  247. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
  248. *
  249. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
  250. *
  251. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  252. *
  253. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
  254. * or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
  255. *
  256. * (*) MSI_VALUE is a constant defined in stm32wbxx_hal.h file (default value
  257. * 4 MHz) but the real value may vary depending on the variations
  258. * in voltage and temperature.
  259. *
  260. * (**) HSI_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value
  261. * 16 MHz) but the real value may vary depending on the variations
  262. * in voltage and temperature.
  263. *
  264. * (***) HSE_VALUE is a constant defined in stm32wbxx_hal_conf.h file (default value
  265. * 32 MHz), user has to ensure that HSE_VALUE is same as the real
  266. * frequency of the crystal used. Otherwise, this function may
  267. * have wrong result.
  268. *
  269. * - The result of this function could be not correct when using fractional
  270. * value for HSE crystal.
  271. *
  272. * @param None
  273. * @retval None
  274. */
  275. void SystemCoreClockUpdate(void) {
  276. uint32_t tmp, msirange, pllvco, pllr, pllsource, pllm;
  277. /* Get MSI Range frequency--------------------------------------------------*/
  278. /*MSI frequency range in Hz*/
  279. msirange = MSIRangeTable[(RCC->CR & RCC_CR_MSIRANGE) >> RCC_CR_MSIRANGE_Pos];
  280. /* Get SYSCLK source -------------------------------------------------------*/
  281. switch(RCC->CFGR & RCC_CFGR_SWS) {
  282. case 0x00: /* MSI used as system clock source */
  283. SystemCoreClock = msirange;
  284. break;
  285. case 0x04: /* HSI used as system clock source */
  286. /* HSI used as system clock source */
  287. SystemCoreClock = HSI_VALUE;
  288. break;
  289. case 0x08: /* HSE used as system clock source */
  290. SystemCoreClock = HSE_VALUE;
  291. break;
  292. case 0x0C: /* PLL used as system clock source */
  293. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  294. SYSCLK = PLL_VCO / PLLR
  295. */
  296. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  297. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1UL;
  298. if(pllsource == 0x02UL) /* HSI used as PLL clock source */
  299. {
  300. pllvco = (HSI_VALUE / pllm);
  301. } else if(pllsource == 0x03UL) /* HSE used as PLL clock source */
  302. {
  303. pllvco = (HSE_VALUE / pllm);
  304. } else /* MSI used as PLL clock source */
  305. {
  306. pllvco = (msirange / pllm);
  307. }
  308. pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  309. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1UL);
  310. SystemCoreClock = pllvco / pllr;
  311. break;
  312. default:
  313. SystemCoreClock = msirange;
  314. break;
  315. }
  316. /* Compute HCLK clock frequency --------------------------------------------*/
  317. /* Get HCLK1 prescaler */
  318. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  319. /* HCLK clock frequency */
  320. SystemCoreClock = SystemCoreClock / tmp;
  321. }
  322. /**
  323. * @}
  324. */
  325. /**
  326. * @}
  327. */
  328. /**
  329. * @}
  330. */
  331. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/