system_stm32g4xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32g4xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M4 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_stm32g4xx.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 (16 MHz) is used as system clock source.
  22. * Then SystemInit() function is called, in "startup_stm32g4xx.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. * APB1 Prescaler | 1
  37. *-----------------------------------------------------------------------------
  38. * APB2 Prescaler | 1
  39. *-----------------------------------------------------------------------------
  40. * PLL_M | 1
  41. *-----------------------------------------------------------------------------
  42. * PLL_N | 16
  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) 2019 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 stm32g4xx_system
  69. * @{
  70. */
  71. /** @addtogroup STM32G4xx_System_Private_Includes
  72. * @{
  73. */
  74. #include "stm32g4xx.h"
  75. #if !defined (HSE_VALUE)
  76. #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */
  77. #endif /* HSE_VALUE */
  78. #if !defined (HSI_VALUE)
  79. #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/
  80. #endif /* HSI_VALUE */
  81. /**
  82. * @}
  83. */
  84. /** @addtogroup STM32G4xx_System_Private_TypesDefinitions
  85. * @{
  86. */
  87. /**
  88. * @}
  89. */
  90. /** @addtogroup STM32G4xx_System_Private_Defines
  91. * @{
  92. */
  93. /************************* Miscellaneous Configuration ************************/
  94. /* Note: Following vector table addresses must be defined in line with linker
  95. configuration. */
  96. /*!< Uncomment the following line if you need to relocate the vector table
  97. anywhere in Flash or Sram, else the vector table is kept at the automatic
  98. remap of boot address selected */
  99. /* #define USER_VECT_TAB_ADDRESS */
  100. #if defined(USER_VECT_TAB_ADDRESS)
  101. /*!< Uncomment the following line if you need to relocate your vector Table
  102. in Sram else user remap will be done in Flash. */
  103. /* #define VECT_TAB_SRAM */
  104. #if defined(VECT_TAB_SRAM)
  105. #define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
  106. This value must be a multiple of 0x200. */
  107. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  108. This value must be a multiple of 0x200. */
  109. #else
  110. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  111. This value must be a multiple of 0x200. */
  112. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  113. This value must be a multiple of 0x200. */
  114. #endif /* VECT_TAB_SRAM */
  115. #endif /* USER_VECT_TAB_ADDRESS */
  116. /******************************************************************************/
  117. /**
  118. * @}
  119. */
  120. /** @addtogroup STM32G4xx_System_Private_Macros
  121. * @{
  122. */
  123. /**
  124. * @}
  125. */
  126. /** @addtogroup STM32G4xx_System_Private_Variables
  127. * @{
  128. */
  129. /* The SystemCoreClock variable is updated in three ways:
  130. 1) by calling CMSIS function SystemCoreClockUpdate()
  131. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  132. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  133. Note: If you use this function to configure the system clock; then there
  134. is no need to call the 2 first functions listed above, since SystemCoreClock
  135. variable is updated automatically.
  136. */
  137. uint32_t SystemCoreClock = HSI_VALUE;
  138. const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  139. const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
  140. /**
  141. * @}
  142. */
  143. /** @addtogroup STM32G4xx_System_Private_FunctionPrototypes
  144. * @{
  145. */
  146. /**
  147. * @}
  148. */
  149. /** @addtogroup STM32G4xx_System_Private_Functions
  150. * @{
  151. */
  152. /**
  153. * @brief Setup the microcontroller system.
  154. * @param None
  155. * @retval None
  156. */
  157. void SystemInit(void)
  158. {
  159. /* FPU settings ------------------------------------------------------------*/
  160. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  161. SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */
  162. #endif
  163. /* Configure the Vector Table location add offset address ------------------*/
  164. #if defined(USER_VECT_TAB_ADDRESS)
  165. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  166. #endif /* USER_VECT_TAB_ADDRESS */
  167. }
  168. /**
  169. * @brief Update SystemCoreClock variable according to Clock Register Values.
  170. * The SystemCoreClock variable contains the core clock (HCLK), it can
  171. * be used by the user application to setup the SysTick timer or configure
  172. * other parameters.
  173. *
  174. * @note Each time the core clock (HCLK) changes, this function must be called
  175. * to update SystemCoreClock variable value. Otherwise, any configuration
  176. * based on this variable will be incorrect.
  177. *
  178. * @note - The system frequency computed by this function is not the real
  179. * frequency in the chip. It is calculated based on the predefined
  180. * constant and the selected clock source:
  181. *
  182. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
  183. *
  184. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  185. *
  186. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
  187. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  188. *
  189. * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value
  190. * 16 MHz) but the real value may vary depending on the variations
  191. * in voltage and temperature.
  192. *
  193. * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value
  194. * 24 MHz), user has to ensure that HSE_VALUE is same as the real
  195. * frequency of the crystal used. Otherwise, this function may
  196. * have wrong result.
  197. *
  198. * - The result of this function could be not correct when using fractional
  199. * value for HSE crystal.
  200. *
  201. * @param None
  202. * @retval None
  203. */
  204. void SystemCoreClockUpdate(void)
  205. {
  206. uint32_t tmp, pllvco, pllr, pllsource, pllm;
  207. /* Get SYSCLK source -------------------------------------------------------*/
  208. switch (RCC->CFGR & RCC_CFGR_SWS)
  209. {
  210. case 0x04: /* HSI used as system clock source */
  211. SystemCoreClock = HSI_VALUE;
  212. break;
  213. case 0x08: /* HSE used as system clock source */
  214. SystemCoreClock = HSE_VALUE;
  215. break;
  216. case 0x0C: /* PLL used as system clock source */
  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) >> 4) + 1U ;
  222. if (pllsource == 0x02UL) /* HSI used as PLL clock source */
  223. {
  224. pllvco = (HSI_VALUE / pllm);
  225. }
  226. else /* HSE used as PLL clock source */
  227. {
  228. pllvco = (HSE_VALUE / pllm);
  229. }
  230. pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8);
  231. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U;
  232. SystemCoreClock = pllvco/pllr;
  233. break;
  234. default:
  235. break;
  236. }
  237. /* Compute HCLK clock frequency --------------------------------------------*/
  238. /* Get HCLK prescaler */
  239. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  240. /* HCLK clock frequency */
  241. SystemCoreClock >>= tmp;
  242. }
  243. /**
  244. * @}
  245. */
  246. /**
  247. * @}
  248. */
  249. /**
  250. * @}
  251. */