stm32f2xx_hal_rcc_ex.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extension 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. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  14. * All rights reserved.</center></h2>
  15. *
  16. * This software component is licensed by ST under BSD 3-Clause license,
  17. * the "License"; You may not use this file except in compliance with the
  18. * License. You may obtain a copy of the License at:
  19. * opensource.org/licenses/BSD-3-Clause
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32f2xx_hal.h"
  25. /** @addtogroup STM32F2xx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup RCCEx RCCEx
  29. * @brief RCCEx HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_RCC_MODULE_ENABLED
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /** @addtogroup RCCEx_Private_Constants
  36. * @{
  37. */
  38. /**
  39. * @}
  40. */
  41. /* Private macro -------------------------------------------------------------*/
  42. /* Private variables ---------------------------------------------------------*/
  43. /* Private function prototypes -----------------------------------------------*/
  44. /* Private functions ---------------------------------------------------------*/
  45. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  46. * @{
  47. */
  48. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  49. * @brief Extended Peripheral Control functions
  50. *
  51. @verbatim
  52. ===============================================================================
  53. ##### Extended Peripheral Control functions #####
  54. ===============================================================================
  55. [..]
  56. This subsection provides a set of functions allowing to control the RCC Clocks
  57. frequencies.
  58. [..]
  59. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  60. select the RTC clock source; in this case the Backup domain will be reset in
  61. order to modify the RTC Clock source, as consequence RTC registers (including
  62. the backup registers) and RCC_BDCR register are set to their reset values.
  63. @endverbatim
  64. * @{
  65. */
  66. /**
  67. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  68. * RCC_PeriphCLKInitTypeDef.
  69. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  70. * contains the configuration information for the Extended Peripherals clocks(I2S and RTC clocks).
  71. *
  72. * @note A caution to be taken when HAL_RCCEx_PeriphCLKConfig() is used to select RTC clock selection, in this case
  73. * the Reset of Backup domain will be applied in order to modify the RTC Clock source as consequence all backup
  74. * domain (RTC and RCC_BDCR register expect BKPSRAM) will be reset
  75. *
  76. * @retval HAL status
  77. */
  78. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  79. {
  80. uint32_t tickstart = 0U;
  81. uint32_t tmpreg1 = 0U;
  82. /* Check the parameters */
  83. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  84. /*---------------------------- I2S configuration ---------------------------*/
  85. if(((PeriphClkInit->PeriphClockSelection & RCC_PERIPHCLK_I2S) == (RCC_PERIPHCLK_I2S))|| \
  86. (PeriphClkInit->PeriphClockSelection == RCC_PERIPHCLK_PLLI2S))
  87. {
  88. /* check for Parameters */
  89. assert_param(IS_RCC_PLLI2SR_VALUE(PeriphClkInit->PLLI2S.PLLI2SR));
  90. assert_param(IS_RCC_PLLI2SN_VALUE(PeriphClkInit->PLLI2S.PLLI2SN));
  91. /* Disable the PLLI2S */
  92. __HAL_RCC_PLLI2S_DISABLE();
  93. /* Get tick */
  94. tickstart = HAL_GetTick();
  95. /* Wait till PLLI2S is disabled */
  96. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) != RESET)
  97. {
  98. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  99. {
  100. /* return in case of Timeout detected */
  101. return HAL_TIMEOUT;
  102. }
  103. }
  104. /* Configure the PLLI2S division factors */
  105. /* PLLI2S_VCO = f(VCO clock) = f(PLLI2S clock input) * (PLLI2SN/PLLM) */
  106. /* I2SCLK = f(PLLI2S clock output) = f(VCO clock) / PLLI2SR */
  107. __HAL_RCC_PLLI2S_CONFIG(PeriphClkInit->PLLI2S.PLLI2SN , PeriphClkInit->PLLI2S.PLLI2SR);
  108. /* Enable the PLLI2S */
  109. __HAL_RCC_PLLI2S_ENABLE();
  110. /* Get tick */
  111. tickstart = HAL_GetTick();
  112. /* Wait till PLLI2S is ready */
  113. while(__HAL_RCC_GET_FLAG(RCC_FLAG_PLLI2SRDY) == RESET)
  114. {
  115. if((HAL_GetTick() - tickstart ) > PLLI2S_TIMEOUT_VALUE)
  116. {
  117. /* return in case of Timeout detected */
  118. return HAL_TIMEOUT;
  119. }
  120. }
  121. }
  122. /*--------------------------------------------------------------------------*/
  123. /*---------------------------- RTC configuration ---------------------------*/
  124. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == (RCC_PERIPHCLK_RTC))
  125. {
  126. /* Check for RTC Parameters used to output RTCCLK */
  127. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  128. /* Enable Power Clock*/
  129. __HAL_RCC_PWR_CLK_ENABLE();
  130. /* Enable write access to Backup domain */
  131. PWR->CR |= PWR_CR_DBP;
  132. /* Get tick */
  133. tickstart = HAL_GetTick();
  134. while((PWR->CR & PWR_CR_DBP) == RESET)
  135. {
  136. if((HAL_GetTick() - tickstart ) > RCC_DBP_TIMEOUT_VALUE)
  137. {
  138. return HAL_TIMEOUT;
  139. }
  140. }
  141. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  142. tmpreg1 = (RCC->BDCR & RCC_BDCR_RTCSEL);
  143. if((tmpreg1 != 0x00000000U) && ((tmpreg1) != (PeriphClkInit->RTCClockSelection & RCC_BDCR_RTCSEL)))
  144. {
  145. /* Store the content of BDCR register before the reset of Backup Domain */
  146. tmpreg1 = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));
  147. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  148. __HAL_RCC_BACKUPRESET_FORCE();
  149. __HAL_RCC_BACKUPRESET_RELEASE();
  150. /* Restore the Content of BDCR register */
  151. RCC->BDCR = tmpreg1;
  152. /* Wait for LSE reactivation if LSE was enable prior to Backup Domain reset */
  153. if(HAL_IS_BIT_SET(RCC->BDCR, RCC_BDCR_LSEON))
  154. {
  155. /* Get tick */
  156. tickstart = HAL_GetTick();
  157. /* Wait till LSE is ready */
  158. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  159. {
  160. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  161. {
  162. return HAL_TIMEOUT;
  163. }
  164. }
  165. }
  166. }
  167. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  168. }
  169. /*--------------------------------------------------------------------------*/
  170. return HAL_OK;
  171. }
  172. /**
  173. * @brief Configures the RCC_OscInitStruct according to the internal
  174. * RCC configuration registers.
  175. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  176. * will be configured.
  177. * @retval None
  178. */
  179. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  180. {
  181. uint32_t tempreg;
  182. /* Set all possible values for the extended clock type parameter------------*/
  183. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_I2S | RCC_PERIPHCLK_RTC;
  184. /* Get the PLLI2S Clock configuration --------------------------------------*/
  185. PeriphClkInit->PLLI2S.PLLI2SN = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SN) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SN));
  186. PeriphClkInit->PLLI2S.PLLI2SR = (uint32_t)((RCC->PLLI2SCFGR & RCC_PLLI2SCFGR_PLLI2SR) >> POSITION_VAL(RCC_PLLI2SCFGR_PLLI2SR));
  187. /* Get the RTC Clock configuration -----------------------------------------------*/
  188. tempreg = (RCC->CFGR & RCC_CFGR_RTCPRE);
  189. PeriphClkInit->RTCClockSelection = (uint32_t)((tempreg) | (RCC->BDCR & RCC_BDCR_RTCSEL));
  190. }
  191. /**
  192. * @}
  193. */
  194. /**
  195. * @}
  196. */
  197. #endif /* HAL_RCC_MODULE_ENABLED */
  198. /**
  199. * @}
  200. */
  201. /**
  202. * @}
  203. */
  204. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/