py32f002b_ll_rcc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_ll_rcc.c
  4. * @author MCU Application Team
  5. * @brief RCC LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya 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. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. #if defined(USE_FULL_LL_DRIVER)
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "py32f002b_ll_rcc.h"
  33. #ifdef USE_FULL_ASSERT
  34. #include "py32_assert.h"
  35. #else
  36. #define assert_param(expr) ((void)0U)
  37. #endif
  38. /** @addtogroup PY32F002B_LL_Driver
  39. * @{
  40. */
  41. #if defined(RCC)
  42. /** @addtogroup RCC_LL
  43. * @{
  44. */
  45. /* Private types -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private constants ---------------------------------------------------------*/
  48. /* Private macros ------------------------------------------------------------*/
  49. /** @addtogroup RCC_LL_Private_Macros
  50. * @{
  51. */
  52. #define IS_LL_RCC_MCO_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_MCO1_CLKSOURCE))
  53. #if (defined(RCC_CCIPR_COMP1SEL) && defined(RCC_CCIPR_COMP2SEL))
  54. #define IS_LL_RCC_COMP_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_COMP1_CLKSOURCE) \
  55. || ((__VALUE__) == LL_RCC_COMP2_CLKSOURCE))
  56. #endif
  57. #if defined(RCC_CCIPR_LPTIMSEL)
  58. #define IS_LL_RCC_LPTIM_CLKSOURCE(__VALUE__) (((__VALUE__) == LL_RCC_LPTIM1_CLKSOURCE))
  59. #endif /* LPTIM1 */
  60. /**
  61. * @}
  62. */
  63. /* Private function prototypes -----------------------------------------------*/
  64. /** @defgroup RCC_LL_Private_Functions RCC Private functions
  65. * @{
  66. */
  67. uint32_t RCC_GetSystemClockFreq(void);
  68. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency);
  69. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency);
  70. /**
  71. * @}
  72. */
  73. /* Exported functions --------------------------------------------------------*/
  74. /** @addtogroup RCC_LL_Exported_Functions
  75. * @{
  76. */
  77. /** @addtogroup RCC_LL_EF_Init
  78. * @{
  79. */
  80. /**
  81. * @brief Reset the RCC clock configuration to the default reset state.
  82. * @note The default reset state of the clock configuration is given below:
  83. * - HSI ON and used as system clock source
  84. * - AHB and APB1 prescaler set to 1.
  85. * - CSS, MCO OFF
  86. * - All interrupts disabled
  87. * @note This function does not modify the configuration of the
  88. * - Peripheral clocks
  89. * - LSI and LSE clocks
  90. * @retval An ErrorStatus enumeration value:
  91. * - SUCCESS: RCC registers are de-initialized
  92. * - ERROR: not applicable
  93. */
  94. ErrorStatus LL_RCC_DeInit(void)
  95. {
  96. /* Set HSI_FS, HSITRIM bits to default value*/
  97. LL_RCC_HSI_SetCalibFreq(LL_RCC_HSICALIBRATION_24MHz);
  98. /* Set HSION bit and wait for HSI READY bit */
  99. LL_RCC_HSI_Enable();
  100. while (LL_RCC_HSI_IsReady() != 1U)
  101. {}
  102. /* Reset CFGR register */
  103. LL_RCC_WriteReg(CFGR, 0x00000000U);
  104. /* Wait till SYSCLK is HSISYS */
  105. while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSISYS)
  106. {}
  107. /* Reset whole CR register but HSI in 2 steps in case HSEBYP is set */
  108. LL_RCC_WriteReg(CR, RCC_CR_HSION);
  109. /* Disable all interrupts */
  110. LL_RCC_WriteReg(CIER, 0x00000000U);
  111. /* Clear all interrupts flags */
  112. LL_RCC_WriteReg(CICR, 0xFFFFFFFFU);
  113. return SUCCESS;
  114. }
  115. /**
  116. * @}
  117. */
  118. /** @addtogroup RCC_LL_EF_Get_Freq
  119. * @brief Return the frequencies of different on chip clocks; System, AHB and APB1 buses clocks
  120. * and different peripheral clocks available on the device.
  121. * @note If SYSCLK source is HSI, function returns values based on HSI_VALUE divided by HSI division factor(**)
  122. * @note If SYSCLK source is HSE, function returns values based on HSE_VALUE(***)
  123. * or HSI_VALUE(**) multiplied/divided by the PLL factors.
  124. * @note (**) HSI_VALUE is a constant defined in this file (default value
  125. * 8 MHz) but the real value may vary depending on the variations
  126. * in voltage and temperature.
  127. * @note The result of this function could be incorrect when using fractional
  128. * value for HSE crystal.
  129. * @note This function can be used by the user application to compute the
  130. * baud-rate for the communication peripherals or configure other parameters.
  131. * @{
  132. */
  133. /**
  134. * @brief Return the frequencies of different on chip clocks; System, AHB and APB1 buses clocks
  135. * @note Each time SYSCLK, HCLK and/or PCLK1 clock changes, this function
  136. * must be called to update structure fields. Otherwise, any
  137. * configuration based on this function will be incorrect.
  138. * @param RCC_Clocks pointer to a @ref LL_RCC_ClocksTypeDef structure which will hold the clocks frequencies
  139. * @retval None
  140. */
  141. void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
  142. {
  143. /* Get SYSCLK frequency */
  144. RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
  145. /* HCLK clock frequency */
  146. RCC_Clocks->HCLK_Frequency = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
  147. /* PCLK1 clock frequency */
  148. RCC_Clocks->PCLK1_Frequency = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
  149. }
  150. /**
  151. * @brief Return MCO clock frequency
  152. * @param MCOx This parameter can be one of the following values:
  153. * @arg @ref LL_RCC_MCO1_CLKSOURCE
  154. * @retval MCO clock frequency (in Hz)
  155. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (HSE, LSI or LSE) is not ready
  156. * - @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  157. */
  158. uint32_t LL_RCC_GetMCOClockFreq(uint32_t MCOx)
  159. {
  160. uint32_t mco_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  161. /* Check parameter */
  162. assert_param(IS_LL_RCC_MCO_CLKSOURCE(MCOx));
  163. switch (LL_RCC_GetMCOClockSource(MCOx))
  164. {
  165. case LL_RCC_MCO1SOURCE_SYSCLK: /* MCO Clock is SYSCLK */
  166. mco_frequency = SystemCoreClock;
  167. break;
  168. case LL_RCC_MCO1SOURCE_HSI: /* MCO Clock is HSI */
  169. mco_frequency = LL_RCC_HSI_GetFreq();
  170. break;
  171. case LL_RCC_MCO1SOURCE_HSE: /* MCO Clock is HSE */
  172. if (LL_RCC_HSE_IsBypass() == 1U)
  173. {
  174. mco_frequency = HSE_VALUE;
  175. }
  176. break;
  177. case LL_RCC_MCO1SOURCE_LSI: /* MCO Clock is LSI */
  178. if (LL_RCC_LSI_IsReady() == 1U)
  179. {
  180. mco_frequency = LL_RCC_LSI_GetFreq();
  181. }
  182. break;
  183. #if defined(RCC_LSE_SUPPORT)
  184. case LL_RCC_MCO1SOURCE_LSE: /* MCO Clock is LSE */
  185. if (LL_RCC_LSE_IsReady() == 1U)
  186. {
  187. mco_frequency = LSE_VALUE;
  188. }
  189. break;
  190. #endif
  191. case LL_RCC_MCO1SOURCE_NOCLOCK: /* No clock used as MCO clock source */
  192. default:
  193. mco_frequency = LL_RCC_PERIPH_FREQUENCY_NA;
  194. return mco_frequency;
  195. }
  196. mco_frequency = mco_frequency / (1U << (LL_RCC_GetMCODiv(MCOx) >> RCC_CFGR_MCOPRE_Pos));
  197. return mco_frequency;
  198. }
  199. #if defined(RCC_BDCR_LSCOEN)
  200. /**
  201. * @brief Return LSC clock frequency
  202. * @retval LSC clock frequency (in Hz)
  203. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (LSI or LSE) is not ready
  204. */
  205. uint32_t LL_RCC_GetLSCClockFreq(void)
  206. {
  207. #if defined(RCC_LSE_SUPPORT)
  208. uint32_t lsc_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  209. switch (LL_RCC_LSCO_GetSource())
  210. {
  211. case LL_RCC_LSCO_CLKSOURCE_LSE: /* LSC Clock is LSE Osc. */
  212. if (LL_RCC_LSE_IsReady() == 1U)
  213. {
  214. lsc_frequency = LSE_VALUE;
  215. }
  216. break;
  217. case LL_RCC_LSCO_CLKSOURCE_LSI: /* LSC Clock is LSI Osc. */
  218. default:
  219. if (LL_RCC_LSI_IsReady() == 1U)
  220. {
  221. lsc_frequency = LL_RCC_LSI_GetFreq();
  222. }
  223. break;
  224. }
  225. return lsc_frequency;
  226. #else
  227. return LL_RCC_LSI_GetFreq();
  228. #endif
  229. }
  230. #endif
  231. #if defined(COMP1)
  232. /**
  233. * @brief Return COMP clock frequency
  234. * @param COMPx This parameter can be one of the following values:
  235. * @arg @ref LL_RCC_COMP1_CLKSOURCE
  236. * @arg @ref LL_RCC_COMP2_CLKSOURCE
  237. * @retval COMP clock frequency (in Hz)
  238. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (PCLK1, LSI or LSE) is not ready
  239. */
  240. uint32_t LL_RCC_GetCOMPClockFreq(uint32_t COMPx)
  241. {
  242. uint32_t comp_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  243. /* Check parameter */
  244. assert_param(IS_LL_RCC_COMP_CLKSOURCE(COMPx));
  245. if (COMPx == LL_RCC_COMP1_CLKSOURCE)
  246. {
  247. /* COMP1CLK clock frequency */
  248. switch (LL_RCC_GetCOMPClockSource(COMPx))
  249. {
  250. case LL_RCC_COMP1_CLKSOURCE_LSC: /* COMP1 Clock is LSC */
  251. comp_frequency = LL_RCC_GetLSCClockFreq();
  252. break;
  253. case LL_RCC_COMP1_CLKSOURCE_PCLK1: /* COMP1 Clock is PCLK1 */
  254. default:
  255. comp_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  256. break;
  257. }
  258. }
  259. #if defined(COMP2)
  260. else
  261. {
  262. /* COMP2CLK clock frequency */
  263. switch (LL_RCC_GetCOMPClockSource(COMPx))
  264. {
  265. case LL_RCC_COMP2_CLKSOURCE_LSC: /* COMP2 Clock is LSC */
  266. comp_frequency = LL_RCC_GetLSCClockFreq();
  267. break;
  268. case LL_RCC_COMP2_CLKSOURCE_PCLK1: /* COMP2 Clock is PCLK1 */
  269. default:
  270. comp_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  271. break;
  272. }
  273. }
  274. #endif
  275. return comp_frequency;
  276. }
  277. #endif
  278. /**
  279. * @brief Return LPTIMx clock frequency
  280. * @param LPTIMx This parameter can be one of the following values:
  281. * @arg @ref LL_RCC_LPTIM1_CLKSOURCE
  282. * @retval LPTIM clock frequency (in Hz)
  283. * - @ref LL_RCC_PERIPH_FREQUENCY_NO indicates that oscillator (PCLK1, LSI or LSE) is not ready
  284. * - @ref LL_RCC_PERIPH_FREQUENCY_NA indicates that no clock source selected
  285. */
  286. uint32_t LL_RCC_GetLPTIMClockFreq(uint32_t LPTIMx)
  287. {
  288. uint32_t lptim_frequency = LL_RCC_PERIPH_FREQUENCY_NO;
  289. /* Check parameter */
  290. assert_param(IS_LL_RCC_LPTIM_CLKSOURCE(LPTIMx));
  291. if (LPTIMx == LL_RCC_LPTIM1_CLKSOURCE)
  292. {
  293. /* LPTIM1CLK clock frequency */
  294. switch (LL_RCC_GetLPTIMClockSource(LPTIMx))
  295. {
  296. case LL_RCC_LPTIM1_CLKSOURCE_LSI: /* LPTIM1 Clock is LSI Osc. */
  297. if (LL_RCC_LSI_IsReady() == 1U)
  298. {
  299. lptim_frequency = LL_RCC_LSI_GetFreq();
  300. }
  301. break;
  302. case LL_RCC_LPTIM1_CLKSOURCE_NONE: /* No clock used as LPTIM1 clock source */
  303. lptim_frequency = LL_RCC_PERIPH_FREQUENCY_NA;
  304. break;
  305. #if defined(RCC_LSE_SUPPORT)
  306. case LL_RCC_LPTIM1_CLKSOURCE_LSE: /* LPTIM1 Clock is LSE Osc. */
  307. if (LL_RCC_LSE_IsReady() == 1U)
  308. {
  309. lptim_frequency = LSE_VALUE;
  310. }
  311. break;
  312. #endif
  313. case LL_RCC_LPTIM1_CLKSOURCE_PCLK1: /* LPTIM1 Clock is PCLK1 */
  314. default:
  315. lptim_frequency = RCC_GetPCLK1ClockFreq(RCC_GetHCLKClockFreq(RCC_GetSystemClockFreq()));
  316. break;
  317. }
  318. }
  319. return lptim_frequency;
  320. }
  321. /**
  322. * @}
  323. */
  324. /**
  325. * @}
  326. */
  327. /** @addtogroup RCC_LL_Private_Functions
  328. * @{
  329. */
  330. /**
  331. * @brief Return SYSTEM clock frequency
  332. * @retval SYSTEM clock frequency (in Hz)
  333. */
  334. uint32_t RCC_GetSystemClockFreq(void)
  335. {
  336. uint32_t frequency;
  337. /* Get SYSCLK source -------------------------------------------------------*/
  338. switch (LL_RCC_GetSysClkSource())
  339. {
  340. case LL_RCC_SYS_CLKSOURCE_STATUS_HSE: /* HSE used as system clock source */
  341. frequency = HSE_VALUE;
  342. break;
  343. case LL_RCC_SYS_CLKSOURCE_STATUS_LSI:
  344. frequency = LL_RCC_LSI_GetFreq();
  345. break;
  346. #if defined(RCC_LSE_SUPPORT)
  347. case LL_RCC_SYS_CLKSOURCE_STATUS_LSE:
  348. frequency = LSE_VALUE;
  349. break;
  350. #endif
  351. case LL_RCC_SYS_CLKSOURCE_STATUS_HSISYS: /* HSISYS used as system clock source */
  352. default:
  353. frequency = __LL_RCC_CALC_HSI_FREQ(LL_RCC_GetHSIDiv());
  354. break;
  355. }
  356. return frequency;
  357. }
  358. /**
  359. * @brief Return HCLK clock frequency
  360. * @param SYSCLK_Frequency SYSCLK clock frequency
  361. * @retval HCLK clock frequency (in Hz)
  362. */
  363. uint32_t RCC_GetHCLKClockFreq(uint32_t SYSCLK_Frequency)
  364. {
  365. /* HCLK clock frequency */
  366. return __LL_RCC_CALC_HCLK_FREQ(SYSCLK_Frequency, LL_RCC_GetAHBPrescaler());
  367. }
  368. /**
  369. * @brief Return PCLK1 clock frequency
  370. * @param HCLK_Frequency HCLK clock frequency
  371. * @retval PCLK1 clock frequency (in Hz)
  372. */
  373. uint32_t RCC_GetPCLK1ClockFreq(uint32_t HCLK_Frequency)
  374. {
  375. /* PCLK1 clock frequency */
  376. return __LL_RCC_CALC_PCLK1_FREQ(HCLK_Frequency, LL_RCC_GetAPB1Prescaler());
  377. }
  378. /**
  379. * @}
  380. */
  381. /**
  382. * @}
  383. */
  384. #endif /* defined(RCC) */
  385. /**
  386. * @}
  387. */
  388. #endif /* USE_FULL_LL_DRIVER */
  389. /************************ (C) COPYRIGHT Puya*****END OF FILE****/