system_stm32l4xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. ******************************************************************************
  3. * @file system_stm32l4xx.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M4 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_stm32l4xx.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. * After each device reset the MSI (4 MHz) is used as system clock source.
  22. * Then SystemInit() function is called, in "startup_stm32l4xx.s" file, to
  23. * configure the system clock before to branch to main program.
  24. *
  25. * This file configures the system clock as follows:
  26. *=============================================================================
  27. *-----------------------------------------------------------------------------
  28. * System Clock source | MSI
  29. *-----------------------------------------------------------------------------
  30. * SYSCLK(Hz) | 4000000
  31. *-----------------------------------------------------------------------------
  32. * HCLK(Hz) | 4000000
  33. *-----------------------------------------------------------------------------
  34. * AHB Prescaler | 1
  35. *-----------------------------------------------------------------------------
  36. * APB1 Prescaler | 1
  37. *-----------------------------------------------------------------------------
  38. * APB2 Prescaler | 1
  39. *-----------------------------------------------------------------------------
  40. * PLL_M | 1
  41. *-----------------------------------------------------------------------------
  42. * PLL_N | 8
  43. *-----------------------------------------------------------------------------
  44. * PLL_P | 7
  45. *-----------------------------------------------------------------------------
  46. * PLL_Q | 2
  47. *-----------------------------------------------------------------------------
  48. * PLL_R | 2
  49. *-----------------------------------------------------------------------------
  50. * PLLSAI1_P | NA
  51. *-----------------------------------------------------------------------------
  52. * PLLSAI1_Q | NA
  53. *-----------------------------------------------------------------------------
  54. * PLLSAI1_R | NA
  55. *-----------------------------------------------------------------------------
  56. * PLLSAI2_P | NA
  57. *-----------------------------------------------------------------------------
  58. * PLLSAI2_Q | NA
  59. *-----------------------------------------------------------------------------
  60. * PLLSAI2_R | NA
  61. *-----------------------------------------------------------------------------
  62. * Require 48MHz for USB OTG FS, | Disabled
  63. * SDIO and RNG clock |
  64. *-----------------------------------------------------------------------------
  65. *=============================================================================
  66. ******************************************************************************
  67. * @attention
  68. *
  69. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  70. *
  71. * Redistribution and use in source and binary forms, with or without modification,
  72. * are permitted provided that the following conditions are met:
  73. * 1. Redistributions of source code must retain the above copyright notice,
  74. * this list of conditions and the following disclaimer.
  75. * 2. Redistributions in binary form must reproduce the above copyright notice,
  76. * this list of conditions and the following disclaimer in the documentation
  77. * and/or other materials provided with the distribution.
  78. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  79. * may be used to endorse or promote products derived from this software
  80. * without specific prior written permission.
  81. *
  82. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  83. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  84. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  85. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  86. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  87. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  88. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  89. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  90. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  91. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  92. *
  93. ******************************************************************************
  94. */
  95. /** @addtogroup CMSIS
  96. * @{
  97. */
  98. /** @addtogroup stm32l4xx_system
  99. * @{
  100. */
  101. /** @addtogroup STM32L4xx_System_Private_Includes
  102. * @{
  103. */
  104. #include "stm32l4xx.h"
  105. #if !defined (HSE_VALUE)
  106. #define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */
  107. #endif /* HSE_VALUE */
  108. #if !defined (MSI_VALUE)
  109. #define MSI_VALUE 4000000U /*!< Value of the Internal oscillator in Hz*/
  110. #endif /* MSI_VALUE */
  111. #if !defined (HSI_VALUE)
  112. #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/
  113. #endif /* HSI_VALUE */
  114. /**
  115. * @}
  116. */
  117. /** @addtogroup STM32L4xx_System_Private_TypesDefinitions
  118. * @{
  119. */
  120. /**
  121. * @}
  122. */
  123. /** @addtogroup STM32L4xx_System_Private_Defines
  124. * @{
  125. */
  126. /************************* Miscellaneous Configuration ************************/
  127. /*!< Uncomment the following line if you need to relocate your vector Table in
  128. Internal SRAM. */
  129. /* #define VECT_TAB_SRAM */
  130. #define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field.
  131. This value must be a multiple of 0x200. */
  132. /******************************************************************************/
  133. /**
  134. * @}
  135. */
  136. /** @addtogroup STM32L4xx_System_Private_Macros
  137. * @{
  138. */
  139. /**
  140. * @}
  141. */
  142. /** @addtogroup STM32L4xx_System_Private_Variables
  143. * @{
  144. */
  145. /* The SystemCoreClock variable is updated in three ways:
  146. 1) by calling CMSIS function SystemCoreClockUpdate()
  147. 2) by calling HAL API function HAL_RCC_GetHCLKFreq()
  148. 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  149. Note: If you use this function to configure the system clock; then there
  150. is no need to call the 2 first functions listed above, since SystemCoreClock
  151. variable is updated automatically.
  152. */
  153. uint32_t SystemCoreClock = 4000000U;
  154. const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
  155. const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
  156. const uint32_t MSIRangeTable[12] = {100000U, 200000U, 400000U, 800000U, 1000000U, 2000000U, \
  157. 4000000U, 8000000U, 16000000U, 24000000U, 32000000U, 48000000U};
  158. /**
  159. * @}
  160. */
  161. /** @addtogroup STM32L4xx_System_Private_FunctionPrototypes
  162. * @{
  163. */
  164. /**
  165. * @}
  166. */
  167. /** @addtogroup STM32L4xx_System_Private_Functions
  168. * @{
  169. */
  170. /**
  171. * @brief Setup the microcontroller system.
  172. * @param None
  173. * @retval None
  174. */
  175. void SystemInit(void)
  176. {
  177. /* FPU settings ------------------------------------------------------------*/
  178. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  179. SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
  180. #endif
  181. /* Reset the RCC clock configuration to the default reset state ------------*/
  182. /* Set MSION bit */
  183. RCC->CR |= RCC_CR_MSION;
  184. /* Reset CFGR register */
  185. RCC->CFGR = 0x00000000U;
  186. /* Reset HSEON, CSSON , HSION, and PLLON bits */
  187. RCC->CR &= 0xEAF6FFFFU;
  188. /* Reset PLLCFGR register */
  189. RCC->PLLCFGR = 0x00001000U;
  190. /* Reset HSEBYP bit */
  191. RCC->CR &= 0xFFFBFFFFU;
  192. /* Disable all interrupts */
  193. RCC->CIER = 0x00000000U;
  194. /* Configure the Vector Table location add offset address ------------------*/
  195. #ifdef VECT_TAB_SRAM
  196. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  197. #else
  198. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  199. #endif
  200. }
  201. /**
  202. * @brief Update SystemCoreClock variable according to Clock Register Values.
  203. * The SystemCoreClock variable contains the core clock (HCLK), it can
  204. * be used by the user application to setup the SysTick timer or configure
  205. * other parameters.
  206. *
  207. * @note Each time the core clock (HCLK) changes, this function must be called
  208. * to update SystemCoreClock variable value. Otherwise, any configuration
  209. * based on this variable will be incorrect.
  210. *
  211. * @note - The system frequency computed by this function is not the real
  212. * frequency in the chip. It is calculated based on the predefined
  213. * constant and the selected clock source:
  214. *
  215. * - If SYSCLK source is MSI, SystemCoreClock will contain the MSI_VALUE(*)
  216. *
  217. * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
  218. *
  219. * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
  220. *
  221. * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
  222. * or HSI_VALUE(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
  223. *
  224. * (*) MSI_VALUE is a constant defined in stm32l4xx_hal.h file (default value
  225. * 4 MHz) but the real value may vary depending on the variations
  226. * in voltage and temperature.
  227. *
  228. * (**) HSI_VALUE is a constant defined in stm32l4xx_hal.h file (default value
  229. * 16 MHz) but the real value may vary depending on the variations
  230. * in voltage and temperature.
  231. *
  232. * (***) HSE_VALUE is a constant defined in stm32l4xx_hal.h file (default value
  233. * 8 MHz), user has to ensure that HSE_VALUE is same as the real
  234. * frequency of the crystal used. Otherwise, this function may
  235. * have wrong result.
  236. *
  237. * - The result of this function could be not correct when using fractional
  238. * value for HSE crystal.
  239. *
  240. * @param None
  241. * @retval None
  242. */
  243. void SystemCoreClockUpdate(void)
  244. {
  245. uint32_t tmp = 0U, msirange = 0U, pllvco = 0U, pllr = 2U, pllsource = 0U, pllm = 2U;
  246. /* Get MSI Range frequency--------------------------------------------------*/
  247. if((RCC->CR & RCC_CR_MSIRGSEL) == RESET)
  248. { /* MSISRANGE from RCC_CSR applies */
  249. msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> 8U;
  250. }
  251. else
  252. { /* MSIRANGE from RCC_CR applies */
  253. msirange = (RCC->CR & RCC_CR_MSIRANGE) >> 4U;
  254. }
  255. /*MSI frequency range in HZ*/
  256. msirange = MSIRangeTable[msirange];
  257. /* Get SYSCLK source -------------------------------------------------------*/
  258. switch (RCC->CFGR & RCC_CFGR_SWS)
  259. {
  260. case 0x00: /* MSI used as system clock source */
  261. SystemCoreClock = msirange;
  262. break;
  263. case 0x04: /* HSI used as system clock source */
  264. SystemCoreClock = HSI_VALUE;
  265. break;
  266. case 0x08: /* HSE used as system clock source */
  267. SystemCoreClock = HSE_VALUE;
  268. break;
  269. case 0x0C: /* PLL used as system clock source */
  270. /* PLL_VCO = (HSE_VALUE or HSI_VALUE or MSI_VALUE/ PLLM) * PLLN
  271. SYSCLK = PLL_VCO / PLLR
  272. */
  273. pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
  274. pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4U) + 1U ;
  275. switch (pllsource)
  276. {
  277. case 0x02: /* HSI used as PLL clock source */
  278. pllvco = (HSI_VALUE / pllm);
  279. break;
  280. case 0x03: /* HSE used as PLL clock source */
  281. pllvco = (HSE_VALUE / pllm);
  282. break;
  283. default: /* MSI used as PLL clock source */
  284. pllvco = (msirange / pllm);
  285. break;
  286. }
  287. pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8U);
  288. pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25U) + 1U) * 2U;
  289. SystemCoreClock = pllvco/pllr;
  290. break;
  291. default:
  292. SystemCoreClock = msirange;
  293. break;
  294. }
  295. /* Compute HCLK clock frequency --------------------------------------------*/
  296. /* Get HCLK prescaler */
  297. tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
  298. /* HCLK clock frequency */
  299. SystemCoreClock >>= tmp;
  300. }
  301. /**
  302. * @}
  303. */
  304. /**
  305. * @}
  306. */
  307. /**
  308. * @}
  309. */
  310. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/