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