py32f002b_hal_rcc_ex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_rcc_ex.c
  4. * @author MCU Application Team
  5. * @brief Extended RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities RCC extended peripheral:
  8. * + Extended Peripheral Control functions
  9. * + Extended Clock management functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  15. * All rights reserved.</center></h2>
  16. *
  17. * This software component is licensed by Puya under BSD 3-Clause license,
  18. * the "License"; You may not use this file except in compliance with the
  19. * License. You may obtain a copy of the License at:
  20. * opensource.org/licenses/BSD-3-Clause
  21. *
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  26. * All rights reserved.</center></h2>
  27. *
  28. * This software component is licensed by ST under BSD 3-Clause license,
  29. * the "License"; You may not use this file except in compliance with the
  30. * License. You may obtain a copy of the License at:
  31. * opensource.org/licenses/BSD-3-Clause
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "py32f0xx_hal.h"
  37. /** @addtogroup PY32F002B_HAL_Driver
  38. * @{
  39. */
  40. /** @defgroup RCCEx RCCEx
  41. * @brief RCC Extended HAL module driver
  42. * @{
  43. */
  44. #ifdef HAL_RCC_MODULE_ENABLED
  45. /* Private typedef -----------------------------------------------------------*/
  46. /* Private defines -----------------------------------------------------------*/
  47. /* Private macros ------------------------------------------------------------*/
  48. /* Private variables ---------------------------------------------------------*/
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* Exported functions --------------------------------------------------------*/
  51. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  52. * @{
  53. */
  54. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  55. * @brief Extended Peripheral Control functions
  56. *
  57. @verbatim
  58. ===============================================================================
  59. ##### Extended Peripheral Control functions #####
  60. ===============================================================================
  61. [..]
  62. This subsection provides a set of functions allowing to control the RCC Clocks
  63. frequencies.
  64. [..]
  65. (@) Important note: Care must be taken when @ref HAL_RCCEx_PeriphCLKConfig() is used to
  66. select the RTC clock source; in this case the Backup domain will be reset in
  67. order to modify the RTC Clock source, as consequence RTC registers (including
  68. the backup registers) and RCC_BDCR register are set to their reset values.
  69. @endverbatim
  70. * @{
  71. */
  72. /**
  73. * @brief Initialize the RCC extended peripherals clocks according to the specified
  74. * parameters in the @ref RCC_PeriphCLKInitTypeDef.
  75. * @param PeriphClkInit pointer to a @ref RCC_PeriphCLKInitTypeDef structure that
  76. * contains a field PeriphClockSelection which can be a combination of the following values:
  77. * @arg @ref RCC_PERIPHCLK_COMP1 COMP1 peripheral clock (1)
  78. * @arg @ref RCC_PERIPHCLK_COMP2 COMP2 peripheral clock (1)
  79. * @arg @ref RCC_PERIPHCLK_LPTIM LPTIM peripheral clock (1)
  80. *
  81. * @note (1) Peripherals maybe not available on some devices
  82. * @note Care must be taken when @ref HAL_RCCEx_PeriphCLKConfig() is used to select
  83. * the RTC clock source: in this case the access to Backup domain is enabled.
  84. *
  85. * @retval HAL status
  86. */
  87. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  88. {
  89. /* Check the parameters */
  90. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  91. #if defined(RCC_CCIPR_COMP1SEL)
  92. /*-------------------------- COMP1 clock source configuration -------------------*/
  93. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_COMP1) == RCC_PERIPHCLK_COMP1)
  94. {
  95. /* Check the parameters */
  96. assert_param(IS_RCC_COMP1CLKSOURCE(PeriphClkInit->Comp1ClockSelection));
  97. /* Configure the COMP1 clock source */
  98. __HAL_RCC_COMP1_CONFIG(PeriphClkInit->Comp1ClockSelection);
  99. }
  100. #endif /* RCC_CCIPR_COMP1SEL */
  101. #if defined(RCC_CCIPR_COMP2SEL)
  102. /*-------------------------- COMP2 clock source configuration -------------------*/
  103. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_COMP2) == RCC_PERIPHCLK_COMP2)
  104. {
  105. /* Check the parameters */
  106. assert_param(IS_RCC_COMP2CLKSOURCE(PeriphClkInit->Comp2ClockSelection));
  107. /* Configure the COMP2 clock source */
  108. __HAL_RCC_COMP2_CONFIG(PeriphClkInit->Comp2ClockSelection);
  109. }
  110. #endif /* RCC_CCIPR_COMP2SEL */
  111. #if defined(RCC_CCIPR_LPTIMSEL)
  112. /*-------------------------- LPTIM clock source configuration -------------------*/
  113. if (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LPTIM) == (RCC_PERIPHCLK_LPTIM))
  114. {
  115. assert_param(IS_RCC_LPTIM1CLKSOURCE(PeriphClkInit->LptimClockSelection));
  116. __HAL_RCC_LPTIM_CONFIG(PeriphClkInit->LptimClockSelection);
  117. }
  118. #endif /* RCC_CCIPR_LPTIM1SEL */
  119. return HAL_OK;
  120. }
  121. /**
  122. * @brief Get the RCC_ClkInitStruct according to the internal RCC configuration registers.
  123. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  124. * returns the configuration information for the Extended Peripherals
  125. * clocks: COMP1, COMP2, LPTIM
  126. * @note Depending on devices and packages, some Peripherals may not be available.
  127. * Refer to device datasheet for Peripheral availability.
  128. * @retval None
  129. */
  130. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  131. {
  132. /* Set all possible values for the extended clock type parameter------------*/
  133. PeriphClkInit->PeriphClockSelection = 0;
  134. #if defined(RCC_CCIPR_COMP1SEL)
  135. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_COMP1;
  136. #endif /* RCC_CCIPR_COMP1SEL */
  137. #if defined(RCC_CCIPR_COMP2SEL)
  138. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_COMP2;
  139. #endif /* RCC_CCIPR_COMP2SEL */
  140. #if defined(RCC_CCIPR_LPTIMSEL)
  141. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LPTIM;
  142. #endif /* RCC_CCIPR_LPTIM2SEL */
  143. #if defined(RCC_CCIPR_COMP1SEL)
  144. /* Get the COMP1 clock source --------------------------------------------*/
  145. PeriphClkInit->Comp1ClockSelection = __HAL_RCC_GET_COMP1_SOURCE();
  146. #endif /* RCC_CCIPR_COMP1SEL */
  147. #if defined(RCC_CCIPR_COMP2SEL)
  148. /* Get the COMP2 clock source ---------------------------------------------*/
  149. PeriphClkInit->Comp2ClockSelection = __HAL_RCC_GET_COMP2_SOURCE();
  150. #endif /* RCC_CCIPR_COMP2SEL */
  151. #if defined(RCC_CCIPR_LPTIMSEL)
  152. /* Get the LPTIM clock source ---------------------------------------------*/
  153. PeriphClkInit->LptimClockSelection = __HAL_RCC_GET_LPTIM_SOURCE();
  154. #endif /* RCC_CCIPR_LPTIM2SEL */
  155. }
  156. /**
  157. * @brief Return the peripheral clock frequency.
  158. * @note Return 0 if peripheral clock identifier not managed by this API
  159. * @param PeriphClk Peripheral clock identifier
  160. * This parameter can be one of the following values:
  161. * @arg @ref RCC_PERIPHCLK_COMP1 COMP1 peripheral clock
  162. * @arg @ref RCC_PERIPHCLK_COMP2 COMP2 peripheral clock
  163. * @arg @ref RCC_PERIPHCLK_LPTIM LPTIM peripheral clock
  164. * @note Depending on devices and packages, some Peripherals may not be available.
  165. * Refer to device datasheet for Peripheral availability.
  166. * @retval Frequency in Hz
  167. */
  168. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  169. {
  170. uint32_t frequency = 0U;
  171. uint32_t srcclk;
  172. /* Check the parameters */
  173. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  174. switch (PeriphClk)
  175. {
  176. #if defined(RCC_CCIPR_COMP1SEL)
  177. case RCC_PERIPHCLK_COMP1:
  178. /* Get the current COMP1 source */
  179. srcclk = __HAL_RCC_GET_COMP1_SOURCE();
  180. if (srcclk == RCC_COMP1CLKSOURCE_PCLK) /* PCLK1 */
  181. {
  182. frequency = HAL_RCC_GetPCLK1Freq();
  183. }
  184. else if ((HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)) && (HAL_IS_BIT_CLR(RCC->BDCR, RCC_BDCR_LSCOSEL)) \
  185. && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSCOEN)) && (srcclk == RCC_COMP1CLKSOURCE_LSC))
  186. {
  187. if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_32768Hz)
  188. {
  189. frequency = 32768U;
  190. }
  191. else if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_38400Hz)
  192. {
  193. frequency = 38400U;
  194. }
  195. else
  196. {
  197. frequency = 0U;
  198. }
  199. }
  200. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSCOSEL)) \
  201. && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSCOEN)) && (srcclk == RCC_COMP1CLKSOURCE_LSC))
  202. {
  203. frequency = LSE_VALUE;
  204. }
  205. /* Clock not enabled for COMP1 */
  206. else
  207. {
  208. /* Nothing to do as frequency already initialized to 0U */
  209. }
  210. break;
  211. #endif
  212. #if defined(RCC_CCIPR_COMP2SEL)
  213. case RCC_PERIPHCLK_COMP2:
  214. /* Get the current COMP2 source */
  215. srcclk = __HAL_RCC_GET_COMP2_SOURCE();
  216. if (srcclk == RCC_COMP2CLKSOURCE_PCLK) /* PCLK1 */
  217. {
  218. frequency = HAL_RCC_GetPCLK1Freq();
  219. }
  220. else if ((HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)) && (HAL_IS_BIT_CLR(RCC->BDCR, RCC_BDCR_LSCOSEL)) \
  221. && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSCOEN)) && (srcclk == RCC_COMP2CLKSOURCE_LSC))
  222. {
  223. if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_32768Hz)
  224. {
  225. frequency = 32768U;
  226. }
  227. else if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_38400Hz)
  228. {
  229. frequency = 38400U;
  230. }
  231. else
  232. {
  233. frequency = 0U;
  234. }
  235. }
  236. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSCOSEL)) \
  237. && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSCOEN)) && (srcclk == RCC_COMP2CLKSOURCE_LSC))
  238. {
  239. frequency = LSE_VALUE;
  240. }
  241. /* Clock not enabled for COMP2 */
  242. else
  243. {
  244. /* Nothing to do as frequency already initialized to 0U */
  245. }
  246. break;
  247. #endif
  248. #if defined(RCC_CCIPR_LPTIMSEL)
  249. case RCC_PERIPHCLK_LPTIM:
  250. /* Get the current LPTIM1 source */
  251. srcclk = __HAL_RCC_GET_LPTIM_SOURCE();
  252. if (srcclk == RCC_LPTIMCLKSOURCE_PCLK)
  253. {
  254. frequency = HAL_RCC_GetPCLK1Freq();
  255. }
  256. else if ((HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)) && (srcclk == RCC_LPTIMCLKSOURCE_LSI))
  257. {
  258. if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_32768Hz)
  259. {
  260. frequency = 32768U;
  261. }
  262. else if ((READ_BIT(RCC->ICSCR, RCC_ICSCR_LSI_TRIM) >> RCC_ICSCR_LSI_TRIM_Pos) == RCC_LSICALIBRATION_38400Hz)
  263. {
  264. frequency = 38400U;
  265. }
  266. else
  267. {
  268. frequency = 0U;
  269. }
  270. }
  271. else if ((HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)) && (srcclk == RCC_LPTIMCLKSOURCE_LSE))
  272. {
  273. frequency = LSE_VALUE;
  274. }
  275. /* Clock not enabled for LPTIM1 */
  276. else
  277. {
  278. /* Nothing to do as frequency already initialized to 0U */
  279. }
  280. break;
  281. #endif /* RCC_CCIPR_LPTIM1SEL */
  282. default:
  283. break;
  284. }
  285. return (frequency);
  286. }
  287. /**
  288. * @}
  289. */
  290. /** @defgroup RCCEx_Exported_Functions_Group2 Extended Clock management functions
  291. * @brief Extended Clock management functions
  292. *
  293. @verbatim
  294. ===============================================================================
  295. ##### Extended clock management functions #####
  296. ===============================================================================
  297. [..]
  298. This subsection provides a set of functions allowing to control the
  299. activation or deactivation of LSE CSS, Low speed clock output and
  300. clock after wake-up from STOP mode.
  301. @endverbatim
  302. * @{
  303. */
  304. /**
  305. * @brief Select the Low Speed clock source to output on LSCO pin (PA2).
  306. * @param LSCOSource specifies the Low Speed clock source to output.
  307. * This parameter can be one of the following values:
  308. * @arg @ref RCC_LSCOSOURCE_LSI LSI clock selected as LSCO source
  309. * @arg @ref RCC_LSCOSOURCE_LSE LSE clock selected as LSCO source
  310. * @retval None
  311. */
  312. void HAL_RCCEx_EnableLSCO(uint32_t LSCOSource)
  313. {
  314. /* Check the parameters */
  315. assert_param(IS_RCC_LSCOSOURCE(LSCOSource));
  316. MODIFY_REG(RCC->BDCR, RCC_BDCR_LSCOSEL | RCC_BDCR_LSCOEN, LSCOSource | RCC_BDCR_LSCOEN);
  317. }
  318. /**
  319. * @brief Disable the Low Speed clock output.
  320. * @retval None
  321. */
  322. void HAL_RCCEx_DisableLSCO(void)
  323. {
  324. CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSCOEN);
  325. }
  326. /**
  327. * @}
  328. */
  329. /**
  330. * @}
  331. */
  332. #endif /* HAL_RCC_MODULE_ENABLED */
  333. /**
  334. * @}
  335. */
  336. /**
  337. * @}
  338. */
  339. /************************ (C) COPYRIGHT Puya *****END OF FILE****/