stm32l1xx_ll_rcc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l1xx_ll_rcc.h"
  38. /** @addtogroup STM32L1xx_LL_Driver
  39. * @{
  40. */
  41. #if defined(RCC)
  42. /** @defgroup RCC_LL RCC
  43. * @{
  44. */
  45. /* Private types -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private constants ---------------------------------------------------------*/
  48. /* Private macros ------------------------------------------------------------*/
  49. /* Private function prototypes -----------------------------------------------*/
  50. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  51. * @{
  52. */
  53. uint32_t RCC_GetSystemClockFreq(void);
  54. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  55. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  56. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency);
  57. uint32_t RCC_PLL_GetFreqDomain_SYS(void);
  58. /**
  59. * @}
  60. */
  61. /* Exported functions --------------------------------------------------------*/
  62. /** @addtogroup RCC_LL_Exported_Functions
  63. * @{
  64. */
  65. /** @addtogroup RCC_LL_EF_Init
  66. * @{
  67. */
  68. /**
  69. * @brief Reset the RCC clock configuration to the default reset state.
  70. * @note The default reset state of the clock configuration is given below:
  71. * - MSI ON and used as system clock source
  72. * - HSE, HSI and PLL OFF
  73. * - AHB, APB1 and APB2 prescaler set to 1.
  74. * - CSS, MCO OFF
  75. * - All interrupts disabled
  76. * @note This function doesn't modify the configuration of the
  77. * - Peripheral clocks
  78. * - LSI, LSE and RTC clocks
  79. * @retval An ErrorStatus enumeration value:
  80. * - SUCCESS: RCC registers are de-initialized
  81. * - ERROR: not applicable
  82. */
  83. ErrorStatus LL_RCC_DeInit(void)
  84. {
  85. uint32_t vl_mask = 0U;
  86. /* Set MSION bit */
  87. LL_RCC_MSI_Enable();
  88. /* Insure MSIRDY bit is set before writing default MSIRANGE value */
  89. while (LL_RCC_MSI_IsReady() == 0U)
  90. {
  91. __NOP();
  92. }
  93. /* Set MSIRANGE default value */
  94. LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_5);
  95. /* Set MSITRIM bits to the reset value*/
  96. LL_RCC_MSI_SetCalibTrimming(0U);
  97. /* Set HSITRIM bits to the reset value*/
  98. LL_RCC_HSI_SetCalibTrimming(0x10U);
  99. /* Reset SW, HPRE, PPRE and MCOSEL bits */
  100. vl_mask = 0xFFFFFFFFU;
  101. CLEAR_BIT(vl_mask, (RCC_CFGR_SW | RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 | RCC_CFGR_MCOSEL));
  102. LL_RCC_WriteReg(CFGR, vl_mask);
  103. /* Reset HSION, HSEON, CSSON, PLLON bits */
  104. vl_mask = 0xFFFFFFFFU;
  105. CLEAR_BIT(vl_mask, (RCC_CR_PLLON | RCC_CR_CSSON | RCC_CR_HSEON | RCC_CR_HSION));
  106. LL_RCC_WriteReg(CR, vl_mask);
  107. /* Reset HSEBYP bit */
  108. LL_RCC_HSE_DisableBypass();
  109. /* Reset CFGR register */
  110. LL_RCC_WriteReg(CFGR, 0x00000000U);
  111. /* Clear pending flags */
  112. #if defined(RCC_LSECSS_SUPPORT)
  113. vl_mask = (LL_RCC_CIR_LSIRDYC | LL_RCC_CIR_LSERDYC | LL_RCC_CIR_HSIRDYC | LL_RCC_CIR_HSERDYC | LL_RCC_CIR_PLLRDYC | LL_RCC_CIR_MSIRDYC | LL_RCC_CIR_LSECSSC | LL_RCC_CIR_CSSC);
  114. #else
  115. vl_mask = (LL_RCC_CIR_LSIRDYC | LL_RCC_CIR_LSERDYC | LL_RCC_CIR_HSIRDYC | LL_RCC_CIR_HSERDYC | LL_RCC_CIR_PLLRDYC | LL_RCC_CIR_MSIRDYC | LL_RCC_CIR_CSSC);
  116. #endif /* RCC_LSECSS_SUPPORT */
  117. SET_BIT(RCC->CIR, vl_mask);
  118. /* Disable all interrupts */
  119. LL_RCC_WriteReg(CIR, 0x00000000U);
  120. return SUCCESS;
  121. }
  122. /**
  123. * @}
  124. */
  125. /** @addtogroup RCC_LL_EF_Get_Freq
  126. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  127. * and different peripheral clocks available on the device.
  128. * @note If SYSCLK source is MSI, function returns values based on MSI clock(*)
  129. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE(**)
  130. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  131. * @note If SYSCLK source is PLL, function returns values based on
  132. * HSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
  133. * @note (*) MSI clock depends on the selected MSI range but the real value
  134. * may vary depending on the variations in voltage and temperature.
  135. * @note (**) HSI_VALUE is a defined constant but the real value may vary
  136. * depending on the variations in voltage and temperature.
  137. * @note (***) HSE_VALUE is a defined constant, user has to ensure that
  138. * HSE_VALUE is same as the real frequency of the crystal used.
  139. * Otherwise, this function may have wrong result.
  140. * @note The result of this function could be incorrect when using fractional
  141. * value for HSE crystal.
  142. * @note This function can be used by the user application to compute the
  143. * baud-rate for the communication peripherals or configure other parameters.
  144. * @{
  145. */
  146. /**
  147. * @brief Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks
  148. * @note Each time SYSCLK, HCLK, PCLK1 and/or PCLK2 clock changes, this function
  149. * must be called to update structure fields. Otherwise, any
  150. * configuration based on this function will be incorrect.
  151. * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  152. * @retval None
  153. */
  154. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  155. {
  156. /* Get SYSCLK frequency */
  157. RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  158. /* HCLK clock frequency */
  159. RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  160. /* PCLK1 clock frequency */
  161. RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  162. /* PCLK2 clock frequency */
  163. RCC_Clocks->PCLK2_Frequency = RCC_GetPCLK2ClockFreq(RCC_Clocks->HCLK_Frequency);
  164. }
  165. /**
  166. * @}
  167. */
  168. /**
  169. * @}
  170. */
  171. /** @addtogroup RCC_LL_Private_Functions
  172. * @{
  173. */
  174. /**
  175. * @brief Return SYSTEM clock frequency
  176. * @retval SYSTEM clock frequency (in Hz)
  177. */
  178. uint32_t RCC_GetSystemClockFreq(void)
  179. {
  180. uint32_t frequency = 0U;
  181. /* Get SYSCLK source -------------------------------------------------------*/
  182. switch (LL_RCC_GetSysClkSource())
  183. {
  184. case LL_RCC_SYS_CLKSOURCE_STATUS_MSI: /* MSI used as system clock source */
  185. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  186. break;
  187. case LL_RCC_SYS_CLKSOURCE_STATUS_HSI: /* HSI used as system clock source */
  188. frequency = HSI_VALUE;
  189. break;
  190. case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
  191. frequency = HSE_VALUE;
  192. break;
  193. case LL_RCC_SYS_CLKSOURCE_STATUS_PLL: /* PLL used as system clock source */
  194. frequency = RCC_PLL_GetFreqDomain_SYS();
  195. break;
  196. default:
  197. frequency = __LL_RCC_CALC_MSI_FREQ(LL_RCC_MSI_GetRange());
  198. break;
  199. }
  200. return frequency;
  201. }
  202. /**
  203. * @brief Return HCLK clock frequency
  204. * @param SYSCLK_Frequency SYSCLK clock frequency
  205. * @retval HCLK clock frequency (in Hz)
  206. */
  207. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  208. {
  209. /* HCLK clock frequency */
  210. return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  211. }
  212. /**
  213. * @brief Return PCLK1 clock frequency
  214. * @param HCLK_Frequency HCLK clock frequency
  215. * @retval PCLK1 clock frequency (in Hz)
  216. */
  217. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  218. {
  219. /* PCLK1 clock frequency */
  220. return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  221. }
  222. /**
  223. * @brief Return PCLK2 clock frequency
  224. * @param HCLK_Frequency HCLK clock frequency
  225. * @retval PCLK2 clock frequency (in Hz)
  226. */
  227. uint32_t RCC_GetPCLK2ClockFreq(uint32_t HCLK_Frequency)
  228. {
  229. /* PCLK2 clock frequency */
  230. return __LL_RCC_CALC_PCLK2_FREQ(HCLK_Frequency, LL_RCC_GetAPB2Prescaler());
  231. }
  232. /**
  233. * @brief Return PLL clock frequency used for system domain
  234. * @retval PLL clock frequency (in Hz)
  235. */
  236. uint32_t RCC_PLL_GetFreqDomain_SYS(void)
  237. {
  238. uint32_t pllinputfreq = 0U, pllsource = 0U;
  239. /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL divider) * PLL Multiplicator */
  240. /* Get PLL source */
  241. pllsource = LL_RCC_PLL_GetMainSource();
  242. switch (pllsource)
  243. {
  244. case LL_RCC_PLLSOURCE_HSI: /* HSI used as PLL clock source */
  245. pllinputfreq = HSI_VALUE;
  246. break;
  247. case LL_RCC_PLLSOURCE_HSE: /* HSE used as PLL clock source */
  248. pllinputfreq = HSE_VALUE;
  249. break;
  250. default:
  251. pllinputfreq = HSI_VALUE;
  252. break;
  253. }
  254. return __LL_RCC_CALC_PLLCLK_FREQ(pllinputfreq, LL_RCC_PLL_GetMultiplicator(), LL_RCC_PLL_GetDivider());
  255. }
  256. /**
  257. * @}
  258. */
  259. /**
  260. * @}
  261. */
  262. #endif /* defined(RCC) */
  263. /**
  264. * @}
  265. */
  266. #endif /* USE_FULL_LL_DRIVER */
  267. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/