You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_ll_rcc.c 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_ll_rcc.c
  4. * @author MCD Application Team
  5. * @brief RCC LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright(c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32l1xx_ll_rcc.h"
  22. /** @addtogroup STM32L1xx_LL_Driver
  23. * @{
  24. */
  25. #if defined(RCC)
  26. /** @defgroup RCC_LL RCC
  27. * @{
  28. */
  29. /* Private types -------------------------------------------------------------*/
  30. /* Private variables ---------------------------------------------------------*/
  31. /* Private constants ---------------------------------------------------------*/
  32. /* Private macros ------------------------------------------------------------*/
  33. /* Private function prototypes -----------------------------------------------*/
  34. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  35. * @{
  36. */
  37. uint32_t RCC_GetSystemClockFreq(void);
  38. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  39. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  40. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency);
  41. uint32_t RCC_PLL_GetFreqDomain_SYS(void);
  42. /**
  43. * @}
  44. */
  45. /* Exported functions --------------------------------------------------------*/
  46. /** @addtogroup RCC_LL_Exported_Functions
  47. * @{
  48. */
  49. /** @addtogroup RCC_LL_EF_Init
  50. * @{
  51. */
  52. /**
  53. * @brief Reset the RCC clock configuration to the default reset state.
  54. * @note The default reset state of the clock configuration is given below:
  55. * - MSI ON and used as system clock source
  56. * - HSE, HSI and PLL OFF
  57. * - AHB, APB1 and APB2 prescaler set to 1.
  58. * - CSS, MCO OFF
  59. * - All interrupts disabled
  60. * @note This function doesn't modify the configuration of the
  61. * - Peripheral clocks
  62. * - LSI, LSE and RTC clocks
  63. * @retval An ErrorStatus enumeration value:
  64. * - SUCCESS: RCC registers are de-initialized
  65. * - ERROR: not applicable
  66. */
  67. ErrorStatus LL_RCC_DeInit(void)
  68. {
  69. __IO uint32_t vl_mask;
  70. /* Set MSION bit */
  71. LL_RCC_MSI_Enable();
  72. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  73. while (LL_RCC_MSI_IsReady() == 0U)
  74. {
  75. __NOP();
  76. }
  77. /* Set MSIRANGE default value */
  78. LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_5);
  79. /* Set MSITRIM bits to the reset value*/
  80. LL_RCC_MSI_SetCalibTrimming(0U);
  81. /* Set HSITRIM bits to the reset value*/
  82. LL_RCC_HSI_SetCalibTrimming(0x10U);
  83. /* Reset SW, HPRE, PPRE and MCOSEL bits */
  84. vl_mask = 0xFFFFFFFFU;
  85. CLEAR_BIT(vl_mask, (RCC_CFGR_SW | RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 | RCC_CFGR_MCOSEL));
  86. LL_RCC_WriteReg(CFGR, vl_mask);
  87. /* Read CR register */
  88. vl_mask = LL_RCC_ReadReg(CR);
  89. /* Reset HSION, HSEON, CSSON, PLLON bits */
  90. CLEAR_BIT(vl_mask, (RCC_CR_PLLON | RCC_CR_CSSON | RCC_CR_HSEON | RCC_CR_HSION));
  91. LL_RCC_WriteReg(CR, vl_mask);
  92. /* Reset HSEBYP bit */
  93. LL_RCC_HSE_DisableBypass();
  94. /* Insure PLL is disabled before to reset PLLSRC/PLLMUL/PLLDIV in CFGR register */
  95. while(LL_RCC_PLL_IsReady() != 0U) {};
  96. /* Reset CFGR register */
  97. LL_RCC_WriteReg(CFGR, 0x00000000U);
  98. /* Disable all interrupts */
  99. LL_RCC_WriteReg(CIR, 0x00000000U);
  100. /* Clear pending flags */
  101. #if defined(RCC_LSECSS_SUPPORT)
  102. vl_mask = (LL_RCC_CIR_LSIRDYC | LL_RCC_CIR_LSERDYC | LL_RCC_CIR_HSIRDYC | LL_RCC_CIR_HSERDYC | \
  103. LL_RCC_CIR_PLLRDYC | LL_RCC_CIR_MSIRDYC | LL_RCC_CIR_LSECSSC | LL_RCC_CIR_CSSC);
  104. #else
  105. vl_mask = (LL_RCC_CIR_LSIRDYC | LL_RCC_CIR_LSERDYC | LL_RCC_CIR_HSIRDYC | LL_RCC_CIR_HSERDYC | \
  106. LL_RCC_CIR_PLLRDYC | LL_RCC_CIR_MSIRDYC | LL_RCC_CIR_CSSC);
  107. #endif /* RCC_LSECSS_SUPPORT */
  108. LL_RCC_WriteReg(CIR, vl_mask);
  109. /* Clear reset flags */
  110. LL_RCC_ClearResetFlags();
  111. return SUCCESS;
  112. }
  113. /**
  114. * @}
  115. */
  116. /** @addtogroup RCC_LL_EF_Get_Freq
  117. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  118. * and different peripheral clocks available on the device.
  119. * @note If SYSCLK source is MSI, function returns values based on MSI clock(*)
  120. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  121. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  122. * @note If SYSCLK source is PLL, function returns values based on
  123. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  124. * @note (*) MSI clock depends on the selected MSI range but the real value
  125. * may vary depending on the variations in voltage and temperature.
  126. * @note (**) HSI_VALUE is a defined constant but the real value may vary
  127. * depending on the variations in voltage and temperature.
  128. * @note (***) HSE_VALUE is a defined constant, user has to ensure that
  129. * HSE_VALUE is same as the real frequency of the crystal used.
  130. * Otherwise, this function may have wrong result.
  131. * @note The result of this function could be incorrect when using fractional
  132. * value for HSE crystal.
  133. * @note This function can be used by the user application to compute the
  134. * baud-rate for the communication peripherals or configure other parameters.
  135. * @{
  136. */
  137. /**
  138. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  139. * @note Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function
  140. * must be called to update structure fields. Otherwise, any
  141. * configuration based on this function will be incorrect.
  142. * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  143. * @retval None
  144. */
  145. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  146. {
  147. /* Get SYSCLK frequency */
  148. RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  149. /* HCLK clock frequency */
  150. RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  151. /* PCLK1 clock frequency */
  152. RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  153. /* PCLK2 clock frequency */
  154. RCC_Clocks->PCLK2_Frequency = RCC_GetPCLK2ClockFreq(RCC_Clocks->HCLK_Frequency);
  155. }
  156. /**
  157. * @}
  158. */
  159. /**
  160. * @}
  161. */
  162. /** @addtogroup RCC_LL_Private_Functions
  163. * @{
  164. */
  165. /**
  166. * @brief Return SYSTEM clock frequency
  167. * @retval SYSTEM clock frequency (in Hz)
  168. */
  169. uint32_t RCC_GetSystemClockFreq(void)
  170. {
  171. uint32_t frequency;
  172. /* Get SYSCLK source -------------------------------------------------------*/
  173. switch (LL_RCC_GetSysClkSource())
  174. {
  175. case LL_RCC_SYS_CLKSOURCE_STATUS_MSI: /* MSI used as system clock source */
  176. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  177. break;
  178. case LL_RCC_SYS_CLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
  179. frequency = HSI_VALUE;
  180. break;
  181. case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
  182. frequency = HSE_VALUE;
  183. break;
  184. case LL_RCC_SYS_CLKSOURCE_STATUS_PLL: /* PLL used as system clock source */
  185. frequency = RCC_PLL_GetFreqDomain_SYS();
  186. break;
  187. default:
  188. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  189. break;
  190. }
  191. return frequency;
  192. }
  193. /**
  194. * @brief Return HCLK clock frequency
  195. * @param SYSCLK_Frequency SYSCLK clock frequency
  196. * @retval HCLK clock frequency (in Hz)
  197. */
  198. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  199. {
  200. /* HCLK clock frequency */
  201. return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  202. }
  203. /**
  204. * @brief Return PCLK1 clock frequency
  205. * @param HCLK_Frequency HCLK clock frequency
  206. * @retval PCLK1 clock frequency (in Hz)
  207. */
  208. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  209. {
  210. /* PCLK1 clock frequency */
  211. return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  212. }
  213. /**
  214. * @brief Return PCLK2 clock frequency
  215. * @param HCLK_Frequency HCLK clock frequency
  216. * @retval PCLK2 clock frequency (in Hz)
  217. */
  218. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency)
  219. {
  220. /* PCLK2 clock frequency */
  221. return __LL_RCC_CALC_PCLK2_FREQ(HCLK_Frequency, LL_RCC_GetAPB2Prescaler());
  222. }
  223. /**
  224. * @brief Return PLL clock frequency used for system domain
  225. * @retval PLL clock frequency (in Hz)
  226. */
  227. uint32_t RCC_PLL_GetFreqDomain_SYS(void)
  228. {
  229. uint32_t pllsource, pllinputfreq;
  230. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL divider) * PLL Multiplicator */
  231. /* Get PLL source */
  232. pllsource = LL_RCC_PLL_GetMainSource();
  233. switch (pllsource)
  234. {
  235. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  236. pllinputfreq = HSI_VALUE;
  237. break;
  238. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  239. pllinputfreq = HSE_VALUE;
  240. break;
  241. default:
  242. pllinputfreq = HSI_VALUE;
  243. break;
  244. }
  245. return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetMultiplicator(), LL_RCC_PLL_GetDivider());
  246. }
  247. /**
  248. * @}
  249. */
  250. /**
  251. * @}
  252. */
  253. #endif /* defined(RCC) */
  254. /**
  255. * @}
  256. */
  257. #endif /* USE_FULL_LL_DRIVER */
  258. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/