STM32L443xx HAL User Manual
stm32l4xx_hal_rtc_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_rtc_ex.c
00004   * @author  MCD Application Team
00005   * @brief   Extended RTC HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Real Time Clock (RTC) Extended peripheral:
00008   *           + RTC Time Stamp functions
00009   *           + RTC Tamper functions
00010   *           + RTC Wake-up functions
00011   *           + Extended Control functions
00012   *           + Extended RTC features functions
00013   *
00014   ******************************************************************************
00015   * @attention
00016   *
00017   * Copyright (c) 2017 STMicroelectronics.
00018   * All rights reserved.
00019   *
00020   * This software is licensed under terms that can be found in the LICENSE file
00021   * in the root directory of this software component.
00022   * If no LICENSE file comes with this software, it is provided AS-IS.
00023   *
00024   ******************************************************************************
00025   @verbatim
00026   ==============================================================================
00027                   ##### How to use this driver #####
00028   ==============================================================================
00029   [..]
00030     (+) Enable the RTC domain access.
00031     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
00032         format using the HAL_RTC_Init() function.
00033 
00034   *** RTC Wakeup configuration ***
00035   ================================
00036   [..]
00037     (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
00038         function. You can also configure the RTC Wakeup timer with interrupt mode
00039         using the HAL_RTCEx_SetWakeUpTimer_IT() function.
00040     (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
00041         function.
00042 
00043   *** Outputs configuration ***
00044   =============================
00045   [..]  The RTC has 2 different outputs:
00046     (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
00047         and WaKeUp signals.
00048         To output the selected RTC signal, use the HAL_RTC_Init() function.
00049     (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
00050         To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
00051     (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB2) managed on
00052         the RTC_OR register.
00053     (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
00054         automatically configured in output alternate function.
00055 
00056   *** Smooth digital Calibration configuration ***
00057   ================================================
00058   [..]
00059     (+) Configure the RTC Original Digital Calibration Value and the corresponding
00060         calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib()
00061         function.
00062 
00063   *** TimeStamp configuration ***
00064   ===============================
00065   [..]
00066     (+) Enable the RTC TimeStamp using the HAL_RTCEx_SetTimeStamp() function.
00067         You can also configure the RTC TimeStamp with interrupt mode using the
00068         HAL_RTCEx_SetTimeStamp_IT() function.
00069     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
00070         function.
00071 
00072   *** Internal TimeStamp configuration ***
00073   ===============================
00074   [..]
00075     (+) Enable the RTC internal TimeStamp using the HAL_RTCEx_SetInternalTimeStamp() function.
00076         User has to check internal timestamp occurrence using __HAL_RTC_INTERNAL_TIMESTAMP_GET_FLAG.
00077     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
00078         function.
00079 
00080    *** Tamper configuration ***
00081    ============================
00082    [..]
00083      (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
00084          or Level according to the Tamper filter (if equal to 0 Edge else Level)
00085          value, sampling frequency, NoErase, MaskFlag,  precharge or discharge and
00086          Pull-UP using the HAL_RTCEx_SetTamper() function. You can configure RTC Tamper
00087          with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
00088      (+) The default configuration of the Tamper erases the backup registers. To avoid
00089          erase, enable the NoErase field on the RTC_TAMPCR register.
00090      (+) STM32L412xx and STM32L422xx only : With new RTC tamper configuration, you have to call HAL_RTC_Init() in order to
00091          perform TAMP base address offset calculation.
00092      (+) STM32L412xx and STM32L422xx only : If you don't intend to have tamper using RTC clock, you can bypass its initialization
00093          by setting ClockEnable inti field to RTC_CLOCK_DISABLE.
00094      (+) STM32L412xx and STM32L422xx only : Enable Internal tamper using HAL_RTCEx_SetInternalTamper. IT mode can be chosen using
00095          setting Interrupt field.
00096 
00097    *** Backup Data Registers configuration ***
00098    ===========================================
00099    [..]
00100      (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
00101          function.
00102      (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
00103          function.
00104      (+) STM32L412xx and STM32L422xx only : Before calling these functions you have to call HAL_RTC_Init() in order to
00105          perform TAMP base address offset calculation.
00106 
00107   @endverbatim
00108   ******************************************************************************
00109   */
00110 
00111 /* Includes ------------------------------------------------------------------*/
00112 #include "stm32l4xx_hal.h"
00113 
00114 /** @addtogroup STM32L4xx_HAL_Driver
00115   * @{
00116   */
00117 
00118 /** @addtogroup RTCEx
00119   * @brief RTC Extended HAL module driver
00120   * @{
00121   */
00122 
00123 #ifdef HAL_RTC_MODULE_ENABLED
00124 
00125 /* Private typedef -----------------------------------------------------------*/
00126 /* Private define ------------------------------------------------------------*/
00127 /* Private macro -------------------------------------------------------------*/
00128 /* Private variables ---------------------------------------------------------*/
00129 /* Private function prototypes -----------------------------------------------*/
00130 /* Exported functions --------------------------------------------------------*/
00131 
00132 /** @addtogroup RTCEx_Exported_Functions
00133   * @{
00134   */
00135 
00136 
00137 /** @addtogroup RTCEx_Exported_Functions_Group1
00138   * @brief   RTC TimeStamp and Tamper functions
00139   *
00140 @verbatim
00141  ===============================================================================
00142                  ##### RTC TimeStamp and Tamper functions #####
00143  ===============================================================================
00144 
00145  [..] This section provides functions allowing to configure TimeStamp feature
00146 
00147 @endverbatim
00148   * @{
00149   */
00150 
00151 /**
00152   * @brief  Set TimeStamp.
00153   * @note   This API must be called before enabling the TimeStamp feature.
00154   * @param  hrtc RTC handle
00155   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
00156   *         activated.
00157   *          This parameter can be one of the following values:
00158   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
00159   *                                        rising edge of the related pin.
00160   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
00161   *                                         falling edge of the related pin.
00162   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
00163   *          This parameter can be one of the following values:
00164   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
00165   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
00166   *               compatibility, this parameter is required.
00167   * @retval HAL status
00168   */
00169 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
00170 {
00171   uint32_t tmpreg;
00172 
00173   /* Check the parameters */
00174   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
00175   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
00176 
00177   /* Prevent unused argument(s) compilation warning if no assert_param check */
00178   UNUSED(RTC_TimeStampPin);
00179 
00180   /* Process Locked */
00181   __HAL_LOCK(hrtc);
00182 
00183   hrtc->State = HAL_RTC_STATE_BUSY;
00184 
00185   /* Get the RTC_CR register and clear the bits to be configured */
00186   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
00187 
00188   tmpreg |= TimeStampEdge;
00189 
00190   /* Disable the write protection for RTC registers */
00191   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00192 
00193   /* Configure the Time Stamp TSEDGE and Enable bits */
00194   hrtc->Instance->CR = (uint32_t)tmpreg;
00195 
00196   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
00197 
00198   /* Enable the write protection for RTC registers */
00199   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00200 
00201   /* Change RTC state */
00202   hrtc->State = HAL_RTC_STATE_READY;
00203 
00204   /* Process Unlocked */
00205   __HAL_UNLOCK(hrtc);
00206 
00207   return HAL_OK;
00208 }
00209 
00210 /**
00211   * @brief  Set TimeStamp with Interrupt.
00212   * @note   This API must be called before enabling the TimeStamp feature.
00213   * @param  hrtc RTC handle
00214   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
00215   *         activated.
00216   *          This parameter can be one of the following values:
00217   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
00218   *                                        rising edge of the related pin.
00219   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
00220   *                                         falling edge of the related pin.
00221   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
00222   *          This parameter can be one of the following values:
00223   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
00224   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
00225   *               compatibility, this parameter is required.
00226   * @retval HAL status
00227   */
00228 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
00229 {
00230   uint32_t tmpreg;
00231 
00232   /* Check the parameters */
00233   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
00234   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
00235 
00236   /* Prevent unused argument(s) compilation warning if no assert_param check */
00237   UNUSED(RTC_TimeStampPin);
00238 
00239   /* Process Locked */
00240   __HAL_LOCK(hrtc);
00241 
00242   hrtc->State = HAL_RTC_STATE_BUSY;
00243 
00244   /* Get the RTC_CR register and clear the bits to be configured */
00245   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
00246 
00247   tmpreg |= TimeStampEdge;
00248 
00249   /* Disable the write protection for RTC registers */
00250   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00251 
00252   /* Configure the Time Stamp TSEDGE and Enable bits */
00253   hrtc->Instance->CR = (uint32_t)tmpreg;
00254 
00255   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
00256 
00257   /* Enable IT timestamp */
00258   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
00259 
00260   /* RTC timestamp Interrupt Configuration: EXTI configuration */
00261   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
00262   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
00263 
00264   /* Enable the write protection for RTC registers */
00265   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00266 
00267   hrtc->State = HAL_RTC_STATE_READY;
00268 
00269   /* Process Unlocked */
00270   __HAL_UNLOCK(hrtc);
00271 
00272   return HAL_OK;
00273 }
00274 
00275 /**
00276   * @brief  Deactivate TimeStamp.
00277   * @param  hrtc RTC handle
00278   * @retval HAL status
00279   */
00280 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
00281 {
00282   uint32_t tmpreg;
00283 
00284   /* Process Locked */
00285   __HAL_LOCK(hrtc);
00286 
00287   hrtc->State = HAL_RTC_STATE_BUSY;
00288 
00289   /* Disable the write protection for RTC registers */
00290   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00291 
00292   /* In case of interrupt mode is used, the interrupt source must disabled */
00293   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
00294 
00295   /* Get the RTC_CR register and clear the bits to be configured */
00296   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
00297 
00298   /* Configure the Time Stamp TSEDGE and Enable bits */
00299   hrtc->Instance->CR = (uint32_t)tmpreg;
00300 
00301   /* Enable the write protection for RTC registers */
00302   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00303 
00304   hrtc->State = HAL_RTC_STATE_READY;
00305 
00306   /* Process Unlocked */
00307   __HAL_UNLOCK(hrtc);
00308 
00309   return HAL_OK;
00310 }
00311 
00312 /**
00313   * @brief  Set Internal TimeStamp.
00314   * @note   This API must be called before enabling the internal TimeStamp feature.
00315   * @param  hrtc RTC handle
00316   * @retval HAL status
00317   */
00318 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
00319 {
00320   /* Process Locked */
00321   __HAL_LOCK(hrtc);
00322 
00323   hrtc->State = HAL_RTC_STATE_BUSY;
00324 
00325   /* Disable the write protection for RTC registers */
00326   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00327 
00328   /* Configure the internal Time Stamp Enable bits */
00329   __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);
00330 
00331   /* Enable the write protection for RTC registers */
00332   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00333 
00334   /* Change RTC state */
00335   hrtc->State = HAL_RTC_STATE_READY;
00336 
00337   /* Process Unlocked */
00338   __HAL_UNLOCK(hrtc);
00339 
00340   return HAL_OK;
00341 }
00342 
00343 /**
00344   * @brief  Deactivate Internal TimeStamp.
00345   * @param  hrtc RTC handle
00346   * @retval HAL status
00347   */
00348 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
00349 {
00350   /* Process Locked */
00351   __HAL_LOCK(hrtc);
00352 
00353   hrtc->State = HAL_RTC_STATE_BUSY;
00354 
00355   /* Disable the write protection for RTC registers */
00356   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00357 
00358   /* Configure the internal Time Stamp Enable bits */
00359   __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
00360 
00361   /* Enable the write protection for RTC registers */
00362   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00363 
00364   hrtc->State = HAL_RTC_STATE_READY;
00365 
00366   /* Process Unlocked */
00367   __HAL_UNLOCK(hrtc);
00368 
00369   return HAL_OK;
00370 }
00371 
00372 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00373 /**
00374   * @brief  Get the RTC TimeStamp value.
00375   * @param  hrtc RTC handle
00376   * @param  sTimeStamp Pointer to Time structure
00377   *          if BinMode = RTC_BINARY_ONLY, sTimeStamp->SubSeconds only is used
00378   * @param  sTimeStampDate Pointer to Date structure
00379   *          if BinMode = RTC_BINARY_ONLY, this parameter is not used.
00380   * @param  Format specifies the format of the entered parameters.
00381   *          if BinMode = RTC_BINARY_ONLY, this parameter is not used
00382   *          else this parameter can be one of the following values
00383   *             @arg RTC_FORMAT_BIN: Binary data format
00384   *             @arg RTC_FORMAT_BCD: BCD data format
00385   * @retval HAL status
00386   */
00387 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
00388 {
00389   uint32_t tmptime, tmpdate;
00390   UNUSED(hrtc);
00391 
00392   sTimeStamp->SubSeconds = READ_REG(RTC->TSSSR);
00393   if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
00394   {
00395     /* Check the parameters */
00396     assert_param(IS_RTC_FORMAT(Format));
00397 
00398     /* Get the TimeStamp time and date registers values */
00399     tmptime = READ_BIT(RTC->TSTR, RTC_TR_RESERVED_MASK);
00400     tmpdate = READ_BIT(RTC->TSDR, RTC_DR_RESERVED_MASK);
00401 
00402     /* Fill the Time structure fields with the read parameters */
00403     sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
00404     sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
00405     sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
00406     sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
00407     sTimeStamp->SubSeconds = READ_BIT(RTC->TSSSR, RTC_TSSSR_SS);
00408 
00409     /* Fill the Date structure fields with the read parameters */
00410     sTimeStampDate->Year = 0U;
00411     sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
00412     sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU));
00413     sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
00414 
00415     /* Check the input parameters format */
00416     if (Format == RTC_FORMAT_BIN)
00417     {
00418       /* Convert the TimeStamp structure parameters to Binary format */
00419       sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
00420       sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
00421       sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
00422 
00423       /* Convert the DateTimeStamp structure parameters to Binary format */
00424       sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
00425       sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
00426       sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
00427     }
00428   }
00429 
00430   /* Clear the TIMESTAMP Flags */
00431   WRITE_REG(RTC->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
00432 
00433   return HAL_OK;
00434 }
00435 #else /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
00436 
00437 /**
00438   * @brief  Get the RTC TimeStamp value.
00439   * @param  hrtc RTC handle
00440   * @param  sTimeStamp Pointer to Time structure
00441   * @param  sTimeStampDate Pointer to Date structure
00442   * @param  Format specifies the format of the entered parameters.
00443   *          This parameter can be one of the following values:
00444   *             @arg RTC_FORMAT_BIN: Binary data format
00445   *             @arg RTC_FORMAT_BCD: BCD data format
00446   * @retval HAL status
00447   */
00448 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
00449 {
00450   uint32_t tmptime, tmpdate;
00451 
00452   /* Check the parameters */
00453   assert_param(IS_RTC_FORMAT(Format));
00454 
00455   /* Get the TimeStamp time and date registers values */
00456   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
00457   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
00458 
00459   /* Fill the Time structure fields with the read parameters */
00460   sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
00461   sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
00462   sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
00463   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
00464   sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
00465 
00466   /* Fill the Date structure fields with the read parameters */
00467   sTimeStampDate->Year = 0U;
00468   sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
00469   sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
00470   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
00471 
00472   /* Check the input parameters format */
00473   if (Format == RTC_FORMAT_BIN)
00474   {
00475     /* Convert the TimeStamp structure parameters to Binary format */
00476     sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
00477     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
00478     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
00479 
00480     /* Convert the DateTimeStamp structure parameters to Binary format */
00481     sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
00482     sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
00483     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
00484   }
00485 
00486   /* Clear the TIMESTAMP Flags */
00487   __HAL_RTC_INTERNAL_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_ITSF);
00488   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
00489 
00490   return HAL_OK;
00491 }
00492 #endif /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
00493 
00494 /**
00495   * @brief  Handle TimeStamp interrupt request.
00496   * @param  hrtc RTC handle
00497   * @retval None
00498   */
00499 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00500 
00501 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
00502 {
00503 
00504   /* Process TAMP instance pointer */
00505   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
00506 
00507   /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
00508   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
00509 
00510   if ((hrtc->Instance->MISR & RTC_MISR_TSMF) != 0u)
00511   {
00512 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00513     /* Call TimeStampEvent registered Callback */
00514     hrtc->TimeStampEventCallback(hrtc);
00515 #else
00516     HAL_RTCEx_TimeStampEventCallback(hrtc);
00517 #endif
00518     /* Not immediatly clear flags because the content of RTC_TSTR and RTC_TSDR arecleared when TSF bit is reset.*/
00519     hrtc->Instance->SCR = RTC_SCR_CTSF;
00520   }
00521 
00522   /* Get interrupt status */
00523   uint32_t tmp = tamp->MISR;
00524 
00525   /* Immediatly clear flags */
00526   tamp->SCR = tmp;
00527 
00528 #if defined(RTC_TAMPER1_SUPPORT)
00529   /* Check Tamper1 status */
00530   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
00531   {
00532 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00533     /* Call Tamper 1 Event registered Callback */
00534     hrtc->Tamper1EventCallback(hrtc);
00535 #else
00536     /* Tamper1 callback */
00537     HAL_RTCEx_Tamper1EventCallback(hrtc);
00538 #endif
00539   }
00540 #endif /* RTC_TAMPER1_SUPPORT */
00541 
00542   /* Check Tamper2 status */
00543   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
00544   {
00545 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00546     /* Call Tamper 2 Event registered Callback */
00547     hrtc->Tamper2EventCallback(hrtc);
00548 #else
00549     /* Tamper2 callback */
00550     HAL_RTCEx_Tamper2EventCallback(hrtc);
00551 #endif
00552   }
00553 
00554 #if defined(RTC_TAMPER3_SUPPORT)
00555   /* Check Tamper3 status */
00556   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
00557   {
00558 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00559     /* Call Tamper 3 Event registered Callback */
00560     hrtc->Tamper3EventCallback(hrtc);
00561 #else
00562     /* Tamper3 callback */
00563     HAL_RTCEx_Tamper3EventCallback(hrtc);
00564 #endif
00565   }
00566 
00567 #endif /* RTC_TAMPER3_SUPPORT */
00568 
00569   /* Change RTC state */
00570   hrtc->State = HAL_RTC_STATE_READY;
00571 }
00572 
00573 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
00574 
00575 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
00576 {
00577   /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
00578   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
00579 
00580   /* Get the TimeStamp interrupt source enable status and pending flag status */
00581   if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
00582   {
00583     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
00584     {
00585       /* TIMESTAMP callback */
00586 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00587       hrtc->TimeStampEventCallback(hrtc);
00588 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00589       HAL_RTCEx_TimeStampEventCallback(hrtc);
00590 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00591 
00592       /* Clear the TIMESTAMP interrupt pending bit (this will clear timestamp time and date registers) */
00593       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
00594     }
00595   }
00596 
00597 #if defined(RTC_TAMPER1_SUPPORT)
00598   /* Get the Tamper1 interrupt source enable status and pending flag status */
00599   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != 0U)
00600   {
00601     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
00602     {
00603       /* Clear the Tamper1 interrupt pending bit */
00604       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
00605 
00606       /* Tamper1 callback */
00607 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00608       hrtc->Tamper1EventCallback(hrtc);
00609 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00610       HAL_RTCEx_Tamper1EventCallback(hrtc);
00611 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00612     }
00613   }
00614 #endif /* RTC_TAMPER1_SUPPORT */
00615 
00616   /* Get the Tamper2 interrupt source enable status and pending flag status */
00617   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != 0U)
00618   {
00619     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
00620     {
00621       /* Clear the Tamper2 interrupt pending bit */
00622       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
00623 
00624       /* Tamper2 callback */
00625 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00626       hrtc->Tamper2EventCallback(hrtc);
00627 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00628       HAL_RTCEx_Tamper2EventCallback(hrtc);
00629 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00630     }
00631   }
00632 
00633 #if defined(RTC_TAMPER3_SUPPORT)
00634   /* Get the Tamper3 interrupts source enable status */
00635   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != 0U)
00636   {
00637     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
00638     {
00639       /* Clear the Tamper3 interrupt pending bit */
00640       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
00641 
00642       /* Tamper3 callback */
00643 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00644       hrtc->Tamper3EventCallback(hrtc);
00645 #else
00646       HAL_RTCEx_Tamper3EventCallback(hrtc);
00647 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
00648     }
00649   }
00650 #endif /* RTC_TAMPER3_SUPPORT */
00651 
00652   /* Change RTC state */
00653   hrtc->State = HAL_RTC_STATE_READY;
00654 }
00655 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
00656 
00657 /**
00658   * @brief  TimeStamp callback.
00659   * @param  hrtc RTC handle
00660   * @retval None
00661   */
00662 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
00663 {
00664   /* Prevent unused argument(s) compilation warning */
00665   UNUSED(hrtc);
00666 
00667   /* NOTE : This function should not be modified, when the callback is needed,
00668             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
00669    */
00670 }
00671 
00672 /**
00673   * @brief  Handle TimeStamp polling request.
00674   * @param  hrtc RTC handle
00675   * @param  Timeout Timeout duration
00676   * @retval HAL status
00677   */
00678 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
00679 {
00680   uint32_t tickstart = HAL_GetTick();
00681 
00682   while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
00683   {
00684     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
00685     {
00686       /* Clear the TIMESTAMP OverRun Flag */
00687       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
00688 
00689       /* Change TIMESTAMP state */
00690       hrtc->State = HAL_RTC_STATE_ERROR;
00691 
00692       return HAL_ERROR;
00693     }
00694 
00695     if (Timeout != HAL_MAX_DELAY)
00696     {
00697       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
00698       {
00699         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00700         return HAL_TIMEOUT;
00701       }
00702     }
00703   }
00704 
00705   /* Change RTC state */
00706   hrtc->State = HAL_RTC_STATE_READY;
00707 
00708   return HAL_OK;
00709 }
00710 
00711 /**
00712   * @}
00713   */
00714 
00715 /** @addtogroup RTCEx_Exported_Functions_Group2
00716   * @brief    RTC Wake-up functions
00717   *
00718 @verbatim
00719  ===============================================================================
00720                         ##### RTC Wake-up functions #####
00721  ===============================================================================
00722 
00723  [..] This section provides functions allowing to configure Wake-up feature
00724 
00725 @endverbatim
00726   * @{
00727   */
00728 
00729 /**
00730   * @brief  Set wake up timer.
00731   * @param  hrtc RTC handle
00732   * @param  WakeUpCounter Wake up counter
00733   * @param  WakeUpClock Wake up clock
00734   * @retval HAL status
00735   */
00736 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
00737 {
00738   uint32_t tickstart;
00739 
00740   /* Check the parameters */
00741   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
00742   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
00743 
00744   /* Process Locked */
00745   __HAL_LOCK(hrtc);
00746 
00747   hrtc->State = HAL_RTC_STATE_BUSY;
00748 
00749   /* Disable the write protection for RTC registers */
00750   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00751 
00752   /* Clear WUTE in RTC_CR to disable the wakeup timer */
00753   CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
00754 
00755   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
00756      counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
00757      calendar initialization mode. */
00758 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00759   if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U)
00760 #else
00761   if (READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U)
00762 #endif
00763   {
00764     tickstart = HAL_GetTick();
00765 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00766     while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_WUTWF) == 0U)
00767 #else
00768     while (READ_BIT(hrtc->Instance->ISR, RTC_ISR_WUTWF) == 0U)
00769 #endif
00770     {
00771       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
00772       {
00773         /* Enable the write protection for RTC registers */
00774         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00775 
00776         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00777 
00778         /* Process Unlocked */
00779         __HAL_UNLOCK(hrtc);
00780 
00781         return HAL_TIMEOUT;
00782       }
00783     }
00784   }
00785 
00786   /* Configure the clock source */
00787   MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
00788 
00789   /* Configure the Wakeup Timer counter */
00790   WRITE_REG(hrtc->Instance->WUTR, (uint32_t)WakeUpCounter);
00791 
00792   /* Enable the Wakeup Timer */
00793   SET_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
00794 
00795   /* Enable the write protection for RTC registers */
00796   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00797 
00798   hrtc->State = HAL_RTC_STATE_READY;
00799 
00800   /* Process Unlocked */
00801   __HAL_UNLOCK(hrtc);
00802 
00803   return HAL_OK;
00804 }
00805 
00806 /**
00807   * @brief  Set wake up timer with interrupt.
00808   * @param  hrtc RTC handle
00809   * @param  WakeUpCounter Wake up counter
00810   * @param  WakeUpClock Wake up clock
00811   * @param  WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
00812   *                       - Only available for STM32L412xx and STM32L422xx
00813   *                       - No effect if WakeUpAutoClr is set to zero
00814   *                       - This feature is meaningful in case of Low power mode to avoid any RTC software execution after Wake Up.
00815   *                         That is why when WakeUpAutoClr is set, EXTI is configured as EVENT instead of Interrupt to avoid useless IRQ handler execution.
00816   * @retval HAL status
00817   */
00818 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00819 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock, uint32_t WakeUpAutoClr)
00820 #else
00821 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
00822 #endif
00823 {
00824   uint32_t tickstart;
00825 
00826   /* Check the parameters */
00827   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
00828   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
00829 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00830   /* (0x0000<=WUTOCLR<=WUT) */
00831   assert_param(WakeUpAutoClr <= WakeUpCounter);
00832 #endif
00833 
00834   /* Process Locked */
00835   __HAL_LOCK(hrtc);
00836 
00837   hrtc->State = HAL_RTC_STATE_BUSY;
00838 
00839   /* Disable the write protection for RTC registers */
00840   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00841 
00842   /* Clear WUTE in RTC_CR to disable the wakeup timer */
00843   CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
00844 
00845   /* Clear flag Wake-Up */
00846   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
00847 
00848   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
00849      counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
00850      calendar initialization mode. */
00851 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00852   if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U)
00853 #else
00854   if (READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U)
00855 #endif
00856   {
00857     tickstart = HAL_GetTick();
00858 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00859     while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_WUTWF) == 0U)
00860 #else
00861     while (READ_BIT(hrtc->Instance->ISR, RTC_ISR_WUTWF) == 0U)
00862 #endif
00863     {
00864       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
00865       {
00866         /* Enable the write protection for RTC registers */
00867         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00868 
00869         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00870 
00871         /* Process Unlocked */
00872         __HAL_UNLOCK(hrtc);
00873 
00874         return HAL_TIMEOUT;
00875       }
00876     }
00877   }
00878 
00879 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00880   /* Configure the Wakeup Timer counter and auto clear value */
00881   hrtc->Instance->WUTR = (uint32_t)(WakeUpCounter | (WakeUpAutoClr << RTC_WUTR_WUTOCLR_Pos));
00882 #else
00883   /* Configure the Wakeup Timer counter */
00884   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
00885 #endif
00886 
00887   /* Configure the clock source */
00888   MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
00889 
00890 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
00891   /* In case of WUT autoclr, the IRQ handler should not be called */
00892   if (WakeUpAutoClr != 0u)
00893   {
00894     /* RTC WakeUpTimer EXTI Configuration: Event configuration */
00895     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
00896   }
00897   else
00898   {
00899     /* RTC WakeUpTimer EXTI Configuration: Interrupt configuration */
00900     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
00901   }
00902 #else /* defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
00903   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
00904 #endif /* defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
00905 
00906   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
00907 
00908   /* Configure the Interrupt in the RTC_CR register */
00909   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
00910 
00911   /* Enable the Wakeup Timer */
00912   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
00913 
00914   /* Enable the write protection for RTC registers */
00915   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00916 
00917   hrtc->State = HAL_RTC_STATE_READY;
00918 
00919   /* Process Unlocked */
00920   __HAL_UNLOCK(hrtc);
00921 
00922   return HAL_OK;
00923 }
00924 
00925 /**
00926   * @brief  Deactivate wake up timer counter.
00927   * @param  hrtc RTC handle
00928   * @retval HAL status
00929   */
00930 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
00931 {
00932   uint32_t tickstart;
00933 
00934   /* Process Locked */
00935   __HAL_LOCK(hrtc);
00936 
00937   hrtc->State = HAL_RTC_STATE_BUSY;
00938 
00939   /* Disable the write protection for RTC registers */
00940   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00941 
00942   /* Disable the Wakeup Timer */
00943   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
00944 
00945   /* In case of interrupt mode is used, the interrupt source must disabled */
00946   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
00947 
00948   tickstart = HAL_GetTick();
00949   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
00950   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
00951   {
00952     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
00953     {
00954       /* Enable the write protection for RTC registers */
00955       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00956 
00957       hrtc->State = HAL_RTC_STATE_TIMEOUT;
00958 
00959       /* Process Unlocked */
00960       __HAL_UNLOCK(hrtc);
00961 
00962       return HAL_TIMEOUT;
00963     }
00964   }
00965 
00966   /* Enable the write protection for RTC registers */
00967   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00968 
00969   hrtc->State = HAL_RTC_STATE_READY;
00970 
00971   /* Process Unlocked */
00972   __HAL_UNLOCK(hrtc);
00973 
00974   return HAL_OK;
00975 }
00976 
00977 /**
00978   * @brief  Get wake up timer counter.
00979   * @param  hrtc RTC handle
00980   * @retval Counter value
00981   */
00982 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
00983 {
00984   /* Get the counter value */
00985   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
00986 }
00987 
00988 /**
00989   * @brief  Handle Wake Up Timer interrupt request.
00990   * @param  hrtc RTC handle
00991   * @retval None
00992   */
00993 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
00994 {
00995   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
00996   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
00997 
00998 
00999 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01000   if ((hrtc->Instance->MISR & RTC_MISR_WUTMF) != 0u)
01001   {
01002     /* Immediately clear flags */
01003     hrtc->Instance->SCR = RTC_SCR_CWUTF;
01004 #else
01005   /* Get the pending status of the WAKEUPTIMER Interrupt */
01006   if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
01007   {
01008     /* Clear the WAKEUPTIMER interrupt pending bit */
01009     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
01010 #endif
01011 
01012     /* WAKEUPTIMER callback */
01013 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
01014     /* Call WakeUpTimerEvent registered Callback */
01015     hrtc->WakeUpTimerEventCallback(hrtc);
01016 #else
01017     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
01018 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
01019   }
01020 
01021   /* Change RTC state */
01022   hrtc->State = HAL_RTC_STATE_READY;
01023 }
01024 
01025 /**
01026   * @brief  Wake Up Timer callback.
01027   * @param  hrtc RTC handle
01028   * @retval None
01029   */
01030 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
01031 {
01032   /* Prevent unused argument(s) compilation warning */
01033   UNUSED(hrtc);
01034 
01035   /* NOTE : This function should not be modified, when the callback is needed,
01036             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
01037    */
01038 }
01039 
01040 
01041 /**
01042   * @brief  Handle Wake Up Timer Polling.
01043   * @param  hrtc RTC handle
01044   * @param  Timeout Timeout duration
01045   * @retval HAL status
01046   */
01047 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
01048 {
01049   uint32_t tickstart = HAL_GetTick();
01050 
01051   while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
01052   {
01053     if (Timeout != HAL_MAX_DELAY)
01054     {
01055       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
01056       {
01057         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01058         return HAL_TIMEOUT;
01059       }
01060     }
01061   }
01062 
01063   /* Clear the WAKEUPTIMER Flag */
01064   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
01065 
01066   /* Change RTC state */
01067   hrtc->State = HAL_RTC_STATE_READY;
01068 
01069   return HAL_OK;
01070 }
01071 
01072 /**
01073   * @}
01074   */
01075 
01076 
01077 /** @addtogroup RTCEx_Exported_Functions_Group3
01078   * @brief    Extended Peripheral Control functions
01079   *
01080 @verbatim
01081  ===============================================================================
01082               ##### Extended Peripheral Control functions #####
01083  ===============================================================================
01084     [..]
01085     This subsection provides functions allowing to
01086       (+) Write a data in a specified RTC Backup data register
01087       (+) Read a data in a specified RTC Backup data register
01088       (+) Set the Coarse calibration parameters.
01089       (+) Deactivate the Coarse calibration parameters
01090       (+) Set the Smooth calibration parameters.
01091       (+) STM32L412xx and STM32L422xx only : Set Low Power calibration parameter.
01092       (+) Configure the Synchronization Shift Control Settings.
01093       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01094       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01095       (+) Enable the RTC reference clock detection.
01096       (+) Disable the RTC reference clock detection.
01097       (+) Enable the Bypass Shadow feature.
01098       (+) Disable the Bypass Shadow feature.
01099 
01100 @endverbatim
01101   * @{
01102   */
01103 
01104 
01105 /**
01106   * @brief  Set the Smooth calibration parameters.
01107   * @param  hrtc RTC handle
01108   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
01109   *          This parameter can be can be one of the following values :
01110   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
01111   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
01112   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
01113   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
01114   *          This parameter can be one of the following values:
01115   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
01116   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
01117   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
01118   *          This parameter can be one any value from 0 to 0x000001FF.
01119   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
01120   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
01121   *         SmoothCalibMinusPulsesValue must be equal to 0.
01122   * @retval HAL status
01123   */
01124 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
01125 {
01126   uint32_t tickstart;
01127 
01128   /* Check the parameters */
01129   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
01130   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
01131   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
01132 
01133   /* Process Locked */
01134   __HAL_LOCK(hrtc);
01135 
01136   hrtc->State = HAL_RTC_STATE_BUSY;
01137 
01138   /* Disable the write protection for RTC registers */
01139   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01140 
01141   /* check if a calibration is pending*/
01142 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01143   if ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
01144 #else
01145   if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
01146 #endif
01147   {
01148     tickstart = HAL_GetTick();
01149 
01150     /* check if a calibration is pending*/
01151 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01152     while ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
01153 #else
01154     while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
01155 #endif
01156     {
01157       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01158       {
01159         /* Enable the write protection for RTC registers */
01160         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01161 
01162         /* Change RTC state */
01163         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01164 
01165         /* Process Unlocked */
01166         __HAL_UNLOCK(hrtc);
01167 
01168         return HAL_TIMEOUT;
01169       }
01170     }
01171   }
01172 
01173   /* Configure the Smooth calibration settings */
01174   MODIFY_REG(hrtc->Instance->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
01175 
01176   /* Enable the write protection for RTC registers */
01177   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01178 
01179   /* Change RTC state */
01180   hrtc->State = HAL_RTC_STATE_READY;
01181 
01182   /* Process Unlocked */
01183   __HAL_UNLOCK(hrtc);
01184 
01185   return HAL_OK;
01186 }
01187 
01188 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01189 /**
01190   * @brief  Select the low power Calibration mode.
01191   * @param  hrtc RTC handle
01192   * @param  LowPowerCalib Low power Calibration mode.
01193   *          This parameter can be can be one of the following values :
01194   *             @arg RTC_LPCAL_SET: Low power mode.
01195   *             @arg RTC_LPCAL_RESET: High consumption mode.
01196   * @retval HAL status
01197   */
01198 HAL_StatusTypeDef HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef *hrtc, uint32_t LowPowerCalib)
01199 {
01200   /* Check the parameters */
01201   assert_param(IS_RTC_LOW_POWER_CALIB(LowPowerCalib));
01202 
01203   /* Process Locked */
01204   __HAL_LOCK(hrtc);
01205 
01206   hrtc->State = HAL_RTC_STATE_BUSY;
01207 
01208   /* Disable the write protection for RTC registers */
01209   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01210 
01211   /* Configure the Smooth calibration settings */
01212   MODIFY_REG(hrtc->Instance->CALR, RTC_CALR_LPCAL, LowPowerCalib);
01213 
01214   /* Enable the write protection for RTC registers */
01215   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01216 
01217   /* Change RTC state */
01218   hrtc->State = HAL_RTC_STATE_READY;
01219 
01220   /* Process Unlocked */
01221   __HAL_UNLOCK(hrtc);
01222 
01223   return HAL_OK;
01224 }
01225 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
01226 
01227 /**
01228   * @brief  Configure the Synchronization Shift Control Settings.
01229   * @note   When REFCKON is set, firmware must not write to Shift control register.
01230   * @param  hrtc RTC handle
01231   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
01232   *          This parameter can be one of the following values:
01233   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
01234   *             @arg RTC_SHIFTADD1S_RESET: No effect.
01235   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
01236   *          This parameter can be one any value from 0 to 0x7FFF.
01237   * @retval HAL status
01238   */
01239 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
01240 {
01241   uint32_t tickstart;
01242 
01243   /* Check the parameters */
01244   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
01245   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
01246 
01247   /* Process Locked */
01248   __HAL_LOCK(hrtc);
01249 
01250   hrtc->State = HAL_RTC_STATE_BUSY;
01251 
01252   /* Disable the write protection for RTC registers */
01253   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01254 
01255   tickstart = HAL_GetTick();
01256 
01257   /* Wait until the shift is completed*/
01258 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01259   while ((hrtc->Instance->ICSR & RTC_ICSR_SHPF) != 0U)
01260 #else
01261   while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
01262 #endif
01263   {
01264     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01265     {
01266       /* Enable the write protection for RTC registers */
01267       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01268 
01269       hrtc->State = HAL_RTC_STATE_TIMEOUT;
01270 
01271       /* Process Unlocked */
01272       __HAL_UNLOCK(hrtc);
01273 
01274       return HAL_TIMEOUT;
01275     }
01276   }
01277 
01278   /* Check if the reference clock detection is disabled */
01279   if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
01280   {
01281     /* Configure the Shift settings */
01282     hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
01283 
01284     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
01285     if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
01286     {
01287       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
01288       {
01289         /* Enable the write protection for RTC registers */
01290         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01291 
01292         hrtc->State = HAL_RTC_STATE_ERROR;
01293 
01294         /* Process Unlocked */
01295         __HAL_UNLOCK(hrtc);
01296 
01297         return HAL_ERROR;
01298       }
01299     }
01300   }
01301   else
01302   {
01303     /* Enable the write protection for RTC registers */
01304     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01305 
01306     /* Change RTC state */
01307     hrtc->State = HAL_RTC_STATE_ERROR;
01308 
01309     /* Process Unlocked */
01310     __HAL_UNLOCK(hrtc);
01311 
01312     return HAL_ERROR;
01313   }
01314 
01315   /* Enable the write protection for RTC registers */
01316   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01317 
01318   /* Change RTC state */
01319   hrtc->State = HAL_RTC_STATE_READY;
01320 
01321   /* Process Unlocked */
01322   __HAL_UNLOCK(hrtc);
01323 
01324   return HAL_OK;
01325 }
01326 
01327 /**
01328   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01329   * @param  hrtc RTC handle
01330   * @param  CalibOutput Select the Calibration output Selection .
01331   *          This parameter can be one of the following values:
01332   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
01333   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
01334   * @retval HAL status
01335   */
01336 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
01337 {
01338   /* Check the parameters */
01339   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
01340 
01341   /* Process Locked */
01342   __HAL_LOCK(hrtc);
01343 
01344   hrtc->State = HAL_RTC_STATE_BUSY;
01345 
01346   /* Disable the write protection for RTC registers */
01347   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01348 
01349   /* Clear flags before config */
01350   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
01351 
01352   /* Configure the RTC_CR register */
01353   hrtc->Instance->CR |= (uint32_t)CalibOutput;
01354 
01355   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
01356 
01357   /* Enable the write protection for RTC registers */
01358   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01359 
01360   /* Change RTC state */
01361   hrtc->State = HAL_RTC_STATE_READY;
01362 
01363   /* Process Unlocked */
01364   __HAL_UNLOCK(hrtc);
01365 
01366   return HAL_OK;
01367 }
01368 
01369 /**
01370   * @brief  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01371   * @param  hrtc RTC handle
01372   * @retval HAL status
01373   */
01374 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
01375 {
01376   /* Process Locked */
01377   __HAL_LOCK(hrtc);
01378 
01379   hrtc->State = HAL_RTC_STATE_BUSY;
01380 
01381   /* Disable the write protection for RTC registers */
01382   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01383 
01384   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
01385 
01386   /* Enable the write protection for RTC registers */
01387   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01388 
01389   /* Change RTC state */
01390   hrtc->State = HAL_RTC_STATE_READY;
01391 
01392   /* Process Unlocked */
01393   __HAL_UNLOCK(hrtc);
01394 
01395   return HAL_OK;
01396 }
01397 
01398 /**
01399   * @brief  Enable the RTC reference clock detection.
01400   * @param  hrtc RTC handle
01401   * @retval HAL status
01402   */
01403 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
01404 {
01405   HAL_StatusTypeDef status;
01406 
01407   /* Process Locked */
01408   __HAL_LOCK(hrtc);
01409 
01410   hrtc->State = HAL_RTC_STATE_BUSY;
01411 
01412   /* Disable the write protection for RTC registers */
01413   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01414 
01415   /* Enter Initialization mode */
01416   status = RTC_EnterInitMode(hrtc);
01417   if (status == HAL_OK)
01418   {
01419     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
01420 
01421     /* Exit Initialization mode */
01422     status = RTC_ExitInitMode(hrtc);
01423   }
01424 
01425   /* Enable the write protection for RTC registers */
01426   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01427 
01428   if (status == HAL_OK)
01429   {
01430     hrtc->State = HAL_RTC_STATE_READY;
01431   }
01432 
01433   /* Process Unlocked */
01434   __HAL_UNLOCK(hrtc);
01435 
01436   return status;
01437 }
01438 
01439 /**
01440   * @brief  Disable the RTC reference clock detection.
01441   * @param  hrtc RTC handle
01442   * @retval HAL status
01443   */
01444 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
01445 {
01446   HAL_StatusTypeDef status;
01447 
01448   /* Process Locked */
01449   __HAL_LOCK(hrtc);
01450 
01451   hrtc->State = HAL_RTC_STATE_BUSY;
01452 
01453   /* Disable the write protection for RTC registers */
01454   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01455 
01456   /* Enter Initialization mode */
01457   status = RTC_EnterInitMode(hrtc);
01458   if (status == HAL_OK)
01459   {
01460     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
01461 
01462     /* Exit Initialization mode */
01463     status = RTC_ExitInitMode(hrtc);
01464   }
01465 
01466   /* Enable the write protection for RTC registers */
01467   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01468 
01469   if (status == HAL_OK)
01470   {
01471     hrtc->State = HAL_RTC_STATE_READY;
01472   }
01473 
01474   /* Process Unlocked */
01475   __HAL_UNLOCK(hrtc);
01476 
01477   return status;
01478 }
01479 
01480 /**
01481   * @brief  Enable the Bypass Shadow feature.
01482   * @note   When the Bypass Shadow is enabled the calendar value are taken
01483   *         directly from the Calendar counter.
01484   * @param  hrtc RTC handle
01485   * @retval HAL status
01486   */
01487 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
01488 {
01489   /* Process Locked */
01490   __HAL_LOCK(hrtc);
01491 
01492   hrtc->State = HAL_RTC_STATE_BUSY;
01493 
01494   /* Disable the write protection for RTC registers */
01495   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01496 
01497   /* Set the BYPSHAD bit */
01498   SET_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD);
01499 
01500   /* Enable the write protection for RTC registers */
01501   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01502 
01503   /* Change RTC state */
01504   hrtc->State = HAL_RTC_STATE_READY;
01505 
01506   /* Process Unlocked */
01507   __HAL_UNLOCK(hrtc);
01508 
01509   return HAL_OK;
01510 }
01511 
01512 /**
01513   * @brief  Disable the Bypass Shadow feature.
01514   * @note   When the Bypass Shadow is enabled the calendar value are taken
01515   *         directly from the Calendar counter.
01516   * @param  hrtc RTC handle
01517   * @retval HAL status
01518   */
01519 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
01520 {
01521   /* Process Locked */
01522   __HAL_LOCK(hrtc);
01523 
01524   hrtc->State = HAL_RTC_STATE_BUSY;
01525 
01526   /* Disable the write protection for RTC registers */
01527   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01528 
01529   /* Clear the BYPSHAD bit */
01530   CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
01531 
01532   /* Enable the write protection for RTC registers */
01533   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01534 
01535   /* Change RTC state */
01536   hrtc->State = HAL_RTC_STATE_READY;
01537 
01538   /* Process Unlocked */
01539   __HAL_UNLOCK(hrtc);
01540 
01541   return HAL_OK;
01542 }
01543 
01544 #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01545 /**
01546   * @brief  Set SSR Underflow detection with Interrupt.
01547   * @param  hrtc RTC handle
01548   * @retval HAL status
01549   */
01550 HAL_StatusTypeDef HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef *hrtc)
01551 {
01552   /* Process Locked */
01553   __HAL_LOCK(hrtc);
01554 
01555   hrtc->State = HAL_RTC_STATE_BUSY;
01556 
01557   /* Disable the write protection for RTC registers */
01558   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01559 
01560   /* Enable IT SSRU */
01561   __HAL_RTC_SSRU_ENABLE_IT(hrtc, RTC_IT_SSRU);
01562 
01563   /* RTC SSRU Interrupt Configuration: EXTI configuration */
01564   __HAL_RTC_SSRU_EXTI_ENABLE_IT();
01565   __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
01566 
01567   /* Enable the write protection for RTC registers */
01568   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01569 
01570   hrtc->State = HAL_RTC_STATE_READY;
01571 
01572   /* Process Unlocked */
01573   __HAL_UNLOCK(hrtc);
01574 
01575   return HAL_OK;
01576 }
01577 
01578 /**
01579   * @brief  Deactivate SSR Underflow.
01580   * @param  hrtc RTC handle
01581   * @retval HAL status
01582   */
01583 HAL_StatusTypeDef HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef *hrtc)
01584 {
01585   /* Process Locked */
01586   __HAL_LOCK(hrtc);
01587 
01588   hrtc->State = HAL_RTC_STATE_BUSY;
01589 
01590   /* Disable the write protection for RTC registers */
01591   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01592 
01593   /* In case of interrupt mode is used, the interrupt source must disabled */
01594   __HAL_RTC_SSRU_DISABLE_IT(hrtc, RTC_IT_TS);
01595 
01596   /* Enable the write protection for RTC registers */
01597   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01598 
01599   hrtc->State = HAL_RTC_STATE_READY;
01600 
01601   /* Process Unlocked */
01602   __HAL_UNLOCK(hrtc);
01603 
01604   return HAL_OK;
01605 }
01606 
01607 /**
01608   * @brief  Handle SSR underflow interrupt request.
01609   * @param  hrtc RTC handle
01610   * @retval None
01611   */
01612 void HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef *hrtc)
01613 {
01614   if ((RTC->MISR & RTC_MISR_SSRUMF) != 0u)
01615   {
01616     /* Immediatly clear flags */
01617     RTC->SCR = RTC_SCR_CSSRUF;
01618 
01619     /* SSRU callback */
01620 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
01621     /* Call SSRUEvent registered Callback */
01622     hrtc->SSRUEventCallback(hrtc);
01623 #else
01624     HAL_RTCEx_SSRUEventCallback(hrtc);
01625 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
01626   }
01627 
01628   /* Change RTC state */
01629   hrtc->State = HAL_RTC_STATE_READY;
01630 }
01631 
01632 /**
01633   * @brief  SSR underflow callback.
01634   * @param  hrtc RTC handle
01635   * @retval None
01636   */
01637 __weak void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc)
01638 {
01639   /* Prevent unused argument(s) compilation warning */
01640   UNUSED(hrtc);
01641 
01642   /* NOTE : This function should not be modified, when the callback is needed,
01643             the HAL_RTCEx_SSRUEventCallback could be implemented in the user file
01644    */
01645 }
01646 #endif /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
01647 
01648 /**
01649   * @}
01650   */
01651 
01652 /** @addtogroup RTCEx_Exported_Functions_Group4
01653   * @brief    Extended features functions
01654   *
01655 @verbatim
01656  ===============================================================================
01657                  ##### Extended features functions #####
01658  ===============================================================================
01659     [..]  This section provides functions allowing to:
01660       (+) RTC Alarm B callback
01661       (+) RTC Poll for Alarm B request
01662 
01663 @endverbatim
01664   * @{
01665   */
01666 
01667 /**
01668   * @brief  Alarm B callback.
01669   * @param  hrtc RTC handle
01670   * @retval None
01671   */
01672 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
01673 {
01674   /* Prevent unused argument(s) compilation warning */
01675   UNUSED(hrtc);
01676 
01677   /* NOTE : This function should not be modified, when the callback is needed,
01678             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
01679    */
01680 }
01681 
01682 /**
01683   * @brief  Handle Alarm B Polling request.
01684   * @param  hrtc RTC handle
01685   * @param  Timeout Timeout duration
01686   * @retval HAL status
01687   */
01688 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
01689 {
01690   uint32_t tickstart = HAL_GetTick();
01691 
01692   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
01693   {
01694     if (Timeout != HAL_MAX_DELAY)
01695     {
01696       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
01697       {
01698         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01699         return HAL_TIMEOUT;
01700       }
01701     }
01702   }
01703 
01704   /* Clear the Alarm Flag */
01705   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
01706 
01707   /* Change RTC state */
01708   hrtc->State = HAL_RTC_STATE_READY;
01709 
01710   return HAL_OK;
01711 }
01712 
01713 /**
01714   * @}
01715   */
01716 
01717 /** @addtogroup RTCEx_Exported_Functions_Group5
01718   * @brief      Extended RTC Tamper functions
01719   *
01720 @verbatim
01721   ==============================================================================
01722                          ##### Tamper functions #####
01723   ==============================================================================
01724   [..]
01725    (+) Before calling any tamper or internal tamper function, you have to call first
01726        HAL_RTC_Init() function.
01727    (+) In that ine you can select to output tamper event on RTC pin.
01728   [..]
01729    (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
01730        or Level according to the Tamper filter (if equal to 0 Edge else Level)
01731        value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
01732        Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
01733        You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
01734    (+) The default configuration of the Tamper erases the backup registers. To avoid
01735        erase, enable the NoErase field on the TAMP_TAMPCR register.
01736   [..]
01737    (+) Enable Internal Tamper and configure it with interrupt, timestamp using
01738        the HAL_RTCEx_SetInternalTamper() function.
01739 
01740 @endverbatim
01741   * @{
01742   */
01743 
01744 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01745 /**
01746   * @brief  Set Tamper
01747   * @param  hrtc RTC handle
01748   * @param  sTamper Pointer to Tamper Structure.
01749   * @retval HAL status
01750   */
01751 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
01752 {
01753   uint32_t tmpreg;
01754   /* Process TAMP instance pointer */
01755   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
01756 
01757   /* Check the parameters */
01758   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
01759   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
01760   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
01761   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
01762   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
01763   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
01764   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
01765   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
01766   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
01767   /* Trigger and Filter have exclusive configurations */
01768   assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL)))
01769                || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
01770 
01771   /* Configuration register 2 */
01772   tmpreg = tamp->CR2;
01773   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
01774 
01775   /* Configure the tamper trigger bit */
01776   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
01777   {
01778     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
01779   }
01780 
01781   /* Configure the tamper flags masking bit */
01782   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
01783   {
01784     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
01785   }
01786 
01787   /* Configure the tamper backup registers erasure bit */
01788   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
01789   {
01790     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
01791   }
01792   tamp->CR2 = tmpreg;
01793 
01794   /* Configure filtering parameters */
01795   tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
01796                 (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
01797 
01798   /* Configure Timestamp saving on tamper detection */
01799   if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
01800   {
01801     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01802     tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
01803     hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
01804     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01805   }
01806 
01807   /* Enable selected tamper */
01808   tamp->CR1 |= (sTamper->Tamper);
01809 
01810   return HAL_OK;
01811 }
01812 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
01813 /**
01814   * @brief  Set Tamper.
01815   * @note   By calling this API we disable the tamper interrupt for all tampers.
01816   * @param  hrtc RTC handle
01817   * @param  sTamper Pointer to Tamper Structure.
01818   * @retval HAL status
01819   */
01820 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
01821 {
01822   uint32_t tmpreg;
01823 
01824   /* Check the parameters */
01825   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
01826   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
01827   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
01828   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
01829   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
01830   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
01831   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
01832   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
01833   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
01834 
01835   /* Process Locked */
01836   __HAL_LOCK(hrtc);
01837 
01838   hrtc->State = HAL_RTC_STATE_BUSY;
01839 
01840   /* Read register */
01841   tmpreg = hrtc->Instance->TAMPCR;
01842 
01843 #if defined(RTC_TAMPER1_SUPPORT)
01844   if ((sTamper->Tamper & RTC_TAMPER_1) != 0)
01845   {
01846     MODIFY_REG(tmpreg,
01847                (RTC_TAMPCR_TAMP1E | RTC_TAMPCR_TAMP1TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE | RTC_TAMPCR_TAMP1NOERASE | RTC_TAMPCR_TAMP1MF), \
01848                sTamper->Tamper | \
01849                (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ?  0U : RTC_TAMPCR_TAMP1TRG) | \
01850                (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP1NOERASE) | \
01851                (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP1MF : 0U) \
01852               );
01853   }
01854 #endif /* RTC_TAMPER1_SUPPORT */
01855 
01856 #if defined(RTC_TAMPER2_SUPPORT)
01857   if ((sTamper->Tamper & RTC_TAMPER_2) != 0)
01858   {
01859     MODIFY_REG(tmpreg,
01860                (RTC_TAMPCR_TAMP2E | RTC_TAMPCR_TAMP2TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP2NOERASE | RTC_TAMPCR_TAMP2MF), \
01861                sTamper->Tamper | \
01862                (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ?  0U : RTC_TAMPCR_TAMP2TRG) | \
01863                (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP2NOERASE) | \
01864                (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP2MF : 0U) \
01865               );
01866   }
01867 #endif /* RTC_TAMPER2_SUPPORT */
01868 
01869 #if defined(RTC_TAMPER3_SUPPORT)
01870   if ((sTamper->Tamper & RTC_TAMPER_3) != 0)
01871   {
01872     MODIFY_REG(tmpreg,
01873                (RTC_TAMPCR_TAMP3E | RTC_TAMPCR_TAMP3TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP3IE | RTC_TAMPCR_TAMP3NOERASE | RTC_TAMPCR_TAMP3MF), \
01874                sTamper->Tamper | \
01875                (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ?  0U : RTC_TAMPCR_TAMP3TRG) | \
01876                (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP3NOERASE) | \
01877                (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP3MF : 0U) \
01878               );
01879   }
01880 #endif /* RTC_TAMPER3_SUPPORT */
01881 
01882   /* Update common parameters */
01883   MODIFY_REG(tmpreg,
01884              (RTC_TAMPCR_TAMPTS | RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH | RTC_TAMPCR_TAMPPUDIS), \
01885              sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp | \
01886              sTamper->TimeStampOnTamperDetection \
01887             );
01888 
01889   /* Set register */
01890   WRITE_REG(hrtc->Instance->TAMPCR, tmpreg);
01891 
01892   hrtc->State = HAL_RTC_STATE_READY;
01893 
01894   /* Process Unlocked */
01895   __HAL_UNLOCK(hrtc);
01896 
01897   return HAL_OK;
01898 }
01899 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
01900 
01901 
01902 
01903 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
01904 /**
01905   * @brief  Set Tamper with interrupt.
01906   * @param  hrtc RTC handle
01907   * @param  sTamper Pointer to Tamper Structure.
01908   * @retval HAL status
01909   */
01910 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
01911 {
01912   uint32_t tmpreg;
01913   /* Process TAMP instance pointer */
01914   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
01915 
01916   /* Check the parameters */
01917   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
01918   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
01919   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
01920   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
01921   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
01922   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
01923   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
01924   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
01925   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
01926 
01927   /* Copy configuration register into temporary variable */
01928   tmpreg = tamp->CR2;
01929 
01930   /* Clear the bits that are going to be configured and leave the others unchanged */
01931   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
01932 
01933   if (sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
01934   {
01935     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
01936   }
01937 
01938   /* Configure the tamper flags masking bit */
01939   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
01940   {
01941     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
01942   }
01943 
01944   /* Configure the tamper backup registers erasure bit */
01945   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
01946   {
01947     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
01948   }
01949   tamp->CR2 = tmpreg;
01950 
01951   /* Configure filtering parameters */
01952   tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
01953                 (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
01954 
01955   /* Configure Timestamp saving on tamper detection */
01956   if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
01957   {
01958     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01959     tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
01960     hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
01961     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01962   }
01963 
01964   /* Configure RTC Tamper Interrupt: EXTI configuration */
01965   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
01966   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
01967 
01968   /* Enable interrupt on selected tamper */
01969   tamp->IER |= sTamper->Tamper;
01970 
01971   /* Enable selected tamper */
01972   tamp->CR1 |= sTamper->Tamper;
01973 
01974   return HAL_OK;
01975 }
01976 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
01977 
01978 /**
01979   * @brief  Set Tamper with interrupt.
01980    * @param  hrtc RTC handle
01981   * @param  sTamper Pointer to Tamper Structure.
01982   * @retval HAL status
01983   */
01984 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
01985 {
01986   uint32_t tmpreg;
01987 
01988   /* Check the parameters */
01989   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
01990   assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
01991   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
01992   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
01993   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
01994   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
01995   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
01996   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
01997   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
01998   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
01999 
02000   /* Process Locked */
02001   __HAL_LOCK(hrtc);
02002 
02003   hrtc->State = HAL_RTC_STATE_BUSY;
02004 
02005   /* Read register */
02006   tmpreg = hrtc->Instance->TAMPCR;
02007 
02008 #if defined(RTC_TAMPER1_SUPPORT)
02009   if ((sTamper->Tamper & RTC_TAMPER_1) != 0)
02010   {
02011     MODIFY_REG(tmpreg,
02012                (RTC_TAMPCR_TAMP1E | RTC_TAMPCR_TAMP1TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE | RTC_TAMPCR_TAMP1NOERASE | RTC_TAMPCR_TAMP1MF), \
02013                sTamper->Tamper | sTamper->Interrupt | \
02014                (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ?  0U : RTC_TAMPCR_TAMP1TRG) | \
02015                (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP1NOERASE) | \
02016                (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP1MF : 0U) \
02017               );
02018   }
02019 #endif /* RTC_TAMPER1_SUPPORT */
02020 
02021 #if defined(RTC_TAMPER2_SUPPORT)
02022   if ((sTamper->Tamper & RTC_TAMPER_2) != 0)
02023   {
02024     MODIFY_REG(tmpreg,
02025                (RTC_TAMPCR_TAMP2E | RTC_TAMPCR_TAMP2TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP2NOERASE | RTC_TAMPCR_TAMP2MF), \
02026                sTamper->Tamper | sTamper->Interrupt | \
02027                (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ?  0U : RTC_TAMPCR_TAMP2TRG) | \
02028                (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP2NOERASE) | \
02029                (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP2MF : 0U) \
02030               );
02031   }
02032 #endif /* RTC_TAMPER2_SUPPORT */
02033 
02034 #if defined(RTC_TAMPER3_SUPPORT)
02035   if ((sTamper->Tamper & RTC_TAMPER_3) != 0)
02036   {
02037     MODIFY_REG(tmpreg,
02038                (RTC_TAMPCR_TAMP3E | RTC_TAMPCR_TAMP3TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP3IE | RTC_TAMPCR_TAMP3NOERASE | RTC_TAMPCR_TAMP3MF), \
02039                sTamper->Tamper | sTamper->Interrupt | \
02040                (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ?  0U : RTC_TAMPCR_TAMP3TRG) | \
02041                (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP3NOERASE) | \
02042                (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP3MF : 0U) \
02043               );
02044   }
02045 #endif /* RTC_TAMPER3_SUPPORT */
02046 
02047   /* Update common parameters */
02048   MODIFY_REG(tmpreg,
02049              (RTC_TAMPCR_TAMPTS | RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH | RTC_TAMPCR_TAMPPUDIS), \
02050              sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp | \
02051              sTamper->TimeStampOnTamperDetection \
02052             );
02053 
02054   /* Set register */
02055   WRITE_REG(hrtc->Instance->TAMPCR, tmpreg);
02056 
02057   /* RTC Tamper Interrupt Configuration: EXTI configuration */
02058   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
02059   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
02060 
02061   hrtc->State = HAL_RTC_STATE_READY;
02062 
02063   /* Process Unlocked */
02064   __HAL_UNLOCK(hrtc);
02065 
02066   return HAL_OK;
02067 }
02068 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
02069 
02070 
02071 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
02072 /**
02073   * @brief  Deactivate Tamper.
02074   * @param  hrtc RTC handle
02075   * @param  Tamper Selected tamper pin.
02076   *         This parameter can be a combination of the following values:
02077   *         @arg RTC_TAMPER_1
02078   *         @arg RTC_TAMPER_2
02079   * @retval HAL status
02080   */
02081 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
02082 {
02083   /* Process TAMP instance pointer */
02084   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
02085 
02086   assert_param(IS_RTC_TAMPER(Tamper));
02087 
02088   /* Disable the selected Tamper pin */
02089   tamp->CR1 &= ~Tamper;
02090 
02091   /* Clear tamper mask/noerase/trigger configuration */
02092   tamp->CR2 &= ~((Tamper << 24) | (Tamper << 16) | Tamper);
02093 
02094   /* Clear tamper interrupt mode configuration */
02095   tamp->IER &= ~Tamper;
02096 
02097   /* Clear tamper interrupt and event flags (WO register) */
02098   tamp->SCR = Tamper;
02099 
02100   return HAL_OK;
02101 }
02102 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
02103 /**
02104   * @brief  Deactivate Tamper.
02105   * @param  hrtc RTC handle
02106   * @param  Tamper Selected tamper pin.
02107   *         This parameter can be any combination of the following values:
02108   *         @arg RTC_TAMPER_1
02109   *         @arg RTC_TAMPER_2
02110   *         @arg RTC_TAMPER_3
02111   * @retval HAL status
02112   */
02113 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
02114 {
02115   assert_param(IS_RTC_TAMPER(Tamper));
02116 
02117   /* Process Locked */
02118   __HAL_LOCK(hrtc);
02119 
02120   hrtc->State = HAL_RTC_STATE_BUSY;
02121 
02122   /* Disable the selected Tamper pin */
02123   hrtc->Instance->TAMPCR &= ~Tamper;
02124 
02125 #if defined(RTC_TAMPER1_SUPPORT)
02126   if ((Tamper & RTC_TAMPER_1) != 0U)
02127   {
02128     /* Disable the Tamper1 interrupt */
02129     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1));
02130   }
02131 #endif /* RTC_TAMPER1_SUPPORT */
02132   if ((Tamper & RTC_TAMPER_2) != 0U)
02133   {
02134     /* Disable the Tamper2 interrupt */
02135     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2));
02136   }
02137 #if defined(RTC_TAMPER3_SUPPORT)
02138   if ((Tamper & RTC_TAMPER_3) != 0U)
02139   {
02140     /* Disable the Tamper3 interrupt */
02141     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3));
02142   }
02143 #endif /* RTC_TAMPER3_SUPPORT */
02144 
02145   hrtc->State = HAL_RTC_STATE_READY;
02146 
02147   /* Process Unlocked */
02148   __HAL_UNLOCK(hrtc);
02149 
02150   return HAL_OK;
02151 }
02152 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
02153 
02154 
02155 #if defined(RTC_TAMPER1_SUPPORT)
02156 /**
02157   * @brief  Handle Tamper 1 Polling.
02158   * @param  hrtc RTC handle
02159   * @param  Timeout Timeout duration
02160   * @retval HAL status
02161   */
02162 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
02163 {
02164   uint32_t tickstart = HAL_GetTick();
02165 
02166   /* Get the status of the Interrupt */
02167   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
02168   {
02169     if (Timeout != HAL_MAX_DELAY)
02170     {
02171       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
02172       {
02173         hrtc->State = HAL_RTC_STATE_TIMEOUT;
02174         return HAL_TIMEOUT;
02175       }
02176     }
02177   }
02178 
02179   /* Clear the Tamper Flag */
02180   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
02181 
02182   /* Change RTC state */
02183   hrtc->State = HAL_RTC_STATE_READY;
02184 
02185   return HAL_OK;
02186 }
02187 #endif /* RTC_TAMPER1_SUPPORT */
02188 
02189 /**
02190   * @brief  Handle Tamper 2 Polling.
02191   * @param  hrtc RTC handle
02192   * @param  Timeout Timeout duration
02193   * @retval HAL status
02194   */
02195 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
02196 {
02197   uint32_t tickstart = HAL_GetTick();
02198 
02199   /* Get the status of the Interrupt */
02200   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
02201   {
02202     if (Timeout != HAL_MAX_DELAY)
02203     {
02204       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
02205       {
02206         hrtc->State = HAL_RTC_STATE_TIMEOUT;
02207         return HAL_TIMEOUT;
02208       }
02209     }
02210   }
02211 
02212   /* Clear the Tamper Flag */
02213   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
02214 
02215   /* Change RTC state */
02216   hrtc->State = HAL_RTC_STATE_READY;
02217 
02218   return HAL_OK;
02219 }
02220 
02221 #if defined(RTC_TAMPER3_SUPPORT)
02222 /**
02223   * @brief  Handle Tamper 3 Polling.
02224   * @param  hrtc  RTC handle
02225   * @param  Timeout  Timeout duration
02226   * @retval HAL status
02227   */
02228 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
02229 {
02230   uint32_t tickstart = HAL_GetTick();
02231 
02232   /* Get the status of the Interrupt */
02233   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
02234   {
02235     if (Timeout != HAL_MAX_DELAY)
02236     {
02237       if ((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
02238       {
02239         hrtc->State = HAL_RTC_STATE_TIMEOUT;
02240         return HAL_TIMEOUT;
02241       }
02242     }
02243   }
02244 
02245   /* Clear the Tamper Flag */
02246   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
02247 
02248   /* Change RTC state */
02249   hrtc->State = HAL_RTC_STATE_READY;
02250 
02251   return HAL_OK;
02252 }
02253 #endif /* RTC_TAMPER3_SUPPORT */
02254 
02255 
02256 
02257 #if defined(RTC_TAMPER1_SUPPORT)
02258 /**
02259   * @brief  Tamper 1 callback.
02260   * @param  hrtc RTC handle
02261   * @retval None
02262   */
02263 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
02264 {
02265   /* Prevent unused argument(s) compilation warning */
02266   UNUSED(hrtc);
02267 
02268   /* NOTE : This function should not be modified, when the callback is needed,
02269             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
02270    */
02271 }
02272 #endif /* RTC_TAMPER1_SUPPORT */
02273 
02274 /**
02275   * @brief  Tamper 2 callback.
02276   * @param  hrtc RTC handle
02277   * @retval None
02278   */
02279 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
02280 {
02281   /* Prevent unused argument(s) compilation warning */
02282   UNUSED(hrtc);
02283 
02284   /* NOTE : This function should not be modified, when the callback is needed,
02285             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
02286    */
02287 }
02288 
02289 #if defined(RTC_TAMPER3_SUPPORT)
02290 /**
02291   * @brief  Tamper 3 callback.
02292   * @param  hrtc RTC handle
02293   * @retval None
02294   */
02295 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
02296 {
02297   /* Prevent unused argument(s) compilation warning */
02298   UNUSED(hrtc);
02299 
02300   /* NOTE : This function should not be modified, when the callback is needed,
02301             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
02302    */
02303 }
02304 #endif /* RTC_TAMPER3_SUPPORT */
02305 
02306 /**
02307   * @}
02308   */
02309 
02310 
02311 /** @addtogroup RTCEx_Exported_Functions_Group6
02312   * @brief      Extended RTC Backup register functions
02313   *
02314 @verbatim
02315   ===============================================================================
02316              ##### Extended RTC Backup register functions #####
02317   ===============================================================================
02318   [..]
02319    (+) Before calling any tamper or internal tamper function, you have to call first
02320        HAL_RTC_Init() function.
02321    (+) In that ine you can select to output tamper event on RTC pin.
02322   [..]
02323    This subsection provides functions allowing to
02324    (+) Write a data in a specified RTC Backup data register
02325    (+) Read a data in a specified RTC Backup data register
02326 @endverbatim
02327   * @{
02328   */
02329 
02330 
02331 /**
02332   * @brief  Write a data in a specified RTC Backup data register.
02333   * @param  hrtc RTC handle
02334   * @param  BackupRegister RTC Backup data Register number.
02335   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
02336   *          specify the register.
02337   * @param  Data Data to be written in the specified Backup data register.
02338   * @retval None
02339   */
02340 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
02341 {
02342   uint32_t __IO tmp;
02343 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
02344   /* Process TAMP instance pointer */
02345   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
02346 
02347   /* Check the parameters */
02348   assert_param(IS_RTC_BKP(BackupRegister));
02349 
02350   tmp = (uint32_t) & (tamp->BKP0R);
02351 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
02352   /* Check the parameters */
02353   assert_param(IS_RTC_BKP(BackupRegister));
02354 
02355   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
02356 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
02357 
02358   tmp += (BackupRegister * 4U);
02359 
02360   /* Write the specified register */
02361   *(__IO uint32_t *)tmp = (uint32_t)Data;
02362 }
02363 
02364 
02365 /**
02366   * @brief  Read data from the specified RTC Backup data Register.
02367   * @param  hrtc RTC handle
02368   * @param  BackupRegister  RTC Backup data Register number.
02369   *         This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
02370   *         specify the register.
02371   * @retval Read value
02372   */
02373 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
02374 {
02375   uint32_t tmp;
02376 #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
02377   /* Process TAMP instance pointer */
02378   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
02379 
02380   /* Check the parameters */
02381   assert_param(IS_RTC_BKP(BackupRegister));
02382 
02383   tmp = (uint32_t) & (tamp->BKP0R);
02384 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
02385   /* Check the parameters */
02386   assert_param(IS_RTC_BKP(BackupRegister));
02387 
02388   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
02389 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
02390 
02391   tmp += (BackupRegister * 4U);
02392 
02393   /* Read the specified register */
02394   return (*(__IO uint32_t *)tmp);
02395 }
02396 
02397 
02398 /**
02399   * @}
02400   */
02401 
02402 /**
02403   * @}
02404   */
02405 
02406 #endif /* HAL_RTC_MODULE_ENABLED */
02407 
02408 /**
02409   * @}
02410   */
02411 
02412 /**
02413   * @}
02414   */