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