stm32l1xx_hal_rcc_ex.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32l1xx_hal.h"
  41. /** @addtogroup STM32L1xx_HAL_Driver
  42. * @{
  43. */
  44. #ifdef HAL_RCC_MODULE_ENABLED
  45. /** @defgroup RCCEx RCCEx
  46. * @brief RCC Extension HAL module driver
  47. * @{
  48. */
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /** @defgroup RCCEx_Private_Constants RCCEx Private Constants
  52. * @{
  53. */
  54. /**
  55. * @}
  56. */
  57. /* Private macro -------------------------------------------------------------*/
  58. /** @defgroup RCCEx_Private_Macros RCCEx Private Macros
  59. * @{
  60. */
  61. /**
  62. * @}
  63. */
  64. /* Private variables ---------------------------------------------------------*/
  65. /* Private function prototypes -----------------------------------------------*/
  66. /* Private functions ---------------------------------------------------------*/
  67. /** @defgroup RCCEx_Exported_Functions RCCEx Exported Functions
  68. * @{
  69. */
  70. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  71. * @brief Extended Peripheral Control functions
  72. *
  73. @verbatim
  74. ===============================================================================
  75. ##### Extended Peripheral Control functions #####
  76. ===============================================================================
  77. [..]
  78. This subsection provides a set of functions allowing to control the RCC Clocks
  79. frequencies.
  80. [..]
  81. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  82. select the RTC clock source; in this case the Backup domain will be reset in
  83. order to modify the RTC Clock source, as consequence RTC registers (including
  84. the backup registers) are set to their reset values.
  85. @endverbatim
  86. * @{
  87. */
  88. /**
  89. * @brief Initializes the RCC extended peripherals clocks according to the specified
  90. * parameters in the RCC_PeriphCLKInitTypeDef.
  91. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  92. * contains the configuration information for the Extended Peripherals clocks(RTC/LCD clock).
  93. * @retval HAL status
  94. * @note If HAL_ERROR returned, first switch-OFF HSE clock oscillator with @ref HAL_RCC_OscConfig()
  95. * to possibly update HSE divider.
  96. */
  97. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  98. {
  99. uint32_t tickstart = 0U;
  100. uint32_t temp_reg = 0U;
  101. /* Check the parameters */
  102. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  103. /*------------------------------- RTC/LCD Configuration ------------------------*/
  104. if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  105. #if defined(LCD)
  106. || (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LCD) == RCC_PERIPHCLK_LCD)
  107. #endif /* LCD */
  108. )
  109. {
  110. /* check for RTC Parameters used to output RTCCLK */
  111. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  112. {
  113. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->RTCClockSelection));
  114. }
  115. #if defined(LCD)
  116. if(((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LCD) == RCC_PERIPHCLK_LCD)
  117. {
  118. assert_param(IS_RCC_RTCCLKSOURCE(PeriphClkInit->LCDClockSelection));
  119. }
  120. #endif /* LCD */
  121. FlagStatus pwrclkchanged = RESET;
  122. /* As soon as function is called to change RTC clock source, activation of the
  123. power domain is done. */
  124. /* Requires to enable write access to Backup Domain of necessary */
  125. if(__HAL_RCC_PWR_IS_CLK_DISABLED())
  126. {
  127. __HAL_RCC_PWR_CLK_ENABLE();
  128. pwrclkchanged = SET;
  129. }
  130. if(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  131. {
  132. /* Enable write access to Backup domain */
  133. SET_BIT(PWR->CR, PWR_CR_DBP);
  134. /* Wait for Backup domain Write protection disable */
  135. tickstart = HAL_GetTick();
  136. while(HAL_IS_BIT_CLR(PWR->CR, PWR_CR_DBP))
  137. {
  138. if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
  139. {
  140. return HAL_TIMEOUT;
  141. }
  142. }
  143. }
  144. /* Check if user wants to change HSE RTC prescaler whereas HSE is enabled */
  145. temp_reg = (RCC->CR & RCC_CR_RTCPRE);
  146. if ((temp_reg != (PeriphClkInit->RTCClockSelection & RCC_CR_RTCPRE))
  147. #if defined (LCD)
  148. || (temp_reg != (PeriphClkInit->LCDClockSelection & RCC_CR_RTCPRE))
  149. #endif /* LCD */
  150. )
  151. { /* Check HSE State */
  152. if (((PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL) == RCC_CSR_RTCSEL_HSE) && HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY))
  153. {
  154. /* To update HSE divider, first switch-OFF HSE clock oscillator*/
  155. return HAL_ERROR;
  156. }
  157. }
  158. /* Reset the Backup domain only if the RTC Clock source selection is modified from reset value */
  159. temp_reg = (RCC->CSR & RCC_CSR_RTCSEL);
  160. if((temp_reg != 0x00000000U) && (((temp_reg != (PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL)) \
  161. && (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC))
  162. #if defined(LCD)
  163. || ((temp_reg != (PeriphClkInit->LCDClockSelection & RCC_CSR_RTCSEL)) \
  164. && (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LCD) == RCC_PERIPHCLK_LCD))
  165. #endif /* LCD */
  166. ))
  167. {
  168. /* Store the content of CSR register before the reset of Backup Domain */
  169. temp_reg = (RCC->CSR & ~(RCC_CSR_RTCSEL));
  170. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  171. __HAL_RCC_BACKUPRESET_FORCE();
  172. __HAL_RCC_BACKUPRESET_RELEASE();
  173. /* Restore the Content of CSR register */
  174. RCC->CSR = temp_reg;
  175. /* Wait for LSERDY if LSE was enabled */
  176. if (HAL_IS_BIT_SET(temp_reg, RCC_CSR_LSEON))
  177. {
  178. /* Get Start Tick */
  179. tickstart = HAL_GetTick();
  180. /* Wait till LSE is ready */
  181. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  182. {
  183. if((HAL_GetTick() - tickstart ) > RCC_LSE_TIMEOUT_VALUE)
  184. {
  185. return HAL_TIMEOUT;
  186. }
  187. }
  188. }
  189. }
  190. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  191. /* Require to disable power clock if necessary */
  192. if(pwrclkchanged == SET)
  193. {
  194. __HAL_RCC_PWR_CLK_DISABLE();
  195. }
  196. }
  197. return HAL_OK;
  198. }
  199. /**
  200. * @brief Get the PeriphClkInit according to the internal RCC configuration registers.
  201. * @param PeriphClkInit pointer to an RCC_PeriphCLKInitTypeDef structure that
  202. * returns the configuration information for the Extended Peripherals clocks(RTC/LCD clocks).
  203. * @retval None
  204. */
  205. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  206. {
  207. uint32_t srcclk = 0U;
  208. /* Set all possible values for the extended clock type parameter------------*/
  209. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_RTC;
  210. #if defined(LCD)
  211. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LCD;
  212. #endif /* LCD */
  213. /* Get the RTC/LCD configuration -----------------------------------------------*/
  214. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  215. if (srcclk != RCC_RTCCLKSOURCE_HSE_DIV2)
  216. {
  217. /* Source clock is LSE or LSI*/
  218. PeriphClkInit->RTCClockSelection = srcclk;
  219. }
  220. else
  221. {
  222. /* Source clock is HSE. Need to get the prescaler value*/
  223. PeriphClkInit->RTCClockSelection = srcclk | (READ_BIT(RCC->CR, RCC_CR_RTCPRE));
  224. }
  225. #if defined(LCD)
  226. PeriphClkInit->LCDClockSelection = PeriphClkInit->RTCClockSelection;
  227. #endif /* LCD */
  228. }
  229. /**
  230. * @brief Return the peripheral clock frequency
  231. * @note Return 0 if peripheral clock is unknown
  232. * @param PeriphClk Peripheral clock identifier
  233. * This parameter can be one of the following values:
  234. * @arg @ref RCC_PERIPHCLK_RTC RTC peripheral clock
  235. * @arg @ref RCC_PERIPHCLK_LCD LCD peripheral clock (*)
  236. * @note (*) means that this peripheral is not present on all the devices
  237. * @retval Frequency in Hz (0: means that no available frequency for the peripheral)
  238. */
  239. uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint32_t PeriphClk)
  240. {
  241. uint32_t temp_reg = 0U, clkprediv = 0U, frequency = 0U;
  242. uint32_t srcclk = 0U;
  243. /* Check the parameters */
  244. assert_param(IS_RCC_PERIPHCLOCK(PeriphClk));
  245. switch (PeriphClk)
  246. {
  247. case RCC_PERIPHCLK_RTC:
  248. #if defined(LCD)
  249. case RCC_PERIPHCLK_LCD:
  250. #endif /* LCD */
  251. {
  252. /* Get RCC CSR configuration ------------------------------------------------------*/
  253. temp_reg = RCC->CSR;
  254. /* Get the current RTC source */
  255. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  256. /* Check if LSE is ready if RTC clock selection is LSE */
  257. if ((srcclk == RCC_RTCCLKSOURCE_LSE) && (HAL_IS_BIT_SET(temp_reg, RCC_CSR_LSERDY)))
  258. {
  259. frequency = LSE_VALUE;
  260. }
  261. /* Check if LSI is ready if RTC clock selection is LSI */
  262. else if ((srcclk == RCC_RTCCLKSOURCE_LSI) && (HAL_IS_BIT_SET(temp_reg, RCC_CSR_LSIRDY)))
  263. {
  264. frequency = LSI_VALUE;
  265. }
  266. /* Check if HSE is ready and if RTC clock selection is HSE */
  267. else if ((srcclk == RCC_RTCCLKSOURCE_HSE_DIVX) && (HAL_IS_BIT_SET(RCC->CR, RCC_CR_HSERDY)))
  268. {
  269. /* Get the current HSE clock divider */
  270. clkprediv = __HAL_RCC_GET_RTC_HSE_PRESCALER();
  271. switch (clkprediv)
  272. {
  273. case RCC_RTC_HSE_DIV_16: /* HSE DIV16 has been selected */
  274. {
  275. frequency = HSE_VALUE / 16U;
  276. break;
  277. }
  278. case RCC_RTC_HSE_DIV_8: /* HSE DIV8 has been selected */
  279. {
  280. frequency = HSE_VALUE / 8U;
  281. break;
  282. }
  283. case RCC_RTC_HSE_DIV_4: /* HSE DIV4 has been selected */
  284. {
  285. frequency = HSE_VALUE / 4U;
  286. break;
  287. }
  288. default: /* HSE DIV2 has been selected */
  289. {
  290. frequency = HSE_VALUE / 2U;
  291. break;
  292. }
  293. }
  294. }
  295. /* Clock not enabled for RTC */
  296. else
  297. {
  298. frequency = 0U;
  299. }
  300. break;
  301. }
  302. default:
  303. {
  304. break;
  305. }
  306. }
  307. return(frequency);
  308. }
  309. #if defined(RCC_LSECSS_SUPPORT)
  310. /**
  311. * @brief Enables the LSE Clock Security System.
  312. * @note If a failure is detected on the external 32 kHz oscillator, the LSE clock is no longer supplied
  313. * to the RTC but no hardware action is made to the registers.
  314. * In Standby mode a wakeup is generated. In other modes an interrupt can be sent to wakeup
  315. * the software (see Section 5.3.4: Clock interrupt register (RCC_CIR) on page 104).
  316. * The software MUST then disable the LSECSSON bit, stop the defective 32 kHz oscillator
  317. * (disabling LSEON), and can change the RTC clock source (no clock or LSI or HSE, with
  318. * RTCSEL), or take any required action to secure the application.
  319. * @note LSE CSS available only for high density and medium+ devices
  320. * @retval None
  321. */
  322. void HAL_RCCEx_EnableLSECSS(void)
  323. {
  324. *(__IO uint32_t *) CSR_LSECSSON_BB = (uint32_t)ENABLE;
  325. }
  326. /**
  327. * @brief Disables the LSE Clock Security System.
  328. * @note Once enabled this bit cannot be disabled, except after an LSE failure detection
  329. * (LSECSSD=1). In that case the software MUST disable the LSECSSON bit.
  330. * Reset by power on reset and RTC software reset (RTCRST bit).
  331. * @note LSE CSS available only for high density and medium+ devices
  332. * @retval None
  333. */
  334. void HAL_RCCEx_DisableLSECSS(void)
  335. {
  336. /* Disable LSE CSS */
  337. *(__IO uint32_t *) CSR_LSECSSON_BB = (uint32_t)DISABLE;
  338. /* Disable LSE CSS IT */
  339. __HAL_RCC_DISABLE_IT(RCC_IT_LSECSS);
  340. }
  341. /**
  342. * @brief Enable the LSE Clock Security System IT & corresponding EXTI line.
  343. * @note LSE Clock Security System IT is mapped on RTC EXTI line 19
  344. * @retval None
  345. */
  346. void HAL_RCCEx_EnableLSECSS_IT(void)
  347. {
  348. /* Enable LSE CSS */
  349. *(__IO uint32_t *) CSR_LSECSSON_BB = (uint32_t)ENABLE;
  350. /* Enable LSE CSS IT */
  351. __HAL_RCC_ENABLE_IT(RCC_IT_LSECSS);
  352. /* Enable IT on EXTI Line 19 */
  353. __HAL_RCC_LSECSS_EXTI_ENABLE_IT();
  354. __HAL_RCC_LSECSS_EXTI_ENABLE_RISING_EDGE();
  355. }
  356. /**
  357. * @brief Handle the RCC LSE Clock Security System interrupt request.
  358. * @retval None
  359. */
  360. void HAL_RCCEx_LSECSS_IRQHandler(void)
  361. {
  362. /* Check RCC LSE CSSF flag */
  363. if(__HAL_RCC_GET_IT(RCC_IT_LSECSS))
  364. {
  365. /* RCC LSE Clock Security System interrupt user callback */
  366. HAL_RCCEx_LSECSS_Callback();
  367. /* Clear RCC LSE CSS pending bit */
  368. __HAL_RCC_CLEAR_IT(RCC_IT_LSECSS);
  369. }
  370. }
  371. /**
  372. * @brief RCCEx LSE Clock Security System interrupt callback.
  373. * @retval none
  374. */
  375. __weak void HAL_RCCEx_LSECSS_Callback(void)
  376. {
  377. /* NOTE : This function should not be modified, when the callback is needed,
  378. the @ref HAL_RCCEx_LSECSS_Callback should be implemented in the user file
  379. */
  380. }
  381. #endif /* RCC_LSECSS_SUPPORT */
  382. /**
  383. * @}
  384. */
  385. /**
  386. * @}
  387. */
  388. /**
  389. * @}
  390. */
  391. /**
  392. * @}
  393. */
  394. #endif /* HAL_RCC_MODULE_ENABLED */
  395. /**
  396. * @}
  397. */
  398. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/