system_stm32g0xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32g0xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M0+ 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_stm32g0xx.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 HSI (8 MHz then 16 MHz) is used as system clock source.
  22. * Then SystemInit() function is called, in "startup_stm32g0xx.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 | HSI
  29. *-----------------------------------------------------------------------------
  30. * SYSCLK(Hz) | 16000000
  31. *-----------------------------------------------------------------------------
  32. * HCLK(Hz) | 16000000
  33. *-----------------------------------------------------------------------------
  34. * AHB Prescaler | 1
  35. *-----------------------------------------------------------------------------
  36. * APB Prescaler | 1
  37. *-----------------------------------------------------------------------------
  38. * HSI Division factor | 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. * Require 48MHz for RNG | Disabled
  51. *-----------------------------------------------------------------------------
  52. *=============================================================================
  53. ******************************************************************************
  54. * @attention
  55. *
  56. * Copyright (c) 2018-2021 STMicroelectronics.
  57. * All rights reserved.
  58. *
  59. * This software is licensed under terms that can be found in the LICENSE file
  60. * in the root directory of this software component.
  61. * If no LICENSE file comes with this software, it is provided AS-IS.
  62. *
  63. ******************************************************************************
  64. */
  65. /** @addtogroup CMSIS
  66. * @{
  67. */
  68. /** @addtogroup stm32g0xx_system
  69. * @{
  70. */
  71. /** @addtogroup STM32G0xx_System_Private_Includes
  72. * @{
  73. */
  74. #include "stm32g0xx.h"
  75. #if !defined (HSE_VALUE)
  76. #define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */
  77. #endif /* HSE_VALUE */
  78. #if !defined (HSI_VALUE)
  79. #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/
  80. #endif /* HSI_VALUE */
  81. #if !defined (LSI_VALUE)
  82. #define LSI_VALUE (32000UL) /*!< Value of LSI in Hz*/
  83. #endif /* LSI_VALUE */
  84. #if !defined (LSE_VALUE)
  85. #define LSE_VALUE (32768UL) /*!< Value of LSE in Hz*/
  86. #endif /* LSE_VALUE */
  87. /**
  88. * @}
  89. */
  90. /** @addtogroup STM32G0xx_System_Private_TypesDefinitions
  91. * @{
  92. */
  93. /**
  94. * @}
  95. */
  96. /** @addtogroup STM32G0xx_System_Private_Defines
  97. * @{
  98. */
  99. /************************* Miscellaneous Configuration ************************/
  100. /* Note: Following vector table addresses must be defined in line with linker
  101. configuration. */
  102. /*!< Uncomment the following line if you need to relocate the vector table
  103. anywhere in Flash or Sram, else the vector table is kept at the automatic
  104. remap of boot address selected */
  105. /* #define USER_VECT_TAB_ADDRESS */
  106. #if defined(USER_VECT_TAB_ADDRESS)
  107. /*!< Uncomment the following line if you need to relocate your vector Table
  108. in Sram else user remap will be done in Flash. */
  109. /* #define VECT_TAB_SRAM */
  110. #if defined(VECT_TAB_SRAM)
  111. #define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
  112. This value must be a multiple of 0x200. */
  113. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  114. This value must be a multiple of 0x200. */
  115. #else
  116. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  117. This value must be a multiple of 0x200. */
  118. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  119. This value must be a multiple of 0x200. */
  120. #endif /* VECT_TAB_SRAM */
  121. #endif /* USER_VECT_TAB_ADDRESS */
  122. /******************************************************************************/
  123. /**
  124. * @}
  125. */
  126. /** @addtogroup STM32G0xx_System_Private_Macros
  127. * @{
  128. */
  129. /**
  130. * @}
  131. */
  132. /** @addtogroup STM32G0xx_System_Private_Variables
  133. * @{
  134. */
  135. /* The SystemCoreClock variable is updated in three ways:
  136. 1) by calling CMSIS function SystemCoreClockUpdate()
  137. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  138. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  139. Note: If you use this function to configure the system clock; then there
  140. is no need to call the 2 first functions listed above, since SystemCoreClock
  141. variable is updated automatically.
  142. */
  143. uint32_t SystemCoreClock = 16000000UL;
  144. const uint32_t AHBPrescTable[16UL] = {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL, 6UL, 7UL, 8UL, 9UL};
  145. const uint32_t APBPrescTable[8UL] = {0UL, 0UL, 0UL, 0UL, 1UL, 2UL, 3UL, 4UL};
  146. /**
  147. * @}
  148. */
  149. /** @addtogroup STM32G0xx_System_Private_FunctionPrototypes
  150. * @{
  151. */
  152. /**
  153. * @}
  154. */
  155. /** @addtogroup STM32G0xx_System_Private_Functions
  156. * @{
  157. */
  158. /**
  159. * @brief Setup the microcontroller system.
  160. * @param None
  161. * @retval None
  162. */
  163. void SystemInit(void)
  164. {
  165. /* Configure the Vector Table location -------------------------------------*/
  166. #if defined(USER_VECT_TAB_ADDRESS)
  167. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation */
  168. #endif /* USER_VECT_TAB_ADDRESS */
  169. }
  170. /**
  171. * @brief Update SystemCoreClock variable according to Clock Register Values.
  172. * The SystemCoreClock variable contains the core clock (HCLK), it can
  173. * be used by the user application to setup the SysTick timer or configure
  174. * other parameters.
  175. *
  176. * @note Each time the core clock (HCLK) changes, this function must be called
  177. * to update SystemCoreClock variable value. Otherwise, any configuration
  178. * based on this variable will be incorrect.
  179. *
  180. * @note - The system frequency computed by this function is not the real
  181. * frequency in the chip. It is calculated based on the predefined
  182. * constant and the selected clock source:
  183. *
  184. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) / HSI division factor
  185. *
  186. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  187. *
  188. * - If SYSCLK source is LSI, SystemCoreClock will contain the LSI_VALUE
  189. *
  190. * - If SYSCLK source is LSE, SystemCoreClock will contain the LSE_VALUE
  191. *
  192. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
  193. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  194. *
  195. * (**) HSI_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  196. * 16 MHz) but the real value may vary depending on the variations
  197. * in voltage and temperature.
  198. *
  199. * (***) HSE_VALUE is a constant defined in stm32g0xx_hal_conf.h file (default value
  200. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  201. * frequency of the crystal used. Otherwise, this function may
  202. * have wrong result.
  203. *
  204. * - The result of this function could be not correct when using fractional
  205. * value for HSE crystal.
  206. *
  207. * @param None
  208. * @retval None
  209. */
  210. void SystemCoreClockUpdate(void)
  211. {
  212. uint32_t tmp;
  213. uint32_t pllvco;
  214. uint32_t pllr;
  215. uint32_t pllsource;
  216. uint32_t pllm;
  217. uint32_t hsidiv;
  218. /* Get SYSCLK source -------------------------------------------------------*/
  219. switch (RCC->CFGR & RCC_CFGR_SWS)
  220. {
  221. case RCC_CFGR_SWS_0: /* HSE used as system clock */
  222. SystemCoreClock = HSE_VALUE;
  223. break;
  224. case (RCC_CFGR_SWS_1 | RCC_CFGR_SWS_0): /* LSI used as system clock */
  225. SystemCoreClock = LSI_VALUE;
  226. break;
  227. case RCC_CFGR_SWS_2: /* LSE used as system clock */
  228. SystemCoreClock = LSE_VALUE;
  229. break;
  230. case RCC_CFGR_SWS_1: /* PLL used as system clock */
  231. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
  232. SYSCLK = PLL_VCO / PLLR
  233. */
  234. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  235. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> RCC_PLLCFGR_PLLM_Pos) + 1UL;
  236. if(pllsource == 0x03UL) /* HSE used as PLL clock source */
  237. {
  238. pllvco = (HSE_VALUE / pllm);
  239. }
  240. else /* HSI used as PLL clock source */
  241. {
  242. pllvco = (HSI_VALUE / pllm);
  243. }
  244. pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos);
  245. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> RCC_PLLCFGR_PLLR_Pos) + 1UL);
  246. SystemCoreClock = pllvco/pllr;
  247. break;
  248. case 0x00000000U: /* HSI used as system clock */
  249. default: /* HSI used as system clock */
  250. hsidiv = (1UL << ((READ_BIT(RCC->CR, RCC_CR_HSIDIV))>> RCC_CR_HSIDIV_Pos));
  251. SystemCoreClock = (HSI_VALUE/hsidiv);
  252. break;
  253. }
  254. /* Compute HCLK clock frequency --------------------------------------------*/
  255. /* Get HCLK prescaler */
  256. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  257. /* HCLK clock frequency */
  258. SystemCoreClock >>= tmp;
  259. }
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /**
  267. * @}
  268. */