1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_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      (+) STM32L412xx and STM32L422xx only : With new RTC tamper configuration, you have to call HAL_RTC_Init() in order to
80          perform TAMP base address offset calculation.
81      (+) STM32L412xx and STM32L422xx only : If you don't intend to have tamper using RTC clock, you can bypass its initialization
82          by setting ClockEnable inti field to RTC_CLOCK_DISABLE.
83      (+) STM32L412xx and STM32L422xx only : 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      (+) STM32L412xx and STM32L422xx only : Before calling these 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) 2017 STMicroelectronics</center></h2>
101   *
102   * Redistribution and use in source and binary forms, with or without modification,
103   * are permitted provided that the following conditions are met:
104   *   1. Redistributions of source code must retain the above copyright notice,
105   *      this list of conditions and the following disclaimer.
106   *   2. Redistributions in binary form must reproduce the above copyright notice,
107   *      this list of conditions and the following disclaimer in the documentation
108   *      and/or other materials provided with the distribution.
109   *   3. Neither the name of STMicroelectronics nor the names of its contributors
110   *      may be used to endorse or promote products derived from this software
111   *      without specific prior written permission.
112   *
113   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
114   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
115   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
116   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
117   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
118   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
119   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
120   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
121   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
122   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123   *
124   ******************************************************************************
125   */
126 
127 /* Includes ------------------------------------------------------------------*/
128 #include "stm32l4xx_hal.h"
129 
130 /** @addtogroup STM32L4xx_HAL_Driver
131   * @{
132   */
133 
134 /** @addtogroup RTCEx
135   * @brief RTC Extended HAL module driver
136   * @{
137   */
138 
139 #ifdef HAL_RTC_MODULE_ENABLED
140 
141 /* Private typedef -----------------------------------------------------------*/
142 /* Private define ------------------------------------------------------------*/
143 #if defined(STM32L412xx) || defined(STM32L422xx)
144 #else
145 #if defined(RTC_TAMPER1_SUPPORT) && defined(RTC_TAMPER3_SUPPORT)
146 #define RTC_TAMPCR_MASK               ((uint32_t)RTC_TAMPCR_TAMPTS    |\
147                                        (uint32_t)RTC_TAMPCR_TAMPFREQ  | (uint32_t)RTC_TAMPCR_TAMPFLT      | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
148                                        (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE                                       |\
149                                        (uint32_t)RTC_TAMPCR_TAMP1IE   | (uint32_t)RTC_TAMPCR_TAMP1NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF  |\
150                                        (uint32_t)RTC_TAMPCR_TAMP2IE   | (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP2MF  |\
151                                        (uint32_t)RTC_TAMPCR_TAMP3IE   | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP3MF)
152 #elif defined(RTC_TAMPER1_SUPPORT)
153 #define RTC_TAMPCR_MASK               ((uint32_t)RTC_TAMPCR_TAMPTS    |\
154                                        (uint32_t)RTC_TAMPCR_TAMPFREQ  | (uint32_t)RTC_TAMPCR_TAMPFLT      | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
155                                        (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE                                       |\
156                                        (uint32_t)RTC_TAMPCR_TAMP1IE   | (uint32_t)RTC_TAMPCR_TAMP1NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF  |\
157                                        (uint32_t)RTC_TAMPCR_TAMP2IE   | (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP2MF)
158 #elif defined(RTC_TAMPER3_SUPPORT)
159 #define RTC_TAMPCR_MASK               ((uint32_t)RTC_TAMPCR_TAMPTS    |\
160                                        (uint32_t)RTC_TAMPCR_TAMPFREQ  | (uint32_t)RTC_TAMPCR_TAMPFLT      | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
161                                        (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE                                       |\
162                                        (uint32_t)RTC_TAMPCR_TAMP2IE   | (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP2MF  |\
163                                        (uint32_t)RTC_TAMPCR_TAMP3IE   | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP3MF)
164 #else
165 #define RTC_TAMPCR_MASK               ((uint32_t)RTC_TAMPCR_TAMPTS    |\
166                                        (uint32_t)RTC_TAMPCR_TAMPFREQ  | (uint32_t)RTC_TAMPCR_TAMPFLT      | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
167                                        (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE                                       |\
168                                        (uint32_t)RTC_TAMPCR_TAMP2IE   | (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP2MF)
169 #endif /* RTC_TAMPER1_SUPPORT && RTC_TAMPER3_SUPPORT */
170 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
171 
172 /* Private macro -------------------------------------------------------------*/
173 /* Private variables ---------------------------------------------------------*/
174 /* Private function prototypes -----------------------------------------------*/
175 /* Exported functions --------------------------------------------------------*/
176 
177 /** @addtogroup RTCEx_Exported_Functions
178   * @{
179   */
180 
181 
182 /** @addtogroup RTCEx_Exported_Functions_Group1
183   * @brief   RTC TimeStamp and Tamper functions
184   *
185 @verbatim
186  ===============================================================================
187                  ##### RTC TimeStamp and Tamper functions #####
188  ===============================================================================
189 
190  [..] This section provides functions allowing to configure TimeStamp feature
191 
192 @endverbatim
193   * @{
194   */
195 
196 /**
197   * @brief  Set TimeStamp.
198   * @note   This API must be called before enabling the TimeStamp feature.
199   * @param  hrtc RTC handle
200   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
201   *         activated.
202   *          This parameter can be one of the following values:
203   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
204   *                                        rising edge of the related pin.
205   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
206   *                                         falling edge of the related pin.
207   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
208   *          This parameter can be one of the following values:
209   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
210   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
211   *               compatibility, this parameter is required.
212   * @retval HAL status
213   */
HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)214 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
215 {
216   uint32_t tmpreg;
217 
218   /* Check the parameters */
219   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
220   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
221 
222   /* Prevent unused argument(s) compilation warning if no assert_param check */
223   UNUSED(RTC_TimeStampPin);
224 
225   /* Process Locked */
226   __HAL_LOCK(hrtc);
227 
228   hrtc->State = HAL_RTC_STATE_BUSY;
229 
230   /* Get the RTC_CR register and clear the bits to be configured */
231   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
232 
233   tmpreg |= TimeStampEdge;
234 
235   /* Disable the write protection for RTC registers */
236   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
237 
238   /* Configure the Time Stamp TSEDGE and Enable bits */
239   hrtc->Instance->CR = (uint32_t)tmpreg;
240 
241   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
242 
243   /* Enable the write protection for RTC registers */
244   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
245 
246   /* Change RTC state */
247   hrtc->State = HAL_RTC_STATE_READY;
248 
249   /* Process Unlocked */
250   __HAL_UNLOCK(hrtc);
251 
252   return HAL_OK;
253 }
254 
255 /**
256   * @brief  Set TimeStamp with Interrupt.
257   * @note   This API must be called before enabling the TimeStamp feature.
258   * @param  hrtc RTC handle
259   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
260   *         activated.
261   *          This parameter can be one of the following values:
262   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
263   *                                        rising edge of the related pin.
264   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
265   *                                         falling edge of the related pin.
266   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
267   *          This parameter can be one of the following values:
268   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
269   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
270   *               compatibility, this parameter is required.
271   * @retval HAL status
272   */
HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef * hrtc,uint32_t TimeStampEdge,uint32_t RTC_TimeStampPin)273 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
274 {
275   uint32_t tmpreg;
276 
277   /* Check the parameters */
278   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
279   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
280 
281   /* Prevent unused argument(s) compilation warning if no assert_param check */
282   UNUSED(RTC_TimeStampPin);
283 
284   /* Process Locked */
285   __HAL_LOCK(hrtc);
286 
287   hrtc->State = HAL_RTC_STATE_BUSY;
288 
289   /* Get the RTC_CR register and clear the bits to be configured */
290   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
291 
292   tmpreg |= TimeStampEdge;
293 
294   /* Disable the write protection for RTC registers */
295   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
296 
297   /* Configure the Time Stamp TSEDGE and Enable bits */
298   hrtc->Instance->CR = (uint32_t)tmpreg;
299 
300   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
301 
302   /* Enable IT timestamp */
303   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
304 
305   /* RTC timestamp Interrupt Configuration: EXTI configuration */
306   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
307   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
308 
309   /* Enable the write protection for RTC registers */
310   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
311 
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 TimeStamp.
322   * @param  hrtc RTC handle
323   * @retval HAL status
324   */
HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef * hrtc)325 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
326 {
327   uint32_t tmpreg;
328 
329   /* Process Locked */
330   __HAL_LOCK(hrtc);
331 
332   hrtc->State = HAL_RTC_STATE_BUSY;
333 
334   /* Disable the write protection for RTC registers */
335   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
336 
337   /* In case of interrupt mode is used, the interrupt source must disabled */
338   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
339 
340   /* Get the RTC_CR register and clear the bits to be configured */
341   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
342 
343   /* Configure the Time Stamp TSEDGE and Enable bits */
344   hrtc->Instance->CR = (uint32_t)tmpreg;
345 
346   /* Enable the write protection for RTC registers */
347   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
348 
349   hrtc->State = HAL_RTC_STATE_READY;
350 
351   /* Process Unlocked */
352   __HAL_UNLOCK(hrtc);
353 
354   return HAL_OK;
355 }
356 
357 /**
358   * @brief  Set Internal TimeStamp.
359   * @note   This API must be called before enabling the internal TimeStamp feature.
360   * @param  hrtc RTC handle
361   * @retval HAL status
362   */
HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef * hrtc)363 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
364 {
365   /* Process Locked */
366   __HAL_LOCK(hrtc);
367 
368   hrtc->State = HAL_RTC_STATE_BUSY;
369 
370   /* Disable the write protection for RTC registers */
371   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
372 
373   /* Configure the internal Time Stamp Enable bits */
374   __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);
375 
376   /* Enable the write protection for RTC registers */
377   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
378 
379   /* Change RTC state */
380   hrtc->State = HAL_RTC_STATE_READY;
381 
382   /* Process Unlocked */
383   __HAL_UNLOCK(hrtc);
384 
385   return HAL_OK;
386 }
387 
388 /**
389   * @brief  Deactivate Internal TimeStamp.
390   * @param  hrtc RTC handle
391   * @retval HAL status
392   */
HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef * hrtc)393 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
394 {
395   /* Process Locked */
396   __HAL_LOCK(hrtc);
397 
398   hrtc->State = HAL_RTC_STATE_BUSY;
399 
400   /* Disable the write protection for RTC registers */
401   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
402 
403   /* Configure the internal Time Stamp Enable bits */
404   __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
405 
406   /* Enable the write protection for RTC registers */
407   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
408 
409   hrtc->State = HAL_RTC_STATE_READY;
410 
411   /* Process Unlocked */
412   __HAL_UNLOCK(hrtc);
413 
414   return HAL_OK;
415 }
416 
417 /**
418   * @brief  Get the RTC TimeStamp value.
419   * @param  hrtc RTC handle
420   * @param  sTimeStamp Pointer to Time structure
421   * @param  sTimeStampDate Pointer to Date structure
422   * @param  Format specifies the format of the entered parameters.
423   *          This parameter can be one of the following values:
424   *             @arg RTC_FORMAT_BIN: Binary data format
425   *             @arg RTC_FORMAT_BCD: BCD data format
426   * @retval HAL status
427   */
HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTimeStamp,RTC_DateTypeDef * sTimeStampDate,uint32_t Format)428 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
429 {
430   uint32_t tmptime, tmpdate;
431 
432   /* Check the parameters */
433   assert_param(IS_RTC_FORMAT(Format));
434 
435   /* Get the TimeStamp time and date registers values */
436   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
437   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
438 
439   /* Fill the Time structure fields with the read parameters */
440   sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
441   sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
442   sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
443   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
444   sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
445 
446   /* Fill the Date structure fields with the read parameters */
447   sTimeStampDate->Year = 0U;
448   sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
449   sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
450   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
451 
452   /* Check the input parameters format */
453   if(Format == RTC_FORMAT_BIN)
454   {
455     /* Convert the TimeStamp structure parameters to Binary format */
456     sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
457     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
458     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
459 
460     /* Convert the DateTimeStamp structure parameters to Binary format */
461     sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
462     sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
463     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
464   }
465 
466   /* Clear the TIMESTAMP Flags */
467   __HAL_RTC_INTERNAL_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_ITSF);
468   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
469 
470   return HAL_OK;
471 }
472 
473 /**
474   * @brief  Handle TimeStamp interrupt request.
475   * @param  hrtc RTC handle
476   * @retval None
477   */
478 #if defined(STM32L412xx) || defined(STM32L422xx)
479 
HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef * hrtc)480 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
481 {
482 
483   /* Process TAMP instance pointer */
484   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
485 
486   /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
487   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
488 
489   if((hrtc->Instance->MISR & RTC_MISR_TSMF) != 0u)
490   {
491 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
492     /* Call TimeStampEvent registered Callback */
493     hrtc->TimeStampEventCallback(hrtc);
494 #else
495     HAL_RTCEx_TimeStampEventCallback(hrtc);
496 #endif
497     /* Not immediatly clear flags because the content of RTC_TSTR and RTC_TSDR arecleared when TSF bit is reset.*/
498     hrtc->Instance->SCR = RTC_SCR_CTSF;
499   }
500 
501   /* Get interrupt status */
502   uint32_t tmp = tamp->MISR;
503 
504   /* Immediatly clear flags */
505   tamp->SCR = tmp;
506 
507 #if defined(RTC_TAMPER1_SUPPORT)
508   /* Check Tamper1 status */
509   if((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
510   {
511 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
512     /* Call Tamper 1 Event registered Callback */
513     hrtc->Tamper1EventCallback(hrtc);
514 #else
515     /* Tamper1 callback */
516     HAL_RTCEx_Tamper1EventCallback(hrtc);
517 #endif
518   }
519 #endif /* RTC_TAMPER1_SUPPORT */
520 
521   /* Check Tamper2 status */
522   if((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
523   {
524 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
525     /* Call Tamper 2 Event registered Callback */
526     hrtc->Tamper2EventCallback(hrtc);
527 #else
528     /* Tamper2 callback */
529     HAL_RTCEx_Tamper2EventCallback(hrtc);
530 #endif
531   }
532 
533 #if defined(RTC_TAMPER3_SUPPORT)
534   /* Check Tamper3 status */
535   if((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
536   {
537 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
538     /* Call Tamper 3 Event registered Callback */
539     hrtc->Tamper3EventCallback(hrtc);
540 #else
541     /* Tamper3 callback */
542     HAL_RTCEx_Tamper3EventCallback(hrtc);
543 #endif
544   }
545 
546 #endif /* RTC_TAMPER3_SUPPORT */
547 
548   /* Change RTC state */
549   hrtc->State = HAL_RTC_STATE_READY;
550 }
551 
552 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
553 
HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef * hrtc)554 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
555 {
556   /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
557   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
558 
559   /* Get the TimeStamp interrupt source enable status and pending flag status */
560   if(__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
561   {
562     if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
563     {
564       /* TIMESTAMP callback */
565 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
566       hrtc->TimeStampEventCallback(hrtc);
567 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
568       HAL_RTCEx_TimeStampEventCallback(hrtc);
569 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
570 
571       /* Clear the TIMESTAMP interrupt pending bit (this will clear timestamp time and date registers) */
572       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
573     }
574   }
575 
576 #if defined(RTC_TAMPER1_SUPPORT)
577   /* Get the Tamper1 interrupt source enable status and pending flag status */
578   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != 0U)
579   {
580     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
581     {
582     /* Clear the Tamper1 interrupt pending bit */
583     __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
584 
585       /* Tamper1 callback */
586 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
587       hrtc->Tamper1EventCallback(hrtc);
588 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
589       HAL_RTCEx_Tamper1EventCallback(hrtc);
590 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
591     }
592   }
593 #endif /* RTC_TAMPER1_SUPPORT */
594 
595   /* Get the Tamper2 interrupt source enable status and pending flag status */
596   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != 0U)
597   {
598     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
599     {
600       /* Clear the Tamper2 interrupt pending bit */
601       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
602 
603     /* Tamper2 callback */
604 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
605       hrtc->Tamper2EventCallback(hrtc);
606 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
607       HAL_RTCEx_Tamper2EventCallback(hrtc);
608 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
609     }
610   }
611 
612 #if defined(RTC_TAMPER3_SUPPORT)
613   /* Get the Tamper3 interrupts source enable status */
614   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != 0U)
615   {
616     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
617     {
618       /* Clear the Tamper3 interrupt pending bit */
619       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
620 
621       /* Tamper3 callback */
622 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
623       hrtc->Tamper3EventCallback(hrtc);
624 #else
625       HAL_RTCEx_Tamper3EventCallback(hrtc);
626 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
627     }
628   }
629 #endif /* RTC_TAMPER3_SUPPORT */
630 
631   /* Change RTC state */
632   hrtc->State = HAL_RTC_STATE_READY;
633 }
634 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
635 
636 /**
637   * @brief  TimeStamp callback.
638   * @param  hrtc RTC handle
639   * @retval None
640   */
HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef * hrtc)641 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
642 {
643   /* Prevent unused argument(s) compilation warning */
644   UNUSED(hrtc);
645 
646   /* NOTE : This function should not be modified, when the callback is needed,
647             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
648    */
649 }
650 
651 /**
652   * @brief  Handle TimeStamp polling request.
653   * @param  hrtc RTC handle
654   * @param  Timeout Timeout duration
655   * @retval HAL status
656   */
HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)657 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
658 {
659   uint32_t tickstart = HAL_GetTick();
660 
661   while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
662   {
663     if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
664     {
665       /* Clear the TIMESTAMP OverRun Flag */
666       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
667 
668       /* Change TIMESTAMP state */
669       hrtc->State = HAL_RTC_STATE_ERROR;
670 
671       return HAL_ERROR;
672     }
673 
674     if(Timeout != HAL_MAX_DELAY)
675     {
676       if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
677       {
678         hrtc->State = HAL_RTC_STATE_TIMEOUT;
679         return HAL_TIMEOUT;
680       }
681     }
682   }
683 
684   /* Change RTC state */
685   hrtc->State = HAL_RTC_STATE_READY;
686 
687   return HAL_OK;
688 }
689 
690 /**
691   * @}
692   */
693 
694 /** @addtogroup RTCEx_Exported_Functions_Group2
695   * @brief    RTC Wake-up functions
696   *
697 @verbatim
698  ===============================================================================
699                         ##### RTC Wake-up functions #####
700  ===============================================================================
701 
702  [..] This section provides functions allowing to configure Wake-up feature
703 
704 @endverbatim
705   * @{
706   */
707 
708 /**
709   * @brief  Set wake up timer.
710   * @param  hrtc RTC handle
711   * @param  WakeUpCounter Wake up counter
712   * @param  WakeUpClock Wake up clock
713   * @retval HAL status
714   */
HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)715 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
716 {
717   uint32_t tickstart;
718 
719   /* Check the parameters */
720   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
721   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
722 
723   /* Process Locked */
724   __HAL_LOCK(hrtc);
725 
726   hrtc->State = HAL_RTC_STATE_BUSY;
727 
728   /* Disable the write protection for RTC registers */
729   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
730 
731   /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
732   if((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
733   {
734     tickstart = HAL_GetTick();
735 
736    /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
737    while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 1U)
738    {
739     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
740       {
741        /* Enable the write protection for RTC registers */
742        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
743 
744        hrtc->State = HAL_RTC_STATE_TIMEOUT;
745 
746        /* Process Unlocked */
747        __HAL_UNLOCK(hrtc);
748 
749        return HAL_TIMEOUT;
750       }
751     }
752   }
753 
754   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
755 
756   tickstart = HAL_GetTick();
757 
758   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
759   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
760   {
761     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
762     {
763       /* Enable the write protection for RTC registers */
764       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
765 
766       hrtc->State = HAL_RTC_STATE_TIMEOUT;
767 
768       /* Process Unlocked */
769       __HAL_UNLOCK(hrtc);
770 
771       return HAL_TIMEOUT;
772     }
773   }
774 
775   /* Clear the Wakeup Timer clock source bits and configure the clock source in CR register */
776   {
777     uint32_t CR_tmp = hrtc->Instance->CR;
778     CR_tmp &= (uint32_t)~RTC_CR_WUCKSEL;
779     CR_tmp |= (uint32_t)WakeUpClock;
780     hrtc->Instance->CR = CR_tmp;
781   }
782 
783   /* Configure the Wakeup Timer counter */
784   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
785 
786    /* Enable the Wakeup Timer */
787   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
788 
789   /* Enable the write protection for RTC registers */
790   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
791 
792   hrtc->State = HAL_RTC_STATE_READY;
793 
794   /* Process Unlocked */
795   __HAL_UNLOCK(hrtc);
796 
797   return HAL_OK;
798 }
799 
800 /**
801   * @brief  Set wake up timer with interrupt.
802   * @param  hrtc RTC handle
803   * @param  WakeUpCounter Wake up counter
804   * @param  WakeUpClock Wake up clock
805   * @param  WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
806   *                       - Only available for STM32L412xx and STM32L422xx
807   *                       - No effect if WakeUpAutoClr is set to zero
808   *                       - This feature is meaningfull in case of Low power mode to avoid any RTC software execution after Wake Up.
809   *                         That's why when WakeUpAutoClr is set, EXTI is configured as EVENT instead of Interrupt to avoid useless IRQ handler execution.
810   * @retval HAL status
811   */
812 #if defined(STM32L412xx) || defined(STM32L422xx)
HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock,uint32_t WakeUpAutoClr)813 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock, uint32_t WakeUpAutoClr)
814 #else
815 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
816 #endif
817 {
818   uint32_t tickstart;
819 
820   /* Check the parameters */
821   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
822   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
823 #if defined(STM32L412xx) || defined(STM32L422xx)
824   /* (0x0000<=WUTOCLR<=WUT) */
825   assert_param(WakeUpAutoClr <= WakeUpCounter);
826 #endif
827 
828   /* Process Locked */
829   __HAL_LOCK(hrtc);
830 
831   hrtc->State = HAL_RTC_STATE_BUSY;
832 
833   /* Disable the write protection for RTC registers */
834   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
835 
836   /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
837   if((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
838   {
839     tickstart = HAL_GetTick();
840 
841    /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
842    while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 1U)
843    {
844     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
845       {
846        /* Enable the write protection for RTC registers */
847        __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
848 
849        hrtc->State = HAL_RTC_STATE_TIMEOUT;
850 
851        /* Process Unlocked */
852        __HAL_UNLOCK(hrtc);
853 
854        return HAL_TIMEOUT;
855       }
856     }
857   }
858   /* Disable the Wake-Up timer */
859   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
860 
861   /* Clear flag Wake-Up */
862   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
863 
864   tickstart = HAL_GetTick();
865 
866   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
867   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
868   {
869     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
870     {
871       /* Enable the write protection for RTC registers */
872       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
873 
874       hrtc->State = HAL_RTC_STATE_TIMEOUT;
875 
876       /* Process Unlocked */
877       __HAL_UNLOCK(hrtc);
878 
879       return HAL_TIMEOUT;
880     }
881   }
882 
883 #if defined(STM32L412xx) || defined(STM32L422xx)
884   /* Configure the Wakeup Timer counter and auto clear value */
885   hrtc->Instance->WUTR = (uint32_t) (WakeUpCounter | (WakeUpAutoClr << RTC_WUTR_WUTOCLR_Pos));
886 #else
887 /* Configure the Wakeup Timer counter */
888   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
889 #endif
890 
891   /* Clear the Wakeup Timer clock source bits and configure the clock source in CR register */
892   {
893     uint32_t CR_tmp = hrtc->Instance->CR;
894     CR_tmp &= (uint32_t)~RTC_CR_WUCKSEL;
895     CR_tmp |= (uint32_t)WakeUpClock;
896     hrtc->Instance->CR = CR_tmp;
897   }
898 
899 #if defined(STM32L412xx) || defined(STM32L422xx)
900   /* In case of WUT autoclr, the IRQ handler should not be called */
901   if (WakeUpAutoClr != 0u)
902   {
903     /* RTC WakeUpTimer EXTI Configuration: Event configuration */
904     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
905   }
906   else
907   {
908     /* RTC WakeUpTimer EXTI Configuration: Interrupt configuration */
909     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
910   }
911 #else /* defined(STM32L412xx) || defined(STM32L422xx) */
912     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
913 #endif /* defined(STM32L412xx) || defined(STM32L422xx) */
914 
915   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
916 
917   /* Configure the Interrupt in the RTC_CR register */
918   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
919 
920   /* Enable the Wakeup Timer */
921   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
922 
923   /* Enable the write protection for RTC registers */
924   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
925 
926   hrtc->State = HAL_RTC_STATE_READY;
927 
928   /* Process Unlocked */
929   __HAL_UNLOCK(hrtc);
930 
931   return HAL_OK;
932 }
933 
934 /**
935   * @brief  Deactivate wake up timer counter.
936   * @param  hrtc RTC handle
937   * @retval HAL status
938   */
HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)939 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
940 {
941   uint32_t tickstart;
942 
943   /* Process Locked */
944   __HAL_LOCK(hrtc);
945 
946   hrtc->State = HAL_RTC_STATE_BUSY;
947 
948   /* Disable the write protection for RTC registers */
949   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
950 
951   /* Disable the Wakeup Timer */
952   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
953 
954   /* In case of interrupt mode is used, the interrupt source must disabled */
955   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
956 
957   tickstart = HAL_GetTick();
958   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
959   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
960   {
961     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
962     {
963       /* Enable the write protection for RTC registers */
964       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
965 
966       hrtc->State = HAL_RTC_STATE_TIMEOUT;
967 
968       /* Process Unlocked */
969       __HAL_UNLOCK(hrtc);
970 
971       return HAL_TIMEOUT;
972     }
973   }
974 
975   /* Enable the write protection for RTC registers */
976   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
977 
978   hrtc->State = HAL_RTC_STATE_READY;
979 
980   /* Process Unlocked */
981   __HAL_UNLOCK(hrtc);
982 
983   return HAL_OK;
984 }
985 
986 /**
987   * @brief  Get wake up timer counter.
988   * @param  hrtc RTC handle
989   * @retval Counter value
990   */
HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)991 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
992 {
993   /* Get the counter value */
994   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
995 }
996 
997 /**
998   * @brief  Handle Wake Up Timer interrupt request.
999   * @param  hrtc RTC handle
1000   * @retval None
1001   */
HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef * hrtc)1002 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
1003 {
1004   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
1005   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1006 
1007 
1008 #if defined(STM32L412xx) || defined(STM32L422xx)
1009   if((hrtc->Instance->MISR & RTC_MISR_WUTMF) != 0u)
1010   {
1011     /* Immediately clear flags */
1012     hrtc->Instance->SCR = RTC_SCR_CWUTF;
1013 #else
1014   /* Get the pending status of the WAKEUPTIMER Interrupt */
1015   if(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
1016   {
1017     /* Clear the WAKEUPTIMER interrupt pending bit */
1018     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1019 #endif
1020 
1021     /* WAKEUPTIMER callback */
1022 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1023     /* Call WakeUpTimerEvent registered Callback */
1024     hrtc->WakeUpTimerEventCallback(hrtc);
1025 #else
1026     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
1027 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1028   }
1029 
1030   /* Change RTC state */
1031   hrtc->State = HAL_RTC_STATE_READY;
1032 }
1033 
1034 /**
1035   * @brief  Wake Up Timer callback.
1036   * @param  hrtc RTC handle
1037   * @retval None
1038   */
1039 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
1040 {
1041   /* Prevent unused argument(s) compilation warning */
1042   UNUSED(hrtc);
1043 
1044   /* NOTE : This function should not be modified, when the callback is needed,
1045             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
1046    */
1047 }
1048 
1049 
1050 /**
1051   * @brief  Handle Wake Up Timer Polling.
1052   * @param  hrtc RTC handle
1053   * @param  Timeout Timeout duration
1054   * @retval HAL status
1055   */
1056 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1057 {
1058   uint32_t tickstart = HAL_GetTick();
1059 
1060   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
1061   {
1062     if(Timeout != HAL_MAX_DELAY)
1063     {
1064       if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
1065       {
1066         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1067         return HAL_TIMEOUT;
1068       }
1069     }
1070   }
1071 
1072   /* Clear the WAKEUPTIMER Flag */
1073   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
1074 
1075   /* Change RTC state */
1076   hrtc->State = HAL_RTC_STATE_READY;
1077 
1078   return HAL_OK;
1079 }
1080 
1081 /**
1082   * @}
1083   */
1084 
1085 
1086 /** @addtogroup RTCEx_Exported_Functions_Group3
1087   * @brief    Extended Peripheral Control functions
1088   *
1089 @verbatim
1090  ===============================================================================
1091               ##### Extended Peripheral Control functions #####
1092  ===============================================================================
1093     [..]
1094     This subsection provides functions allowing to
1095       (+) Write a data in a specified RTC Backup data register
1096       (+) Read a data in a specified RTC Backup data register
1097       (+) Set the Coarse calibration parameters.
1098       (+) Deactivate the Coarse calibration parameters
1099       (+) Set the Smooth calibration parameters.
1100       (+) STM32L412xx and STM32L422xx only : Set Low Power calibration parameter.
1101       (+) Configure the Synchronization Shift Control Settings.
1102       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1103       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1104       (+) Enable the RTC reference clock detection.
1105       (+) Disable the RTC reference clock detection.
1106       (+) Enable the Bypass Shadow feature.
1107       (+) Disable the Bypass Shadow feature.
1108 
1109 @endverbatim
1110   * @{
1111   */
1112 
1113 
1114 /**
1115   * @brief  Set the Smooth calibration parameters.
1116   * @param  hrtc RTC handle
1117   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
1118   *          This parameter can be can be one of the following values :
1119   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
1120   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
1121   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
1122   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
1123   *          This parameter can be one of the following values:
1124   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
1125   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
1126   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
1127   *          This parameter can be one any value from 0 to 0x000001FF.
1128   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
1129   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
1130   *         SmoothCalibMinusPulsesValue must be equal to 0.
1131   * @retval HAL status
1132   */
1133 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
1134 {
1135   uint32_t tickstart;
1136 
1137   /* Check the parameters */
1138   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
1139   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
1140   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
1141 
1142   /* Process Locked */
1143   __HAL_LOCK(hrtc);
1144 
1145   hrtc->State = HAL_RTC_STATE_BUSY;
1146 
1147   /* Disable the write protection for RTC registers */
1148   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1149 
1150   /* check if a calibration is pending*/
1151 #if defined(STM32L412xx) || defined(STM32L422xx)
1152   if((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
1153 #else
1154   if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1155 #endif
1156   {
1157     tickstart = HAL_GetTick();
1158 
1159     /* check if a calibration is pending*/
1160 #if defined(STM32L412xx) || defined(STM32L422xx)
1161     while((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
1162 #else
1163     while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
1164 #endif
1165     {
1166       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1167       {
1168         /* Enable the write protection for RTC registers */
1169         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1170 
1171         /* Change RTC state */
1172         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1173 
1174         /* Process Unlocked */
1175         __HAL_UNLOCK(hrtc);
1176 
1177         return HAL_TIMEOUT;
1178       }
1179     }
1180   }
1181 
1182   /* Configure the Smooth calibration settings */
1183   MODIFY_REG(hrtc->Instance->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
1184 
1185   /* Enable the write protection for RTC registers */
1186   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1187 
1188   /* Change RTC state */
1189   hrtc->State = HAL_RTC_STATE_READY;
1190 
1191   /* Process Unlocked */
1192   __HAL_UNLOCK(hrtc);
1193 
1194   return HAL_OK;
1195 }
1196 
1197 #if defined(STM32L412xx) || defined(STM32L422xx)
1198 /**
1199   * @brief  Select the low power Calibration mode.
1200   * @param  hrtc: RTC handle
1201   * @param  LowPowerCalib: Low power Calibration mode.
1202   *          This parameter can be can be one of the following values :
1203   *             @arg RTC_LPCAL_SET: Low power mode.
1204   *             @arg RTC_LPCAL_RESET: High consumption mode.
1205   * @retval HAL status
1206   */
1207 HAL_StatusTypeDef HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef *hrtc, uint32_t LowPowerCalib)
1208 {
1209   /* Check the parameters */
1210   assert_param(IS_RTC_LOW_POWER_CALIB(LowPowerCalib));
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   /* Configure the Smooth calibration settings */
1221   MODIFY_REG(hrtc->Instance->CALR, RTC_CALR_LPCAL, LowPowerCalib);
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 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1235 
1236 /**
1237   * @brief  Configure the Synchronization Shift Control Settings.
1238   * @note   When REFCKON is set, firmware must not write to Shift control register.
1239   * @param  hrtc RTC handle
1240   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
1241   *          This parameter can be one of the following values:
1242   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
1243   *             @arg RTC_SHIFTADD1S_RESET: No effect.
1244   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
1245   *          This parameter can be one any value from 0 to 0x7FFF.
1246   * @retval HAL status
1247   */
1248 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
1249 {
1250   uint32_t tickstart;
1251 
1252   /* Check the parameters */
1253   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
1254   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
1255 
1256   /* Process Locked */
1257   __HAL_LOCK(hrtc);
1258 
1259   hrtc->State = HAL_RTC_STATE_BUSY;
1260 
1261   /* Disable the write protection for RTC registers */
1262   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1263 
1264     tickstart = HAL_GetTick();
1265 
1266     /* Wait until the shift is completed*/
1267 #if defined(STM32L412xx) || defined(STM32L422xx)
1268     while((hrtc->Instance->ICSR & RTC_ICSR_SHPF) != 0U)
1269 #else
1270     while((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
1271 #endif
1272     {
1273       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
1274       {
1275         /* Enable the write protection for RTC registers */
1276         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1277 
1278         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1279 
1280         /* Process Unlocked */
1281         __HAL_UNLOCK(hrtc);
1282 
1283         return HAL_TIMEOUT;
1284       }
1285     }
1286 
1287     /* Check if the reference clock detection is disabled */
1288     if((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
1289     {
1290       /* Configure the Shift settings */
1291       hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
1292 
1293       /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
1294       if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
1295       {
1296         if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1297         {
1298           /* Enable the write protection for RTC registers */
1299           __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1300 
1301           hrtc->State = HAL_RTC_STATE_ERROR;
1302 
1303           /* Process Unlocked */
1304           __HAL_UNLOCK(hrtc);
1305 
1306           return HAL_ERROR;
1307         }
1308       }
1309     }
1310     else
1311     {
1312       /* Enable the write protection for RTC registers */
1313       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1314 
1315       /* Change RTC state */
1316       hrtc->State = HAL_RTC_STATE_ERROR;
1317 
1318       /* Process Unlocked */
1319       __HAL_UNLOCK(hrtc);
1320 
1321       return HAL_ERROR;
1322     }
1323 
1324   /* Enable the write protection for RTC registers */
1325   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1326 
1327   /* Change RTC state */
1328   hrtc->State = HAL_RTC_STATE_READY;
1329 
1330   /* Process Unlocked */
1331   __HAL_UNLOCK(hrtc);
1332 
1333   return HAL_OK;
1334 }
1335 
1336 /**
1337   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1338   * @param  hrtc RTC handle
1339   * @param  CalibOutput Select the Calibration output Selection .
1340   *          This parameter can be one of the following values:
1341   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
1342   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
1343   * @retval HAL status
1344   */
1345 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
1346 {
1347   /* Check the parameters */
1348   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
1349 
1350   /* Process Locked */
1351   __HAL_LOCK(hrtc);
1352 
1353   hrtc->State = HAL_RTC_STATE_BUSY;
1354 
1355   /* Disable the write protection for RTC registers */
1356   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1357 
1358   /* Clear flags before config */
1359   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
1360 
1361   /* Configure the RTC_CR register */
1362   hrtc->Instance->CR |= (uint32_t)CalibOutput;
1363 
1364   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
1365 
1366   /* Enable the write protection for RTC registers */
1367   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1368 
1369   /* Change RTC state */
1370   hrtc->State = HAL_RTC_STATE_READY;
1371 
1372   /* Process Unlocked */
1373   __HAL_UNLOCK(hrtc);
1374 
1375   return HAL_OK;
1376 }
1377 
1378 /**
1379   * @brief  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1380   * @param  hrtc RTC handle
1381   * @retval HAL status
1382   */
1383 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
1384 {
1385   /* Process Locked */
1386   __HAL_LOCK(hrtc);
1387 
1388   hrtc->State = HAL_RTC_STATE_BUSY;
1389 
1390   /* Disable the write protection for RTC registers */
1391   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1392 
1393   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
1394 
1395   /* Enable the write protection for RTC registers */
1396   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1397 
1398   /* Change RTC state */
1399   hrtc->State = HAL_RTC_STATE_READY;
1400 
1401   /* Process Unlocked */
1402   __HAL_UNLOCK(hrtc);
1403 
1404   return HAL_OK;
1405 }
1406 
1407 /**
1408   * @brief  Enable the RTC reference clock detection.
1409   * @param  hrtc RTC handle
1410   * @retval HAL status
1411   */
1412 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
1413 {
1414   /* Process Locked */
1415   __HAL_LOCK(hrtc);
1416 
1417   hrtc->State = HAL_RTC_STATE_BUSY;
1418 
1419   /* Disable the write protection for RTC registers */
1420   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1421 
1422   /* Set Initialization mode */
1423   if(RTC_EnterInitMode(hrtc) != HAL_OK)
1424   {
1425     /* Enable the write protection for RTC registers */
1426     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1427 
1428     /* Set RTC state*/
1429     hrtc->State = HAL_RTC_STATE_ERROR;
1430 
1431     /* Process Unlocked */
1432     __HAL_UNLOCK(hrtc);
1433 
1434     return HAL_ERROR;
1435   }
1436   else
1437   {
1438     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
1439 
1440     /* Exit Initialization mode */
1441 #if defined(STM32L412xx) || defined(STM32L422xx)
1442     hrtc->Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
1443 #else
1444     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1445 #endif
1446   }
1447 
1448   /* Enable the write protection for RTC registers */
1449   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1450 
1451    /* Change RTC state */
1452   hrtc->State = HAL_RTC_STATE_READY;
1453 
1454   /* Process Unlocked */
1455   __HAL_UNLOCK(hrtc);
1456 
1457   return HAL_OK;
1458 }
1459 
1460 /**
1461   * @brief  Disable the RTC reference clock detection.
1462   * @param  hrtc RTC handle
1463   * @retval HAL status
1464   */
1465 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
1466 {
1467   /* Process Locked */
1468   __HAL_LOCK(hrtc);
1469 
1470   hrtc->State = HAL_RTC_STATE_BUSY;
1471 
1472   /* Disable the write protection for RTC registers */
1473   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1474 
1475   /* Set Initialization mode */
1476   if(RTC_EnterInitMode(hrtc) != HAL_OK)
1477   {
1478     /* Enable the write protection for RTC registers */
1479     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1480 
1481     /* Set RTC state*/
1482     hrtc->State = HAL_RTC_STATE_ERROR;
1483 
1484     /* Process Unlocked */
1485     __HAL_UNLOCK(hrtc);
1486 
1487     return HAL_ERROR;
1488   }
1489   else
1490   {
1491     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
1492 
1493     /* Exit Initialization mode */
1494 #if defined(STM32L412xx) || defined(STM32L422xx)
1495     hrtc->Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
1496 #else
1497     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
1498 #endif
1499   }
1500 
1501   /* Enable the write protection for RTC registers */
1502   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1503 
1504   /* Change RTC state */
1505   hrtc->State = HAL_RTC_STATE_READY;
1506 
1507   /* Process Unlocked */
1508   __HAL_UNLOCK(hrtc);
1509 
1510   return HAL_OK;
1511 }
1512 
1513 /**
1514   * @brief  Enable the Bypass Shadow feature.
1515   * @note   When the Bypass Shadow is enabled the calendar value are taken
1516   *         directly from the Calendar counter.
1517   * @param  hrtc RTC handle
1518   * @retval HAL status
1519   */
1520 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
1521 {
1522   /* Process Locked */
1523   __HAL_LOCK(hrtc);
1524 
1525   hrtc->State = HAL_RTC_STATE_BUSY;
1526 
1527   /* Disable the write protection for RTC registers */
1528   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1529 
1530   /* Set the BYPSHAD bit */
1531   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
1532 
1533   /* Enable the write protection for RTC registers */
1534   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1535 
1536   /* Change RTC state */
1537   hrtc->State = HAL_RTC_STATE_READY;
1538 
1539   /* Process Unlocked */
1540   __HAL_UNLOCK(hrtc);
1541 
1542   return HAL_OK;
1543 }
1544 
1545 /**
1546   * @brief  Disable the Bypass Shadow feature.
1547   * @note   When the Bypass Shadow is enabled the calendar value are taken
1548   *         directly from the Calendar counter.
1549   * @param  hrtc RTC handle
1550   * @retval HAL status
1551   */
1552 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
1553 {
1554   /* Process Locked */
1555   __HAL_LOCK(hrtc);
1556 
1557   hrtc->State = HAL_RTC_STATE_BUSY;
1558 
1559   /* Disable the write protection for RTC registers */
1560   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1561 
1562   /* Reset the BYPSHAD bit */
1563   hrtc->Instance->CR &= ((uint8_t)~RTC_CR_BYPSHAD);
1564 
1565   /* Enable the write protection for RTC registers */
1566   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1567 
1568   /* Change RTC state */
1569   hrtc->State = HAL_RTC_STATE_READY;
1570 
1571   /* Process Unlocked */
1572   __HAL_UNLOCK(hrtc);
1573 
1574   return HAL_OK;
1575 }
1576 
1577 /**
1578   * @}
1579   */
1580 
1581 /** @addtogroup RTCEx_Exported_Functions_Group4
1582   * @brief    Extended features functions
1583   *
1584 @verbatim
1585  ===============================================================================
1586                  ##### Extended features functions #####
1587  ===============================================================================
1588     [..]  This section provides functions allowing to:
1589       (+) RTC Alarm B callback
1590       (+) RTC Poll for Alarm B request
1591 
1592 @endverbatim
1593   * @{
1594   */
1595 
1596 /**
1597   * @brief  Alarm B callback.
1598   * @param  hrtc RTC handle
1599   * @retval None
1600   */
1601 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
1602 {
1603   /* Prevent unused argument(s) compilation warning */
1604   UNUSED(hrtc);
1605 
1606   /* NOTE : This function should not be modified, when the callback is needed,
1607             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
1608    */
1609 }
1610 
1611 /**
1612   * @brief  Handle Alarm B Polling request.
1613   * @param  hrtc RTC handle
1614   * @param  Timeout Timeout duration
1615   * @retval HAL status
1616   */
1617 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1618 {
1619   uint32_t tickstart = HAL_GetTick();
1620 
1621   while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
1622   {
1623     if(Timeout != HAL_MAX_DELAY)
1624     {
1625       if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
1626       {
1627         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1628         return HAL_TIMEOUT;
1629       }
1630     }
1631   }
1632 
1633   /* Clear the Alarm Flag */
1634   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1635 
1636   /* Change RTC state */
1637   hrtc->State = HAL_RTC_STATE_READY;
1638 
1639   return HAL_OK;
1640 }
1641 
1642 /**
1643   * @}
1644   */
1645 
1646 /** @addtogroup RTCEx_Exported_Functions_Group5
1647   * @brief      Extended RTC Tamper functions
1648   *
1649 @verbatim
1650   ==============================================================================
1651                          ##### Tamper functions #####
1652   ==============================================================================
1653   [..]
1654    (+) Before calling any tamper or internal tamper function, you have to call first
1655        HAL_RTC_Init() function.
1656    (+) In that ine you can select to output tamper event on RTC pin.
1657   [..]
1658    (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
1659        or Level according to the Tamper filter (if equal to 0 Edge else Level)
1660        value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
1661        Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
1662        You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
1663    (+) The default configuration of the Tamper erases the backup registers. To avoid
1664        erase, enable the NoErase field on the TAMP_TAMPCR register.
1665   [..]
1666    (+) Enable Internal Tamper and configure it with interrupt, timestamp using
1667        the HAL_RTCEx_SetInternalTamper() function.
1668 
1669 @endverbatim
1670   * @{
1671   */
1672 
1673 #if defined(STM32L412xx) || defined(STM32L422xx)
1674 /**
1675   * @brief  Set Tamper
1676   * @param  hrtc RTC handle
1677   * @param  sTamper Pointer to Tamper Structure.
1678   * @retval HAL status
1679   */
1680 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1681 {
1682   uint32_t tmpreg;
1683   /* Process TAMP instance pointer */
1684   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
1685 
1686   /* Check the parameters */
1687   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1688   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1689   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1690   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1691   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1692   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1693   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1694   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1695   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1696   /* Trigger and Filter have exclusive configurations */
1697   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL)))
1698             || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
1699 
1700   /* Configuration register 2 */
1701   tmpreg = tamp->CR2;
1702   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1703 
1704   /* Configure the tamper trigger bit */
1705   if((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
1706   {
1707     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1708   }
1709 
1710   /* Configure the tamper flags masking bit */
1711   if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1712   {
1713     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1714   }
1715 
1716   /* Configure the tamper backup registers erasure bit */
1717   if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1718   {
1719     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1720   }
1721   tamp->CR2 = tmpreg;
1722 
1723   /* Configure filtering parameters */
1724   tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
1725                  (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
1726 
1727   /* Configure Timestamp saving on tamper detection */
1728   if((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
1729   {
1730     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1731     tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
1732     hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
1733     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1734   }
1735 
1736   /* Enable selected tamper */
1737   tamp->CR1 |= (sTamper->Tamper);
1738 
1739   return HAL_OK;
1740 }
1741 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1742 /**
1743   * @brief  Set Tamper.
1744   * @note   By calling this API we disable the tamper interrupt for all tampers.
1745   * @param  hrtc RTC handle
1746   * @param  sTamper Pointer to Tamper Structure.
1747   * @retval HAL status
1748   */
1749 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1750 {
1751   uint32_t tmpreg;
1752 
1753   /* Check the parameters */
1754   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1755   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1756   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1757   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1758   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1759   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1760   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1761   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1762   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1763 
1764   /* Process Locked */
1765   __HAL_LOCK(hrtc);
1766 
1767   hrtc->State = HAL_RTC_STATE_BUSY;
1768 
1769   /* Configure the tamper trigger */
1770   if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
1771   {
1772     sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
1773   }
1774 
1775   if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1776   {
1777     sTamper->NoErase = 0;
1778 #if defined(RTC_TAMPER1_SUPPORT)
1779     if((sTamper->Tamper & RTC_TAMPER_1) != 0)
1780     {
1781       sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
1782     }
1783 #endif /* RTC_TAMPER1_SUPPORT */
1784     if((sTamper->Tamper & RTC_TAMPER_2) != 0)
1785     {
1786       sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
1787     }
1788 #if defined(RTC_TAMPER3_SUPPORT)
1789     if((sTamper->Tamper & RTC_TAMPER_3) != 0)
1790     {
1791       sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
1792     }
1793 #endif /* RTC_TAMPER3_SUPPORT */
1794   }
1795 
1796   if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1797   {
1798     sTamper->MaskFlag = 0;
1799 #if defined(RTC_TAMPER1_SUPPORT)
1800     if((sTamper->Tamper & RTC_TAMPER_1) != 0)
1801     {
1802       sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
1803     }
1804 #endif /* RTC_TAMPER1_SUPPORT */
1805     if((sTamper->Tamper & RTC_TAMPER_2) != 0)
1806     {
1807       sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
1808     }
1809 #if defined(RTC_TAMPER3_SUPPORT)
1810     if((sTamper->Tamper & RTC_TAMPER_3) != 0)
1811     {
1812       sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
1813     }
1814 #endif /* RTC_TAMPER3_SUPPORT */
1815   }
1816 
1817   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger  | (uint32_t)sTamper->NoErase |\
1818             (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
1819             (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
1820 
1821   hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | RTC_TAMPCR_MASK);
1822 
1823   hrtc->Instance->TAMPCR |= tmpreg;
1824 
1825   hrtc->State = HAL_RTC_STATE_READY;
1826 
1827   /* Process Unlocked */
1828   __HAL_UNLOCK(hrtc);
1829 
1830   return HAL_OK;
1831 }
1832 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1833 
1834 
1835 
1836 #if defined(STM32L412xx) || defined(STM32L422xx)
1837 /**
1838   * @brief  Set Tamper with interrupt.
1839   * @param  hrtc RTC handle
1840   * @param  sTamper Pointer to Tamper Structure.
1841   * @retval HAL status
1842   */
1843 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1844 {
1845   uint32_t tmpreg;
1846   /* Process TAMP instance pointer */
1847   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
1848 
1849   /* Check the parameters */
1850   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1851   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1852   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1853   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1854   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1855   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1856   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1857   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1858   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1859 
1860   /* Copy configuration register into temporary variable */
1861   tmpreg = tamp->CR2;
1862 
1863   /* Clear the bits that are going to be configured and leave the others unchanged */
1864   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
1865 
1866   if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
1867   {
1868     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
1869   }
1870 
1871   /* Configure the tamper flags masking bit */
1872   if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1873   {
1874     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
1875   }
1876 
1877   /* Configure the tamper backup registers erasure bit */
1878   if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1879   {
1880     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
1881   }
1882   tamp->CR2 = tmpreg;
1883 
1884   /* Configure filtering parameters */
1885   tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
1886                  (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
1887 
1888   /* Configure Timestamp saving on tamper detection */
1889   if((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
1890   {
1891     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1892     tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
1893     hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
1894     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1895   }
1896 
1897   /* Configure RTC Tamper Interrupt: EXTI configuration */
1898   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
1899   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
1900 
1901   /* Enable interrupt on selected tamper */
1902   tamp->IER |= sTamper->Tamper;
1903 
1904   /* Enable selected tamper */
1905   tamp->CR1 |= sTamper->Tamper;
1906 
1907   return HAL_OK;
1908 }
1909 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1910 
1911 /**
1912   * @brief  Set Tamper with interrupt.
1913   * @note   By calling this API we force the tamper interrupt for all tampers.
1914   * @param  hrtc RTC handle
1915   * @param  sTamper Pointer to Tamper Structure.
1916   * @retval HAL status
1917   */
1918 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
1919 {
1920   uint32_t tmpreg = 0;
1921 
1922   /* Check the parameters */
1923   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
1924   assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
1925   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
1926   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
1927   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
1928   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
1929   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
1930   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
1931   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
1932   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
1933 
1934   /* Process Locked */
1935   __HAL_LOCK(hrtc);
1936 
1937   hrtc->State = HAL_RTC_STATE_BUSY;
1938 
1939   /* Configure the tamper trigger */
1940   if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
1941   {
1942     sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
1943   }
1944 
1945   if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1946   {
1947     sTamper->NoErase = 0;
1948 #if defined(RTC_TAMPER1_SUPPORT)
1949     if((sTamper->Tamper & RTC_TAMPER_1) != 0)
1950     {
1951       sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
1952     }
1953 #endif /* RTC_TAMPER1_SUPPORT */
1954     if((sTamper->Tamper & RTC_TAMPER_2) != 0)
1955     {
1956       sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
1957     }
1958 #if defined(RTC_TAMPER3_SUPPORT)
1959     if((sTamper->Tamper & RTC_TAMPER_3) != 0)
1960     {
1961       sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
1962     }
1963 #endif /* RTC_TAMPER3_SUPPORT */
1964   }
1965 
1966   if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1967   {
1968     sTamper->MaskFlag = 0;
1969 #if defined(RTC_TAMPER1_SUPPORT)
1970     if((sTamper->Tamper & RTC_TAMPER_1) != 0)
1971     {
1972       sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
1973     }
1974 #endif /* RTC_TAMPER1_SUPPORT */
1975     if((sTamper->Tamper & RTC_TAMPER_2) != 0)
1976     {
1977       sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
1978     }
1979 #if defined(RTC_TAMPER3_SUPPORT)
1980     if((sTamper->Tamper & RTC_TAMPER_3) != 0)
1981     {
1982       sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
1983     }
1984 #endif /* RTC_TAMPER3_SUPPORT */
1985   }
1986 
1987   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Interrupt | (uint32_t)sTamper->Trigger  | (uint32_t)sTamper->NoErase |\
1988             (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
1989             (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
1990 
1991   hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | RTC_TAMPCR_MASK);
1992 
1993   hrtc->Instance->TAMPCR |= tmpreg;
1994 
1995   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1996   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
1997   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
1998 
1999   hrtc->State = HAL_RTC_STATE_READY;
2000 
2001   /* Process Unlocked */
2002   __HAL_UNLOCK(hrtc);
2003 
2004   return HAL_OK;
2005 }
2006 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
2007 
2008 
2009 #if defined(STM32L412xx) || defined(STM32L422xx)
2010 /**
2011   * @brief  Deactivate Tamper.
2012   * @param  hrtc RTC handle
2013   * @param  Tamper Selected tamper pin.
2014   *         This parameter can be a combination of the following values:
2015   *         @arg RTC_TAMPER_1
2016   *         @arg RTC_TAMPER_2
2017   * @retval HAL status
2018   */
2019 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
2020 {
2021   /* Process TAMP instance pointer */
2022   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
2023 
2024   assert_param(IS_RTC_TAMPER(Tamper));
2025 
2026   /* Disable the selected Tamper pin */
2027   tamp->CR1 &= ~Tamper;
2028 
2029   /* Clear tamper mask/noerase/trigger configuration */
2030   tamp->CR2 &= ~((Tamper << 24) | (Tamper << 16) | Tamper);
2031 
2032   /* Clear tamper interrupt mode configuration */
2033   tamp->IER &= ~Tamper;
2034 
2035   /* Clear tamper interrupt and event flags (WO register) */
2036   tamp->SCR = Tamper;
2037 
2038   return HAL_OK;
2039 }
2040 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
2041 /**
2042   * @brief  Deactivate Tamper.
2043   * @param  hrtc RTC handle
2044   * @param  Tamper Selected tamper pin.
2045   *         This parameter can be any combination of the following values:
2046   *         @arg RTC_TAMPER_1
2047   *         @arg RTC_TAMPER_2
2048   *         @arg RTC_TAMPER_3
2049   * @retval HAL status
2050   */
2051 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
2052 {
2053   assert_param(IS_RTC_TAMPER(Tamper));
2054 
2055   /* Process Locked */
2056   __HAL_LOCK(hrtc);
2057 
2058   hrtc->State = HAL_RTC_STATE_BUSY;
2059 
2060   /* Disable the selected Tamper pin */
2061   hrtc->Instance->TAMPCR &= ~Tamper;
2062 
2063 #if defined(RTC_TAMPER1_SUPPORT)
2064   if ((Tamper & RTC_TAMPER_1) != 0U)
2065   {
2066     /* Disable the Tamper1 interrupt */
2067     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1));
2068   }
2069 #endif /* RTC_TAMPER1_SUPPORT */
2070   if ((Tamper & RTC_TAMPER_2) != 0U)
2071   {
2072     /* Disable the Tamper2 interrupt */
2073     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2));
2074   }
2075 #if defined(RTC_TAMPER3_SUPPORT)
2076   if ((Tamper & RTC_TAMPER_3) != 0U)
2077   {
2078     /* Disable the Tamper3 interrupt */
2079     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3));
2080   }
2081 #endif /* RTC_TAMPER3_SUPPORT */
2082 
2083   hrtc->State = HAL_RTC_STATE_READY;
2084 
2085   /* Process Unlocked */
2086   __HAL_UNLOCK(hrtc);
2087 
2088   return HAL_OK;
2089 }
2090 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
2091 
2092 
2093 #if defined(RTC_TAMPER1_SUPPORT)
2094 /**
2095   * @brief  Handle Tamper 1 Polling.
2096   * @param  hrtc RTC handle
2097   * @param  Timeout Timeout duration
2098   * @retval HAL status
2099   */
2100 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2101 {
2102   uint32_t tickstart = HAL_GetTick();
2103 
2104   /* Get the status of the Interrupt */
2105   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== 0U)
2106   {
2107     if(Timeout != HAL_MAX_DELAY)
2108     {
2109       if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
2110       {
2111         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2112         return HAL_TIMEOUT;
2113       }
2114     }
2115   }
2116 
2117   /* Clear the Tamper Flag */
2118   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
2119 
2120   /* Change RTC state */
2121   hrtc->State = HAL_RTC_STATE_READY;
2122 
2123   return HAL_OK;
2124 }
2125 #endif /* RTC_TAMPER1_SUPPORT */
2126 
2127 /**
2128   * @brief  Handle Tamper 2 Polling.
2129   * @param  hrtc RTC handle
2130   * @param  Timeout Timeout duration
2131   * @retval HAL status
2132   */
2133 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2134 {
2135   uint32_t tickstart = HAL_GetTick();
2136 
2137   /* Get the status of the Interrupt */
2138   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
2139   {
2140     if(Timeout != HAL_MAX_DELAY)
2141     {
2142       if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
2143       {
2144         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2145         return HAL_TIMEOUT;
2146       }
2147     }
2148   }
2149 
2150   /* Clear the Tamper Flag */
2151   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
2152 
2153   /* Change RTC state */
2154   hrtc->State = HAL_RTC_STATE_READY;
2155 
2156   return HAL_OK;
2157 }
2158 
2159 #if defined(RTC_TAMPER3_SUPPORT)
2160 /**
2161   * @brief  Handle Tamper 3 Polling.
2162   * @param  hrtc  RTC handle
2163   * @param  Timeout  Timeout duration
2164   * @retval HAL status
2165   */
2166 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
2167 {
2168   uint32_t tickstart = HAL_GetTick();
2169 
2170   /* Get the status of the Interrupt */
2171   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
2172   {
2173     if(Timeout != HAL_MAX_DELAY)
2174     {
2175       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
2176       {
2177         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2178         return HAL_TIMEOUT;
2179       }
2180     }
2181   }
2182 
2183   /* Clear the Tamper Flag */
2184   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
2185 
2186   /* Change RTC state */
2187   hrtc->State = HAL_RTC_STATE_READY;
2188 
2189   return HAL_OK;
2190 }
2191 #endif /* RTC_TAMPER3_SUPPORT */
2192 
2193 
2194 
2195 #if defined(RTC_TAMPER1_SUPPORT)
2196 /**
2197   * @brief  Tamper 1 callback.
2198   * @param  hrtc RTC handle
2199   * @retval None
2200   */
2201 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
2202 {
2203   /* Prevent unused argument(s) compilation warning */
2204   UNUSED(hrtc);
2205 
2206   /* NOTE : This function should not be modified, when the callback is needed,
2207             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
2208    */
2209 }
2210 #endif /* RTC_TAMPER1_SUPPORT */
2211 
2212 /**
2213   * @brief  Tamper 2 callback.
2214   * @param  hrtc RTC handle
2215   * @retval None
2216   */
2217 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
2218 {
2219   /* Prevent unused argument(s) compilation warning */
2220   UNUSED(hrtc);
2221 
2222   /* NOTE : This function should not be modified, when the callback is needed,
2223             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
2224    */
2225 }
2226 
2227 #if defined(RTC_TAMPER3_SUPPORT)
2228 /**
2229   * @brief  Tamper 3 callback.
2230   * @param  hrtc RTC handle
2231   * @retval None
2232   */
2233 __weak void HAL_RTCEx_Tamper3EventCallback(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_Tamper3EventCallback could be implemented in the user file
2240    */
2241 }
2242 #endif /* RTC_TAMPER3_SUPPORT */
2243 
2244 /**
2245   * @}
2246   */
2247 
2248 
2249 /** @addtogroup RTCEx_Exported_Functions_Group6
2250   * @brief      Extended RTC Backup register functions
2251   *
2252 @verbatim
2253   ===============================================================================
2254              ##### Extended RTC Backup register functions #####
2255   ===============================================================================
2256   [..]
2257    (+) Before calling any tamper or internal tamper function, you have to call first
2258        HAL_RTC_Init() function.
2259    (+) In that ine you can select to output tamper event on RTC pin.
2260   [..]
2261    This subsection provides functions allowing to
2262    (+) Write a data in a specified RTC Backup data register
2263    (+) Read a data in a specified RTC Backup data register
2264 @endverbatim
2265   * @{
2266   */
2267 
2268 
2269 /**
2270   * @brief  Write a data in a specified RTC Backup data register.
2271   * @param  hrtc RTC handle
2272   * @param  BackupRegister RTC Backup data Register number.
2273   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
2274   *          specify the register.
2275   * @param  Data Data to be written in the specified Backup data register.
2276   * @retval None
2277   */
2278 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
2279 {
2280   uint32_t __IO tmp;
2281 #if defined(STM32L412xx) || defined(STM32L422xx)
2282   /* Process TAMP instance pointer */
2283   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
2284 
2285   /* Check the parameters */
2286   assert_param(IS_RTC_BKP(BackupRegister));
2287 
2288   tmp = (uint32_t)&(tamp->BKP0R);
2289 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
2290   /* Check the parameters */
2291   assert_param(IS_RTC_BKP(BackupRegister));
2292 
2293   tmp = (uint32_t)&(hrtc->Instance->BKP0R);
2294 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
2295 
2296   tmp += (BackupRegister * 4U);
2297 
2298   /* Write the specified register */
2299   *(__IO uint32_t *)tmp = (uint32_t)Data;
2300 }
2301 
2302 
2303 /**
2304   * @brief  Read data from the specified RTC Backup data Register.
2305   * @param  hrtc RTC handle
2306   * @param  BackupRegister  RTC Backup data Register number.
2307   *         This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
2308   *         specify the register.
2309   * @retval Read value
2310   */
2311 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
2312 {
2313   uint32_t tmp;
2314 #if defined(STM32L412xx) || defined(STM32L422xx)
2315   /* Process TAMP instance pointer */
2316   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
2317 
2318   /* Check the parameters */
2319   assert_param(IS_RTC_BKP(BackupRegister));
2320 
2321   tmp = (uint32_t)&(tamp->BKP0R);
2322 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
2323   /* Check the parameters */
2324   assert_param(IS_RTC_BKP(BackupRegister));
2325 
2326   tmp = (uint32_t)&(hrtc->Instance->BKP0R);
2327 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
2328 
2329   tmp += (BackupRegister * 4U);
2330 
2331   /* Read the specified register */
2332   return (*(__IO uint32_t *)tmp);
2333 }
2334 
2335 
2336 /**
2337   * @}
2338   */
2339 
2340 /**
2341   * @}
2342   */
2343 
2344 #endif /* HAL_RTC_MODULE_ENABLED */
2345 
2346 /**
2347   * @}
2348   */
2349 
2350 /**
2351   * @}
2352   */
2353 
2354 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2355 
2356