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.
 
 
 
 
 

1883 lines
59 KiB

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