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     (+) The RTC Smooth Digital Calibration value and the corresponding calibration
88         cycle period (32s, 16s, or 8s) can be calibrated using the
89         HAL_RTCEx_SetSmoothCalib() 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   /* RTC Timestamp Interrupt Configuration: EXTI configuration */
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 of 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   /* RTC Tamper Interrupt Configuration: EXTI configuration */
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   * @note   RTC_TAMPER_3 is not applicable to all devices.
539   * @retval HAL status
540   */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)541 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
542 {
543   assert_param(IS_RTC_TAMPER(Tamper));
544 
545   /* Process Locked */
546   __HAL_LOCK(hrtc);
547 
548   hrtc->State = HAL_RTC_STATE_BUSY;
549 
550   /* Disable the selected Tamper pin */
551   hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
552 
553   hrtc->State = HAL_RTC_STATE_READY;
554 
555   /* Process Unlocked */
556   __HAL_UNLOCK(hrtc);
557 
558   return HAL_OK;
559 }
560 
561 /**
562   * @brief  Handles Timestamp and Tamper interrupt request.
563   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
564   *                the configuration information for RTC.
565   * @retval None
566   */
HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef * hrtc)567 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
568 {
569   /* Clear the EXTI's Flag for RTC Timestamp and Tamper */
570   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
571 
572   /* Get the Timestamp interrupt source enable status */
573   if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
574   {
575     /* Get the pending status of the Timestamp Interrupt */
576     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
577     {
578       /* Timestamp callback */
579 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
580       hrtc->TimeStampEventCallback(hrtc);
581 #else
582       HAL_RTCEx_TimeStampEventCallback(hrtc);
583 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
584 
585       /* Clear the Timestamp interrupt pending bit after returning from callback
586          as RTC_TSTR and RTC_TSDR registers are cleared when TSF bit is reset */
587       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
588     }
589   }
590 
591   /* Get the Tamper 1 interrupt source enable status */
592   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
593   {
594     /* Get the pending status of the Tamper 1 Interrupt */
595     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
596     {
597       /* Clear the Tamper interrupt pending bit */
598       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
599 
600       /* Tamper callback */
601 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
602       hrtc->Tamper1EventCallback(hrtc);
603 #else
604       HAL_RTCEx_Tamper1EventCallback(hrtc);
605 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
606     }
607   }
608 
609   /* Get the Tamper 2 interrupt source enable status */
610   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
611   {
612     /* Get the pending status of the Tamper 2 Interrupt */
613     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
614     {
615       /* Clear the Tamper interrupt pending bit */
616       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
617 
618       /* Tamper callback */
619 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
620       hrtc->Tamper2EventCallback(hrtc);
621 #else
622       HAL_RTCEx_Tamper2EventCallback(hrtc);
623 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
624     }
625   }
626 
627 #if defined(RTC_TAMPER3_SUPPORT)
628   /* Get the Tamper 3 interrupt source enable status */
629   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
630   {
631     /* Get the pending status of the Tamper 3 Interrupt */
632     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
633     {
634       /* Clear the Tamper interrupt pending bit */
635       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
636 
637       /* Tamper callback */
638 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
639       hrtc->Tamper3EventCallback(hrtc);
640 #else
641       HAL_RTCEx_Tamper3EventCallback(hrtc);
642 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
643     }
644   }
645 #endif /* RTC_TAMPER3_SUPPORT */
646 
647   /* Change RTC state */
648   hrtc->State = HAL_RTC_STATE_READY;
649 }
650 
651 /**
652   * @brief  Timestamp callback.
653   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
654   *                the configuration information for RTC.
655   * @retval None
656   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)657 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
658 {
659   /* Prevent unused argument(s) compilation warning */
660   UNUSED(hrtc);
661 
662   /* NOTE: This function should not be modified, when the callback is needed,
663            the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
664   */
665 }
666 
667 /**
668   * @brief  Tamper 1 callback.
669   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
670   *                the configuration information for RTC.
671   * @retval None
672   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)673 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
674 {
675   /* Prevent unused argument(s) compilation warning */
676   UNUSED(hrtc);
677 
678   /* NOTE: This function should not be modified, when the callback is needed,
679            the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
680    */
681 }
682 
683 /**
684   * @brief  Tamper 2 callback.
685   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
686   *                the configuration information for RTC.
687   * @retval None
688   */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)689 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
690 {
691   /* Prevent unused argument(s) compilation warning */
692   UNUSED(hrtc);
693 
694   /* NOTE: This function should not be modified, when the callback is needed,
695            the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
696    */
697 }
698 
699 #if defined(RTC_TAMPER3_SUPPORT)
700 /**
701   * @brief  Tamper 3 callback.
702   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
703   *                the configuration information for RTC.
704   * @retval None
705   */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)706 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
707 {
708   /* Prevent unused argument(s) compilation warning */
709   UNUSED(hrtc);
710 
711   /* NOTE: This function should not be modified, when the callback is needed,
712            the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
713    */
714 }
715 #endif /* RTC_TAMPER3_SUPPORT */
716 
717 /**
718   * @brief  Handles Timestamp polling request.
719   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
720   *                the configuration information for RTC.
721   * @param  Timeout Timeout duration
722   * @retval HAL status
723   */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)724 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
725 {
726   uint32_t tickstart = 0U;
727 
728   /* Get tick */
729   tickstart = HAL_GetTick();
730 
731   while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
732   {
733     if (Timeout != HAL_MAX_DELAY)
734     {
735       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
736       {
737         hrtc->State = HAL_RTC_STATE_TIMEOUT;
738         return HAL_TIMEOUT;
739       }
740     }
741 
742     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
743     {
744       /* Clear the Timestamp Overrun Flag */
745       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
746 
747       /* Change Timestamp state */
748       hrtc->State = HAL_RTC_STATE_ERROR;
749 
750       return HAL_ERROR;
751     }
752   }
753 
754   /* Change RTC state */
755   hrtc->State = HAL_RTC_STATE_READY;
756 
757   return HAL_OK;
758 }
759 
760 /**
761   * @brief  Handles Tamper 1 Polling.
762   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
763   *                the configuration information for RTC.
764   * @param  Timeout Timeout duration
765   * @retval HAL status
766   */
HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)767 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
768 {
769   uint32_t tickstart = 0U;
770 
771   /* Get tick */
772   tickstart = HAL_GetTick();
773 
774   /* Get the status of the Interrupt */
775   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
776   {
777     if (Timeout != HAL_MAX_DELAY)
778     {
779       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
780       {
781         hrtc->State = HAL_RTC_STATE_TIMEOUT;
782         return HAL_TIMEOUT;
783       }
784     }
785   }
786 
787   /* Clear the Tamper Flag */
788   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
789 
790   /* Change RTC state */
791   hrtc->State = HAL_RTC_STATE_READY;
792 
793   return HAL_OK;
794 }
795 
796 /**
797   * @brief  Handles Tamper 2 Polling.
798   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
799   *                the configuration information for RTC.
800   * @param  Timeout Timeout duration
801   * @retval HAL status
802   */
HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)803 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
804 {
805   uint32_t tickstart = 0U;
806 
807   /* Get tick */
808   tickstart = HAL_GetTick();
809 
810   /* Get the status of the Interrupt */
811   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
812   {
813     if (Timeout != HAL_MAX_DELAY)
814     {
815       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
816       {
817         hrtc->State = HAL_RTC_STATE_TIMEOUT;
818         return HAL_TIMEOUT;
819       }
820     }
821   }
822 
823   /* Clear the Tamper Flag */
824   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
825 
826   /* Change RTC state */
827   hrtc->State = HAL_RTC_STATE_READY;
828 
829   return HAL_OK;
830 }
831 
832 #if defined(RTC_TAMPER3_SUPPORT)
833 /**
834   * @brief  Handles Tamper 3 Polling.
835   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
836   *                the configuration information for RTC.
837   * @param  Timeout Timeout duration
838   * @retval HAL status
839   */
HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef * hrtc,uint32_t Timeout)840 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
841 {
842   uint32_t tickstart = HAL_GetTick();
843 
844   /* Get the status of the Interrupt */
845   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
846   {
847     if (Timeout != HAL_MAX_DELAY)
848     {
849       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
850       {
851         hrtc->State = HAL_RTC_STATE_TIMEOUT;
852         return HAL_TIMEOUT;
853       }
854     }
855   }
856 
857   /* Clear the Tamper Flag */
858   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
859 
860   /* Change RTC state */
861   hrtc->State = HAL_RTC_STATE_READY;
862 
863   return HAL_OK;
864 }
865 #endif /* RTC_TAMPER3_SUPPORT */
866 
867 /**
868   * @}
869   */
870 
871 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wakeup functions
872   * @brief    RTC Wakeup functions
873   *
874 @verbatim
875  ===============================================================================
876                         ##### RTC Wakeup functions #####
877  ===============================================================================
878 
879  [..] This section provides functions allowing to configure Wakeup feature
880 
881 @endverbatim
882   * @{
883   */
884 
885 /**
886   * @brief  Sets wakeup timer.
887   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
888   *                the configuration information for RTC.
889   * @param  WakeUpCounter Wakeup counter
890   * @param  WakeUpClock Wakeup clock
891   * @retval HAL status
892   */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)893 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
894 {
895   uint32_t tickstart = 0U;
896 
897   /* Check the parameters */
898   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
899   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
900 
901   /* Process Locked */
902   __HAL_LOCK(hrtc);
903 
904   hrtc->State = HAL_RTC_STATE_BUSY;
905 
906   /* Disable the write protection for RTC registers */
907   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
908 
909   /* Check RTC WUTWF flag is reset only when wakeup timer enabled*/
910   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
911   {
912     tickstart = HAL_GetTick();
913 
914     /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
915     while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
916     {
917       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
918       {
919         /* Enable the write protection for RTC registers */
920         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
921 
922         hrtc->State = HAL_RTC_STATE_TIMEOUT;
923 
924         /* Process Unlocked */
925         __HAL_UNLOCK(hrtc);
926 
927         return HAL_TIMEOUT;
928       }
929     }
930   }
931 
932   /* Disable the Wakeup timer */
933   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
934 
935   /* Clear the Wakeup flag */
936   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
937 
938   /* Get tick */
939   tickstart = HAL_GetTick();
940 
941   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
942   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
943   {
944     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
945     {
946       /* Enable the write protection for RTC registers */
947       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
948 
949       hrtc->State = HAL_RTC_STATE_TIMEOUT;
950 
951       /* Process Unlocked */
952       __HAL_UNLOCK(hrtc);
953 
954       return HAL_TIMEOUT;
955     }
956   }
957 
958   /* Clear the Wakeup Timer clock source bits in CR register */
959   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
960 
961   /* Configure the clock source */
962   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
963 
964   /* Configure the Wakeup Timer counter */
965   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
966 
967   /* Enable the Wakeup Timer */
968   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
969 
970   /* Enable the write protection for RTC registers */
971   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
972 
973   hrtc->State = HAL_RTC_STATE_READY;
974 
975   /* Process Unlocked */
976   __HAL_UNLOCK(hrtc);
977 
978   return HAL_OK;
979 }
980 
981 /**
982   * @brief  Sets wakeup timer with interrupt.
983   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
984   *                the configuration information for RTC.
985   * @param  WakeUpCounter Wakeup counter
986   * @param  WakeUpClock Wakeup clock
987   * @retval HAL status
988   */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)989 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
990 {
991   __IO uint32_t count  = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
992 
993   /* Check the parameters */
994   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
995   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
996 
997   /* Process Locked */
998   __HAL_LOCK(hrtc);
999 
1000   hrtc->State = HAL_RTC_STATE_BUSY;
1001 
1002   /* Disable the write protection for RTC registers */
1003   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1004 
1005   /* Check RTC WUTWF flag is reset only when wakeup timer enabled */
1006   if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
1007   {
1008     /* Wait till RTC WUTWF flag is reset 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 
1027   /* Disable the Wakeup timer */
1028   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1029 
1030   /* Clear the Wakeup flag */
1031   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1032 
1033   /* Reload the counter */
1034   count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
1035 
1036   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1037   do
1038   {
1039     count = count - 1U;
1040     if (count == 0U)
1041     {
1042       /* Enable the write protection for RTC registers */
1043       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1044 
1045       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1046 
1047       /* Process Unlocked */
1048       __HAL_UNLOCK(hrtc);
1049 
1050       return HAL_TIMEOUT;
1051     }
1052   } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
1053 
1054   /* Clear the Wakeup Timer clock source bits in CR register */
1055   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1056 
1057   /* Configure the clock source */
1058   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
1059 
1060   /* Configure the Wakeup Timer counter */
1061   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1062 
1063   /* RTC wakeup timer Interrupt Configuration: EXTI configuration */
1064   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
1065   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1066 
1067   /* Configure the interrupt in the RTC_CR register */
1068   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
1069 
1070   /* Enable the Wakeup Timer */
1071   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1072 
1073   /* Enable the write protection for RTC registers */
1074   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1075 
1076   hrtc->State = HAL_RTC_STATE_READY;
1077 
1078   /* Process Unlocked */
1079   __HAL_UNLOCK(hrtc);
1080 
1081   return HAL_OK;
1082 }
1083 
1084 /**
1085   * @brief  Deactivates wakeup timer counter.
1086   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1087   *                the configuration information for RTC.
1088   * @retval HAL status
1089   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)1090 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1091 {
1092   uint32_t tickstart = 0U;
1093 
1094   /* Process Locked */
1095   __HAL_LOCK(hrtc);
1096 
1097   hrtc->State = HAL_RTC_STATE_BUSY;
1098 
1099   /* Disable the write protection for RTC registers */
1100   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1101 
1102   /* Disable the Wakeup Timer */
1103   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1104 
1105   /* In case of interrupt mode is used, the interrupt source must disabled */
1106   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
1107 
1108   /* Get tick */
1109   tickstart = HAL_GetTick();
1110 
1111   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
1112   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1113   {
1114     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1115     {
1116       /* Enable the write protection for RTC registers */
1117       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1118 
1119       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1120 
1121       /* Process Unlocked */
1122       __HAL_UNLOCK(hrtc);
1123 
1124       return HAL_TIMEOUT;
1125     }
1126   }
1127 
1128   /* Enable the write protection for RTC registers */
1129   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1130 
1131   hrtc->State = HAL_RTC_STATE_READY;
1132 
1133   /* Process Unlocked */
1134   __HAL_UNLOCK(hrtc);
1135 
1136   return HAL_OK;
1137 }
1138 
1139 /**
1140   * @brief  Gets wakeup timer counter.
1141   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1142   *                the configuration information for RTC.
1143   * @retval Counter value
1144   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)1145 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1146 {
1147   /* Get the counter value */
1148   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1149 }
1150 
1151 /**
1152   * @brief  Handles Wakeup Timer interrupt request.
1153   * @note   Unlike alarm interrupt line (shared by Alarms A and B) or tamper
1154   *         interrupt line (shared by timestamp and tampers) wakeup timer
1155   *         interrupt line is exclusive to the wakeup timer.
1156   *         There is no need in this case to check on the interrupt enable
1157   *         status via __HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE().
1158   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1159   *                the configuration information for RTC.
1160   * @retval None
1161   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)1162 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1163 {
1164   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1165   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1166 
1167   /* Get the pending status of the Wakeup timer Interrupt */
1168   if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
1169   {
1170     /* Clear the Wakeup timer interrupt pending bit */
1171     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1172 
1173     /* Wakeup timer callback */
1174 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1175     hrtc->WakeUpTimerEventCallback(hrtc);
1176 #else
1177     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1178 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1179   }
1180 
1181   /* Change RTC state */
1182   hrtc->State = HAL_RTC_STATE_READY;
1183 }
1184 
1185 /**
1186   * @brief  Wakeup Timer callback.
1187   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1188   *                the configuration information for RTC.
1189   * @retval None
1190   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)1191 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1192 {
1193   /* Prevent unused argument(s) compilation warning */
1194   UNUSED(hrtc);
1195 
1196   /* NOTE: This function should not be modified, when the callback is needed,
1197            the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1198    */
1199 }
1200 
1201 /**
1202   * @brief  Handles Wakeup Timer Polling.
1203   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1204   *                the configuration information for RTC.
1205   * @param  Timeout Timeout duration
1206   * @retval HAL status
1207   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1208 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1209 {
1210   uint32_t tickstart = 0U;
1211 
1212   /* Get tick */
1213   tickstart = HAL_GetTick();
1214 
1215   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
1216   {
1217     if (Timeout != HAL_MAX_DELAY)
1218     {
1219       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1220       {
1221         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1222         return HAL_TIMEOUT;
1223       }
1224     }
1225   }
1226 
1227   /* Clear the Wakeup timer Flag */
1228   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1229 
1230   /* Change RTC state */
1231   hrtc->State = HAL_RTC_STATE_READY;
1232 
1233   return HAL_OK;
1234 }
1235 
1236 /**
1237   * @}
1238   */
1239 
1240 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
1241   * @brief    Extended Peripheral Control functions
1242   *
1243 @verbatim
1244  ===============================================================================
1245               ##### Extended Peripheral Control functions #####
1246  ===============================================================================
1247     [..]
1248     This subsection provides functions allowing to
1249       (+) Write a data in a specified RTC Backup data register
1250       (+) Read a data in a specified RTC Backup data register
1251       (+) Set the Smooth calibration parameters.
1252       (+) Configure the Synchronization Shift Control Settings.
1253       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1254       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1255       (+) Enable the RTC reference clock detection.
1256       (+) Disable the RTC reference clock detection.
1257       (+) Enable the Bypass Shadow feature.
1258       (+) Disable the Bypass Shadow feature.
1259 
1260 @endverbatim
1261   * @{
1262   */
1263 
1264 /**
1265   * @brief  Writes a data in a specified RTC Backup data register.
1266   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1267   *                the configuration information for RTC.
1268   * @param  BackupRegister RTC Backup data Register number.
1269   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 15)
1270   *                                 to specify the register.
1271   * @note   The range of values possible for parameter BackupRegister depends on
1272   *         the total number of Backup data Registers. This total number is not
1273   *         the same on all devices.
1274   * @param  Data Data to be written in the specified RTC Backup data register.
1275   * @retval None
1276   */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)1277 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
1278 {
1279   uint32_t tmp = 0U;
1280 
1281   /* Check the parameters */
1282   assert_param(IS_RTC_BKP(BackupRegister));
1283 
1284   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1285   tmp += (BackupRegister * 4U);
1286 
1287   /* Write the specified register */
1288   *(__IO uint32_t *)tmp = (uint32_t)Data;
1289 }
1290 
1291 /**
1292   * @brief  Reads data from the specified RTC Backup data Register.
1293   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1294   *                the configuration information for RTC.
1295   * @param  BackupRegister RTC Backup data Register number.
1296   *          This parameter can be: RTC_BKP_DRx (where x can be from 0 to 15)
1297   *                                 to specify the register.
1298   * @note   The range of values possible for parameter BackupRegister depends on
1299   *         the total number of Backup data Registers. This total number is not
1300   *         the same on all devices.
1301   * @retval Read value
1302   */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)1303 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
1304 {
1305   uint32_t tmp = 0U;
1306 
1307   /* Check the parameters */
1308   assert_param(IS_RTC_BKP(BackupRegister));
1309 
1310   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
1311   tmp += (BackupRegister * 4U);
1312 
1313   /* Read the specified register */
1314   return (*(__IO uint32_t *)tmp);
1315 }
1316 
1317 /**
1318   * @brief  Sets the Smooth calibration parameters.
1319   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1320   *                the configuration information for RTC.
1321   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
1322   *          This parameter can be can be one of the following values:
1323   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
1324   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
1325   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
1326   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
1327   *          This parameter can be one of the following values:
1328   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
1329   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1330   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
1331   *          This parameter can be one any value from 0 to 0x000001FF.
1332   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1333   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1334   *         SmoothCalibMinusPulsesValue must be equal to 0.
1335   * @retval HAL status
1336   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)1337 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
1338 {
1339   uint32_t tickstart = 0U;
1340 
1341   /* Check the parameters */
1342   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1343   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1344   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
1345 
1346   /* Process Locked */
1347   __HAL_LOCK(hrtc);
1348 
1349   hrtc->State = HAL_RTC_STATE_BUSY;
1350 
1351   /* Disable the write protection for RTC registers */
1352   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1353 
1354   /* check if a calibration is pending*/
1355   if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1356   {
1357     /* Get tick */
1358     tickstart = HAL_GetTick();
1359 
1360     /* check if a calibration is pending*/
1361     while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1362     {
1363       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1364       {
1365         /* Enable the write protection for RTC registers */
1366         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1367 
1368         /* Change RTC state */
1369         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1370 
1371         /* Process Unlocked */
1372         __HAL_UNLOCK(hrtc);
1373 
1374         return HAL_TIMEOUT;
1375       }
1376     }
1377   }
1378 
1379   /* Configure the Smooth calibration settings */
1380   hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod     | \
1381                                     (uint32_t)SmoothCalibPlusPulses | \
1382                                     (uint32_t)SmoothCalibMinusPulsesValue);
1383 
1384   /* Enable the write protection for RTC registers */
1385   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1386 
1387   /* Change RTC state */
1388   hrtc->State = HAL_RTC_STATE_READY;
1389 
1390   /* Process Unlocked */
1391   __HAL_UNLOCK(hrtc);
1392 
1393   return HAL_OK;
1394 }
1395 
1396 /**
1397   * @brief  Configures the Synchronization Shift Control Settings.
1398   * @note   When REFCKON is set, firmware must not write to Shift control register.
1399   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1400   *                the configuration information for RTC.
1401   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
1402   *          This parameter can be one of the following values:
1403   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1404   *             @arg RTC_SHIFTADD1S_RESET: No effect.
1405   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
1406   *          This parameter can be one any value from 0 to 0x7FFF.
1407   * @retval HAL status
1408   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)1409 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1410 {
1411   uint32_t tickstart = 0U;
1412 
1413   /* Check the parameters */
1414   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1415   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1416 
1417   /* Process Locked */
1418   __HAL_LOCK(hrtc);
1419 
1420   hrtc->State = HAL_RTC_STATE_BUSY;
1421 
1422   /* Disable the write protection for RTC registers */
1423   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1424 
1425   /* Get tick */
1426   tickstart = HAL_GetTick();
1427 
1428   /* Wait until the shift is completed */
1429   while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
1430   {
1431     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1432     {
1433       /* Enable the write protection for RTC registers */
1434       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1435 
1436       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1437 
1438       /* Process Unlocked */
1439       __HAL_UNLOCK(hrtc);
1440 
1441       return HAL_TIMEOUT;
1442     }
1443   }
1444 
1445   /* Check if the reference clock detection is disabled */
1446   if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
1447   {
1448     /* Configure the Shift settings */
1449     hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1450 
1451     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1452     if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
1453     {
1454       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1455       {
1456         /* Enable the write protection for RTC registers */
1457         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1458 
1459         hrtc->State = HAL_RTC_STATE_ERROR;
1460 
1461         /* Process Unlocked */
1462         __HAL_UNLOCK(hrtc);
1463 
1464         return HAL_ERROR;
1465       }
1466     }
1467   }
1468   else
1469   {
1470     /* Enable the write protection for RTC registers */
1471     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1472 
1473     /* Change RTC state */
1474     hrtc->State = HAL_RTC_STATE_ERROR;
1475 
1476     /* Process Unlocked */
1477     __HAL_UNLOCK(hrtc);
1478 
1479     return HAL_ERROR;
1480   }
1481 
1482   /* Enable the write protection for RTC registers */
1483   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1484 
1485   /* Change RTC state */
1486   hrtc->State = HAL_RTC_STATE_READY;
1487 
1488   /* Process Unlocked */
1489   __HAL_UNLOCK(hrtc);
1490 
1491   return HAL_OK;
1492 }
1493 
1494 /**
1495   * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1496   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1497   *                the configuration information for RTC.
1498   * @param  CalibOutput Select the Calibration output Selection.
1499   *          This parameter can be one of the following values:
1500   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1501   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1502   * @retval HAL status
1503   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1504 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1505 {
1506   /* Check the parameters */
1507   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1508 
1509   /* Process Locked */
1510   __HAL_LOCK(hrtc);
1511 
1512   hrtc->State = HAL_RTC_STATE_BUSY;
1513 
1514   /* Disable the write protection for RTC registers */
1515   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1516 
1517   /* Clear flags before config */
1518   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1519 
1520   /* Configure the RTC_CR register */
1521   hrtc->Instance->CR |= (uint32_t)CalibOutput;
1522 
1523   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1524 
1525   /* Enable the write protection for RTC registers */
1526   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1527 
1528   /* Change RTC state */
1529   hrtc->State = HAL_RTC_STATE_READY;
1530 
1531   /* Process Unlocked */
1532   __HAL_UNLOCK(hrtc);
1533 
1534   return HAL_OK;
1535 }
1536 
1537 /**
1538   * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1539   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1540   *                the configuration information for RTC.
1541   * @retval HAL status
1542   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1543 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1544 {
1545   /* Process Locked */
1546   __HAL_LOCK(hrtc);
1547 
1548   hrtc->State = HAL_RTC_STATE_BUSY;
1549 
1550   /* Disable the write protection for RTC registers */
1551   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1552 
1553   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1554 
1555   /* Enable the write protection for RTC registers */
1556   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1557 
1558   /* Change RTC state */
1559   hrtc->State = HAL_RTC_STATE_READY;
1560 
1561   /* Process Unlocked */
1562   __HAL_UNLOCK(hrtc);
1563 
1564   return HAL_OK;
1565 }
1566 
1567 /**
1568   * @brief  Enables the RTC reference clock detection.
1569   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1570   *                the configuration information for RTC.
1571   * @retval HAL status
1572   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1573 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1574 {
1575   HAL_StatusTypeDef status;
1576 
1577   /* Process Locked */
1578   __HAL_LOCK(hrtc);
1579 
1580   hrtc->State = HAL_RTC_STATE_BUSY;
1581 
1582   /* Disable the write protection for RTC registers */
1583   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1584 
1585   /* Enter Initialization mode */
1586   status = RTC_EnterInitMode(hrtc);
1587 
1588   if (status == HAL_OK)
1589   {
1590     /* Enable the reference clock detection */
1591     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1592 
1593     /* Exit Initialization mode */
1594     status = RTC_ExitInitMode(hrtc);
1595   }
1596 
1597   if (status == HAL_OK)
1598   {
1599     hrtc->State = HAL_RTC_STATE_READY;
1600   }
1601 
1602   /* Enable the write protection for RTC registers */
1603   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1604 
1605   /* Process Unlocked */
1606   __HAL_UNLOCK(hrtc);
1607 
1608   return status;
1609 }
1610 
1611 /**
1612   * @brief  Disable the RTC reference clock detection.
1613   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1614   *                the configuration information for RTC.
1615   * @retval HAL status
1616   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1617 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1618 {
1619   HAL_StatusTypeDef status;
1620 
1621   /* Process Locked */
1622   __HAL_LOCK(hrtc);
1623 
1624   hrtc->State = HAL_RTC_STATE_BUSY;
1625 
1626   /* Disable the write protection for RTC registers */
1627   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1628 
1629   /* Enter Initialization mode */
1630   status = RTC_EnterInitMode(hrtc);
1631 
1632   if (status == HAL_OK)
1633   {
1634     /* Disable the reference clock detection */
1635     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1636 
1637     /* Exit Initialization mode */
1638     status = RTC_ExitInitMode(hrtc);
1639   }
1640 
1641   if (status == HAL_OK)
1642   {
1643     hrtc->State = HAL_RTC_STATE_READY;
1644   }
1645 
1646   /* Enable the write protection for RTC registers */
1647   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1648 
1649   /* Process Unlocked */
1650   __HAL_UNLOCK(hrtc);
1651 
1652   return status;
1653 }
1654 
1655 /**
1656   * @brief  Enables the Bypass Shadow feature.
1657   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1658   *                the configuration information for RTC.
1659   * @note   When the Bypass Shadow is enabled the calendar value are taken
1660   *         directly from the Calendar counter.
1661   * @retval HAL status
1662   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1663 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1664 {
1665   /* Process Locked */
1666   __HAL_LOCK(hrtc);
1667 
1668   hrtc->State = HAL_RTC_STATE_BUSY;
1669 
1670   /* Disable the write protection for RTC registers */
1671   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1672 
1673   /* Set the BYPSHAD bit */
1674   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1675 
1676   /* Enable the write protection for RTC registers */
1677   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1678 
1679   /* Change RTC state */
1680   hrtc->State = HAL_RTC_STATE_READY;
1681 
1682   /* Process Unlocked */
1683   __HAL_UNLOCK(hrtc);
1684 
1685   return HAL_OK;
1686 }
1687 
1688 /**
1689   * @brief  Disables the Bypass Shadow feature.
1690   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1691   *                the configuration information for RTC.
1692   * @note   When the Bypass Shadow is enabled the calendar value are taken
1693   *         directly from the Calendar counter.
1694   * @retval HAL status
1695   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1696 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1697 {
1698   /* Process Locked */
1699   __HAL_LOCK(hrtc);
1700 
1701   hrtc->State = HAL_RTC_STATE_BUSY;
1702 
1703   /* Disable the write protection for RTC registers */
1704   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1705 
1706   /* Reset the BYPSHAD bit */
1707   hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
1708 
1709   /* Enable the write protection for RTC registers */
1710   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1711 
1712   /* Change RTC state */
1713   hrtc->State = HAL_RTC_STATE_READY;
1714 
1715   /* Process Unlocked */
1716   __HAL_UNLOCK(hrtc);
1717 
1718   return HAL_OK;
1719 }
1720 
1721 /**
1722   * @}
1723   */
1724 
1725 /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
1726   * @brief    Extended features functions
1727   *
1728 @verbatim
1729  ===============================================================================
1730                  ##### Extended features functions #####
1731  ===============================================================================
1732     [..]  This section provides functions allowing to:
1733       (+) RTC Alarm B callback
1734       (+) RTC Poll for Alarm B request
1735 
1736 @endverbatim
1737   * @{
1738   */
1739 
1740 /**
1741   * @brief  Alarm B callback.
1742   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1743   *                the configuration information for RTC.
1744   * @retval None
1745   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1746 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1747 {
1748   /* Prevent unused argument(s) compilation warning */
1749   UNUSED(hrtc);
1750 
1751   /* NOTE: This function should not be modified, when the callback is needed,
1752            the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1753    */
1754 }
1755 
1756 /**
1757   * @brief  Handles Alarm B Polling request.
1758   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
1759   *                the configuration information for RTC.
1760   * @param  Timeout Timeout duration
1761   * @retval HAL status
1762   */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1763 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1764 {
1765   uint32_t tickstart = 0U;
1766 
1767   /* Get tick */
1768   tickstart = HAL_GetTick();
1769 
1770   /* Wait till RTC ALRBF flag is set and if timeout is reached exit */
1771   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
1772   {
1773     if (Timeout != HAL_MAX_DELAY)
1774     {
1775       if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1776       {
1777         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1778         return HAL_TIMEOUT;
1779       }
1780     }
1781   }
1782 
1783   /* Clear the Alarm flag */
1784   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1785 
1786   /* Change RTC state */
1787   hrtc->State = HAL_RTC_STATE_READY;
1788 
1789   return HAL_OK;
1790 }
1791 
1792 /**
1793   * @}
1794   */
1795 
1796 /**
1797   * @}
1798   */
1799 
1800 #endif /* HAL_RTC_MODULE_ENABLED */
1801 /**
1802   * @}
1803   */
1804 
1805 /**
1806   * @}
1807   */
1808