system_stm32f3xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  48. * All rights reserved.</center></h2>
  49. *
  50. * This software component is licensed by ST under BSD 3-Clause license,
  51. * the "License"; You may not use this file except in compliance with the
  52. * License. You may obtain a copy of the License at:
  53. * opensource.org/licenses/BSD-3-Clause
  54. *
  55. ******************************************************************************
  56. */
  57. /** @addtogroup CMSIS
  58. * @{
  59. */
  60. /** @addtogroup stm32f3xx_system
  61. * @{
  62. */
  63. /** @addtogroup STM32F3xx_System_Private_Includes
  64. * @{
  65. */
  66. #include "stm32f3xx.h"
  67. /**
  68. * @}
  69. */
  70. /** @addtogroup STM32F3xx_System_Private_TypesDefinitions
  71. * @{
  72. */
  73. /**
  74. * @}
  75. */
  76. /** @addtogroup STM32F3xx_System_Private_Defines
  77. * @{
  78. */
  79. #if !defined (HSE_VALUE)
  80. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
  81. This value can be provided and adapted by the user application. */
  82. #endif /* HSE_VALUE */
  83. #if !defined (HSI_VALUE)
  84. #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
  85. This value can be provided and adapted by the user application. */
  86. #endif /* HSI_VALUE */
  87. /* Note: Following vector table addresses must be defined in line with linker
  88. configuration. */
  89. /*!< Uncomment the following line if you need to relocate the vector table
  90. anywhere in Flash or Sram, else the vector table is kept at the automatic
  91. remap of boot address selected */
  92. /* #define USER_VECT_TAB_ADDRESS */
  93. #if defined(USER_VECT_TAB_ADDRESS)
  94. /*!< Uncomment the following line if you need to relocate your vector Table
  95. in Sram else user remap will be done in Flash. */
  96. /* #define VECT_TAB_SRAM */
  97. #if defined(VECT_TAB_SRAM)
  98. #define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
  99. This value must be a multiple of 0x200. */
  100. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  101. This value must be a multiple of 0x200. */
  102. #else
  103. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  104. This value must be a multiple of 0x200. */
  105. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  106. This value must be a multiple of 0x200. */
  107. #endif /* VECT_TAB_SRAM */
  108. #endif /* USER_VECT_TAB_ADDRESS */
  109. /******************************************************************************/
  110. /**
  111. * @}
  112. */
  113. /** @addtogroup STM32F3xx_System_Private_Macros
  114. * @{
  115. */
  116. /**
  117. * @}
  118. */
  119. /** @addtogroup STM32F3xx_System_Private_Variables
  120. * @{
  121. */
  122. /* This variable is updated in three ways:
  123. 1) by calling CMSIS function SystemCoreClockUpdate()
  124. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  125. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  126. Note: If you use this function to configure the system clock there is no need to
  127. call the 2 first functions listed above, since SystemCoreClock variable is
  128. updated automatically.
  129. */
  130. uint32_t SystemCoreClock = 8000000;
  131. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  132. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  133. /**
  134. * @}
  135. */
  136. /** @addtogroup STM32F3xx_System_Private_FunctionPrototypes
  137. * @{
  138. */
  139. /**
  140. * @}
  141. */
  142. /** @addtogroup STM32F3xx_System_Private_Functions
  143. * @{
  144. */
  145. /**
  146. * @brief Setup the microcontroller system
  147. * @param None
  148. * @retval None
  149. */
  150. void SystemInit(void)
  151. {
  152. /* FPU settings --------------------------------------------------------------*/
  153. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  154. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  155. #endif
  156. /* Configure the Vector Table location -------------------------------------*/
  157. #if defined(USER_VECT_TAB_ADDRESS)
  158. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  159. #endif /* USER_VECT_TAB_ADDRESS */
  160. }
  161. /**
  162. * @brief Update SystemCoreClock variable according to Clock Register Values.
  163. * The SystemCoreClock variable contains the core clock (HCLK), it can
  164. * be used by the user application to setup the SysTick timer or configure
  165. * other parameters.
  166. *
  167. * @note Each time the core clock (HCLK) changes, this function must be called
  168. * to update SystemCoreClock variable value. Otherwise, any configuration
  169. * based on this variable will be incorrect.
  170. *
  171. * @note - The system frequency computed by this function is not the real
  172. * frequency in the chip. It is calculated based on the predefined
  173. * constant and the selected clock source:
  174. *
  175. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  176. *
  177. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  178. *
  179. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  180. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  181. *
  182. * (*) HSI_VALUE is a constant defined in stm32f3xx_hal.h file (default value
  183. * 8 MHz) but the real value may vary depending on the variations
  184. * in voltage and temperature.
  185. *
  186. * (**) HSE_VALUE is a constant defined in stm32f3xx_hal.h file (default value
  187. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  188. * frequency of the crystal used. Otherwise, this function may
  189. * have wrong result.
  190. *
  191. * - The result of this function could be not correct when using fractional
  192. * value for HSE crystal.
  193. *
  194. * @param None
  195. * @retval None
  196. */
  197. void SystemCoreClockUpdate (void)
  198. {
  199. uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
  200. /* Get SYSCLK source -------------------------------------------------------*/
  201. tmp = RCC->CFGR & RCC_CFGR_SWS;
  202. switch (tmp)
  203. {
  204. case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
  205. SystemCoreClock = HSI_VALUE;
  206. break;
  207. case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
  208. SystemCoreClock = HSE_VALUE;
  209. break;
  210. case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
  211. /* Get PLL clock source and multiplication factor ----------------------*/
  212. pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
  213. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  214. pllmull = ( pllmull >> 18) + 2;
  215. #if defined (STM32F302xE) || defined (STM32F303xE) || defined (STM32F398xx)
  216. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  217. if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
  218. {
  219. /* HSE oscillator clock selected as PREDIV1 clock entry */
  220. SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
  221. }
  222. else
  223. {
  224. /* HSI oscillator clock selected as PREDIV1 clock entry */
  225. SystemCoreClock = (HSI_VALUE / predivfactor) * pllmull;
  226. }
  227. #else
  228. if (pllsource == RCC_CFGR_PLLSRC_HSI_DIV2)
  229. {
  230. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  231. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  232. }
  233. else
  234. {
  235. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  236. /* HSE oscillator clock selected as PREDIV1 clock entry */
  237. SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull;
  238. }
  239. #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
  240. break;
  241. default: /* HSI used as system clock */
  242. SystemCoreClock = HSI_VALUE;
  243. break;
  244. }
  245. /* Compute HCLK clock frequency ----------------*/
  246. /* Get HCLK prescaler */
  247. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  248. /* HCLK clock frequency */
  249. SystemCoreClock >>= tmp;
  250. }
  251. /**
  252. * @}
  253. */
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @}
  259. */
  260. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/