system_stm32g0xx.c 10 KB

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