TUKS MCU Introductory course / TUKS-COURSE-2-LED
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_hal_rtc.c Source File

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