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