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.
 
 
 

981 lines
35 KiB

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