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.
 
 
 

1834 lines
55 KiB

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