stm32l1xx_hal_rcc_ex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_rcc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief Extended RCC HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities RCC extension peripheral:
  11. * + Extended Peripheral Control functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are permitted provided that the following conditions are met:
  20. * 1. Redistributions of source code must retain the above copyright notice,
  21. * this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ******************************************************************************
  41. */
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32l1xx_hal.h"
  44. /** @addtogroup STM32L1xx_HAL_Driver
  45. * @{
  46. */
  47. /** @defgroup RCCEx RCCEx
  48. * @brief RCC Extension HAL module driver
  49. * @{
  50. */
  51. #ifdef HAL_RCC_MODULE_ENABLED
  52. /* Private typedef -----------------------------------------------------------*/
  53. /* Private define ------------------------------------------------------------*/
  54. /* Private macro -------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private function prototypes -----------------------------------------------*/
  57. /* Private functions ---------------------------------------------------------*/
  58. /** @defgroup RCCEx_Private_Functions RCCEx Exported Functions
  59. * @{
  60. */
  61. /** @defgroup RCCEx_Exported_Functions_Group1 Extended Peripheral Control functions
  62. * @brief Extended Peripheral Control functions
  63. *
  64. @verbatim
  65. ===============================================================================
  66. ##### Extended Peripheral Control functions #####
  67. ===============================================================================
  68. [..]
  69. This subsection provides a set of functions allowing to control the RCC Clocks
  70. frequencies.
  71. [..]
  72. (@) Important note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to
  73. select the RTC clock source; in this case the Backup domain will be reset in
  74. order to modify the RTC Clock source, as consequence RTC registers (including
  75. the backup registers) and RCC_BDCR register are set to their reset values.
  76. @endverbatim
  77. * @{
  78. */
  79. /**
  80. * @brief Initializes the RCC extended peripherals clocks according to the specified parameters in the
  81. * RCC_PeriphCLKInitTypeDef.
  82. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  83. * contains the configuration information for the Extended Peripherals clocks(RTC/LCD clock).
  84. * @retval HAL status
  85. */
  86. HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  87. {
  88. uint32_t tickstart = 0;
  89. uint32_t tmpreg = 0;
  90. /* Check the parameters */
  91. assert_param(IS_RCC_PERIPHCLOCK(PeriphClkInit->PeriphClockSelection));
  92. /*------------------------------- RTC/LCD Configuration ------------------------*/
  93. if ((((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_RTC) == RCC_PERIPHCLK_RTC)
  94. #if defined (STM32L100xB) || defined (STM32L100xBA) || defined (STM32L100xC) ||\
  95. defined(STM32L152xB) || defined(STM32L152xBA) || defined(STM32L152xC) || \
  96. defined(STM32L162xC) || defined(STM32L152xCA) || defined(STM32L152xD) || \
  97. defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L152xE) || \
  98. defined(STM32L162xE)
  99. || (((PeriphClkInit->PeriphClockSelection) & RCC_PERIPHCLK_LCD) == RCC_PERIPHCLK_LCD)
  100. #endif /* STM32L100xB || STM32L152xBA || ... || STM32L152xE || STM32L162xE */
  101. )
  102. {
  103. /* Enable Power Controller clock */
  104. __PWR_CLK_ENABLE();
  105. /* Enable write access to Backup domain */
  106. SET_BIT(PWR->CR, PWR_CR_DBP);
  107. /* Wait for Backup domain Write protection disable */
  108. tickstart = HAL_GetTick();
  109. while((PWR->CR & PWR_CR_DBP) == RESET)
  110. {
  111. if((HAL_GetTick() - tickstart ) > DBP_TIMEOUT_VALUE)
  112. {
  113. return HAL_TIMEOUT;
  114. }
  115. }
  116. tmpreg = (RCC->CSR & RCC_CSR_RTCSEL);
  117. /* Reset the Backup domain only if the RTC Clock source selection is modified */
  118. if((tmpreg != (PeriphClkInit->RTCClockSelection & RCC_CSR_RTCSEL))
  119. #if defined (STM32L100xB) || defined (STM32L100xBA) || defined (STM32L100xC) ||\
  120. defined(STM32L152xB) || defined(STM32L152xBA) || defined(STM32L152xC) || \
  121. defined(STM32L162xC) || defined(STM32L152xCA) || defined(STM32L152xD) || \
  122. defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L152xE) || \
  123. defined(STM32L162xE)
  124. || (tmpreg != (PeriphClkInit->LCDClockSelection & RCC_CSR_RTCSEL))
  125. #endif /* STM32L100xB || STM32L152xBA || ... || STM32L152xE || STM32L162xE */
  126. )
  127. {
  128. /* Store the content of CSR register before the reset of Backup Domain */
  129. tmpreg = (RCC->CSR & ~(RCC_CSR_RTCSEL));
  130. /* RTC Clock selection can be changed only if the Backup Domain is reset */
  131. __HAL_RCC_BACKUPRESET_FORCE();
  132. __HAL_RCC_BACKUPRESET_RELEASE();
  133. /* Restore the Content of CSR register */
  134. RCC->CSR = tmpreg;
  135. }
  136. /* If LSE is selected as RTC clock source, wait for LSE reactivation */
  137. if ((PeriphClkInit->RTCClockSelection == RCC_RTCCLKSOURCE_LSE)
  138. #if defined (STM32L100xB) || defined (STM32L100xBA) || defined (STM32L100xC) ||\
  139. defined(STM32L152xB) || defined(STM32L152xBA) || defined(STM32L152xC) || \
  140. defined(STM32L162xC) || defined(STM32L152xCA) || defined(STM32L152xD) || \
  141. defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L152xE) || \
  142. defined(STM32L162xE)
  143. || (PeriphClkInit->LCDClockSelection == RCC_RTCCLKSOURCE_LSE)
  144. #endif /* STM32L100xB || STM32L152xBA || ... || STM32L152xE || STM32L162xE */
  145. )
  146. {
  147. /* Get timeout */
  148. tickstart = HAL_GetTick();
  149. /* Wait till LSE is ready */
  150. while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET)
  151. {
  152. if((HAL_GetTick() - tickstart ) > LSE_TIMEOUT_VALUE)
  153. {
  154. return HAL_TIMEOUT;
  155. }
  156. }
  157. }
  158. __HAL_RCC_RTC_CONFIG(PeriphClkInit->RTCClockSelection);
  159. }
  160. return HAL_OK;
  161. }
  162. /**
  163. * @brief Get the PeriphClkInit according to the internal
  164. * RCC configuration registers.
  165. * @param PeriphClkInit: pointer to an RCC_PeriphCLKInitTypeDef structure that
  166. * returns the configuration information for the Extended Peripherals clocks(RTC/LCD clocks).
  167. * @retval None
  168. */
  169. void HAL_RCCEx_GetPeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
  170. {
  171. uint32_t srcclk = 0;
  172. /* Set all possible values for the extended clock type parameter------------*/
  173. PeriphClkInit->PeriphClockSelection = RCC_PERIPHCLK_RTC;
  174. #if defined (STM32L100xB) || defined (STM32L100xBA) || defined (STM32L100xC) ||\
  175. defined(STM32L152xB) || defined(STM32L152xBA) || defined(STM32L152xC) || \
  176. defined(STM32L162xC) || defined(STM32L152xCA) || defined(STM32L152xD) || \
  177. defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L152xE) || \
  178. defined(STM32L162xE)
  179. PeriphClkInit->PeriphClockSelection |= RCC_PERIPHCLK_LCD;
  180. #endif /* STM32L100xB || STM32L152xBA || ... || STM32L152xE || STM32L162xE */
  181. /* Get the RTC/LCD configuration -----------------------------------------------*/
  182. srcclk = __HAL_RCC_GET_RTC_SOURCE();
  183. if (srcclk != RCC_RTCCLKSOURCE_HSE_DIV2)
  184. {
  185. /* Source clock is LSE or LSI*/
  186. PeriphClkInit->RTCClockSelection = srcclk;
  187. }
  188. else
  189. {
  190. /* Source clock is HSE. Need to get the prescaler value*/
  191. PeriphClkInit->RTCClockSelection = srcclk | (READ_BIT(RCC->CR, RCC_CR_RTCPRE));
  192. }
  193. #if defined (STM32L100xB) || defined (STM32L100xBA) || defined (STM32L100xC) ||\
  194. defined(STM32L152xB) || defined(STM32L152xBA) || defined(STM32L152xC) || \
  195. defined(STM32L162xC) || defined(STM32L152xCA) || defined(STM32L152xD) || \
  196. defined(STM32L162xCA) || defined(STM32L162xD) || defined(STM32L152xE) || \
  197. defined(STM32L162xE)
  198. PeriphClkInit->LCDClockSelection = PeriphClkInit->RTCClockSelection;
  199. #endif /* STM32L100xB || STM32L152xBA || ... || STM32L152xE || STM32L162xE */
  200. }
  201. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || \
  202. defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || \
  203. defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || \
  204. defined(STM32L151xE) || defined (STM32L152xE) || defined (STM32L162xE)
  205. /**
  206. * @brief Enables the LSE Clock Security System.
  207. * @note If a failure is detected on the external 32 kHz oscillator, the LSE clock is no longer supplied
  208. * to the RTC but no hardware action is made to the registers.
  209. * In Standby mode a wakeup is generated. In other modes an interrupt can be sent to wakeup
  210. * the software (see Section 5.3.4: Clock interrupt register (RCC_CIR) on page 104).
  211. * The software MUST then disable the LSECSSON bit, stop the defective 32 kHz oscillator
  212. * (disabling LSEON), and can change the RTC clock source (no clock or LSI or HSE, with
  213. * RTCSEL), or take any required action to secure the application.
  214. * @note LSE CSS available only for high density and medium+ devices
  215. * @retval None
  216. */
  217. void HAL_RCCEx_EnableLSECSS(void)
  218. {
  219. *(__IO uint32_t *) CSR_LSECSSON_BB = (uint32_t)ENABLE;
  220. }
  221. /**
  222. * @brief Disables the LSE Clock Security System.
  223. * @note Once enabled this bit cannot be disabled, except after an LSE failure detection
  224. * (LSECSSD=1). In that case the software MUST disable the LSECSSON bit.
  225. * Reset by power on reset and RTC software reset (RTCRST bit).
  226. * @note LSE CSS available only for high density and medium+ devices
  227. * @retval None
  228. */
  229. void HAL_RCCEx_DisableLSECSS(void)
  230. {
  231. *(__IO uint32_t *) CSR_LSECSSON_BB = (uint32_t)DISABLE;
  232. }
  233. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
  234. /**
  235. * @}
  236. */
  237. /**
  238. * @}
  239. */
  240. #endif /* HAL_RCC_MODULE_ENABLED */
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/