STM32F479xx HAL User Manual
stm32f4xx_hal_rtc_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_rtc_ex.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) Extension peripheral:
00008   *           + RTC Time Stamp functions
00009   *           + RTC Tamper functions
00010   *           + RTC Wake-up functions
00011   *           + Extension Control functions
00012   *           + Extension RTC features functions
00013   *
00014   @verbatim
00015   ==============================================================================
00016                   ##### How to use this driver #####
00017   ==============================================================================
00018   [..]
00019     (+) Enable the RTC domain access.
00020     (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
00021         format using the HAL_RTC_Init() function.
00022 
00023   *** RTC Wake-up configuration ***
00024   ================================
00025   [..]
00026     (+) To configure the RTC Wake-up Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
00027         function. You can also configure the RTC Wake-up timer in interrupt mode
00028         using the HAL_RTCEx_SetWakeUpTimer_IT() function.
00029     (+) To read the RTC Wake-up Counter register, use the HAL_RTCEx_GetWakeUpTimer()
00030         function.
00031 
00032   *** TimeStamp configuration ***
00033   ===============================
00034   [..]
00035     (+) Configure the RTC_AFx trigger and enable the RTC TimeStamp using the
00036         HAL_RTCEx_SetTimeStamp() function. You can also configure the RTC TimeStamp with
00037         interrupt mode using the HAL_RTCEx_SetTimeStamp_IT() function.
00038     (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
00039         function.
00040     (+) The TIMESTAMP alternate function can be mapped either to RTC_AF1 (PC13)
00041         or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TSINSEL bit in
00042         RTC_TAFCR register. The corresponding pin is also selected by HAL_RTCEx_SetTimeStamp()
00043         or HAL_RTCEx_SetTimeStamp_IT() function.
00044 
00045   *** Tamper configuration ***
00046   ============================
00047   [..]
00048     (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
00049         or Level according to the Tamper filter (if equal to 0 Edge else Level)
00050         value, sampling frequency, precharge or discharge and Pull-UP using the
00051         HAL_RTCEx_SetTamper() function. You can configure RTC Tamper in interrupt
00052         mode using HAL_RTCEx_SetTamper_IT() function.
00053     (+) The TAMPER1 alternate function can be mapped either to RTC_AF1 (PC13)
00054         or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TAMP1INSEL bit in
00055         RTC_TAFCR register. The corresponding pin is also selected by HAL_RTCEx_SetTamper()
00056         or HAL_RTCEx_SetTamper_IT() function.
00057 
00058   *** Backup Data Registers configuration ***
00059   ===========================================
00060   [..]
00061     (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
00062         function.
00063     (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
00064         function.
00065 
00066    @endverbatim
00067   ******************************************************************************
00068   * @attention
00069   *
00070   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
00071   * All rights reserved.</center></h2>
00072   *
00073   * This software component is licensed by ST under BSD 3-Clause license,
00074   * the "License"; You may not use this file except in compliance with the
00075   * License. You may obtain a copy of the License at:
00076   *                        opensource.org/licenses/BSD-3-Clause
00077   *
00078   ******************************************************************************
00079   */
00080 
00081 /* Includes ------------------------------------------------------------------*/
00082 #include "stm32f4xx_hal.h"
00083 
00084 /** @addtogroup STM32F4xx_HAL_Driver
00085   * @{
00086   */
00087 
00088 /** @defgroup RTCEx RTCEx
00089   * @brief RTC HAL module driver
00090   * @{
00091   */
00092 
00093 #ifdef HAL_RTC_MODULE_ENABLED
00094 
00095 /* Private typedef -----------------------------------------------------------*/
00096 /* Private define ------------------------------------------------------------*/
00097 /* Private macro -------------------------------------------------------------*/
00098 /* Private variables ---------------------------------------------------------*/
00099 /* Private function prototypes -----------------------------------------------*/
00100 /* Private functions ---------------------------------------------------------*/
00101 
00102 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
00103   * @{
00104   */
00105 
00106 /** @defgroup RTCEx_Exported_Functions_Group1 RTC TimeStamp and Tamper functions
00107  *  @brief   RTC TimeStamp and Tamper functions
00108  *
00109 @verbatim
00110  ===============================================================================
00111                  ##### RTC TimeStamp and Tamper functions #####
00112  ===============================================================================
00113 
00114  [..] This section provides functions allowing to configure TimeStamp feature
00115 
00116 @endverbatim
00117   * @{
00118   */
00119 
00120 /**
00121   * @brief  Sets TimeStamp.
00122   * @note   This API must be called before enabling the TimeStamp feature.
00123   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00124   *                the configuration information for RTC.
00125   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
00126   *         activated.
00127   *          This parameter can be one of the following values:
00128   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
00129   *                                        rising edge of the related pin.
00130   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
00131   *                                         falling edge of the related pin.
00132   * @param  RTC_TimeStampPin specifies the RTC TimeStamp Pin.
00133   *          This parameter can be one of the following values:
00134   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
00135   *             @arg RTC_TIMESTAMPPIN_POS1: PI8/PA0 is selected as RTC TimeStamp Pin.
00136   *             (not applicable in the case of STM32F412xx, STM32F413xx and STM32F423xx devices)
00137   *             (PI8 for all STM32 devices except for STM32F446xx devices the PA0 is used)
00138   *             @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
00139   * @retval HAL status
00140   */
00141 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
00142 {
00143   uint32_t tmpreg = 0U;
00144 
00145   /* Check the parameters */
00146   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
00147   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
00148 
00149   /* Process Locked */
00150   __HAL_LOCK(hrtc);
00151 
00152   hrtc->State = HAL_RTC_STATE_BUSY;
00153 
00154   /* Get the RTC_CR register and clear the bits to be configured */
00155   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
00156 
00157   tmpreg|= TimeStampEdge;
00158 
00159   /* Disable the write protection for RTC registers */
00160   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00161 
00162   hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
00163   hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
00164 
00165   /* Configure the Time Stamp TSEDGE and Enable bits */
00166   hrtc->Instance->CR = (uint32_t)tmpreg;
00167 
00168   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
00169 
00170   /* Enable the write protection for RTC registers */
00171   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00172 
00173   /* Change RTC state */
00174   hrtc->State = HAL_RTC_STATE_READY;
00175 
00176   /* Process Unlocked */
00177   __HAL_UNLOCK(hrtc);
00178 
00179   return HAL_OK;
00180 }
00181 
00182 /**
00183   * @brief  Sets TimeStamp with Interrupt.
00184   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00185   *                the configuration information for RTC.
00186   * @note   This API must be called before enabling the TimeStamp feature.
00187   * @param  TimeStampEdge Specifies the pin edge on which the TimeStamp is
00188   *         activated.
00189   *          This parameter can be one of the following values:
00190   *             @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
00191   *                                        rising edge of the related pin.
00192   *             @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
00193   *                                         falling edge of the related pin.
00194   * @param  RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
00195   *          This parameter can be one of the following values:
00196   *             @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
00197   *             @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin. (not applicable in the case of STM32F446xx, STM32F412xx, STM32F413xx and STM32F423xx devices)
00198   *             @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
00199   * @retval HAL status
00200   */
00201 HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
00202 {
00203   uint32_t tmpreg = 0U;
00204 
00205   /* Check the parameters */
00206   assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
00207   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
00208 
00209   /* Process Locked */
00210   __HAL_LOCK(hrtc);
00211 
00212   hrtc->State = HAL_RTC_STATE_BUSY;
00213 
00214   /* Get the RTC_CR register and clear the bits to be configured */
00215   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
00216 
00217   tmpreg |= TimeStampEdge;
00218 
00219   /* Disable the write protection for RTC registers */
00220   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00221 
00222   /* Configure the Time Stamp TSEDGE and Enable bits */
00223   hrtc->Instance->CR = (uint32_t)tmpreg;
00224 
00225   hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
00226   hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
00227 
00228   /* Clear RTC Timestamp flag */
00229   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
00230 
00231   __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
00232 
00233   /* Enable IT timestamp */
00234   __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
00235 
00236   /* RTC timestamp Interrupt Configuration: EXTI configuration */
00237   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
00238 
00239   EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
00240 
00241   /* Enable the write protection for RTC registers */
00242   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00243 
00244   hrtc->State = HAL_RTC_STATE_READY;
00245 
00246   /* Process Unlocked */
00247   __HAL_UNLOCK(hrtc);
00248 
00249   return HAL_OK;
00250 }
00251 
00252 /**
00253   * @brief  Deactivates TimeStamp.
00254   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00255   *                the configuration information for RTC.
00256   * @retval HAL status
00257   */
00258 HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
00259 {
00260   uint32_t tmpreg = 0U;
00261 
00262   /* Process Locked */
00263   __HAL_LOCK(hrtc);
00264 
00265   hrtc->State = HAL_RTC_STATE_BUSY;
00266 
00267   /* Disable the write protection for RTC registers */
00268   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00269 
00270   /* In case of interrupt mode is used, the interrupt source must disabled */
00271   __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
00272 
00273   /* Get the RTC_CR register and clear the bits to be configured */
00274   tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
00275 
00276   /* Configure the Time Stamp TSEDGE and Enable bits */
00277   hrtc->Instance->CR = (uint32_t)tmpreg;
00278 
00279   /* Enable the write protection for RTC registers */
00280   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00281 
00282   hrtc->State = HAL_RTC_STATE_READY;
00283 
00284   /* Process Unlocked */
00285   __HAL_UNLOCK(hrtc);
00286 
00287   return HAL_OK;
00288 }
00289 
00290 /**
00291   * @brief  Gets the RTC TimeStamp value.
00292   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00293   *                the configuration information for RTC.
00294   * @param  sTimeStamp Pointer to Time structure
00295   * @param  sTimeStampDate Pointer to Date structure
00296   * @param  Format specifies the format of the entered parameters.
00297   *          This parameter can be one of the following values:
00298   *             RTC_FORMAT_BIN: Binary data format
00299   *             RTC_FORMAT_BCD: BCD data format
00300   * @retval HAL status
00301   */
00302 HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
00303 {
00304   uint32_t tmptime = 0U, tmpdate = 0U;
00305 
00306   /* Check the parameters */
00307   assert_param(IS_RTC_FORMAT(Format));
00308 
00309   /* Get the TimeStamp time and date registers values */
00310   tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
00311   tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
00312 
00313   /* Fill the Time structure fields with the read parameters */
00314   sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
00315   sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
00316   sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
00317   sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16U);
00318   sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
00319 
00320   /* Fill the Date structure fields with the read parameters */
00321   sTimeStampDate->Year = 0U;
00322   sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
00323   sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
00324   sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13U);
00325 
00326   /* Check the input parameters format */
00327   if(Format == RTC_FORMAT_BIN)
00328   {
00329     /* Convert the TimeStamp structure parameters to Binary format */
00330     sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
00331     sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
00332     sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
00333 
00334     /* Convert the DateTimeStamp structure parameters to Binary format */
00335     sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
00336     sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
00337     sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
00338   }
00339 
00340   /* Clear the TIMESTAMP Flag */
00341   __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
00342 
00343   return HAL_OK;
00344 }
00345 
00346 /**
00347   * @brief  Sets Tamper
00348   * @note   By calling this API we disable the tamper interrupt for all tampers.
00349   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00350   *                the configuration information for RTC.
00351   * @param  sTamper Pointer to Tamper Structure.
00352   * @retval HAL status
00353   */
00354 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
00355 {
00356   uint32_t tmpreg = 0U;
00357 
00358   /* Check the parameters */
00359   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
00360   assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
00361   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
00362   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
00363   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
00364   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
00365   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
00366   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
00367 
00368   /* Process Locked */
00369   __HAL_LOCK(hrtc);
00370 
00371   hrtc->State = HAL_RTC_STATE_BUSY;
00372 
00373   if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
00374   {
00375     sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1U);
00376   }
00377 
00378   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger  |\
00379             (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
00380             (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
00381 
00382   hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1U) | (uint32_t)RTC_TAFCR_TAMPTS |\
00383                                        (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
00384                                        (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL | (uint32_t)RTC_TAFCR_TAMPIE);
00385 
00386   hrtc->Instance->TAFCR |= tmpreg;
00387 
00388   hrtc->State = HAL_RTC_STATE_READY;
00389 
00390   /* Process Unlocked */
00391   __HAL_UNLOCK(hrtc);
00392 
00393   return HAL_OK;
00394 }
00395 
00396 /**
00397   * @brief  Sets Tamper with interrupt.
00398   * @note   By calling this API we force the tamper interrupt for all tampers.
00399   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00400   *                the configuration information for RTC.
00401   * @param  sTamper Pointer to RTC Tamper.
00402   * @retval HAL status
00403   */
00404 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
00405 {
00406   uint32_t tmpreg = 0U;
00407 
00408   /* Check the parameters */
00409   assert_param(IS_RTC_TAMPER(sTamper->Tamper));
00410   assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
00411   assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
00412   assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
00413   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
00414   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
00415   assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
00416   assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
00417 
00418   /* Process Locked */
00419   __HAL_LOCK(hrtc);
00420 
00421   hrtc->State = HAL_RTC_STATE_BUSY;
00422 
00423   /* Configure the tamper trigger */
00424   if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
00425   {
00426     sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1U);
00427   }
00428 
00429   tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger  |\
00430             (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
00431             (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
00432 
00433   hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1U) | (uint32_t)RTC_TAFCR_TAMPTS |\
00434                                        (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
00435                                        (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL);
00436 
00437   hrtc->Instance->TAFCR |= tmpreg;
00438 
00439   /* Configure the Tamper Interrupt in the RTC_TAFCR */
00440   hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
00441 
00442   if(sTamper->Tamper == RTC_TAMPER_1)
00443   {
00444     /* Clear RTC Tamper 1 flag */
00445     __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
00446   }
00447   else
00448   {
00449     /* Clear RTC Tamper 2 flag */
00450     __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
00451   }
00452 
00453   /* RTC Tamper Interrupt Configuration: EXTI configuration */
00454   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
00455 
00456   EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
00457 
00458   hrtc->State = HAL_RTC_STATE_READY;
00459 
00460   /* Process Unlocked */
00461   __HAL_UNLOCK(hrtc);
00462 
00463   return HAL_OK;
00464 }
00465 
00466 /**
00467   * @brief  Deactivates Tamper.
00468   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00469   *                the configuration information for RTC.
00470   * @param  Tamper Selected tamper pin.
00471   *          This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
00472   * @retval HAL status
00473   */
00474 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
00475 {
00476   assert_param(IS_RTC_TAMPER(Tamper));
00477 
00478   /* Process Locked */
00479   __HAL_LOCK(hrtc);
00480 
00481   hrtc->State = HAL_RTC_STATE_BUSY;
00482 
00483   /* Disable the selected Tamper pin */
00484   hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
00485 
00486   hrtc->State = HAL_RTC_STATE_READY;
00487 
00488   /* Process Unlocked */
00489   __HAL_UNLOCK(hrtc);
00490 
00491   return HAL_OK;
00492 }
00493 
00494 /**
00495   * @brief  This function handles TimeStamp interrupt request.
00496   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00497   *                the configuration information for RTC.
00498   * @retval None
00499   */
00500 void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
00501 {
00502   /* Get the TimeStamp interrupt source enable status */
00503   if(__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != (uint32_t)RESET)
00504   {
00505     /* Get the pending status of the TIMESTAMP Interrupt */
00506     if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != (uint32_t)RESET)
00507     {
00508       /* TIMESTAMP callback */
00509 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00510       hrtc->TimeStampEventCallback(hrtc);
00511 #else
00512       HAL_RTCEx_TimeStampEventCallback(hrtc);
00513 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
00514 
00515       /* Clear the TIMESTAMP interrupt pending bit */
00516       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
00517     }
00518   }
00519 
00520   /* Get the Tamper1 interrupt source enable status */
00521   if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != (uint32_t)RESET)
00522   {
00523     /* Get the pending status of the Tamper1 Interrupt */
00524     if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != (uint32_t)RESET)
00525     {
00526       /* Tamper callback */
00527 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00528       hrtc->Tamper1EventCallback(hrtc);
00529 #else
00530       HAL_RTCEx_Tamper1EventCallback(hrtc);
00531 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
00532 
00533       /* Clear the Tamper interrupt pending bit */
00534       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
00535     }
00536   }
00537 
00538   /* Get the Tamper2 interrupt source enable status */
00539   if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != (uint32_t)RESET)
00540   {
00541     /* Get the pending status of the Tamper2 Interrupt */
00542     if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != (uint32_t)RESET)
00543     {
00544       /* Tamper callback */
00545 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
00546       hrtc->Tamper2EventCallback(hrtc);
00547 #else
00548       HAL_RTCEx_Tamper2EventCallback(hrtc);
00549 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
00550 
00551       /* Clear the Tamper interrupt pending bit */
00552       __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
00553     }
00554   }
00555 
00556   /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
00557   __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
00558 
00559   /* Change RTC state */
00560   hrtc->State = HAL_RTC_STATE_READY;
00561 }
00562 
00563 /**
00564   * @brief  TimeStamp callback.
00565   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00566   *                the configuration information for RTC.
00567   * @retval None
00568   */
00569 __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
00570 {
00571   /* Prevent unused argument(s) compilation warning */
00572   UNUSED(hrtc);
00573   /* NOTE : This function Should not be modified, when the callback is needed,
00574             the HAL_RTC_TimeStampEventCallback could be implemented in the user file
00575   */
00576 }
00577 
00578 /**
00579   * @brief  Tamper 1 callback.
00580   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00581   *                the configuration information for RTC.
00582   * @retval None
00583   */
00584 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
00585 {
00586   /* Prevent unused argument(s) compilation warning */
00587   UNUSED(hrtc);
00588   /* NOTE : This function Should not be modified, when the callback is needed,
00589             the HAL_RTC_Tamper1EventCallback could be implemented in the user file
00590    */
00591 }
00592 
00593 /**
00594   * @brief  Tamper 2 callback.
00595   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00596   *                the configuration information for RTC.
00597   * @retval None
00598   */
00599 __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
00600 {
00601   /* Prevent unused argument(s) compilation warning */
00602   UNUSED(hrtc);
00603   /* NOTE : This function Should not be modified, when the callback is needed,
00604             the HAL_RTC_Tamper2EventCallback could be implemented in the user file
00605    */
00606 }
00607 
00608 /**
00609   * @brief  This function handles TimeStamp polling request.
00610   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00611   *                the configuration information for RTC.
00612   * @param  Timeout Timeout duration
00613   * @retval HAL status
00614   */
00615 HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
00616 {
00617   uint32_t tickstart = 0U;
00618 
00619   /* Get tick */
00620   tickstart = HAL_GetTick();
00621 
00622   while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
00623   {
00624     if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
00625     {
00626       /* Clear the TIMESTAMP Overrun Flag */
00627       __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
00628 
00629       /* Change TIMESTAMP state */
00630       hrtc->State = HAL_RTC_STATE_ERROR;
00631 
00632       return HAL_ERROR;
00633     }
00634 
00635     if(Timeout != HAL_MAX_DELAY)
00636     {
00637       if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
00638       {
00639         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00640         return HAL_TIMEOUT;
00641       }
00642     }
00643   }
00644 
00645   /* Change RTC state */
00646   hrtc->State = HAL_RTC_STATE_READY;
00647 
00648   return HAL_OK;
00649 }
00650 
00651 /**
00652   * @brief  This function handles Tamper1 Polling.
00653   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00654   *                the configuration information for RTC.
00655   * @param  Timeout Timeout duration
00656   * @retval HAL status
00657   */
00658 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
00659 {
00660   uint32_t tickstart = 0U;
00661 
00662   /* Get tick */
00663   tickstart = HAL_GetTick();
00664 
00665   /* Get the status of the Interrupt */
00666   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
00667   {
00668     if(Timeout != HAL_MAX_DELAY)
00669     {
00670       if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
00671       {
00672         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00673         return HAL_TIMEOUT;
00674       }
00675     }
00676   }
00677 
00678   /* Clear the Tamper Flag */
00679   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
00680 
00681   /* Change RTC state */
00682   hrtc->State = HAL_RTC_STATE_READY;
00683 
00684   return HAL_OK;
00685 }
00686 
00687 /**
00688   * @brief  This function handles Tamper2 Polling.
00689   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00690   *                the configuration information for RTC.
00691   * @param  Timeout Timeout duration
00692   * @retval HAL status
00693   */
00694 HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
00695 {
00696   uint32_t tickstart = 0U;
00697 
00698   /* Get tick */
00699   tickstart = HAL_GetTick();
00700 
00701   /* Get the status of the Interrupt */
00702   while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
00703   {
00704     if(Timeout != HAL_MAX_DELAY)
00705     {
00706       if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
00707       {
00708         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00709         return HAL_TIMEOUT;
00710       }
00711     }
00712   }
00713 
00714   /* Clear the Tamper Flag */
00715   __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
00716 
00717   /* Change RTC state */
00718   hrtc->State = HAL_RTC_STATE_READY;
00719 
00720   return HAL_OK;
00721 }
00722 
00723 /**
00724   * @}
00725   */
00726 
00727 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wake-up functions
00728  *  @brief   RTC Wake-up functions
00729  *
00730 @verbatim
00731  ===============================================================================
00732                         ##### RTC Wake-up functions #####
00733  ===============================================================================
00734 
00735  [..] This section provides functions allowing to configure Wake-up feature
00736 
00737 @endverbatim
00738   * @{
00739   */
00740 
00741 /**
00742   * @brief  Sets wake up timer.
00743   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00744   *                the configuration information for RTC.
00745   * @param  WakeUpCounter Wake up counter
00746   * @param  WakeUpClock Wake up clock
00747   * @retval HAL status
00748   */
00749 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
00750 {
00751   uint32_t tickstart = 0U;
00752 
00753   /* Check the parameters */
00754   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
00755   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
00756 
00757   /* Process Locked */
00758   __HAL_LOCK(hrtc);
00759 
00760   hrtc->State = HAL_RTC_STATE_BUSY;
00761 
00762   /* Disable the write protection for RTC registers */
00763   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00764 
00765   /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
00766   if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
00767   {
00768     tickstart = HAL_GetTick();
00769 
00770     /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
00771     while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
00772     {
00773       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
00774       {
00775         /* Enable the write protection for RTC registers */
00776         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00777 
00778         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00779 
00780         /* Process Unlocked */
00781         __HAL_UNLOCK(hrtc);
00782 
00783         return HAL_TIMEOUT;
00784       }
00785     }
00786   }
00787 
00788   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
00789 
00790   tickstart = HAL_GetTick();
00791 
00792   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
00793   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
00794   {
00795     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
00796     {
00797       /* Enable the write protection for RTC registers */
00798       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00799 
00800       hrtc->State = HAL_RTC_STATE_TIMEOUT;
00801 
00802       /* Process Unlocked */
00803       __HAL_UNLOCK(hrtc);
00804 
00805       return HAL_TIMEOUT;
00806     }
00807   }
00808 
00809   /* Clear the Wake-up Timer clock source bits in CR register */
00810   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
00811 
00812   /* Configure the clock source */
00813   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
00814 
00815   /* Configure the Wake-up Timer counter */
00816   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
00817 
00818    /* Enable the Wake-up Timer */
00819   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
00820 
00821   /* Enable the write protection for RTC registers */
00822   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00823 
00824   hrtc->State = HAL_RTC_STATE_READY;
00825 
00826   /* Process Unlocked */
00827   __HAL_UNLOCK(hrtc);
00828 
00829   return HAL_OK;
00830 }
00831 
00832 /**
00833   * @brief  Sets wake up timer with interrupt
00834   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00835   *                the configuration information for RTC.
00836   * @param  WakeUpCounter Wake up counter
00837   * @param  WakeUpClock Wake up clock
00838   * @retval HAL status
00839   */
00840 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
00841 {
00842   __IO uint32_t count;
00843 
00844   /* Check the parameters */
00845   assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
00846   assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
00847 
00848   /* Process Locked */
00849   __HAL_LOCK(hrtc);
00850 
00851   hrtc->State = HAL_RTC_STATE_BUSY;
00852 
00853   /* Disable the write protection for RTC registers */
00854   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00855 
00856   /* Check RTC WUTWF flag is reset only when wake up timer enabled */
00857   if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
00858   {
00859     /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
00860     count = RTC_TIMEOUT_VALUE  * (SystemCoreClock / 32U / 1000U);
00861     do
00862     {
00863       if(count-- == 0U)
00864       {
00865         /* Enable the write protection for RTC registers */
00866         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00867 
00868         hrtc->State = HAL_RTC_STATE_TIMEOUT;
00869 
00870         /* Process Unlocked */
00871         __HAL_UNLOCK(hrtc);
00872 
00873         return HAL_TIMEOUT;
00874       }
00875     }
00876     while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET);
00877   }
00878 
00879   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
00880 
00881   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
00882   count = RTC_TIMEOUT_VALUE  * (SystemCoreClock / 32U / 1000U);
00883   do
00884   {
00885     if(count-- == 0U)
00886     {
00887       /* Enable the write protection for RTC registers */
00888       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00889 
00890       hrtc->State = HAL_RTC_STATE_TIMEOUT;
00891 
00892       /* Process Unlocked */
00893       __HAL_UNLOCK(hrtc);
00894 
00895       return HAL_TIMEOUT;
00896     }
00897   }
00898   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET);
00899 
00900   /* Configure the Wake-up Timer counter */
00901   hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
00902 
00903   /* Clear the Wake-up Timer clock source bits in CR register */
00904   hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
00905 
00906   /* Configure the clock source */
00907   hrtc->Instance->CR |= (uint32_t)WakeUpClock;
00908 
00909   /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
00910   __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
00911 
00912   EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
00913 
00914   /* Clear RTC Wake Up timer Flag */
00915   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
00916 
00917   /* Configure the Interrupt in the RTC_CR register */
00918   __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
00919 
00920   /* Enable the Wake-up Timer */
00921   __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
00922 
00923   /* Enable the write protection for RTC registers */
00924   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00925 
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  Deactivates wake up timer counter.
00936   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00937   *                the configuration information for RTC.
00938   * @retval HAL status
00939   */
00940 uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
00941 {
00942   uint32_t tickstart = 0U;
00943 
00944   /* Process Locked */
00945   __HAL_LOCK(hrtc);
00946 
00947   hrtc->State = HAL_RTC_STATE_BUSY;
00948 
00949   /* Disable the write protection for RTC registers */
00950   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
00951 
00952   /* Disable the Wake-up Timer */
00953   __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
00954 
00955   /* In case of interrupt mode is used, the interrupt source must disabled */
00956   __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
00957 
00958   /* Get tick */
00959   tickstart = HAL_GetTick();
00960 
00961   /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
00962   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
00963   {
00964     if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
00965     {
00966       /* Enable the write protection for RTC registers */
00967       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00968 
00969       hrtc->State = HAL_RTC_STATE_TIMEOUT;
00970 
00971       /* Process Unlocked */
00972       __HAL_UNLOCK(hrtc);
00973 
00974       return HAL_TIMEOUT;
00975     }
00976   }
00977 
00978   /* Enable the write protection for RTC registers */
00979   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
00980 
00981   hrtc->State = HAL_RTC_STATE_READY;
00982 
00983   /* Process Unlocked */
00984   __HAL_UNLOCK(hrtc);
00985 
00986   return HAL_OK;
00987 }
00988 
00989 /**
00990   * @brief  Gets wake up timer counter.
00991   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
00992   *                the configuration information for RTC.
00993   * @retval Counter value
00994   */
00995 uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
00996 {
00997   /* Get the counter value */
00998   return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
00999 }
01000 
01001 /**
01002   * @brief  This function handles Wake Up Timer interrupt request.
01003   * @note   Unlike alarm interrupt line (shared by AlarmA and AlarmB) and tamper
01004   *         interrupt line (shared by timestamp and tampers) wakeup timer
01005   *         interrupt line is exclusive to the wakeup timer.
01006   *         There is no need in this case to check on the interrupt enable
01007   *         status via __HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE().
01008   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01009   *                the configuration information for RTC.
01010   * @retval None
01011   */
01012 void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
01013 {
01014   /* Get the pending status of the WAKEUPTIMER Interrupt */
01015   if(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != (uint32_t)RESET)
01016   {
01017     /* WAKEUPTIMER callback */
01018 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
01019     hrtc->WakeUpTimerEventCallback(hrtc);
01020 #else
01021     HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
01022 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
01023 
01024     /* Clear the WAKEUPTIMER interrupt pending bit */
01025     __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
01026   }
01027 
01028   /* Clear the EXTI's line Flag for RTC WakeUpTimer */
01029   __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
01030 
01031   /* Change RTC state */
01032   hrtc->State = HAL_RTC_STATE_READY;
01033 }
01034 
01035 /**
01036   * @brief  Wake Up Timer callback.
01037   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01038   *                the configuration information for RTC.
01039   * @retval None
01040   */
01041 __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
01042 {
01043   /* Prevent unused argument(s) compilation warning */
01044   UNUSED(hrtc);
01045   /* NOTE : This function Should not be modified, when the callback is needed,
01046             the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
01047    */
01048 }
01049 
01050 /**
01051   * @brief  This function handles Wake Up Timer Polling.
01052   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01053   *                the configuration information for RTC.
01054   * @param  Timeout Timeout duration
01055   * @retval HAL status
01056   */
01057 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
01058 {
01059   uint32_t tickstart = 0U;
01060 
01061   /* Get tick */
01062   tickstart = HAL_GetTick();
01063 
01064   while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
01065   {
01066     if(Timeout != HAL_MAX_DELAY)
01067     {
01068       if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
01069       {
01070         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01071 
01072         return HAL_TIMEOUT;
01073       }
01074     }
01075   }
01076 
01077   /* Clear the WAKEUPTIMER Flag */
01078   __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
01079 
01080   /* Change RTC state */
01081   hrtc->State = HAL_RTC_STATE_READY;
01082 
01083   return HAL_OK;
01084 }
01085 
01086 /**
01087   * @}
01088   */
01089 
01090 
01091 /** @defgroup RTCEx_Exported_Functions_Group3 Extension Peripheral Control functions
01092  *  @brief   Extension Peripheral Control functions
01093  *
01094 @verbatim
01095  ===============================================================================
01096               ##### Extension Peripheral Control functions #####
01097  ===============================================================================
01098     [..]
01099     This subsection provides functions allowing to
01100       (+) Write a data in a specified RTC Backup data register
01101       (+) Read a data in a specified RTC Backup data register
01102       (+) Set the Coarse calibration parameters.
01103       (+) Deactivate the Coarse calibration parameters
01104       (+) Set the Smooth calibration parameters.
01105       (+) Configure the Synchronization Shift Control Settings.
01106       (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01107       (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01108       (+) Enable the RTC reference clock detection.
01109       (+) Disable the RTC reference clock detection.
01110       (+) Enable the Bypass Shadow feature.
01111       (+) Disable the Bypass Shadow feature.
01112 
01113 @endverbatim
01114   * @{
01115   */
01116 
01117 /**
01118   * @brief  Writes a data in a specified RTC Backup data register.
01119   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01120   *                the configuration information for RTC.
01121   * @param  BackupRegister RTC Backup data Register number.
01122   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
01123   *                                 specify the register.
01124   * @param  Data Data to be written in the specified RTC Backup data register.
01125   * @retval None
01126   */
01127 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
01128 {
01129   uint32_t tmp = 0U;
01130 
01131   /* Check the parameters */
01132   assert_param(IS_RTC_BKP(BackupRegister));
01133 
01134   tmp = (uint32_t)&(hrtc->Instance->BKP0R);
01135   tmp += (BackupRegister * 4U);
01136 
01137   /* Write the specified register */
01138   *(__IO uint32_t *)tmp = (uint32_t)Data;
01139 }
01140 
01141 /**
01142   * @brief  Reads data from the specified RTC Backup data Register.
01143   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01144   *                the configuration information for RTC.
01145   * @param  BackupRegister RTC Backup data Register number.
01146   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
01147   *                                 specify the register.
01148   * @retval Read value
01149   */
01150 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
01151 {
01152   uint32_t tmp = 0U;
01153 
01154   /* Check the parameters */
01155   assert_param(IS_RTC_BKP(BackupRegister));
01156 
01157   tmp = (uint32_t)&(hrtc->Instance->BKP0R);
01158   tmp += (BackupRegister * 4U);
01159 
01160   /* Read the specified register */
01161   return (*(__IO uint32_t *)tmp);
01162 }
01163 
01164 /**
01165   * @brief  Sets the Coarse calibration parameters.
01166   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01167   *                the configuration information for RTC.
01168   * @param  CalibSign Specifies the sign of the coarse calibration value.
01169   *          This parameter can be  one of the following values :
01170   *             @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
01171   *             @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
01172   * @param  Value value of coarse calibration expressed in ppm (coded on 5 bits).
01173   *
01174   * @note   This Calibration value should be between 0 and 63 when using negative
01175   *         sign with a 2-ppm step.
01176   *
01177   * @note   This Calibration value should be between 0 and 126 when using positive
01178   *         sign with a 4-ppm step.
01179   * @retval HAL status
01180   */
01181 HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
01182 {
01183   /* Check the parameters */
01184   assert_param(IS_RTC_CALIB_SIGN(CalibSign));
01185   assert_param(IS_RTC_CALIB_VALUE(Value));
01186 
01187   /* Process Locked */
01188   __HAL_LOCK(hrtc);
01189 
01190   hrtc->State = HAL_RTC_STATE_BUSY;
01191 
01192   /* Disable the write protection for RTC registers */
01193   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01194 
01195   /* Set Initialization mode */
01196   if(RTC_EnterInitMode(hrtc) != HAL_OK)
01197   {
01198     /* Enable the write protection for RTC registers */
01199     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01200 
01201     /* Set RTC state*/
01202     hrtc->State = HAL_RTC_STATE_ERROR;
01203 
01204     /* Process Unlocked */
01205     __HAL_UNLOCK(hrtc);
01206 
01207     return HAL_ERROR;
01208   }
01209   else
01210   {
01211     /* Enable the Coarse Calibration */
01212     __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
01213 
01214     /* Set the coarse calibration value */
01215     hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
01216 
01217     /* Exit Initialization mode */
01218     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
01219   }
01220 
01221   /* Enable the write protection for RTC registers */
01222   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01223 
01224   /* Change state */
01225   hrtc->State = HAL_RTC_STATE_READY;
01226 
01227   /* Process Unlocked */
01228   __HAL_UNLOCK(hrtc);
01229 
01230   return HAL_OK;
01231 }
01232 
01233 /**
01234   * @brief  Deactivates the Coarse calibration parameters.
01235   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01236   *                the configuration information for RTC.
01237   * @retval HAL status
01238   */
01239 HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
01240 {
01241   /* Process Locked */
01242   __HAL_LOCK(hrtc);
01243 
01244   hrtc->State = HAL_RTC_STATE_BUSY;
01245 
01246   /* Disable the write protection for RTC registers */
01247   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01248 
01249   /* Set Initialization mode */
01250   if(RTC_EnterInitMode(hrtc) != HAL_OK)
01251   {
01252     /* Enable the write protection for RTC registers */
01253     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01254 
01255     /* Set RTC state*/
01256     hrtc->State = HAL_RTC_STATE_ERROR;
01257 
01258     /* Process Unlocked */
01259     __HAL_UNLOCK(hrtc);
01260 
01261     return HAL_ERROR;
01262   }
01263   else
01264   {
01265     /* Enable the Coarse Calibration */
01266     __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
01267 
01268     /* Exit Initialization mode */
01269     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
01270   }
01271 
01272   /* Enable the write protection for RTC registers */
01273   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01274 
01275   /* Change state */
01276   hrtc->State = HAL_RTC_STATE_READY;
01277 
01278   /* Process Unlocked */
01279   __HAL_UNLOCK(hrtc);
01280 
01281   return HAL_OK;
01282 }
01283 
01284 /**
01285   * @brief  Sets the Smooth calibration parameters.
01286   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01287   *                the configuration information for RTC.
01288   * @param  SmoothCalibPeriod Select the Smooth Calibration Period.
01289   *          This parameter can be can be one of the following values :
01290   *             @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
01291   *             @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
01292   *             @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
01293   * @param  SmoothCalibPlusPulses Select to Set or reset the CALP bit.
01294   *          This parameter can be one of the following values:
01295   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
01296   *             @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
01297   * @param  SmouthCalibMinusPulsesValue Select the value of CALM[80] bits.
01298   *          This parameter can be one any value from 0 to 0x000001FF.
01299   * @note   To deactivate the smooth calibration, the field SmoothCalibPlusPulses
01300   *         must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
01301   *         SmouthCalibMinusPulsesValue must be equal to 0.
01302   * @retval HAL status
01303   */
01304 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
01305 {
01306   uint32_t tickstart = 0U;
01307 
01308   /* Check the parameters */
01309   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
01310   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
01311   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
01312 
01313   /* Process Locked */
01314   __HAL_LOCK(hrtc);
01315 
01316   hrtc->State = HAL_RTC_STATE_BUSY;
01317 
01318   /* Disable the write protection for RTC registers */
01319   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01320 
01321   /* check if a calibration is pending*/
01322   if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
01323   {
01324   /* Get tick */
01325   tickstart = HAL_GetTick();
01326 
01327     /* check if a calibration is pending*/
01328     while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
01329     {
01330       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
01331       {
01332         /* Enable the write protection for RTC registers */
01333         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01334 
01335         /* Change RTC state */
01336         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01337 
01338         /* Process Unlocked */
01339         __HAL_UNLOCK(hrtc);
01340 
01341         return HAL_TIMEOUT;
01342       }
01343     }
01344   }
01345 
01346   /* Configure the Smooth calibration settings */
01347   hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
01348 
01349   /* Enable the write protection for RTC registers */
01350   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01351 
01352   /* Change RTC state */
01353   hrtc->State = HAL_RTC_STATE_READY;
01354 
01355   /* Process Unlocked */
01356   __HAL_UNLOCK(hrtc);
01357 
01358   return HAL_OK;
01359 }
01360 
01361 /**
01362   * @brief  Configures the Synchronization Shift Control Settings.
01363   * @note   When REFCKON is set, firmware must not write to Shift control register.
01364   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01365   *                the configuration information for RTC.
01366   * @param  ShiftAdd1S Select to add or not 1 second to the time calendar.
01367   *          This parameter can be one of the following values :
01368   *             @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
01369   *             @arg RTC_SHIFTADD1S_RESET: No effect.
01370   * @param  ShiftSubFS Select the number of Second Fractions to substitute.
01371   *          This parameter can be one any value from 0 to 0x7FFF.
01372   * @retval HAL status
01373   */
01374 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
01375 {
01376   uint32_t tickstart = 0U;
01377 
01378   /* Check the parameters */
01379   assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
01380   assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
01381 
01382   /* Process Locked */
01383   __HAL_LOCK(hrtc);
01384 
01385   hrtc->State = HAL_RTC_STATE_BUSY;
01386 
01387   /* Disable the write protection for RTC registers */
01388   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01389 
01390   /* Get tick */
01391   tickstart = HAL_GetTick();
01392 
01393     /* Wait until the shift is completed*/
01394     while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
01395     {
01396       if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
01397       {
01398         /* Enable the write protection for RTC registers */
01399         __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01400 
01401         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01402 
01403         /* Process Unlocked */
01404         __HAL_UNLOCK(hrtc);
01405 
01406         return HAL_TIMEOUT;
01407       }
01408     }
01409 
01410     /* Check if the reference clock detection is disabled */
01411     if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
01412     {
01413       /* Configure the Shift settings */
01414       hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
01415 
01416       /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
01417       if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
01418       {
01419         if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
01420         {
01421           /* Enable the write protection for RTC registers */
01422           __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01423 
01424           hrtc->State = HAL_RTC_STATE_ERROR;
01425 
01426           /* Process Unlocked */
01427           __HAL_UNLOCK(hrtc);
01428 
01429           return HAL_ERROR;
01430         }
01431       }
01432     }
01433     else
01434     {
01435       /* Enable the write protection for RTC registers */
01436       __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01437 
01438       /* Change RTC state */
01439       hrtc->State = HAL_RTC_STATE_ERROR;
01440 
01441       /* Process Unlocked */
01442       __HAL_UNLOCK(hrtc);
01443 
01444       return HAL_ERROR;
01445     }
01446 
01447   /* Enable the write protection for RTC registers */
01448   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01449 
01450   /* Change RTC state */
01451   hrtc->State = HAL_RTC_STATE_READY;
01452 
01453   /* Process Unlocked */
01454   __HAL_UNLOCK(hrtc);
01455 
01456   return HAL_OK;
01457 }
01458 
01459 /**
01460   * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01461   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01462   *                the configuration information for RTC.
01463   * @param  CalibOutput Select the Calibration output Selection .
01464   *          This parameter can be one of the following values:
01465   *             @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
01466   *             @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
01467   * @retval HAL status
01468   */
01469 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
01470 {
01471   /* Check the parameters */
01472   assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
01473 
01474   /* Process Locked */
01475   __HAL_LOCK(hrtc);
01476 
01477   hrtc->State = HAL_RTC_STATE_BUSY;
01478 
01479   /* Disable the write protection for RTC registers */
01480   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01481 
01482   /* Clear flags before config */
01483   hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
01484 
01485   /* Configure the RTC_CR register */
01486   hrtc->Instance->CR |= (uint32_t)CalibOutput;
01487 
01488   __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
01489 
01490   /* Enable the write protection for RTC registers */
01491   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01492 
01493   /* Change RTC state */
01494   hrtc->State = HAL_RTC_STATE_READY;
01495 
01496   /* Process Unlocked */
01497   __HAL_UNLOCK(hrtc);
01498 
01499   return HAL_OK;
01500 }
01501 
01502 /**
01503   * @brief  Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01504   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01505   *                the configuration information for RTC.
01506   * @retval HAL status
01507   */
01508 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
01509 {
01510   /* Process Locked */
01511   __HAL_LOCK(hrtc);
01512 
01513   hrtc->State = HAL_RTC_STATE_BUSY;
01514 
01515   /* Disable the write protection for RTC registers */
01516   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01517 
01518   __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
01519 
01520   /* Enable the write protection for RTC registers */
01521   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01522 
01523   /* Change RTC state */
01524   hrtc->State = HAL_RTC_STATE_READY;
01525 
01526   /* Process Unlocked */
01527   __HAL_UNLOCK(hrtc);
01528 
01529   return HAL_OK;
01530 }
01531 
01532 /**
01533   * @brief  Enables the RTC reference clock detection.
01534   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01535   *                the configuration information for RTC.
01536   * @retval HAL status
01537   */
01538 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
01539 {
01540   /* Process Locked */
01541   __HAL_LOCK(hrtc);
01542 
01543   hrtc->State = HAL_RTC_STATE_BUSY;
01544 
01545   /* Disable the write protection for RTC registers */
01546   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01547 
01548   /* Set Initialization mode */
01549   if(RTC_EnterInitMode(hrtc) != HAL_OK)
01550   {
01551     /* Enable the write protection for RTC registers */
01552     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01553 
01554     /* Set RTC state*/
01555     hrtc->State = HAL_RTC_STATE_ERROR;
01556 
01557     /* Process Unlocked */
01558     __HAL_UNLOCK(hrtc);
01559 
01560     return HAL_ERROR;
01561   }
01562   else
01563   {
01564     __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
01565 
01566     /* Exit Initialization mode */
01567     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
01568   }
01569 
01570   /* Enable the write protection for RTC registers */
01571   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01572 
01573    /* Change RTC state */
01574   hrtc->State = HAL_RTC_STATE_READY;
01575 
01576   /* Process Unlocked */
01577   __HAL_UNLOCK(hrtc);
01578 
01579   return HAL_OK;
01580 }
01581 
01582 /**
01583   * @brief  Disable the RTC reference clock detection.
01584   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01585   *                the configuration information for RTC.
01586   * @retval HAL status
01587   */
01588 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
01589 {
01590   /* Process Locked */
01591   __HAL_LOCK(hrtc);
01592 
01593   hrtc->State = HAL_RTC_STATE_BUSY;
01594 
01595   /* Disable the write protection for RTC registers */
01596   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01597 
01598   /* Set Initialization mode */
01599   if(RTC_EnterInitMode(hrtc) != HAL_OK)
01600   {
01601     /* Enable the write protection for RTC registers */
01602     __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01603 
01604     /* Set RTC state*/
01605     hrtc->State = HAL_RTC_STATE_ERROR;
01606 
01607     /* Process Unlocked */
01608     __HAL_UNLOCK(hrtc);
01609 
01610     return HAL_ERROR;
01611   }
01612   else
01613   {
01614     __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
01615 
01616     /* Exit Initialization mode */
01617     hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
01618   }
01619 
01620   /* Enable the write protection for RTC registers */
01621   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01622 
01623   /* Change RTC state */
01624   hrtc->State = HAL_RTC_STATE_READY;
01625 
01626   /* Process Unlocked */
01627   __HAL_UNLOCK(hrtc);
01628 
01629   return HAL_OK;
01630 }
01631 
01632 /**
01633   * @brief  Enables the Bypass Shadow feature.
01634   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01635   *                the configuration information for RTC.
01636   * @note   When the Bypass Shadow is enabled the calendar value are taken
01637   *         directly from the Calendar counter.
01638   * @retval HAL status
01639   */
01640 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
01641 {
01642   /* Process Locked */
01643   __HAL_LOCK(hrtc);
01644 
01645   hrtc->State = HAL_RTC_STATE_BUSY;
01646 
01647   /* Disable the write protection for RTC registers */
01648   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01649 
01650   /* Set the BYPSHAD bit */
01651   hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
01652 
01653   /* Enable the write protection for RTC registers */
01654   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01655 
01656   /* Change RTC state */
01657   hrtc->State = HAL_RTC_STATE_READY;
01658 
01659   /* Process Unlocked */
01660   __HAL_UNLOCK(hrtc);
01661 
01662   return HAL_OK;
01663 }
01664 
01665 /**
01666   * @brief  Disables the Bypass Shadow feature.
01667   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01668   *                the configuration information for RTC.
01669   * @note   When the Bypass Shadow is enabled the calendar value are taken
01670   *         directly from the Calendar counter.
01671   * @retval HAL status
01672   */
01673 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
01674 {
01675   /* Process Locked */
01676   __HAL_LOCK(hrtc);
01677 
01678   hrtc->State = HAL_RTC_STATE_BUSY;
01679 
01680   /* Disable the write protection for RTC registers */
01681   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
01682 
01683   /* Reset the BYPSHAD bit */
01684   hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
01685 
01686   /* Enable the write protection for RTC registers */
01687   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
01688 
01689   /* Change RTC state */
01690   hrtc->State = HAL_RTC_STATE_READY;
01691 
01692   /* Process Unlocked */
01693   __HAL_UNLOCK(hrtc);
01694 
01695   return HAL_OK;
01696 }
01697 
01698 /**
01699   * @}
01700   */
01701 
01702   /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
01703  *  @brief    Extended features functions
01704  *
01705 @verbatim
01706  ===============================================================================
01707                  ##### Extended features functions #####
01708  ===============================================================================
01709     [..]  This section provides functions allowing to:
01710       (+) RTC Alarm B callback
01711       (+) RTC Poll for Alarm B request
01712 
01713 @endverbatim
01714   * @{
01715   */
01716 
01717 /**
01718   * @brief  Alarm B callback.
01719   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01720   *                the configuration information for RTC.
01721   * @retval None
01722   */
01723 __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
01724 {
01725   /* Prevent unused argument(s) compilation warning */
01726   UNUSED(hrtc);
01727   /* NOTE : This function Should not be modified, when the callback is needed,
01728             the HAL_RTC_AlarmBEventCallback could be implemented in the user file
01729    */
01730 }
01731 
01732 /**
01733   * @brief  This function handles AlarmB Polling request.
01734   * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
01735   *                the configuration information for RTC.
01736   * @param  Timeout Timeout duration
01737   * @retval HAL status
01738   */
01739 HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
01740 {
01741   uint32_t tickstart = 0U;
01742 
01743   /* Get tick */
01744   tickstart = HAL_GetTick();
01745 
01746   while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
01747   {
01748     if(Timeout != HAL_MAX_DELAY)
01749     {
01750       if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
01751       {
01752         hrtc->State = HAL_RTC_STATE_TIMEOUT;
01753         return HAL_TIMEOUT;
01754       }
01755     }
01756   }
01757 
01758   /* Clear the Alarm Flag */
01759   __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
01760 
01761   /* Change RTC state */
01762   hrtc->State = HAL_RTC_STATE_READY;
01763 
01764   return HAL_OK;
01765 }
01766 
01767 /**
01768   * @}
01769   */
01770 
01771 /**
01772   * @}
01773   */
01774 
01775 #endif /* HAL_RTC_MODULE_ENABLED */
01776 /**
01777   * @}
01778   */
01779 
01780 /**
01781   * @}
01782   */
01783 
01784 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/