system_stm32wbxx.c 13 KB

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