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