system_stm32l0xx.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  26. *
  27. * Redistribution and use in source and binary forms, with or without modification,
  28. * are permitted provided that the following conditions are met:
  29. * 1. Redistributions of source code must retain the above copyright notice,
  30. * this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright notice,
  32. * this list of conditions and the following disclaimer in the documentation
  33. * and/or other materials provided with the distribution.
  34. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  44. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  47. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. *
  49. ******************************************************************************
  50. */
  51. /** @addtogroup CMSIS
  52. * @{
  53. */
  54. /** @addtogroup stm32l0xx_system
  55. * @{
  56. */
  57. /** @addtogroup STM32L0xx_System_Private_Includes
  58. * @{
  59. */
  60. #include "stm32l0xx.h"
  61. #if !defined (HSE_VALUE)
  62. #define HSE_VALUE ((uint32_t)8000000U) /*!< Value of the External oscillator in Hz */
  63. #endif /* HSE_VALUE */
  64. #if !defined (MSI_VALUE)
  65. #define MSI_VALUE ((uint32_t)2000000U) /*!< Value of the Internal oscillator in Hz*/
  66. #endif /* MSI_VALUE */
  67. #if !defined (HSI_VALUE)
  68. #define HSI_VALUE ((uint32_t)16000000U) /*!< Value of the Internal oscillator in Hz*/
  69. #endif /* HSI_VALUE */
  70. /**
  71. * @}
  72. */
  73. /** @addtogroup STM32L0xx_System_Private_TypesDefinitions
  74. * @{
  75. */
  76. /**
  77. * @}
  78. */
  79. /** @addtogroup STM32L0xx_System_Private_Defines
  80. * @{
  81. */
  82. /************************* Miscellaneous Configuration ************************/
  83. /*!< Uncomment the following line if you need to relocate your vector Table in
  84. Internal SRAM. */
  85. /* #define VECT_TAB_SRAM */
  86. #define VECT_TAB_OFFSET 0x00U /*!< Vector Table base offset field.
  87. This value must be a multiple of 0x200. */
  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 = 2000000U;
  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. /*!< Set MSION bit */
  133. RCC->CR |= (uint32_t)0x00000100U;
  134. /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */
  135. RCC->CFGR &= (uint32_t) 0x88FF400CU;
  136. /*!< Reset HSION, HSIDIVEN, HSEON, CSSON and PLLON bits */
  137. RCC->CR &= (uint32_t)0xFEF6FFF6U;
  138. /*!< Reset HSI48ON bit */
  139. RCC->CRRCR &= (uint32_t)0xFFFFFFFEU;
  140. /*!< Reset HSEBYP bit */
  141. RCC->CR &= (uint32_t)0xFFFBFFFFU;
  142. /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */
  143. RCC->CFGR &= (uint32_t)0xFF02FFFFU;
  144. /*!< Disable all interrupts */
  145. RCC->CIER = 0x00000000U;
  146. /* Configure the Vector Table location add offset address ------------------*/
  147. #ifdef VECT_TAB_SRAM
  148. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  149. #else
  150. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  151. #endif
  152. }
  153. /**
  154. * @brief Update SystemCoreClock according to Clock Register Values
  155. * The SystemCoreClock variable contains the core clock (HCLK), it can
  156. * be used by the user application to setup the SysTick timer or configure
  157. * other parameters.
  158. *
  159. * @note Each time the core clock (HCLK) changes, this function must be called
  160. * to update SystemCoreClock variable value. Otherwise, any configuration
  161. * based on this variable will be incorrect.
  162. *
  163. * @note - The system frequency computed by this function is not the real
  164. * frequency in the chip. It is calculated based on the predefined
  165. * constant and the selected clock source:
  166. *
  167. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI
  168. * value as defined by the MSI range.
  169. *
  170. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
  171. *
  172. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**)
  173. *
  174. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**)
  175. * or HSI_VALUE(*) multiplied/divided by the PLL factors.
  176. *
  177. * (*) HSI_VALUE is a constant defined in stm32l0xx_hal.h file (default value
  178. * 16 MHz) but the real value may vary depending on the variations
  179. * in voltage and temperature.
  180. *
  181. * (**) HSE_VALUE is a constant defined in stm32l0xx_hal.h file (default value
  182. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  183. * frequency of the crystal used. Otherwise, this function may
  184. * have wrong result.
  185. *
  186. * - The result of this function could be not correct when using fractional
  187. * value for HSE crystal.
  188. * @param None
  189. * @retval None
  190. */
  191. void SystemCoreClockUpdate (void)
  192. {
  193. uint32_t tmp = 0U, pllmul = 0U, plldiv = 0U, pllsource = 0U, msirange = 0U;
  194. /* Get SYSCLK source -------------------------------------------------------*/
  195. tmp = RCC->CFGR & RCC_CFGR_SWS;
  196. switch (tmp)
  197. {
  198. case 0x00U: /* MSI used as system clock */
  199. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13U;
  200. SystemCoreClock = (32768U * (1U << (msirange + 1U)));
  201. break;
  202. case 0x04U: /* HSI used as system clock */
  203. SystemCoreClock = HSI_VALUE;
  204. break;
  205. case 0x08U: /* HSE used as system clock */
  206. SystemCoreClock = HSE_VALUE;
  207. break;
  208. case 0x0CU: /* PLL used as system clock */
  209. /* Get PLL clock source and multiplication factor ----------------------*/
  210. pllmul = RCC->CFGR & RCC_CFGR_PLLMUL;
  211. plldiv = RCC->CFGR & RCC_CFGR_PLLDIV;
  212. pllmul = PLLMulTable[(pllmul >> 18U)];
  213. plldiv = (plldiv >> 22U) + 1U;
  214. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  215. if (pllsource == 0x00U)
  216. {
  217. /* HSI oscillator clock selected as PLL clock entry */
  218. SystemCoreClock = (((HSI_VALUE) * pllmul) / plldiv);
  219. }
  220. else
  221. {
  222. /* HSE selected as PLL clock entry */
  223. SystemCoreClock = (((HSE_VALUE) * pllmul) / plldiv);
  224. }
  225. break;
  226. default: /* MSI used as system clock */
  227. msirange = (RCC->ICSCR & RCC_ICSCR_MSIRANGE) >> 13U;
  228. SystemCoreClock = (32768U * (1U << (msirange + 1U)));
  229. break;
  230. }
  231. /* Compute HCLK clock frequency --------------------------------------------*/
  232. /* Get HCLK prescaler */
  233. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
  234. /* HCLK clock frequency */
  235. SystemCoreClock >>= tmp;
  236. }
  237. /**
  238. * @}
  239. */
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @}
  245. */
  246. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/