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