system_stm32l0xx.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32l0xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M0+ 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_stm32l0xx.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 stm32l0xx_system
  38. * @{
  39. */
  40. /** @addtogroup STM32L0xx_System_Private_Includes
  41. * @{
  42. */
  43. #include "stm32l0xx.h"
  44. #if !defined (HSE_VALUE)
  45. #define HSE_VALUE ((uint32_t)8000000U) /*!< Value of the External oscillator in Hz */
  46. #endif /* HSE_VALUE */
  47. #if !defined (MSI_VALUE)
  48. #define MSI_VALUE ((uint32_t)2097152U) /*!< Value of the Internal oscillator in Hz*/
  49. #endif /* MSI_VALUE */
  50. #if !defined (HSI_VALUE)
  51. #define HSI_VALUE ((uint32_t)16000000U) /*!< Value of the Internal oscillator in Hz*/
  52. #endif /* HSI_VALUE */
  53. /**
  54. * @}
  55. */
  56. /** @addtogroup STM32L0xx_System_Private_TypesDefinitions
  57. * @{
  58. */
  59. /**
  60. * @}
  61. */
  62. /** @addtogroup STM32L0xx_System_Private_Defines
  63. * @{
  64. */
  65. /************************* Miscellaneous Configuration ************************/
  66. /* Note: Following vector table addresses must be defined in line with linker
  67. configuration. */
  68. /*!< Uncomment the following line if you need to relocate the vector table
  69. anywhere in Flash or Sram, else the vector table is kept at the automatic
  70. remap of boot address selected */
  71. /* #define USER_VECT_TAB_ADDRESS */
  72. #if defined(USER_VECT_TAB_ADDRESS)
  73. /*!< Uncomment the following line if you need to relocate your vector Table
  74. in Sram else user remap will be done in Flash. */
  75. /* #define VECT_TAB_SRAM */
  76. #if defined(VECT_TAB_SRAM)
  77. #define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field.
  78. This value must be a multiple of 0x200. */
  79. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  80. This value must be a multiple of 0x200. */
  81. #else
  82. #define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field.
  83. This value must be a multiple of 0x200. */
  84. #define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field.
  85. This value must be a multiple of 0x200. */
  86. #endif /* VECT_TAB_SRAM */
  87. #endif /* USER_VECT_TAB_ADDRESS */
  88. /******************************************************************************/
  89. /**
  90. * @}
  91. */
  92. /** @addtogroup STM32L0xx_System_Private_Macros
  93. * @{
  94. */
  95. /**
  96. * @}
  97. */
  98. /** @addtogroup STM32L0xx_System_Private_Variables
  99. * @{
  100. */
  101. /* This variable is updated in three ways:
  102. 1) by calling CMSIS function SystemCoreClockUpdate()
  103. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  104. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  105. Note: If you use this function to configure the system clock; then there
  106. is no need to call the 2 first functions listed above, since SystemCoreClock
  107. variable is updated automatically.
  108. */
  109. uint32_t SystemCoreClock = 2097152U; /* 32.768 kHz * 2^6 */
  110. const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  111. const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
  112. const uint8_t PLLMulTable[9] = {3U, 4U, 6U, 8U, 12U, 16U, 24U, 32U, 48U};
  113. /**
  114. * @}
  115. */
  116. /** @addtogroup STM32L0xx_System_Private_FunctionPrototypes
  117. * @{
  118. */
  119. /**
  120. * @}
  121. */
  122. /** @addtogroup STM32L0xx_System_Private_Functions
  123. * @{
  124. */
  125. /**
  126. * @brief Setup the microcontroller system.
  127. * @param None
  128. * @retval None
  129. */
  130. void SystemInit (void)
  131. {
  132. /* Configure the Vector Table location add offset address ------------------*/
  133. #if defined (USER_VECT_TAB_ADDRESS)
  134. SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  135. #endif /* USER_VECT_TAB_ADDRESS */
  136. }
  137. /**
  138. * @brief Update SystemCoreClock variable according to Clock Register Values.
  139. * The SystemCoreClock variable contains the core clock (HCLK), it can
  140. * be used by the user application to setup the SysTick timer or configure
  141. * other parameters.
  142. *
  143. * @note Each time the core clock (HCLK) changes, this function must be called
  144. * to update SystemCoreClock variable value. Otherwise, any configuration
  145. * based on this variable will be incorrect.
  146. *
  147. * @note - The system frequency computed by this function is not the real
  148. * frequency in the chip. It is calculated based on the predefined
  149. * constant and the selected clock source:
  150. *
  151. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
  152. * value as defined by the MSI range.
  153. *
  154. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  155. *
  156. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  157. *
  158. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  159. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  160. *
  161. * (*) HSI_VALUE is a constant defined in stm32l0xx_hal.h file (default value
  162. * 16 MHz) but the real value may vary depending on the variations
  163. * in voltage and temperature.
  164. *
  165. * (**) HSE_VALUE is a constant defined in stm32l0xx_hal.h file (default value
  166. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  167. * frequency of the crystal used. Otherwise, this function may
  168. * have wrong result.
  169. *
  170. * - The result of this function could be not correct when using fractional
  171. * value for HSE crystal.
  172. * @param None
  173. * @retval None
  174. */
  175. void SystemCoreClockUpdate (void)
  176. {
  177. uint32_t tmp = 0U, pllmul = 0U, plldiv = 0U, pllsource = 0U, msirange = 0U;
  178. /* Get SYSCLK source -------------------------------------------------------*/
  179. tmp = RCC->CFGR & RCC_CFGR_SWS;
  180. switch (tmp)
  181. {
  182. case 0x00U: /* MSI used as system clock */
  183. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> RCC_ICSCR_MSIRANGE_Pos;
  184. SystemCoreClock = (32768U * (1U << (msirange + 1U)));
  185. break;
  186. case 0x04U: /* HSI used as system clock */
  187. if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
  188. {
  189. SystemCoreClock = HSI_VALUE / 4U;
  190. }
  191. else
  192. {
  193. SystemCoreClock = HSI_VALUE;
  194. }
  195. break;
  196. case 0x08U: /* HSE used as system clock */
  197. SystemCoreClock = HSE_VALUE;
  198. break;
  199. default: /* PLL used as system clock */
  200. /* Get PLL clock source and multiplication factor ----------------------*/
  201. pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
  202. plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
  203. pllmul = PLLMulTable[(pllmul >> RCC_CFGR_PLLMUL_Pos)];
  204. plldiv = (plldiv >> RCC_CFGR_PLLDIV_Pos) + 1U;
  205. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  206. if (pllsource == 0x00U)
  207. {
  208. /* HSI oscillator clock selected as PLL clock entry */
  209. if ((RCC->CR & RCC_CR_HSIDIVF) != 0U)
  210. {
  211. SystemCoreClock = (((HSI_VALUE / 4U) * pllmul) / plldiv);
  212. }
  213. else
  214. {
  215. SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
  216. }
  217. }
  218. else
  219. {
  220. /* HSE selected as PLL clock entry */
  221. SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
  222. }
  223. break;
  224. }
  225. /* Compute HCLK clock frequency --------------------------------------------*/
  226. /* Get HCLK prescaler */
  227. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> RCC_CFGR_HPRE_Pos)];
  228. /* HCLK clock frequency */
  229. SystemCoreClock >>= tmp;
  230. }
  231. /**
  232. * @}
  233. */
  234. /**
  235. * @}
  236. */
  237. /**
  238. * @}
  239. */