Back to home page

LXR

 
 

    


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

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_rtc.c
0004   * @author  MCD Application Team
0005   * @brief   RTC HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Real-Time Clock (RTC) peripheral:
0008   *           + Initialization/de-initialization
0009   *           + Calendar (Time and Date) configuration
0010   *           + Alarms (Alarm A and Alarm B) configuration
0011   *           + WakeUp Timer configuration
0012   *           + TimeStamp configuration
0013   *           + Tampers configuration
0014   *           + Backup Data Registers configuration
0015   *           + RTC Tamper and TimeStamp Pins Selection
0016   *           + Interrupts and flags management
0017   *
0018   ******************************************************************************
0019   * @attention
0020   *
0021   * Copyright (c) 2017 STMicroelectronics.
0022   * All rights reserved.
0023   *
0024   * This software is licensed under terms that can be found in the LICENSE file
0025   * in the root directory of this software component.
0026   * If no LICENSE file comes with this software, it is provided AS-IS.
0027   *
0028   ******************************************************************************
0029   @verbatim
0030  ===============================================================================
0031                           ##### RTC Operating Condition #####
0032  ===============================================================================
0033   [..] The real-time clock (RTC) and the RTC backup registers can be powered
0034        from the VBAT voltage when the main VDD supply is powered off.
0035        To retain the content of the RTC backup registers and supply the RTC
0036        when VDD is turned off, VBAT pin can be connected to an optional
0037        standby voltage supplied by a battery or by another source.
0038 
0039                    ##### Backup Domain Reset #####
0040  ===============================================================================
0041   [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
0042        to their reset values.
0043        A backup domain reset is generated when one of the following events occurs:
0044     (#) Software reset, triggered by setting the BDRST bit in the
0045         RCC Backup domain control register (RCC_BDCR).
0046     (#) VDD or VBAT power on, if both supplies have previously been powered off.
0047     (#) Tamper detection event resets all data backup registers.
0048 
0049                    ##### Backup Domain Access #####
0050  ===================================================================
0051   [..] After reset, the backup domain (RTC registers, RTC backup data
0052        registers and backup SRAM) is protected against possible unwanted write
0053        accesses.
0054 
0055   [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
0056     (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
0057         PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
0058     (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
0059 
0060                   ##### How to use RTC Driver #####
0061  ===================================================================
0062   [..]
0063     (+) Enable the RTC domain access (see description in the section above).
0064     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
0065         format using the HAL_RTC_Init() function.
0066 
0067   *** Time and Date configuration ***
0068   ===================================
0069   [..]
0070     (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
0071         and HAL_RTC_SetDate() functions.
0072     (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
0073 
0074   *** Alarm configuration ***
0075   ===========================
0076   [..]
0077     (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
0078             You can also configure the RTC Alarm with interrupt mode using the
0079             HAL_RTC_SetAlarm_IT() function.
0080     (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
0081 
0082                   ##### RTC and low power modes #####
0083  ===================================================================
0084   [..] The MCU can be woken up from a low power mode by an RTC alternate
0085        function.
0086   [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
0087        RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
0088        These RTC alternate functions can wake up the system from the Stop and
0089        Standby low power modes.
0090   [..] The system can also wake up from low power modes without depending
0091        on an external interrupt (Auto-wakeup mode), by using the RTC alarm
0092        or the RTC wakeup events.
0093   [..] The RTC provides a programmable time base for waking up from the
0094        Stop or Standby mode at regular intervals.
0095        Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
0096        is LSE or LSI.
0097 
0098   *** Callback registration ***
0099   =============================================
0100   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
0101   not defined, the callback registration feature is not available and all callbacks
0102   are set to the corresponding weak functions. This is the recommended configuration
0103   in order to optimize memory/code consumption footprint/performances.
0104 
0105   The compilation define  USE_RTC_REGISTER_CALLBACKS when set to 1
0106   allows the user to configure dynamically the driver callbacks.
0107   Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
0108 
0109   Function HAL_RTC_RegisterCallback() allows to register following callbacks:
0110     (+) AlarmAEventCallback          : RTC Alarm A Event callback.
0111     (+) AlarmBEventCallback          : RTC Alarm B Event callback.
0112     (+) TimeStampEventCallback       : RTC TimeStamp Event callback.
0113     (+) WakeUpTimerEventCallback     : RTC WakeUpTimer Event callback.
0114     (+) Tamper1EventCallback         : RTC Tamper 1 Event callback.
0115     (+) Tamper2EventCallback         : RTC Tamper 2 Event callback.
0116     (+) Tamper3EventCallback         : RTC Tamper 3 Event callback.
0117     (+) MspInitCallback              : RTC MspInit callback.
0118     (+) MspDeInitCallback            : RTC MspDeInit callback.
0119   This function takes as parameters the HAL peripheral handle, the Callback ID
0120   and a pointer to the user callback function.
0121 
0122   Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
0123   weak function.
0124   HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
0125   and the Callback ID.
0126   This function allows to reset following callbacks:
0127     (+) AlarmAEventCallback          : RTC Alarm A Event callback.
0128     (+) AlarmBEventCallback          : RTC Alarm B Event callback.
0129     (+) TimeStampEventCallback       : RTC TimeStamp Event callback.
0130     (+) WakeUpTimerEventCallback     : RTC WakeUpTimer Event callback.
0131     (+) Tamper1EventCallback         : RTC Tamper 1 Event callback.
0132     (+) Tamper2EventCallback         : RTC Tamper 2 Event callback.
0133     (+) Tamper3EventCallback         : RTC Tamper 3 Event callback.
0134     (+) MspInitCallback              : RTC MspInit callback.
0135     (+) MspDeInitCallback            : RTC MspDeInit callback.
0136 
0137   By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
0138   all callbacks are set to the corresponding weak functions :
0139   examples AlarmAEventCallback(), WakeUpTimerEventCallback().
0140   Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
0141   in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
0142   (not registered beforehand).
0143   If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
0144   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
0145 
0146   Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
0147   Exception done MspInit/MspDeInit that can be registered/unregistered
0148   in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
0149   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
0150   In that case first register the MspInit/MspDeInit user callbacks
0151   using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
0152   or HAL_RTC_Init() function.
0153 
0154   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
0155   not defined, the callback registration feature is not available and all callbacks
0156   are set to the corresponding weak functions.
0157    @endverbatim
0158 
0159   */
0160 
0161 /* Includes ------------------------------------------------------------------*/
0162 #include "stm32h7xx_hal.h"
0163 
0164 /** @addtogroup STM32H7xx_HAL_Driver
0165   * @{
0166   */
0167 
0168 
0169 /** @addtogroup RTC
0170   * @brief RTC HAL module driver
0171   * @{
0172   */
0173 
0174 #ifdef HAL_RTC_MODULE_ENABLED
0175 
0176 /* Private typedef -----------------------------------------------------------*/
0177 /* Private define ------------------------------------------------------------*/
0178 /* Private macro -------------------------------------------------------------*/
0179 /* Private variables ---------------------------------------------------------*/
0180 /* Private function prototypes -----------------------------------------------*/
0181 /* Exported functions --------------------------------------------------------*/
0182 
0183 /** @addtogroup RTC_Exported_Functions
0184   * @{
0185   */
0186 
0187 /** @addtogroup RTC_Exported_Functions_Group1
0188  *  @brief    Initialization and Configuration functions
0189  *
0190 @verbatim
0191  ===============================================================================
0192               ##### Initialization and de-initialization functions #####
0193  ===============================================================================
0194    [..] This section provides functions allowing to initialize and configure the
0195          RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
0196          RTC registers Write protection, enter and exit the RTC initialization mode,
0197          RTC registers synchronization check and reference clock detection enable.
0198          (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
0199              It is split into 2 programmable prescalers to minimize power consumption.
0200              (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
0201              (++) When both prescalers are used, it is recommended to configure the
0202                  asynchronous prescaler to a high value to minimize power consumption.
0203          (#) All RTC registers are Write protected. Writing to the RTC registers
0204              is enabled by writing a key into the Write Protection register, RTC_WPR.
0205          (#) To configure the RTC Calendar, user application should enter
0206              initialization mode. In this mode, the calendar counter is stopped
0207              and its value can be updated. When the initialization sequence is
0208              complete, the calendar restarts counting after 4 RTCCLK cycles.
0209          (#) To read the calendar through the shadow registers after Calendar
0210              initialization, calendar update or after wakeup from low power modes
0211              the software must first clear the RSF flag. The software must then
0212              wait until it is set again before reading the calendar, which means
0213              that the calendar registers have been correctly copied into the
0214              RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
0215              implements the above software sequence (RSF clear and RSF check).
0216 
0217 @endverbatim
0218   * @{
0219   */
0220 
0221 /**
0222   * @brief  Initialize the RTC peripheral
0223   * @param  hrtc RTC handle
0224   * @retval HAL status
0225   */
0226 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
0227 {
0228   HAL_StatusTypeDef status = HAL_ERROR;
0229 
0230   /* Check RTC handler */
0231   if(hrtc != NULL)
0232   {
0233     /* Check the parameters */
0234     assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
0235     assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
0236     assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
0237     assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
0238     assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
0239     assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
0240     assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
0241     assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
0242 #if defined(TAMP)
0243     assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
0244 #endif /* TAMP */
0245 
0246 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
0247     if(hrtc->State == HAL_RTC_STATE_RESET)
0248     {
0249       /* Allocate lock resource and initialize it */
0250       hrtc->Lock = HAL_UNLOCKED;
0251 
0252       hrtc->AlarmAEventCallback          =  HAL_RTC_AlarmAEventCallback;             /* Legacy weak AlarmAEventCallback      */
0253       hrtc->AlarmBEventCallback          =  HAL_RTCEx_AlarmBEventCallback;           /* Legacy weak AlarmBEventCallback      */
0254       hrtc->TimeStampEventCallback       =  HAL_RTCEx_TimeStampEventCallback;        /* Legacy weak TimeStampEventCallback   */
0255       hrtc->WakeUpTimerEventCallback     =  HAL_RTCEx_WakeUpTimerEventCallback;      /* Legacy weak WakeUpTimerEventCallback */
0256       hrtc->Tamper1EventCallback         =  HAL_RTCEx_Tamper1EventCallback;          /* Legacy weak Tamper1EventCallback     */
0257       hrtc->Tamper2EventCallback         =  HAL_RTCEx_Tamper2EventCallback;          /* Legacy weak Tamper2EventCallback     */
0258       hrtc->Tamper3EventCallback         =  HAL_RTCEx_Tamper3EventCallback;          /* Legacy weak Tamper3EventCallback     */
0259 
0260 #if defined(TAMP)
0261       hrtc->InternalTamper1EventCallback =  HAL_RTCEx_InternalTamper1EventCallback;
0262       hrtc->InternalTamper2EventCallback =  HAL_RTCEx_InternalTamper2EventCallback;
0263       hrtc->InternalTamper3EventCallback =  HAL_RTCEx_InternalTamper3EventCallback;
0264       hrtc->InternalTamper4EventCallback =  HAL_RTCEx_InternalTamper4EventCallback;
0265       hrtc->InternalTamper5EventCallback =  HAL_RTCEx_InternalTamper5EventCallback;
0266       hrtc->InternalTamper6EventCallback =  HAL_RTCEx_InternalTamper6EventCallback;
0267       hrtc->InternalTamper8EventCallback =  HAL_RTCEx_InternalTamper8EventCallback;
0268 #endif /* TAMP */
0269 
0270 
0271       if(hrtc->MspInitCallback == NULL)
0272       {
0273         hrtc->MspInitCallback = HAL_RTC_MspInit;
0274       }
0275       /* Init the low level hardware */
0276       hrtc->MspInitCallback(hrtc);
0277 
0278       if(hrtc->MspDeInitCallback == NULL)
0279       {
0280         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
0281       }
0282     }
0283 #else /*  (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
0284     if(hrtc->State == HAL_RTC_STATE_RESET)
0285     {
0286       /* Allocate lock resource and initialize it */
0287       hrtc->Lock = HAL_UNLOCKED;
0288 
0289       /* Initialize RTC MSP */
0290       HAL_RTC_MspInit(hrtc);
0291     }
0292 #endif /*  (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
0293 
0294     /* Set RTC state */
0295     hrtc->State = HAL_RTC_STATE_BUSY;
0296 
0297     /* Check whether the calendar needs to be initialized */
0298     if (__HAL_RTC_IS_CALENDAR_INITIALIZED(hrtc) == 0U)
0299     {
0300       /* Disable the write protection for RTC registers */
0301       __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0302 
0303      /* Enter Initialization mode */
0304       status = RTC_EnterInitMode(hrtc);
0305       if (status == HAL_OK)
0306       {
0307 #if defined(TAMP)
0308         /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */
0309         hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
0310 #else
0311         /* Clear RTC_CR FMT, OSEL and POL Bits */
0312         hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
0313 #endif /* TAMP */
0314 
0315         /* Set RTC_CR register */
0316         hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
0317 
0318         /* Configure the RTC PRER */
0319         hrtc->Instance->PRER = (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos) | (hrtc->Init.SynchPrediv << RTC_PRER_PREDIV_S_Pos);
0320 
0321         /* Exit Initialization mode */
0322         status = RTC_ExitInitMode(hrtc);
0323       }
0324       if(status == HAL_OK)
0325       {
0326 #if defined(TAMP)
0327         hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
0328         hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
0329 #else
0330         hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
0331         hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
0332 #endif /* TAMP */
0333       }
0334 
0335     /* Enable the write protection for RTC registers */
0336     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0337     }
0338     else
0339     {
0340       /* The calendar is already initialized */
0341       status = HAL_OK;
0342     }
0343 
0344     if (status == HAL_OK)
0345     {
0346       /* Set RTC state */
0347       hrtc->State = HAL_RTC_STATE_READY;
0348     }
0349   }
0350 
0351   /* return status */
0352   return status;
0353 }
0354 
0355 /**
0356   * @brief  DeInitialize the RTC peripheral.
0357   * @note   This function doesn't reset the RTC Backup Data registers.
0358   * @param  hrtc RTC handle
0359   * @retval HAL status
0360   */
0361 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
0362 {
0363   HAL_StatusTypeDef status = HAL_ERROR;
0364   uint32_t          tickstart;
0365 
0366   /* Check RTC handler */
0367   if(hrtc != NULL)
0368   {
0369     /* Check the parameters */
0370     assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
0371 
0372     /* Set RTC state */
0373     hrtc->State = HAL_RTC_STATE_BUSY;
0374 
0375     /* Disable the write protection for RTC registers */
0376     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0377 
0378     /* Enter Initialization mode */
0379     status = RTC_EnterInitMode(hrtc);
0380 
0381     if (status == HAL_OK)
0382     {
0383       /* Reset TR, DR and CR registers */
0384       hrtc->Instance->TR = 0x00000000U;
0385       hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
0386 
0387       /* Reset All CR bits except CR[2:0] (which cannot be written before bit
0388            WUTE of CR is cleared) */
0389       hrtc->Instance->CR = 0x00000000U;
0390 
0391       /* Wait till WUTWF is set (to be able to reset CR[2:0] and WUTR) and if
0392          timeout is reached exit */
0393       tickstart = HAL_GetTick();
0394 
0395 #if defined(TAMP)
0396       while ((((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U) && (status != HAL_TIMEOUT))
0397 #else
0398       while ((((hrtc->Instance->ISR)  & RTC_ISR_WUTWF)  == 0U) && (status != HAL_TIMEOUT))
0399 #endif /* TAMP */
0400       {
0401         if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
0402         {
0403           /* Enable the write protection for RTC registers */
0404           __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0405 
0406           /* Set RTC state */
0407           hrtc->State = HAL_RTC_STATE_TIMEOUT;
0408           status = HAL_TIMEOUT;
0409 
0410         }
0411       }
0412     }
0413 
0414     if (status == HAL_OK)
0415     {
0416       /* Reset RTC CR register bits [2:0] */
0417       hrtc->Instance->CR = 0x00000000U;
0418 
0419       /* Reset other RTC registers */
0420       hrtc->Instance->WUTR     = RTC_WUTR_WUT;
0421       hrtc->Instance->PRER     = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
0422       hrtc->Instance->ALRMAR   = 0x00000000U;
0423       hrtc->Instance->ALRMBR   = 0x00000000U;
0424       hrtc->Instance->SHIFTR   = 0x00000000U;
0425       hrtc->Instance->CALR     = 0x00000000U;
0426       hrtc->Instance->ALRMASSR = 0x00000000U;
0427       hrtc->Instance->ALRMBSSR = 0x00000000U;
0428 
0429       /* Exit initialization mode */
0430       status = RTC_ExitInitMode(hrtc);
0431     }
0432 
0433     if(status == HAL_OK)
0434     {
0435 #if defined(TAMP)
0436       /* Reset TAMP registers */
0437       ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR1 = 0xFFFF0000U;
0438       ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR2 = 0x00000000U;
0439 #else
0440       /* Reset Tamper configuration register */
0441       hrtc->Instance->TAMPCR = 0x00000000U;
0442 
0443       /* Reset Option register */
0444       hrtc->Instance->OR = 0x00000000U;
0445 #endif /* TAMP */
0446 
0447       /* Enable the write protection for RTC registers */
0448       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0449 
0450 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
0451       if(hrtc->MspDeInitCallback == NULL)
0452       {
0453         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
0454       }
0455 
0456       /* DeInit the low level hardware: CLOCK, NVIC.*/
0457       hrtc->MspDeInitCallback(hrtc);
0458 #else
0459       /* De-Initialize RTC MSP */
0460       HAL_RTC_MspDeInit(hrtc);
0461 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
0462 
0463       hrtc->State = HAL_RTC_STATE_RESET;
0464 
0465       /* Release Lock */
0466       __HAL_UNLOCK(hrtc);
0467     }
0468   }
0469 
0470   /* return status */
0471   return status;
0472 }
0473 
0474 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
0475 /**
0476   * @brief  Register a User RTC Callback
0477   *         To be used instead of the weak predefined callback
0478   * @param  hrtc RTC handle
0479   * @param  CallbackID ID of the callback to be registered
0480   *         This parameter can be one of the following values:
0481   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
0482   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID          Alarm B Event Callback ID
0483   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID        TimeStamp Event Callback ID
0484   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID      WakeUp Timer Event Callback ID
0485   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 Callback ID
0486   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID          Tamper 2 Callback ID
0487   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID          Tamper 3 Callback ID
0488   *          @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
0489   *          @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
0490   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
0491   *          @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
0492   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
0493   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
0494   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
0495   *          @arg @ref HAL_RTC_MSPINIT_CB_ID                Msp Init callback ID
0496   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID              Msp DeInit callback ID
0497   * @param  pCallback pointer to the Callback function
0498   * @retval HAL status
0499   */
0500 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
0501 {
0502   HAL_StatusTypeDef status = HAL_OK;
0503 
0504   if(pCallback == NULL)
0505   {
0506     return HAL_ERROR;
0507   }
0508 
0509   /* Process locked */
0510   __HAL_LOCK(hrtc);
0511 
0512   if(HAL_RTC_STATE_READY == hrtc->State)
0513   {
0514     switch (CallbackID)
0515     {
0516       case HAL_RTC_ALARM_A_EVENT_CB_ID :
0517         hrtc->AlarmAEventCallback = pCallback;
0518         break;
0519 
0520       case HAL_RTC_ALARM_B_EVENT_CB_ID :
0521         hrtc->AlarmBEventCallback = pCallback;
0522         break;
0523 
0524       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
0525         hrtc->TimeStampEventCallback = pCallback;
0526         break;
0527 
0528       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
0529         hrtc->WakeUpTimerEventCallback = pCallback;
0530         break;
0531 
0532       case HAL_RTC_TAMPER1_EVENT_CB_ID :
0533         hrtc->Tamper1EventCallback = pCallback;
0534         break;
0535 
0536       case HAL_RTC_TAMPER2_EVENT_CB_ID :
0537         hrtc->Tamper2EventCallback = pCallback;
0538         break;
0539 
0540       case HAL_RTC_TAMPER3_EVENT_CB_ID :
0541         hrtc->Tamper3EventCallback = pCallback;
0542         break;
0543 
0544 #if defined(TAMP)
0545       case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
0546         hrtc->InternalTamper1EventCallback =  pCallback;
0547         break;
0548 
0549       case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
0550         hrtc->InternalTamper2EventCallback =  pCallback;
0551         break;
0552 
0553       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
0554         hrtc->InternalTamper3EventCallback =  pCallback;
0555         break;
0556 
0557       case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
0558         hrtc->InternalTamper4EventCallback =  pCallback;
0559         break;
0560 
0561       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
0562         hrtc->InternalTamper5EventCallback =  pCallback;
0563         break;
0564 
0565       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
0566         hrtc->InternalTamper6EventCallback =  pCallback;
0567         break;
0568 
0569       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
0570         hrtc->InternalTamper8EventCallback =  pCallback;
0571         break;
0572 #endif /* TAMP */
0573 
0574       case HAL_RTC_MSPINIT_CB_ID :
0575         hrtc->MspInitCallback = pCallback;
0576         break;
0577 
0578       case HAL_RTC_MSPDEINIT_CB_ID :
0579         hrtc->MspDeInitCallback = pCallback;
0580         break;
0581 
0582       default :
0583         /* Return error status */
0584         status =  HAL_ERROR;
0585         break;
0586     }
0587   }
0588   else if(HAL_RTC_STATE_RESET == hrtc->State)
0589   {
0590     switch (CallbackID)
0591     {
0592       case HAL_RTC_MSPINIT_CB_ID :
0593         hrtc->MspInitCallback = pCallback;
0594         break;
0595 
0596       case HAL_RTC_MSPDEINIT_CB_ID :
0597         hrtc->MspDeInitCallback = pCallback;
0598         break;
0599 
0600       default :
0601         /* Return error status */
0602         status =  HAL_ERROR;
0603         break;
0604     }
0605   }
0606   else
0607   {
0608     /* Return error status */
0609     status =  HAL_ERROR;
0610   }
0611 
0612   /* Release Lock */
0613   __HAL_UNLOCK(hrtc);
0614 
0615   return status;
0616 }
0617 
0618 /**
0619   * @brief  Unregister an RTC Callback
0620   *         RTC callback is redirected to the weak predefined callback
0621   * @param  hrtc RTC handle
0622   * @param  CallbackID ID of the callback to be unregistered
0623   *         This parameter can be one of the following values:
0624   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
0625   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID          Alarm B Event Callback ID
0626   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID        TimeStamp Event Callback ID
0627   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID      WakeUp Timer Event Callback ID
0628   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 Callback ID
0629   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID          Tamper 2 Callback ID
0630   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID          Tamper 3 Callback ID
0631   *          @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
0632   *          @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
0633   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
0634   *          @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
0635   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
0636   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
0637   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
0638   *          @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
0639   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
0640   * @retval HAL status
0641   */
0642 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
0643 {
0644   HAL_StatusTypeDef status = HAL_OK;
0645 
0646   /* Process locked */
0647   __HAL_LOCK(hrtc);
0648 
0649   if(HAL_RTC_STATE_READY == hrtc->State)
0650   {
0651     switch (CallbackID)
0652     {
0653       case HAL_RTC_ALARM_A_EVENT_CB_ID :
0654         hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback;             /* Legacy weak AlarmAEventCallback    */
0655         break;
0656 
0657       case HAL_RTC_ALARM_B_EVENT_CB_ID :
0658         hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback;           /* Legacy weak AlarmBEventCallback */
0659         break;
0660 
0661       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
0662         hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback;     /* Legacy weak TimeStampEventCallback    */
0663         break;
0664 
0665       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
0666         hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
0667         break;
0668 
0669       case HAL_RTC_TAMPER1_EVENT_CB_ID :
0670         hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback;         /* Legacy weak Tamper1EventCallback   */
0671         break;
0672 
0673       case HAL_RTC_TAMPER2_EVENT_CB_ID :
0674         hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback;         /* Legacy weak Tamper2EventCallback         */
0675         break;
0676 
0677       case HAL_RTC_TAMPER3_EVENT_CB_ID :
0678         hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback;         /* Legacy weak Tamper3EventCallback         */
0679         break;
0680 
0681 #if defined(TAMP)
0682       case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
0683         hrtc->InternalTamper1EventCallback =  HAL_RTCEx_InternalTamper1EventCallback;
0684         break;
0685 
0686       case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
0687         hrtc->InternalTamper2EventCallback =  HAL_RTCEx_InternalTamper2EventCallback;
0688         break;
0689 
0690       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
0691         hrtc->InternalTamper3EventCallback =  HAL_RTCEx_InternalTamper3EventCallback;
0692         break;
0693 
0694       case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
0695         hrtc->InternalTamper4EventCallback =  HAL_RTCEx_InternalTamper4EventCallback;
0696         break;
0697 
0698       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
0699         hrtc->InternalTamper5EventCallback =  HAL_RTCEx_InternalTamper5EventCallback;
0700         break;
0701 
0702       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
0703         hrtc->InternalTamper6EventCallback =  HAL_RTCEx_InternalTamper6EventCallback;
0704         break;
0705 
0706       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
0707         hrtc->InternalTamper8EventCallback =  HAL_RTCEx_InternalTamper8EventCallback;
0708         break;
0709 #endif /* TAMP */
0710 
0711       case HAL_RTC_MSPINIT_CB_ID :
0712         hrtc->MspInitCallback = HAL_RTC_MspInit;
0713         break;
0714 
0715       case HAL_RTC_MSPDEINIT_CB_ID :
0716         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
0717         break;
0718 
0719       default :
0720         /* Return error status */
0721         status =  HAL_ERROR;
0722         break;
0723     }
0724   }
0725   else if(HAL_RTC_STATE_RESET == hrtc->State)
0726   {
0727     switch (CallbackID)
0728     {
0729       case HAL_RTC_MSPINIT_CB_ID :
0730         hrtc->MspInitCallback = HAL_RTC_MspInit;
0731         break;
0732 
0733       case HAL_RTC_MSPDEINIT_CB_ID :
0734         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
0735         break;
0736 
0737       default :
0738         /* Return error status */
0739         status =  HAL_ERROR;
0740         break;
0741     }
0742   }
0743   else
0744   {
0745     /* Return error status */
0746     status =  HAL_ERROR;
0747   }
0748 
0749   /* Release Lock */
0750   __HAL_UNLOCK(hrtc);
0751 
0752   return status;
0753 }
0754 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
0755 
0756 /**
0757   * @brief  Initialize the RTC MSP.
0758   * @param  hrtc RTC handle
0759   * @retval None
0760   */
0761 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
0762 {
0763   /* Prevent unused argument(s) compilation warning */
0764   UNUSED(hrtc);
0765 
0766   /* NOTE : This function should not be modified, when the callback is needed,
0767             the HAL_RTC_MspInit could be implemented in the user file
0768    */
0769 }
0770 
0771 /**
0772   * @brief  DeInitialize the RTC MSP.
0773   * @param  hrtc RTC handle
0774   * @retval None
0775   */
0776 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
0777 {
0778   /* Prevent unused argument(s) compilation warning */
0779   UNUSED(hrtc);
0780 
0781   /* NOTE : This function should not be modified, when the callback is needed,
0782             the HAL_RTC_MspDeInit could be implemented in the user file
0783    */
0784 }
0785 
0786 /**
0787   * @}
0788   */
0789 
0790 /** @addtogroup RTC_Exported_Functions_Group2
0791  *  @brief   RTC Time and Date functions
0792  *
0793 @verbatim
0794  ===============================================================================
0795                  ##### RTC Time and Date functions #####
0796  ===============================================================================
0797 
0798  [..] This section provides functions allowing to configure Time and Date features
0799 
0800 @endverbatim
0801   * @{
0802   */
0803 
0804 /**
0805   * @brief  Set RTC current time.
0806   * @param  hrtc RTC handle
0807   * @param  sTime Pointer to Time structure
0808   * @param  Format Specifies the format of the entered parameters.
0809   *          This parameter can be one of the following values:
0810   *            @arg RTC_FORMAT_BIN: Binary data format
0811   *            @arg RTC_FORMAT_BCD: BCD data format
0812   * @retval HAL status
0813   */
0814 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
0815 {
0816   uint32_t tmpreg;
0817 HAL_StatusTypeDef status;
0818 
0819   /* Check the parameters */
0820   assert_param(IS_RTC_FORMAT(Format));
0821   assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
0822   assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
0823 
0824   /* Process Locked */
0825   __HAL_LOCK(hrtc);
0826 
0827   hrtc->State = HAL_RTC_STATE_BUSY;
0828 
0829   /* Disable the write protection for RTC registers */
0830   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0831  /* Enter Initialization mode */
0832   status = RTC_EnterInitMode(hrtc);
0833   if (status == HAL_OK)
0834   {
0835     if(Format == RTC_FORMAT_BIN)
0836     {
0837       if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
0838       {
0839         assert_param(IS_RTC_HOUR12(sTime->Hours));
0840         assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
0841       }
0842       else
0843       {
0844         sTime->TimeFormat = 0x00U;
0845         assert_param(IS_RTC_HOUR24(sTime->Hours));
0846       }
0847       assert_param(IS_RTC_MINUTES(sTime->Minutes));
0848       assert_param(IS_RTC_SECONDS(sTime->Seconds));
0849 
0850       tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours)   << RTC_TR_HU_Pos)  | \
0851                           ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
0852                           ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos)  | \
0853                           (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
0854     }
0855     else
0856     {
0857       if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
0858       {
0859         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
0860         assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
0861       }
0862       else
0863       {
0864         sTime->TimeFormat = 0x00U;
0865         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
0866       }
0867       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
0868       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
0869       tmpreg = (((uint32_t)(sTime->Hours)   << RTC_TR_HU_Pos)  | \
0870                 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
0871                 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos)  | \
0872                 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
0873     }
0874 
0875     /* Set the RTC_TR register */
0876     hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
0877 
0878     /* Clear the bits to be configured */
0879     hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
0880 
0881     /* Configure the RTC_CR register */
0882     hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
0883 
0884     /* Exit Initialization mode */
0885     status = RTC_ExitInitMode(hrtc);
0886   }
0887 
0888   /* Enable the write protection for RTC registers */
0889   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0890 
0891   if (status == HAL_OK)
0892   {
0893     hrtc->State = HAL_RTC_STATE_READY;
0894   }
0895 
0896   /* Process Unlocked */
0897   __HAL_UNLOCK(hrtc);
0898 
0899   return status;
0900 }
0901 
0902 /**
0903   * @brief  Get RTC current time.
0904   * @param  hrtc RTC handle
0905   * @param  sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
0906   *                with input format (BIN or BCD), also SubSeconds field returning the
0907   *                RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
0908   *                factor to be used for second fraction ratio computation.
0909   * @param  Format Specifies the format of the entered parameters.
0910   *          This parameter can be one of the following values:
0911   *            @arg RTC_FORMAT_BIN: Binary data format
0912   *            @arg RTC_FORMAT_BCD: BCD data format
0913   * @note  You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
0914   *        value in second fraction ratio with time unit following generic formula:
0915   *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
0916   *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
0917   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
0918   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
0919   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read
0920   *        to ensure consistency between the time and date values.
0921   * @retval HAL status
0922   */
0923 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
0924 {
0925   uint32_t tmpreg;
0926 
0927   /* Check the parameters */
0928   assert_param(IS_RTC_FORMAT(Format));
0929 
0930   /* Get subseconds structure field from the corresponding register*/
0931   sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
0932 
0933   /* Get SecondFraction structure field from the corresponding register field*/
0934   sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
0935 
0936   /* Get the TR register */
0937   tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
0938 
0939   /* Fill the structure fields with the read parameters */
0940   sTime->Hours      = (uint8_t)((tmpreg & (RTC_TR_HT  | RTC_TR_HU))  >> RTC_TR_HU_Pos);
0941   sTime->Minutes    = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
0942   sTime->Seconds    = (uint8_t)((tmpreg & (RTC_TR_ST  | RTC_TR_SU))  >> RTC_TR_SU_Pos);
0943   sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM))               >> RTC_TR_PM_Pos);
0944 
0945   /* Check the input parameters format */
0946   if(Format == RTC_FORMAT_BIN)
0947   {
0948     /* Convert the time structure parameters to Binary format */
0949     sTime->Hours   = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
0950     sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
0951     sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
0952   }
0953 
0954   return HAL_OK;
0955 }
0956 
0957 /**
0958   * @brief  Set RTC current date.
0959   * @param  hrtc RTC handle
0960   * @param  sDate Pointer to date structure
0961   * @param  Format specifies the format of the entered parameters.
0962   *          This parameter can be one of the following values:
0963   *            @arg RTC_FORMAT_BIN: Binary data format
0964   *            @arg RTC_FORMAT_BCD: BCD data format
0965   * @retval HAL status
0966   */
0967 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
0968 {
0969   uint32_t datetmpreg;
0970   HAL_StatusTypeDef status;
0971 
0972   /* Check the parameters */
0973   assert_param(IS_RTC_FORMAT(Format));
0974 
0975   /* Process Locked */
0976   __HAL_LOCK(hrtc);
0977 
0978   hrtc->State = HAL_RTC_STATE_BUSY;
0979 
0980   if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
0981   {
0982     sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
0983   }
0984 
0985   assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
0986 
0987   if(Format == RTC_FORMAT_BIN)
0988   {
0989     assert_param(IS_RTC_YEAR(sDate->Year));
0990     assert_param(IS_RTC_MONTH(sDate->Month));
0991     assert_param(IS_RTC_DATE(sDate->Date));
0992 
0993     datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year)  << RTC_DR_YU_Pos) | \
0994                   ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
0995                   ((uint32_t)RTC_ByteToBcd2(sDate->Date)  << RTC_DR_DU_Pos) | \
0996                   ((uint32_t)sDate->WeekDay               << RTC_DR_WDU_Pos));
0997   }
0998   else
0999   {
1000     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
1001     assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
1002     assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
1003 
1004     datetmpreg = ((((uint32_t)sDate->Year)    << RTC_DR_YU_Pos) | \
1005                   (((uint32_t)sDate->Month)   << RTC_DR_MU_Pos) | \
1006                   (((uint32_t)sDate->Date)    << RTC_DR_DU_Pos) | \
1007                   (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
1008   }
1009 
1010   /* Disable the write protection for RTC registers */
1011   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1012 
1013 
1014   /* Enter Initialization mode */
1015   status = RTC_EnterInitMode(hrtc);
1016   if (status == HAL_OK)
1017   {
1018     /* Set the RTC_DR register */
1019     hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
1020 
1021 
1022     /* Exit Initialization mode */
1023     status = RTC_ExitInitMode(hrtc);
1024   }
1025 
1026   /* Enable the write protection for RTC registers */
1027   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1028 
1029   if (status == HAL_OK)
1030   {
1031     hrtc->State = HAL_RTC_STATE_READY;
1032   }
1033 
1034   /* Process Unlocked */
1035   __HAL_UNLOCK(hrtc);
1036 
1037   return status;
1038 
1039 
1040 }
1041 
1042 /**
1043   * @brief  Get RTC current date.
1044   * @param  hrtc RTC handle
1045   * @param  sDate Pointer to Date structure
1046   * @param  Format Specifies the format of the entered parameters.
1047   *          This parameter can be one of the following values:
1048   *            @arg RTC_FORMAT_BIN:  Binary data format
1049   *            @arg RTC_FORMAT_BCD:  BCD data format
1050   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
1051   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
1052   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read.
1053   * @retval HAL status
1054   */
1055 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1056 {
1057   uint32_t datetmpreg;
1058 
1059   /* Check the parameters */
1060   assert_param(IS_RTC_FORMAT(Format));
1061 
1062   /* Get the DR register */
1063   datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
1064 
1065   /* Fill the structure fields with the read parameters */
1066   sDate->Year    = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
1067   sDate->Month   = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
1068   sDate->Date    = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
1069   sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU))            >> RTC_DR_WDU_Pos);
1070 
1071   /* Check the input parameters format */
1072   if(Format == RTC_FORMAT_BIN)
1073   {
1074     /* Convert the date structure parameters to Binary format */
1075     sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
1076     sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
1077     sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
1078   }
1079   return HAL_OK;
1080 }
1081 
1082 /**
1083   * @}
1084   */
1085 
1086 /** @addtogroup RTC_Exported_Functions_Group3
1087  *  @brief   RTC Alarm functions
1088  *
1089 @verbatim
1090  ===============================================================================
1091                  ##### RTC Alarm functions #####
1092  ===============================================================================
1093 
1094  [..] This section provides functions allowing to configure Alarm feature
1095 
1096 @endverbatim
1097   * @{
1098   */
1099 /**
1100   * @brief  Set the specified RTC Alarm.
1101   * @param  hrtc RTC handle
1102   * @param  sAlarm Pointer to Alarm structure
1103   * @param  Format Specifies the format of the entered parameters.
1104   *          This parameter can be one of the following values:
1105   *             @arg RTC_FORMAT_BIN: Binary data format
1106   *             @arg RTC_FORMAT_BCD: BCD data format
1107   * @retval HAL status
1108   */
1109 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1110 {
1111   uint32_t tickstart;
1112   uint32_t tmpreg;
1113   uint32_t subsecondtmpreg;
1114 
1115   /* Check the parameters */
1116   assert_param(IS_RTC_FORMAT(Format));
1117   assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1118   assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1119   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1120   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1121   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1122 
1123   /* Process Locked */
1124   __HAL_LOCK(hrtc);
1125 
1126   hrtc->State = HAL_RTC_STATE_BUSY;
1127 
1128   if(Format == RTC_FORMAT_BIN)
1129   {
1130     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1131     {
1132       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1133       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1134     }
1135     else
1136     {
1137       sAlarm->AlarmTime.TimeFormat = 0x00U;
1138       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1139     }
1140     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1141     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1142 
1143     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1144     {
1145       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1146     }
1147     else
1148     {
1149       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1150     }
1151 
1152     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours)   << RTC_ALRMAR_HU_Pos)  | \
1153               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1154               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos)  | \
1155               ((uint32_t)sAlarm->AlarmTime.TimeFormat              << RTC_ALRMAR_PM_Pos)  | \
1156               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay)  << RTC_ALRMAR_DU_Pos)  | \
1157               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                                     | \
1158               ((uint32_t)sAlarm->AlarmMask));
1159   }
1160   else
1161   {
1162     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1163     {
1164       assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1165       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1166     }
1167     else
1168     {
1169       sAlarm->AlarmTime.TimeFormat = 0x00U;
1170       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1171     }
1172 
1173     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1174     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1175 
1176     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1177     {
1178       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1179     }
1180     else
1181     {
1182       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1183     }
1184 
1185     tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours       << RTC_ALRMAR_HU_Pos)  | \
1186               ((uint32_t)sAlarm->AlarmTime.Minutes     << RTC_ALRMAR_MNU_Pos) | \
1187               ((uint32_t)sAlarm->AlarmTime.Seconds     << RTC_ALRMAR_SU_Pos)  | \
1188               ((uint32_t)sAlarm->AlarmTime.TimeFormat  << RTC_ALRMAR_PM_Pos)  | \
1189               ((uint32_t)sAlarm->AlarmDateWeekDay      << RTC_ALRMAR_DU_Pos)  | \
1190               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                         | \
1191               ((uint32_t)sAlarm->AlarmMask));
1192   }
1193 
1194   /* Configure the Alarm A or Alarm B Sub Second registers */
1195   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1196 
1197   /* Disable the write protection for RTC registers */
1198   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1199 
1200   /* Configure the Alarm register */
1201   if(sAlarm->Alarm == RTC_ALARM_A)
1202   {
1203     /* Disable the Alarm A interrupt */
1204     __HAL_RTC_ALARMA_DISABLE(hrtc);
1205     /* Clear flag alarm A */
1206     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1207     /* In case of interrupt mode is used, the interrupt source must disabled */
1208     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1209 
1210     tickstart = HAL_GetTick();
1211     /* Wait till RTC ALRAWF flag is set and if timeout is reached exit */
1212 #if defined(TAMP)
1213     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1214 #else
1215     while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1216 #endif /* TAMP */
1217     {
1218       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1219       {
1220         /* Enable the write protection for RTC registers */
1221         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1222 
1223         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1224 
1225         /* Process Unlocked */
1226         __HAL_UNLOCK(hrtc);
1227 
1228         return HAL_TIMEOUT;
1229       }
1230     }
1231 
1232     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1233     /* Configure the Alarm A Sub Second register */
1234     hrtc->Instance->ALRMASSR = subsecondtmpreg;
1235     /* Configure the Alarm state: Enable Alarm */
1236     __HAL_RTC_ALARMA_ENABLE(hrtc);
1237   }
1238   else
1239   {
1240     /* Disable the Alarm B interrupt */
1241     __HAL_RTC_ALARMB_DISABLE(hrtc);
1242     /* Clear flag alarm B */
1243     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1244     /* In case of interrupt mode is used, the interrupt source must disabled */
1245     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1246 
1247     tickstart = HAL_GetTick();
1248     /* Wait till RTC ALRBWF flag is set and if timeout is reached exit */
1249 #if defined(TAMP)
1250     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1251 #else
1252     while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1253 #endif /* TAMP */
1254     {
1255       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1256       {
1257         /* Enable the write protection for RTC registers */
1258         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1259 
1260         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1261 
1262         /* Process Unlocked */
1263         __HAL_UNLOCK(hrtc);
1264 
1265         return HAL_TIMEOUT;
1266       }
1267     }
1268 
1269     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1270     /* Configure the Alarm B Sub Second register */
1271     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1272     /* Configure the Alarm state: Enable Alarm */
1273     __HAL_RTC_ALARMB_ENABLE(hrtc);
1274   }
1275 
1276   /* Enable the write protection for RTC registers */
1277   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1278 
1279   /* Change RTC state */
1280   hrtc->State = HAL_RTC_STATE_READY;
1281 
1282   /* Process Unlocked */
1283   __HAL_UNLOCK(hrtc);
1284 
1285   return HAL_OK;
1286 }
1287 
1288 /**
1289   * @brief  Set the specified RTC Alarm with Interrupt.
1290   * @param  hrtc RTC handle
1291   * @param  sAlarm Pointer to Alarm structure
1292   * @param  Format Specifies the format of the entered parameters.
1293   *          This parameter can be one of the following values:
1294   *             @arg RTC_FORMAT_BIN: Binary data format
1295   *             @arg RTC_FORMAT_BCD: BCD data format
1296   * @note   The Alarm register can only be written when the corresponding Alarm
1297   *         is disabled (Use the HAL_RTC_DeactivateAlarm()).
1298   * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
1299   * @retval HAL status
1300   */
1301 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1302 {
1303   uint32_t tickstart;
1304   uint32_t tmpreg;
1305   uint32_t subsecondtmpreg;
1306 
1307   /* Check the parameters */
1308   assert_param(IS_RTC_FORMAT(Format));
1309   assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1310   assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1311   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1312   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1313   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1314 
1315   /* Process Locked */
1316   __HAL_LOCK(hrtc);
1317 
1318   hrtc->State = HAL_RTC_STATE_BUSY;
1319 
1320   if(Format == RTC_FORMAT_BIN)
1321   {
1322     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1323     {
1324       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1325       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1326     }
1327     else
1328     {
1329       sAlarm->AlarmTime.TimeFormat = 0x00U;
1330       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1331     }
1332     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1333     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1334 
1335     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1336     {
1337       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1338     }
1339     else
1340     {
1341       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1342     }
1343 
1344     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours)   << RTC_ALRMAR_HU_Pos)  | \
1345               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1346               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos)  | \
1347               ((uint32_t)sAlarm->AlarmTime.TimeFormat              << RTC_ALRMAR_PM_Pos)  | \
1348               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay)  << RTC_ALRMAR_DU_Pos)  | \
1349               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                                     | \
1350               ((uint32_t)sAlarm->AlarmMask));
1351   }
1352   else
1353   {
1354     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1355     {
1356       assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1357       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1358     }
1359     else
1360     {
1361       sAlarm->AlarmTime.TimeFormat = 0x00U;
1362       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1363     }
1364 
1365     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1366     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1367 
1368     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1369     {
1370       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1371     }
1372     else
1373     {
1374       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1375     }
1376 
1377     tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours       << RTC_ALRMAR_HU_Pos)  | \
1378               ((uint32_t)sAlarm->AlarmTime.Minutes     << RTC_ALRMAR_MNU_Pos) | \
1379               ((uint32_t)sAlarm->AlarmTime.Seconds     << RTC_ALRMAR_SU_Pos)  | \
1380               ((uint32_t)sAlarm->AlarmTime.TimeFormat  << RTC_ALRMAR_PM_Pos)  | \
1381               ((uint32_t)sAlarm->AlarmDateWeekDay      << RTC_ALRMAR_DU_Pos)  | \
1382               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                         | \
1383               ((uint32_t)sAlarm->AlarmMask));
1384   }
1385   /* Configure the Alarm A or Alarm B Sub Second registers */
1386   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1387 
1388   /* Disable the write protection for RTC registers */
1389   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1390 
1391   /* Configure the Alarm register */
1392   if(sAlarm->Alarm == RTC_ALARM_A)
1393   {
1394     /* Disable the Alarm A interrupt */
1395     __HAL_RTC_ALARMA_DISABLE(hrtc);
1396 
1397     /* Clear flag alarm A */
1398     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1399 
1400     tickstart = HAL_GetTick();
1401     /* Wait till RTC ALRAWF flag is set and if timeout is reached exit */
1402 #if defined(TAMP)
1403     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1404 #else
1405     while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1406 #endif /* TAMP */
1407     {
1408       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1409       {
1410         /* Enable the write protection for RTC registers */
1411         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1412 
1413         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1414 
1415         /* Process Unlocked */
1416         __HAL_UNLOCK(hrtc);
1417 
1418         return HAL_TIMEOUT;
1419       }
1420     }
1421 
1422     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1423     /* Configure the Alarm A Sub Second register */
1424     hrtc->Instance->ALRMASSR = subsecondtmpreg;
1425     /* Configure the Alarm state: Enable Alarm */
1426     __HAL_RTC_ALARMA_ENABLE(hrtc);
1427     /* Configure the Alarm interrupt */
1428     __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
1429   }
1430   else
1431   {
1432     /* Disable the Alarm B interrupt */
1433     __HAL_RTC_ALARMB_DISABLE(hrtc);
1434 
1435     /* Clear flag alarm B */
1436     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1437 
1438     tickstart = HAL_GetTick();
1439     /* Wait till RTC ALRBWF flag is set and if timeout is reached exit */
1440 #if defined(TAMP)
1441     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1442 #else
1443     while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1444 #endif /* TAMP */
1445     {
1446       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1447       {
1448         /* Enable the write protection for RTC registers */
1449         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1450 
1451         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1452 
1453         /* Process Unlocked */
1454         __HAL_UNLOCK(hrtc);
1455 
1456         return HAL_TIMEOUT;
1457       }
1458     }
1459 
1460     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1461     /* Configure the Alarm B Sub Second register */
1462     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1463     /* Configure the Alarm state: Enable Alarm */
1464     __HAL_RTC_ALARMB_ENABLE(hrtc);
1465     /* Configure the Alarm interrupt */
1466     __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1467   }
1468 
1469   /* RTC Alarm Interrupt Configuration: EXTI configuration */
1470 #if defined(DUAL_CORE)
1471   if (HAL_GetCurrentCPUID() == CM7_CPUID)
1472   {
1473     __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1474   }
1475   else
1476   {
1477     __HAL_RTC_ALARM_EXTID2_ENABLE_IT();
1478   }
1479 #else  /* SINGLE_CORE */
1480   __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1481 #endif
1482 
1483   __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1484 
1485   /* Enable the write protection for RTC registers */
1486   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1487 
1488   hrtc->State = HAL_RTC_STATE_READY;
1489 
1490   /* Process Unlocked */
1491   __HAL_UNLOCK(hrtc);
1492 
1493   return HAL_OK;
1494 }
1495 
1496 /**
1497   * @brief  Deactivate the specified RTC Alarm.
1498   * @param  hrtc RTC handle
1499   * @param  Alarm Specifies the Alarm.
1500   *          This parameter can be one of the following values:
1501   *            @arg RTC_ALARM_A:  AlarmA
1502   *            @arg RTC_ALARM_B:  AlarmB
1503   * @retval HAL status
1504   */
1505 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1506 {
1507   uint32_t tickstart;
1508 
1509   /* Check the parameters */
1510   assert_param(IS_RTC_ALARM(Alarm));
1511 
1512   /* Process Locked */
1513   __HAL_LOCK(hrtc);
1514 
1515   hrtc->State = HAL_RTC_STATE_BUSY;
1516 
1517   /* Disable the write protection for RTC registers */
1518   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1519 
1520   if(Alarm == RTC_ALARM_A)
1521   {
1522     /* AlarmA */
1523     __HAL_RTC_ALARMA_DISABLE(hrtc);
1524 
1525     /* In case of interrupt mode is used, the interrupt source must disabled */
1526     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1527 
1528     tickstart = HAL_GetTick();
1529 
1530     /* Wait till RTC ALRxWF flag is set and if timeout is reached exit */
1531 #if defined(TAMP)
1532     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
1533 #else
1534     while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1535 #endif /* TAMP */
1536     {
1537       if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
1538       {
1539         /* Enable the write protection for RTC registers */
1540         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1541 
1542         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1543 
1544         /* Process Unlocked */
1545         __HAL_UNLOCK(hrtc);
1546 
1547         return HAL_TIMEOUT;
1548       }
1549     }
1550   }
1551   else
1552   {
1553     /* AlarmB */
1554     __HAL_RTC_ALARMB_DISABLE(hrtc);
1555 
1556     /* In case of interrupt mode is used, the interrupt source must disabled */
1557     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1558 
1559     tickstart = HAL_GetTick();
1560 
1561     /* Wait till RTC ALRxWF flag is set and if timeout is reached exit */
1562 #if defined(TAMP)
1563     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
1564 #else
1565     while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1566 #endif /* TAMP */
1567     {
1568       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1569       {
1570         /* Enable the write protection for RTC registers */
1571         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1572 
1573         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1574 
1575         /* Process Unlocked */
1576         __HAL_UNLOCK(hrtc);
1577 
1578         return HAL_TIMEOUT;
1579       }
1580     }
1581   }
1582   /* Enable the write protection for RTC registers */
1583   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1584 
1585   hrtc->State = HAL_RTC_STATE_READY;
1586 
1587   /* Process Unlocked */
1588   __HAL_UNLOCK(hrtc);
1589 
1590   return HAL_OK;
1591 }
1592 
1593 /**
1594   * @brief  Get the RTC Alarm value and masks.
1595   * @param  hrtc RTC handle
1596   * @param  sAlarm Pointer to Date structure
1597   * @param  Alarm Specifies the Alarm.
1598   *          This parameter can be one of the following values:
1599   *             @arg RTC_ALARM_A: AlarmA
1600   *             @arg RTC_ALARM_B: AlarmB
1601   * @param  Format Specifies the format of the entered parameters.
1602   *          This parameter can be one of the following values:
1603   *             @arg RTC_FORMAT_BIN: Binary data format
1604   *             @arg RTC_FORMAT_BCD: BCD data format
1605   * @retval HAL status
1606   */
1607 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1608 {
1609   uint32_t tmpreg;
1610   uint32_t subsecondtmpreg;
1611 
1612   /* Check the parameters */
1613   assert_param(IS_RTC_FORMAT(Format));
1614   assert_param(IS_RTC_ALARM(Alarm));
1615 
1616   if(Alarm == RTC_ALARM_A)
1617   {
1618     /* AlarmA */
1619     sAlarm->Alarm = RTC_ALARM_A;
1620 
1621     tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1622     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
1623 
1624     /* Fill the structure with the read parameters */
1625     sAlarm->AlarmTime.Hours      = (uint8_t)((tmpreg & (RTC_ALRMAR_HT  | RTC_ALRMAR_HU))  >> RTC_ALRMAR_HU_Pos);
1626     sAlarm->AlarmTime.Minutes    = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1627     sAlarm->AlarmTime.Seconds    = (uint8_t)((tmpreg & (RTC_ALRMAR_ST  | RTC_ALRMAR_SU))  >> RTC_ALRMAR_SU_Pos);
1628     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg &  RTC_ALRMAR_PM)                    >> RTC_ALRMAR_PM_Pos);
1629     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1630     sAlarm->AlarmDateWeekDay     = (uint8_t)((tmpreg & (RTC_ALRMAR_DT  | RTC_ALRMAR_DU))  >> RTC_ALRMAR_DU_Pos);
1631     sAlarm->AlarmDateWeekDaySel  = (uint32_t)(tmpreg &  RTC_ALRMAR_WDSEL);
1632     sAlarm->AlarmMask            = (uint32_t)(tmpreg &  RTC_ALARMMASK_ALL);
1633   }
1634   else
1635   {
1636     sAlarm->Alarm = RTC_ALARM_B;
1637 
1638     tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1639     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1640 
1641     /* Fill the structure with the read parameters */
1642     sAlarm->AlarmTime.Hours      = (uint8_t)((tmpreg & (RTC_ALRMBR_HT  | RTC_ALRMBR_HU))  >> RTC_ALRMBR_HU_Pos);
1643     sAlarm->AlarmTime.Minutes    = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1644     sAlarm->AlarmTime.Seconds    = (uint8_t)((tmpreg & (RTC_ALRMBR_ST  | RTC_ALRMBR_SU))  >> RTC_ALRMBR_SU_Pos);
1645     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg &  RTC_ALRMBR_PM)                    >> RTC_ALRMBR_PM_Pos);
1646     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1647     sAlarm->AlarmDateWeekDay     = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU))   >> RTC_ALRMBR_DU_Pos);
1648     sAlarm->AlarmDateWeekDaySel  = (uint32_t)(tmpreg &  RTC_ALRMBR_WDSEL);
1649     sAlarm->AlarmMask            = (uint32_t)(tmpreg &  RTC_ALARMMASK_ALL);
1650   }
1651 
1652   if(Format == RTC_FORMAT_BIN)
1653   {
1654     sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1655     sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1656     sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1657     sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1658   }
1659 
1660   return HAL_OK;
1661 }
1662 
1663 /**
1664   * @brief  Handle Alarm interrupt request.
1665   * @param  hrtc RTC handle
1666   * @retval None
1667   */
1668 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
1669 {
1670   /* Clear the EXTI's line Flag for RTC Alarm */
1671 #if defined(DUAL_CORE)
1672   if(HAL_GetCurrentCPUID() == CM7_CPUID)
1673   {
1674     __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1675   }
1676   else
1677   {
1678     __HAL_RTC_ALARM_EXTID2_CLEAR_FLAG();
1679   }
1680 #else  /* SINGLE_CORE */
1681   __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1682 #endif /* DUAL_CORE */
1683 
1684 #if defined(TAMP)
1685   /* Get interrupt status */
1686   uint32_t tmp = hrtc->Instance->MISR;
1687 
1688   if((tmp & RTC_FLAG_ALRAF) != 0u)
1689   {
1690     /* Clear the AlarmA interrupt pending bit */
1691     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1692 
1693     /* Call Alarm A Callback */
1694 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1695     hrtc->AlarmAEventCallback(hrtc);
1696 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1697     HAL_RTC_AlarmAEventCallback(hrtc);
1698 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1699   }
1700 
1701   if((tmp & RTC_MISR_ALRBMF) != 0u)
1702   {
1703     /* Clear the AlarmB interrupt pending bit */
1704     hrtc->Instance->SCR = RTC_SCR_CALRBF;
1705 
1706     /* Call Alarm B Callback */
1707 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1708     hrtc->AlarmBEventCallback(hrtc);
1709 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1710     HAL_RTCEx_AlarmBEventCallback(hrtc);
1711 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1712   }
1713 #else
1714   /* Get the AlarmA interrupt source enable status */
1715   if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
1716   {
1717     /* Get the pending status of the AlarmA Interrupt */
1718     if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
1719     {
1720       /* Clear the AlarmA interrupt pending bit */
1721       __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1722 
1723 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1724       hrtc->AlarmAEventCallback(hrtc);
1725 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1726       HAL_RTC_AlarmAEventCallback(hrtc);
1727 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1728     }
1729   }
1730 
1731   /* Get the AlarmB interrupt source enable status */
1732   if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
1733   {
1734     /* Get the pending status of the AlarmB Interrupt */
1735     if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
1736     {
1737       /* Clear the AlarmB interrupt pending bit */
1738       __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1739 
1740       /* AlarmB callback */
1741 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1742       hrtc->AlarmBEventCallback(hrtc);
1743 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1744       HAL_RTCEx_AlarmBEventCallback(hrtc);
1745 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1746     }
1747   }
1748 #endif /* TAMP */
1749 
1750   /* Change RTC state */
1751   hrtc->State = HAL_RTC_STATE_READY;
1752 }
1753 
1754 /**
1755   * @brief  Alarm A callback.
1756   * @param  hrtc RTC handle
1757   * @retval None
1758   */
1759 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1760 {
1761   /* Prevent unused argument(s) compilation warning */
1762   UNUSED(hrtc);
1763 
1764   /* NOTE : This function should not be modified, when the callback is needed,
1765             the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1766    */
1767 }
1768 
1769 /**
1770   * @brief  Handle AlarmA Polling request.
1771   * @param  hrtc RTC handle
1772   * @param  Timeout Timeout duration
1773   * @retval HAL status
1774   */
1775 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1776 {
1777 
1778   uint32_t tickstart = HAL_GetTick();
1779 
1780   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
1781   {
1782     if(Timeout != HAL_MAX_DELAY)
1783     {
1784       if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1785       {
1786         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1787         return HAL_TIMEOUT;
1788       }
1789     }
1790   }
1791 
1792   /* Clear the Alarm interrupt pending bit */
1793   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1794 
1795   /* Change RTC state */
1796   hrtc->State = HAL_RTC_STATE_READY;
1797 
1798   return HAL_OK;
1799 }
1800 
1801 /**
1802   * @}
1803   */
1804 
1805 /** @addtogroup RTC_Exported_Functions_Group4
1806  *  @brief   Peripheral Control functions
1807  *
1808 @verbatim
1809  ===============================================================================
1810                      ##### Peripheral Control functions #####
1811  ===============================================================================
1812     [..]
1813     This subsection provides functions allowing to
1814       (+) Wait for RTC Time and Date Synchronization
1815 
1816 @endverbatim
1817   * @{
1818   */
1819 
1820 /**
1821   * @brief  Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1822   *         synchronized with RTC APB clock.
1823   * @note   The RTC Resynchronization mode is write protected, use the
1824   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1825   * @note   To read the calendar through the shadow registers after Calendar
1826   *         initialization, calendar update or after wakeup from low power modes
1827   *         the software must first clear the RSF flag.
1828   *         The software must then wait until it is set again before reading
1829   *         the calendar, which means that the calendar registers have been
1830   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
1831   * @param  hrtc RTC handle
1832   * @retval HAL status
1833   */
1834 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
1835 {
1836   uint32_t tickstart;
1837 
1838   /* Clear RSF flag, keep reserved bits at reset values (setting other flags has no effect) */
1839 #if defined(TAMP)
1840   hrtc->Instance->ICSR = ((uint32_t)(RTC_RSF_MASK & RTC_ICSR_RESERVED_MASK));
1841 #else
1842   hrtc->Instance->ISR = ((uint32_t)(RTC_RSF_MASK & RTC_ISR_RESERVED_MASK));
1843 #endif /* TAMP */
1844 
1845   tickstart = HAL_GetTick();
1846 
1847   /* Wait the registers to be synchronised */
1848 #if defined(TAMP)
1849   while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
1850 #else
1851     while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
1852 #endif /* TAMP */
1853     {
1854       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1855       {
1856         return HAL_TIMEOUT;
1857       }
1858     }
1859 
1860   return HAL_OK;
1861 }
1862 
1863 /**
1864   * @}
1865   */
1866 
1867 /** @addtogroup RTC_Exported_Functions_Group5
1868  *  @brief   Peripheral State functions
1869  *
1870 @verbatim
1871  ===============================================================================
1872                      ##### Peripheral State functions #####
1873  ===============================================================================
1874     [..]
1875     This subsection provides functions allowing to
1876       (+) Get RTC state
1877 
1878 @endverbatim
1879   * @{
1880   */
1881 /**
1882   * @brief  Return the RTC handle state.
1883   * @param  hrtc RTC handle
1884   * @retval HAL state
1885   */
1886 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
1887 {
1888   /* Return RTC handle state */
1889   return hrtc->State;
1890 }
1891 
1892 /**
1893   * @}
1894   */
1895 
1896 /**
1897   * @}
1898   */
1899 
1900 /** @addtogroup RTC_Private_Functions
1901   * @{
1902   */
1903 /**
1904   * @brief  Enter the RTC Initialization mode.
1905   * @note   The RTC Initialization mode is write protected, use the
1906   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1907   * @param  hrtc RTC handle
1908   * @retval HAL status
1909   */
1910 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
1911 {
1912   uint32_t tickstart;
1913   HAL_StatusTypeDef status = HAL_OK;
1914   /* Check if the Initialization mode is set */
1915 #if defined(TAMP)
1916   if ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
1917   {
1918     /* Set the Initialization mode */
1919     SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
1920 
1921     tickstart = HAL_GetTick();
1922 
1923     /* Wait till RTC is in INIT state and if timeout is reached exit */
1924     while (((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
1925 #else
1926   if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
1927   {
1928     /* Set the Initialization mode */
1929     hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1930 
1931     tickstart = HAL_GetTick();
1932 
1933     /* Wait till RTC is in INIT state and if timeout is reached exit */
1934     while (((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U) && (status != HAL_TIMEOUT))
1935 #endif /* TAMP */
1936     {
1937       if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
1938       {
1939         status = HAL_TIMEOUT;
1940         hrtc->State = HAL_RTC_STATE_TIMEOUT;
1941       }
1942     }
1943   }
1944 
1945   return status;
1946 }
1947 
1948 /**
1949   * @brief  Exit the RTC Initialization mode.
1950   * @param  hrtc RTC handle
1951   * @retval HAL status
1952   */
1953 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
1954 {
1955   HAL_StatusTypeDef status = HAL_OK;
1956 
1957   /* Check if the Initialization mode is set */
1958 
1959   /* Exit Initialization mode */
1960 #if defined(TAMP)
1961   CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
1962 #else
1963   CLEAR_BIT(RTC->ISR, RTC_ISR_INIT);
1964 #endif /* TAMP */
1965 
1966   /* If CR_BYPSHAD bit = 0, wait for synchro */
1967   if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1968   {
1969     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1970     {
1971       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1972       status = HAL_TIMEOUT;
1973     }
1974   }
1975   else
1976   {
1977     /* Clear BYPSHAD bit */
1978     CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1979     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1980     {
1981       hrtc->State = HAL_RTC_STATE_TIMEOUT;
1982       status = HAL_TIMEOUT;
1983     }
1984     /* Restore BYPSHAD bit */
1985     SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1986   }
1987 
1988   return status;
1989 }
1990 
1991 /**
1992   * @brief  Convert a 2 digit decimal to BCD format.
1993   * @param  Value Byte to be converted
1994   * @retval Converted byte
1995   */
1996 uint8_t RTC_ByteToBcd2(uint8_t Value)
1997 {
1998   uint32_t bcdhigh = 0U;
1999   uint8_t  bcdlow  = Value;
2000 
2001   while (bcdlow >= 10U)
2002   {
2003     bcdhigh++;
2004     bcdlow -= 10U;
2005   }
2006 
2007   return ((uint8_t)(bcdhigh << 4U) | bcdlow);
2008 }
2009 
2010 /**
2011   * @brief  Convert from 2 digit BCD to Binary.
2012   * @param  Value BCD value to be converted
2013   * @retval Converted word
2014   */
2015 uint8_t RTC_Bcd2ToByte(uint8_t Value)
2016 {
2017   uint8_t tmp;
2018   tmp = ((Value & 0xF0U) >> 4U) * 10U;
2019   return (tmp + (Value & 0x0FU));
2020 }
2021 
2022 /**
2023   * @}
2024   */
2025 
2026 #endif /* HAL_RTC_MODULE_ENABLED */
2027 /**
2028   * @}
2029   */
2030 
2031 /**
2032   * @}
2033   */
2034