Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:08

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_rtc_ex.c
0004   * @author  MCD Application Team
0005   * @brief   Extended RTC HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Real Time Clock (RTC) Extended peripheral:
0008   *           + RTC Time Stamp functions
0009   *           + RTC Tamper functions
0010   *           + RTC Wake-up functions
0011   *           + Extended Control functions
0012   *           + Extended RTC features functions
0013   *
0014   ******************************************************************************
0015   * @attention
0016   *
0017   * Copyright (c) 2017 STMicroelectronics.
0018   * All rights reserved.
0019   *
0020   * This software is licensed under terms that can be found in the LICENSE file
0021   * in the root directory of this software component.
0022   * If no LICENSE file comes with this software, it is provided AS-IS.
0023   *
0024   ******************************************************************************
0025   @verbatim
0026   ==============================================================================
0027                   ##### How to use this driver #####
0028   ==============================================================================
0029   [..]
0030     (+) Enable the RTC domain access.
0031     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
0032         format using the HAL_RTC_Init() function.
0033 
0034   *** RTC Wakeup configuration ***
0035   ================================
0036   [..]
0037     (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
0038         function. You can also configure the RTC Wakeup timer with interrupt mode
0039         using the HAL_RTCEx_SetWakeUpTimer_IT() function.
0040     (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
0041         function.
0042 
0043   *** Outputs configuration ***
0044   =============================
0045   [..]  The RTC has 2 different outputs:
0046     (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
0047         and WaKeUp signals.
0048         To output the selected RTC signal, use the HAL_RTC_Init() function.
0049     (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
0050         To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
0051     (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB2) managed on
0052         the RTC_OR register.
0053     (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
0054         automatically configured in output alternate function.
0055 
0056   *** Smooth digital Calibration configuration ***
0057   ================================================
0058   [..]
0059     (+) Configure the RTC Original Digital Calibration Value and the corresponding
0060         calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib()
0061         function.
0062 
0063   *** TimeStamp configuration ***
0064   ===============================
0065   [..]
0066     (+) Enable the RTC TimeStamp using the HAL_RTCEx_SetTimeStamp() function.
0067         You can also configure the RTC TimeStamp with interrupt mode using the
0068         HAL_RTCEx_SetTimeStamp_IT() function.
0069     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
0070         function.
0071 
0072   *** Internal TimeStamp configuration ***
0073   ===============================
0074   [..]
0075     (+) Enable the RTC internal TimeStamp using the HAL_RTCEx_SetInternalTimeStamp() function.
0076         User has to check internal timestamp occurrence using __HAL_RTC_INTERNAL_TIMESTAMP_GET_FLAG.
0077     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
0078         function.
0079 
0080   *** Tamper configuration ***
0081   ============================
0082   [..]
0083     (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
0084         or Level according to the Tamper filter (if equal to 0 Edge else Level)
0085         value, sampling frequency, NoErase, MaskFlag,  precharge or discharge and
0086         Pull-UP using the HAL_RTCEx_SetTamper() function. You can configure RTC Tamper
0087         with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
0088     (+) The default configuration of the Tamper erases the backup registers. To avoid
0089         erase, enable the NoErase field on the RTC_TAMPCR register.
0090 
0091   *** Backup Data Registers configuration ***
0092   ===========================================
0093   [..]
0094     (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
0095         function.
0096     (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
0097         function.
0098 
0099    @endverbatim
0100   */
0101 
0102 /* Includes ------------------------------------------------------------------*/
0103 #include "stm32h7xx_hal.h"
0104 
0105 /** @addtogroup STM32H7xx_HAL_Driver
0106   * @{
0107   */
0108 
0109 /** @addtogroup RTCEx
0110   * @brief RTC Extended HAL module driver
0111   * @{
0112   */
0113 
0114 #ifdef HAL_RTC_MODULE_ENABLED
0115 
0116 /* Private typedef -----------------------------------------------------------*/
0117 /* Private define ------------------------------------------------------------*/
0118 #define TAMP_ALL (TAMP_CR1_TAMP1E | TAMP_CR1_TAMP2E | TAMP_CR1_TAMP3E)
0119 
0120 /* Private macro -------------------------------------------------------------*/
0121 /* Private variables ---------------------------------------------------------*/
0122 /* Private function prototypes -----------------------------------------------*/
0123 /* Exported functions --------------------------------------------------------*/
0124 
0125 /** @addtogroup RTCEx_Exported_Functions
0126   * @{
0127   */
0128 
0129 
0130 /** @addtogroup RTCEx_Exported_Functions_Group1
0131  *  @brief   RTC TimeStamp and Tamper functions
0132   *
0133 @verbatim
0134  ===============================================================================
0135                  ##### RTC TimeStamp and Tamper functions #####
0136  ===============================================================================
0137 
0138  [..] This section provides functions allowing to configure TimeStamp feature
0139 
0140 @endverbatim
0141   * @{
0142   */
0143 
0144 /**
0145   * @brief  Set TimeStamp.
0146   * @note   This API must be called before enabling the TimeStamp feature.
0147   * @param  hrtc RTC handle
0148   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
0149   *         activated.
0150   *          This parameter can be one of the following values:
0151   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
0152   *                                        rising edge of the related pin.
0153   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
0154   *                                         falling edge of the related pin.
0155   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
0156   *          This parameter can be one of the following values:
0157   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
0158   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
0159   *               compatibility, this parameter is required.
0160   * @retval HAL status
0161   */
0162 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
0163 {
0164   uint32_t tmpreg;
0165 
0166   /* Check the parameters */
0167   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
0168   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
0169 
0170   /* Prevent unused argument(s) compilation warning if no assert_param check */
0171   UNUSED(RTC_TimeStampPin);
0172 
0173   /* Process Locked */
0174   __HAL_LOCK(hrtc);
0175 
0176   hrtc->State = HAL_RTC_STATE_BUSY;
0177 
0178   /* Get the RTC_CR register and clear the bits to be configured */
0179   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
0180 
0181   tmpreg |= TimeStampEdge;
0182 
0183   /* Disable the write protection for RTC registers */
0184   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0185 
0186   /* Configure the Time Stamp TSEDGE and Enable bits */
0187   hrtc->Instance->CR = (uint32_t)tmpreg;
0188 
0189   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
0190 
0191   /* Enable the write protection for RTC registers */
0192   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0193 
0194   /* Change RTC state */
0195   hrtc->State = HAL_RTC_STATE_READY;
0196 
0197   /* Process Unlocked */
0198   __HAL_UNLOCK(hrtc);
0199 
0200   return HAL_OK;
0201 }
0202 
0203 /**
0204   * @brief  Set TimeStamp with Interrupt.
0205   * @note   This API must be called before enabling the TimeStamp feature.
0206   * @param  hrtc RTC handle
0207   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
0208   *         activated.
0209   *          This parameter can be one of the following values:
0210   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
0211   *                                        rising edge of the related pin.
0212   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
0213   *                                         falling edge of the related pin.
0214   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
0215   *          This parameter can be one of the following values:
0216   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
0217   *               The RTC TimeStamp Pin is per default PC13, but for reasons of
0218   *               compatibility, this parameter is required.
0219   * @retval HAL status
0220   */
0221 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
0222 {
0223   uint32_t tmpreg;
0224 
0225   /* Check the parameters */
0226   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
0227   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
0228 
0229   /* Prevent unused argument(s) compilation warning if no assert_param check */
0230   UNUSED(RTC_TimeStampPin);
0231 
0232   /* Process Locked */
0233   __HAL_LOCK(hrtc);
0234 
0235   hrtc->State = HAL_RTC_STATE_BUSY;
0236 
0237   /* Get the RTC_CR register and clear the bits to be configured */
0238   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
0239 
0240   tmpreg |= TimeStampEdge;
0241 
0242   /* Disable the write protection for RTC registers */
0243   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0244 
0245   /* Configure the Time Stamp TSEDGE and Enable bits */
0246   hrtc->Instance->CR = (uint32_t)tmpreg;
0247 
0248   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
0249 
0250   /* Enable IT timestamp */
0251   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
0252 
0253   /* RTC timestamp Interrupt Configuration: EXTI configuration */
0254 #if defined(DUAL_CORE)
0255   if (HAL_GetCurrentCPUID() == CM7_CPUID)
0256   {
0257     __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
0258   }
0259   else
0260   {
0261     __HAL_RTC_TAMPER_TIMESTAMP_EXTID2_ENABLE_IT();
0262   }
0263 #else /* SINGLE_CORE */
0264   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
0265 #endif /* DUAL_CORE */
0266 
0267   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
0268 
0269   /* Enable the write protection for RTC registers */
0270   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0271 
0272   hrtc->State = HAL_RTC_STATE_READY;
0273 
0274   /* Process Unlocked */
0275   __HAL_UNLOCK(hrtc);
0276 
0277   return HAL_OK;
0278 }
0279 
0280 /**
0281   * @brief  Deactivate TimeStamp.
0282   * @param  hrtc RTC handle
0283   * @retval HAL status
0284   */
0285 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
0286 {
0287   uint32_t tmpreg;
0288 
0289   /* Process Locked */
0290   __HAL_LOCK(hrtc);
0291 
0292   hrtc->State = HAL_RTC_STATE_BUSY;
0293 
0294   /* Disable the write protection for RTC registers */
0295   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0296 
0297   /* In case of interrupt mode is used, the interrupt source must disabled */
0298   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
0299 
0300   /* Get the RTC_CR register and clear the bits to be configured */
0301   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
0302 
0303   /* Configure the Time Stamp TSEDGE and Enable bits */
0304   hrtc->Instance->CR = (uint32_t)tmpreg;
0305 
0306   /* Enable the write protection for RTC registers */
0307   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0308 
0309   hrtc->State = HAL_RTC_STATE_READY;
0310 
0311   /* Process Unlocked */
0312   __HAL_UNLOCK(hrtc);
0313 
0314   return HAL_OK;
0315 }
0316 
0317 /**
0318   * @brief  Set Internal TimeStamp.
0319   * @note   This API must be called before enabling the internal TimeStamp feature.
0320   * @param  hrtc RTC handle
0321   * @retval HAL status
0322   */
0323 HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
0324 {
0325   /* Process Locked */
0326   __HAL_LOCK(hrtc);
0327 
0328   hrtc->State = HAL_RTC_STATE_BUSY;
0329 
0330   /* Disable the write protection for RTC registers */
0331   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0332 
0333   /* Configure the internal Time Stamp Enable bits */
0334   __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);
0335 
0336   /* Enable the write protection for RTC registers */
0337   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0338 
0339   /* Change RTC state */
0340   hrtc->State = HAL_RTC_STATE_READY;
0341 
0342   /* Process Unlocked */
0343   __HAL_UNLOCK(hrtc);
0344 
0345   return HAL_OK;
0346 }
0347 
0348 /**
0349   * @brief  Deactivate Internal TimeStamp.
0350   * @param  hrtc RTC handle
0351   * @retval HAL status
0352   */
0353 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
0354 {
0355   /* Process Locked */
0356   __HAL_LOCK(hrtc);
0357 
0358   hrtc->State = HAL_RTC_STATE_BUSY;
0359 
0360   /* Disable the write protection for RTC registers */
0361   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0362 
0363   /* Configure the internal Time Stamp Enable bits */
0364   __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
0365 
0366   /* Enable the write protection for RTC registers */
0367   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0368 
0369   hrtc->State = HAL_RTC_STATE_READY;
0370 
0371   /* Process Unlocked */
0372   __HAL_UNLOCK(hrtc);
0373 
0374   return HAL_OK;
0375 }
0376 
0377 /**
0378   * @brief  Get the RTC TimeStamp value.
0379   * @param  hrtc RTC handle
0380   * @param  sTimeStamp Pointer to Time structure
0381   * @param  sTimeStampDate Pointer to Date structure
0382   * @param  Format specifies the format of the entered parameters.
0383   *          This parameter can be one of the following values:
0384   *             @arg RTC_FORMAT_BIN: Binary data format
0385   *             @arg RTC_FORMAT_BCD: BCD data format
0386   * @retval HAL status
0387   */
0388 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
0389 {
0390   uint32_t tmptime;
0391   uint32_t tmpdate;
0392 
0393   /* Check the parameters */
0394   assert_param(IS_RTC_FORMAT(Format));
0395 
0396   /* Get the TimeStamp time and date registers values */
0397   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
0398   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
0399 
0400   /* Fill the Time structure fields with the read parameters */
0401   sTimeStamp->Hours      = (uint8_t)((tmptime & (RTC_TSTR_HT  | RTC_TSTR_HU))  >> RTC_TSTR_HU_Pos);
0402   sTimeStamp->Minutes    = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
0403   sTimeStamp->Seconds    = (uint8_t)((tmptime & (RTC_TSTR_ST  | RTC_TSTR_SU))  >> RTC_TSTR_SU_Pos);
0404   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM))                 >> RTC_TSTR_PM_Pos);
0405   sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
0406 
0407   /* Fill the Date structure fields with the read parameters */
0408   sTimeStampDate->Year    = 0U;
0409   sTimeStampDate->Month   = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
0410   sTimeStampDate->Date    = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
0411   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU))              >> RTC_TSDR_WDU_Pos);
0412 
0413   /* Check the input parameters format */
0414   if (Format == RTC_FORMAT_BIN)
0415   {
0416     /* Convert the TimeStamp structure parameters to Binary format */
0417     sTimeStamp->Hours   = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
0418     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
0419     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
0420 
0421     /* Convert the DateTimeStamp structure parameters to Binary format */
0422     sTimeStampDate->Month   = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
0423     sTimeStampDate->Date    = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
0424     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
0425   }
0426 
0427   /* Clear the TIMESTAMP Flags */
0428   __HAL_RTC_INTERNAL_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_ITSF);
0429   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
0430 
0431   return HAL_OK;
0432 }
0433 
0434 /**
0435   * @}
0436   */
0437 
0438 /** @addtogroup RTCEx_Exported_Functions_Group5
0439   * @brief      Extended RTC Tamper functions
0440   *
0441 @verbatim
0442  ==============================================================================
0443                         ##### Tamper functions #####
0444  ==============================================================================
0445  [..]
0446   (+) Before calling any tamper or internal tamper function, you have to call first
0447       HAL_RTC_Init() function.
0448   (+) In that ine you can select to output tamper event on RTC pin.
0449  [..]
0450   (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
0451       or Level according to the Tamper filter (if equal to 0 Edge else Level)
0452       value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
0453       Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
0454       You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
0455   (+) The default configuration of the Tamper erases the backup registers. To avoid
0456       erase, enable the NoErase field on the TAMP_TAMPCR register.
0457  [..]
0458   (+) Enable Internal Tamper and configure it with interrupt, timestamp using
0459       the HAL_RTCEx_SetInternalTamper() function.
0460 
0461 @endverbatim
0462 * @{
0463 */
0464 
0465 #if defined(TAMP)
0466 /**
0467   * @brief  Set Tamper
0468   * @param  hrtc RTC handle
0469   * @param  sTamper Pointer to Tamper Structure.
0470   * @retval HAL status
0471   */
0472 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
0473 {
0474   uint32_t tmpreg;
0475 
0476   /* Point on TAMPER registers base address */
0477   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
0478 
0479   /* Check the parameters */
0480   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
0481   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
0482   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
0483   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
0484   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
0485   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
0486   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
0487   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
0488   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
0489   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
0490 
0491   /* Configuration register 2 */
0492   tmpreg = tamp->CR2;
0493   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
0494 
0495   /* Configure the tamper trigger bit */
0496   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
0497   {
0498     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
0499   }
0500 
0501   /* Configure the tamper flags masking bit */
0502   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
0503   {
0504     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
0505   }
0506 
0507   /* Configure the tamper backup registers erasure bit */
0508   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
0509   {
0510     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
0511   }
0512   tamp->CR2 = tmpreg;
0513 
0514   /* Configure filtering parameters */
0515   tamp->FLTCR = (sTamper->Filter)            | (sTamper->SamplingFrequency) | \
0516                 (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
0517 
0518   /* Configure Timestamp saving on tamper detection */
0519   if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
0520   {
0521     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0522     tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
0523     hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
0524     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0525   }
0526 
0527   /* Enable selected tamper */
0528   tamp->CR1 |= (sTamper->Tamper);
0529 
0530   return HAL_OK;
0531 }
0532 #else
0533 /**
0534   * @brief  Set Tamper.
0535   * @note   By calling this API we disable the tamper interrupt for all tampers.
0536   * @param  hrtc RTC handle
0537   * @param  sTamper Pointer to Tamper Structure.
0538   * @retval HAL status
0539   */
0540 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
0541 {
0542   uint32_t tmpreg;
0543 
0544   /* Check the parameters */
0545   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
0546   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
0547   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
0548   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
0549   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
0550   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
0551   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
0552   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
0553   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
0554   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
0555 
0556   /* Process Locked */
0557   __HAL_LOCK(hrtc);
0558 
0559   hrtc->State = HAL_RTC_STATE_BUSY;
0560 
0561   /* Copy control register into temporary variable */
0562   tmpreg = hrtc->Instance->TAMPCR;
0563 
0564   /* Enable selected tamper */
0565   tmpreg |= (sTamper->Tamper);
0566 
0567   /* Configure the bit (located just next to the tamper enable bit) */
0568   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
0569   {
0570     /* Set the tamper trigger bit */
0571     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
0572   }
0573   else
0574   {
0575     /* Clear the tamper trigger bit */
0576     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
0577   }
0578 
0579   /* Configure the tamper backup registers erasure bit */
0580   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
0581   {
0582     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0583     {
0584       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
0585     }
0586     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0587     {
0588       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
0589     }
0590     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0591     {
0592       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
0593     }
0594   }
0595   else
0596   {
0597     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0598     {
0599       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
0600     }
0601     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0602     {
0603       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
0604     }
0605     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0606     {
0607       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
0608     }
0609   }
0610 
0611   /* Configure the tamper flags masking bit */
0612   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
0613   {
0614     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0615     {
0616       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
0617     }
0618     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0619     {
0620       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
0621     }
0622     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0623     {
0624       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
0625     }
0626   }
0627   else
0628   {
0629     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0630     {
0631       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
0632     }
0633     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0634     {
0635       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
0636     }
0637     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0638     {
0639       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
0640     }
0641   }
0642 
0643   /* Clearing remaining fields before setting them */
0644   tmpreg &= ~(RTC_TAMPERFILTER_MASK            | RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
0645               RTC_TAMPERPRECHARGEDURATION_MASK | RTC_TAMPER_PULLUP_MASK             | \
0646               RTC_TIMESTAMPONTAMPERDETECTION_MASK);
0647 
0648   /* Set remaining parameters of desired configuration into temporary variable */
0649   tmpreg |= ((uint32_t)sTamper->Filter            | \
0650              (uint32_t)sTamper->SamplingFrequency | \
0651              (uint32_t)sTamper->PrechargeDuration | \
0652              (uint32_t)sTamper->TamperPullUp      | \
0653              (uint32_t)sTamper->TimeStampOnTamperDetection);
0654 
0655   /* Copy desired configuration into configuration register */
0656   hrtc->Instance->TAMPCR = tmpreg;
0657 
0658   hrtc->State = HAL_RTC_STATE_READY;
0659 
0660   /* Process Unlocked */
0661   __HAL_UNLOCK(hrtc);
0662 
0663   return HAL_OK;
0664 }
0665 #endif /* TAMP */
0666 
0667 #if defined(TAMP)
0668 /**
0669   * @brief  Set Tamper with interrupt.
0670   * @param  hrtc RTC handle
0671   * @param  sTamper Pointer to Tamper Structure.
0672   * @retval HAL status
0673   */
0674 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
0675 {
0676   uint32_t tmpreg;
0677 
0678   /* Point on TAMPER registers base address */
0679   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
0680 
0681   /* Check the parameters */
0682   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
0683   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
0684   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
0685   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
0686   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
0687   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
0688   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
0689   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
0690   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
0691   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
0692 
0693   /* Copy configuration register into temporary variable */
0694   tmpreg = tamp->CR2;
0695 
0696   /* Clear the bits that are going to be configured and leave the others unchanged */
0697   tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
0698 
0699   /* Configure the tamper trigger bit */
0700   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
0701   {
0702     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
0703   }
0704 
0705   /* Configure the tamper flags masking bit */
0706   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
0707   {
0708     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
0709   }
0710 
0711   /* Configure the tamper backup registers erasure bit */
0712   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
0713   {
0714     tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
0715   }
0716   tamp->CR2 = tmpreg;
0717 
0718   /* Configure filtering parameters */
0719   tamp->FLTCR = (sTamper->Filter)            | (sTamper->SamplingFrequency) | \
0720                 (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
0721 
0722   /* Configure Timestamp saving on tamper detection */
0723   if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
0724   {
0725     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0726     tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
0727     hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
0728     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0729   }
0730 
0731   /* Configure RTC Tamper Interrupt: EXTI configuration */
0732   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
0733   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
0734 
0735   /* Enable interrupt on selected tamper */
0736   tamp->IER |= sTamper->Tamper;
0737 
0738   /* Enable selected tamper */
0739   tamp->CR1 |= sTamper->Tamper;
0740 
0741   return HAL_OK;
0742 }
0743 #else
0744 /**
0745   * @brief  Set Tamper with interrupt.
0746   * @note   By calling this API we force the tamper interrupt for all tampers.
0747   * @param  hrtc RTC handle
0748   * @param  sTamper Pointer to Tamper Structure.
0749   * @retval HAL status
0750   */
0751 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
0752 {
0753   uint32_t tmpreg;
0754 
0755   /* Check the parameters */
0756   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
0757   assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
0758   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
0759   assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
0760   assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
0761   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
0762   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
0763   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
0764   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
0765   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
0766   assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
0767 
0768   /* Process Locked */
0769   __HAL_LOCK(hrtc);
0770 
0771   hrtc->State = HAL_RTC_STATE_BUSY;
0772 
0773   /* Copy control register into temporary variable */
0774   tmpreg = hrtc->Instance->TAMPCR;
0775 
0776   /* Enable selected tamper */
0777   tmpreg |= (sTamper->Tamper);
0778 
0779   /* Configure the tamper trigger bit (located just next to the tamper enable bit) */
0780   if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
0781   {
0782     /* Set the tamper trigger bit */
0783     tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
0784   }
0785   else
0786   {
0787     /* Clear the tamper trigger bit */
0788     tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
0789   }
0790 
0791   /* Configure the tamper backup registers erasure bit */
0792   if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
0793   {
0794     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0795     {
0796       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
0797     }
0798     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0799     {
0800       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
0801     }
0802     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0803     {
0804       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
0805     }
0806   }
0807   else
0808   {
0809     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0810     {
0811       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
0812     }
0813     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0814     {
0815       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
0816     }
0817     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0818     {
0819       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
0820     }
0821   }
0822 
0823   /* Configure the tamper flags masking bit */
0824   if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
0825   {
0826     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0827     {
0828       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
0829     }
0830     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0831     {
0832       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
0833     }
0834     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0835     {
0836       tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
0837     }
0838   }
0839   else
0840   {
0841     if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
0842     {
0843       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
0844     }
0845     if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
0846     {
0847       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
0848     }
0849     if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
0850     {
0851       tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
0852     }
0853   }
0854 
0855   /* Clearing remaining fields before setting them */
0856   tmpreg &= ~(RTC_TAMPERFILTER_MASK            | RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
0857               RTC_TAMPERPRECHARGEDURATION_MASK | RTC_TAMPER_PULLUP_MASK             | \
0858               RTC_TIMESTAMPONTAMPERDETECTION_MASK);
0859 
0860   /* Set remaining parameters of desired configuration into temporary variable */
0861   tmpreg |= ((uint32_t)sTamper->Filter            | \
0862              (uint32_t)sTamper->SamplingFrequency | \
0863              (uint32_t)sTamper->PrechargeDuration | \
0864              (uint32_t)sTamper->TamperPullUp      | \
0865              (uint32_t)sTamper->TimeStampOnTamperDetection);
0866 
0867   /* Enable interrupt on selected tamper */
0868   tmpreg |= (uint32_t)sTamper->Interrupt;
0869 
0870   /* Copy desired configuration into configuration register */
0871   hrtc->Instance->TAMPCR = tmpreg;
0872 
0873   /* RTC Tamper Interrupt Configuration: EXTI configuration */
0874 #if defined(DUAL_CORE)
0875   if (HAL_GetCurrentCPUID() == CM7_CPUID)
0876   {
0877     __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
0878   }
0879   else
0880   {
0881     __HAL_RTC_TAMPER_TIMESTAMP_EXTID2_ENABLE_IT();
0882   }
0883 #else /* SINGLE_CORE */
0884   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
0885 #endif /* DUAL_CORE */
0886 
0887   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
0888 
0889   hrtc->State = HAL_RTC_STATE_READY;
0890 
0891   /* Process Unlocked */
0892   __HAL_UNLOCK(hrtc);
0893 
0894   return HAL_OK;
0895 }
0896 #endif /* TAMP */
0897 
0898 #if defined(TAMP)
0899 /**
0900   * @brief  Deactivate Tamper.
0901   * @param  hrtc RTC handle
0902   * @param  Tamper Selected tamper pin.
0903   *         This parameter can be a combination of the following values:
0904   *         @arg RTC_TAMPER_1
0905   *         @arg RTC_TAMPER_2
0906   * @retval HAL status
0907   */
0908 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc, uint32_t Tamper)
0909 {
0910   /* Point on TAMPER registers base address */
0911   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
0912 
0913   assert_param(IS_RTC_TAMPER(Tamper));
0914 
0915   /* Disable the selected Tamper pin */
0916   tamp->CR1 &= ~Tamper;
0917 
0918   /* Disable the selected Tamper interrupt */
0919   tamp->IER &= ~Tamper;
0920 
0921   /* Clear the selected tamper flags in SR register by setting corresponding bits in SCR register */
0922   tamp->SCR = Tamper;
0923 
0924   /* Clear the selected tamper configuration (trigger, mask flag, and no-erase) */
0925   tamp->CR2 &= ~((Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | (Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
0926 
0927   return HAL_OK;
0928 }
0929 #else
0930 /**
0931   * @brief  Deactivate Tamper.
0932   * @param  hrtc RTC handle
0933   * @param  Tamper Selected tamper pin.
0934   *         This parameter can be any combination of the following values:
0935   *         @arg RTC_TAMPER_1
0936   *         @arg RTC_TAMPER_2
0937   *         @arg RTC_TAMPER_3
0938   * @retval HAL status
0939   */
0940 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc, uint32_t Tamper)
0941 {
0942   assert_param(IS_RTC_TAMPER(Tamper));
0943 
0944   /* Process Locked */
0945   __HAL_LOCK(hrtc);
0946 
0947   hrtc->State = HAL_RTC_STATE_BUSY;
0948 
0949   /* Disable the selected Tamper pin */
0950   hrtc->Instance->TAMPCR &= ((uint32_t)~Tamper);
0951 
0952   /* Disable the selected Tamper interrupt */
0953   if ((Tamper & RTC_TAMPER_1) != 0U)
0954   {
0955     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1));
0956   }
0957 
0958   if ((Tamper & RTC_TAMPER_2) != 0U)
0959   {
0960     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2));
0961   }
0962 
0963   if ((Tamper & RTC_TAMPER_3) != 0U)
0964   {
0965     hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3));
0966   }
0967 
0968   hrtc->State = HAL_RTC_STATE_READY;
0969 
0970   /* Process Unlocked */
0971   __HAL_UNLOCK(hrtc);
0972 
0973   return HAL_OK;
0974 }
0975 #endif /* TAMP */
0976 
0977 #if defined(TAMP)
0978 /**
0979   * @brief  Set Internal Tamper
0980   * @param  hrtc RTC handle
0981   * @param  sIntTamper Pointer to Internal Tamper Structure.
0982   * @retval HAL status
0983   */
0984 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
0985 {
0986   /* Check the parameters */
0987   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
0988   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
0989 
0990   /* Time-Stamp on internal tamper */
0991   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
0992   {
0993     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0994     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
0995     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0996   }
0997 
0998   /* Control register 1 */
0999   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1000 
1001   return HAL_OK;
1002 }
1003 
1004 /**
1005   * @brief  Set Internal Tamper in interrupt mode
1006   * @param  hrtc RTC handle
1007   * @param  sIntTamper Pointer to Internal Tamper Structure.
1008   * @retval HAL status
1009   */
1010 HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
1011 {
1012   /* Check the parameters */
1013   assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
1014   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
1015 
1016   /* Time-stamp on internal tamper */
1017   if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
1018   {
1019     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1020     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
1021     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1022   }
1023 
1024   /* RTC Tamper Interrupt Configuration: EXTI configuration */
1025   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
1026   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
1027   /* Interrupt enable register */
1028   SET_BIT(TAMP->IER, sIntTamper->IntTamper);
1029 
1030   /* Control register 1 */
1031   SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
1032 
1033   return HAL_OK;
1034 }
1035 
1036 /**
1037   * @brief  Deactivate Internal Tamper.
1038   * @param  hrtc RTC handle
1039   * @param  IntTamper Selected internal tamper event.
1040   *          This parameter can be any combination of existing internal tampers.
1041   * @retval HAL status
1042   */
1043 HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
1044 {
1045   UNUSED(hrtc);
1046   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1047 
1048   /* Disable the selected Tamper pin */
1049   CLEAR_BIT(TAMP->CR1, IntTamper);
1050 
1051   /* Clear internal tamper interrupt mode configuration */
1052   CLEAR_BIT(TAMP->IER, IntTamper);
1053 
1054   /* Clear internal tamper interrupt */
1055   WRITE_REG(TAMP->SCR, IntTamper);
1056 
1057   return HAL_OK;
1058 }
1059 
1060 /**
1061   * @brief  Set all active Tampers at the same time.
1062   * @param  hrtc RTC handle
1063   * @param  sAllTamper Pointer to active Tamper Structure.
1064   * @retval HAL status
1065   */
1066 HAL_StatusTypeDef HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef *hrtc, RTC_ActiveTampersTypeDef *sAllTamper)
1067 {
1068   uint32_t IER, CR1, CR2, ATCR1, CR, i, tickstart;
1069 
1070 #ifdef  USE_FULL_ASSERT
1071   for (i = 0; i < RTC_TAMP_NB; i++)
1072   {
1073     assert_param(IS_RTC_TAMPER_ERASE_MODE(sAllTamper->TampInput[i].NoErase));
1074     assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sAllTamper->TampInput[i].MaskFlag));
1075     /* Mask flag only supported by TAMPER 1, 2, and 3 */
1076     assert_param(!((sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (i > RTC_TAMPER_3)));
1077   }
1078 
1079   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sAllTamper->TimeStampOnTamperDetection));
1080 #endif /* USE_FULL_ASSERT */
1081 
1082   /* Active Tampers must not be already enabled */
1083   if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) != 0U)
1084   {
1085     /* Disable all active tampers with HAL_RTCEx_DeactivateActiveTampers */
1086     if (HAL_RTCEx_DeactivateActiveTampers(hrtc) != HAL_OK)
1087     {
1088       return HAL_ERROR;
1089     }
1090   }
1091 
1092   /* Set TimeStamp on tamper detection */
1093   CR = READ_REG(RTC->CR);
1094   if ((CR & RTC_CR_TAMPTS) != (sAllTamper->TimeStampOnTamperDetection))
1095   {
1096     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1097     MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sAllTamper->TimeStampOnTamperDetection);
1098     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1099   }
1100 
1101   CR1 = READ_REG(TAMP->CR1);
1102   CR2 = READ_REG(TAMP->CR2);
1103   IER = READ_REG(TAMP->IER);
1104 
1105   /* Set common parameters */
1106   ATCR1 = (sAllTamper->ActiveFilter | (sAllTamper->ActiveOutputChangePeriod << TAMP_ATCR1_ATPER_Pos) | sAllTamper->ActiveAsyncPrescaler);
1107 
1108   /* Set specific parameters for each active tamper inputs if enable */
1109   for (i = 0; i < RTC_TAMP_NB; i++)
1110   {
1111     if (sAllTamper->TampInput[i].Enable != RTC_ATAMP_DISABLE)
1112     {
1113       CR1 |= (TAMP_CR1_TAMP1E << i);
1114       ATCR1 |= (TAMP_ATCR1_TAMP1AM << i);
1115 
1116       if (sAllTamper->TampInput[i].Interrupt != RTC_ATAMP_INTERRUPT_DISABLE)
1117       {
1118         /* RTC Tamper Interrupt Configuration: EXTI configuration */
1119         __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
1120         __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
1121 
1122         /* Interrupt enable register */
1123         IER |= (TAMP_IER_TAMP1IE << i);
1124       }
1125 
1126       if (sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
1127       {
1128         CR2 |= (TAMP_CR2_TAMP1MSK << i);
1129       }
1130 
1131       if (sAllTamper->TampInput[i].NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
1132       {
1133         CR2 |= (TAMP_CR2_TAMP1NOERASE << i);
1134       }
1135 
1136       /* Set ATOSHARE and configure ATOSELx[] in case of output sharing */
1137       if (sAllTamper->TampInput[i].Output != i)
1138       {
1139         ATCR1 |= TAMP_ATCR1_ATOSHARE;
1140         ATCR1 |= sAllTamper->TampInput[i].Output << ((2u * i) + TAMP_ATCR1_ATOSEL1_Pos);
1141       }
1142     }
1143   }
1144 
1145   WRITE_REG(TAMP->IER, IER);
1146   WRITE_REG(TAMP->IER, IER);
1147   WRITE_REG(TAMP->ATCR1, ATCR1);
1148 #if defined(TAMP_ATCR2_ATOSEL1)
1149   WRITE_REG(TAMP->ATCR2, ATCR2);
1150 #endif /* TAMP_ATCR2_ATOSEL1 */
1151   WRITE_REG(TAMP->CR2, CR2);
1152   WRITE_REG(TAMP->CR1, CR1);
1153 
1154   /* Write seed */
1155   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1156   {
1157     WRITE_REG(TAMP->ATSEEDR, sAllTamper->Seed[i]);
1158   }
1159 
1160   /* Wait till RTC SEEDF flag is set and if timeout is reached exit */
1161   tickstart = HAL_GetTick();
1162   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0u)
1163   {
1164     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1165     {
1166       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1167       return HAL_TIMEOUT;
1168     }
1169   }
1170 
1171   return HAL_OK;
1172 }
1173 
1174 /**
1175   * @brief  Write a new seed. Active tamper must be enabled.
1176   * @param  hrtc RTC handle
1177   * @param  pSeed Pointer to active tamper seed values.
1178   * @retval HAL status
1179   */
1180 HAL_StatusTypeDef HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef *hrtc, uint32_t *pSeed)
1181 {
1182   uint32_t i, tickstart;
1183 
1184   /* Active Tampers must be enabled */
1185   if (READ_BIT(TAMP->ATOR,  TAMP_ATOR_INITS) == 0U)
1186   {
1187     return HAL_ERROR;
1188   }
1189 
1190   for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
1191   {
1192     WRITE_REG(TAMP->ATSEEDR, pSeed[i]);
1193   }
1194 
1195   /* Wait till RTC SEEDF flag is set and if timeout is reached exit */
1196   tickstart = HAL_GetTick();
1197   while (READ_BIT(TAMP->ATOR,  TAMP_ATOR_SEEDF) != 0U)
1198   {
1199     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1200     {
1201       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1202       return HAL_TIMEOUT;
1203     }
1204   }
1205 
1206   return HAL_OK;
1207 }
1208 
1209 /**
1210   * @brief  Deactivate all Active Tampers at the same time.
1211   * @param  hrtc RTC handle
1212   * @retval HAL status
1213   */
1214 HAL_StatusTypeDef HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef *hrtc)
1215 {
1216   /* Get Active tampers */
1217   uint32_t ATamp_mask = READ_BIT(TAMP->ATCR1, TAMP_ALL);
1218 
1219   UNUSED(hrtc);
1220   /* Disable all actives tampers but not passives tampers */
1221   CLEAR_BIT(TAMP->CR1, ATamp_mask);
1222   /* Disable no erase and mask */
1223   CLEAR_BIT(TAMP->CR2, (ATamp_mask | ((ATamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM | TAMP_ATCR1_TAMP3AM)) << TAMP_CR2_TAMP1MSK_Pos)));
1224 
1225   /* Clear tamper interrupt and event flags (WO register) of all actives tampers but not passives tampers */
1226   WRITE_REG(TAMP->SCR, ATamp_mask);
1227 
1228   /* Clear all active tampers interrupt mode configuration but not passives tampers */
1229   CLEAR_BIT(TAMP->IER, ATamp_mask);
1230 
1231   CLEAR_BIT(TAMP->ATCR1, TAMP_ALL | TAMP_ATCR1_ATCKSEL | TAMP_ATCR1_ATPER | \
1232             TAMP_ATCR1_ATOSHARE | TAMP_ATCR1_FLTEN);
1233 
1234 #if defined(TAMP_ATCR2_ATOSEL1)
1235   CLEAR_BIT(TAMP->ATCR2, TAMP_ATCR2_ATOSEL1 | TAMP_ATCR2_ATOSEL2 | TAMP_ATCR2_ATOSEL3 | TAMP_ATCR2_ATOSEL4 |
1236             TAMP_ATCR2_ATOSEL5 | TAMP_ATCR2_ATOSEL6 | TAMP_ATCR2_ATOSEL7 | TAMP_ATCR2_ATOSEL8);
1237 #endif /* TAMP_ATCR2_ATOSEL1 */
1238 
1239   return HAL_OK;
1240 }
1241 #endif /* TAMP */
1242 
1243 /**
1244   * @}
1245   */
1246 
1247 /** @addtogroup RTCEx_Exported_Functions_Group1
1248  *  @brief   RTC TimeStamp and Tamper functions
1249  *
1250 * @{
1251 */
1252 
1253 /**
1254   * @brief  Handle Tamper and TimeStamp interrupt request.
1255   * @param  hrtc RTC handle
1256   * @retval None
1257   */
1258 #if defined(TAMP)
1259 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
1260 {
1261 
1262   /* Point on TAMPER registers base address */
1263   TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
1264 
1265   /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
1266   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
1267 
1268   if ((hrtc->Instance->MISR & RTC_MISR_TSMF) != 0u)
1269   {
1270 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1271     /* Call TimeStampEvent registered Callback */
1272     hrtc->TimeStampEventCallback(hrtc);
1273 #else
1274     HAL_RTCEx_TimeStampEventCallback(hrtc);
1275 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1276     /* Not immediately clear flags because the content of RTC_TSTR and RTC_TSDR are cleared when TSF bit is reset.*/
1277     hrtc->Instance->SCR = RTC_SCR_CTSF;
1278   }
1279 
1280   /* Get interrupt status */
1281   uint32_t tmp = tamp->MISR;
1282 
1283   /* Immediately clear flags */
1284   tamp->SCR = tmp;
1285 
1286   /* Check Tamper 1 status */
1287   if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
1288   {
1289 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1290     /* Call Tamper 1 Event registered Callback */
1291     hrtc->Tamper1EventCallback(hrtc);
1292 #else
1293     /* Tamper 1 callback */
1294     HAL_RTCEx_Tamper1EventCallback(hrtc);
1295 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1296   }
1297 
1298   /* Check Tamper 2 status */
1299   if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
1300   {
1301 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1302     /* Call Tamper 2 Event registered Callback */
1303     hrtc->Tamper2EventCallback(hrtc);
1304 #else
1305     /* Tamper 2 callback */
1306     HAL_RTCEx_Tamper2EventCallback(hrtc);
1307 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1308   }
1309 
1310   /* Check Tamper 3 status */
1311   if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
1312   {
1313 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1314     /* Call Tamper 3 Event registered Callback */
1315     hrtc->Tamper3EventCallback(hrtc);
1316 #else
1317     /* Tamper 3 callback */
1318     HAL_RTCEx_Tamper3EventCallback(hrtc);
1319 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1320   }
1321 
1322   /* Check Internal Tamper 1 status */
1323   if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
1324   {
1325 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1326     /* Call Internal Tamper 1 Event registered callback */
1327     hrtc->InternalTamper1EventCallback(hrtc);
1328 #else
1329     /* Call Internal Tamper 1 Event by-default callback */
1330     HAL_RTCEx_InternalTamper1EventCallback(hrtc);
1331 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1332   }
1333 
1334   /* Check Internal Tamper 2 status */
1335   if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
1336   {
1337 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1338     /* Call Internal Tamper 2 Event registered callback */
1339     hrtc->InternalTamper2EventCallback(hrtc);
1340 #else
1341     /* Call Internal Tamper 2 Event by-default callback */
1342     HAL_RTCEx_InternalTamper2EventCallback(hrtc);
1343 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1344   }
1345 
1346   /* Check Internal Tamper 3 status */
1347   if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
1348   {
1349 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1350     /* Call Internal Tamper 3 Event registered callback */
1351     hrtc->InternalTamper3EventCallback(hrtc);
1352 #else
1353     /* Call Internal Tamper 3 Event by-default callback */
1354     HAL_RTCEx_InternalTamper3EventCallback(hrtc);
1355 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1356   }
1357 
1358   /* Check Internal Tamper 4 status */
1359   if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
1360   {
1361 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1362     /* Call Internal Tamper 4 Event registered callback */
1363     hrtc->InternalTamper4EventCallback(hrtc);
1364 #else
1365     /* Call Internal Tamper 4 Event by-default callback */
1366     HAL_RTCEx_InternalTamper4EventCallback(hrtc);
1367 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1368   }
1369 
1370   /* Check Internal Tamper 5 status */
1371   if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
1372   {
1373 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1374     /* Call Internal Tamper 5 Event registered callback */
1375     hrtc->InternalTamper5EventCallback(hrtc);
1376 #else
1377     /* Call Internal Tamper 5 Event by-default callback */
1378     HAL_RTCEx_InternalTamper5EventCallback(hrtc);
1379 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1380   }
1381 
1382   /* Check Internal Tamper 6 status */
1383   if ((tmp & RTC_INT_TAMPER_6) == RTC_INT_TAMPER_6)
1384   {
1385 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1386     /* Call Internal Tamper 6 Event registered callback */
1387     hrtc->InternalTamper6EventCallback(hrtc);
1388 #else
1389     /* Call Internal Tamper 6 Event by-default callback */
1390     HAL_RTCEx_InternalTamper6EventCallback(hrtc);
1391 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1392   }
1393 
1394   /* Check Internal Tamper 8 status */
1395   if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
1396   {
1397 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1398     /* Call Internal Tamper 8 Event registered callback */
1399     hrtc->InternalTamper8EventCallback(hrtc);
1400 #else
1401     /* Call Internal Tamper 8 Event by-default callback */
1402     HAL_RTCEx_InternalTamper8EventCallback(hrtc);
1403 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
1404   }
1405 
1406   /* Change RTC state */
1407   hrtc->State = HAL_RTC_STATE_READY;
1408 }
1409 #else
1410 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
1411 {
1412   /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
1413 #if defined(DUAL_CORE)
1414   if (HAL_GetCurrentCPUID() == CM7_CPUID)
1415   {
1416     __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
1417   }
1418   else
1419   {
1420     __HAL_RTC_TAMPER_TIMESTAMP_EXTID2_CLEAR_FLAG();
1421   }
1422 #else /* SINGLE_CORE */
1423   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
1424 #endif /* DUAL_CORE */
1425 
1426   /* Get the TimeStamp interrupt source enable status */
1427   if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
1428   {
1429     /* Get the pending status of the TIMESTAMP Interrupt */
1430     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
1431     {
1432       /* TIMESTAMP callback */
1433 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1434       hrtc->TimeStampEventCallback(hrtc);
1435 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1436       HAL_RTCEx_TimeStampEventCallback(hrtc);
1437 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1438 
1439       /* Clear the TIMESTAMP interrupt pending bit (this will clear timestamp time and date registers) */
1440       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
1441     }
1442   }
1443 
1444   /* Get the Tamper 1 interrupt source enable status */
1445   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != 0U)
1446   {
1447     /* Get the pending status of the Tamper 1 Interrupt */
1448     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
1449     {
1450       /* Clear the Tamper 1 interrupt pending bit */
1451       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
1452 
1453       /* Tamper 1 callback */
1454 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1455       hrtc->Tamper1EventCallback(hrtc);
1456 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1457       HAL_RTCEx_Tamper1EventCallback(hrtc);
1458 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1459     }
1460   }
1461 
1462   /* Get the Tamper 2 interrupt source enable status */
1463   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != 0U)
1464   {
1465     /* Get the pending status of the Tamper 2 Interrupt */
1466     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
1467     {
1468       /* Clear the Tamper 2 interrupt pending bit */
1469       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
1470 
1471       /* Tamper 2 callback */
1472 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1473       hrtc->Tamper2EventCallback(hrtc);
1474 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1475       HAL_RTCEx_Tamper2EventCallback(hrtc);
1476 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1477     }
1478   }
1479 
1480   /* Get the Tamper 3 interrupts source enable status */
1481   if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != 0U)
1482   {
1483     /* Get the pending status of the Tamper 3 Interrupt */
1484     if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
1485     {
1486       /* Clear the Tamper 3 interrupt pending bit */
1487       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
1488 
1489       /* Tamper 3 callback */
1490 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1491       hrtc->Tamper3EventCallback(hrtc);
1492 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1493       HAL_RTCEx_Tamper3EventCallback(hrtc);
1494 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1495     }
1496   }
1497 
1498   /* Change RTC state */
1499   hrtc->State = HAL_RTC_STATE_READY;
1500 }
1501 #endif /* TAMP */
1502 
1503 /**
1504   * @brief  TimeStamp callback.
1505   * @param  hrtc RTC handle
1506   * @retval None
1507   */
1508 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
1509 {
1510   /* Prevent unused argument(s) compilation warning */
1511   UNUSED(hrtc);
1512 
1513   /* NOTE : This function should not be modified, when the callback is needed,
1514             the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
1515   */
1516 }
1517 
1518 /**
1519   * @}
1520   */
1521 
1522 /** @addtogroup RTCEx_Exported_Functions_Group5
1523   * @brief      Extended RTC Tamper functions
1524   *
1525 * @{
1526 */
1527 
1528 /**
1529   * @brief  Tamper 1 callback.
1530   * @param  hrtc RTC handle
1531   * @retval None
1532   */
1533 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)
1534 {
1535   /* Prevent unused argument(s) compilation warning */
1536   UNUSED(hrtc);
1537 
1538   /* NOTE : This function should not be modified, when the callback is needed,
1539             the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
1540    */
1541 }
1542 
1543 /**
1544   * @brief  Tamper 2 callback.
1545   * @param  hrtc RTC handle
1546   * @retval None
1547   */
1548 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)
1549 {
1550   /* Prevent unused argument(s) compilation warning */
1551   UNUSED(hrtc);
1552 
1553   /* NOTE : This function should not be modified, when the callback is needed,
1554             the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
1555    */
1556 }
1557 
1558 /**
1559   * @brief  Tamper 3 callback.
1560   * @param  hrtc RTC handle
1561   * @retval None
1562   */
1563 __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)
1564 {
1565   /* Prevent unused argument(s) compilation warning */
1566   UNUSED(hrtc);
1567 
1568   /* NOTE : This function should not be modified, when the callback is needed,
1569             the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
1570    */
1571 }
1572 
1573 #if defined(TAMP)
1574 /**
1575   * @brief  Internal Tamper 1 callback.
1576   * @param  hrtc RTC handle
1577   * @retval None
1578   */
1579 __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
1580 {
1581   /* Prevent unused argument(s) compilation warning */
1582   UNUSED(hrtc);
1583 
1584   /* NOTE : This function should not be modified, when the callback is needed,
1585             the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
1586    */
1587 }
1588 
1589 /**
1590   * @brief  Internal Tamper 2 callback.
1591   * @param  hrtc RTC handle
1592   * @retval None
1593   */
1594 __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
1595 {
1596   /* Prevent unused argument(s) compilation warning */
1597   UNUSED(hrtc);
1598 
1599   /* NOTE : This function should not be modified, when the callback is needed,
1600             the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
1601    */
1602 }
1603 
1604 /**
1605   * @brief  Internal Tamper 3 callback.
1606   * @param  hrtc RTC handle
1607   * @retval None
1608   */
1609 __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
1610 {
1611   /* Prevent unused argument(s) compilation warning */
1612   UNUSED(hrtc);
1613 
1614   /* NOTE : This function should not be modified, when the callback is needed,
1615             the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
1616    */
1617 }
1618 
1619 /**
1620   * @brief  Internal Tamper 4 callback.
1621   * @param  hrtc RTC handle
1622   * @retval None
1623   */
1624 __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
1625 {
1626   /* Prevent unused argument(s) compilation warning */
1627   UNUSED(hrtc);
1628 
1629   /* NOTE : This function should not be modified, when the callback is needed,
1630             the HAL_RTCEx_InternalTamper4EventCallback could be implemented in the user file
1631    */
1632 }
1633 
1634 /**
1635   * @brief  Internal Tamper 5 callback.
1636   * @param  hrtc RTC handle
1637   * @retval None
1638   */
1639 __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
1640 {
1641   /* Prevent unused argument(s) compilation warning */
1642   UNUSED(hrtc);
1643 
1644   /* NOTE : This function should not be modified, when the callback is needed,
1645             the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
1646    */
1647 }
1648 
1649 /**
1650   * @brief  Internal Tamper 6 callback.
1651   * @param  hrtc RTC handle
1652   * @retval None
1653   */
1654 __weak void HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef *hrtc)
1655 {
1656   /* Prevent unused argument(s) compilation warning */
1657   UNUSED(hrtc);
1658 
1659   /* NOTE : This function should not be modified, when the callback is needed,
1660             the HAL_RTCEx_InternalTamper6EventCallback could be implemented in the user file
1661    */
1662 }
1663 
1664 /**
1665   * @brief  Internal Tamper 8 callback.
1666   * @param  hrtc RTC handle
1667   * @retval None
1668   */
1669 __weak void HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef *hrtc)
1670 {
1671   /* Prevent unused argument(s) compilation warning */
1672   UNUSED(hrtc);
1673 
1674   /* NOTE : This function should not be modified, when the callback is needed,
1675             the HAL_RTCEx_InternalTamper8EventCallback could be implemented in the user file
1676    */
1677 }
1678 #endif /* TAMP */
1679 
1680 /**
1681   * @}
1682   */
1683 
1684 /** @addtogroup RTCEx_Exported_Functions_Group1
1685  *  @brief   RTC TimeStamp and Tamper functions
1686  *
1687 * @{
1688 */
1689 
1690 /**
1691   * @brief  Handle TimeStamp polling request.
1692   * @param  hrtc RTC handle
1693   * @param  Timeout Timeout duration
1694   * @retval HAL status
1695   */
1696 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1697 {
1698   uint32_t tickstart = HAL_GetTick();
1699 
1700   while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
1701   {
1702     if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
1703     {
1704       /* Clear the TIMESTAMP OverRun Flag */
1705       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
1706 
1707       /* Change TIMESTAMP state */
1708       hrtc->State = HAL_RTC_STATE_ERROR;
1709 
1710       return HAL_ERROR;
1711     }
1712 
1713     if (Timeout != HAL_MAX_DELAY)
1714     {
1715       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1716       {
1717         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1718         return HAL_TIMEOUT;
1719       }
1720     }
1721   }
1722 
1723   /* Change RTC state */
1724   hrtc->State = HAL_RTC_STATE_READY;
1725 
1726   return HAL_OK;
1727 }
1728 
1729 /**
1730   * @}
1731   */
1732 
1733 /** @addtogroup RTCEx_Exported_Functions_Group5
1734   * @brief      Extended RTC Tamper functions
1735   *
1736 * @{
1737 */
1738 
1739 /**
1740   * @brief  Handle Tamper1 Polling.
1741   * @param  hrtc RTC handle
1742   * @param  Timeout Timeout duration
1743   * @retval HAL status
1744   */
1745 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
1746 {
1747   uint32_t tickstart = HAL_GetTick();
1748 
1749   /* Get the status of the Interrupt */
1750   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
1751   {
1752     if (Timeout != HAL_MAX_DELAY)
1753     {
1754       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1755       {
1756         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1757         return HAL_TIMEOUT;
1758       }
1759     }
1760   }
1761 
1762   /* Clear the Tamper Flag */
1763   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
1764 
1765   /* Change RTC state */
1766   hrtc->State = HAL_RTC_STATE_READY;
1767 
1768   return HAL_OK;
1769 }
1770 
1771 /**
1772   * @brief  Handle Tamper2 Polling.
1773   * @param  hrtc RTC handle
1774   * @param  Timeout Timeout duration
1775   * @retval HAL status
1776   */
1777 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
1778 {
1779   uint32_t tickstart = HAL_GetTick();
1780 
1781   /* Get the status of the Interrupt */
1782   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
1783   {
1784     if (Timeout != HAL_MAX_DELAY)
1785     {
1786       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1787       {
1788         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1789         return HAL_TIMEOUT;
1790       }
1791     }
1792   }
1793 
1794   /* Clear the Tamper Flag */
1795   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
1796 
1797   /* Change RTC state */
1798   hrtc->State = HAL_RTC_STATE_READY;
1799 
1800   return HAL_OK;
1801 }
1802 
1803 /**
1804   * @brief  Handle Tamper3 Polling.
1805   * @param  hrtc RTC handle
1806   * @param  Timeout Timeout duration
1807   * @retval HAL status
1808   */
1809 HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
1810 {
1811   uint32_t tickstart = HAL_GetTick();
1812 
1813   /* Get the status of the Interrupt */
1814   while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
1815   {
1816     if (Timeout != HAL_MAX_DELAY)
1817     {
1818       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1819       {
1820         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1821         return HAL_TIMEOUT;
1822       }
1823     }
1824   }
1825 
1826   /* Clear the Tamper Flag */
1827   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
1828 
1829   /* Change RTC state */
1830   hrtc->State = HAL_RTC_STATE_READY;
1831 
1832   return HAL_OK;
1833 }
1834 
1835 #if defined(TAMP)
1836 /**
1837   * @brief  Internal Tamper event polling.
1838   * @param  hrtc RTC handle
1839   * @param  IntTamper selected tamper.
1840   *          This parameter can be any combination of existing internal tampers.
1841   * @param  Timeout Timeout duration
1842   * @retval HAL status
1843   */
1844 HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t IntTamper, uint32_t Timeout)
1845 {
1846   UNUSED(hrtc);
1847   assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
1848 
1849   uint32_t tickstart = HAL_GetTick();
1850 
1851   /* Get the status of the Interrupt */
1852   while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
1853   {
1854     if (Timeout != HAL_MAX_DELAY)
1855     {
1856       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1857       {
1858         return HAL_TIMEOUT;
1859       }
1860     }
1861   }
1862 
1863   /* Clear the Tamper Flag */
1864   WRITE_REG(TAMP->SCR, IntTamper);
1865 
1866   return HAL_OK;
1867 }
1868 #endif /* TAMP */
1869 
1870 /**
1871   * @}
1872   */
1873 
1874 /** @addtogroup RTCEx_Exported_Functions_Group2
1875   * @brief    RTC Wake-up functions
1876   *
1877 @verbatim
1878  ===============================================================================
1879                         ##### RTC Wake-up functions #####
1880  ===============================================================================
1881 
1882  [..] This section provides functions allowing to configure Wake-up feature
1883 
1884 @endverbatim
1885   * @{
1886   */
1887 
1888 /**
1889   * @brief  Set wake up timer.
1890   * @param  hrtc RTC handle
1891   * @param  WakeUpCounter Wake up counter
1892   * @param  WakeUpClock Wake up clock
1893   * @retval HAL status
1894   */
1895 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1896 {
1897   uint32_t tickstart;
1898 
1899   /* Check the parameters */
1900   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1901   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1902 
1903   /* Process Locked */
1904   __HAL_LOCK(hrtc);
1905 
1906   hrtc->State = HAL_RTC_STATE_BUSY;
1907 
1908   /* Disable the write protection for RTC registers */
1909   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1910 
1911   /* Clear WUTE in RTC_CR to disable the wakeup timer */
1912   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
1913 
1914   /* Poll WUTWF until it is set in RTC_ICSR / RTC_ISR to make sure the access to wakeup autoreload
1915   counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
1916   calendar initialization mode. */
1917 #if defined(TAMP)
1918   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
1919   {
1920     tickstart = HAL_GetTick();
1921 
1922     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) == 0U)
1923 #else
1924   if (READ_BIT(RTC->ISR, RTC_ISR_INITF) == 0U)
1925   {
1926     tickstart = HAL_GetTick();
1927 
1928     while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
1929 #endif /* TAMP */
1930     {
1931       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1932       {
1933         /* Enable the write protection for RTC registers */
1934         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1935 
1936         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1937 
1938         /* Process Unlocked */
1939         __HAL_UNLOCK(hrtc);
1940 
1941         return HAL_TIMEOUT;
1942       }
1943     }
1944   }
1945 
1946   /* Clear the Wakeup Timer clock source bits and configure the clock source in CR register */
1947   uint32_t CR_tmp = hrtc->Instance->CR;
1948   CR_tmp &= (uint32_t)~RTC_CR_WUCKSEL;
1949   CR_tmp |= (uint32_t)WakeUpClock;
1950   hrtc->Instance->CR = CR_tmp;
1951 
1952   /* Configure the Wakeup Timer counter */
1953   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
1954 
1955   /* Enable the Wakeup Timer */
1956   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1957 
1958   /* Enable the write protection for RTC registers */
1959   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1960 
1961   hrtc->State = HAL_RTC_STATE_READY;
1962 
1963   /* Process Unlocked */
1964   __HAL_UNLOCK(hrtc);
1965 
1966   return HAL_OK;
1967 }
1968 
1969 /**
1970   * @brief  Set wake up timer with interrupt.
1971   * @param  hrtc RTC handle
1972   * @param  WakeUpCounter Wake up counter
1973   * @param  WakeUpClock Wake up clock
1974   * @retval HAL status
1975   */
1976 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1977 {
1978   uint32_t tickstart;
1979 
1980   /* Check the parameters */
1981   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
1982   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
1983 
1984   /* Process Locked */
1985   __HAL_LOCK(hrtc);
1986 
1987   hrtc->State = HAL_RTC_STATE_BUSY;
1988 
1989   /* Disable the write protection for RTC registers */
1990   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1991 
1992   /* Clear WUTE in RTC_CR to disable the wakeup timer */
1993   CLEAR_BIT(RTC->CR, RTC_CR_WUTE);
1994 
1995   /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
1996   counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
1997   calendar initialization mode. */
1998 #if defined(TAMP)
1999   if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
2000   {
2001     tickstart = HAL_GetTick();
2002 
2003     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) == 0U)
2004 #else
2005   if (READ_BIT(RTC->ISR, RTC_ISR_INITF) == 0U)
2006   {
2007     tickstart = HAL_GetTick();
2008 
2009     while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
2010 #endif /* TAMP */
2011     {
2012       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2013       {
2014         /* Enable the write protection for RTC registers */
2015         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2016 
2017         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2018 
2019         /* Process Unlocked */
2020         __HAL_UNLOCK(hrtc);
2021 
2022         return HAL_TIMEOUT;
2023       }
2024     }
2025   }
2026 
2027   /* Configure the Wakeup Timer counter */
2028   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
2029 
2030   /* Clear the Wakeup Timer clock source bits and configure the clock source in CR register */
2031   {
2032     uint32_t CR_tmp = hrtc->Instance->CR;
2033     CR_tmp &= (uint32_t)~RTC_CR_WUCKSEL;
2034     CR_tmp |= (uint32_t)WakeUpClock;
2035     hrtc->Instance->CR = CR_tmp;
2036   }
2037 
2038   /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
2039 #if defined(DUAL_CORE)
2040   if (HAL_GetCurrentCPUID() == CM7_CPUID)
2041   {
2042     __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
2043   }
2044   else
2045   {
2046     __HAL_RTC_WAKEUPTIMER_EXTID2_ENABLE_IT();
2047   }
2048 #else /* SINGLE_CORE */
2049   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
2050 #endif /* DUAL_CORE */
2051 
2052   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
2053 
2054   /* Configure the Interrupt in the RTC_CR register */
2055   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
2056 
2057   /* Enable the Wakeup Timer */
2058   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
2059 
2060   /* Enable the write protection for RTC registers */
2061   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2062 
2063   hrtc->State = HAL_RTC_STATE_READY;
2064 
2065   /* Process Unlocked */
2066   __HAL_UNLOCK(hrtc);
2067 
2068   return HAL_OK;
2069 }
2070 
2071 /**
2072   * @brief  Deactivate wake up timer counter.
2073   * @param  hrtc RTC handle
2074   * @retval HAL status
2075   */
2076 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
2077 {
2078   uint32_t tickstart;
2079 
2080   /* Process Locked */
2081   __HAL_LOCK(hrtc);
2082 
2083   hrtc->State = HAL_RTC_STATE_BUSY;
2084 
2085   /* Disable the write protection for RTC registers */
2086   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2087 
2088   /* Disable the Wakeup Timer */
2089   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
2090 
2091   /* In case of interrupt mode is used, the interrupt source must disabled */
2092   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
2093 
2094   tickstart = HAL_GetTick();
2095   /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
2096 #if defined(TAMP)
2097   while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) == 0U)
2098 #else
2099   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
2100 #endif /* TAMP */
2101   {
2102     if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2103     {
2104       /* Enable the write protection for RTC registers */
2105       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2106 
2107       hrtc->State = HAL_RTC_STATE_TIMEOUT;
2108 
2109       /* Process Unlocked */
2110       __HAL_UNLOCK(hrtc);
2111 
2112       return HAL_TIMEOUT;
2113     }
2114   }
2115 
2116   /* Enable the write protection for RTC registers */
2117   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2118 
2119   hrtc->State = HAL_RTC_STATE_READY;
2120 
2121   /* Process Unlocked */
2122   __HAL_UNLOCK(hrtc);
2123 
2124   return HAL_OK;
2125 }
2126 
2127 /**
2128   * @brief  Get wake up timer counter.
2129   * @param  hrtc RTC handle
2130   * @retval Counter value
2131   */
2132 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
2133 {
2134   /* Get the counter value */
2135   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
2136 }
2137 
2138 /**
2139   * @brief  Handle Wake Up Timer interrupt request.
2140   * @param  hrtc RTC handle
2141   * @retval None
2142   */
2143 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
2144 {
2145   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
2146 #if defined(DUAL_CORE)
2147   if (HAL_GetCurrentCPUID() == CM7_CPUID)
2148   {
2149     __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
2150   }
2151   else
2152   {
2153     __HAL_RTC_WAKEUPTIMER_EXTID2_CLEAR_FLAG();
2154   }
2155 #else /* SINGLE_CORE */
2156   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
2157 #endif /* DUAL_CORE */
2158 
2159 #if defined(TAMP)
2160   /* Get the pending status of the WAKEUPTIMER Interrupt */
2161   if ((hrtc->Instance->MISR & RTC_MISR_WUTMF) != 0u)
2162   {
2163     /* Immediately clear flags */
2164     hrtc->Instance->SCR = RTC_SCR_CWUTF;
2165 
2166     /* WAKEUPTIMER callback */
2167   #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2168     /* Call WakeUpTimerEvent registered Callback */
2169     hrtc->WakeUpTimerEventCallback(hrtc);
2170   #else
2171     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
2172   #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2173   }
2174 #else
2175   /* Get the pending status of the WAKEUPTIMER Interrupt */
2176   if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
2177   {
2178     /* Clear the WAKEUPTIMER interrupt pending bit */
2179     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
2180 
2181     /* WAKEUPTIMER callback */
2182   #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
2183     /* Call WakeUpTimerEvent registered Callback */
2184     hrtc->WakeUpTimerEventCallback(hrtc);
2185   #else
2186     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
2187   #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
2188   }
2189 #endif /* TAMP */
2190 
2191   /* Change RTC state */
2192   hrtc->State = HAL_RTC_STATE_READY;
2193 }
2194 
2195 /**
2196   * @brief  Wake Up Timer callback.
2197   * @param  hrtc RTC handle
2198   * @retval None
2199   */
2200 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)
2201 {
2202   /* Prevent unused argument(s) compilation warning */
2203   UNUSED(hrtc);
2204 
2205   /* NOTE : This function should not be modified, when the callback is needed,
2206             the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
2207    */
2208 }
2209 
2210 
2211 /**
2212   * @brief  Handle Wake Up Timer Polling.
2213   * @param  hrtc RTC handle
2214   * @param  Timeout Timeout duration
2215   * @retval HAL status
2216   */
2217 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
2218 {
2219   uint32_t tickstart = HAL_GetTick();
2220 
2221   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
2222   {
2223     if (Timeout != HAL_MAX_DELAY)
2224     {
2225       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2226       {
2227         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2228         return HAL_TIMEOUT;
2229       }
2230     }
2231   }
2232 
2233   /* Clear the WAKEUPTIMER Flag */
2234   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
2235 
2236   /* Change RTC state */
2237   hrtc->State = HAL_RTC_STATE_READY;
2238 
2239   return HAL_OK;
2240 }
2241 
2242 /**
2243   * @}
2244   */
2245 
2246 
2247 /** @addtogroup RTCEx_Exported_Functions_Group6
2248   * @brief      Extended RTC Backup register functions
2249   *
2250 @verbatim
2251  ===============================================================================
2252              ##### Extended RTC Backup register functions #####
2253  ===============================================================================
2254   [..]
2255    (+) Before calling any tamper or internal tamper function, you have to call first
2256        HAL_RTC_Init() function.
2257    (+) In that ine you can select to output tamper event on RTC pin.
2258   [..]
2259    This subsection provides functions allowing to
2260    (+) Write a data in a specified RTC Backup data register
2261    (+) Read a data in a specified RTC Backup data register
2262 @endverbatim
2263   * @{
2264   */
2265 
2266 
2267 /**
2268   * @brief  Write a data in a specified RTC Backup data register.
2269   * @param  hrtc RTC handle
2270   * @param  BackupRegister RTC Backup data Register number.
2271   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
2272   *                                 specify the register.
2273   * @param  Data Data to be written in the specified Backup data register.
2274   * @retval None
2275   */
2276 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc, uint32_t BackupRegister, uint32_t Data)
2277 {
2278   uint32_t tmp;
2279 
2280   /* Check the parameters */
2281   assert_param(IS_RTC_BKP(BackupRegister));
2282 
2283   /* Point on address of first backup register */
2284 #if defined(TAMP)
2285   tmp = (uint32_t) & (((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->BKP0R);
2286 #else
2287   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
2288 #endif /* TAMP */
2289 
2290   tmp += (BackupRegister * 4U);
2291 
2292   /* Write the specified register */
2293   *(__IO uint32_t *)tmp = (uint32_t)Data;
2294 }
2295 
2296 
2297 /**
2298   * @brief  Read data from the specified RTC Backup data Register.
2299   * @param  hrtc RTC handle
2300   * @param  BackupRegister RTC Backup data Register number.
2301   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
2302   *                                 specify the register.
2303   * @retval Read value
2304   */
2305 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc, uint32_t BackupRegister)
2306 {
2307   uint32_t tmp;
2308 
2309   /* Check the parameters */
2310   assert_param(IS_RTC_BKP(BackupRegister));
2311 
2312   /* Point on address of first backup register */
2313 #if defined(TAMP)
2314   tmp = (uint32_t) & (((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->BKP0R);
2315 #else
2316   tmp = (uint32_t) & (hrtc->Instance->BKP0R);
2317 #endif /* TAMP */
2318 
2319   tmp += (BackupRegister * 4U);
2320 
2321   /* Read the specified register */
2322   return (*(__IO uint32_t *)tmp);
2323 }
2324 
2325 
2326 /**
2327   * @}
2328   */
2329 
2330 
2331 /** @addtogroup RTCEx_Exported_Functions_Group3
2332   * @brief    Extended Peripheral Control functions
2333   *
2334 @verbatim
2335  ===============================================================================
2336               ##### Extended Peripheral Control functions #####
2337  ===============================================================================
2338     [..]
2339     This subsection provides functions allowing to
2340       (+) Write a data in a specified RTC Backup data register
2341       (+) Read a data in a specified RTC Backup data register
2342       (+) Set the Smooth calibration parameters.
2343       (+) Set Low Power calibration parameter (if feature supported).
2344       (+) Configure the Synchronization Shift Control Settings.
2345       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2346       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2347       (+) Enable the RTC reference clock detection.
2348       (+) Disable the RTC reference clock detection.
2349       (+) Enable the Bypass Shadow feature.
2350       (+) Disable the Bypass Shadow feature.
2351 
2352 @endverbatim
2353   * @{
2354   */
2355 
2356 
2357 /**
2358   * @brief  Set the Smooth calibration parameters.
2359   * @param  hrtc RTC handle
2360   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
2361   *          This parameter can be can be one of the following values :
2362   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
2363   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
2364   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
2365   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
2366   *          This parameter can be one of the following values:
2367   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
2368   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
2369   * @param  SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
2370   *          This parameter can be one any value from 0 to 0x000001FF.
2371   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
2372   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
2373   *         SmoothCalibMinusPulsesValue must be equal to 0.
2374   * @retval HAL status
2375   */
2376 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
2377 {
2378   uint32_t tickstart;
2379 
2380   /* Check the parameters */
2381   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
2382   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
2383   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
2384 
2385   /* Process Locked */
2386   __HAL_LOCK(hrtc);
2387 
2388   hrtc->State = HAL_RTC_STATE_BUSY;
2389 
2390   /* Disable the write protection for RTC registers */
2391   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2392 
2393 #if defined(TAMP)
2394   /* check if a calibration operation is pending */
2395   if ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
2396   {
2397     tickstart = HAL_GetTick();
2398 
2399     /* Wait for pending calibration operation to finish */
2400     while ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
2401 #else
2402   /* check if a calibration operation is pending */
2403   if ((hrtc->Instance->ISR  & RTC_ISR_RECALPF)  != 0U)
2404   {
2405     tickstart = HAL_GetTick();
2406 
2407     /* Wait for pending calibration operation to finish */
2408     while ((hrtc->Instance->ISR  & RTC_ISR_RECALPF)  != 0U)
2409 #endif /* TAMP */
2410     {
2411       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2412       {
2413         /* Enable the write protection for RTC registers */
2414         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2415 
2416         /* Change RTC state */
2417         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2418 
2419         /* Process Unlocked */
2420         __HAL_UNLOCK(hrtc);
2421 
2422         return HAL_TIMEOUT;
2423       }
2424     }
2425   }
2426 
2427   /* Configure the Smooth calibration settings */
2428   MODIFY_REG(hrtc->Instance->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
2429 
2430   /* Enable the write protection for RTC registers */
2431   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2432 
2433   /* Change RTC state */
2434   hrtc->State = HAL_RTC_STATE_READY;
2435 
2436   /* Process Unlocked */
2437   __HAL_UNLOCK(hrtc);
2438 
2439   return HAL_OK;
2440 }
2441 
2442 /**
2443   * @brief  Configure the Synchronization Shift Control Settings.
2444   * @note   When REFCKON is set, firmware must not write to Shift control register.
2445   * @param  hrtc RTC handle
2446   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
2447   *          This parameter can be one of the following values:
2448   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
2449   *             @arg RTC_SHIFTADD1S_RESET: No effect.
2450   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
2451   *          This parameter can be one any value from 0 to 0x7FFF.
2452   * @retval HAL status
2453   */
2454 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
2455 {
2456   uint32_t tickstart;
2457 
2458   /* Check the parameters */
2459   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
2460   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
2461 
2462   /* Process Locked */
2463   __HAL_LOCK(hrtc);
2464 
2465   hrtc->State = HAL_RTC_STATE_BUSY;
2466 
2467   /* Disable the write protection for RTC registers */
2468   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2469 
2470   tickstart = HAL_GetTick();
2471 
2472   /* Wait until the shift is completed */
2473 #if defined(TAMP)
2474   while ((hrtc->Instance->ICSR & RTC_ICSR_SHPF) != 0U)
2475 #else
2476     while ((hrtc->Instance->ISR  & RTC_ISR_SHPF)  != 0U)
2477 #endif /* TAMP */
2478     {
2479       if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
2480       {
2481         /* Enable the write protection for RTC registers */
2482         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2483 
2484         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2485 
2486         /* Process Unlocked */
2487         __HAL_UNLOCK(hrtc);
2488 
2489         return HAL_TIMEOUT;
2490       }
2491     }
2492 
2493   /* Check if the reference clock detection is disabled */
2494   if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
2495   {
2496     /* Configure the Shift settings */
2497     hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
2498 
2499     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
2500     if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
2501     {
2502       if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
2503       {
2504         /* Enable the write protection for RTC registers */
2505         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2506 
2507         hrtc->State = HAL_RTC_STATE_ERROR;
2508 
2509         /* Process Unlocked */
2510         __HAL_UNLOCK(hrtc);
2511 
2512         return HAL_ERROR;
2513       }
2514     }
2515   }
2516   else
2517   {
2518     /* Enable the write protection for RTC registers */
2519     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2520 
2521     /* Change RTC state */
2522     hrtc->State = HAL_RTC_STATE_ERROR;
2523 
2524     /* Process Unlocked */
2525     __HAL_UNLOCK(hrtc);
2526 
2527     return HAL_ERROR;
2528   }
2529 
2530   /* Enable the write protection for RTC registers */
2531   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2532 
2533   /* Change RTC state */
2534   hrtc->State = HAL_RTC_STATE_READY;
2535 
2536   /* Process Unlocked */
2537   __HAL_UNLOCK(hrtc);
2538 
2539   return HAL_OK;
2540 }
2541 
2542 /**
2543   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2544   * @param  hrtc RTC handle
2545   * @param  CalibOutput Select the Calibration output Selection.
2546   *          This parameter can be one of the following values:
2547   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
2548   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
2549   * @retval HAL status
2550   */
2551 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc, uint32_t CalibOutput)
2552 {
2553   /* Check the parameters */
2554   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
2555 
2556   /* Process Locked */
2557   __HAL_LOCK(hrtc);
2558 
2559   hrtc->State = HAL_RTC_STATE_BUSY;
2560 
2561   /* Disable the write protection for RTC registers */
2562   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2563 
2564   /* Clear flags before config */
2565   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
2566 
2567   /* Configure the RTC_CR register */
2568   hrtc->Instance->CR |= (uint32_t)CalibOutput;
2569 
2570   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
2571 
2572   /* Enable the write protection for RTC registers */
2573   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2574 
2575   /* Change RTC state */
2576   hrtc->State = HAL_RTC_STATE_READY;
2577 
2578   /* Process Unlocked */
2579   __HAL_UNLOCK(hrtc);
2580 
2581   return HAL_OK;
2582 }
2583 
2584 /**
2585   * @brief  Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
2586   * @param  hrtc RTC handle
2587   * @retval HAL status
2588   */
2589 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)
2590 {
2591   /* Process Locked */
2592   __HAL_LOCK(hrtc);
2593 
2594   hrtc->State = HAL_RTC_STATE_BUSY;
2595 
2596   /* Disable the write protection for RTC registers */
2597   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2598 
2599   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
2600 
2601   /* Enable the write protection for RTC registers */
2602   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2603 
2604   /* Change RTC state */
2605   hrtc->State = HAL_RTC_STATE_READY;
2606 
2607   /* Process Unlocked */
2608   __HAL_UNLOCK(hrtc);
2609 
2610   return HAL_OK;
2611 }
2612 
2613 /**
2614   * @brief  Enable the RTC reference clock detection.
2615   * @param  hrtc RTC handle
2616   * @retval HAL status
2617   */
2618 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)
2619 {
2620   HAL_StatusTypeDef status;
2621   /* Process Locked */
2622   __HAL_LOCK(hrtc);
2623 
2624   hrtc->State = HAL_RTC_STATE_BUSY;
2625 
2626   /* Disable the write protection for RTC registers */
2627   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2628 
2629   /* Enter Initialization mode */
2630   status = RTC_EnterInitMode(hrtc);
2631   if (status == HAL_OK)
2632   {
2633     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
2634 
2635     /* Exit Initialization mode */
2636     status = RTC_ExitInitMode(hrtc);
2637   }
2638 
2639   /* Enable the write protection for RTC registers */
2640   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2641   if (status == HAL_OK)
2642   {
2643     /* Change RTC state */
2644     hrtc->State = HAL_RTC_STATE_READY;
2645   }
2646   /* Process Unlocked */
2647   __HAL_UNLOCK(hrtc);
2648 
2649   return HAL_OK;
2650 }
2651 
2652 /**
2653   * @brief  Disable the RTC reference clock detection.
2654   * @param  hrtc RTC handle
2655   * @retval HAL status
2656   */
2657 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)
2658 {
2659   HAL_StatusTypeDef status;
2660   /* Process Locked */
2661   __HAL_LOCK(hrtc);
2662 
2663   hrtc->State = HAL_RTC_STATE_BUSY;
2664 
2665   /* Disable the write protection for RTC registers */
2666   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2667 
2668   /* Enter Initialization mode */
2669   status = RTC_EnterInitMode(hrtc);
2670   if (status == HAL_OK)
2671   {
2672     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
2673 
2674     /* Exit Initialization mode */
2675     status = RTC_ExitInitMode(hrtc);
2676   }
2677 
2678   /* Enable the write protection for RTC registers */
2679   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2680 
2681   if (status == HAL_OK)
2682   {
2683     /* Change RTC state */
2684     hrtc->State = HAL_RTC_STATE_READY;
2685 
2686   }
2687 
2688   /* Process Unlocked */
2689   __HAL_UNLOCK(hrtc);
2690 
2691   return HAL_OK;
2692 }
2693 
2694 /**
2695   * @brief  Enable the Bypass Shadow feature.
2696   * @note   When the Bypass Shadow is enabled the calendar value are taken
2697   *         directly from the Calendar counter.
2698   * @param  hrtc RTC handle
2699   * @retval HAL status
2700   */
2701 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)
2702 {
2703   /* Process Locked */
2704   __HAL_LOCK(hrtc);
2705 
2706   hrtc->State = HAL_RTC_STATE_BUSY;
2707 
2708   /* Disable the write protection for RTC registers */
2709   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2710 
2711   /* Set the BYPSHAD bit */
2712   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
2713 
2714   /* Enable the write protection for RTC registers */
2715   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2716 
2717   /* Change RTC state */
2718   hrtc->State = HAL_RTC_STATE_READY;
2719 
2720   /* Process Unlocked */
2721   __HAL_UNLOCK(hrtc);
2722 
2723   return HAL_OK;
2724 }
2725 
2726 /**
2727   * @brief  Disable the Bypass Shadow feature.
2728   * @note   When the Bypass Shadow is enabled the calendar value are taken
2729   *         directly from the Calendar counter.
2730   * @param  hrtc RTC handle
2731   * @retval HAL status
2732   */
2733 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)
2734 {
2735   /* Process Locked */
2736   __HAL_LOCK(hrtc);
2737 
2738   hrtc->State = HAL_RTC_STATE_BUSY;
2739 
2740   /* Disable the write protection for RTC registers */
2741   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
2742 
2743   /* Reset the BYPSHAD bit */
2744   hrtc->Instance->CR &= ((uint8_t)~RTC_CR_BYPSHAD);
2745 
2746   /* Enable the write protection for RTC registers */
2747   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
2748 
2749   /* Change RTC state */
2750   hrtc->State = HAL_RTC_STATE_READY;
2751 
2752   /* Process Unlocked */
2753   __HAL_UNLOCK(hrtc);
2754 
2755   return HAL_OK;
2756 }
2757 
2758 #if defined(TAMP)
2759 /**
2760   * @brief  Increment Monotonic counter.
2761   * @param  hrtc RTC handle
2762   * @param  Instance  Monotonic counter Instance
2763   *         This parameter can be can be one of the following values :
2764   *           @arg RTC_MONOTONIC_COUNTER_1
2765   * @retval HAL status
2766   */
2767 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef *hrtc,  uint32_t Instance)
2768 {
2769   UNUSED(hrtc);
2770   UNUSED(Instance);
2771   /* This register is read-only only and is incremented by one when a write access is done to this
2772      register. This register cannot roll-over and is frozen when reaching the maximum value. */
2773   CLEAR_REG(TAMP->COUNTR);
2774 
2775   return HAL_OK;
2776 }
2777 
2778 /**
2779   * @brief  Monotonic counter incrementation.
2780   * @param  hrtc RTC handle
2781   * @param  Instance  Monotonic counter Instance
2782   *         This parameter can be can be one of the following values :
2783   *           @arg RTC_MONOTONIC_COUNTER_1
2784   * @param  Counter monotonic counter value
2785   * @retval HAL status
2786   */
2787 HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef *hrtc, uint32_t *Counter,  uint32_t Instance)
2788 {
2789   UNUSED(hrtc);
2790   UNUSED(Instance);
2791   /* This register is read-only only and is incremented by one when a write access is done to this
2792      register. This register cannot roll-over and is frozen when reaching the maximum value. */
2793   *Counter = READ_REG(TAMP->COUNTR);
2794 
2795   return HAL_OK;
2796 }
2797 #endif /* TAMP */
2798 
2799 /**
2800   * @}
2801   */
2802 
2803 /** @addtogroup RTCEx_Exported_Functions_Group4
2804   * @brief    Extended features functions
2805   *
2806 @verbatim
2807  ===============================================================================
2808                  ##### Extended features functions #####
2809  ===============================================================================
2810     [..]  This section provides functions allowing to:
2811       (+) RTC Alarm B callback
2812       (+) RTC Poll for Alarm B request
2813 
2814 @endverbatim
2815   * @{
2816   */
2817 
2818 /**
2819   * @brief  Alarm B callback.
2820   * @param  hrtc RTC handle
2821   * @retval None
2822   */
2823 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)
2824 {
2825   /* Prevent unused argument(s) compilation warning */
2826   UNUSED(hrtc);
2827 
2828   /* NOTE : This function should not be modified, when the callback is needed,
2829             the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
2830    */
2831 }
2832 
2833 /**
2834   * @brief  Handle Alarm B Polling request.
2835   * @param  hrtc RTC handle
2836   * @param  Timeout Timeout duration
2837   * @retval HAL status
2838   */
2839 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
2840 {
2841   uint32_t tickstart = HAL_GetTick();
2842 
2843   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
2844   {
2845     if (Timeout != HAL_MAX_DELAY)
2846     {
2847       if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2848       {
2849         hrtc->State = HAL_RTC_STATE_TIMEOUT;
2850         return HAL_TIMEOUT;
2851       }
2852     }
2853   }
2854 
2855   /* Clear the Alarm Flag */
2856   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
2857 
2858   /* Change RTC state */
2859   hrtc->State = HAL_RTC_STATE_READY;
2860 
2861   return HAL_OK;
2862 }
2863 
2864 /**
2865   * @}
2866   */
2867 
2868 
2869 /**
2870   * @}
2871   */
2872 
2873 #endif /* HAL_RTC_MODULE_ENABLED */
2874 
2875 /**
2876   * @}
2877   */
2878 
2879 /**
2880   * @}
2881   */
2882