• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_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   * @attention
15   *
16   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
17   * All rights reserved.</center></h2>
18   *
19   * This software component is licensed by ST under BSD 3-Clause license,
20   * the "License"; You may not use this file except in compliance with the
21   * License. You may obtain a copy of the License at:
22   *                        opensource.org/licenses/BSD-3-Clause
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 these functions you have to call HAL_RTC_Init() in order to
105          perform TAMP base address offset calculation.
106 
107    @endverbatim
108   */
109 
110 /* Includes ------------------------------------------------------------------*/
111 #include "stm32mp1xx_hal.h"
112 
113 /** @addtogroup STM32MP1xx_HAL_Driver
114   * @{
115   */
116 
117 /** @addtogroup RTCEx
118   * @brief RTC Extended HAL module driver
119   * @{
120   */
121 
122 #ifdef HAL_RTC_MODULE_ENABLED
123 
124 /* Private typedef -----------------------------------------------------------*/
125 /* Private define ------------------------------------------------------------*/
126 #define TAMP_ALL (TAMP_CR1_TAMP1E | TAMP_CR1_TAMP2E | TAMP_CR1_TAMP3E)
127 
128 
129 /* Private macro -------------------------------------------------------------*/
130 /* Private variables ---------------------------------------------------------*/
131 /* Private function prototypes -----------------------------------------------*/
132 /* Exported functions --------------------------------------------------------*/
133 
134 /** @addtogroup RTCEx_Exported_Functions
135   * @{
136   */
137 
138 
139 /** @addtogroup RTCEx_Exported_Functions_Group1
140  *  @brief   RTC TimeStamp and Tamper functions
141   *
142 @verbatim
143  ===============================================================================
144                  ##### RTC TimeStamp and Tamper functions #####
145  ===============================================================================
146 
147  [..] This section provides functions allowing to configure TimeStamp feature
148 
149 @endverbatim
150   * @{
151   */
152 
153 /**
154   * @brief  Set TimeStamp.
155   * @note   This API must be called before enabling the TimeStamp feature.
156   * @param  hrtc RTC handle
157   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
158   *         activated.
159   *          This parameter can be one of the following values:
160   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
161   *                                        rising edge of the related pin.
162   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
163   *                                         falling edge of the related pin.
164   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
165   *          This parameter can be one of the following values:
166   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
167   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
168   *               compatibility, this parameter is required.
169   * @retval HAL status
170   */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)171 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
172 {
173   /* Check the parameters */
174   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
175   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
176   UNUSED(RTC_TimeStampPin);
177 
178   /* Process Locked */
179   __HAL_LOCK(hrtc);
180 
181   hrtc->State = HAL_RTC_STATE_BUSY;
182 
183   /* Get the RTC_CR register and clear the bits to be configured */
184   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
185 
186   /* Disable the write protection for RTC registers */
187   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
188 
189   /* Configure the Time Stamp TSEDGE and Enable bits */
190   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge | RTC_CR_TSE);
191 
192   /* Enable the write protection for RTC registers */
193   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
194 
195   /* Change RTC state */
196   hrtc->State = HAL_RTC_STATE_READY;
197 
198   /* Process Unlocked */
199   __HAL_UNLOCK(hrtc);
200 
201   return HAL_OK;
202 }
203 
204 /**
205   * @brief  Set TimeStamp with Interrupt.
206   * @note   This API must be called before enabling the TimeStamp feature.
207   * @param  hrtc RTC handle
208   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
209   *         activated.
210   *          This parameter can be one of the following values:
211   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
212   *                                        rising edge of the related pin.
213   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
214   *                                         falling edge of the related pin.
215   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
216   *          This parameter can be one of the following values:
217   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
218   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
219   *               compatibility, this parameter is required.
220   * @retval HAL status
221   */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)222 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
223 {
224   /* Check the parameters */
225   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
226   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
227   UNUSED(RTC_TimeStampPin);
228 
229   /* Process Locked */
230   __HAL_LOCK(hrtc);
231 
232   hrtc->State = HAL_RTC_STATE_BUSY;
233 
234   /* RTC timestamp Interrupt Configuration: EXTI configuration */
235   __HAL_RTC_TIMESTAMP_EXTI_ENABLE_IT();
236 
237   /* Get the RTC_CR register and clear the bits to be configured */
238   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
239 
240   /* Disable the write protection for RTC registers */
241   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
242 
243   /* Configure the Time Stamp TSEDGE before Enable bit to avoid unwanted TSF setting. */
244   SET_BIT(RTC->CR, (uint32_t)TimeStampEdge);
245 
246   /* Enable timestamp and IT */
247   SET_BIT(RTC->CR, RTC_CR_TSE | RTC_CR_TSIE);
248 
249   /* Enable the write protection for RTC registers */
250   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
251 
252   hrtc->State = HAL_RTC_STATE_READY;
253 
254   /* Process Unlocked */
255   __HAL_UNLOCK(hrtc);
256 
257   return HAL_OK;
258 }
259 
260 /**
261   * @brief  Deactivate TimeStamp.
262   * @param  hrtc RTC handle
263   * @retval HAL status
264   */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)265 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
266 {
267   /* Process Locked */
268   __HAL_LOCK(hrtc);
269 
270   hrtc->State = HAL_RTC_STATE_BUSY;
271 
272   /* Disable the write protection for RTC registers */
273   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
274 
275   /* In case of interrupt mode is used, the interrupt source must disabled */
276   CLEAR_BIT(RTC->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
277 
278   /* Enable the write protection for RTC registers */
279   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
280 
281   hrtc->State = HAL_RTC_STATE_READY;
282 
283   /* Process Unlocked */
284   __HAL_UNLOCK(hrtc);
285 
286   return HAL_OK;
287 }
288 
289 /**
290   * @brief  Set Internal TimeStamp.
291   * @note   This API must be called before enabling the internal TimeStamp feature.
292   * @param  hrtc RTC handle
293   * @retval HAL status
294   */
HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef * hrtc)295 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
296 {
297   /* Process Locked */
298   __HAL_LOCK(hrtc);
299 
300   hrtc->State = HAL_RTC_STATE_BUSY;
301 
302   /* Disable the write protection for RTC registers */
303   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
304 
305   /* Configure the internal Time Stamp Enable bits */
306   SET_BIT(RTC->CR, RTC_CR_ITSE);
307 
308   /* Enable the write protection for RTC registers */
309   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
310 
311   /* Change RTC state */
312   hrtc->State = HAL_RTC_STATE_READY;
313 
314   /* Process Unlocked */
315   __HAL_UNLOCK(hrtc);
316 
317   return HAL_OK;
318 }
319 
320 /**
321   * @brief  Deactivate Internal TimeStamp.
322   * @param  hrtc RTC handle
323   * @retval HAL status
324   */
HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef * hrtc)325 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
326 {
327   /* Process Locked */
328   __HAL_LOCK(hrtc);
329 
330   hrtc->State = HAL_RTC_STATE_BUSY;
331 
332   /* Disable the write protection for RTC registers */
333   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
334 
335   /* Configure the internal Time Stamp Enable bits */
336   CLEAR_BIT(RTC->CR, RTC_CR_ITSE);
337 
338   /* Enable the write protection for RTC registers */
339   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
340 
341   hrtc->State = HAL_RTC_STATE_READY;
342 
343   /* Process Unlocked */
344   __HAL_UNLOCK(hrtc);
345 
346   return HAL_OK;
347 }
348 
349 /**
350   * @brief  Get the RTC TimeStamp value.
351   * @param  hrtc RTC handle
352   * @param  sTimeStamp Pointer to Time structure
353   * @param  sTimeStampDate Pointer to Date structure
354   * @param  Format specifies the format of the entered parameters.
355   *          This parameter can be one of the following values:
356   *             @arg RTC_FORMAT_BIN: Binary data format
357   *             @arg RTC_FORMAT_BCD: BCD data format
358   * @retval HAL status
359   */
HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)360 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
361 {
362   uint32_t tmptime, tmpdate;
363 
364   UNUSED(hrtc);
365   /* Check the parameters */
366   assert_param(IS_RTC_FORMAT(Format));
367 
368   /* Get the TimeStamp time and date registers values */
369   tmptime = READ_BIT(RTC->TSTR, RTC_TR_RESERVED_MASK);
370   tmpdate = READ_BIT(RTC->TSDR, RTC_DR_RESERVED_MASK);
371 
372   /* Fill the Time structure fields with the read parameters */
373   sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
374   sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
375   sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
376   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
377   sTimeStamp->SubSeconds = READ_BIT(RTC->TSSSR, RTC_TSSSR_SS);
378 
379   /* Fill the Date structure fields with the read parameters */
380   sTimeStampDate->Year = 0U;
381   sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
382   sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU));
383   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
384 
385   /* Check the input parameters format */
386   if (Format == RTC_FORMAT_BIN)
387   {
388     /* Convert the TimeStamp structure parameters to Binary format */
389     sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
390     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
391     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
392 
393     /* Convert the DateTimeStamp structure parameters to Binary format */
394     sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
395     sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
396     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
397   }
398 
399   /* Clear the TIMESTAMP Flags */
400   WRITE_REG(RTC->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
401 
402   return HAL_OK;
403 }
404 
405 #if defined (CORTEX_IN_SECURE_STATE)
406 /**
407   * @brief  Handle TimeStamp secure interrupt request.
408   * @param  hrtc RTC handle
409   * @retval None
410   */
HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef * hrtc)411 void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
412 {
413   if (READ_BIT(RTC->SMISR, RTC_SMISR_TSMF) != 0U)
414   {
415 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
416     /* Call TimeStampEvent registered Callback */
417     hrtc->TimeStampEventCallback(hrtc);
418 #else
419     HAL_RTCEx_TimeStampEventCallback(hrtc);
420 #endif
421     /* Clearing flags after the Callback because the content of RTC_TSTR and RTC_TSDR are cleared when TSF bit is reset.*/
422     WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSF);
423   }
424 
425   /* Change RTC state */
426   hrtc->State = HAL_RTC_STATE_READY;
427 }
428 
429 #else /* #if defined (CORTEX_IN_SECURE_STATE) */
430 
431 /**
432   * @brief  Handle TimeStamp non-secure interrupt request.
433   * @param  hrtc RTC handle
434   * @retval None
435   */
HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef * hrtc)436 void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
437 {
438   if (READ_BIT(RTC->MISR, RTC_MISR_TSMF) != 0U)
439   {
440 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
441     /* Call TimeStampEvent registered Callback */
442     hrtc->TimeStampEventCallback(hrtc);
443 #else
444     HAL_RTCEx_TimeStampEventCallback(hrtc);
445 #endif
446     /* Clearing flags after the Callback because the content of RTC_TSTR and RTC_TSDR are cleared when TSF bit is reset.*/
447     WRITE_REG(RTC->SCR, RTC_SCR_CITSF | RTC_SCR_CTSF);
448   }
449 
450   /* Change RTC state */
451   hrtc->State = HAL_RTC_STATE_READY;
452 }
453 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
454 
455 /**
456   * @brief  TimeStamp callback.
457   * @param  hrtc RTC handle
458   * @retval None
459   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)460 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
461 {
462   /* Prevent unused argument(s) compilation warning */
463   UNUSED(hrtc);
464 
465   /* NOTE : This function should not be modified, when the callback is needed,
466             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
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 (READ_BIT(RTC->SR, RTC_SR_TSF) == 0U)
481   {
482     if (READ_BIT(RTC->SR, RTC_SR_TSOVF) != 0U)
483     {
484       /* Clear the TIMESTAMP OverRun Flag */
485       WRITE_REG(RTC->SCR, RTC_SCR_CTSOVF);
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 (READ_BIT(RTC->ICSR, RTC_ICSR_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   /* Configure the clock source */
577   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
578 
579   /* Configure the Wakeup Timer counter */
580   WRITE_REG(RTC->WUTR, (uint32_t)WakeUpCounter);
581 
582   /* Enable the Wakeup Timer */
583   SET_BIT(RTC->CR, RTC_CR_WUTE);
584 
585   /* Enable the write protection for RTC registers */
586   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
587 
588   hrtc->State = HAL_RTC_STATE_READY;
589 
590   /* Process Unlocked */
591   __HAL_UNLOCK(hrtc);
592 
593   return HAL_OK;
594 }
595 
596 /**
597   * @brief  Set wake up timer with interrupt.
598   * @param  hrtc RTC handle
599   * @param  WakeUpCounter Wake up counter
600   * @param  WakeUpClock Wake up clock
601   * @param  WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
602   *                       - No effect if WakeUpAutoClr is set to zero
603   *                       - This feature is meaningful in case of Low power mode to avoid any RTC software execution after Wake Up.
604   *                         That is why when WakeUpAutoClr is set, EXTI is configured as EVENT instead of Interrupt to avoid useless IRQ handler execution.
605   * @retval HAL status
606   */
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock,uint32_t WakeUpAutoClr)607 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock, uint32_t WakeUpAutoClr)
608 {
609   uint32_t tickstart;
610 
611   /* Check the parameters */
612   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
613   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
614   /* (0x0000<=WUTOCLR<=WUT) */
615   assert_param(WakeUpAutoClr <= WakeUpCounter);
616 
617   /* Process Locked */
618   __HAL_LOCK(hrtc);
619 
620   hrtc->State = HAL_RTC_STATE_BUSY;
621 
622   /* Disable the write protection for RTC registers */
623   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
624 
625   /* Clear WUTE in RTC_CR to disable the wakeup timer */
626   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
627 
628   /* Clear flag Wake-Up */
629   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
630 
631   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
632      counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
633      calendar initialization mode. */
634   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
635   {
636     tickstart = HAL_GetTick();
637     while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
638     {
639       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
640       {
641         /* Enable the write protection for RTC registers */
642         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
643 
644         hrtc->State = HAL_RTC_STATE_TIMEOUT;
645 
646         /* Process Unlocked */
647         __HAL_UNLOCK(hrtc);
648 
649         return HAL_TIMEOUT;
650       }
651     }
652   }
653 
654   /* Configure the Wakeup Timer counter and auto clear value */
655   WRITE_REG(RTC->WUTR, (uint32_t)(WakeUpCounter));
656 
657   /* Configure the clock source */
658   MODIFY_REG(RTC->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
659 
660   /* In case of WUT autoclr, the IRQ handler should not be called */
661   if (WakeUpAutoClr != 0U)
662   {
663     /* RTC WakeUpTimer EXTI Configuration: Event configuration */
664     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
665   }
666   else
667   {
668     /* RTC WakeUpTimer EXTI Configuration: Interrupt configuration */
669     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
670   }
671 
672   /* Configure the Interrupt in the RTC_CR register and Enable the Wakeup Timer*/
673   SET_BIT(RTC->CR, (RTC_CR_WUTIE | RTC_CR_WUTE));
674 
675   /* Enable the write protection for RTC registers */
676   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
677 
678   hrtc->State = HAL_RTC_STATE_READY;
679 
680   /* Process Unlocked */
681   __HAL_UNLOCK(hrtc);
682 
683   return HAL_OK;
684 }
685 
686 /**
687   * @brief  Deactivate wake up timer counter.
688   * @param  hrtc RTC handle
689   * @retval HAL status
690   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)691 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
692 {
693   uint32_t tickstart;
694 
695   /* Process Locked */
696   __HAL_LOCK(hrtc);
697 
698   hrtc->State = HAL_RTC_STATE_BUSY;
699 
700   /* Disable the write protection for RTC registers */
701   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
702 
703   /* Disable the Wakeup Timer */
704   /* In case of interrupt mode is used, the interrupt source must disabled */
705   CLEAR_BIT(RTC->CR, (RTC_CR_WUTE | RTC_CR_WUTIE));
706 
707   tickstart = HAL_GetTick();
708   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
709   while (READ_BIT(RTC->ICSR, RTC_ICSR_WUTWF) == 0U)
710   {
711     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
712     {
713       /* Enable the write protection for RTC registers */
714       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
715 
716       hrtc->State = HAL_RTC_STATE_TIMEOUT;
717 
718       /* Process Unlocked */
719       __HAL_UNLOCK(hrtc);
720 
721       return HAL_TIMEOUT;
722     }
723   }
724 
725   /* Enable the write protection for RTC registers */
726   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
727 
728   hrtc->State = HAL_RTC_STATE_READY;
729 
730   /* Process Unlocked */
731   __HAL_UNLOCK(hrtc);
732 
733   return HAL_OK;
734 }
735 
736 /**
737   * @brief  Get wake up timer counter.
738   * @param  hrtc RTC handle
739   * @retval Counter value
740   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)741 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
742 {
743   UNUSED(hrtc);
744   /* Get the counter value */
745   return (uint32_t)(READ_BIT(RTC->WUTR, RTC_WUTR_WUT));
746 }
747 
748 #if defined (CORTEX_IN_SECURE_STATE)
749 /**
750   * @brief  Handle Wake Up Timer secure interrupt request.
751   * @param  hrtc RTC handle
752   * @retval None
753   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)754 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
755 {
756   if ((RTC->SMISR & RTC_SMISR_WUTMF) != 0u)
757   {
758     /* Immediatly clear flags */
759     WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
760 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
761     /* Call wake up timer registered Callback */
762     hrtc->WakeUpTimerEventCallback(hrtc);
763 #else
764     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
765 #endif
766   }
767 
768   /* Change RTC state */
769   hrtc->State = HAL_RTC_STATE_READY;
770 }
771 
772 #else /* #if defined (CORTEX_IN_SECURE_STATE) */
773 
774 /**
775   * @brief  Handle Wake Up Timer non-secure interrupt request.
776   * @param  hrtc RTC handle
777   * @retval None
778   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)779 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
780 {
781   /* Get the pending status of the WAKEUPTIMER Interrupt */
782   if (READ_BIT(RTC->MISR, RTC_MISR_WUTMF) != 0U)
783   {
784     /* Clear the WAKEUPTIMER interrupt pending bit */
785     WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
786 
787 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
788     /* Call WakeUpTimerEvent registered Callback */
789     hrtc->WakeUpTimerEventCallback(hrtc);
790 #else
791     /* WAKEUPTIMER callback */
792     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
793 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
794   }
795 
796   /* Change RTC state */
797   hrtc->State = HAL_RTC_STATE_READY;
798 }
799 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
800 
801 /**
802   * @brief  Wake Up Timer callback.
803   * @param  hrtc RTC handle
804   * @retval None
805   */
HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)806 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
807 {
808   /* Prevent unused argument(s) compilation warning */
809   UNUSED(hrtc);
810 
811   /* NOTE : This function should not be modified, when the callback is needed,
812             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
813    */
814 }
815 
816 /**
817   * @brief  Handle Wake Up Timer Polling.
818   * @param  hrtc RTC handle
819   * @param  Timeout Timeout duration
820   * @retval HAL status
821   */
HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)822 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
823 {
824   uint32_t tickstart = HAL_GetTick();
825 
826   while (READ_BIT(RTC->SR, RTC_SR_WUTF) == 0U)
827   {
828     if (Timeout != HAL_MAX_DELAY)
829     {
830       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
831       {
832         hrtc->State = HAL_RTC_STATE_TIMEOUT;
833         return HAL_TIMEOUT;
834       }
835     }
836   }
837 
838   /* Clear the WAKEUPTIMER Flag */
839   WRITE_REG(RTC->SCR, RTC_SCR_CWUTF);
840 
841   /* Change RTC state */
842   hrtc->State = HAL_RTC_STATE_READY;
843 
844   return HAL_OK;
845 }
846 
847 /**
848   * @}
849   */
850 
851 /** @addtogroup RTCEx_Exported_Functions_Group3
852   * @brief    Extended Peripheral Control functions
853   *
854 @verbatim
855  ===============================================================================
856               ##### Extended Peripheral Control functions #####
857  ===============================================================================
858     [..]
859     This subsection provides functions allowing to
860       (+) Write a data in a specified RTC Backup data register
861       (+) Read a data in a specified RTC Backup data register
862       (+) Set the Coarse calibration parameters.
863       (+) Deactivate the Coarse calibration parameters
864       (+) Set the Smooth calibration parameters.
865       (+) Set Low Power calibration parameter.
866       (+) Configure the Synchronization Shift Control Settings.
867       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
868       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
869       (+) Enable the RTC reference clock detection.
870       (+) Disable the RTC reference clock detection.
871       (+) Enable the Bypass Shadow feature.
872       (+) Disable the Bypass Shadow feature.
873 
874 @endverbatim
875   * @{
876   */
877 
878 /**
879   * @brief  Set the Smooth calibration parameters.
880   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
881   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
882   *         SmoothCalibMinusPulsesValue must be equal to 0.
883   * @param  hrtc RTC handle
884   * @param  SmoothCalibPeriod  Select the Smooth Calibration Period.
885   *          This parameter can be can be one of the following values :
886   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
887   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
888   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
889   * @param  SmoothCalibPlusPulses  Select to Set or reset the CALP bit.
890   *          This parameter can be one of the following values:
891   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
892   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
893   * @param  SmoothCalibMinusPulsesValue  Select the value of CALM[8:0] bits.
894   *          This parameter can be one any value from 0 to 0x000001FF.
895   * @retval HAL status
896   */
HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc,uint32_t SmoothCalibPeriod,uint32_t SmoothCalibPlusPulses,uint32_t SmoothCalibMinusPulsesValue)897 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
898 {
899   uint32_t tickstart;
900 
901   /* Check the parameters */
902   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
903   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
904   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
905 
906   /* Process Locked */
907   __HAL_LOCK(hrtc);
908 
909   hrtc->State = HAL_RTC_STATE_BUSY;
910 
911   /* Disable the write protection for RTC registers */
912   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
913 
914   /* check if a calibration is pending*/
915   if (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
916   {
917     tickstart = HAL_GetTick();
918 
919     /* check if a calibration is pending*/
920     while (READ_BIT(RTC->ICSR, RTC_ICSR_RECALPF) != 0U)
921     {
922       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
923       {
924         /* Enable the write protection for RTC registers */
925         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
926 
927         /* Change RTC state */
928         hrtc->State = HAL_RTC_STATE_TIMEOUT;
929 
930         /* Process Unlocked */
931         __HAL_UNLOCK(hrtc);
932 
933         return HAL_TIMEOUT;
934       }
935     }
936   }
937 
938   /* Configure the Smooth calibration settings */
939   MODIFY_REG(RTC->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
940 
941   /* Enable the write protection for RTC registers */
942   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
943 
944   /* Change RTC state */
945   hrtc->State = HAL_RTC_STATE_READY;
946 
947   /* Process Unlocked */
948   __HAL_UNLOCK(hrtc);
949 
950   return HAL_OK;
951 }
952 
953 /**
954   * @brief  Configure the Synchronization Shift Control Settings.
955   * @note   When REFCKON is set, firmware must not write to Shift control register.
956   * @param  hrtc RTC handle
957   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
958   *          This parameter can be one of the following values:
959   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
960   *             @arg RTC_SHIFTADD1S_RESET: No effect.
961   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
962   *          This parameter can be one any value from 0 to 0x7FFF.
963   * @retval HAL status
964   */
HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc,uint32_t ShiftAdd1S,uint32_t ShiftSubFS)965 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
966 {
967   uint32_t tickstart;
968 
969   /* Check the parameters */
970   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
971   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
972 
973   /* Process Locked */
974   __HAL_LOCK(hrtc);
975 
976   hrtc->State = HAL_RTC_STATE_BUSY;
977 
978   /* Disable the write protection for RTC registers */
979   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
980 
981   tickstart = HAL_GetTick();
982 
983   /* Wait until the shift is completed*/
984   while (READ_BIT(RTC->ICSR, RTC_ICSR_SHPF) != 0U)
985   {
986     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
987     {
988       /* Enable the write protection for RTC registers */
989       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
990 
991       hrtc->State = HAL_RTC_STATE_TIMEOUT;
992 
993       /* Process Unlocked */
994       __HAL_UNLOCK(hrtc);
995 
996       return HAL_TIMEOUT;
997     }
998   }
999 
1000   /* Check if the reference clock detection is disabled */
1001   if (READ_BIT(RTC->CR, RTC_CR_REFCKON) == 0U)
1002   {
1003     /* Configure the Shift settings */
1004     MODIFY_REG(RTC->SHIFTR, RTC_SHIFTR_SUBFS, (uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S));
1005 
1006     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1007     if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1008     {
1009       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1010       {
1011         /* Enable the write protection for RTC registers */
1012         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1013 
1014         hrtc->State = HAL_RTC_STATE_ERROR;
1015 
1016         /* Process Unlocked */
1017         __HAL_UNLOCK(hrtc);
1018 
1019         return HAL_ERROR;
1020       }
1021     }
1022   }
1023   else
1024   {
1025     /* Enable the write protection for RTC registers */
1026     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1027 
1028     /* Change RTC state */
1029     hrtc->State = HAL_RTC_STATE_ERROR;
1030 
1031     /* Process Unlocked */
1032     __HAL_UNLOCK(hrtc);
1033 
1034     return HAL_ERROR;
1035   }
1036 
1037   /* Enable the write protection for RTC registers */
1038   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1039 
1040   /* Change RTC state */
1041   hrtc->State = HAL_RTC_STATE_READY;
1042 
1043   /* Process Unlocked */
1044   __HAL_UNLOCK(hrtc);
1045 
1046   return HAL_OK;
1047 }
1048 
1049 /**
1050   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1051   * @param  hrtc RTC handle
1052   * @param  CalibOutput Select the Calibration output Selection .
1053   *          This parameter can be one of the following values:
1054   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1055   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1056   * @retval HAL status
1057   */
HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc,uint32_t CalibOutput)1058 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
1059 {
1060   /* Check the parameters */
1061   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
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   /* Configure the RTC_CR register */
1072   MODIFY_REG(RTC->CR, RTC_CR_COSEL, CalibOutput);
1073 
1074   /* Enable calibration output */
1075   SET_BIT(RTC->CR, RTC_CR_COE);
1076 
1077   /* Enable the write protection for RTC registers */
1078   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1079 
1080   /* Change RTC state */
1081   hrtc->State = HAL_RTC_STATE_READY;
1082 
1083   /* Process Unlocked */
1084   __HAL_UNLOCK(hrtc);
1085 
1086   return HAL_OK;
1087 }
1088 
1089 /**
1090   * @brief  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1091   * @param  hrtc RTC handle
1092   * @retval HAL status
1093   */
HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)1094 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
1095 {
1096   /* Process Locked */
1097   __HAL_LOCK(hrtc);
1098 
1099   hrtc->State = HAL_RTC_STATE_BUSY;
1100 
1101   /* Disable the write protection for RTC registers */
1102   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1103 
1104   /* Disable calibration output */
1105   CLEAR_BIT(RTC->CR, RTC_CR_COE);
1106 
1107   /* Enable the write protection for RTC registers */
1108   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1109 
1110   /* Change RTC state */
1111   hrtc->State = HAL_RTC_STATE_READY;
1112 
1113   /* Process Unlocked */
1114   __HAL_UNLOCK(hrtc);
1115 
1116   return HAL_OK;
1117 }
1118 
1119 /**
1120   * @brief  Enable the RTC reference clock detection.
1121   * @param  hrtc RTC handle
1122   * @retval HAL status
1123   */
HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)1124 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
1125 {
1126   HAL_StatusTypeDef status;
1127 
1128   /* Process Locked */
1129   __HAL_LOCK(hrtc);
1130 
1131   hrtc->State = HAL_RTC_STATE_BUSY;
1132 
1133   /* Disable the write protection for RTC registers */
1134   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1135 
1136   /* Enter Initialization mode */
1137   status = RTC_EnterInitMode(hrtc);
1138   if (status == HAL_OK)
1139   {
1140     /* Enable clockref detection */
1141     SET_BIT(RTC->CR, RTC_CR_REFCKON);
1142 
1143     /* Exit Initialization mode */
1144     status = RTC_ExitInitMode(hrtc);
1145   }
1146 
1147   /* Enable the write protection for RTC registers */
1148   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1149 
1150   if (status == HAL_OK)
1151   {
1152     hrtc->State = HAL_RTC_STATE_READY;
1153   }
1154 
1155   /* Process Unlocked */
1156   __HAL_UNLOCK(hrtc);
1157 
1158   return status;
1159 }
1160 
1161 /**
1162   * @brief  Disable the RTC reference clock detection.
1163   * @param  hrtc RTC handle
1164   * @retval HAL status
1165   */
HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)1166 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
1167 {
1168   HAL_StatusTypeDef status;
1169 
1170   /* Process Locked */
1171   __HAL_LOCK(hrtc);
1172 
1173   hrtc->State = HAL_RTC_STATE_BUSY;
1174 
1175   /* Disable the write protection for RTC registers */
1176   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1177 
1178   /* Enter Initialization mode */
1179   status = RTC_EnterInitMode(hrtc);
1180   if (status == HAL_OK)
1181   {
1182     /* Disable clockref detection */
1183     CLEAR_BIT(RTC->CR, RTC_CR_REFCKON);
1184 
1185     /* Exit Initialization mode */
1186     status = RTC_ExitInitMode(hrtc);
1187   }
1188 
1189   /* Enable the write protection for RTC registers */
1190   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1191 
1192   if (status == HAL_OK)
1193   {
1194     hrtc->State = HAL_RTC_STATE_READY;
1195   }
1196 
1197   /* Process Unlocked */
1198   __HAL_UNLOCK(hrtc);
1199 
1200   return status;
1201 }
1202 
1203 /**
1204   * @brief  Enable the Bypass Shadow feature.
1205   * @note   When the Bypass Shadow is enabled the calendar value are taken
1206   *         directly from the Calendar counter.
1207   * @param  hrtc RTC handle
1208   * @retval HAL status
1209   */
HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)1210 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
1211 {
1212   /* Process Locked */
1213   __HAL_LOCK(hrtc);
1214 
1215   hrtc->State = HAL_RTC_STATE_BUSY;
1216 
1217   /* Disable the write protection for RTC registers */
1218   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1219 
1220   /* Set the BYPSHAD bit */
1221   SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1222 
1223   /* Enable the write protection for RTC registers */
1224   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1225 
1226   /* Change RTC state */
1227   hrtc->State = HAL_RTC_STATE_READY;
1228 
1229   /* Process Unlocked */
1230   __HAL_UNLOCK(hrtc);
1231 
1232   return HAL_OK;
1233 }
1234 
1235 /**
1236   * @brief  Disable the Bypass Shadow feature.
1237   * @note   When the Bypass Shadow is enabled the calendar value are taken
1238   *         directly from the Calendar counter.
1239   * @param  hrtc RTC handle
1240   * @retval HAL status
1241   */
HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)1242 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
1243 {
1244   /* Process Locked */
1245   __HAL_LOCK(hrtc);
1246 
1247   hrtc->State = HAL_RTC_STATE_BUSY;
1248 
1249   /* Disable the write protection for RTC registers */
1250   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1251 
1252   /* Reset the BYPSHAD bit */
1253   CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1254 
1255   /* Enable the write protection for RTC registers */
1256   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1257 
1258   /* Change RTC state */
1259   hrtc->State = HAL_RTC_STATE_READY;
1260 
1261   /* Process Unlocked */
1262   __HAL_UNLOCK(hrtc);
1263 
1264   return HAL_OK;
1265 }
1266 
1267 /**
1268   * @brief  Increment Monotonic counter.
1269   * @param  hrtc RTC handle
1270   * @param  Instance  Monotonic counter Instance
1271   *         This parameter can be can be one of the following values :
1272   *           @arg RTC_MONOTONIC_COUNTER_1
1273   * @retval HAL status
1274   */
HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef * hrtc,uint32_t Instance)1275 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef *hrtc, uint32_t Instance)
1276 {
1277   UNUSED(hrtc);
1278   UNUSED(Instance);
1279   /* This register is read-only only and is incremented by one when a write access is done to this
1280      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1281   CLEAR_REG(TAMP->COUNTR);
1282 
1283   return HAL_OK;
1284 }
1285 
1286 /**
1287   * @brief  Monotonic counter incrementation.
1288   * @param  hrtc RTC handle
1289   * @param  Instance  Monotonic counter Instance
1290   *         This parameter can be can be one of the following values :
1291   *           @arg RTC_MONOTONIC_COUNTER_1
1292   * @param  Value Pointer to the counter monotonic counter value
1293   * @retval HAL status
1294   */
HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef * hrtc,uint32_t Instance,uint32_t * Value)1295 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef *hrtc, uint32_t Instance, uint32_t *Value)
1296 {
1297   UNUSED(hrtc);
1298   UNUSED(Instance);
1299 
1300   /* This register is read-only only and is incremented by one when a write access is done to this
1301      register. This register cannot roll-over and is frozen when reaching the maximum value. */
1302   *Value = READ_REG(TAMP->COUNTR);
1303 
1304   return HAL_OK;
1305 }
1306 
1307 /**
1308   * @}
1309   */
1310 
1311 /** @addtogroup RTCEx_Exported_Functions_Group4
1312   * @brief    Extended features functions
1313   *
1314 @verbatim
1315  ===============================================================================
1316                  ##### Extended features functions #####
1317  ===============================================================================
1318     [..]  This section provides functions allowing to:
1319       (+) RTC Alarm B callback
1320       (+) RTC Poll for Alarm B request
1321 
1322 @endverbatim
1323   * @{
1324   */
1325 
1326 /**
1327   * @brief  Alarm B callback.
1328   * @param  hrtc RTC handle
1329   * @retval None
1330   */
HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)1331 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1332 {
1333   /* Prevent unused argument(s) compilation warning */
1334   UNUSED(hrtc);
1335 
1336   /* NOTE : This function should not be modified, when the callback is needed,
1337             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1338    */
1339 }
1340 
1341 /**
1342   * @brief  Handle Alarm B Polling request.
1343   * @param  hrtc RTC handle
1344   * @param  Timeout Timeout duration
1345   * @retval HAL status
1346   */
HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1347 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1348 {
1349   uint32_t tickstart = HAL_GetTick();
1350 
1351   while (READ_BIT(RTC->SR, RTC_SR_ALRBF) == 0U)
1352   {
1353     if (Timeout != HAL_MAX_DELAY)
1354     {
1355       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1356       {
1357         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1358         return HAL_TIMEOUT;
1359       }
1360     }
1361   }
1362 
1363   /* Clear the Alarm Flag */
1364   WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
1365 
1366   /* Change RTC state */
1367   hrtc->State = HAL_RTC_STATE_READY;
1368 
1369   return HAL_OK;
1370 }
1371 
1372 /**
1373   * @}
1374   */
1375 
1376 /** @addtogroup RTCEx_Exported_Functions_Group5
1377   * @brief      Extended RTC Tamper functions
1378   *
1379 @verbatim
1380   ==============================================================================
1381                          ##### Tamper functions #####
1382   ==============================================================================
1383   [..]
1384    (+) Before calling any tamper or internal tamper function, you have to call first
1385        HAL_RTC_Init() function.
1386    (+) In that ine you can select to output tamper event on RTC pin.
1387   [..]
1388    (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
1389        or Level according to the Tamper filter (if equal to 0 Edge else Level)
1390        value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
1391        Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
1392        You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
1393    (+) The default configuration of the Tamper erases the backup registers. To avoid
1394        erase, enable the NoErase field on the TAMP_TAMPCR register.
1395   [..]
1396    (+) Enable Internal Tamper and configure it with interrupt, timestamp using
1397        the HAL_RTCEx_SetInternalTamper() function.
1398 
1399 @endverbatim
1400   * @{
1401   */
1402 
1403 
1404 /**
1405   * @brief  Set Tamper
1406   * @param  hrtc RTC handle
1407   * @param  sTamper Pointer to Tamper Structure.
1408   * @retval HAL status
1409   */
HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1410 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
1411 {
1412   uint32_t tmpreg;
1413 
1414   /* Prevent unused argument(s) compilation warning */
1415   UNUSED(hrtc);
1416 
1417   /* Check the parameters */
1418   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1419   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1420   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1421   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1422   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1423   /* Mask flag only supported by TAMPER 1, 2 and 3 */
1424   assert_param(!((sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (sTamper->Tamper > RTC_TAMPER_3)));
1425   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1426   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1427   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1428   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1429   /* Trigger and Filter have exclusive configurations */
1430   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL)))
1431                || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1432 
1433   /* Configuration register 2 */
1434   tmpreg = READ_REG(TAMP->CR2);
1435   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1436 
1437   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1438   {
1439     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1440   }
1441 
1442   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1443   {
1444     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1445   }
1446 
1447   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1448   {
1449     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1450   }
1451   WRITE_REG(TAMP->CR2, tmpreg);
1452 
1453   /* Filter control register */
1454   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp);
1455 
1456   /* Timestamp on tamper */
1457   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1458   {
1459     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1460     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1461     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1462   }
1463 
1464   /* Control register 1 */
1465   SET_BIT(TAMP->CR1, sTamper->Tamper);
1466 
1467   return HAL_OK;
1468 }
1469 
1470 
1471 /**
1472   * @brief  Set Tamper in IT mode
1473   * @param  hrtc RTC handle
1474   * @param  sTamper Pointer to Tamper Structure.
1475   * @retval HAL status
1476   */
HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc,RTC_TamperTypeDef * sTamper)1477 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
1478 {
1479   uint32_t tmpreg;
1480 
1481   /* Prevent unused argument(s) compilation warning */
1482   UNUSED(hrtc);
1483 
1484   /* Check the parameters */
1485   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1486   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1487   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1488   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1489   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1490   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1491   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1492   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1493   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1494 
1495   /* Configuration register 2 */
1496   tmpreg = READ_REG(TAMP->CR2);
1497   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1498 
1499   if (sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
1500   {
1501     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1502   }
1503 
1504   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1505   {
1506     /* Feature only supported by TAMPER 1, 2 and 3 */
1507     if (sTamper->Tamper <= RTC_TAMPER_3)
1508     {
1509       tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1510     }
1511     else
1512     {
1513       return HAL_ERROR;
1514     }
1515   }
1516 
1517   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1518   {
1519     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1520   }
1521   WRITE_REG(TAMP->CR2, tmpreg);
1522 
1523   /* Filter control register */
1524   WRITE_REG(TAMP->FLTCR, sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp);
1525 
1526   /* Timestamp on tamper */
1527   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
1528   {
1529     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1530     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
1531     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1532   }
1533 
1534   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1535   __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1536 
1537   /* Interrupt enable register */
1538   SET_BIT(TAMP->IER, sTamper->Tamper);
1539 
1540   /* Control register 1 */
1541   SET_BIT(TAMP->CR1, sTamper->Tamper);
1542 
1543   return HAL_OK;
1544 }
1545 
1546 /**
1547   * @brief  Deactivate Tamper.
1548   * @param  hrtc RTC handle
1549   * @param  Tamper Selected tamper pin.
1550   *         This parameter can be a combination of the following values:
1551   *         @arg RTC_TAMPER_1
1552   *         @arg RTC_TAMPER_2
1553   *         @arg RTC_TAMPER_3
1554   *         @arg RTC_TAMPER_4
1555   *         @arg RTC_TAMPER_5
1556   *         @arg RTC_TAMPER_6
1557   *         @arg RTC_TAMPER_7
1558   *         @arg RTC_TAMPER_8
1559   * @retval HAL status
1560   */
HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc,uint32_t Tamper)1561 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
1562 {
1563   UNUSED(hrtc);
1564   assert_param(IS_RTC_TAMPER(Tamper));
1565 
1566   /* Disable the selected Tamper pin */
1567   CLEAR_BIT(TAMP->CR1, Tamper);
1568 
1569   /* Clear tamper mask/noerase/trigger configuration */
1570   CLEAR_BIT(TAMP->CR2, (Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | (Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1571 
1572   /* Clear tamper interrupt mode configuration */
1573   CLEAR_BIT(TAMP->IER, Tamper);
1574 
1575   /* Clear tamper interrupt and event flags (WO register) */
1576   WRITE_REG(TAMP->SCR, Tamper);
1577 
1578   return HAL_OK;
1579 }
1580 
1581 /**
1582   * @brief  Set all active Tampers at the same time.
1583   * @param  hrtc RTC handle
1584   * @param  sAllTamper Pointer to active Tamper Structure.
1585   * @retval HAL status
1586   */
HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef * hrtc,RTC_ActiveTampersTypeDef * sAllTamper)1587 HAL_StatusTypeDef HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef *hrtc, RTC_ActiveTampersTypeDef *sAllTamper)
1588 {
1589   uint32_t IER, CR1, CR2, ATCR1, CR, i, tickstart;
1590 
1591 #ifdef  USE_FULL_ASSERT
1592   for (i = 0; i < RTC_TAMP_NB; i++)
1593   {
1594     assert_param(IS_RTC_TAMPER_ERASE_MODE(sAllTamper->TampInput[i].NoErase));
1595     assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sAllTamper->TampInput[i].MaskFlag));
1596     /* Mask flag only supported by TAMPER 1, 2 and 3 */
1597     assert_param(!((sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (i > RTC_TAMPER_3)));
1598   }
1599   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sAllTamper->TimeStampOnTamperDetection));
1600 #endif /* #ifdef  USE_FULL_ASSERT */
1601 
1602   /* Active Tampers must not be already enabled */
1603   if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) != 0U)
1604   {
1605     /* Disable all actives tampers with HAL_RTCEx_DeactivateActiveTampers and try again */
1606     return HAL_ERROR;
1607   }
1608 
1609   /* Set TimeStamp on tamper detection */
1610   CR = READ_REG(RTC->CR);
1611   if ((CR & RTC_CR_TAMPTS) != (sAllTamper->TimeStampOnTamperDetection))
1612   {
1613     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1614     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sAllTamper->TimeStampOnTamperDetection);
1615     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1616   }
1617 
1618   CR1 = READ_REG(TAMP->CR1);
1619   CR2 = READ_REG(TAMP->CR2);
1620   IER = READ_REG(TAMP->IER);
1621 
1622   /* Set common parameters */
1623   ATCR1 = (sAllTamper->ActiveFilter | (sAllTamper->ActiveOutputChangePeriod << TAMP_ATCR1_ATPER_Pos) | sAllTamper->ActiveAsyncPrescaler);
1624 
1625   /* Set specific parameters for each active tamper inputs if enable */
1626   for (i = 0; i < RTC_TAMP_NB; i++)
1627   {
1628     if (sAllTamper->TampInput[i].Enable != RTC_ATAMP_DISABLE)
1629     {
1630       CR1 |= (TAMP_CR1_TAMP1E << i);
1631       ATCR1 |= (TAMP_ATCR1_TAMP1AM << i);
1632 
1633       if (sAllTamper->TampInput[i].Interrupt != RTC_ATAMP_INTERRUPT_DISABLE)
1634       {
1635         /* RTC Tamper Interrupt Configuration: EXTI configuration */
1636         __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1637 
1638         /* Interrupt enable register */
1639         IER |= (TAMP_IER_TAMP1IE << i);
1640       }
1641 
1642       if (sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1643       {
1644         CR2 |= (TAMP_CR2_TAMP1MSK << i);
1645       }
1646 
1647       if (sAllTamper->TampInput[i].NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1648       {
1649         CR2 |= (TAMP_CR2_TAMP1NOERASE << i);
1650       }
1651 
1652       if (i != sAllTamper->TampInput[i].Output)
1653       {
1654         ATCR1 |= TAMP_ATCR1_ATOSHARE;
1655       }
1656     }
1657   }
1658 
1659   WRITE_REG(TAMP->IER, IER);
1660   WRITE_REG(TAMP->IER, IER);
1661   WRITE_REG(TAMP->ATCR1, ATCR1);
1662   WRITE_REG(TAMP->CR2, CR2);
1663   WRITE_REG(TAMP->CR1, CR1);
1664 
1665   /* Write seed */
1666   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1667   {
1668     WRITE_REG(TAMP->ATSEEDR, sAllTamper->Seed[i]);
1669   }
1670 
1671   /* Wait till RTC SEEDF flag is set and if Time out is reached exit */
1672   tickstart = HAL_GetTick();
1673   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0u)
1674   {
1675     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1676     {
1677       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1678       return HAL_TIMEOUT;
1679     }
1680   }
1681 
1682   return HAL_OK;
1683 }
1684 
1685 /**
1686   * @brief  Write a new seed. Active tamper must be enabled.
1687   * @param  hrtc RTC handle
1688   * @param  pSeed Pointer to active tamper seed values.
1689   * @retval HAL status
1690   */
HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef * hrtc,uint32_t * pSeed)1691 HAL_StatusTypeDef HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef *hrtc, uint32_t *pSeed)
1692 {
1693   uint32_t i, tickstart;
1694 
1695   /* Active Tampers must be enabled */
1696   if (READ_BIT(TAMP->ATOR,  TAMP_ATOR_INITS) == 0U)
1697   {
1698     return HAL_ERROR;
1699   }
1700 
1701   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1702   {
1703     WRITE_REG(TAMP->ATSEEDR, pSeed[i]);
1704   }
1705 
1706   /* Wait till RTC SEEDF flag is set and if Time out is reached exit */
1707   tickstart = HAL_GetTick();
1708   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0U)
1709   {
1710     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1711     {
1712       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1713       return HAL_TIMEOUT;
1714     }
1715   }
1716 
1717   return HAL_OK;
1718 }
1719 
1720 /**
1721   * @brief  Deactivate all Active Tampers at the same time.
1722   * @param  hrtc RTC handle
1723   * @retval HAL status
1724   */
HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef * hrtc)1725 HAL_StatusTypeDef HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef *hrtc)
1726 {
1727   /* Get Active tampers */
1728   uint32_t ATamp_mask = READ_BIT(TAMP->ATCR1, TAMP_ALL);
1729 
1730   UNUSED(hrtc);
1731   /* Disable all actives tampers but not passives tampers */
1732   CLEAR_BIT(TAMP->CR1, ATamp_mask);
1733   /* Disable no erase and mask */
1734   CLEAR_BIT(TAMP->CR2, (ATamp_mask | ((ATamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM | TAMP_ATCR1_TAMP3AM)) << TAMP_CR2_TAMP1MSK_Pos)));
1735 
1736   /* Clear tamper interrupt and event flags (WO register) of all actives tampers but not passives tampers */
1737   WRITE_REG(TAMP->SCR, ATamp_mask);
1738 
1739   /* Clear all active tampers interrupt mode configuration but not passives tampers */
1740   CLEAR_BIT(TAMP->IER, ATamp_mask);
1741 
1742   CLEAR_BIT(TAMP->ATCR1, TAMP_ALL | TAMP_ATCR1_ATCKSEL | TAMP_ATCR1_ATPER | \
1743             TAMP_ATCR1_ATOSHARE | TAMP_ATCR1_FLTEN);
1744 
1745   return HAL_OK;
1746 }
1747 
1748 
1749 /**
1750   * @brief  Tamper event polling.
1751   * @param  hrtc RTC handle
1752   * @param  Tamper Selected tamper pin.
1753   *         This parameter can be a combination of the following values:
1754   *         @arg RTC_TAMPER_1
1755   *         @arg RTC_TAMPER_2
1756   *         @arg RTC_TAMPER_3
1757   *         @arg RTC_TAMPER_4
1758   *         @arg RTC_TAMPER_5
1759   *         @arg RTC_TAMPER_6
1760   *         @arg RTC_TAMPER_7
1761   *         @arg RTC_TAMPER_8
1762   * @param  Timeout Timeout duration
1763   * @retval HAL status
1764   */
HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t Tamper,uint32_t Timeout)1765 HAL_StatusTypeDef HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t Tamper, uint32_t Timeout)
1766 {
1767   UNUSED(hrtc);
1768   assert_param(IS_RTC_TAMPER(Tamper));
1769 
1770   uint32_t tickstart = HAL_GetTick();
1771 
1772   /* Get the status of the Interrupt */
1773   while (READ_BIT(TAMP->SR, Tamper) != Tamper)
1774   {
1775     if (Timeout != HAL_MAX_DELAY)
1776     {
1777       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1778       {
1779         return HAL_TIMEOUT;
1780       }
1781     }
1782   }
1783 
1784   /* Clear the Tamper Flag */
1785   WRITE_REG(TAMP->SCR, Tamper);
1786 
1787   return HAL_OK;
1788 }
1789 
1790 
1791 /**
1792   * @brief  Set Internal Tamper in interrupt mode
1793   * @param  hrtc RTC handle
1794   * @param  sIntTamper Pointer to Internal Tamper Structure.
1795   * @retval HAL status
1796   */
HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef * hrtc,RTC_InternalTamperTypeDef * sIntTamper)1797 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
1798 {
1799   /* Prevent unused argument(s) compilation warning */
1800   UNUSED(hrtc);
1801 
1802   /* Check the parameters */
1803   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
1804   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
1805   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
1806 
1807   /* timestamp on internal tamper */
1808   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
1809   {
1810     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1811     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
1812     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1813   }
1814 
1815   /* Control register 1 */
1816   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1817 
1818   return HAL_OK;
1819 }
1820 
1821 
1822 /**
1823   * @brief  Set Internal Tamper
1824   * @param  hrtc RTC handle
1825   * @param  sIntTamper Pointer to Internal Tamper Structure.
1826   * @retval HAL status
1827   */
HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef * hrtc,RTC_InternalTamperTypeDef * sIntTamper)1828 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
1829 {
1830   /* Prevent unused argument(s) compilation warning */
1831   UNUSED(hrtc);
1832 
1833   /* Check the parameters */
1834   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
1835   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
1836   assert_param(IS_RTC_TAMPER_ERASE_MODE(sIntTamper->NoErase));
1837 
1838   /* timestamp on internal tamper */
1839   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
1840   {
1841     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1842     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
1843     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1844   }
1845 
1846   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1847   __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
1848 
1849   /* Interrupt enable register */
1850   SET_BIT(TAMP->IER, sIntTamper->IntTamper);
1851 
1852   /* Control register 1 */
1853   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1854 
1855   return HAL_OK;
1856 }
1857 
1858 /**
1859   * @brief  Deactivate Internal Tamper.
1860   * @param  hrtc RTC handle
1861   * @param  IntTamper Selected internal tamper event.
1862   *          This parameter can be any combination of existing internal tampers.
1863   * @retval HAL status
1864   */
HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef * hrtc,uint32_t IntTamper)1865 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
1866 {
1867   UNUSED(hrtc);
1868   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1869 
1870   /* Disable the selected Tamper pin */
1871   CLEAR_BIT(TAMP->CR1, IntTamper);
1872 
1873   /* Clear internal tamper interrupt mode configuration */
1874   CLEAR_BIT(TAMP->IER, IntTamper);
1875 
1876   /* Clear internal tamper interrupt */
1877   WRITE_REG(TAMP->SCR, IntTamper);
1878 
1879   return HAL_OK;
1880 }
1881 
1882 
1883 /**
1884   * @brief  Internal Tamper event polling.
1885   * @param  hrtc RTC handle
1886   * @param  IntTamper selected tamper.
1887   *          This parameter can be any combination of existing internal tampers.
1888   * @param  Timeout Timeout duration
1889   * @retval HAL status
1890   */
HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef * hrtc,uint32_t IntTamper,uint32_t Timeout)1891 HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t IntTamper, uint32_t Timeout)
1892 {
1893   UNUSED(hrtc);
1894   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1895 
1896   uint32_t tickstart = HAL_GetTick();
1897 
1898   /* Get the status of the Interrupt */
1899   while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
1900   {
1901     if (Timeout != HAL_MAX_DELAY)
1902     {
1903       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1904       {
1905         return HAL_TIMEOUT;
1906       }
1907     }
1908   }
1909 
1910   /* Clear the Tamper Flag */
1911   WRITE_REG(TAMP->SCR, IntTamper);
1912 
1913   return HAL_OK;
1914 }
1915 
1916 
1917 #if defined (CORTEX_IN_SECURE_STATE)
1918 /**
1919   * @brief  Handle Tamper secure interrupt request.
1920   * @param  hrtc RTC handle
1921   * @retval None
1922   */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)1923 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
1924 {
1925   uint32_t tmp;
1926 
1927   /* Get secure interrupt status */
1928   tmp = READ_REG(TAMP->SMISR);
1929 
1930   /* Immediatly clear flags */
1931   WRITE_REG(TAMP->SCR, tmp);
1932 
1933   /* Check Tamper1 status */
1934   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
1935   {
1936 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1937     /* Call Tamper 1 Event registered secure Callback */
1938     hrtc->Tamper1EventCallback(hrtc);
1939 #else
1940     /* Tamper1 secure callback */
1941     HAL_RTCEx_Tamper1EventCallback(hrtc);
1942 #endif
1943   }
1944 
1945   /* Check Tamper2 status */
1946   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
1947   {
1948 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1949     /* Call Tamper 2 Event registered secure Callback */
1950     hrtc->Tamper2EventCallback(hrtc);
1951 #else
1952     /* Tamper2 secure callback */
1953     HAL_RTCEx_Tamper2EventCallback(hrtc);
1954 #endif
1955   }
1956 
1957   /* Check Tamper3 status */
1958   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
1959   {
1960 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1961     /* Call Tamper 3 Event registered secure Callback */
1962     hrtc->Tamper3EventCallback(hrtc);
1963 #else
1964     /* Tamper3 secure callback */
1965     HAL_RTCEx_Tamper3EventCallback(hrtc);
1966 #endif
1967   }
1968 
1969   /* Check Internal Tamper1 status */
1970   if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
1971   {
1972 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1973     /* Call Internal Tamper 1 Event registered secure Callback */
1974     hrtc->InternalTamper1EventCallback(hrtc);
1975 #else
1976     /* Internal Tamper1 secure callback */
1977     HAL_RTCEx_InternalTamper1EventCallback(hrtc);
1978 #endif
1979   }
1980 
1981   /* Check Internal Tamper2 status */
1982   if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
1983   {
1984 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1985     /* Call Internal Tamper 2 Event registered secure Callback */
1986     hrtc->InternalTamper2EventCallback(hrtc);
1987 #else
1988     /* Internal Tamper2 secure callback */
1989     HAL_RTCEx_InternalTamper2EventCallback(hrtc);
1990 #endif
1991   }
1992 
1993   /* Check Internal Tamper3 status */
1994   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
1995   {
1996 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1997     /* Call Internal Tamper 3 Event registered secure Callback */
1998     hrtc->InternalTamper3EventCallback(hrtc);
1999 #else
2000     /* Internal Tamper3 secure callback */
2001     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
2002 #endif
2003   }
2004 
2005   /* Check Internal Tamper4 status */
2006   if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
2007   {
2008 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2009     /* Call Internal Tamper 4 Event registered Callback */
2010     hrtc->InternalTamper4EventCallback(hrtc);
2011 #else
2012     /* Internal Tamper4 callback */
2013     HAL_RTCEx_InternalTamper4EventCallback(hrtc);
2014 #endif
2015   }
2016 
2017   /* Check Internal Tamper5 status */
2018   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
2019   {
2020 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2021     /* Call Internal Tamper 5 Event registered secure Callback */
2022     hrtc->InternalTamper5EventCallback(hrtc);
2023 #else
2024     /* Internal Tamper5 secure callback */
2025     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
2026 #endif
2027   }
2028 
2029   /* Check Internal Tamper8 status */
2030   if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
2031   {
2032 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2033     /* Call Internal Tamper 8 Event registered secure Callback */
2034     hrtc->InternalTamper8EventCallback(hrtc);
2035 #else
2036     /* Internal Tamper8 secure callback */
2037     HAL_RTCEx_InternalTamper8EventCallback(hrtc);
2038 #endif
2039   }
2040 }
2041 
2042 #else /* #if defined (CORTEX_IN_SECURE_STATE) */
2043 
2044 /**
2045   * @brief  Handle Tamper non-secure interrupt request.
2046   * @param  hrtc RTC handle
2047   * @retval None
2048   */
HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef * hrtc)2049 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
2050 {
2051   /* Get interrupt status */
2052   uint32_t tmp = READ_REG(TAMP->MISR);
2053 
2054   /* Immediately clear flags */
2055   WRITE_REG(TAMP->SCR, tmp);
2056 
2057   /* Check Tamper1 status */
2058   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
2059   {
2060 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2061     /* Call Tamper 1 Event registered Callback */
2062     hrtc->Tamper1EventCallback(hrtc);
2063 #else
2064     /* Tamper1 callback */
2065     HAL_RTCEx_Tamper1EventCallback(hrtc);
2066 #endif
2067   }
2068 
2069   /* Check Tamper2 status */
2070   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
2071   {
2072 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2073     /* Call Tamper 2 Event registered Callback */
2074     hrtc->Tamper2EventCallback(hrtc);
2075 #else
2076     /* Tamper2 callback */
2077     HAL_RTCEx_Tamper2EventCallback(hrtc);
2078 #endif
2079   }
2080 
2081   /* Check Tamper3 status */
2082   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
2083   {
2084 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2085     /* Call Tamper 3 Event registered Callback */
2086     hrtc->Tamper3EventCallback(hrtc);
2087 #else
2088     /* Tamper3 callback */
2089     HAL_RTCEx_Tamper3EventCallback(hrtc);
2090 #endif
2091   }
2092 
2093   /* Check Internal Tamper1 status */
2094   if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
2095   {
2096 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2097     /* Call Internal Tamper 1 Event registered Callback */
2098     hrtc->InternalTamper1EventCallback(hrtc);
2099 #else
2100     /* Internal Tamper1 callback */
2101     HAL_RTCEx_InternalTamper1EventCallback(hrtc);
2102 #endif
2103   }
2104 
2105   /* Check Internal Tamper2 status */
2106   if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
2107   {
2108 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2109     /* Call Internal Tamper 2 Event registered Callback */
2110     hrtc->InternalTamper2EventCallback(hrtc);
2111 #else
2112     /* Internal Tamper2 callback */
2113     HAL_RTCEx_InternalTamper2EventCallback(hrtc);
2114 #endif
2115   }
2116 
2117   /* Check Internal Tamper3 status */
2118   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
2119   {
2120 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2121     /* Call Internal Tamper 3 Event registered Callback */
2122     hrtc->InternalTamper3EventCallback(hrtc);
2123 #else
2124     /* Internal Tamper3 callback */
2125     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
2126 #endif
2127   }
2128 
2129   /* Check Internal Tamper4 status */
2130   if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
2131   {
2132 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2133     /* Call Internal Tamper 4 Event registered Callback */
2134     hrtc->InternalTamper4EventCallback(hrtc);
2135 #else
2136     /* Internal Tamper4 callback */
2137     HAL_RTCEx_InternalTamper4EventCallback(hrtc);
2138 #endif
2139   }
2140 
2141   /* Check Internal Tamper5 status */
2142   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
2143   {
2144 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2145     /* Call Internal Tamper 5 Event registered Callback */
2146     hrtc->InternalTamper5EventCallback(hrtc);
2147 #else
2148     /* Internal Tamper5 callback */
2149     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
2150 #endif
2151   }
2152 
2153   /* Check Internal Tamper8 status */
2154   if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
2155   {
2156 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2157     /* Call Internal Tamper 8 Event registered Callback */
2158     hrtc->InternalTamper8EventCallback(hrtc);
2159 #else
2160     /* Internal Tamper8 callback */
2161     HAL_RTCEx_InternalTamper8EventCallback(hrtc);
2162 #endif
2163   }
2164 }
2165 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
2166 
2167 /**
2168   * @brief  Tamper 1 callback.
2169   * @param  hrtc RTC handle
2170   * @retval None
2171   */
HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)2172 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
2173 {
2174   /* Prevent unused argument(s) compilation warning */
2175   UNUSED(hrtc);
2176 
2177   /* NOTE : This function should not be modified, when the callback is needed,
2178             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
2179    */
2180 }
2181 
2182 /**
2183   * @brief  Tamper 2 callback.
2184   * @param  hrtc RTC handle
2185   * @retval None
2186   */
HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)2187 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
2188 {
2189   /* Prevent unused argument(s) compilation warning */
2190   UNUSED(hrtc);
2191 
2192   /* NOTE : This function should not be modified, when the callback is needed,
2193             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
2194    */
2195 }
2196 
2197 /**
2198   * @brief  Tamper 3 callback.
2199   * @param  hrtc RTC handle
2200   * @retval None
2201   */
HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)2202 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
2203 {
2204   /* Prevent unused argument(s) compilation warning */
2205   UNUSED(hrtc);
2206 
2207   /* NOTE : This function should not be modified, when the callback is needed,
2208             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
2209    */
2210 }
2211 
2212 
2213 /**
2214   * @brief  Internal Tamper 1 callback.
2215   * @param  hrtc RTC handle
2216   * @retval None
2217   */
HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef * hrtc)2218 __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
2219 {
2220   /* Prevent unused argument(s) compilation warning */
2221   UNUSED(hrtc);
2222 
2223   /* NOTE : This function should not be modified, when the callback is needed,
2224             the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
2225    */
2226 }
2227 
2228 /**
2229   * @brief  Internal Tamper 2 callback.
2230   * @param  hrtc RTC handle
2231   * @retval None
2232   */
HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef * hrtc)2233 __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
2234 {
2235   /* Prevent unused argument(s) compilation warning */
2236   UNUSED(hrtc);
2237 
2238   /* NOTE : This function should not be modified, when the callback is needed,
2239             the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
2240    */
2241 }
2242 
2243 /**
2244   * @brief  Internal Tamper 3 callback.
2245   * @param  hrtc RTC handle
2246   * @retval None
2247   */
HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef * hrtc)2248 __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
2249 {
2250   /* Prevent unused argument(s) compilation warning */
2251   UNUSED(hrtc);
2252 
2253   /* NOTE : This function should not be modified, when the callback is needed,
2254             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
2255    */
2256 }
2257 
2258 /**
2259   * @brief  Internal Tamper 4 callback.
2260   * @param  hrtc RTC handle
2261   * @retval None
2262   */
HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef * hrtc)2263 __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
2264 {
2265   /* Prevent unused argument(s) compilation warning */
2266   UNUSED(hrtc);
2267 
2268   /* NOTE : This function should not be modified, when the callback is needed,
2269             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
2270    */
2271 }
2272 
2273 /**
2274   * @brief  Internal Tamper 5 callback.
2275   * @param  hrtc RTC handle
2276   * @retval None
2277   */
HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef * hrtc)2278 __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
2279 {
2280   /* Prevent unused argument(s) compilation warning */
2281   UNUSED(hrtc);
2282 
2283   /* NOTE : This function should not be modified, when the callback is needed,
2284             the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
2285    */
2286 }
2287 
2288 
2289 /**
2290   * @brief  Internal Tamper 8 callback.
2291   * @param  hrtc RTC handle
2292   * @retval None
2293   */
HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef * hrtc)2294 __weak void HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef *hrtc)
2295 {
2296   /* Prevent unused argument(s) compilation warning */
2297   UNUSED(hrtc);
2298 
2299   /* NOTE : This function should not be modified, when the callback is needed,
2300             the HAL_RTCEx_InternalTamper8EventCallback could be implemented in the user file
2301    */
2302 }
2303 /**
2304   * @}
2305   */
2306 
2307 
2308 /** @addtogroup RTCEx_Exported_Functions_Group6
2309   * @brief      Extended RTC Backup register functions
2310   *
2311 @verbatim
2312   ===============================================================================
2313              ##### Extended RTC Backup register functions #####
2314   ===============================================================================
2315   [..]
2316    (+) Before calling any tamper or internal tamper function, you have to call first
2317        HAL_RTC_Init() function.
2318    (+) In that ine you can select to output tamper event on RTC pin.
2319   [..]
2320    This subsection provides functions allowing to
2321    (+) Write a data in a specified RTC Backup data register
2322    (+) Read a data in a specified RTC Backup data register
2323 @endverbatim
2324   * @{
2325   */
2326 
2327 
2328 /**
2329   * @brief  Write a data in a specified RTC Backup data register.
2330   * @param  hrtc RTC handle
2331   * @param  BackupRegister RTC Backup data Register number.
2332   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2333   * @param  Data Data to be written in the specified Backup data register.
2334   * @retval None
2335   */
HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister,uint32_t Data)2336 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
2337 {
2338   uint32_t tmp;
2339 
2340   UNUSED(hrtc);
2341   /* Check the parameters */
2342   assert_param(IS_RTC_BKP(BackupRegister));
2343 
2344   tmp = (uint32_t) & (TAMP->BKP0R);
2345   tmp += (BackupRegister * 4U);
2346 
2347   /* Write the specified register */
2348   *(__IO uint32_t *)tmp = (uint32_t)Data;
2349 }
2350 
2351 
2352 /**
2353   * @brief  Reads data from the specified RTC Backup data Register.
2354   * @param  hrtc RTC handle
2355   * @param  BackupRegister RTC Backup data Register number.
2356   *          This parameter can be RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB
2357   * @retval Read value
2358   */
HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc,uint32_t BackupRegister)2359 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
2360 {
2361   uint32_t tmp;
2362 
2363   UNUSED(hrtc);
2364   /* Check the parameters */
2365   assert_param(IS_RTC_BKP(BackupRegister));
2366 
2367   tmp = (uint32_t) & (TAMP->BKP0R);
2368   tmp += (BackupRegister * 4U);
2369 
2370   /* Read the specified register */
2371   return (*(__IO uint32_t *)tmp);
2372 }
2373 
2374 /**
2375   * @}
2376   */
2377 
2378 
2379 /** @addtogroup RTCEx_Exported_Functions_Group7
2380   * @brief      Extended RTC security functions
2381   *
2382 @verbatim
2383   ===============================================================================
2384              ##### Extended RTC security functions #####
2385   ===============================================================================
2386   [..]
2387    (+) Before calling security function, you have to call first
2388        HAL_RTC_Init() function.
2389 @endverbatim
2390   * @{
2391   */
2392 
2393 /**
2394   * @brief  Get the security level of the RTC.
2395   *         To set the secure level please call HAL_RTCEx_SecureModeSet.
2396   * @param  hrtc RTC handle
2397   * @param  secureState  Secure state
2398   * @retval HAL_StatusTypeDef
2399   */
HAL_RTCEx_SecureModeGet(RTC_HandleTypeDef * hrtc,RTC_SecureStateTypeDef * secureState)2400 HAL_StatusTypeDef HAL_RTCEx_SecureModeGet(RTC_HandleTypeDef *hrtc, RTC_SecureStateTypeDef  *secureState)
2401 {
2402   UNUSED(hrtc);
2403   /* Read registers */
2404   uint32_t rtc_smcr = READ_REG(RTC->SMCR);
2405   uint32_t tamp_smcr = READ_REG(TAMP->SMCR);
2406 
2407   /* RTC */
2408   secureState->rtcSecureFull = READ_BIT(rtc_smcr, RTC_SMCR_DECPROT);
2409 
2410   /* Warning, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2411   secureState->rtcNonSecureFeatures = READ_BIT(rtc_smcr, RTC_NONSECURE_FEATURE_ALL);
2412 
2413   /* TAMP */
2414   secureState->tampSecureFull = READ_BIT(tamp_smcr, TAMP_SMCR_TAMPDPROT);
2415 
2416   /* Backup register start zones
2417      Warning : Backup register start zones are shared with privilege configuration */
2418   secureState->backupRegisterStartZone2 = READ_BIT(tamp_smcr, TAMP_SMCR_BKPRWDPROT) >> TAMP_SMCR_BKPRWDPROT_Pos;
2419   secureState->backupRegisterStartZone3 = READ_BIT(tamp_smcr, TAMP_SMCR_BKPWDPROT) >> TAMP_SMCR_BKPWDPROT_Pos;
2420 
2421   return HAL_OK;
2422 }
2423 
2424 
2425 #if defined (CORTEX_IN_SECURE_STATE)
2426 /**
2427   * @brief  Set the security level of the RTC/TAMP/Backup registers.
2428   *         To get the current security level call HAL_RTCEx_SecureModeGet.
2429   * @param  hrtc RTC handle
2430   * @param  secureState  Secure state
2431   * @retval HAL_StatusTypeDef
2432   */
HAL_RTCEx_SecureModeSet(RTC_HandleTypeDef * hrtc,RTC_SecureStateTypeDef * secureState)2433 HAL_StatusTypeDef HAL_RTCEx_SecureModeSet(RTC_HandleTypeDef *hrtc, RTC_SecureStateTypeDef  *secureState)
2434 {
2435   UNUSED(hrtc);
2436   assert_param(IS_RTC_SECURE_FULL(secureState->rtcSecureFull));
2437   assert_param(IS_RTC_NONSECURE_FEATURES(secureState->rtcNonSecureFeatures));
2438   assert_param(IS_TAMP_SECURE_FULL(secureState->tampSecureFull));
2439   assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone2));
2440   assert_param(IS_RTC_BKP(secureState->backupRegisterStartZone3));
2441 
2442   /* RTC, rtcNonSecureFeatures is only relevant if secureState->rtcSecureFull == RTC_SECURE_FULL_NO */
2443   WRITE_REG(RTC->SMCR, secureState->rtcSecureFull | secureState->rtcNonSecureFeatures);
2444 
2445   /* Tamper + Backup register
2446      Warning : Backup register start zone are Shared with privilege configuration */
2447   WRITE_REG(TAMP->SMCR,
2448             secureState->tampSecureFull |
2449             (TAMP_SMCR_BKPRWDPROT & (secureState->backupRegisterStartZone2 << TAMP_SMCR_BKPRWDPROT_Pos)) |
2450             (TAMP_SMCR_BKPWDPROT & (secureState->backupRegisterStartZone3 << TAMP_SMCR_BKPWDPROT_Pos)));
2451 
2452   return HAL_OK;
2453 }
2454 #endif /* #if defined (CORTEX_IN_SECURE_STATE) */
2455 
2456 /**
2457   * @}
2458   */
2459 
2460 /**
2461   * @}
2462   */
2463 #endif /* HAL_RTC_MODULE_ENABLED */
2464 
2465 /**
2466   * @}
2467   */
2468 
2469 
2470 /**
2471   * @}
2472   */
2473 
2474 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2475