system_stm32f3xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f3xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File.
  6. *
  7. * 1. 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_stm32f3xx.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. * 2. After each device reset the HSI (8 MHz) is used as system clock source.
  22. * Then SystemInit() function is called, in "startup_stm32f3xx.s" file, to
  23. * configure the system clock before to branch to main program.
  24. *
  25. * 3. This file configures the system clock as follows:
  26. *=============================================================================
  27. * Supported STM32F3xx device
  28. *-----------------------------------------------------------------------------
  29. * System Clock source | HSI
  30. *-----------------------------------------------------------------------------
  31. * SYSCLK(Hz) | 8000000
  32. *-----------------------------------------------------------------------------
  33. * HCLK(Hz) | 8000000
  34. *-----------------------------------------------------------------------------
  35. * AHB Prescaler | 1
  36. *-----------------------------------------------------------------------------
  37. * APB2 Prescaler | 1
  38. *-----------------------------------------------------------------------------
  39. * APB1 Prescaler | 1
  40. *-----------------------------------------------------------------------------
  41. * USB Clock | DISABLE
  42. *-----------------------------------------------------------------------------
  43. *=============================================================================
  44. ******************************************************************************
  45. * @attention
  46. *
  47. * Copyright (c) 2016 STMicroelectronics.
  48. * All rights reserved.
  49. *
  50. * This software is licensed under terms that can be found in the LICENSE file
  51. * in the root directory of this software component.
  52. * If no LICENSE file comes with this software, it is provided AS-IS.
  53. *
  54. ******************************************************************************
  55. */
  56. /** @addtogroup CMSIS
  57. * @{
  58. */
  59. /** @addtogroup stm32f3xx_system
  60. * @{
  61. */
  62. /** @addtogroup STM32F3xx_System_Private_Includes
  63. * @{
  64. */
  65. #include "stm32f3xx.h"
  66. /**
  67. * @}
  68. */
  69. /** @addtogroup STM32F3xx_System_Private_TypesDefinitions
  70. * @{
  71. */
  72. /**
  73. * @}
  74. */
  75. /** @addtogroup STM32F3xx_System_Private_Defines
  76. * @{
  77. */
  78. #if !defined (HSE_VALUE)
  79. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
  80. This value can be provided and adapted by the user application. */
  81. #endif /* HSE_VALUE */
  82. #if !defined (HSI_VALUE)
  83. #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
  84. This value can be provided and adapted by the user application. */
  85. #endif /* HSI_VALUE */
  86. /* Note: Following vector table addresses must be defined in line with linker
  87. configuration. */
  88. /*!< Uncomment the following line if you need to relocate the vector table
  89. anywhere in Flash or Sram, else the vector table is kept at the automatic
  90. remap of boot address selected */
  91. /* #define USER_VECT_TAB_ADDRESS */
  92. #if defined(USER_VECT_TAB_ADDRESS)
  93. /*!< Uncomment the following line if you need to relocate your vector Table
  94. in Sram else user remap will be done in Flash. */
  95. /* #define VECT_TAB_SRAM */
  96. #if defined(VECT_TAB_SRAM)
  97. #define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
  98. This value must be a multiple of 0x200. */
  99. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  100. This value must be a multiple of 0x200. */
  101. #else
  102. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  103. This value must be a multiple of 0x200. */
  104. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  105. This value must be a multiple of 0x200. */
  106. #endif /* VECT_TAB_SRAM */
  107. #endif /* USER_VECT_TAB_ADDRESS */
  108. /******************************************************************************/
  109. /**
  110. * @}
  111. */
  112. /** @addtogroup STM32F3xx_System_Private_Macros
  113. * @{
  114. */
  115. /**
  116. * @}
  117. */
  118. /** @addtogroup STM32F3xx_System_Private_Variables
  119. * @{
  120. */
  121. /* This variable is updated in three ways:
  122. 1) by calling CMSIS function SystemCoreClockUpdate()
  123. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  124. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  125. Note: If you use this function to configure the system clock there is no need to
  126. call the 2 first functions listed above, since SystemCoreClock variable is
  127. updated automatically.
  128. */
  129. uint32_t SystemCoreClock = 8000000;
  130. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  131. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  132. /**
  133. * @}
  134. */
  135. /** @addtogroup STM32F3xx_System_Private_FunctionPrototypes
  136. * @{
  137. */
  138. /**
  139. * @}
  140. */
  141. /** @addtogroup STM32F3xx_System_Private_Functions
  142. * @{
  143. */
  144. /**
  145. * @brief Setup the microcontroller system
  146. * @param None
  147. * @retval None
  148. */
  149. void SystemInit(void)
  150. {
  151. /* FPU settings --------------------------------------------------------------*/
  152. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  153. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  154. #endif
  155. /* Configure the Vector Table location -------------------------------------*/
  156. #if defined(USER_VECT_TAB_ADDRESS)
  157. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  158. #endif /* USER_VECT_TAB_ADDRESS */
  159. }
  160. /**
  161. * @brief Update SystemCoreClock variable according to Clock Register Values.
  162. * The SystemCoreClock variable contains the core clock (HCLK), it can
  163. * be used by the user application to setup the SysTick timer or configure
  164. * other parameters.
  165. *
  166. * @note Each time the core clock (HCLK) changes, this function must be called
  167. * to update SystemCoreClock variable value. Otherwise, any configuration
  168. * based on this variable will be incorrect.
  169. *
  170. * @note - The system frequency computed by this function is not the real
  171. * frequency in the chip. It is calculated based on the predefined
  172. * constant and the selected clock source:
  173. *
  174. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  175. *
  176. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_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 stm32f3xx_hal.h file (default value
  182. * 8 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 stm32f3xx_hal.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 = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
  199. /* Get SYSCLK source -------------------------------------------------------*/
  200. tmp = RCC->CFGR & RCC_CFGR_SWS;
  201. switch (tmp)
  202. {
  203. case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
  204. SystemCoreClock = HSI_VALUE;
  205. break;
  206. case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
  207. SystemCoreClock = HSE_VALUE;
  208. break;
  209. case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
  210. /* Get PLL clock source and multiplication factor ----------------------*/
  211. pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
  212. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  213. pllmull = ( pllmull >> 18) + 2;
  214. #if defined (STM32F302xE) || defined (STM32F303xE) || defined (STM32F398xx)
  215. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  216. if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
  217. {
  218. /* HSE oscillator clock selected as PREDIV1 clock entry */
  219. SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
  220. }
  221. else
  222. {
  223. /* HSI oscillator clock selected as PREDIV1 clock entry */
  224. SystemCoreClock = (HSI_VALUE / predivfactor) * pllmull;
  225. }
  226. #else
  227. if (pllsource == RCC_CFGR_PLLSRC_HSI_DIV2)
  228. {
  229. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  230. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  231. }
  232. else
  233. {
  234. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  235. /* HSE oscillator clock selected as PREDIV1 clock entry */
  236. SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
  237. }
  238. #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
  239. break;
  240. default: /* HSI used as system clock */
  241. SystemCoreClock = HSI_VALUE;
  242. break;
  243. }
  244. /* Compute HCLK clock frequency ----------------*/
  245. /* Get HCLK prescaler */
  246. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  247. /* HCLK clock frequency */
  248. SystemCoreClock >>= tmp;
  249. }
  250. /**
  251. * @}
  252. */
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */