STM32H735xx HAL User Manual
stm32h7xx_hal_rtc.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_hal_rtc.c
00004   * @author  MCD Application Team
00005   * @brief   RTC HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Real-Time Clock (RTC) peripheral:
00008   *           + Initialization/de-initialization
00009   *           + Calendar (Time and Date) configuration
00010   *           + Alarms (Alarm A and Alarm B) configuration
00011   *           + WakeUp Timer configuration
00012   *           + TimeStamp configuration
00013   *           + Tampers configuration
00014   *           + Backup Data Registers configuration
00015   *           + RTC Tamper and TimeStamp Pins Selection
00016   *           + Interrupts and flags management
00017   *
00018   ******************************************************************************
00019   * @attention
00020   *
00021   * Copyright (c) 2017 STMicroelectronics.
00022   * All rights reserved.
00023   *
00024   * This software is licensed under terms that can be found in the LICENSE file
00025   * in the root directory of this software component.
00026   * If no LICENSE file comes with this software, it is provided AS-IS.
00027   *
00028   ******************************************************************************
00029   @verbatim
00030  ===============================================================================
00031                           ##### RTC Operating Condition #####
00032  ===============================================================================
00033   [..] The real-time clock (RTC) and the RTC backup registers can be powered
00034        from the VBAT voltage when the main VDD supply is powered off.
00035        To retain the content of the RTC backup registers and supply the RTC
00036        when VDD is turned off, VBAT pin can be connected to an optional
00037        standby voltage supplied by a battery or by another source.
00038 
00039                    ##### Backup Domain Reset #####
00040  ===============================================================================
00041   [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
00042        to their reset values.
00043        A backup domain reset is generated when one of the following events occurs:
00044     (#) Software reset, triggered by setting the BDRST bit in the
00045         RCC Backup domain control register (RCC_BDCR).
00046     (#) VDD or VBAT power on, if both supplies have previously been powered off.
00047     (#) Tamper detection event resets all data backup registers.
00048 
00049                    ##### Backup Domain Access #####
00050  ===================================================================
00051   [..] After reset, the backup domain (RTC registers, RTC backup data
00052        registers and backup SRAM) is protected against possible unwanted write
00053        accesses.
00054 
00055   [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
00056     (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
00057         PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
00058     (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
00059 
00060                   ##### How to use RTC Driver #####
00061  ===================================================================
00062   [..]
00063     (+) Enable the RTC domain access (see description in the section above).
00064     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
00065         format using the HAL_RTC_Init() function.
00066 
00067   *** Time and Date configuration ***
00068   ===================================
00069   [..]
00070     (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
00071         and HAL_RTC_SetDate() functions.
00072     (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
00073 
00074   *** Alarm configuration ***
00075   ===========================
00076   [..]
00077     (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
00078             You can also configure the RTC Alarm with interrupt mode using the
00079             HAL_RTC_SetAlarm_IT() function.
00080     (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
00081 
00082                   ##### RTC and low power modes #####
00083  ===================================================================
00084   [..] The MCU can be woken up from a low power mode by an RTC alternate
00085        function.
00086   [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
00087        RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
00088        These RTC alternate functions can wake up the system from the Stop and
00089        Standby low power modes.
00090   [..] The system can also wake up from low power modes without depending
00091        on an external interrupt (Auto-wakeup mode), by using the RTC alarm
00092        or the RTC wakeup events.
00093   [..] The RTC provides a programmable time base for waking up from the
00094        Stop or Standby mode at regular intervals.
00095        Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
00096        is LSE or LSI.
00097 
00098   *** Callback registration ***
00099   =============================================
00100   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
00101   not defined, the callback registration feature is not available and all callbacks
00102   are set to the corresponding weak functions. This is the recommended configuration
00103   in order to optimize memory/code consumption footprint/performances.
00104 
00105   The compilation define  USE_RTC_REGISTER_CALLBACKS when set to 1
00106   allows the user to configure dynamically the driver callbacks.
00107   Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
00108 
00109   Function HAL_RTC_RegisterCallback() allows to register following callbacks:
00110     (+) AlarmAEventCallback          : RTC Alarm A Event callback.
00111     (+) AlarmBEventCallback          : RTC Alarm B Event callback.
00112     (+) TimeStampEventCallback       : RTC TimeStamp Event callback.
00113     (+) WakeUpTimerEventCallback     : RTC WakeUpTimer Event callback.
00114     (+) Tamper1EventCallback         : RTC Tamper 1 Event callback.
00115     (+) Tamper2EventCallback         : RTC Tamper 2 Event callback.
00116     (+) Tamper3EventCallback         : RTC Tamper 3 Event callback.
00117     (+) MspInitCallback              : RTC MspInit callback.
00118     (+) MspDeInitCallback            : RTC MspDeInit callback.
00119   This function takes as parameters the HAL peripheral handle, the Callback ID
00120   and a pointer to the user callback function.
00121 
00122   Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
00123   weak function.
00124   HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00125   and the Callback ID.
00126   This function allows to reset following callbacks:
00127     (+) AlarmAEventCallback          : RTC Alarm A Event callback.
00128     (+) AlarmBEventCallback          : RTC Alarm B Event callback.
00129     (+) TimeStampEventCallback       : RTC TimeStamp Event callback.
00130     (+) WakeUpTimerEventCallback     : RTC WakeUpTimer Event callback.
00131     (+) Tamper1EventCallback         : RTC Tamper 1 Event callback.
00132     (+) Tamper2EventCallback         : RTC Tamper 2 Event callback.
00133     (+) Tamper3EventCallback         : RTC Tamper 3 Event callback.
00134     (+) MspInitCallback              : RTC MspInit callback.
00135     (+) MspDeInitCallback            : RTC MspDeInit callback.
00136 
00137   By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
00138   all callbacks are set to the corresponding weak functions :
00139   examples AlarmAEventCallback(), WakeUpTimerEventCallback().
00140   Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
00141   in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
00142   (not registered beforehand).
00143   If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
00144   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
00145 
00146   Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
00147   Exception done MspInit/MspDeInit that can be registered/unregistered
00148   in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
00149   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
00150   In that case first register the MspInit/MspDeInit user callbacks
00151   using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
00152   or HAL_RTC_Init() function.
00153 
00154   When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
00155   not defined, the callback registration feature is not available and all callbacks
00156   are set to the corresponding weak functions.
00157    @endverbatim
00158 
00159   */
00160 
00161 /* Includes ------------------------------------------------------------------*/
00162 #include "stm32h7xx_hal.h"
00163 
00164 /** @addtogroup STM32H7xx_HAL_Driver
00165   * @{
00166   */
00167 
00168 
00169 /** @addtogroup RTC
00170   * @brief RTC HAL module driver
00171   * @{
00172   */
00173 
00174 #ifdef HAL_RTC_MODULE_ENABLED
00175 
00176 /* Private typedef -----------------------------------------------------------*/
00177 /* Private define ------------------------------------------------------------*/
00178 /* Private macro -------------------------------------------------------------*/
00179 /* Private variables ---------------------------------------------------------*/
00180 /* Private function prototypes -----------------------------------------------*/
00181 /* Exported functions --------------------------------------------------------*/
00182 
00183 /** @addtogroup RTC_Exported_Functions
00184   * @{
00185   */
00186 
00187 /** @addtogroup RTC_Exported_Functions_Group1
00188  *  @brief    Initialization and Configuration functions
00189  *
00190 @verbatim
00191  ===============================================================================
00192               ##### Initialization and de-initialization functions #####
00193  ===============================================================================
00194    [..] This section provides functions allowing to initialize and configure the
00195          RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
00196          RTC registers Write protection, enter and exit the RTC initialization mode,
00197          RTC registers synchronization check and reference clock detection enable.
00198          (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
00199              It is split into 2 programmable prescalers to minimize power consumption.
00200              (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
00201              (++) When both prescalers are used, it is recommended to configure the
00202                  asynchronous prescaler to a high value to minimize power consumption.
00203          (#) All RTC registers are Write protected. Writing to the RTC registers
00204              is enabled by writing a key into the Write Protection register, RTC_WPR.
00205          (#) To configure the RTC Calendar, user application should enter
00206              initialization mode. In this mode, the calendar counter is stopped
00207              and its value can be updated. When the initialization sequence is
00208              complete, the calendar restarts counting after 4 RTCCLK cycles.
00209          (#) To read the calendar through the shadow registers after Calendar
00210              initialization, calendar update or after wakeup from low power modes
00211              the software must first clear the RSF flag. The software must then
00212              wait until it is set again before reading the calendar, which means
00213              that the calendar registers have been correctly copied into the
00214              RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
00215              implements the above software sequence (RSF clear and RSF check).
00216 
00217 @endverbatim
00218   * @{
00219   */
00220 
00221 /**
00222   * @brief  Initialize the RTC peripheral
00223   * @param  hrtc RTC handle
00224   * @retval HAL status
00225   */
00226 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
00227 {
00228   HAL_StatusTypeDef status = HAL_ERROR;
00229 
00230   /* Check RTC handler */
00231   if(hrtc != NULL)
00232   {
00233     /* Check the parameters */
00234     assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
00235     assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
00236     assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
00237     assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
00238     assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
00239     assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
00240     assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
00241     assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
00242 #if defined(RTC_CR_TAMPALRM_PU)
00243     assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
00244 #endif /* RTC_CR_TAMPALRM_PU */
00245 
00246 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00247     if(hrtc->State == HAL_RTC_STATE_RESET)
00248     {
00249       /* Allocate lock resource and initialize it */
00250       hrtc->Lock = HAL_UNLOCKED;
00251 
00252       hrtc->AlarmAEventCallback          =  HAL_RTC_AlarmAEventCallback;             /* Legacy weak AlarmAEventCallback      */
00253       hrtc->AlarmBEventCallback          =  HAL_RTCEx_AlarmBEventCallback;           /* Legacy weak AlarmBEventCallback      */
00254       hrtc->TimeStampEventCallback       =  HAL_RTCEx_TimeStampEventCallback;        /* Legacy weak TimeStampEventCallback   */
00255       hrtc->WakeUpTimerEventCallback     =  HAL_RTCEx_WakeUpTimerEventCallback;      /* Legacy weak WakeUpTimerEventCallback */
00256       hrtc->Tamper1EventCallback         =  HAL_RTCEx_Tamper1EventCallback;          /* Legacy weak Tamper1EventCallback     */
00257       hrtc->Tamper2EventCallback         =  HAL_RTCEx_Tamper2EventCallback;          /* Legacy weak Tamper2EventCallback     */
00258       hrtc->Tamper3EventCallback         =  HAL_RTCEx_Tamper3EventCallback;          /* Legacy weak Tamper3EventCallback     */
00259 
00260 #if defined(TAMP_CR1_ITAMP1E)
00261       hrtc->InternalTamper1EventCallback =  HAL_RTCEx_InternalTamper1EventCallback;
00262 #endif /* TAMP_CR1_ITAMP1E */
00263 #if defined(TAMP_CR1_ITAMP2E)
00264       hrtc->InternalTamper2EventCallback =  HAL_RTCEx_InternalTamper2EventCallback;
00265 #endif /* TAMP_CR1_ITAMP2E */
00266 #if defined(TAMP_CR1_ITAMP3E)
00267       hrtc->InternalTamper3EventCallback =  HAL_RTCEx_InternalTamper3EventCallback;
00268 #endif /* TAMP_CR1_ITAMP3E */
00269 #if defined(TAMP_CR1_ITAMP4E)
00270       hrtc->InternalTamper4EventCallback =  HAL_RTCEx_InternalTamper4EventCallback;
00271 #endif /* TAMP_CR1_ITAMP4E */
00272 #if defined(TAMP_CR1_ITAMP5E)
00273       hrtc->InternalTamper5EventCallback =  HAL_RTCEx_InternalTamper5EventCallback;
00274 #endif /* TAMP_CR1_ITAMP5E */
00275 #if defined(TAMP_CR1_ITAMP6E)
00276       hrtc->InternalTamper6EventCallback =  HAL_RTCEx_InternalTamper6EventCallback;
00277 #endif /* TAMP_CR1_ITAMP6E */
00278 #if defined(TAMP_CR1_ITAMP8E)
00279       hrtc->InternalTamper8EventCallback =  HAL_RTCEx_InternalTamper8EventCallback;
00280 #endif /* TAMP_CR1_ITAMP8E */
00281 
00282 
00283       if(hrtc->MspInitCallback == NULL)
00284       {
00285         hrtc->MspInitCallback = HAL_RTC_MspInit;
00286       }
00287       /* Init the low level hardware */
00288       hrtc->MspInitCallback(hrtc);
00289 
00290       if(hrtc->MspDeInitCallback == NULL)
00291       {
00292         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
00293       }
00294     }
00295 #else /*  (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00296     if(hrtc->State == HAL_RTC_STATE_RESET)
00297     {
00298       /* Allocate lock resource and initialize it */
00299       hrtc->Lock = HAL_UNLOCKED;
00300 
00301       /* Initialize RTC MSP */
00302       HAL_RTC_MspInit(hrtc);
00303     }
00304 #endif /*  (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
00305 
00306     /* Set RTC state */
00307     hrtc->State = HAL_RTC_STATE_BUSY;
00308 
00309     /* Disable the write protection for RTC registers */
00310     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00311 
00312    /* Enter Initialization mode */
00313     status = RTC_EnterInitMode(hrtc);
00314     if (status == HAL_OK)
00315     {
00316 #if defined(RTC_CR_TAMPOE)
00317       /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */
00318       hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
00319 #else  /* RTC_CR_TAMPOE */
00320       /* Clear RTC_CR FMT, OSEL and POL Bits */
00321       hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
00322 #endif /* RTC_CR_TAMPOE */
00323 
00324       /* Set RTC_CR register */
00325       hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
00326 
00327       /* Configure the RTC PRER */
00328       hrtc->Instance->PRER = (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos) | (hrtc->Init.SynchPrediv << RTC_PRER_PREDIV_S_Pos);
00329 
00330       /* Exit Initialization mode */
00331       status = RTC_ExitInitMode(hrtc);
00332 
00333       if(status == HAL_OK)
00334       {
00335 #if defined(RTC_CR_TAMPALRM_PU) && defined(RTC_CR_TAMPALRM_TYPE) && defined(RTC_CR_OUT2EN)
00336         hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
00337         hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
00338 #endif /* RTC_CR_TAMPALRM_TYPE && RTC_CR_OUT2EN && RTC_CR_TAMPALRM_PU */
00339 
00340 #if defined(RTC_OR_ALARMOUTTYPE) && defined(RTC_OR_OUT_RMP)
00341         hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
00342         hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
00343 #endif /* ALARMOUTTYPE && RTC_OR_OUT_RMP */
00344       }
00345     }
00346     /* Enable the write protection for RTC registers */
00347     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00348     if (status == HAL_OK)
00349     {
00350       /* Set RTC state */
00351       hrtc->State = HAL_RTC_STATE_READY;
00352     }
00353   }
00354 
00355   /* return status */
00356   return status;
00357 }
00358 
00359 /**
00360   * @brief  DeInitialize the RTC peripheral.
00361   * @note   This function doesn't reset the RTC Backup Data registers.
00362   * @param  hrtc RTC handle
00363   * @retval HAL status
00364   */
00365 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
00366 {
00367   HAL_StatusTypeDef status = HAL_ERROR;
00368   uint32_t          tickstart;
00369 
00370   /* Check RTC handler */
00371   if(hrtc != NULL)
00372   {
00373     /* Check the parameters */
00374     assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
00375 
00376     /* Set RTC state */
00377     hrtc->State = HAL_RTC_STATE_BUSY;
00378 
00379     /* Disable the write protection for RTC registers */
00380     __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00381     /* Enter Initialization mode */
00382     status = RTC_EnterInitMode(hrtc);
00383     if (status == HAL_OK)
00384     {
00385       /* Reset TR, DR and CR registers */
00386       hrtc->Instance->TR = 0x00000000U;
00387       hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
00388       /* Reset All CR bits except CR[2:0] */
00389       hrtc->Instance->CR &= RTC_CR_WUCKSEL;
00390 
00391       tickstart = HAL_GetTick();
00392 
00393       /* Wait till WUTWF flag is set and if Time out is reached exit */
00394 #if defined(RTC_ICSR_WUTWF)
00395       while ((((hrtc->Instance->ICSR) & RTC_ICSR_WUTWF) == 0U) && (status != HAL_TIMEOUT))
00396 #endif /* RTC_ICSR_WUTWF */
00397 #if defined(RTC_ISR_WUTWF)
00398         while ((((hrtc->Instance->ISR)  & RTC_ISR_WUTWF)  == 0U) && (status != HAL_TIMEOUT))
00399 #endif /* RTC_ISR_WUTWF */
00400         {
00401           if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
00402           {
00403             /* Enable the write protection for RTC registers */
00404             __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00405 
00406             /* Set RTC state */
00407             hrtc->State = HAL_RTC_STATE_TIMEOUT;
00408             status = HAL_TIMEOUT;
00409 
00410           }
00411         }
00412 
00413       /* Exit initialization mode */
00414       status = RTC_ExitInitMode(hrtc);
00415       if (status == HAL_OK)
00416       {
00417         /* Reset all RTC CR register bits */
00418         hrtc->Instance->CR &= 0x00000000U;
00419 
00420         /* Reset other RTC registers */
00421         hrtc->Instance->WUTR = RTC_WUTR_WUT;
00422         hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
00423         hrtc->Instance->ALRMAR = 0x00000000U;
00424         hrtc->Instance->ALRMBR = 0x00000000U;
00425         hrtc->Instance->SHIFTR = 0x00000000U;
00426         hrtc->Instance->CALR = 0x00000000U;
00427         hrtc->Instance->ALRMASSR = 0x00000000U;
00428         hrtc->Instance->ALRMBSSR = 0x00000000U;
00429 
00430 #if defined(RTC_ISR_INIT)
00431         /* Reset Tamper configuration register */
00432         hrtc->Instance->TAMPCR = 0x00000000U;
00433 
00434         /* Reset Option register */
00435         hrtc->Instance->OR = 0x00000000U;
00436 #endif /* RTC_ISR_INIT */
00437       }
00438     }
00439 
00440     if(status == HAL_OK)
00441     {
00442 #if defined(TAMP_CR1_TAMP1E)
00443       /* Reset TAMP registers */
00444       ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR1 = 0xFFFF0000U;
00445       ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->CR2 = 0x00000000U;
00446 #endif /* TAMP_CR1_TAMP1E */
00447 
00448       /* Enable the write protection for RTC registers */
00449       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00450 
00451 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00452       if(hrtc->MspDeInitCallback == NULL)
00453       {
00454         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
00455       }
00456 
00457       /* DeInit the low level hardware: CLOCK, NVIC.*/
00458       hrtc->MspDeInitCallback(hrtc);
00459 #else
00460       /* De-Initialize RTC MSP */
00461       HAL_RTC_MspDeInit(hrtc);
00462 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
00463 
00464       hrtc->State = HAL_RTC_STATE_RESET;
00465 
00466       /* Release Lock */
00467       __HAL_UNLOCK(hrtc);
00468     }
00469   }
00470 
00471   /* return status */
00472   return status;
00473 }
00474 
00475 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00476 /**
00477   * @brief  Register a User RTC Callback
00478   *         To be used instead of the weak predefined callback
00479   * @param  hrtc RTC handle
00480   * @param  CallbackID ID of the callback to be registered
00481   *         This parameter can be one of the following values:
00482   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
00483   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID          Alarm B Event Callback ID
00484   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID        TimeStamp Event Callback ID
00485   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID      WakeUp Timer Event Callback ID
00486   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 Callback ID
00487   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID          Tamper 2 Callback ID
00488   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID          Tamper 3 Callback ID
00489   *          @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
00490   *          @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
00491   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
00492   *          @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
00493   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
00494   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
00495   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
00496   *          @arg @ref HAL_RTC_MSPINIT_CB_ID                Msp Init callback ID
00497   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID              Msp DeInit callback ID
00498   * @param  pCallback pointer to the Callback function
00499   * @retval HAL status
00500   */
00501 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
00502 {
00503   HAL_StatusTypeDef status = HAL_OK;
00504 
00505   if(pCallback == NULL)
00506   {
00507     return HAL_ERROR;
00508   }
00509 
00510   /* Process locked */
00511   __HAL_LOCK(hrtc);
00512 
00513   if(HAL_RTC_STATE_READY == hrtc->State)
00514   {
00515     switch (CallbackID)
00516     {
00517       case HAL_RTC_ALARM_A_EVENT_CB_ID :
00518         hrtc->AlarmAEventCallback = pCallback;
00519         break;
00520 
00521       case HAL_RTC_ALARM_B_EVENT_CB_ID :
00522         hrtc->AlarmBEventCallback = pCallback;
00523         break;
00524 
00525       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
00526         hrtc->TimeStampEventCallback = pCallback;
00527         break;
00528 
00529       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
00530         hrtc->WakeUpTimerEventCallback = pCallback;
00531         break;
00532 
00533       case HAL_RTC_TAMPER1_EVENT_CB_ID :
00534         hrtc->Tamper1EventCallback = pCallback;
00535         break;
00536 
00537       case HAL_RTC_TAMPER2_EVENT_CB_ID :
00538         hrtc->Tamper2EventCallback = pCallback;
00539         break;
00540 
00541       case HAL_RTC_TAMPER3_EVENT_CB_ID :
00542         hrtc->Tamper3EventCallback = pCallback;
00543         break;
00544 
00545 #if defined(TAMP_CR1_ITAMP1E)
00546       case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
00547         hrtc->InternalTamper1EventCallback =  pCallback;
00548         break;
00549 #endif /* TAMP_CR1_ITAMP1E */
00550 
00551 #if defined(TAMP_CR1_ITAMP2E)
00552       case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
00553         hrtc->InternalTamper2EventCallback =  pCallback;
00554         break;
00555 #endif /* TAMP_CR1_ITAMP2E */
00556 
00557 #if defined(TAMP_CR1_ITAMP3E)
00558       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
00559         hrtc->InternalTamper3EventCallback =  pCallback;
00560         break;
00561 #endif /* TAMP_CR1_ITAMP3E */
00562 
00563 #if defined(TAMP_CR1_ITAMP4E)
00564       case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
00565         hrtc->InternalTamper4EventCallback =  pCallback;
00566         break;
00567 #endif /* TAMP_CR1_ITAMP4E */
00568 
00569 #if defined(TAMP_CR1_ITAMP5E)
00570       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
00571         hrtc->InternalTamper5EventCallback =  pCallback;
00572         break;
00573 #endif /* TAMP_CR1_ITAMP5E */
00574 
00575 #if defined(TAMP_CR1_ITAMP6E)
00576       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
00577         hrtc->InternalTamper6EventCallback =  pCallback;
00578         break;
00579 #endif /* TAMP_CR1_ITAMP6E */
00580 
00581 #if defined(TAMP_CR1_ITAMP8E)
00582       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
00583         hrtc->InternalTamper8EventCallback =  pCallback;
00584         break;
00585 #endif /* TAMP_CR1_ITAMP8E */
00586 
00587       case HAL_RTC_MSPINIT_CB_ID :
00588         hrtc->MspInitCallback = pCallback;
00589         break;
00590 
00591       case HAL_RTC_MSPDEINIT_CB_ID :
00592         hrtc->MspDeInitCallback = pCallback;
00593         break;
00594 
00595       default :
00596         /* Return error status */
00597         status =  HAL_ERROR;
00598         break;
00599     }
00600   }
00601   else if(HAL_RTC_STATE_RESET == hrtc->State)
00602   {
00603     switch (CallbackID)
00604     {
00605       case HAL_RTC_MSPINIT_CB_ID :
00606         hrtc->MspInitCallback = pCallback;
00607         break;
00608 
00609       case HAL_RTC_MSPDEINIT_CB_ID :
00610         hrtc->MspDeInitCallback = pCallback;
00611         break;
00612 
00613       default :
00614         /* Return error status */
00615         status =  HAL_ERROR;
00616         break;
00617     }
00618   }
00619   else
00620   {
00621     /* Return error status */
00622     status =  HAL_ERROR;
00623   }
00624 
00625   /* Release Lock */
00626   __HAL_UNLOCK(hrtc);
00627 
00628   return status;
00629 }
00630 
00631 /**
00632   * @brief  Unregister an RTC Callback
00633   *         RTC callabck is redirected to the weak predefined callback
00634   * @param  hrtc RTC handle
00635   * @param  CallbackID ID of the callback to be unregistered
00636   *         This parameter can be one of the following values:
00637   *          @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID          Alarm A Event Callback ID
00638   *          @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID          Alarm B Event Callback ID
00639   *          @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID        TimeStamp Event Callback ID
00640   *          @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID      WakeUp Timer Event Callback ID
00641   *          @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID          Tamper 1 Callback ID
00642   *          @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID          Tamper 2 Callback ID
00643   *          @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID          Tamper 3 Callback ID
00644   *          @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
00645   *          @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
00646   *          @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
00647   *          @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
00648   *          @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
00649   *          @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
00650   *          @arg @ref HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID Internal Tamper 8 Callback ID
00651   *          @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
00652   *          @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
00653   * @retval HAL status
00654   */
00655 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
00656 {
00657   HAL_StatusTypeDef status = HAL_OK;
00658 
00659   /* Process locked */
00660   __HAL_LOCK(hrtc);
00661 
00662   if(HAL_RTC_STATE_READY == hrtc->State)
00663   {
00664     switch (CallbackID)
00665     {
00666       case HAL_RTC_ALARM_A_EVENT_CB_ID :
00667         hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback;             /* Legacy weak AlarmAEventCallback    */
00668         break;
00669 
00670       case HAL_RTC_ALARM_B_EVENT_CB_ID :
00671         hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback;           /* Legacy weak AlarmBEventCallback */
00672         break;
00673 
00674       case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
00675         hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback;     /* Legacy weak TimeStampEventCallback    */
00676         break;
00677 
00678       case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
00679         hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
00680         break;
00681 
00682       case HAL_RTC_TAMPER1_EVENT_CB_ID :
00683         hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback;         /* Legacy weak Tamper1EventCallback   */
00684         break;
00685 
00686       case HAL_RTC_TAMPER2_EVENT_CB_ID :
00687         hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback;         /* Legacy weak Tamper2EventCallback         */
00688         break;
00689 
00690       case HAL_RTC_TAMPER3_EVENT_CB_ID :
00691         hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback;         /* Legacy weak Tamper3EventCallback         */
00692         break;
00693 
00694 #if defined(TAMP_CR1_ITAMP1E)
00695       case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
00696         hrtc->InternalTamper1EventCallback =  HAL_RTCEx_InternalTamper1EventCallback;
00697         break;
00698 #endif /* TAMP_CR1_ITAMP1E */
00699 
00700 #if defined(TAMP_CR1_ITAMP2E)
00701       case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
00702         hrtc->InternalTamper2EventCallback =  HAL_RTCEx_InternalTamper2EventCallback;
00703         break;
00704 #endif /* TAMP_CR1_ITAMP2E */
00705 
00706 #if defined(TAMP_CR1_ITAMP3E)
00707       case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
00708         hrtc->InternalTamper3EventCallback =  HAL_RTCEx_InternalTamper3EventCallback;
00709         break;
00710 #endif /* TAMP_CR1_ITAMP3E */
00711 
00712 #if defined(TAMP_CR1_ITAMP4E)
00713       case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
00714         hrtc->InternalTamper4EventCallback =  HAL_RTCEx_InternalTamper4EventCallback;
00715         break;
00716 #endif /* TAMP_CR1_ITAMP4E */
00717 
00718 #if defined(TAMP_CR1_ITAMP5E)
00719       case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
00720         hrtc->InternalTamper5EventCallback =  HAL_RTCEx_InternalTamper5EventCallback;
00721         break;
00722 #endif /* TAMP_CR1_ITAMP5E */
00723 
00724 #if defined(TAMP_CR1_ITAMP6E)
00725       case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
00726         hrtc->InternalTamper6EventCallback =  HAL_RTCEx_InternalTamper6EventCallback;
00727         break;
00728 #endif /* TAMP_CR1_ITAMP6E */
00729 
00730 #if defined(TAMP_CR1_ITAMP8E)
00731       case HAL_RTC_INTERNAL_TAMPER8_EVENT_CB_ID :
00732         hrtc->InternalTamper8EventCallback =  HAL_RTCEx_InternalTamper8EventCallback;
00733         break;
00734 #endif /* TAMP_CR1_ITAMP8E */
00735 
00736       case HAL_RTC_MSPINIT_CB_ID :
00737         hrtc->MspInitCallback = HAL_RTC_MspInit;
00738         break;
00739 
00740       case HAL_RTC_MSPDEINIT_CB_ID :
00741         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
00742         break;
00743 
00744       default :
00745         /* Return error status */
00746         status =  HAL_ERROR;
00747         break;
00748     }
00749   }
00750   else if(HAL_RTC_STATE_RESET == hrtc->State)
00751   {
00752     switch (CallbackID)
00753     {
00754       case HAL_RTC_MSPINIT_CB_ID :
00755         hrtc->MspInitCallback = HAL_RTC_MspInit;
00756         break;
00757 
00758       case HAL_RTC_MSPDEINIT_CB_ID :
00759         hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
00760         break;
00761 
00762       default :
00763         /* Return error status */
00764         status =  HAL_ERROR;
00765         break;
00766     }
00767   }
00768   else
00769   {
00770     /* Return error status */
00771     status =  HAL_ERROR;
00772   }
00773 
00774   /* Release Lock */
00775   __HAL_UNLOCK(hrtc);
00776 
00777   return status;
00778 }
00779 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
00780 
00781 /**
00782   * @brief  Initialize the RTC MSP.
00783   * @param  hrtc RTC handle
00784   * @retval None
00785   */
00786 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
00787 {
00788   /* Prevent unused argument(s) compilation warning */
00789   UNUSED(hrtc);
00790 
00791   /* NOTE : This function should not be modified, when the callback is needed,
00792             the HAL_RTC_MspInit could be implemented in the user file
00793    */
00794 }
00795 
00796 /**
00797   * @brief  DeInitialize the RTC MSP.
00798   * @param  hrtc RTC handle
00799   * @retval None
00800   */
00801 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
00802 {
00803   /* Prevent unused argument(s) compilation warning */
00804   UNUSED(hrtc);
00805 
00806   /* NOTE : This function should not be modified, when the callback is needed,
00807             the HAL_RTC_MspDeInit could be implemented in the user file
00808    */
00809 }
00810 
00811 /**
00812   * @}
00813   */
00814 
00815 /** @addtogroup RTC_Exported_Functions_Group2
00816  *  @brief   RTC Time and Date functions
00817  *
00818 @verbatim
00819  ===============================================================================
00820                  ##### RTC Time and Date functions #####
00821  ===============================================================================
00822 
00823  [..] This section provides functions allowing to configure Time and Date features
00824 
00825 @endverbatim
00826   * @{
00827   */
00828 
00829 /**
00830   * @brief  Set RTC current time.
00831   * @param  hrtc RTC handle
00832   * @param  sTime Pointer to Time structure
00833   * @param  Format Specifies the format of the entered parameters.
00834   *          This parameter can be one of the following values:
00835   *            @arg RTC_FORMAT_BIN: Binary data format
00836   *            @arg RTC_FORMAT_BCD: BCD data format
00837   * @retval HAL status
00838   */
00839 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
00840 {
00841   uint32_t tmpreg;
00842 HAL_StatusTypeDef status;
00843 
00844   /* Check the parameters */
00845   assert_param(IS_RTC_FORMAT(Format));
00846   assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
00847   assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
00848 
00849   /* Process Locked */
00850   __HAL_LOCK(hrtc);
00851 
00852   hrtc->State = HAL_RTC_STATE_BUSY;
00853 
00854   /* Disable the write protection for RTC registers */
00855   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00856  /* Enter Initialization mode */
00857   status = RTC_EnterInitMode(hrtc);
00858   if (status == HAL_OK)
00859   {
00860     if(Format == RTC_FORMAT_BIN)
00861     {
00862       if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
00863       {
00864         assert_param(IS_RTC_HOUR12(sTime->Hours));
00865         assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
00866       }
00867       else
00868       {
00869         sTime->TimeFormat = 0x00U;
00870         assert_param(IS_RTC_HOUR24(sTime->Hours));
00871       }
00872       assert_param(IS_RTC_MINUTES(sTime->Minutes));
00873       assert_param(IS_RTC_SECONDS(sTime->Seconds));
00874 
00875       tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours)   << RTC_TR_HU_Pos)  | \
00876                           ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
00877                           ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos)  | \
00878                           (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
00879     }
00880     else
00881     {
00882       if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
00883       {
00884         assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
00885         assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
00886       }
00887       else
00888       {
00889         sTime->TimeFormat = 0x00U;
00890         assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
00891       }
00892       assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
00893       assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
00894       tmpreg = (((uint32_t)(sTime->Hours)   << RTC_TR_HU_Pos)  | \
00895                 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
00896                 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos)  | \
00897                 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
00898     }
00899 
00900     /* Set the RTC_TR register */
00901     hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
00902 
00903     /* Clear the bits to be configured */
00904     hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
00905 
00906     /* Configure the RTC_CR register */
00907     hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
00908 
00909     /* Exit Initialization mode */
00910     status = RTC_ExitInitMode(hrtc);
00911   }
00912 
00913   /* Enable the write protection for RTC registers */
00914   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00915 
00916   if (status == HAL_OK)
00917   {
00918     hrtc->State = HAL_RTC_STATE_READY;
00919   }
00920 
00921   /* Process Unlocked */
00922   __HAL_UNLOCK(hrtc);
00923 
00924   return status;
00925 }
00926 
00927 /**
00928   * @brief  Get RTC current time.
00929   * @param  hrtc RTC handle
00930   * @param  sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
00931   *                with input format (BIN or BCD), also SubSeconds field returning the
00932   *                RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
00933   *                factor to be used for second fraction ratio computation.
00934   * @param  Format Specifies the format of the entered parameters.
00935   *          This parameter can be one of the following values:
00936   *            @arg RTC_FORMAT_BIN: Binary data format
00937   *            @arg RTC_FORMAT_BCD: BCD data format
00938   * @note  You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
00939   *        value in second fraction ratio with time unit following generic formula:
00940   *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
00941   *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
00942   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
00943   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
00944   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read
00945   *        to ensure consistency between the time and date values.
00946   * @retval HAL status
00947   */
00948 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
00949 {
00950   uint32_t tmpreg;
00951 
00952   /* Check the parameters */
00953   assert_param(IS_RTC_FORMAT(Format));
00954 
00955   /* Get subseconds structure field from the corresponding register*/
00956   sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
00957 
00958   /* Get SecondFraction structure field from the corresponding register field*/
00959   sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
00960 
00961   /* Get the TR register */
00962   tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
00963 
00964   /* Fill the structure fields with the read parameters */
00965   sTime->Hours      = (uint8_t)((tmpreg & (RTC_TR_HT  | RTC_TR_HU))  >> RTC_TR_HU_Pos);
00966   sTime->Minutes    = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
00967   sTime->Seconds    = (uint8_t)((tmpreg & (RTC_TR_ST  | RTC_TR_SU))  >> RTC_TR_SU_Pos);
00968   sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM))               >> RTC_TR_PM_Pos);
00969 
00970   /* Check the input parameters format */
00971   if(Format == RTC_FORMAT_BIN)
00972   {
00973     /* Convert the time structure parameters to Binary format */
00974     sTime->Hours   = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
00975     sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
00976     sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
00977   }
00978 
00979   return HAL_OK;
00980 }
00981 
00982 /**
00983   * @brief  Set RTC current date.
00984   * @param  hrtc RTC handle
00985   * @param  sDate Pointer to date structure
00986   * @param  Format specifies the format of the entered parameters.
00987   *          This parameter can be one of the following values:
00988   *            @arg RTC_FORMAT_BIN: Binary data format
00989   *            @arg RTC_FORMAT_BCD: BCD data format
00990   * @retval HAL status
00991   */
00992 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
00993 {
00994   uint32_t datetmpreg;
00995   HAL_StatusTypeDef status;
00996 
00997   /* Check the parameters */
00998   assert_param(IS_RTC_FORMAT(Format));
00999 
01000   /* Process Locked */
01001   __HAL_LOCK(hrtc);
01002 
01003   hrtc->State = HAL_RTC_STATE_BUSY;
01004 
01005   if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
01006   {
01007     sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
01008   }
01009 
01010   assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
01011 
01012   if(Format == RTC_FORMAT_BIN)
01013   {
01014     assert_param(IS_RTC_YEAR(sDate->Year));
01015     assert_param(IS_RTC_MONTH(sDate->Month));
01016     assert_param(IS_RTC_DATE(sDate->Date));
01017 
01018     datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year)  << RTC_DR_YU_Pos) | \
01019                   ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
01020                   ((uint32_t)RTC_ByteToBcd2(sDate->Date)  << RTC_DR_DU_Pos) | \
01021                   ((uint32_t)sDate->WeekDay               << RTC_DR_WDU_Pos));
01022   }
01023   else
01024   {
01025     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
01026     assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
01027     assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
01028 
01029     datetmpreg = ((((uint32_t)sDate->Year)    << RTC_DR_YU_Pos) | \
01030                   (((uint32_t)sDate->Month)   << RTC_DR_MU_Pos) | \
01031                   (((uint32_t)sDate->Date)    << RTC_DR_DU_Pos) | \
01032                   (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
01033   }
01034 
01035   /* Disable the write protection for RTC registers */
01036   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01037 
01038 
01039   /* Enter Initialization mode */
01040   status = RTC_EnterInitMode(hrtc);
01041   if (status == HAL_OK)
01042   {
01043     /* Set the RTC_DR register */
01044     hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
01045 
01046 
01047     /* Exit Initialization mode */
01048     status = RTC_ExitInitMode(hrtc);
01049   }
01050 
01051   /* Enable the write protection for RTC registers */
01052   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01053 
01054   if (status == HAL_OK)
01055   {
01056     hrtc->State = HAL_RTC_STATE_READY;
01057   }
01058 
01059   /* Process Unlocked */
01060   __HAL_UNLOCK(hrtc);
01061 
01062   return status;
01063 
01064 
01065 }
01066 
01067 /**
01068   * @brief  Get RTC current date.
01069   * @param  hrtc RTC handle
01070   * @param  sDate Pointer to Date structure
01071   * @param  Format Specifies the format of the entered parameters.
01072   *          This parameter can be one of the following values:
01073   *            @arg RTC_FORMAT_BIN:  Binary data format
01074   *            @arg RTC_FORMAT_BCD:  BCD data format
01075   * @note  You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
01076   *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
01077   *        Reading RTC current time locks the values in calendar shadow registers until Current date is read.
01078   * @retval HAL status
01079   */
01080 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
01081 {
01082   uint32_t datetmpreg;
01083 
01084   /* Check the parameters */
01085   assert_param(IS_RTC_FORMAT(Format));
01086 
01087   /* Get the DR register */
01088   datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
01089 
01090   /* Fill the structure fields with the read parameters */
01091   sDate->Year    = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
01092   sDate->Month   = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
01093   sDate->Date    = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
01094   sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU))            >> RTC_DR_WDU_Pos);
01095 
01096   /* Check the input parameters format */
01097   if(Format == RTC_FORMAT_BIN)
01098   {
01099     /* Convert the date structure parameters to Binary format */
01100     sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
01101     sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
01102     sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
01103   }
01104   return HAL_OK;
01105 }
01106 
01107 /**
01108   * @}
01109   */
01110 
01111 /** @addtogroup RTC_Exported_Functions_Group3
01112  *  @brief   RTC Alarm functions
01113  *
01114 @verbatim
01115  ===============================================================================
01116                  ##### RTC Alarm functions #####
01117  ===============================================================================
01118 
01119  [..] This section provides functions allowing to configure Alarm feature
01120 
01121 @endverbatim
01122   * @{
01123   */
01124 /**
01125   * @brief  Set the specified RTC Alarm.
01126   * @param  hrtc RTC handle
01127   * @param  sAlarm Pointer to Alarm structure
01128   * @param  Format Specifies the format of the entered parameters.
01129   *          This parameter can be one of the following values:
01130   *             @arg RTC_FORMAT_BIN: Binary data format
01131   *             @arg RTC_FORMAT_BCD: BCD data format
01132   * @retval HAL status
01133   */
01134 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
01135 {
01136   uint32_t tickstart;
01137   uint32_t tmpreg;
01138   uint32_t subsecondtmpreg;
01139 
01140   /* Check the parameters */
01141   assert_param(IS_RTC_FORMAT(Format));
01142   assert_param(IS_RTC_ALARM(sAlarm->Alarm));
01143   assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
01144   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
01145   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
01146   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
01147 
01148   /* Process Locked */
01149   __HAL_LOCK(hrtc);
01150 
01151   hrtc->State = HAL_RTC_STATE_BUSY;
01152 
01153   if(Format == RTC_FORMAT_BIN)
01154   {
01155     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
01156     {
01157       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
01158       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
01159     }
01160     else
01161     {
01162       sAlarm->AlarmTime.TimeFormat = 0x00U;
01163       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
01164     }
01165     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
01166     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
01167 
01168     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
01169     {
01170       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
01171     }
01172     else
01173     {
01174       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
01175     }
01176 
01177     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours)   << RTC_ALRMAR_HU_Pos)  | \
01178               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
01179               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos)  | \
01180               ((uint32_t)sAlarm->AlarmTime.TimeFormat              << RTC_ALRMAR_PM_Pos)  | \
01181               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay)  << RTC_ALRMAR_DU_Pos)  | \
01182               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                                     | \
01183               ((uint32_t)sAlarm->AlarmMask));
01184   }
01185   else
01186   {
01187     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
01188     {
01189       assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
01190       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
01191     }
01192     else
01193     {
01194       sAlarm->AlarmTime.TimeFormat = 0x00U;
01195       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
01196     }
01197 
01198     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
01199     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
01200 
01201     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
01202     {
01203       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
01204     }
01205     else
01206     {
01207       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
01208     }
01209 
01210     tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours       << RTC_ALRMAR_HU_Pos)  | \
01211               ((uint32_t)sAlarm->AlarmTime.Minutes     << RTC_ALRMAR_MNU_Pos) | \
01212               ((uint32_t)sAlarm->AlarmTime.Seconds     << RTC_ALRMAR_SU_Pos)  | \
01213               ((uint32_t)sAlarm->AlarmTime.TimeFormat  << RTC_ALRMAR_PM_Pos)  | \
01214               ((uint32_t)sAlarm->AlarmDateWeekDay      << RTC_ALRMAR_DU_Pos)  | \
01215               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                         | \
01216               ((uint32_t)sAlarm->AlarmMask));
01217   }
01218 
01219   /* Configure the Alarm A or Alarm B Sub Second registers */
01220   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
01221 
01222   /* Disable the write protection for RTC registers */
01223   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01224 
01225   /* Configure the Alarm register */
01226   if(sAlarm->Alarm == RTC_ALARM_A)
01227   {
01228     /* Disable the Alarm A interrupt */
01229     __HAL_RTC_ALARMA_DISABLE(hrtc);
01230     /* Clear flag alarm A */
01231     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
01232     /* In case of interrupt mode is used, the interrupt source must disabled */
01233     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
01234 
01235     tickstart = HAL_GetTick();
01236     /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
01237   #if defined(RTC_ICSR_ALRAWF)
01238     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
01239   #endif /* RTC_ICSR_ALRAWF */
01240   #if defined(RTC_ISR_ALRAWF)
01241     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
01242   #endif /* RTC_ISR_ALRAWF */
01243     {
01244       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01245       {
01246         /* Enable the write protection for RTC registers */
01247         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01248 
01249         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01250 
01251         /* Process Unlocked */
01252         __HAL_UNLOCK(hrtc);
01253 
01254         return HAL_TIMEOUT;
01255       }
01256     }
01257 
01258     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
01259     /* Configure the Alarm A Sub Second register */
01260     hrtc->Instance->ALRMASSR = subsecondtmpreg;
01261     /* Configure the Alarm state: Enable Alarm */
01262     __HAL_RTC_ALARMA_ENABLE(hrtc);
01263   }
01264   else
01265   {
01266     /* Disable the Alarm B interrupt */
01267     __HAL_RTC_ALARMB_DISABLE(hrtc);
01268     /* Clear flag alarm B */
01269     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
01270     /* In case of interrupt mode is used, the interrupt source must disabled */
01271     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
01272 
01273     tickstart = HAL_GetTick();
01274     /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
01275   #if defined(RTC_ICSR_ALRBWF)
01276     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
01277   #endif /* RTC_ICSR_ALRBWF */
01278   #if defined(RTC_ISR_ALRBWF)
01279     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
01280   #endif /* RTC_ISR_ALRBWF */
01281     {
01282       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01283       {
01284         /* Enable the write protection for RTC registers */
01285         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01286 
01287         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01288 
01289         /* Process Unlocked */
01290         __HAL_UNLOCK(hrtc);
01291 
01292         return HAL_TIMEOUT;
01293       }
01294     }
01295 
01296     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
01297     /* Configure the Alarm B Sub Second register */
01298     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
01299     /* Configure the Alarm state: Enable Alarm */
01300     __HAL_RTC_ALARMB_ENABLE(hrtc);
01301   }
01302 
01303   /* Enable the write protection for RTC registers */
01304   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01305 
01306   /* Change RTC state */
01307   hrtc->State = HAL_RTC_STATE_READY;
01308 
01309   /* Process Unlocked */
01310   __HAL_UNLOCK(hrtc);
01311 
01312   return HAL_OK;
01313 }
01314 
01315 /**
01316   * @brief  Set the specified RTC Alarm with Interrupt.
01317   * @param  hrtc RTC handle
01318   * @param  sAlarm Pointer to Alarm structure
01319   * @param  Format Specifies the format of the entered parameters.
01320   *          This parameter can be one of the following values:
01321   *             @arg RTC_FORMAT_BIN: Binary data format
01322   *             @arg RTC_FORMAT_BCD: BCD data format
01323   * @note   The Alarm register can only be written when the corresponding Alarm
01324   *         is disabled (Use the HAL_RTC_DeactivateAlarm()).
01325   * @note   The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
01326   * @retval HAL status
01327   */
01328 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
01329 {
01330   uint32_t tickstart;
01331   uint32_t tmpreg;
01332   uint32_t subsecondtmpreg;
01333 
01334   /* Check the parameters */
01335   assert_param(IS_RTC_FORMAT(Format));
01336   assert_param(IS_RTC_ALARM(sAlarm->Alarm));
01337   assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
01338   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
01339   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
01340   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
01341 
01342   /* Process Locked */
01343   __HAL_LOCK(hrtc);
01344 
01345   hrtc->State = HAL_RTC_STATE_BUSY;
01346 
01347   if(Format == RTC_FORMAT_BIN)
01348   {
01349     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
01350     {
01351       assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
01352       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
01353     }
01354     else
01355     {
01356       sAlarm->AlarmTime.TimeFormat = 0x00U;
01357       assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
01358     }
01359     assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
01360     assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
01361 
01362     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
01363     {
01364       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
01365     }
01366     else
01367     {
01368       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
01369     }
01370 
01371     tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours)   << RTC_ALRMAR_HU_Pos)  | \
01372               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
01373               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos)  | \
01374               ((uint32_t)sAlarm->AlarmTime.TimeFormat              << RTC_ALRMAR_PM_Pos)  | \
01375               ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay)  << RTC_ALRMAR_DU_Pos)  | \
01376               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                                     | \
01377               ((uint32_t)sAlarm->AlarmMask));
01378   }
01379   else
01380   {
01381     if((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
01382     {
01383       assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
01384       assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
01385     }
01386     else
01387     {
01388       sAlarm->AlarmTime.TimeFormat = 0x00U;
01389       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
01390     }
01391 
01392     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
01393     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
01394 
01395     if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
01396     {
01397       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
01398     }
01399     else
01400     {
01401       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
01402     }
01403 
01404     tmpreg = (((uint32_t)sAlarm->AlarmTime.Hours       << RTC_ALRMAR_HU_Pos)  | \
01405               ((uint32_t)sAlarm->AlarmTime.Minutes     << RTC_ALRMAR_MNU_Pos) | \
01406               ((uint32_t)sAlarm->AlarmTime.Seconds     << RTC_ALRMAR_SU_Pos)  | \
01407               ((uint32_t)sAlarm->AlarmTime.TimeFormat  << RTC_ALRMAR_PM_Pos)  | \
01408               ((uint32_t)sAlarm->AlarmDateWeekDay      << RTC_ALRMAR_DU_Pos)  | \
01409               ((uint32_t)sAlarm->AlarmDateWeekDaySel)                         | \
01410               ((uint32_t)sAlarm->AlarmMask));
01411   }
01412   /* Configure the Alarm A or Alarm B Sub Second registers */
01413   subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
01414 
01415   /* Disable the write protection for RTC registers */
01416   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01417 
01418   /* Configure the Alarm register */
01419   if(sAlarm->Alarm == RTC_ALARM_A)
01420   {
01421     /* Disable the Alarm A interrupt */
01422     __HAL_RTC_ALARMA_DISABLE(hrtc);
01423 
01424     /* Clear flag alarm A */
01425     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
01426 
01427     tickstart = HAL_GetTick();
01428     /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
01429   #if defined(RTC_ICSR_ALRAWF)
01430     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
01431   #endif /* RTC_ICSR_ALRAWF */
01432   #if defined(RTC_ISR_ALRAWF)
01433     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
01434   #endif /* RTC_ISR_ALRAWF */
01435     {
01436       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01437       {
01438         /* Enable the write protection for RTC registers */
01439         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01440 
01441         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01442 
01443         /* Process Unlocked */
01444         __HAL_UNLOCK(hrtc);
01445 
01446         return HAL_TIMEOUT;
01447       }
01448     }
01449 
01450     hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
01451     /* Configure the Alarm A Sub Second register */
01452     hrtc->Instance->ALRMASSR = subsecondtmpreg;
01453     /* Configure the Alarm state: Enable Alarm */
01454     __HAL_RTC_ALARMA_ENABLE(hrtc);
01455     /* Configure the Alarm interrupt */
01456     __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
01457   }
01458   else
01459   {
01460     /* Disable the Alarm B interrupt */
01461     __HAL_RTC_ALARMB_DISABLE(hrtc);
01462 
01463     /* Clear flag alarm B */
01464     __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
01465 
01466     tickstart = HAL_GetTick();
01467     /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
01468 #if defined(RTC_ICSR_ALRBWF)
01469     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
01470 #endif /* RTC_ICSR_ALRBWF */
01471 #if defined(RTC_ISR_ALRBWF)
01472     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
01473 #endif /* RTC_ISR_ALRBWF */
01474     {
01475       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01476       {
01477         /* Enable the write protection for RTC registers */
01478         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01479 
01480         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01481 
01482         /* Process Unlocked */
01483         __HAL_UNLOCK(hrtc);
01484 
01485         return HAL_TIMEOUT;
01486       }
01487     }
01488 
01489     hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
01490     /* Configure the Alarm B Sub Second register */
01491     hrtc->Instance->ALRMBSSR = subsecondtmpreg;
01492     /* Configure the Alarm state: Enable Alarm */
01493     __HAL_RTC_ALARMB_ENABLE(hrtc);
01494     /* Configure the Alarm interrupt */
01495     __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
01496   }
01497 
01498   /* RTC Alarm Interrupt Configuration: EXTI configuration */
01499 #if defined(DUAL_CORE)
01500   if (HAL_GetCurrentCPUID() == CM7_CPUID)
01501   {
01502     __HAL_RTC_ALARM_EXTI_ENABLE_IT();
01503   }
01504   else
01505   {
01506     __HAL_RTC_ALARM_EXTID2_ENABLE_IT();
01507   }
01508 #else  /* SINGLE_CORE */
01509   __HAL_RTC_ALARM_EXTI_ENABLE_IT();
01510 #endif
01511 
01512   __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
01513 
01514   /* Enable the write protection for RTC registers */
01515   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01516 
01517   hrtc->State = HAL_RTC_STATE_READY;
01518 
01519   /* Process Unlocked */
01520   __HAL_UNLOCK(hrtc);
01521 
01522   return HAL_OK;
01523 }
01524 
01525 /**
01526   * @brief  Deactivate the specified RTC Alarm.
01527   * @param  hrtc RTC handle
01528   * @param  Alarm Specifies the Alarm.
01529   *          This parameter can be one of the following values:
01530   *            @arg RTC_ALARM_A:  AlarmA
01531   *            @arg RTC_ALARM_B:  AlarmB
01532   * @retval HAL status
01533   */
01534 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
01535 {
01536   uint32_t tickstart;
01537 
01538   /* Check the parameters */
01539   assert_param(IS_RTC_ALARM(Alarm));
01540 
01541   /* Process Locked */
01542   __HAL_LOCK(hrtc);
01543 
01544   hrtc->State = HAL_RTC_STATE_BUSY;
01545 
01546   /* Disable the write protection for RTC registers */
01547   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01548 
01549   if(Alarm == RTC_ALARM_A)
01550   {
01551     /* AlarmA */
01552     __HAL_RTC_ALARMA_DISABLE(hrtc);
01553 
01554     /* In case of interrupt mode is used, the interrupt source must disabled */
01555     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
01556 
01557     tickstart = HAL_GetTick();
01558 
01559     /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
01560 #if defined(RTC_ICSR_ALRAWF)
01561     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRAWF) == 0U)
01562 #endif /* RTC_ICSR_ALRAWF */
01563 #if defined(RTC_ISR_ALRAWF)
01564     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
01565 #endif /* RTC_ISR_ALRAWF */
01566     {
01567       if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
01568       {
01569         /* Enable the write protection for RTC registers */
01570         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01571 
01572         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01573 
01574         /* Process Unlocked */
01575         __HAL_UNLOCK(hrtc);
01576 
01577         return HAL_TIMEOUT;
01578       }
01579     }
01580   }
01581   else
01582   {
01583     /* AlarmB */
01584     __HAL_RTC_ALARMB_DISABLE(hrtc);
01585 
01586     /* In case of interrupt mode is used, the interrupt source must disabled */
01587     __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
01588 
01589     tickstart = HAL_GetTick();
01590 
01591     /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
01592 #if defined(RTC_ICSR_ALRBWF)
01593     while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_ALRBWF) == 0U)
01594 #endif /* RTC_ICSR_ALRBWF */
01595 #if defined(RTC_ISR_ALRBWF)
01596     while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
01597 #endif /* RTC_ISR_ALRBWF */
01598     {
01599       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01600       {
01601         /* Enable the write protection for RTC registers */
01602         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01603 
01604         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01605 
01606         /* Process Unlocked */
01607         __HAL_UNLOCK(hrtc);
01608 
01609         return HAL_TIMEOUT;
01610       }
01611     }
01612   }
01613   /* Enable the write protection for RTC registers */
01614   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01615 
01616   hrtc->State = HAL_RTC_STATE_READY;
01617 
01618   /* Process Unlocked */
01619   __HAL_UNLOCK(hrtc);
01620 
01621   return HAL_OK;
01622 }
01623 
01624 /**
01625   * @brief  Get the RTC Alarm value and masks.
01626   * @param  hrtc RTC handle
01627   * @param  sAlarm Pointer to Date structure
01628   * @param  Alarm Specifies the Alarm.
01629   *          This parameter can be one of the following values:
01630   *             @arg RTC_ALARM_A: AlarmA
01631   *             @arg RTC_ALARM_B: AlarmB
01632   * @param  Format Specifies the format of the entered parameters.
01633   *          This parameter can be one of the following values:
01634   *             @arg RTC_FORMAT_BIN: Binary data format
01635   *             @arg RTC_FORMAT_BCD: BCD data format
01636   * @retval HAL status
01637   */
01638 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
01639 {
01640   uint32_t tmpreg;
01641   uint32_t subsecondtmpreg;
01642 
01643   /* Check the parameters */
01644   assert_param(IS_RTC_FORMAT(Format));
01645   assert_param(IS_RTC_ALARM(Alarm));
01646 
01647   if(Alarm == RTC_ALARM_A)
01648   {
01649     /* AlarmA */
01650     sAlarm->Alarm = RTC_ALARM_A;
01651 
01652     tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
01653     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
01654 
01655     /* Fill the structure with the read parameters */
01656     sAlarm->AlarmTime.Hours      = (uint8_t)((tmpreg & (RTC_ALRMAR_HT  | RTC_ALRMAR_HU))  >> RTC_ALRMAR_HU_Pos);
01657     sAlarm->AlarmTime.Minutes    = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
01658     sAlarm->AlarmTime.Seconds    = (uint8_t)((tmpreg & (RTC_ALRMAR_ST  | RTC_ALRMAR_SU))  >> RTC_ALRMAR_SU_Pos);
01659     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg &  RTC_ALRMAR_PM)                    >> RTC_ALRMAR_PM_Pos);
01660     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
01661     sAlarm->AlarmDateWeekDay     = (uint8_t)((tmpreg & (RTC_ALRMAR_DT  | RTC_ALRMAR_DU))  >> RTC_ALRMAR_DU_Pos);
01662     sAlarm->AlarmDateWeekDaySel  = (uint32_t)(tmpreg &  RTC_ALRMAR_WDSEL);
01663     sAlarm->AlarmMask            = (uint32_t)(tmpreg &  RTC_ALARMMASK_ALL);
01664   }
01665   else
01666   {
01667     sAlarm->Alarm = RTC_ALARM_B;
01668 
01669     tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
01670     subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
01671 
01672     /* Fill the structure with the read parameters */
01673     sAlarm->AlarmTime.Hours      = (uint8_t)((tmpreg & (RTC_ALRMBR_HT  | RTC_ALRMBR_HU))  >> RTC_ALRMBR_HU_Pos);
01674     sAlarm->AlarmTime.Minutes    = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
01675     sAlarm->AlarmTime.Seconds    = (uint8_t)((tmpreg & (RTC_ALRMBR_ST  | RTC_ALRMBR_SU))  >> RTC_ALRMBR_SU_Pos);
01676     sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg &  RTC_ALRMBR_PM)                    >> RTC_ALRMBR_PM_Pos);
01677     sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
01678     sAlarm->AlarmDateWeekDay     = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU))   >> RTC_ALRMBR_DU_Pos);
01679     sAlarm->AlarmDateWeekDaySel  = (uint32_t)(tmpreg &  RTC_ALRMBR_WDSEL);
01680     sAlarm->AlarmMask            = (uint32_t)(tmpreg &  RTC_ALARMMASK_ALL);
01681   }
01682 
01683   if(Format == RTC_FORMAT_BIN)
01684   {
01685     sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
01686     sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
01687     sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
01688     sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
01689   }
01690 
01691   return HAL_OK;
01692 }
01693 
01694 /**
01695   * @brief  Handle Alarm interrupt request.
01696   * @param  hrtc RTC handle
01697   * @retval None
01698   */
01699 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
01700 {
01701   /* Clear the EXTI's line Flag for RTC Alarm */
01702 #if defined(DUAL_CORE)
01703   if(HAL_GetCurrentCPUID() == CM7_CPUID)
01704   {
01705     __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
01706   }
01707   else
01708   {
01709     __HAL_RTC_ALARM_EXTID2_CLEAR_FLAG();
01710   }
01711 #else  /* SINGLE_CORE */
01712   __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
01713 #endif /* DUAL_CORE */
01714 
01715 #if defined(RTC_MISR_ALRAMF)
01716   /* Get interrupt status */
01717   uint32_t tmp = hrtc->Instance->MISR;
01718 
01719   if((tmp & RTC_MISR_ALRAMF) != 0u)
01720   {
01721     /* Clear the AlarmA interrupt pending bit */
01722     hrtc->Instance->SCR = RTC_SCR_CALRAF;
01723 
01724 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
01725     /* Call Compare Match registered Callback */
01726     hrtc->AlarmAEventCallback(hrtc);
01727 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01728     HAL_RTC_AlarmAEventCallback(hrtc);
01729 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01730   }
01731 #endif /* RTC_MISR_ALRAMF */
01732 
01733 #if defined(RTC_MISR_ALRBMF)
01734   if((tmp & RTC_MISR_ALRBMF) != 0u)
01735   {
01736     /* Clear the AlarmB interrupt pending bit */
01737     hrtc->Instance->SCR = RTC_SCR_CALRBF;
01738 
01739 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
01740     /* Call Compare Match registered Callback */
01741     hrtc->AlarmBEventCallback(hrtc);
01742 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01743     HAL_RTCEx_AlarmBEventCallback(hrtc);
01744 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01745   }
01746 #endif /* RTC_MISR_ALRBMF */
01747 
01748 #if defined(RTC_ISR_ALRAF)
01749   /* Get the AlarmA interrupt source enable status */
01750   if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
01751   {
01752     /* Get the pending status of the AlarmA Interrupt */
01753     if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
01754     {
01755       /* Clear the AlarmA interrupt pending bit */
01756       __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
01757 
01758 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
01759       hrtc->AlarmAEventCallback(hrtc);
01760 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01761       HAL_RTC_AlarmAEventCallback(hrtc);
01762 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01763     }
01764   }
01765 #endif /* RTC_ISR_ALRAF */
01766 
01767 #if defined(RTC_ISR_ALRBF)
01768   /* Get the AlarmB interrupt source enable status */
01769   if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
01770   {
01771     /* Get the pending status of the AlarmB Interrupt */
01772     if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
01773     {
01774       /* Clear the AlarmB interrupt pending bit */
01775       __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
01776 
01777       /* AlarmB callback */
01778 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
01779       hrtc->AlarmBEventCallback(hrtc);
01780 #else  /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01781       HAL_RTCEx_AlarmBEventCallback(hrtc);
01782 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
01783     }
01784   }
01785 #endif /* RTC_ISR_ALRBF */
01786 
01787   /* Change RTC state */
01788   hrtc->State = HAL_RTC_STATE_READY;
01789 }
01790 
01791 /**
01792   * @brief  Alarm A callback.
01793   * @param  hrtc RTC handle
01794   * @retval None
01795   */
01796 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
01797 {
01798   /* Prevent unused argument(s) compilation warning */
01799   UNUSED(hrtc);
01800 
01801   /* NOTE : This function should not be modified, when the callback is needed,
01802             the HAL_RTC_AlarmAEventCallback could be implemented in the user file
01803    */
01804 }
01805 
01806 /**
01807   * @brief  Handle AlarmA Polling request.
01808   * @param  hrtc RTC handle
01809   * @param  Timeout Timeout duration
01810   * @retval HAL status
01811   */
01812 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
01813 {
01814 
01815   uint32_t tickstart = HAL_GetTick();
01816 
01817   while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
01818   {
01819     if(Timeout != HAL_MAX_DELAY)
01820     {
01821       if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
01822       {
01823         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01824         return HAL_TIMEOUT;
01825       }
01826     }
01827   }
01828 
01829   /* Clear the Alarm interrupt pending bit */
01830   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
01831 
01832   /* Change RTC state */
01833   hrtc->State = HAL_RTC_STATE_READY;
01834 
01835   return HAL_OK;
01836 }
01837 
01838 /**
01839   * @}
01840   */
01841 
01842 /** @addtogroup RTC_Exported_Functions_Group4
01843  *  @brief   Peripheral Control functions
01844  *
01845 @verbatim
01846  ===============================================================================
01847                      ##### Peripheral Control functions #####
01848  ===============================================================================
01849     [..]
01850     This subsection provides functions allowing to
01851       (+) Wait for RTC Time and Date Synchronization
01852 
01853 @endverbatim
01854   * @{
01855   */
01856 
01857 /**
01858   * @brief  Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
01859   *         synchronized with RTC APB clock.
01860   * @note   The RTC Resynchronization mode is write protected, use the
01861   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
01862   * @note   To read the calendar through the shadow registers after Calendar
01863   *         initialization, calendar update or after wakeup from low power modes
01864   *         the software must first clear the RSF flag.
01865   *         The software must then wait until it is set again before reading
01866   *         the calendar, which means that the calendar registers have been
01867   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
01868   * @param  hrtc RTC handle
01869   * @retval HAL status
01870   */
01871 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
01872 {
01873   uint32_t tickstart;
01874 
01875   /* Clear RSF flag */
01876 #if defined(RTC_ICSR_RSF)
01877   hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK;
01878 #endif /* RTC_ICSR_RSF */
01879 #if defined(RTC_ISR_RSF)
01880   hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
01881 #endif /* RTC_ISR_RSF */
01882 
01883   tickstart = HAL_GetTick();
01884 
01885   /* Wait the registers to be synchronised */
01886 #if defined(RTC_ICSR_RSF)
01887   while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
01888 #endif /* RTC_ICSR_RSF */
01889 #if defined(RTC_ISR_RSF)
01890     while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
01891 #endif /* RTC_ISR_RSF */
01892     {
01893       if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
01894       {
01895         return HAL_TIMEOUT;
01896       }
01897     }
01898 
01899   return HAL_OK;
01900 }
01901 
01902 /**
01903   * @}
01904   */
01905 
01906 /** @addtogroup RTC_Exported_Functions_Group5
01907  *  @brief   Peripheral State functions
01908  *
01909 @verbatim
01910  ===============================================================================
01911                      ##### Peripheral State functions #####
01912  ===============================================================================
01913     [..]
01914     This subsection provides functions allowing to
01915       (+) Get RTC state
01916 
01917 @endverbatim
01918   * @{
01919   */
01920 /**
01921   * @brief  Return the RTC handle state.
01922   * @param  hrtc RTC handle
01923   * @retval HAL state
01924   */
01925 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
01926 {
01927   /* Return RTC handle state */
01928   return hrtc->State;
01929 }
01930 
01931 /**
01932   * @}
01933   */
01934 
01935 /**
01936   * @}
01937   */
01938 
01939 /** @addtogroup RTC_Private_Functions
01940   * @{
01941   */
01942 /**
01943   * @brief  Enter the RTC Initialization mode.
01944   * @note   The RTC Initialization mode is write protected, use the
01945   *         __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
01946   * @param  hrtc RTC handle
01947   * @retval HAL status
01948   */
01949 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
01950 {
01951   uint32_t tickstart;
01952   HAL_StatusTypeDef status = HAL_OK;
01953   /* Check if the Initialization mode is set */
01954 #if defined(RTC_ICSR_INITF)
01955   if((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
01956   {
01957     /* Set the Initialization mode */
01958     SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
01959 
01960     tickstart = HAL_GetTick();
01961     /* Wait till RTC is in INIT state and if Time out is reached exit */
01962     while (((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
01963     {
01964       if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
01965       {
01966         status = HAL_TIMEOUT;
01967         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01968       }
01969     }
01970   }
01971 #endif /* RTC_ICSR_INITF */
01972 #if defined(RTC_ISR_INITF)
01973   if((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
01974   {
01975     /* Set the Initialization mode */
01976     hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
01977 
01978     tickstart = HAL_GetTick();
01979     /* Wait till RTC is in INIT state and if Time out is reached exit */
01980     while (((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U) && (status != HAL_TIMEOUT))
01981     {
01982       if((HAL_GetTick()  - tickstart) > RTC_TIMEOUT_VALUE)
01983       {
01984         status = HAL_TIMEOUT;
01985         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01986       }
01987     }
01988   }
01989 #endif /* RTC_ISR_INITF */
01990 
01991   return status;
01992 }
01993 
01994 /**
01995   * @brief  Exit the RTC Initialization mode.
01996   * @param  hrtc RTC handle
01997   * @retval HAL status
01998   */
01999 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
02000 {
02001   HAL_StatusTypeDef status = HAL_OK;
02002 
02003   /* Check if the Initialization mode is set */
02004 #if defined(RTC_ICSR_INITF)
02005 
02006   /* Exit Initialization mode */
02007   CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
02008 
02009 #elif defined(RTC_ISR_INITF)
02010 
02011   /* Exit Initialization mode */
02012   CLEAR_BIT(RTC->ISR, RTC_ISR_INITF);
02013 
02014 #endif /* RTC_ISR_INITF */
02015   /* If CR_BYPSHAD bit = 0, wait for synchro */
02016   if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
02017   {
02018     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
02019     {
02020       hrtc->State = HAL_RTC_STATE_TIMEOUT;
02021       status = HAL_TIMEOUT;
02022     }
02023   }
02024   else
02025   {
02026     /* Clear BYPSHAD bit */
02027     CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
02028     if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
02029     {
02030       hrtc->State = HAL_RTC_STATE_TIMEOUT;
02031       status = HAL_TIMEOUT;
02032     }
02033     /* Restore BYPSHAD bit */
02034     SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
02035   }
02036 
02037   return status;
02038 }
02039 
02040 /**
02041   * @brief  Convert a 2 digit decimal to BCD format.
02042   * @param  Value Byte to be converted
02043   * @retval Converted byte
02044   */
02045 uint8_t RTC_ByteToBcd2(uint8_t Value)
02046 {
02047   uint32_t bcdhigh = 0U;
02048   uint8_t  bcdlow  = Value;
02049 
02050   while (bcdlow >= 10U)
02051   {
02052     bcdhigh++;
02053     bcdlow -= 10U;
02054   }
02055 
02056   return ((uint8_t)(bcdhigh << 4U) | bcdlow);
02057 }
02058 
02059 /**
02060   * @brief  Convert from 2 digit BCD to Binary.
02061   * @param  Value BCD value to be converted
02062   * @retval Converted word
02063   */
02064 uint8_t RTC_Bcd2ToByte(uint8_t Value)
02065 {
02066   uint8_t tmp;
02067   tmp = ((Value & 0xF0U) >> 4U) * 10U;
02068   return (tmp + (Value & 0x0FU));
02069 }
02070 
02071 /**
02072   * @}
02073   */
02074 
02075 #endif /* HAL_RTC_MODULE_ENABLED */
02076 /**
02077   * @}
02078   */
02079 
02080 /**
02081   * @}
02082   */
02083