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.
 
 

1781 line
54 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @brief RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real Time Clock (RTC) Extension peripheral:
  8. * + RTC Time Stamp functions
  9. * + RTC Tamper functions
  10. * + RTC Wake-up functions
  11. * + Extension Control functions
  12. * + Extension RTC features functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. (+) Enable the RTC domain access.
  20. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  21. format using the HAL_RTC_Init() function.
  22. *** RTC Wake-up configuration ***
  23. ================================
  24. [..]
  25. (+) To configure the RTC Wake-up Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
  26. function. You can also configure the RTC Wake-up timer in interrupt mode
  27. using the HAL_RTCEx_SetWakeUpTimer_IT() function.
  28. (+) To read the RTC Wake-up Counter register, use the HAL_RTCEx_GetWakeUpTimer()
  29. function.
  30. *** TimeStamp configuration ***
  31. ===============================
  32. [..]
  33. (+) Configure the RTC_AFx trigger and enable the RTC TimeStamp using the
  34. HAL_RTCEx_SetTimeStamp() function. You can also configure the RTC TimeStamp with
  35. interrupt mode using the HAL_RTCEx_SetTimeStamp_IT() function.
  36. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
  37. function.
  38. (+) The TIMESTAMP alternate function can be mapped either to RTC_AF1 (PC13)
  39. or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TSINSEL bit in
  40. RTC_TAFCR register. The corresponding pin is also selected by HAL_RTCEx_SetTimeStamp()
  41. or HAL_RTCEx_SetTimeStamp_IT() function.
  42. *** Tamper configuration ***
  43. ============================
  44. [..]
  45. (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
  46. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  47. value, sampling frequency, precharge or discharge and Pull-UP using the
  48. HAL_RTCEx_SetTamper() function. You can configure RTC Tamper in interrupt
  49. mode using HAL_RTCEx_SetTamper_IT() function.
  50. (+) The TAMPER1 alternate function can be mapped either to RTC_AF1 (PC13)
  51. or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TAMP1INSEL bit in
  52. RTC_TAFCR register. The corresponding pin is also selected by HAL_RTCEx_SetTamper()
  53. or HAL_RTCEx_SetTamper_IT() function.
  54. *** Backup Data Registers configuration ***
  55. ===========================================
  56. [..]
  57. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  58. function.
  59. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  60. function.
  61. @endverbatim
  62. ******************************************************************************
  63. * @attention
  64. *
  65. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  66. * All rights reserved.</center></h2>
  67. *
  68. * This software component is licensed by ST under BSD 3-Clause license,
  69. * the "License"; You may not use this file except in compliance with the
  70. * License. You may obtain a copy of the License at:
  71. * opensource.org/licenses/BSD-3-Clause
  72. *
  73. ******************************************************************************
  74. */
  75. /* Includes ------------------------------------------------------------------*/
  76. #include "stm32f4xx_hal.h"
  77. /** @addtogroup STM32F4xx_HAL_Driver
  78. * @{
  79. */
  80. /** @defgroup RTCEx RTCEx
  81. * @brief RTC HAL module driver
  82. * @{
  83. */
  84. #ifdef HAL_RTC_MODULE_ENABLED
  85. /* Private typedef -----------------------------------------------------------*/
  86. /* Private define ------------------------------------------------------------*/
  87. /* Private macro -------------------------------------------------------------*/
  88. /* Private variables ---------------------------------------------------------*/
  89. /* Private function prototypes -----------------------------------------------*/
  90. /* Private functions ---------------------------------------------------------*/
  91. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  92. * @{
  93. */
  94. /** @defgroup RTCEx_Exported_Functions_Group1 RTC TimeStamp and Tamper functions
  95. * @brief RTC TimeStamp and Tamper functions
  96. *
  97. @verbatim
  98. ===============================================================================
  99. ##### RTC TimeStamp and Tamper functions #####
  100. ===============================================================================
  101. [..] This section provides functions allowing to configure TimeStamp feature
  102. @endverbatim
  103. * @{
  104. */
  105. /**
  106. * @brief Sets TimeStamp.
  107. * @note This API must be called before enabling the TimeStamp feature.
  108. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  109. * the configuration information for RTC.
  110. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  111. * activated.
  112. * This parameter can be one of the following values:
  113. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  114. * rising edge of the related pin.
  115. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  116. * falling edge of the related pin.
  117. * @param RTC_TimeStampPin specifies the RTC TimeStamp Pin.
  118. * This parameter can be one of the following values:
  119. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  120. * @arg RTC_TIMESTAMPPIN_POS1: PI8/PA0 is selected as RTC TimeStamp Pin.
  121. * (not applicable in the case of STM32F412xx, STM32F413xx and STM32F423xx devices)
  122. * (PI8 for all STM32 devices except for STM32F446xx devices the PA0 is used)
  123. * @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
  124. * @retval HAL status
  125. */
  126. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  127. {
  128. uint32_t tmpreg = 0U;
  129. /* Check the parameters */
  130. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  131. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  132. /* Process Locked */
  133. __HAL_LOCK(hrtc);
  134. hrtc->State = HAL_RTC_STATE_BUSY;
  135. /* Get the RTC_CR register and clear the bits to be configured */
  136. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  137. tmpreg|= TimeStampEdge;
  138. /* Disable the write protection for RTC registers */
  139. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  140. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
  141. hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
  142. /* Configure the Time Stamp TSEDGE and Enable bits */
  143. hrtc->Instance->CR = (uint32_t)tmpreg;
  144. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  145. /* Enable the write protection for RTC registers */
  146. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  147. /* Change RTC state */
  148. hrtc->State = HAL_RTC_STATE_READY;
  149. /* Process Unlocked */
  150. __HAL_UNLOCK(hrtc);
  151. return HAL_OK;
  152. }
  153. /**
  154. * @brief Sets TimeStamp with Interrupt.
  155. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  156. * the configuration information for RTC.
  157. * @note This API must be called before enabling the TimeStamp feature.
  158. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  159. * activated.
  160. * This parameter can be one of the following values:
  161. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  162. * rising edge of the related pin.
  163. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  164. * falling edge of the related pin.
  165. * @param RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
  166. * This parameter can be one of the following values:
  167. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  168. * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin. (not applicable in the case of STM32F446xx, STM32F412xx, STM32F413xx and STM32F423xx devices)
  169. * @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  173. {
  174. uint32_t tmpreg = 0U;
  175. /* Check the parameters */
  176. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  177. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  178. /* Process Locked */
  179. __HAL_LOCK(hrtc);
  180. hrtc->State = HAL_RTC_STATE_BUSY;
  181. /* Get the RTC_CR register and clear the bits to be configured */
  182. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  183. tmpreg |= TimeStampEdge;
  184. /* Disable the write protection for RTC registers */
  185. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  186. /* Configure the Time Stamp TSEDGE and Enable bits */
  187. hrtc->Instance->CR = (uint32_t)tmpreg;
  188. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
  189. hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
  190. /* Clear RTC Timestamp flag */
  191. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  192. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  193. /* Enable IT timestamp */
  194. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
  195. /* RTC timestamp Interrupt Configuration: EXTI configuration */
  196. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  197. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  198. /* Enable the write protection for RTC registers */
  199. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  200. hrtc->State = HAL_RTC_STATE_READY;
  201. /* Process Unlocked */
  202. __HAL_UNLOCK(hrtc);
  203. return HAL_OK;
  204. }
  205. /**
  206. * @brief Deactivates TimeStamp.
  207. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  208. * the configuration information for RTC.
  209. * @retval HAL status
  210. */
  211. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  212. {
  213. uint32_t tmpreg = 0U;
  214. /* Process Locked */
  215. __HAL_LOCK(hrtc);
  216. hrtc->State = HAL_RTC_STATE_BUSY;
  217. /* Disable the write protection for RTC registers */
  218. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  219. /* In case of interrupt mode is used, the interrupt source must disabled */
  220. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  221. /* Get the RTC_CR register and clear the bits to be configured */
  222. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  223. /* Configure the Time Stamp TSEDGE and Enable bits */
  224. hrtc->Instance->CR = (uint32_t)tmpreg;
  225. /* Enable the write protection for RTC registers */
  226. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  227. hrtc->State = HAL_RTC_STATE_READY;
  228. /* Process Unlocked */
  229. __HAL_UNLOCK(hrtc);
  230. return HAL_OK;
  231. }
  232. /**
  233. * @brief Gets the RTC TimeStamp value.
  234. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  235. * the configuration information for RTC.
  236. * @param sTimeStamp Pointer to Time structure
  237. * @param sTimeStampDate Pointer to Date structure
  238. * @param Format specifies the format of the entered parameters.
  239. * This parameter can be one of the following values:
  240. * RTC_FORMAT_BIN: Binary data format
  241. * RTC_FORMAT_BCD: BCD data format
  242. * @retval HAL status
  243. */
  244. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
  245. {
  246. uint32_t tmptime = 0U, tmpdate = 0U;
  247. /* Check the parameters */
  248. assert_param(IS_RTC_FORMAT(Format));
  249. /* Get the TimeStamp time and date registers values */
  250. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  251. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  252. /* Fill the Time structure fields with the read parameters */
  253. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
  254. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
  255. sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
  256. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16U);
  257. sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  258. /* Fill the Date structure fields with the read parameters */
  259. sTimeStampDate->Year = 0U;
  260. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
  261. sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  262. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13U);
  263. /* Check the input parameters format */
  264. if(Format == RTC_FORMAT_BIN)
  265. {
  266. /* Convert the TimeStamp structure parameters to Binary format */
  267. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  268. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  269. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  270. /* Convert the DateTimeStamp structure parameters to Binary format */
  271. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  272. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  273. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  274. }
  275. /* Clear the TIMESTAMP Flag */
  276. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  277. return HAL_OK;
  278. }
  279. /**
  280. * @brief Sets Tamper
  281. * @note By calling this API we disable the tamper interrupt for all tampers.
  282. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  283. * the configuration information for RTC.
  284. * @param sTamper Pointer to Tamper Structure.
  285. * @retval HAL status
  286. */
  287. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  288. {
  289. uint32_t tmpreg = 0U;
  290. /* Check the parameters */
  291. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  292. assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
  293. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  294. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  295. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  296. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  297. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  298. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  299. /* Process Locked */
  300. __HAL_LOCK(hrtc);
  301. hrtc->State = HAL_RTC_STATE_BUSY;
  302. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  303. {
  304. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1U);
  305. }
  306. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
  307. (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  308. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  309. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1U) | (uint32_t)RTC_TAFCR_TAMPTS |\
  310. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  311. (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL | (uint32_t)RTC_TAFCR_TAMPIE);
  312. hrtc->Instance->TAFCR |= tmpreg;
  313. hrtc->State = HAL_RTC_STATE_READY;
  314. /* Process Unlocked */
  315. __HAL_UNLOCK(hrtc);
  316. return HAL_OK;
  317. }
  318. /**
  319. * @brief Sets Tamper with interrupt.
  320. * @note By calling this API we force the tamper interrupt for all tampers.
  321. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  322. * the configuration information for RTC.
  323. * @param sTamper Pointer to RTC Tamper.
  324. * @retval HAL status
  325. */
  326. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  327. {
  328. uint32_t tmpreg = 0U;
  329. /* Check the parameters */
  330. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  331. assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
  332. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  333. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  334. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  335. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  336. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  337. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  338. /* Process Locked */
  339. __HAL_LOCK(hrtc);
  340. hrtc->State = HAL_RTC_STATE_BUSY;
  341. /* Configure the tamper trigger */
  342. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  343. {
  344. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1U);
  345. }
  346. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
  347. (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  348. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  349. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1U) | (uint32_t)RTC_TAFCR_TAMPTS |\
  350. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  351. (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL);
  352. hrtc->Instance->TAFCR |= tmpreg;
  353. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  354. hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
  355. if(sTamper->Tamper == RTC_TAMPER_1)
  356. {
  357. /* Clear RTC Tamper 1 flag */
  358. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  359. }
  360. else
  361. {
  362. /* Clear RTC Tamper 2 flag */
  363. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  364. }
  365. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  366. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  367. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  368. hrtc->State = HAL_RTC_STATE_READY;
  369. /* Process Unlocked */
  370. __HAL_UNLOCK(hrtc);
  371. return HAL_OK;
  372. }
  373. /**
  374. * @brief Deactivates Tamper.
  375. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  376. * the configuration information for RTC.
  377. * @param Tamper Selected tamper pin.
  378. * This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
  379. * @retval HAL status
  380. */
  381. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  382. {
  383. assert_param(IS_RTC_TAMPER(Tamper));
  384. /* Process Locked */
  385. __HAL_LOCK(hrtc);
  386. hrtc->State = HAL_RTC_STATE_BUSY;
  387. /* Disable the selected Tamper pin */
  388. hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
  389. hrtc->State = HAL_RTC_STATE_READY;
  390. /* Process Unlocked */
  391. __HAL_UNLOCK(hrtc);
  392. return HAL_OK;
  393. }
  394. /**
  395. * @brief This function handles TimeStamp interrupt request.
  396. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  397. * the configuration information for RTC.
  398. * @retval None
  399. */
  400. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  401. {
  402. if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
  403. {
  404. /* Get the status of the Interrupt */
  405. if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
  406. {
  407. /* TIMESTAMP callback */
  408. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  409. hrtc->TimeStampEventCallback(hrtc);
  410. #else
  411. HAL_RTCEx_TimeStampEventCallback(hrtc);
  412. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  413. /* Clear the TIMESTAMP interrupt pending bit */
  414. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
  415. }
  416. }
  417. /* Get the status of the Interrupt */
  418. if(__HAL_RTC_TAMPER_GET_IT(hrtc,RTC_IT_TAMP1))
  419. {
  420. /* Get the TAMPER Interrupt enable bit and pending bit */
  421. if(((hrtc->Instance->TAFCR & (RTC_TAFCR_TAMPIE))) != (uint32_t)RESET)
  422. {
  423. /* Tamper callback */
  424. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  425. hrtc->Tamper1EventCallback(hrtc);
  426. #else
  427. HAL_RTCEx_Tamper1EventCallback(hrtc);
  428. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  429. /* Clear the Tamper interrupt pending bit */
  430. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  431. }
  432. }
  433. /* Get the status of the Interrupt */
  434. if(__HAL_RTC_TAMPER_GET_IT(hrtc, RTC_IT_TAMP2))
  435. {
  436. /* Get the TAMPER Interrupt enable bit and pending bit */
  437. if(((hrtc->Instance->TAFCR & RTC_TAFCR_TAMPIE)) != (uint32_t)RESET)
  438. {
  439. /* Tamper callback */
  440. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  441. hrtc->Tamper2EventCallback(hrtc);
  442. #else
  443. HAL_RTCEx_Tamper2EventCallback(hrtc);
  444. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  445. /* Clear the Tamper interrupt pending bit */
  446. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  447. }
  448. }
  449. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  450. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  451. /* Change RTC state */
  452. hrtc->State = HAL_RTC_STATE_READY;
  453. }
  454. /**
  455. * @brief TimeStamp callback.
  456. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  457. * the configuration information for RTC.
  458. * @retval None
  459. */
  460. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  461. {
  462. /* Prevent unused argument(s) compilation warning */
  463. UNUSED(hrtc);
  464. /* NOTE : This function Should not be modified, when the callback is needed,
  465. the HAL_RTC_TimeStampEventCallback could be implemented in the user file
  466. */
  467. }
  468. /**
  469. * @brief Tamper 1 callback.
  470. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  471. * the configuration information for RTC.
  472. * @retval None
  473. */
  474. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  475. {
  476. /* Prevent unused argument(s) compilation warning */
  477. UNUSED(hrtc);
  478. /* NOTE : This function Should not be modified, when the callback is needed,
  479. the HAL_RTC_Tamper1EventCallback could be implemented in the user file
  480. */
  481. }
  482. /**
  483. * @brief Tamper 2 callback.
  484. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  485. * the configuration information for RTC.
  486. * @retval None
  487. */
  488. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  489. {
  490. /* Prevent unused argument(s) compilation warning */
  491. UNUSED(hrtc);
  492. /* NOTE : This function Should not be modified, when the callback is needed,
  493. the HAL_RTC_Tamper2EventCallback could be implemented in the user file
  494. */
  495. }
  496. /**
  497. * @brief This function handles TimeStamp polling request.
  498. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  499. * the configuration information for RTC.
  500. * @param Timeout Timeout duration
  501. * @retval HAL status
  502. */
  503. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  504. {
  505. uint32_t tickstart = 0U;
  506. /* Get tick */
  507. tickstart = HAL_GetTick();
  508. while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
  509. {
  510. if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
  511. {
  512. /* Clear the TIMESTAMP Overrun Flag */
  513. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  514. /* Change TIMESTAMP state */
  515. hrtc->State = HAL_RTC_STATE_ERROR;
  516. return HAL_ERROR;
  517. }
  518. if(Timeout != HAL_MAX_DELAY)
  519. {
  520. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  521. {
  522. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  523. return HAL_TIMEOUT;
  524. }
  525. }
  526. }
  527. /* Change RTC state */
  528. hrtc->State = HAL_RTC_STATE_READY;
  529. return HAL_OK;
  530. }
  531. /**
  532. * @brief This function handles Tamper1 Polling.
  533. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  534. * the configuration information for RTC.
  535. * @param Timeout Timeout duration
  536. * @retval HAL status
  537. */
  538. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  539. {
  540. uint32_t tickstart = 0U;
  541. /* Get tick */
  542. tickstart = HAL_GetTick();
  543. /* Get the status of the Interrupt */
  544. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
  545. {
  546. if(Timeout != HAL_MAX_DELAY)
  547. {
  548. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  549. {
  550. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  551. return HAL_TIMEOUT;
  552. }
  553. }
  554. }
  555. /* Clear the Tamper Flag */
  556. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  557. /* Change RTC state */
  558. hrtc->State = HAL_RTC_STATE_READY;
  559. return HAL_OK;
  560. }
  561. /**
  562. * @brief This function handles Tamper2 Polling.
  563. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  564. * the configuration information for RTC.
  565. * @param Timeout Timeout duration
  566. * @retval HAL status
  567. */
  568. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  569. {
  570. uint32_t tickstart = 0U;
  571. /* Get tick */
  572. tickstart = HAL_GetTick();
  573. /* Get the status of the Interrupt */
  574. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
  575. {
  576. if(Timeout != HAL_MAX_DELAY)
  577. {
  578. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  579. {
  580. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  581. return HAL_TIMEOUT;
  582. }
  583. }
  584. }
  585. /* Clear the Tamper Flag */
  586. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
  587. /* Change RTC state */
  588. hrtc->State = HAL_RTC_STATE_READY;
  589. return HAL_OK;
  590. }
  591. /**
  592. * @}
  593. */
  594. /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wake-up functions
  595. * @brief RTC Wake-up functions
  596. *
  597. @verbatim
  598. ===============================================================================
  599. ##### RTC Wake-up functions #####
  600. ===============================================================================
  601. [..] This section provides functions allowing to configure Wake-up feature
  602. @endverbatim
  603. * @{
  604. */
  605. /**
  606. * @brief Sets wake up timer.
  607. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  608. * the configuration information for RTC.
  609. * @param WakeUpCounter Wake up counter
  610. * @param WakeUpClock Wake up clock
  611. * @retval HAL status
  612. */
  613. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  614. {
  615. uint32_t tickstart = 0U;
  616. /* Check the parameters */
  617. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  618. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  619. /* Process Locked */
  620. __HAL_LOCK(hrtc);
  621. hrtc->State = HAL_RTC_STATE_BUSY;
  622. /* Disable the write protection for RTC registers */
  623. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  624. /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
  625. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  626. {
  627. tickstart = HAL_GetTick();
  628. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  629. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
  630. {
  631. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  632. {
  633. /* Enable the write protection for RTC registers */
  634. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  635. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  636. /* Process Unlocked */
  637. __HAL_UNLOCK(hrtc);
  638. return HAL_TIMEOUT;
  639. }
  640. }
  641. }
  642. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  643. tickstart = HAL_GetTick();
  644. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  645. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  646. {
  647. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  648. {
  649. /* Enable the write protection for RTC registers */
  650. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  651. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  652. /* Process Unlocked */
  653. __HAL_UNLOCK(hrtc);
  654. return HAL_TIMEOUT;
  655. }
  656. }
  657. /* Clear the Wake-up Timer clock source bits in CR register */
  658. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  659. /* Configure the clock source */
  660. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  661. /* Configure the Wake-up Timer counter */
  662. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  663. /* Enable the Wake-up Timer */
  664. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  665. /* Enable the write protection for RTC registers */
  666. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  667. hrtc->State = HAL_RTC_STATE_READY;
  668. /* Process Unlocked */
  669. __HAL_UNLOCK(hrtc);
  670. return HAL_OK;
  671. }
  672. /**
  673. * @brief Sets wake up timer with interrupt
  674. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  675. * the configuration information for RTC.
  676. * @param WakeUpCounter Wake up counter
  677. * @param WakeUpClock Wake up clock
  678. * @retval HAL status
  679. */
  680. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  681. {
  682. __IO uint32_t count;
  683. /* Check the parameters */
  684. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  685. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  686. /* Process Locked */
  687. __HAL_LOCK(hrtc);
  688. hrtc->State = HAL_RTC_STATE_BUSY;
  689. /* Disable the write protection for RTC registers */
  690. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  691. /* Check RTC WUTWF flag is reset only when wake up timer enabled */
  692. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  693. {
  694. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  695. count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  696. do
  697. {
  698. if(count-- == 0U)
  699. {
  700. /* Enable the write protection for RTC registers */
  701. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  702. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  703. /* Process Unlocked */
  704. __HAL_UNLOCK(hrtc);
  705. return HAL_TIMEOUT;
  706. }
  707. }
  708. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET);
  709. }
  710. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  711. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  712. count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  713. do
  714. {
  715. if(count-- == 0U)
  716. {
  717. /* Enable the write protection for RTC registers */
  718. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  719. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  720. /* Process Unlocked */
  721. __HAL_UNLOCK(hrtc);
  722. return HAL_TIMEOUT;
  723. }
  724. }
  725. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET);
  726. /* Configure the Wake-up Timer counter */
  727. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  728. /* Clear the Wake-up Timer clock source bits in CR register */
  729. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  730. /* Configure the clock source */
  731. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  732. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  733. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  734. EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
  735. /* Clear RTC Wake Up timer Flag */
  736. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  737. /* Configure the Interrupt in the RTC_CR register */
  738. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
  739. /* Enable the Wake-up Timer */
  740. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  741. /* Enable the write protection for RTC registers */
  742. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  743. hrtc->State = HAL_RTC_STATE_READY;
  744. /* Process Unlocked */
  745. __HAL_UNLOCK(hrtc);
  746. return HAL_OK;
  747. }
  748. /**
  749. * @brief Deactivates wake up timer counter.
  750. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  751. * the configuration information for RTC.
  752. * @retval HAL status
  753. */
  754. uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  755. {
  756. uint32_t tickstart = 0U;
  757. /* Process Locked */
  758. __HAL_LOCK(hrtc);
  759. hrtc->State = HAL_RTC_STATE_BUSY;
  760. /* Disable the write protection for RTC registers */
  761. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  762. /* Disable the Wake-up Timer */
  763. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  764. /* In case of interrupt mode is used, the interrupt source must disabled */
  765. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
  766. /* Get tick */
  767. tickstart = HAL_GetTick();
  768. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  769. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  770. {
  771. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  772. {
  773. /* Enable the write protection for RTC registers */
  774. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  775. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  776. /* Process Unlocked */
  777. __HAL_UNLOCK(hrtc);
  778. return HAL_TIMEOUT;
  779. }
  780. }
  781. /* Enable the write protection for RTC registers */
  782. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  783. hrtc->State = HAL_RTC_STATE_READY;
  784. /* Process Unlocked */
  785. __HAL_UNLOCK(hrtc);
  786. return HAL_OK;
  787. }
  788. /**
  789. * @brief Gets wake up timer counter.
  790. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  791. * the configuration information for RTC.
  792. * @retval Counter value
  793. */
  794. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  795. {
  796. /* Get the counter value */
  797. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  798. }
  799. /**
  800. * @brief This function handles Wake Up Timer interrupt request.
  801. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  802. * the configuration information for RTC.
  803. * @retval None
  804. */
  805. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  806. {
  807. if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
  808. {
  809. /* Get the status of the Interrupt */
  810. if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
  811. {
  812. /* WAKEUPTIMER callback */
  813. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  814. hrtc->WakeUpTimerEventCallback(hrtc);
  815. #else
  816. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  817. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  818. /* Clear the WAKEUPTIMER interrupt pending bit */
  819. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  820. }
  821. }
  822. /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  823. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  824. /* Change RTC state */
  825. hrtc->State = HAL_RTC_STATE_READY;
  826. }
  827. /**
  828. * @brief Wake Up Timer callback.
  829. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  830. * the configuration information for RTC.
  831. * @retval None
  832. */
  833. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  834. {
  835. /* Prevent unused argument(s) compilation warning */
  836. UNUSED(hrtc);
  837. /* NOTE : This function Should not be modified, when the callback is needed,
  838. the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
  839. */
  840. }
  841. /**
  842. * @brief This function handles Wake Up Timer Polling.
  843. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  844. * the configuration information for RTC.
  845. * @param Timeout Timeout duration
  846. * @retval HAL status
  847. */
  848. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  849. {
  850. uint32_t tickstart = 0U;
  851. /* Get tick */
  852. tickstart = HAL_GetTick();
  853. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
  854. {
  855. if(Timeout != HAL_MAX_DELAY)
  856. {
  857. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  858. {
  859. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  860. return HAL_TIMEOUT;
  861. }
  862. }
  863. }
  864. /* Clear the WAKEUPTIMER Flag */
  865. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  866. /* Change RTC state */
  867. hrtc->State = HAL_RTC_STATE_READY;
  868. return HAL_OK;
  869. }
  870. /**
  871. * @}
  872. */
  873. /** @defgroup RTCEx_Exported_Functions_Group3 Extension Peripheral Control functions
  874. * @brief Extension Peripheral Control functions
  875. *
  876. @verbatim
  877. ===============================================================================
  878. ##### Extension Peripheral Control functions #####
  879. ===============================================================================
  880. [..]
  881. This subsection provides functions allowing to
  882. (+) Write a data in a specified RTC Backup data register
  883. (+) Read a data in a specified RTC Backup data register
  884. (+) Set the Coarse calibration parameters.
  885. (+) Deactivate the Coarse calibration parameters
  886. (+) Set the Smooth calibration parameters.
  887. (+) Configure the Synchronization Shift Control Settings.
  888. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  889. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  890. (+) Enable the RTC reference clock detection.
  891. (+) Disable the RTC reference clock detection.
  892. (+) Enable the Bypass Shadow feature.
  893. (+) Disable the Bypass Shadow feature.
  894. @endverbatim
  895. * @{
  896. */
  897. /**
  898. * @brief Writes a data in a specified RTC Backup data register.
  899. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  900. * the configuration information for RTC.
  901. * @param BackupRegister RTC Backup data Register number.
  902. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  903. * specify the register.
  904. * @param Data Data to be written in the specified RTC Backup data register.
  905. * @retval None
  906. */
  907. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  908. {
  909. uint32_t tmp = 0U;
  910. /* Check the parameters */
  911. assert_param(IS_RTC_BKP(BackupRegister));
  912. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  913. tmp += (BackupRegister * 4U);
  914. /* Write the specified register */
  915. *(__IO uint32_t *)tmp = (uint32_t)Data;
  916. }
  917. /**
  918. * @brief Reads data from the specified RTC Backup data Register.
  919. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  920. * the configuration information for RTC.
  921. * @param BackupRegister RTC Backup data Register number.
  922. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  923. * specify the register.
  924. * @retval Read value
  925. */
  926. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  927. {
  928. uint32_t tmp = 0U;
  929. /* Check the parameters */
  930. assert_param(IS_RTC_BKP(BackupRegister));
  931. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  932. tmp += (BackupRegister * 4U);
  933. /* Read the specified register */
  934. return (*(__IO uint32_t *)tmp);
  935. }
  936. /**
  937. * @brief Sets the Coarse calibration parameters.
  938. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  939. * the configuration information for RTC.
  940. * @param CalibSign Specifies the sign of the coarse calibration value.
  941. * This parameter can be one of the following values :
  942. * @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
  943. * @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
  944. * @param Value value of coarse calibration expressed in ppm (coded on 5 bits).
  945. *
  946. * @note This Calibration value should be between 0 and 63 when using negative
  947. * sign with a 2-ppm step.
  948. *
  949. * @note This Calibration value should be between 0 and 126 when using positive
  950. * sign with a 4-ppm step.
  951. * @retval HAL status
  952. */
  953. HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
  954. {
  955. /* Check the parameters */
  956. assert_param(IS_RTC_CALIB_SIGN(CalibSign));
  957. assert_param(IS_RTC_CALIB_VALUE(Value));
  958. /* Process Locked */
  959. __HAL_LOCK(hrtc);
  960. hrtc->State = HAL_RTC_STATE_BUSY;
  961. /* Disable the write protection for RTC registers */
  962. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  963. /* Set Initialization mode */
  964. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  965. {
  966. /* Enable the write protection for RTC registers */
  967. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  968. /* Set RTC state*/
  969. hrtc->State = HAL_RTC_STATE_ERROR;
  970. /* Process Unlocked */
  971. __HAL_UNLOCK(hrtc);
  972. return HAL_ERROR;
  973. }
  974. else
  975. {
  976. /* Enable the Coarse Calibration */
  977. __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
  978. /* Set the coarse calibration value */
  979. hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
  980. /* Exit Initialization mode */
  981. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  982. }
  983. /* Enable the write protection for RTC registers */
  984. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  985. /* Change state */
  986. hrtc->State = HAL_RTC_STATE_READY;
  987. /* Process Unlocked */
  988. __HAL_UNLOCK(hrtc);
  989. return HAL_OK;
  990. }
  991. /**
  992. * @brief Deactivates the Coarse calibration parameters.
  993. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  994. * the configuration information for RTC.
  995. * @retval HAL status
  996. */
  997. HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
  998. {
  999. /* Process Locked */
  1000. __HAL_LOCK(hrtc);
  1001. hrtc->State = HAL_RTC_STATE_BUSY;
  1002. /* Disable the write protection for RTC registers */
  1003. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1004. /* Set Initialization mode */
  1005. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1006. {
  1007. /* Enable the write protection for RTC registers */
  1008. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1009. /* Set RTC state*/
  1010. hrtc->State = HAL_RTC_STATE_ERROR;
  1011. /* Process Unlocked */
  1012. __HAL_UNLOCK(hrtc);
  1013. return HAL_ERROR;
  1014. }
  1015. else
  1016. {
  1017. /* Enable the Coarse Calibration */
  1018. __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
  1019. /* Exit Initialization mode */
  1020. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1021. }
  1022. /* Enable the write protection for RTC registers */
  1023. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1024. /* Change state */
  1025. hrtc->State = HAL_RTC_STATE_READY;
  1026. /* Process Unlocked */
  1027. __HAL_UNLOCK(hrtc);
  1028. return HAL_OK;
  1029. }
  1030. /**
  1031. * @brief Sets the Smooth calibration parameters.
  1032. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1033. * the configuration information for RTC.
  1034. * @param SmoothCalibPeriod Select the Smooth Calibration Period.
  1035. * This parameter can be can be one of the following values :
  1036. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  1037. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  1038. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  1039. * @param SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  1040. * This parameter can be one of the following values:
  1041. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  1042. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  1043. * @param SmouthCalibMinusPulsesValue Select the value of CALM[80] bits.
  1044. * This parameter can be one any value from 0 to 0x000001FF.
  1045. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  1046. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  1047. * SmouthCalibMinusPulsesValue must be equal to 0.
  1048. * @retval HAL status
  1049. */
  1050. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
  1051. {
  1052. uint32_t tickstart = 0U;
  1053. /* Check the parameters */
  1054. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  1055. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  1056. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
  1057. /* Process Locked */
  1058. __HAL_LOCK(hrtc);
  1059. hrtc->State = HAL_RTC_STATE_BUSY;
  1060. /* Disable the write protection for RTC registers */
  1061. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1062. /* check if a calibration is pending*/
  1063. if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1064. {
  1065. /* Get tick */
  1066. tickstart = HAL_GetTick();
  1067. /* check if a calibration is pending*/
  1068. while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1069. {
  1070. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1071. {
  1072. /* Enable the write protection for RTC registers */
  1073. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1074. /* Change RTC state */
  1075. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1076. /* Process Unlocked */
  1077. __HAL_UNLOCK(hrtc);
  1078. return HAL_TIMEOUT;
  1079. }
  1080. }
  1081. }
  1082. /* Configure the Smooth calibration settings */
  1083. hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
  1084. /* Enable the write protection for RTC registers */
  1085. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1086. /* Change RTC state */
  1087. hrtc->State = HAL_RTC_STATE_READY;
  1088. /* Process Unlocked */
  1089. __HAL_UNLOCK(hrtc);
  1090. return HAL_OK;
  1091. }
  1092. /**
  1093. * @brief Configures the Synchronization Shift Control Settings.
  1094. * @note When REFCKON is set, firmware must not write to Shift control register.
  1095. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1096. * the configuration information for RTC.
  1097. * @param ShiftAdd1S Select to add or not 1 second to the time calendar.
  1098. * This parameter can be one of the following values :
  1099. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1100. * @arg RTC_SHIFTADD1S_RESET: No effect.
  1101. * @param ShiftSubFS Select the number of Second Fractions to substitute.
  1102. * This parameter can be one any value from 0 to 0x7FFF.
  1103. * @retval HAL status
  1104. */
  1105. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1106. {
  1107. uint32_t tickstart = 0U;
  1108. /* Check the parameters */
  1109. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1110. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1111. /* Process Locked */
  1112. __HAL_LOCK(hrtc);
  1113. hrtc->State = HAL_RTC_STATE_BUSY;
  1114. /* Disable the write protection for RTC registers */
  1115. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1116. /* Get tick */
  1117. tickstart = HAL_GetTick();
  1118. /* Wait until the shift is completed*/
  1119. while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
  1120. {
  1121. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1122. {
  1123. /* Enable the write protection for RTC registers */
  1124. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1125. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1126. /* Process Unlocked */
  1127. __HAL_UNLOCK(hrtc);
  1128. return HAL_TIMEOUT;
  1129. }
  1130. }
  1131. /* Check if the reference clock detection is disabled */
  1132. if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
  1133. {
  1134. /* Configure the Shift settings */
  1135. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1136. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1137. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  1138. {
  1139. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1140. {
  1141. /* Enable the write protection for RTC registers */
  1142. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1143. hrtc->State = HAL_RTC_STATE_ERROR;
  1144. /* Process Unlocked */
  1145. __HAL_UNLOCK(hrtc);
  1146. return HAL_ERROR;
  1147. }
  1148. }
  1149. }
  1150. else
  1151. {
  1152. /* Enable the write protection for RTC registers */
  1153. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1154. /* Change RTC state */
  1155. hrtc->State = HAL_RTC_STATE_ERROR;
  1156. /* Process Unlocked */
  1157. __HAL_UNLOCK(hrtc);
  1158. return HAL_ERROR;
  1159. }
  1160. /* Enable the write protection for RTC registers */
  1161. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1162. /* Change RTC state */
  1163. hrtc->State = HAL_RTC_STATE_READY;
  1164. /* Process Unlocked */
  1165. __HAL_UNLOCK(hrtc);
  1166. return HAL_OK;
  1167. }
  1168. /**
  1169. * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1170. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1171. * the configuration information for RTC.
  1172. * @param CalibOutput Select the Calibration output Selection .
  1173. * This parameter can be one of the following values:
  1174. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1175. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1176. * @retval HAL status
  1177. */
  1178. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
  1179. {
  1180. /* Check the parameters */
  1181. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1182. /* Process Locked */
  1183. __HAL_LOCK(hrtc);
  1184. hrtc->State = HAL_RTC_STATE_BUSY;
  1185. /* Disable the write protection for RTC registers */
  1186. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1187. /* Clear flags before config */
  1188. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1189. /* Configure the RTC_CR register */
  1190. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1191. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1192. /* Enable the write protection for RTC registers */
  1193. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1194. /* Change RTC state */
  1195. hrtc->State = HAL_RTC_STATE_READY;
  1196. /* Process Unlocked */
  1197. __HAL_UNLOCK(hrtc);
  1198. return HAL_OK;
  1199. }
  1200. /**
  1201. * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1202. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1203. * the configuration information for RTC.
  1204. * @retval HAL status
  1205. */
  1206. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
  1207. {
  1208. /* Process Locked */
  1209. __HAL_LOCK(hrtc);
  1210. hrtc->State = HAL_RTC_STATE_BUSY;
  1211. /* Disable the write protection for RTC registers */
  1212. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1213. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1214. /* Enable the write protection for RTC registers */
  1215. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1216. /* Change RTC state */
  1217. hrtc->State = HAL_RTC_STATE_READY;
  1218. /* Process Unlocked */
  1219. __HAL_UNLOCK(hrtc);
  1220. return HAL_OK;
  1221. }
  1222. /**
  1223. * @brief Enables the RTC reference clock detection.
  1224. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1225. * the configuration information for RTC.
  1226. * @retval HAL status
  1227. */
  1228. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
  1229. {
  1230. /* Process Locked */
  1231. __HAL_LOCK(hrtc);
  1232. hrtc->State = HAL_RTC_STATE_BUSY;
  1233. /* Disable the write protection for RTC registers */
  1234. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1235. /* Set Initialization mode */
  1236. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1237. {
  1238. /* Enable the write protection for RTC registers */
  1239. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1240. /* Set RTC state*/
  1241. hrtc->State = HAL_RTC_STATE_ERROR;
  1242. /* Process Unlocked */
  1243. __HAL_UNLOCK(hrtc);
  1244. return HAL_ERROR;
  1245. }
  1246. else
  1247. {
  1248. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1249. /* Exit Initialization mode */
  1250. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1251. }
  1252. /* Enable the write protection for RTC registers */
  1253. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1254. /* Change RTC state */
  1255. hrtc->State = HAL_RTC_STATE_READY;
  1256. /* Process Unlocked */
  1257. __HAL_UNLOCK(hrtc);
  1258. return HAL_OK;
  1259. }
  1260. /**
  1261. * @brief Disable the RTC reference clock detection.
  1262. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1263. * the configuration information for RTC.
  1264. * @retval HAL status
  1265. */
  1266. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
  1267. {
  1268. /* Process Locked */
  1269. __HAL_LOCK(hrtc);
  1270. hrtc->State = HAL_RTC_STATE_BUSY;
  1271. /* Disable the write protection for RTC registers */
  1272. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1273. /* Set Initialization mode */
  1274. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1275. {
  1276. /* Enable the write protection for RTC registers */
  1277. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1278. /* Set RTC state*/
  1279. hrtc->State = HAL_RTC_STATE_ERROR;
  1280. /* Process Unlocked */
  1281. __HAL_UNLOCK(hrtc);
  1282. return HAL_ERROR;
  1283. }
  1284. else
  1285. {
  1286. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1287. /* Exit Initialization mode */
  1288. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1289. }
  1290. /* Enable the write protection for RTC registers */
  1291. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1292. /* Change RTC state */
  1293. hrtc->State = HAL_RTC_STATE_READY;
  1294. /* Process Unlocked */
  1295. __HAL_UNLOCK(hrtc);
  1296. return HAL_OK;
  1297. }
  1298. /**
  1299. * @brief Enables the Bypass Shadow feature.
  1300. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1301. * the configuration information for RTC.
  1302. * @note When the Bypass Shadow is enabled the calendar value are taken
  1303. * directly from the Calendar counter.
  1304. * @retval HAL status
  1305. */
  1306. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
  1307. {
  1308. /* Process Locked */
  1309. __HAL_LOCK(hrtc);
  1310. hrtc->State = HAL_RTC_STATE_BUSY;
  1311. /* Disable the write protection for RTC registers */
  1312. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1313. /* Set the BYPSHAD bit */
  1314. hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  1315. /* Enable the write protection for RTC registers */
  1316. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1317. /* Change RTC state */
  1318. hrtc->State = HAL_RTC_STATE_READY;
  1319. /* Process Unlocked */
  1320. __HAL_UNLOCK(hrtc);
  1321. return HAL_OK;
  1322. }
  1323. /**
  1324. * @brief Disables the Bypass Shadow feature.
  1325. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1326. * the configuration information for RTC.
  1327. * @note When the Bypass Shadow is enabled the calendar value are taken
  1328. * directly from the Calendar counter.
  1329. * @retval HAL status
  1330. */
  1331. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
  1332. {
  1333. /* Process Locked */
  1334. __HAL_LOCK(hrtc);
  1335. hrtc->State = HAL_RTC_STATE_BUSY;
  1336. /* Disable the write protection for RTC registers */
  1337. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1338. /* Reset the BYPSHAD bit */
  1339. hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  1340. /* Enable the write protection for RTC registers */
  1341. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1342. /* Change RTC state */
  1343. hrtc->State = HAL_RTC_STATE_READY;
  1344. /* Process Unlocked */
  1345. __HAL_UNLOCK(hrtc);
  1346. return HAL_OK;
  1347. }
  1348. /**
  1349. * @}
  1350. */
  1351. /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
  1352. * @brief Extended features functions
  1353. *
  1354. @verbatim
  1355. ===============================================================================
  1356. ##### Extended features functions #####
  1357. ===============================================================================
  1358. [..] This section provides functions allowing to:
  1359. (+) RTC Alarm B callback
  1360. (+) RTC Poll for Alarm B request
  1361. @endverbatim
  1362. * @{
  1363. */
  1364. /**
  1365. * @brief Alarm B callback.
  1366. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1367. * the configuration information for RTC.
  1368. * @retval None
  1369. */
  1370. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  1371. {
  1372. /* Prevent unused argument(s) compilation warning */
  1373. UNUSED(hrtc);
  1374. /* NOTE : This function Should not be modified, when the callback is needed,
  1375. the HAL_RTC_AlarmBEventCallback could be implemented in the user file
  1376. */
  1377. }
  1378. /**
  1379. * @brief This function handles AlarmB Polling request.
  1380. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1381. * the configuration information for RTC.
  1382. * @param Timeout Timeout duration
  1383. * @retval HAL status
  1384. */
  1385. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1386. {
  1387. uint32_t tickstart = 0U;
  1388. /* Get tick */
  1389. tickstart = HAL_GetTick();
  1390. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
  1391. {
  1392. if(Timeout != HAL_MAX_DELAY)
  1393. {
  1394. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1395. {
  1396. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1397. return HAL_TIMEOUT;
  1398. }
  1399. }
  1400. }
  1401. /* Clear the Alarm Flag */
  1402. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1403. /* Change RTC state */
  1404. hrtc->State = HAL_RTC_STATE_READY;
  1405. return HAL_OK;
  1406. }
  1407. /**
  1408. * @}
  1409. */
  1410. /**
  1411. * @}
  1412. */
  1413. #endif /* HAL_RTC_MODULE_ENABLED */
  1414. /**
  1415. * @}
  1416. */
  1417. /**
  1418. * @}
  1419. */
  1420. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/