system_stm32f0xx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32f0xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M0 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_stm32f0xx.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. *
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * Copyright (c) 2016 STMicroelectronics.
  26. * All rights reserved.
  27. *
  28. * This software is licensed under terms that can be found in the LICENSE file
  29. * in the root directory of this software component.
  30. * If no LICENSE file comes with this software, it is provided AS-IS.
  31. *
  32. ******************************************************************************
  33. */
  34. /** @addtogroup CMSIS
  35. * @{
  36. */
  37. /** @addtogroup stm32f0xx_system
  38. * @{
  39. */
  40. /** @addtogroup STM32F0xx_System_Private_Includes
  41. * @{
  42. */
  43. #include "stm32f0xx.h"
  44. /**
  45. * @}
  46. */
  47. /** @addtogroup STM32F0xx_System_Private_TypesDefinitions
  48. * @{
  49. */
  50. /**
  51. * @}
  52. */
  53. /** @addtogroup STM32F0xx_System_Private_Defines
  54. * @{
  55. */
  56. #if !defined (HSE_VALUE)
  57. #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz.
  58. This value can be provided and adapted by the user application. */
  59. #endif /* HSE_VALUE */
  60. #if !defined (HSI_VALUE)
  61. #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz.
  62. This value can be provided and adapted by the user application. */
  63. #endif /* HSI_VALUE */
  64. #if !defined (HSI48_VALUE)
  65. #define HSI48_VALUE ((uint32_t)48000000) /*!< Default value of the HSI48 Internal oscillator in Hz.
  66. This value can be provided and adapted by the user application. */
  67. #endif /* HSI48_VALUE */
  68. /**
  69. * @}
  70. */
  71. /** @addtogroup STM32F0xx_System_Private_Macros
  72. * @{
  73. */
  74. /**
  75. * @}
  76. */
  77. /** @addtogroup STM32F0xx_System_Private_Variables
  78. * @{
  79. */
  80. /* This variable is updated in three ways:
  81. 1) by calling CMSIS function SystemCoreClockUpdate()
  82. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  83. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  84. Note: If you use this function to configure the system clock; then there
  85. is no need to call the 2 first functions listed above, since SystemCoreClock
  86. variable is updated automatically.
  87. */
  88. uint32_t SystemCoreClock = 8000000;
  89. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  90. const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
  91. /**
  92. * @}
  93. */
  94. /** @addtogroup STM32F0xx_System_Private_FunctionPrototypes
  95. * @{
  96. */
  97. /**
  98. * @}
  99. */
  100. /** @addtogroup STM32F0xx_System_Private_Functions
  101. * @{
  102. */
  103. /**
  104. * @brief Setup the microcontroller system
  105. * @param None
  106. * @retval None
  107. */
  108. void SystemInit(void)
  109. {
  110. /* NOTE :SystemInit(): This function is called at startup just after reset and
  111. before branch to main program. This call is made inside
  112. the "startup_stm32f0xx.s" file.
  113. User can setups the default system clock (System clock source, PLL Multiplier
  114. and Divider factors, AHB/APBx prescalers and Flash settings).
  115. */
  116. }
  117. /**
  118. * @brief Update SystemCoreClock variable according to Clock Register Values.
  119. * The SystemCoreClock variable contains the core clock (HCLK), it can
  120. * be used by the user application to setup the SysTick timer or configure
  121. * other parameters.
  122. *
  123. * @note Each time the core clock (HCLK) changes, this function must be called
  124. * to update SystemCoreClock variable value. Otherwise, any configuration
  125. * based on this variable will be incorrect.
  126. *
  127. * @note - The system frequency computed by this function is not the real
  128. * frequency in the chip. It is calculated based on the predefined
  129. * constant and the selected clock source:
  130. *
  131. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  132. *
  133. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  134. *
  135. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  136. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  137. *
  138. * - If SYSCLK source is HSI48, SystemCoreClock will contain the HSI48_VALUE(***)
  139. *
  140. * (*) HSI_VALUE is a constant defined in stm32f0xx_hal_conf.h file (default value
  141. * 8 MHz) but the real value may vary depending on the variations
  142. * in voltage and temperature.
  143. *
  144. * (**) HSE_VALUE is a constant defined in stm32f0xx_hal_conf.h file (its value
  145. * depends on the application requirements), user has to ensure that HSE_VALUE
  146. * is same as the real frequency of the crystal used. Otherwise, this function
  147. * may have wrong result.
  148. *
  149. * (***) HSI48_VALUE is a constant defined in stm32f0xx_hal_conf.h file (default value
  150. * 48 MHz) but the real value may vary depending on the variations
  151. * in voltage and temperature.
  152. *
  153. * - The result of this function could be not correct when using fractional
  154. * value for HSE crystal.
  155. *
  156. * @param None
  157. * @retval None
  158. */
  159. void SystemCoreClockUpdate (void)
  160. {
  161. uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0;
  162. /* Get SYSCLK source -------------------------------------------------------*/
  163. tmp = RCC->CFGR & RCC_CFGR_SWS;
  164. switch (tmp)
  165. {
  166. case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
  167. SystemCoreClock = HSI_VALUE;
  168. break;
  169. case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
  170. SystemCoreClock = HSE_VALUE;
  171. break;
  172. case RCC_CFGR_SWS_PLL: /* PLL used as system clock */
  173. /* Get PLL clock source and multiplication factor ----------------------*/
  174. pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
  175. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  176. pllmull = ( pllmull >> 18) + 2;
  177. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1;
  178. if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
  179. {
  180. /* HSE used as PLL clock source : SystemCoreClock = HSE/PREDIV * PLLMUL */
  181. SystemCoreClock = (HSE_VALUE/predivfactor) * pllmull;
  182. }
  183. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F091xC) || defined(STM32F098xx)
  184. else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV)
  185. {
  186. /* HSI48 used as PLL clock source : SystemCoreClock = HSI48/PREDIV * PLLMUL */
  187. SystemCoreClock = (HSI48_VALUE/predivfactor) * pllmull;
  188. }
  189. #endif /* STM32F042x6 || STM32F048xx || STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx */
  190. else
  191. {
  192. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F070x6) \
  193. || defined(STM32F078xx) || defined(STM32F071xB) || defined(STM32F072xB) \
  194. || defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx) || defined(STM32F030xC)
  195. /* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */
  196. SystemCoreClock = (HSI_VALUE/predivfactor) * pllmull;
  197. #else
  198. /* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */
  199. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  200. #endif /* STM32F042x6 || STM32F048xx || STM32F070x6 ||
  201. STM32F071xB || STM32F072xB || STM32F078xx || STM32F070xB ||
  202. STM32F091xC || STM32F098xx || STM32F030xC */
  203. }
  204. break;
  205. default: /* HSI used as system clock */
  206. SystemCoreClock = HSI_VALUE;
  207. break;
  208. }
  209. /* Compute HCLK clock frequency ----------------*/
  210. /* Get HCLK prescaler */
  211. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
  212. /* HCLK clock frequency */
  213. SystemCoreClock >>= tmp;
  214. }
  215. /**
  216. * @}
  217. */
  218. /**
  219. * @}
  220. */
  221. /**
  222. * @}
  223. */