system_stm32f0xx.c 8.5 KB

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