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