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