Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

441 lines
14 KiB

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