stm32f0xx_hal_rcc_ex.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RCC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities RCC extension peripheral:
  8. * + Extended Peripheral Control functions
  9. * + Extended Clock Recovery System Control functions
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2016 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file in
  18. * the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f0xx_hal.h"
  24. /** @addtogroup STM32F0xx_HAL_Driver
  25. * @{
  26. */
  27. #ifdef HAL_RCC_MODULE_ENABLED
  28. /** @defgroup RCCEx RCCEx
  29. * @brief RCC Extension HAL module driver.
  30. * @{
  31. */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. #if defined(CRS)
  35. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  36. * @{
  37. */
  38. /* Bit position in register */
  39. #define CRS_CFGR_FELIM_BITNUMBER 16
  40. #define CRS_CR_TRIM_BITNUMBER 8
  41. #define CRS_ISR_FECAP_BITNUMBER 16
  42. /**
  43. * @}
  44. */
  45. #endif /* CRS */
  46. /* Private macro -------------------------------------------------------------*/
  47. /** @defgroup RCCEx_Private_Macros RCCEx Private Macros
  48. * @{
  49. */
  50. /**
  51. * @}
  52. */
  53. /* Private variables ---------------------------------------------------------*/
  54. /* Private function prototypes -----------------------------------------------*/
  55. /* Private functions ---------------------------------------------------------*/
  56. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  57. * @{
  58. */
  59. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  60. * @brief Extended Peripheral Control functions
  61. *
  62. @verbatim
  63. ===============================================================================
  64. ##### Extended Peripheral Control functions #####
  65. ===============================================================================
  66. [..]
  67. This subsection provides a set of functions allowing to control the RCC Clocks
  68. frequencies.
  69. [..]
  70. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  71. select the RTC clock source; in this case the Backup domain will be reset in
  72. order to modify the RTC Clock source, as consequence RTC registers (including
  73. the backup registers) are set to their reset values.
  74. @endverbatim
  75. * @{
  76. */
  77. /**
  78. * @brief Initializes the RCC extended peripherals clocks according to the specified
  79. * parameters in the RCC_PeriphCLKInitTypeDef.
  80. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  81. * contains the configuration information for the Extended Peripherals clocks
  82. * (USART, RTC, I2C, CEC and USB).
  83. *
  84. * @note Care must be taken when @ref HAL_RCCEx_PeriphCLKConfig() is used to select
  85. * the RTC clock source; in this case the Backup domain will be reset in
  86. * order to modify the RTC Clock source, as consequence RTC registers (including
  87. * the backup registers) and RCC_BDCR register are set to their reset values.
  88. *
  89. * @retval HAL status
  90. */
  91. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  92. {
  93. uint32_t tickstart = 0U;
  94. uint32_t temp_reg = 0U;
  95. /* Check the parameters */
  96. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  97. /*---------------------------- RTC configuration -------------------------------*/
  98. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  99. {
  100. /* check for RTC Parameters used to output RTCCLK */
  101. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  102. FlagStatus pwrclkchanged = RESET;
  103. /* As soon as function is called to change RTC clock source, activation of the
  104. power domain is done. */
  105. /* Requires to enable write access to Backup Domain of necessary */
  106. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  107. {
  108. __HAL_RCC_PWR_CLK_ENABLE();
  109. pwrclkchanged = SET;
  110. }
  111. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  112. {
  113. /* Enable write access to Backup domain */
  114. SET_BIT(PWR->CR, PWR_CR_DBP);
  115. /* Wait for Backup domain Write protection disable */
  116. tickstart = HAL_GetTick();
  117. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  118. {
  119. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  120. {
  121. return HAL_TIMEOUT;
  122. }
  123. }
  124. }
  125. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  126. temp_reg = (RCC->BDCR & RCC_BDCR_RTCSEL);
  127. if((temp_reg != 0x00000000U) && (temp_reg != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  128. {
  129. /* Store the content of BDCR register before the reset of Backup Domain */
  130. temp_reg = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  131. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  132. __HAL_RCC_BACKUPRESET_FORCE();
  133. __HAL_RCC_BACKUPRESET_RELEASE();
  134. /* Restore the Content of BDCR register */
  135. RCC->BDCR = temp_reg;
  136. /* Wait for LSERDY if LSE was enabled */
  137. if (HAL_IS_BIT_SET(temp_reg, RCC_BDCR_LSEON))
  138. {
  139. /* Get Start Tick */
  140. tickstart = HAL_GetTick();
  141. /* Wait till LSE is ready */
  142. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  143. {
  144. if((HAL_GetTick() - tickstart) > RCC_LSE_TIMEOUT_VALUE)
  145. {
  146. return HAL_TIMEOUT;
  147. }
  148. }
  149. }
  150. }
  151. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  152. /* Require to disable power clock if necessary */
  153. if(pwrclkchanged == SET)
  154. {
  155. __HAL_RCC_PWR_CLK_DISABLE();
  156. }
  157. }
  158. /*------------------------------- USART1 Configuration ------------------------*/
  159. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART1) == RCC_PERIPHCLK_USART1)
  160. {
  161. /* Check the parameters */
  162. assert_param(IS_RCC_USART1CLKSOURCE(PeriphClkInit->Usart1ClockSelection));
  163. /* Configure the USART1 clock source */
  164. __HAL_RCC_USART1_CONFIG(PeriphClkInit->Usart1ClockSelection);
  165. }
  166. #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)\
  167. || defined(STM32F091xC) || defined(STM32F098xx)
  168. /*----------------------------- USART2 Configuration --------------------------*/
  169. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART2) == RCC_PERIPHCLK_USART2)
  170. {
  171. /* Check the parameters */
  172. assert_param(IS_RCC_USART2CLKSOURCE(PeriphClkInit->Usart2ClockSelection));
  173. /* Configure the USART2 clock source */
  174. __HAL_RCC_USART2_CONFIG(PeriphClkInit->Usart2ClockSelection);
  175. }
  176. #endif /* STM32F071xB || STM32F072xB || STM32F078xx || */
  177. /* STM32F091xC || STM32F098xx */
  178. #if defined(STM32F091xC) || defined(STM32F098xx)
  179. /*----------------------------- USART3 Configuration --------------------------*/
  180. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USART3) == RCC_PERIPHCLK_USART3)
  181. {
  182. /* Check the parameters */
  183. assert_param(IS_RCC_USART3CLKSOURCE(PeriphClkInit->Usart3ClockSelection));
  184. /* Configure the USART3 clock source */
  185. __HAL_RCC_USART3_CONFIG(PeriphClkInit->Usart3ClockSelection);
  186. }
  187. #endif /* STM32F091xC || STM32F098xx */
  188. /*------------------------------ I2C1 Configuration ------------------------*/
  189. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_I2C1) == RCC_PERIPHCLK_I2C1)
  190. {
  191. /* Check the parameters */
  192. assert_param(IS_RCC_I2C1CLKSOURCE(PeriphClkInit->I2c1ClockSelection));
  193. /* Configure the I2C1 clock source */
  194. __HAL_RCC_I2C1_CONFIG(PeriphClkInit->I2c1ClockSelection);
  195. }
  196. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || defined(STM32F070x6)
  197. /*------------------------------ USB Configuration ------------------------*/
  198. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_USB) == RCC_PERIPHCLK_USB)
  199. {
  200. /* Check the parameters */
  201. assert_param(IS_RCC_USBCLKSOURCE(PeriphClkInit->UsbClockSelection));
  202. /* Configure the USB clock source */
  203. __HAL_RCC_USB_CONFIG(PeriphClkInit->UsbClockSelection);
  204. }
  205. #endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F070xB || STM32F070x6 */
  206. #if defined(STM32F042x6) || defined(STM32F048xx)\
  207. || defined(STM32F051x8) || defined(STM32F058xx)\
  208. || defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)\
  209. || defined(STM32F091xC) || defined(STM32F098xx)
  210. /*------------------------------ CEC clock Configuration -------------------*/
  211. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_CEC) == RCC_PERIPHCLK_CEC)
  212. {
  213. /* Check the parameters */
  214. assert_param(IS_RCC_CECCLKSOURCE(PeriphClkInit->CecClockSelection));
  215. /* Configure the CEC clock source */
  216. __HAL_RCC_CEC_CONFIG(PeriphClkInit->CecClockSelection);
  217. }
  218. #endif /* STM32F042x6 || STM32F048xx || */
  219. /* STM32F051x8 || STM32F058xx || */
  220. /* STM32F071xB || STM32F072xB || STM32F078xx || */
  221. /* STM32F091xC || STM32F098xx */
  222. return HAL_OK;
  223. }
  224. /**
  225. * @brief Get the RCC_ClkInitStruct according to the internal
  226. * RCC configuration registers.
  227. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  228. * returns the configuration information for the Extended Peripherals clocks
  229. * (USART, RTC, I2C, CEC and USB).
  230. * @retval None
  231. */
  232. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  233. {
  234. /* Set all possible values for the extended clock type parameter------------*/
  235. /* Common part first */
  236. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_I2C1 | RCC_PERIPHCLK_RTC;
  237. /* Get the RTC configuration --------------------------------------------*/
  238. PeriphClkInit->RTCClockSelection = __HAL_RCC_GET_RTC_SOURCE();
  239. /* Get the USART1 clock configuration --------------------------------------------*/
  240. PeriphClkInit->Usart1ClockSelection = __HAL_RCC_GET_USART1_SOURCE();
  241. /* Get the I2C1 clock source -----------------------------------------------*/
  242. PeriphClkInit->I2c1ClockSelection = __HAL_RCC_GET_I2C1_SOURCE();
  243. #if defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)\
  244. || defined(STM32F091xC) || defined(STM32F098xx)
  245. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USART2;
  246. /* Get the USART2 clock source ---------------------------------------------*/
  247. PeriphClkInit->Usart2ClockSelection = __HAL_RCC_GET_USART2_SOURCE();
  248. #endif /* STM32F071xB || STM32F072xB || STM32F078xx || */
  249. /* STM32F091xC || STM32F098xx */
  250. #if defined(STM32F091xC) || defined(STM32F098xx)
  251. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USART3;
  252. /* Get the USART3 clock source ---------------------------------------------*/
  253. PeriphClkInit->Usart3ClockSelection = __HAL_RCC_GET_USART3_SOURCE();
  254. #endif /* STM32F091xC || STM32F098xx */
  255. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB) || defined(STM32F070x6)
  256. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_USB;
  257. /* Get the USB clock source ---------------------------------------------*/
  258. PeriphClkInit->UsbClockSelection = __HAL_RCC_GET_USB_SOURCE();
  259. #endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F070xB || STM32F070x6 */
  260. #if defined(STM32F042x6) || defined(STM32F048xx)\
  261. || defined(STM32F051x8) || defined(STM32F058xx)\
  262. || defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx)\
  263. || defined(STM32F091xC) || defined(STM32F098xx)
  264. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_CEC;
  265. /* Get the CEC clock source ------------------------------------------------*/
  266. PeriphClkInit->CecClockSelection = __HAL_RCC_GET_CEC_SOURCE();
  267. #endif /* STM32F042x6 || STM32F048xx || */
  268. /* STM32F051x8 || STM32F058xx || */
  269. /* STM32F071xB || STM32F072xB || STM32F078xx || */
  270. /* STM32F091xC || STM32F098xx */
  271. }
  272. /**
  273. * @brief Returns the peripheral clock frequency
  274. * @note Returns 0 if peripheral clock is unknown
  275. * @param PeriphClk Peripheral clock identifier
  276. * This parameter can be one of the following values:
  277. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  278. * @arg @ref RCC_PERIPHCLK_USART1 USART1 peripheral clock
  279. * @arg @ref RCC_PERIPHCLK_I2C1 I2C1 peripheral clock
  280. @if STM32F042x6
  281. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  282. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  283. @endif
  284. @if STM32F048xx
  285. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  286. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  287. @endif
  288. @if STM32F051x8
  289. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  290. @endif
  291. @if STM32F058xx
  292. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  293. @endif
  294. @if STM32F070x6
  295. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  296. @endif
  297. @if STM32F070xB
  298. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  299. @endif
  300. @if STM32F071xB
  301. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock
  302. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  303. @endif
  304. @if STM32F072xB
  305. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock
  306. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  307. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  308. @endif
  309. @if STM32F078xx
  310. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock
  311. * @arg @ref RCC_PERIPHCLK_USB USB peripheral clock
  312. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  313. @endif
  314. @if STM32F091xC
  315. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock
  316. * @arg @ref RCC_PERIPHCLK_USART3 USART2 peripheral clock
  317. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  318. @endif
  319. @if STM32F098xx
  320. * @arg @ref RCC_PERIPHCLK_USART2 USART2 peripheral clock
  321. * @arg @ref RCC_PERIPHCLK_USART3 USART2 peripheral clock
  322. * @arg @ref RCC_PERIPHCLK_CEC CEC peripheral clock
  323. @endif
  324. * @retval Frequency in Hz (0: means that no available frequency for the peripheral)
  325. */
  326. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  327. {
  328. /* frequency == 0 : means that no available frequency for the peripheral */
  329. uint32_t frequency = 0U;
  330. uint32_t srcclk = 0U;
  331. #if defined(USB)
  332. uint32_t pllmull = 0U, pllsource = 0U, predivfactor = 0U;
  333. #endif /* USB */
  334. /* Check the parameters */
  335. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  336. switch (PeriphClk)
  337. {
  338. case RCC_PERIPHCLK_RTC:
  339. {
  340. /* Get the current RTC source */
  341. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  342. /* Check if LSE is ready and if RTC clock selection is LSE */
  343. if ((srcclk == RCC_RTCCLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  344. {
  345. frequency = LSE_VALUE;
  346. }
  347. /* Check if LSI is ready and if RTC clock selection is LSI */
  348. else if ((srcclk == RCC_RTCCLKSOURCE_LSI) && (HAL_IS_BIT_SET(RCC->CSR, RCC_CSR_LSIRDY)))
  349. {
  350. frequency = LSI_VALUE;
  351. }
  352. /* Check if HSE is ready and if RTC clock selection is HSI_DIV32*/
  353. else if ((srcclk == RCC_RTCCLKSOURCE_HSE_DIV32) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)))
  354. {
  355. frequency = HSE_VALUE / 32U;
  356. }
  357. break;
  358. }
  359. case RCC_PERIPHCLK_USART1:
  360. {
  361. /* Get the current USART1 source */
  362. srcclk = __HAL_RCC_GET_USART1_SOURCE();
  363. /* Check if USART1 clock selection is PCLK1 */
  364. if (srcclk == RCC_USART1CLKSOURCE_PCLK1)
  365. {
  366. frequency = HAL_RCC_GetPCLK1Freq();
  367. }
  368. /* Check if HSI is ready and if USART1 clock selection is HSI */
  369. else if ((srcclk == RCC_USART1CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  370. {
  371. frequency = HSI_VALUE;
  372. }
  373. /* Check if USART1 clock selection is SYSCLK */
  374. else if (srcclk == RCC_USART1CLKSOURCE_SYSCLK)
  375. {
  376. frequency = HAL_RCC_GetSysClockFreq();
  377. }
  378. /* Check if LSE is ready and if USART1 clock selection is LSE */
  379. else if ((srcclk == RCC_USART1CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  380. {
  381. frequency = LSE_VALUE;
  382. }
  383. break;
  384. }
  385. #if defined(RCC_CFGR3_USART2SW)
  386. case RCC_PERIPHCLK_USART2:
  387. {
  388. /* Get the current USART2 source */
  389. srcclk = __HAL_RCC_GET_USART2_SOURCE();
  390. /* Check if USART2 clock selection is PCLK1 */
  391. if (srcclk == RCC_USART2CLKSOURCE_PCLK1)
  392. {
  393. frequency = HAL_RCC_GetPCLK1Freq();
  394. }
  395. /* Check if HSI is ready and if USART2 clock selection is HSI */
  396. else if ((srcclk == RCC_USART2CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  397. {
  398. frequency = HSI_VALUE;
  399. }
  400. /* Check if USART2 clock selection is SYSCLK */
  401. else if (srcclk == RCC_USART2CLKSOURCE_SYSCLK)
  402. {
  403. frequency = HAL_RCC_GetSysClockFreq();
  404. }
  405. /* Check if LSE is ready and if USART2 clock selection is LSE */
  406. else if ((srcclk == RCC_USART2CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  407. {
  408. frequency = LSE_VALUE;
  409. }
  410. break;
  411. }
  412. #endif /* RCC_CFGR3_USART2SW */
  413. #if defined(RCC_CFGR3_USART3SW)
  414. case RCC_PERIPHCLK_USART3:
  415. {
  416. /* Get the current USART3 source */
  417. srcclk = __HAL_RCC_GET_USART3_SOURCE();
  418. /* Check if USART3 clock selection is PCLK1 */
  419. if (srcclk == RCC_USART3CLKSOURCE_PCLK1)
  420. {
  421. frequency = HAL_RCC_GetPCLK1Freq();
  422. }
  423. /* Check if HSI is ready and if USART3 clock selection is HSI */
  424. else if ((srcclk == RCC_USART3CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  425. {
  426. frequency = HSI_VALUE;
  427. }
  428. /* Check if USART3 clock selection is SYSCLK */
  429. else if (srcclk == RCC_USART3CLKSOURCE_SYSCLK)
  430. {
  431. frequency = HAL_RCC_GetSysClockFreq();
  432. }
  433. /* Check if LSE is ready and if USART3 clock selection is LSE */
  434. else if ((srcclk == RCC_USART3CLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  435. {
  436. frequency = LSE_VALUE;
  437. }
  438. break;
  439. }
  440. #endif /* RCC_CFGR3_USART3SW */
  441. case RCC_PERIPHCLK_I2C1:
  442. {
  443. /* Get the current I2C1 source */
  444. srcclk = __HAL_RCC_GET_I2C1_SOURCE();
  445. /* Check if HSI is ready and if I2C1 clock selection is HSI */
  446. if ((srcclk == RCC_I2C1CLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  447. {
  448. frequency = HSI_VALUE;
  449. }
  450. /* Check if I2C1 clock selection is SYSCLK */
  451. else if (srcclk == RCC_I2C1CLKSOURCE_SYSCLK)
  452. {
  453. frequency = HAL_RCC_GetSysClockFreq();
  454. }
  455. break;
  456. }
  457. #if defined(USB)
  458. case RCC_PERIPHCLK_USB:
  459. {
  460. /* Get the current USB source */
  461. srcclk = __HAL_RCC_GET_USB_SOURCE();
  462. /* Check if PLL is ready and if USB clock selection is PLL */
  463. if ((srcclk == RCC_USBCLKSOURCE_PLL) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_PLLRDY)))
  464. {
  465. /* Get PLL clock source and multiplication factor ----------------------*/
  466. pllmull = RCC->CFGR & RCC_CFGR_PLLMUL;
  467. pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
  468. pllmull = (pllmull >> RCC_CFGR_PLLMUL_BITNUMBER) + 2U;
  469. predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1U;
  470. if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV)
  471. {
  472. /* HSE used as PLL clock source : frequency = HSE/PREDIV * PLLMUL */
  473. frequency = (HSE_VALUE/predivfactor) * pllmull;
  474. }
  475. #if defined(RCC_CR2_HSI48ON)
  476. else if (pllsource == RCC_CFGR_PLLSRC_HSI48_PREDIV)
  477. {
  478. /* HSI48 used as PLL clock source : frequency = HSI48/PREDIV * PLLMUL */
  479. frequency = (HSI48_VALUE / predivfactor) * pllmull;
  480. }
  481. #endif /* RCC_CR2_HSI48ON */
  482. else
  483. {
  484. #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F078xx) || defined(STM32F072xB) || defined(STM32F070xB)
  485. /* HSI used as PLL clock source : frequency = HSI/PREDIV * PLLMUL */
  486. frequency = (HSI_VALUE / predivfactor) * pllmull;
  487. #else
  488. /* HSI used as PLL clock source : frequency = HSI/2U * PLLMUL */
  489. frequency = (HSI_VALUE >> 1U) * pllmull;
  490. #endif /* STM32F042x6 || STM32F048xx || STM32F072xB || STM32F078xx || STM32F070xB */
  491. }
  492. }
  493. #if defined(RCC_CR2_HSI48ON)
  494. /* Check if HSI48 is ready and if USB clock selection is HSI48 */
  495. else if ((srcclk == RCC_USBCLKSOURCE_HSI48) && (HAL_IS_BIT_SET(RCC->CR2, RCC_CR2_HSI48RDY)))
  496. {
  497. frequency = HSI48_VALUE;
  498. }
  499. #endif /* RCC_CR2_HSI48ON */
  500. break;
  501. }
  502. #endif /* USB */
  503. #if defined(CEC)
  504. case RCC_PERIPHCLK_CEC:
  505. {
  506. /* Get the current CEC source */
  507. srcclk = __HAL_RCC_GET_CEC_SOURCE();
  508. /* Check if HSI is ready and if CEC clock selection is HSI */
  509. if ((srcclk == RCC_CECCLKSOURCE_HSI) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSIRDY)))
  510. {
  511. frequency = HSI_VALUE;
  512. }
  513. /* Check if LSE is ready and if CEC clock selection is LSE */
  514. else if ((srcclk == RCC_CECCLKSOURCE_LSE) && (HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSERDY)))
  515. {
  516. frequency = LSE_VALUE;
  517. }
  518. break;
  519. }
  520. #endif /* CEC */
  521. default:
  522. {
  523. break;
  524. }
  525. }
  526. return(frequency);
  527. }
  528. /**
  529. * @}
  530. */
  531. #if defined(CRS)
  532. /** @defgroup RCCEx_Exported_Functions_Group3 Extended Clock Recovery System Control functions
  533. * @brief Extended Clock Recovery System Control functions
  534. *
  535. @verbatim
  536. ===============================================================================
  537. ##### Extended Clock Recovery System Control functions #####
  538. ===============================================================================
  539. [..]
  540. For devices with Clock Recovery System feature (CRS), RCC Extension HAL driver can be used as follows:
  541. (#) In System clock config, HSI48 needs to be enabled
  542. (#) Enable CRS clock in IP MSP init which will use CRS functions
  543. (#) Call CRS functions as follows:
  544. (##) Prepare synchronization configuration necessary for HSI48 calibration
  545. (+++) Default values can be set for frequency Error Measurement (reload and error limit)
  546. and also HSI48 oscillator smooth trimming.
  547. (+++) Macro __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
  548. directly reload value with target and synchronization frequencies values
  549. (##) Call function HAL_RCCEx_CRSConfig which
  550. (+++) Reset CRS registers to their default values.
  551. (+++) Configure CRS registers with synchronization configuration
  552. (+++) Enable automatic calibration and frequency error counter feature
  553. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
  554. periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
  555. provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
  556. precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
  557. should be used as SYNC signal.
  558. (##) A polling function is provided to wait for complete synchronization
  559. (+++) Call function HAL_RCCEx_CRSWaitSynchronization()
  560. (+++) According to CRS status, user can decide to adjust again the calibration or continue
  561. application if synchronization is OK
  562. (#) User can retrieve information related to synchronization in calling function
  563. HAL_RCCEx_CRSGetSynchronizationInfo()
  564. (#) Regarding synchronization status and synchronization information, user can try a new calibration
  565. in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
  566. Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
  567. it means that the actual frequency is lower than the target (and so, that the TRIM value should be
  568. incremented), while when it is detected during the upcounting phase it means that the actual frequency
  569. is higher (and that the TRIM value should be decremented).
  570. (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
  571. through CRS Handler (RCC_IRQn/RCC_IRQHandler)
  572. (++) Call function HAL_RCCEx_CRSConfig()
  573. (++) Enable RCC_IRQn (thanks to NVIC functions)
  574. (++) Enable CRS interrupt (__HAL_RCC_CRS_ENABLE_IT)
  575. (++) Implement CRS status management in the following user callbacks called from
  576. HAL_RCCEx_CRS_IRQHandler():
  577. (+++) HAL_RCCEx_CRS_SyncOkCallback()
  578. (+++) HAL_RCCEx_CRS_SyncWarnCallback()
  579. (+++) HAL_RCCEx_CRS_ExpectedSyncCallback()
  580. (+++) HAL_RCCEx_CRS_ErrorCallback()
  581. (#) To force a SYNC EVENT, user can use the function HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
  582. This function can be called before calling HAL_RCCEx_CRSConfig (for instance in Systick handler)
  583. @endverbatim
  584. * @{
  585. */
  586. /**
  587. * @brief Start automatic synchronization for polling mode
  588. * @param pInit Pointer on RCC_CRSInitTypeDef structure
  589. * @retval None
  590. */
  591. void HAL_RCCEx_CRSConfig(RCC_CRSInitTypeDef *pInit)
  592. {
  593. uint32_t value = 0U;
  594. /* Check the parameters */
  595. assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
  596. assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
  597. assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
  598. assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
  599. assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
  600. assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
  601. /* CONFIGURATION */
  602. /* Before configuration, reset CRS registers to their default values*/
  603. __HAL_RCC_CRS_FORCE_RESET();
  604. __HAL_RCC_CRS_RELEASE_RESET();
  605. /* Set the SYNCDIV[2:0] bits according to Prescaler value */
  606. /* Set the SYNCSRC[1:0] bits according to Source value */
  607. /* Set the SYNCSPOL bit according to Polarity value */
  608. value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
  609. /* Set the RELOAD[15:0] bits according to ReloadValue value */
  610. value |= pInit->ReloadValue;
  611. /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
  612. value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_BITNUMBER);
  613. WRITE_REG(CRS->CFGR, value);
  614. /* Adjust HSI48 oscillator smooth trimming */
  615. /* Set the TRIM[5:0] bits according to RCC_CRS_HSI48CalibrationValue value */
  616. MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_BITNUMBER));
  617. /* START AUTOMATIC SYNCHRONIZATION*/
  618. /* Enable Automatic trimming & Frequency error counter */
  619. SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
  620. }
  621. /**
  622. * @brief Generate the software synchronization event
  623. * @retval None
  624. */
  625. void HAL_RCCEx_CRSSoftwareSynchronizationGenerate(void)
  626. {
  627. SET_BIT(CRS->CR, CRS_CR_SWSYNC);
  628. }
  629. /**
  630. * @brief Return synchronization info
  631. * @param pSynchroInfo Pointer on RCC_CRSSynchroInfoTypeDef structure
  632. * @retval None
  633. */
  634. void HAL_RCCEx_CRSGetSynchronizationInfo(RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
  635. {
  636. /* Check the parameter */
  637. assert_param(pSynchroInfo != NULL);
  638. /* Get the reload value */
  639. pSynchroInfo->ReloadValue = (uint32_t)(READ_BIT(CRS->CFGR, CRS_CFGR_RELOAD));
  640. /* Get HSI48 oscillator smooth trimming */
  641. pSynchroInfo->HSI48CalibrationValue = (uint32_t)(READ_BIT(CRS->CR, CRS_CR_TRIM) >> CRS_CR_TRIM_BITNUMBER);
  642. /* Get Frequency error capture */
  643. pSynchroInfo->FreqErrorCapture = (uint32_t)(READ_BIT(CRS->ISR, CRS_ISR_FECAP) >> CRS_ISR_FECAP_BITNUMBER);
  644. /* Get Frequency error direction */
  645. pSynchroInfo->FreqErrorDirection = (uint32_t)(READ_BIT(CRS->ISR, CRS_ISR_FEDIR));
  646. }
  647. /**
  648. * @brief Wait for CRS Synchronization status.
  649. * @param Timeout Duration of the timeout
  650. * @note Timeout is based on the maximum time to receive a SYNC event based on synchronization
  651. * frequency.
  652. * @note If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
  653. * @retval Combination of Synchronization status
  654. * This parameter can be a combination of the following values:
  655. * @arg @ref RCC_CRS_TIMEOUT
  656. * @arg @ref RCC_CRS_SYNCOK
  657. * @arg @ref RCC_CRS_SYNCWARN
  658. * @arg @ref RCC_CRS_SYNCERR
  659. * @arg @ref RCC_CRS_SYNCMISS
  660. * @arg @ref RCC_CRS_TRIMOVF
  661. */
  662. uint32_t HAL_RCCEx_CRSWaitSynchronization(uint32_t Timeout)
  663. {
  664. uint32_t crsstatus = RCC_CRS_NONE;
  665. uint32_t tickstart = 0U;
  666. /* Get timeout */
  667. tickstart = HAL_GetTick();
  668. /* Wait for CRS flag or timeout detection */
  669. do
  670. {
  671. if(Timeout != HAL_MAX_DELAY)
  672. {
  673. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  674. {
  675. crsstatus = RCC_CRS_TIMEOUT;
  676. }
  677. }
  678. /* Check CRS SYNCOK flag */
  679. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
  680. {
  681. /* CRS SYNC event OK */
  682. crsstatus |= RCC_CRS_SYNCOK;
  683. /* Clear CRS SYNC event OK bit */
  684. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
  685. }
  686. /* Check CRS SYNCWARN flag */
  687. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
  688. {
  689. /* CRS SYNC warning */
  690. crsstatus |= RCC_CRS_SYNCWARN;
  691. /* Clear CRS SYNCWARN bit */
  692. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
  693. }
  694. /* Check CRS TRIM overflow flag */
  695. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
  696. {
  697. /* CRS SYNC Error */
  698. crsstatus |= RCC_CRS_TRIMOVF;
  699. /* Clear CRS Error bit */
  700. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
  701. }
  702. /* Check CRS Error flag */
  703. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
  704. {
  705. /* CRS SYNC Error */
  706. crsstatus |= RCC_CRS_SYNCERR;
  707. /* Clear CRS Error bit */
  708. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
  709. }
  710. /* Check CRS SYNC Missed flag */
  711. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
  712. {
  713. /* CRS SYNC Missed */
  714. crsstatus |= RCC_CRS_SYNCMISS;
  715. /* Clear CRS SYNC Missed bit */
  716. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
  717. }
  718. /* Check CRS Expected SYNC flag */
  719. if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
  720. {
  721. /* frequency error counter reached a zero value */
  722. __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
  723. }
  724. } while(RCC_CRS_NONE == crsstatus);
  725. return crsstatus;
  726. }
  727. /**
  728. * @brief Handle the Clock Recovery System interrupt request.
  729. * @retval None
  730. */
  731. void HAL_RCCEx_CRS_IRQHandler(void)
  732. {
  733. uint32_t crserror = RCC_CRS_NONE;
  734. /* Get current IT flags and IT sources values */
  735. uint32_t itflags = READ_REG(CRS->ISR);
  736. uint32_t itsources = READ_REG(CRS->CR);
  737. /* Check CRS SYNCOK flag */
  738. if(((itflags & RCC_CRS_FLAG_SYNCOK) != RESET) && ((itsources & RCC_CRS_IT_SYNCOK) != RESET))
  739. {
  740. /* Clear CRS SYNC event OK flag */
  741. WRITE_REG(CRS->ICR, CRS_ICR_SYNCOKC);
  742. /* user callback */
  743. HAL_RCCEx_CRS_SyncOkCallback();
  744. }
  745. /* Check CRS SYNCWARN flag */
  746. else if(((itflags & RCC_CRS_FLAG_SYNCWARN) != RESET) && ((itsources & RCC_CRS_IT_SYNCWARN) != RESET))
  747. {
  748. /* Clear CRS SYNCWARN flag */
  749. WRITE_REG(CRS->ICR, CRS_ICR_SYNCWARNC);
  750. /* user callback */
  751. HAL_RCCEx_CRS_SyncWarnCallback();
  752. }
  753. /* Check CRS Expected SYNC flag */
  754. else if(((itflags & RCC_CRS_FLAG_ESYNC) != RESET) && ((itsources & RCC_CRS_IT_ESYNC) != RESET))
  755. {
  756. /* frequency error counter reached a zero value */
  757. WRITE_REG(CRS->ICR, CRS_ICR_ESYNCC);
  758. /* user callback */
  759. HAL_RCCEx_CRS_ExpectedSyncCallback();
  760. }
  761. /* Check CRS Error flags */
  762. else
  763. {
  764. if(((itflags & RCC_CRS_FLAG_ERR) != RESET) && ((itsources & RCC_CRS_IT_ERR) != RESET))
  765. {
  766. if((itflags & RCC_CRS_FLAG_SYNCERR) != RESET)
  767. {
  768. crserror |= RCC_CRS_SYNCERR;
  769. }
  770. if((itflags & RCC_CRS_FLAG_SYNCMISS) != RESET)
  771. {
  772. crserror |= RCC_CRS_SYNCMISS;
  773. }
  774. if((itflags & RCC_CRS_FLAG_TRIMOVF) != RESET)
  775. {
  776. crserror |= RCC_CRS_TRIMOVF;
  777. }
  778. /* Clear CRS Error flags */
  779. WRITE_REG(CRS->ICR, CRS_ICR_ERRC);
  780. /* user error callback */
  781. HAL_RCCEx_CRS_ErrorCallback(crserror);
  782. }
  783. }
  784. }
  785. /**
  786. * @brief RCCEx Clock Recovery System SYNCOK interrupt callback.
  787. * @retval none
  788. */
  789. __weak void HAL_RCCEx_CRS_SyncOkCallback(void)
  790. {
  791. /* NOTE : This function should not be modified, when the callback is needed,
  792. the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
  793. */
  794. }
  795. /**
  796. * @brief RCCEx Clock Recovery System SYNCWARN interrupt callback.
  797. * @retval none
  798. */
  799. __weak void HAL_RCCEx_CRS_SyncWarnCallback(void)
  800. {
  801. /* NOTE : This function should not be modified, when the callback is needed,
  802. the @ref HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
  803. */
  804. }
  805. /**
  806. * @brief RCCEx Clock Recovery System Expected SYNC interrupt callback.
  807. * @retval none
  808. */
  809. __weak void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
  810. {
  811. /* NOTE : This function should not be modified, when the callback is needed,
  812. the @ref HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
  813. */
  814. }
  815. /**
  816. * @brief RCCEx Clock Recovery System Error interrupt callback.
  817. * @param Error Combination of Error status.
  818. * This parameter can be a combination of the following values:
  819. * @arg @ref RCC_CRS_SYNCERR
  820. * @arg @ref RCC_CRS_SYNCMISS
  821. * @arg @ref RCC_CRS_TRIMOVF
  822. * @retval none
  823. */
  824. __weak void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
  825. {
  826. /* Prevent unused argument(s) compilation warning */
  827. UNUSED(Error);
  828. /* NOTE : This function should not be modified, when the callback is needed,
  829. the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
  830. */
  831. }
  832. /**
  833. * @}
  834. */
  835. #endif /* CRS */
  836. /**
  837. * @}
  838. */
  839. /**
  840. * @}
  841. */
  842. /**
  843. * @}
  844. */
  845. #endif /* HAL_RCC_MODULE_ENABLED */
  846. /**
  847. * @}
  848. */