Martin Johnson / STM32F3-Discovery

Dependents:   Space_Invaders_Demo neopixels gpio_test_stm32f3_discovery gpio_test_systimer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f30x_rtc.c Source File

stm32f30x_rtc.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f30x_rtc.c
00004   * @author  MCD Application Team
00005   * @version V1.2.3
00006   * @date    10-July-2015
00007   * @brief   This file provides firmware functions to manage the following 
00008   *          functionalities of the Real-Time Clock (RTC) peripheral:
00009   *           + Initialization
00010   *           + Calendar (Time and Date) configuration
00011   *           + Alarms (Alarm A and Alarm B) configuration
00012   *           + WakeUp Timer configuration
00013   *           + Daylight Saving configuration
00014   *           + Output pin Configuration
00015   *           + Smooth digital Calibration configuration
00016   *           + TimeStamp configuration
00017   *           + Tampers configuration
00018   *           + Backup Data Registers configuration
00019   *           + Output Type Config configuration
00020   *           + Shift control synchronisation  
00021   *           + Interrupts and flags management       
00022   *                     
00023   @verbatim
00024        
00025  ===============================================================================     
00026                           ##### RTC Operating Condition #####
00027  ===============================================================================
00028     [..] The real-time clock (RTC) and the RTC backup registers can be powered
00029          from the VBAT voltage when the main VDD supply is powered off.
00030          To retain the content of the RTC backup registers and supply the RTC 
00031          when VDD is turned off, VBAT pin can be connected to an optional
00032          standby voltage supplied by a battery or by another source.
00033   
00034     [..] To allow the RTC to operate even when the main digital supply (VDD) 
00035          is turned off, the VBAT pin powers the following blocks:
00036            (#) The RTC
00037            (#) The LSE oscillator
00038            (#) PC13 to PC15 I/Os (when available)
00039   
00040     [..] When the backup domain is supplied by VDD (analog switch connected 
00041          to VDD), the following functions are available:
00042            (#) PC14 and PC15 can be used as either GPIO or LSE pins
00043            (#) PC13 can be used as a GPIO or as the RTC_AF pin
00044   
00045     [..] When the backup domain is supplied by VBAT (analog switch connected 
00046          to VBAT because VDD is not present), the following functions are available:
00047            (#) PC14 and PC15 can be used as LSE pins only
00048            (#) PC13 can be used as the RTC_AF pin 
00049 
00050                         ##### Backup Domain Reset #####
00051  ===============================================================================
00052     [..] The backup domain reset sets all RTC registers and the RCC_BDCR 
00053          register to their reset values. 
00054          A backup domain reset is generated when one of the following events
00055          occurs:
00056            (#) Software reset, triggered by setting the BDRST bit in the 
00057                RCC Backup domain control register (RCC_BDCR). You can use the
00058                RCC_BackupResetCmd().
00059            (#) VDD or VBAT power on, if both supplies have previously been
00060                powered off.
00061                          
00062                         ##### Backup Domain Access #####
00063  ===============================================================================
00064     [..] After reset, the backup domain (RTC registers and RTC backup data 
00065          registers) is protected against possible unwanted write accesses. 
00066     [..] To enable access to the Backup Domain and RTC registers, proceed as follows:
00067          (#) Enable the Power Controller (PWR) APB1 interface clock using the
00068              RCC_APB1PeriphClockCmd() function.
00069          (#) Enable access to Backup domain using the PWR_BackupAccessCmd() function.
00070          (#) Select the RTC clock source using the RCC_RTCCLKConfig() function.
00071          (#) Enable RTC Clock using the RCC_RTCCLKCmd() function.
00072               
00073                          ##### How to use this driver #####
00074  ===============================================================================
00075     [..]     
00076         (+) Enable the backup domain access (see description in the section above)
00077          (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and
00078              RTC hour format using the RTC_Init() function.
00079                 
00080     *** Time and Date configuration ***
00081     ===================================    
00082     [..]     
00083          (+) To configure the RTC Calendar (Time and Date) use the RTC_SetTime()
00084              and RTC_SetDate() functions.
00085          (+) To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()
00086              functions.
00087          (+) To read the RTC subsecond, use the RTC_GetSubSecond() function.
00088          (+) Use the RTC_DayLightSavingConfig() function to add or sub one
00089              hour to the RTC Calendar.    
00090                 
00091     *** Alarm configuration ***
00092     ===========================    
00093     [..]
00094          (+) To configure the RTC Alarm use the RTC_SetAlarm() function.
00095          (+) Enable the selected RTC Alarm using the RTC_AlarmCmd() function.
00096          (+) To read the RTC Alarm, use the RTC_GetAlarm() function.
00097          (+) To read the RTC alarm SubSecond, use the RTC_GetAlarmSubSecond() function.
00098               
00099     *** RTC Wakeup configuration ***
00100     ================================    
00101     [..]
00102          (+) Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
00103              function.
00104          (+) Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter() 
00105              function  
00106          (+) Enable the RTC WakeUp using the RTC_WakeUpCmd() function  
00107          (+) To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter() 
00108              function.
00109                 
00110     *** Outputs configuration ***
00111     =============================  
00112     [..] The RTC has 2 different outputs:
00113          (+) AFO_ALARM: this output is used to manage the RTC Alarm A, Alarm B
00114              and WaKeUp signals.          
00115              To output the selected RTC signal on RTC_AF pin, use the 
00116              RTC_OutputConfig() function.                
00117          (+) AFO_CALIB: this output is 512Hz signal or 1Hz .
00118              To output the RTC Clock on RTC_AF pin, use the RTC_CalibOutputCmd()
00119              function.                
00120                 
00121     *** Smooth digital Calibration configuration ***
00122     ================================================    
00123     [..]
00124          (+) Configure the RTC Original Digital Calibration Value and the corresponding
00125              calibration cycle period (32s,16s and 8s) using the RTC_SmoothCalibConfig() 
00126              function.                                                       
00127                 
00128     *** TimeStamp configuration ***
00129     ===============================    
00130     [..]
00131          (+) Configure the RTC_AF trigger and enables the RTC TimeStamp 
00132              using the RTC_TimeStampCmd() function.
00133          (+) To read the RTC TimeStamp Time and Date register, use the 
00134              RTC_GetTimeStamp() function.
00135          (+) To read the RTC TimeStamp SubSecond register, use the 
00136              RTC_GetTimeStampSubSecond() function.    
00137 
00138     *** Tamper configuration ***
00139     ============================    
00140     [..]
00141          (+) Configure the Tamper filter count using RTC_TamperFilterConfig()
00142              function. 
00143          (+) Configure the RTC Tamper trigger Edge or Level according to the Tamper 
00144              filter (if equal to 0 Edge else Level) value using the RTC_TamperConfig() function.
00145          (+) Configure the Tamper sampling frequency using RTC_TamperSamplingFreqConfig()
00146              function.
00147          (+) Configure the Tamper precharge or discharge duration using 
00148              RTC_TamperPinsPrechargeDuration() function.
00149          (+) Enable the Tamper Pull-UP using RTC_TamperPullUpDisableCmd() function.
00150          (+) Enable the RTC Tamper using the RTC_TamperCmd() function.
00151          (+) Enable the Time stamp on Tamper detection event using  
00152              RTC_TSOnTamperDetecCmd() function.     
00153 
00154     *** Backup Data Registers configuration ***
00155     ===========================================    
00156     [..]
00157          (+) To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()
00158              function.  
00159          (+) To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()
00160              function.  
00161                                   
00162                          ##### RTC and low power modes #####
00163  =============================================================================== 
00164     [..] The MCU can be woken up from a low power mode by an RTC alternate 
00165          function.
00166     [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B), 
00167          RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
00168          These RTC alternate functions can wake up the system from the Stop 
00169          and Standby lowpower modes.
00170          The system can also wake up from low power modes without depending 
00171          on an external interrupt (Auto-wakeup mode), by using the RTC alarm 
00172          or the RTC wakeup events.
00173     [..] The RTC provides a programmable time base for waking up from the 
00174          Stop or Standby mode at regular intervals.
00175          Wakeup from STOP and Standby modes is possible only when the RTC 
00176          clock source is LSE or LSI.
00177            
00178                 ##### Selection of RTC_AF alternate functions #####
00179  ===============================================================================
00180     [..] The RTC_AF pin (PC13) can be used for the following purposes:
00181          (+) Wakeup pin 2 (WKUP2) using the PWR_WakeUpPinCmd() function.
00182          (+) AFO_ALARM output      
00183          (+) AFO_CALIB output
00184          (+) AFI_TAMPER
00185          (+) AFI_TIMESTAMP
00186                          
00187  +------------------------------------------------------------------------------------------+
00188  |     Pin         |RTC ALARM |RTC CALIB |RTC TAMPER |RTC TIMESTAMP |PC13MODE|  PC13VALUE   |
00189  |  configuration  | OUTPUT   | OUTPUT   |  INPUT    |    INPUT     |  bit   |     bit      |
00190  |  and function   | ENABLED  | ENABLED  | ENABLED   |   ENABLED    |        |              |
00191  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00192  |   Alarm out     |          |          |           |              | Don't  |              |
00193  |   output OD     |     1    |Don't care|Don't care | Don't care   | care   |      0       |
00194  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00195  |   Alarm out     |          |          |           |              | Don't  |              |
00196  |   output PP     |     1    |Don't care|Don't care | Don't care   | care   |      1       |
00197  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00198  | Calibration out |          |          |           |              | Don't  |              |
00199  |   output PP     |     0    |    1     |Don't care | Don't care   | care   |  Don't care  |
00200  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00201  |  TAMPER input   |          |          |           |              | Don't  |              |
00202  |   floating      |     0    |    0     |     1     |      0       | care   |  Don't care  |
00203  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00204  |  TIMESTAMP and  |          |          |           |              | Don't  |              |
00205  |  TAMPER input   |     0    |    0     |     1     |      1       | care   |  Don't care  |
00206  |   floating      |          |          |           |              |        |              |
00207  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00208  | TIMESTAMP input |          |          |           |              | Don't  |              |
00209  |    floating     |     0    |    0     |     0     |      1       | care   |  Don't care  |
00210  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00211  |   Output PP     |     0    |    0     |     0     |      0       |   1    | PC13 output  |
00212  |    Forced       |          |          |           |              |        |              |
00213  |-----------------|----------|----------|-----------|--------------|--------|--------------|
00214  |  Wakeup Pin or  |     0    |    0     |     0     |      0       |   0    | Don't care   |
00215  |  Standard GPIO  |          |          |           |              |        |              |
00216  +------------------------------------------------------------------------------------------+
00217     
00218   @endverbatim
00219                       
00220   ******************************************************************************
00221   * @attention
00222   *
00223   * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
00224   *
00225   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00226   * You may not use this file except in compliance with the License.
00227   * You may obtain a copy of the License at:
00228   *
00229   *        http://www.st.com/software_license_agreement_liberty_v2
00230   *
00231   * Unless required by applicable law or agreed to in writing, software 
00232   * distributed under the License is distributed on an "AS IS" BASIS, 
00233   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00234   * See the License for the specific language governing permissions and
00235   * limitations under the License.
00236   *
00237   ******************************************************************************
00238   */ 
00239 
00240 /* Includes ------------------------------------------------------------------*/
00241 #include "stm32f30x_rtc.h"
00242 #include "stm32f30x_rcc.h"
00243 
00244 /** @addtogroup STM32F30x_StdPeriph_Driver
00245   * @{
00246   */
00247 
00248 /** @defgroup RTC 
00249   * @brief RTC driver modules
00250   * @{
00251   */
00252 
00253 /* Private typedef -----------------------------------------------------------*/
00254 /* Private define ------------------------------------------------------------*/
00255 
00256 /* Masks Definition */
00257 #define RTC_TR_RESERVED_MASK    ((uint32_t)0x007F7F7F)
00258 #define RTC_DR_RESERVED_MASK    ((uint32_t)0x00FFFF3F) 
00259 #define RTC_INIT_MASK           ((uint32_t)0xFFFFFFFF)  
00260 #define RTC_RSF_MASK            ((uint32_t)0xFFFFFF5F)
00261 #define RTC_FLAGS_MASK          ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_WUTF | \
00262                                             RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_INITF | \
00263                                             RTC_FLAG_RSF | RTC_FLAG_INITS | RTC_FLAG_WUTWF | \
00264                                             RTC_FLAG_ALRBWF | RTC_FLAG_ALRAWF | RTC_FLAG_TAMP1F | \
00265                                             RTC_FLAG_TAMP2F | RTC_FLAG_TAMP3F | RTC_FLAG_RECALPF | \
00266                                             RTC_FLAG_SHPF))
00267 
00268 #define INITMODE_TIMEOUT         ((uint32_t) 0x00002000)
00269 #define SYNCHRO_TIMEOUT          ((uint32_t) 0x00008000)
00270 #define RECALPF_TIMEOUT          ((uint32_t) 0x00001000)
00271 #define SHPF_TIMEOUT             ((uint32_t) 0x00002000)
00272 
00273 /* Private macro -------------------------------------------------------------*/
00274 /* Private variables ---------------------------------------------------------*/
00275 /* Private function prototypes -----------------------------------------------*/
00276 static uint8_t RTC_ByteToBcd2(uint8_t Value);
00277 static uint8_t RTC_Bcd2ToByte(uint8_t Value);
00278 
00279 /* Private functions ---------------------------------------------------------*/
00280 
00281 /** @defgroup RTC_Private_Functions
00282   * @{
00283   */ 
00284 
00285 /** @defgroup RTC_Group1 Initialization and Configuration functions
00286  *  @brief   Initialization and Configuration functions 
00287  *
00288 @verbatim   
00289  ===============================================================================
00290             ##### Initialization and Configuration functions #####
00291  ===============================================================================  
00292     [..] This section provide functions allowing to initialize and configure the RTC
00293          Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers
00294          Write protection, enter and exit the RTC initialization mode, RTC registers
00295          synchronization check and reference clock detection enable.
00296          (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is
00297              split into 2 programmable prescalers to minimize power consumption.
00298              (++) A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
00299              (++) When both prescalers are used, it is recommended to configure the 
00300                   asynchronous prescaler to a high value to minimize consumption.
00301          (#) All RTC registers are Write protected. Writing to the RTC registers
00302              is enabled by writing a key into the Write Protection register, RTC_WPR.
00303          (#) To Configure the RTC Calendar, user application should enter initialization
00304              mode. In this mode, the calendar counter is stopped and its value 
00305              can be updated. When the initialization sequence is complete, the 
00306              calendar restarts counting after 4 RTCCLK cycles.
00307          (#) To read the calendar through the shadow registers after Calendar 
00308              initialization, calendar update or after wakeup from low power modes 
00309              the software must first clear the RSF flag. The software must then 
00310              wait until it is set again before reading the calendar, which means 
00311              that the calendar registers have been correctly copied into the RTC_TR 
00312              and RTC_DR shadow registers. The RTC_WaitForSynchro() function 
00313              implements the above software sequence (RSF clear and RSF check).
00314 
00315 @endverbatim
00316   * @{
00317   */
00318 
00319 /**
00320   * @brief  Deinitializes the RTC registers to their default reset values.
00321   * @note   This function doesn't reset the RTC Clock source and RTC Backup Data
00322   *         registers.       
00323   * @param  None
00324   * @retval An ErrorStatus enumeration value:
00325   *          - SUCCESS: RTC registers are deinitialized
00326   *          - ERROR: RTC registers are not deinitialized
00327   */
00328 ErrorStatus RTC_DeInit(void)
00329 {
00330   __IO uint32_t wutcounter = 0x00;
00331   uint32_t wutwfstatus = 0x00;
00332   ErrorStatus status = ERROR;
00333   
00334   /* Disable the write protection for RTC registers */
00335   RTC->WPR = 0xCA;
00336   RTC->WPR = 0x53;
00337 
00338   /* Set Initialization mode */
00339   if (RTC_EnterInitMode() == ERROR)
00340   {
00341     status = ERROR;
00342   }  
00343   else
00344   {
00345     /* Reset TR, DR and CR registers */
00346     RTC->TR = (uint32_t)0x00000000;
00347     RTC->DR = (uint32_t)0x00002101;
00348     
00349     /* Reset All CR bits except CR[2:0] */
00350     RTC->CR &= (uint32_t)0x00000007;
00351   
00352     /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
00353     do
00354     {
00355       wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
00356       wutcounter++;  
00357     } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
00358     
00359     if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
00360     {
00361       status = ERROR;
00362     }
00363     else
00364     {
00365       /* Reset all RTC CR register bits */
00366       RTC->CR        &= (uint32_t)0x00000000;
00367       RTC->WUTR      = (uint32_t)0x0000FFFF;
00368       RTC->PRER      = (uint32_t)0x007F00FF;
00369       RTC->ALRMAR    = (uint32_t)0x00000000;
00370       RTC->ALRMBR    = (uint32_t)0x00000000;
00371       RTC->SHIFTR    = (uint32_t)0x00000000;
00372       RTC->CALR       = (uint32_t)0x00000000;
00373       RTC->ALRMASSR  = (uint32_t)0x00000000;
00374       RTC->ALRMBSSR  = (uint32_t)0x00000000;
00375 
00376       /* Reset ISR register and exit initialization mode */
00377       RTC->ISR = (uint32_t)0x00000000;
00378       
00379       /* Reset Tamper and alternate functions configuration register */
00380       RTC->TAFCR = 0x00000000;
00381       
00382       /* Wait till the RTC RSF flag is set */
00383       if (RTC_WaitForSynchro() == ERROR)
00384       {
00385         status = ERROR;
00386       }
00387       else
00388       {
00389         status = SUCCESS;
00390       }
00391     }
00392   }
00393   
00394   /* Enable the write protection for RTC registers */
00395   RTC->WPR = 0xFF;  
00396   
00397   return status;
00398 }
00399 
00400 /**
00401   * @brief  Initializes the RTC registers according to the specified parameters 
00402   *         in RTC_InitStruct.
00403   * @param  RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains 
00404   *         the configuration information for the RTC peripheral.
00405   * @note   The RTC Prescaler register is write protected and can be written in 
00406   *         initialization mode only.  
00407   * @retval An ErrorStatus enumeration value:
00408   *          - SUCCESS: RTC registers are initialized
00409   *          - ERROR: RTC registers are not initialized  
00410   */
00411 ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
00412 {
00413   ErrorStatus status = ERROR;
00414   
00415   /* Check the parameters */
00416   assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
00417   assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
00418   assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
00419 
00420   /* Disable the write protection for RTC registers */
00421   RTC->WPR = 0xCA;
00422   RTC->WPR = 0x53;
00423 
00424   /* Set Initialization mode */
00425   if (RTC_EnterInitMode() == ERROR)
00426   {
00427     status = ERROR;
00428   } 
00429   else
00430   {
00431     /* Clear RTC CR FMT Bit */
00432     RTC->CR &= ((uint32_t)~(RTC_CR_FMT));
00433     /* Set RTC_CR register */
00434     RTC->CR |=  ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
00435   
00436     /* Configure the RTC PRER */
00437     RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
00438     RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
00439 
00440     /* Exit Initialization mode */
00441     RTC_ExitInitMode();
00442 
00443     status = SUCCESS;    
00444   }
00445   /* Enable the write protection for RTC registers */
00446   RTC->WPR = 0xFF; 
00447   
00448   return status;
00449 }
00450 
00451 /**
00452   * @brief  Fills each RTC_InitStruct member with its default value.
00453   * @param  RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be 
00454   *         initialized.
00455   * @retval None
00456   */
00457 void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
00458 {
00459   /* Initialize the RTC_HourFormat member */
00460   RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
00461     
00462   /* Initialize the RTC_AsynchPrediv member */
00463   RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
00464 
00465   /* Initialize the RTC_SynchPrediv member */
00466   RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF; 
00467 }
00468 
00469 /**
00470   * @brief  Enables or disables the RTC registers write protection.
00471   * @note   All the RTC registers are write protected except for RTC_ISR[13:8], 
00472   *         RTC_TAFCR and RTC_BKPxR.
00473   * @note   Writing a wrong key reactivates the write protection.
00474   * @note   The protection mechanism is not affected by system reset.  
00475   * @param  NewState: new state of the write protection.
00476   *   This parameter can be: ENABLE or DISABLE.
00477   * @retval None
00478   */
00479 void RTC_WriteProtectionCmd(FunctionalState NewState)
00480 {
00481   /* Check the parameters */
00482   assert_param(IS_FUNCTIONAL_STATE(NewState));
00483     
00484   if (NewState != DISABLE)
00485   {
00486     /* Enable the write protection for RTC registers */
00487     RTC->WPR = 0xFF;   
00488   }
00489   else
00490   {
00491     /* Disable the write protection for RTC registers */
00492     RTC->WPR = 0xCA;
00493     RTC->WPR = 0x53;    
00494   }
00495 }
00496 
00497 /**
00498   * @brief  Enters the RTC Initialization mode.
00499   * @note   The RTC Initialization mode is write protected, use the 
00500   *         RTC_WriteProtectionCmd(DISABLE) before calling this function.    
00501   * @param  None
00502   * @retval An ErrorStatus enumeration value:
00503   *          - SUCCESS: RTC is in Init mode
00504   *          - ERROR: RTC is not in Init mode  
00505   */
00506 ErrorStatus RTC_EnterInitMode(void)
00507 {
00508   __IO uint32_t initcounter = 0x00;
00509   ErrorStatus status = ERROR;
00510   uint32_t initstatus = 0x00;
00511      
00512   /* Check if the Initialization mode is set */
00513   if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
00514   {
00515     /* Set the Initialization mode */
00516     RTC->ISR = (uint32_t)RTC_INIT_MASK;
00517     
00518     /* Wait till RTC is in INIT state and if Time out is reached exit */
00519     do
00520     {
00521       initstatus = RTC->ISR & RTC_ISR_INITF;
00522       initcounter++;  
00523     } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
00524     
00525     if ((RTC->ISR & RTC_ISR_INITF) != RESET)
00526     {
00527       status = SUCCESS;
00528     }
00529     else
00530     {
00531       status = ERROR;
00532     }        
00533   }
00534   else
00535   {
00536     status = SUCCESS;  
00537   } 
00538     
00539   return (status);  
00540 }
00541 
00542 /**
00543   * @brief  Exits the RTC Initialization mode.
00544   * @note   When the initialization sequence is complete, the calendar restarts 
00545   *         counting after 4 RTCCLK cycles.  
00546   * @note   The RTC Initialization mode is write protected, use the 
00547   *         RTC_WriteProtectionCmd(DISABLE) before calling this function.      
00548   * @param  None
00549   * @retval None
00550   */
00551 void RTC_ExitInitMode(void)
00552 {
00553   /* Exit Initialization mode */
00554   RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
00555 }
00556 
00557 /**
00558   * @brief  Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are 
00559   *         synchronized with RTC APB clock.
00560   * @note   The RTC Resynchronization mode is write protected, use the 
00561   *         RTC_WriteProtectionCmd(DISABLE) before calling this function. 
00562   * @note   To read the calendar through the shadow registers after Calendar 
00563   *         initialization, calendar update or after wakeup from low power modes 
00564   *         the software must first clear the RSF flag. 
00565   *         The software must then wait until it is set again before reading 
00566   *         the calendar, which means that the calendar registers have been 
00567   *         correctly copied into the RTC_TR and RTC_DR shadow registers.   
00568   * @param  None
00569   * @retval An ErrorStatus enumeration value:
00570   *          - SUCCESS: RTC registers are synchronised
00571   *          - ERROR: RTC registers are not synchronised
00572   */
00573 ErrorStatus RTC_WaitForSynchro(void)
00574 {
00575   __IO uint32_t synchrocounter = 0;
00576   ErrorStatus status = ERROR;
00577   uint32_t synchrostatus = 0x00;
00578 
00579   if ((RTC->CR & RTC_CR_BYPSHAD) != RESET)
00580   {
00581     /* Bypass shadow mode */
00582     status = SUCCESS;
00583   }
00584   else
00585   {
00586     /* Disable the write protection for RTC registers */
00587     RTC->WPR = 0xCA;
00588     RTC->WPR = 0x53;
00589     
00590     /* Clear RSF flag */
00591     RTC->ISR &= (uint32_t)RTC_RSF_MASK;
00592     
00593     /* Wait the registers to be synchronised */
00594     do
00595     {
00596       synchrostatus = RTC->ISR & RTC_ISR_RSF;
00597       synchrocounter++;  
00598     } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
00599     
00600     if ((RTC->ISR & RTC_ISR_RSF) != RESET)
00601     {
00602       status = SUCCESS;
00603     }
00604     else
00605     {
00606       status = ERROR;
00607     }
00608 
00609     /* Enable the write protection for RTC registers */
00610     RTC->WPR = 0xFF;
00611   } 
00612   
00613   return (status);
00614 }
00615 
00616 /**
00617   * @brief  Enables or disables the RTC reference clock detection.
00618   * @param  NewState: new state of the RTC reference clock.
00619   *   This parameter can be: ENABLE or DISABLE.
00620   * @retval An ErrorStatus enumeration value:
00621   *          - SUCCESS: RTC reference clock detection is enabled
00622   *          - ERROR: RTC reference clock detection is disabled  
00623   */
00624 ErrorStatus RTC_RefClockCmd(FunctionalState NewState)
00625 {
00626   ErrorStatus status = ERROR;
00627 
00628   /* Check the parameters */
00629   assert_param(IS_FUNCTIONAL_STATE(NewState));
00630 
00631   /* Disable the write protection for RTC registers */
00632   RTC->WPR = 0xCA;
00633   RTC->WPR = 0x53;
00634 
00635   /* Set Initialization mode */
00636   if (RTC_EnterInitMode() == ERROR)
00637   {
00638     status = ERROR;
00639   }
00640   else
00641   {
00642     if (NewState != DISABLE)
00643     {
00644       /* Enable the RTC reference clock detection */
00645       RTC->CR |= RTC_CR_REFCKON;   
00646     }
00647     else
00648     {
00649       /* Disable the RTC reference clock detection */
00650       RTC->CR &= ~RTC_CR_REFCKON;    
00651     }
00652     /* Exit Initialization mode */
00653     RTC_ExitInitMode();
00654 
00655     status = SUCCESS;
00656   }
00657 
00658   /* Enable the write protection for RTC registers */
00659   RTC->WPR = 0xFF;
00660 
00661   return status;
00662 }
00663 
00664 /**
00665   * @brief  Enables or Disables the Bypass Shadow feature.
00666   * @note   When the Bypass Shadow is enabled the calendar value are taken 
00667   *         directly from the Calendar counter.
00668   * @param  NewState: new state of the Bypass Shadow feature.
00669   *         This parameter can be: ENABLE or DISABLE.
00670   * @retval None
00671 */
00672 void RTC_BypassShadowCmd(FunctionalState NewState)
00673 {
00674   /* Check the parameters */
00675   assert_param(IS_FUNCTIONAL_STATE(NewState));
00676 
00677   /* Disable the write protection for RTC registers */
00678   RTC->WPR = 0xCA;
00679   RTC->WPR = 0x53;
00680   
00681   if (NewState != DISABLE)
00682   {
00683     /* Set the BYPSHAD bit */
00684     RTC->CR |= (uint8_t)RTC_CR_BYPSHAD;
00685   }
00686   else
00687   {
00688     /* Reset the BYPSHAD bit */
00689     RTC->CR &= (uint8_t)~RTC_CR_BYPSHAD;
00690   }
00691 
00692   /* Enable the write protection for RTC registers */
00693   RTC->WPR = 0xFF;
00694 }
00695 
00696 /**
00697   * @}
00698   */
00699 
00700 /** @defgroup RTC_Group2 Time and Date configuration functions
00701  *  @brief   Time and Date configuration functions 
00702  *
00703 @verbatim   
00704  ===============================================================================
00705                ##### Time and Date configuration functions #####
00706  ===============================================================================  
00707     [..] This section provide functions allowing to program and read the RTC Calendar
00708          (Time and Date).
00709 
00710 @endverbatim
00711   * @{
00712   */
00713 
00714 /**
00715   * @brief  Set the RTC current time.
00716   * @param  RTC_Format: specifies the format of the entered parameters.
00717   *   This parameter can be  one of the following values:
00718   *     @arg RTC_Format_BIN:  Binary data format 
00719   *     @arg RTC_Format_BCD:  BCD data format
00720   * @param  RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that contains 
00721   *                        the time configuration information for the RTC.     
00722   * @retval An ErrorStatus enumeration value:
00723   *          - SUCCESS: RTC Time register is configured
00724   *          - ERROR: RTC Time register is not configured
00725   */
00726 ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
00727 {
00728   uint32_t tmpreg = 0;
00729   ErrorStatus status = ERROR;
00730     
00731   /* Check the parameters */
00732   assert_param(IS_RTC_FORMAT(RTC_Format));
00733   
00734   if (RTC_Format == RTC_Format_BIN)
00735   {
00736     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
00737     {
00738       assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
00739       assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
00740     } 
00741     else
00742     {
00743       RTC_TimeStruct->RTC_H12 = 0x00;
00744       assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
00745     }
00746     assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
00747     assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
00748   }
00749   else
00750   {
00751     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
00752     {
00753       tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
00754       assert_param(IS_RTC_HOUR12(tmpreg));
00755       assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12)); 
00756     } 
00757     else
00758     {
00759       RTC_TimeStruct->RTC_H12 = 0x00;
00760       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
00761     }
00762     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
00763     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
00764   }
00765   
00766   /* Check the input parameters format */
00767   if (RTC_Format != RTC_Format_BIN)
00768   {
00769     tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \
00770              ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \
00771              ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \
00772              ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16)); 
00773   }  
00774   else
00775   {
00776     tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \
00777                    ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \
00778                    ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \
00779                    (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));
00780   }  
00781 
00782   /* Disable the write protection for RTC registers */
00783   RTC->WPR = 0xCA;
00784   RTC->WPR = 0x53;
00785 
00786   /* Set Initialization mode */
00787   if (RTC_EnterInitMode() == ERROR)
00788   {
00789     status = ERROR;
00790   } 
00791   else
00792   {
00793     /* Set the RTC_TR register */
00794     RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
00795 
00796     /* Exit Initialization mode */
00797     RTC_ExitInitMode(); 
00798 
00799     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
00800     if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
00801     {
00802       if (RTC_WaitForSynchro() == ERROR)
00803       {
00804         status = ERROR;
00805       }
00806       else
00807       {
00808         status = SUCCESS;
00809       }
00810     }
00811     else
00812     {
00813       status = SUCCESS;
00814     }
00815   
00816   }
00817   /* Enable the write protection for RTC registers */
00818   RTC->WPR = 0xFF;
00819     
00820   return status;
00821 }
00822 
00823 /**
00824   * @brief  Fills each RTC_TimeStruct member with its default value
00825   *         (Time = 00h:00min:00sec).
00826   * @param  RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be 
00827   *         initialized.
00828   * @retval None
00829   */
00830 void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
00831 {
00832   /* Time = 00h:00min:00sec */
00833   RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
00834   RTC_TimeStruct->RTC_Hours = 0;
00835   RTC_TimeStruct->RTC_Minutes = 0;
00836   RTC_TimeStruct->RTC_Seconds = 0; 
00837 }
00838 
00839 /**
00840   * @brief  Get the RTC current Time.
00841   * @param  RTC_Format: specifies the format of the returned parameters.
00842   *   This parameter can be  one of the following values:
00843   *     @arg RTC_Format_BIN:  Binary data format 
00844   *     @arg RTC_Format_BCD:  BCD data format
00845   * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will 
00846   *                        contain the returned current time configuration.     
00847   * @retval None
00848   */
00849 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
00850 {
00851   uint32_t tmpreg = 0;
00852 
00853   /* Check the parameters */
00854   assert_param(IS_RTC_FORMAT(RTC_Format));
00855 
00856   /* Get the RTC_TR register */
00857   tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK); 
00858   
00859   /* Fill the structure fields with the read parameters */
00860   RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
00861   RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
00862   RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
00863   RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);  
00864 
00865   /* Check the input parameters format */
00866   if (RTC_Format == RTC_Format_BIN)
00867   {
00868     /* Convert the structure parameters to Binary format */
00869     RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
00870     RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
00871     RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);   
00872   }
00873 }
00874 
00875 /**
00876   * @brief  Gets the RTC current Calendar Subseconds value.
00877   * @note   This function freeze the Time and Date registers after reading the 
00878   *         SSR register.
00879   * @param  None
00880   * @retval RTC current Calendar Subseconds value.
00881   */
00882 uint32_t RTC_GetSubSecond(void)
00883 {
00884   uint32_t tmpreg = 0;
00885   
00886   /* Get subseconds values from the correspondent registers*/
00887   tmpreg = (uint32_t)(RTC->SSR);
00888   
00889   /* Read DR register to unfroze calendar registers */
00890   (void) (RTC->DR);
00891   
00892   return (tmpreg);
00893 }
00894 
00895 /**
00896   * @brief  Set the RTC current date.
00897   * @param  RTC_Format: specifies the format of the entered parameters.
00898   *   This parameter can be  one of the following values:
00899   *     @arg RTC_Format_BIN:  Binary data format 
00900   *     @arg RTC_Format_BCD:  BCD data format
00901   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains 
00902   *                         the date configuration information for the RTC.
00903   * @retval An ErrorStatus enumeration value:
00904   *          - SUCCESS: RTC Date register is configured
00905   *          - ERROR: RTC Date register is not configured
00906   */
00907 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
00908 {
00909   uint32_t tmpreg = 0;
00910   ErrorStatus status = ERROR;
00911   
00912   /* Check the parameters */
00913   assert_param(IS_RTC_FORMAT(RTC_Format));
00914 
00915   if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
00916   {
00917     RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
00918   }  
00919   if (RTC_Format == RTC_Format_BIN)
00920   {
00921     assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
00922     assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
00923     assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
00924   }
00925   else
00926   {
00927     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
00928     tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
00929     assert_param(IS_RTC_MONTH(tmpreg));
00930     tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
00931     assert_param(IS_RTC_DATE(tmpreg));
00932   }
00933   assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
00934 
00935   /* Check the input parameters format */
00936   if (RTC_Format != RTC_Format_BIN)
00937   {
00938     tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
00939               (((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
00940               ((uint32_t)RTC_DateStruct->RTC_Date) | \
00941               (((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13)); 
00942   }  
00943   else
00944   {
00945     tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
00946               ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
00947               ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
00948               ((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
00949   }
00950 
00951   /* Disable the write protection for RTC registers */
00952   RTC->WPR = 0xCA;
00953   RTC->WPR = 0x53;
00954 
00955   /* Set Initialization mode */
00956   if (RTC_EnterInitMode() == ERROR)
00957   {
00958     status = ERROR;
00959   } 
00960   else
00961   {
00962     /* Set the RTC_DR register */
00963     RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
00964 
00965     /* Exit Initialization mode */
00966     RTC_ExitInitMode(); 
00967 
00968     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
00969     if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
00970     {
00971       if (RTC_WaitForSynchro() == ERROR)
00972       {
00973         status = ERROR;
00974       }
00975       else
00976       {
00977         status = SUCCESS;
00978       }
00979     }
00980     else
00981     {
00982       status = SUCCESS;
00983     }
00984   }
00985   /* Enable the write protection for RTC registers */
00986   RTC->WPR = 0xFF;
00987   
00988   return status;
00989 }
00990 
00991 /**
00992   * @brief  Fills each RTC_DateStruct member with its default value
00993   *         (Monday, January 01 xx00).
00994   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure which will be 
00995   *         initialized.
00996   * @retval None
00997   */
00998 void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
00999 {
01000   /* Monday, January 01 xx00 */
01001   RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;
01002   RTC_DateStruct->RTC_Date = 1;
01003   RTC_DateStruct->RTC_Month = RTC_Month_January;
01004   RTC_DateStruct->RTC_Year = 0;
01005 }
01006 
01007 /**
01008   * @brief  Get the RTC current date.
01009   * @param  RTC_Format: specifies the format of the returned parameters.
01010   *   This parameter can be one of the following values:
01011   *     @arg RTC_Format_BIN: Binary data format 
01012   *     @arg RTC_Format_BCD: BCD data format
01013   * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that will 
01014   *                        contain the returned current date configuration.
01015   * @retval None
01016   */
01017 void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
01018 {
01019   uint32_t tmpreg = 0;
01020 
01021   /* Check the parameters */
01022   assert_param(IS_RTC_FORMAT(RTC_Format));
01023   
01024   /* Get the RTC_TR register */
01025   tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK); 
01026 
01027   /* Fill the structure fields with the read parameters */
01028   RTC_DateStruct->RTC_Year = (uint8_t)((tmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
01029   RTC_DateStruct->RTC_Month = (uint8_t)((tmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
01030   RTC_DateStruct->RTC_Date = (uint8_t)(tmpreg & (RTC_DR_DT | RTC_DR_DU));
01031   RTC_DateStruct->RTC_WeekDay = (uint8_t)((tmpreg & (RTC_DR_WDU)) >> 13);  
01032 
01033   /* Check the input parameters format */
01034   if (RTC_Format == RTC_Format_BIN)
01035   {
01036     /* Convert the structure parameters to Binary format */
01037     RTC_DateStruct->RTC_Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year);
01038     RTC_DateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
01039     RTC_DateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
01040     RTC_DateStruct->RTC_WeekDay = (uint8_t)(RTC_DateStruct->RTC_WeekDay);   
01041   }
01042 }
01043 
01044 /**
01045   * @}
01046   */
01047 
01048 /** @defgroup RTC_Group3 Alarms configuration functions
01049  *  @brief   Alarms (Alarm A and Alarm B) configuration functions 
01050  *
01051 @verbatim   
01052  ===============================================================================
01053         ##### Alarms (Alarm A and Alarm B) configuration functions #####
01054  ===============================================================================  
01055     [..] This section provides functions allowing to program and read the RTC Alarms.
01056 
01057 @endverbatim
01058   * @{
01059   */
01060 
01061 /**
01062   * @brief  Set the specified RTC Alarm.
01063   * @note   The Alarm register can only be written when the corresponding Alarm
01064   *         is disabled (Use the RTC_AlarmCmd(DISABLE)).    
01065   * @param  RTC_Format: specifies the format of the returned parameters.
01066   *   This parameter can be one of the following values:
01067   *     @arg RTC_Format_BIN: Binary data format 
01068   *     @arg RTC_Format_BCD: BCD data format
01069   * @param  RTC_Alarm: specifies the alarm to be configured.
01070   *   This parameter can be one of the following values:
01071   *     @arg RTC_Alarm_A: to select Alarm A
01072   *     @arg RTC_Alarm_B: to select Alarm B  
01073   * @param  RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that 
01074   *                          contains the alarm configuration parameters.     
01075   * @retval None
01076   */
01077 void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
01078 {
01079   uint32_t tmpreg = 0;
01080   
01081   /* Check the parameters */
01082   assert_param(IS_RTC_FORMAT(RTC_Format));
01083   assert_param(IS_RTC_ALARM(RTC_Alarm));
01084   assert_param(IS_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
01085   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
01086 
01087   if (RTC_Format == RTC_Format_BIN)
01088   {
01089     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
01090     {
01091       assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
01092       assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
01093     } 
01094     else
01095     {
01096       RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
01097       assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
01098     }
01099     assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
01100     assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
01101     
01102     if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
01103     {
01104       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
01105     }
01106     else
01107     {
01108       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
01109     }
01110   }
01111   else
01112   {
01113     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
01114     {
01115       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
01116       assert_param(IS_RTC_HOUR12(tmpreg));
01117       assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
01118     } 
01119     else
01120     {
01121       RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
01122       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
01123     }
01124     
01125     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
01126     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
01127     
01128     if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
01129     {
01130       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
01131       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));    
01132     }
01133     else
01134     {
01135       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
01136       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));      
01137     }    
01138   }
01139 
01140   /* Check the input parameters format */
01141   if (RTC_Format != RTC_Format_BIN)
01142   {
01143     tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
01144               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
01145               ((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
01146               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
01147               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
01148               ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
01149               ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask)); 
01150   }  
01151   else
01152   {
01153     tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
01154               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
01155               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
01156               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
01157               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
01158               ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
01159               ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask)); 
01160   } 
01161 
01162   /* Disable the write protection for RTC registers */
01163   RTC->WPR = 0xCA;
01164   RTC->WPR = 0x53;
01165 
01166   /* Configure the Alarm register */
01167   if (RTC_Alarm == RTC_Alarm_A)
01168   {
01169     RTC->ALRMAR = (uint32_t)tmpreg;
01170   }
01171   else
01172   {
01173     RTC->ALRMBR = (uint32_t)tmpreg;
01174   }
01175 
01176   /* Enable the write protection for RTC registers */
01177   RTC->WPR = 0xFF;   
01178 }
01179 
01180 /**
01181   * @brief  Fills each RTC_AlarmStruct member with its default value
01182   *         (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
01183   *         all fields are masked).
01184   * @param  RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
01185   *         will be initialized.
01186   * @retval None
01187   */
01188 void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
01189 {
01190   /* Alarm Time Settings : Time = 00h:00mn:00sec */
01191   RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
01192   RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
01193   RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
01194   RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
01195 
01196   /* Alarm Date Settings : Date = 1st day of the month */
01197   RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
01198   RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
01199 
01200   /* Alarm Masks Settings : Mask =  all fields are not masked */
01201   RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
01202 }
01203 
01204 /**
01205   * @brief  Get the RTC Alarm value and masks.
01206   * @param  RTC_Format: specifies the format of the output parameters.
01207   *   This parameter can be one of the following values:
01208   *     @arg RTC_Format_BIN: Binary data format 
01209   *     @arg RTC_Format_BCD: BCD data format
01210   * @param  RTC_Alarm: specifies the alarm to be read.
01211   *   This parameter can be one of the following values:
01212   *     @arg RTC_Alarm_A: to select Alarm A
01213   *     @arg RTC_Alarm_B: to select Alarm B  
01214   * @param  RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will 
01215   *                          contains the output alarm configuration values.     
01216   * @retval None
01217   */
01218 void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
01219 {
01220   uint32_t tmpreg = 0;
01221 
01222   /* Check the parameters */
01223   assert_param(IS_RTC_FORMAT(RTC_Format));
01224   assert_param(IS_RTC_ALARM(RTC_Alarm)); 
01225 
01226   /* Get the RTC_ALRMxR register */
01227   if (RTC_Alarm == RTC_Alarm_A)
01228   {
01229     tmpreg = (uint32_t)(RTC->ALRMAR);
01230   }
01231   else
01232   {
01233     tmpreg = (uint32_t)(RTC->ALRMBR);
01234   }
01235 
01236   /* Fill the structure with the read parameters */
01237   RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \
01238                                                      RTC_ALRMAR_HU)) >> 16);
01239   RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \
01240                                                      RTC_ALRMAR_MNU)) >> 8);
01241   RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \
01242                                                      RTC_ALRMAR_SU));
01243   RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
01244   RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
01245   RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
01246   RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);
01247 
01248   if (RTC_Format == RTC_Format_BIN)
01249   {
01250     RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
01251                                                         RTC_AlarmTime.RTC_Hours);
01252     RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
01253                                                         RTC_AlarmTime.RTC_Minutes);
01254     RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
01255                                                         RTC_AlarmTime.RTC_Seconds);
01256     RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
01257   }  
01258 }
01259 
01260 /**
01261   * @brief  Enables or disables the specified RTC Alarm.
01262   * @param  RTC_Alarm: specifies the alarm to be configured.
01263   *   This parameter can be any combination of the following values:
01264   *     @arg RTC_Alarm_A: to select Alarm A
01265   *     @arg RTC_Alarm_B: to select Alarm B  
01266   * @param  NewState: new state of the specified alarm.
01267   *   This parameter can be: ENABLE or DISABLE.
01268   * @retval An ErrorStatus enumeration value:
01269   *          - SUCCESS: RTC Alarm is enabled/disabled
01270   *          - ERROR: RTC Alarm is not enabled/disabled  
01271   */
01272 ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
01273 {
01274   __IO uint32_t alarmcounter = 0x00;
01275   uint32_t alarmstatus = 0x00;
01276   ErrorStatus status = ERROR;
01277     
01278   /* Check the parameters */
01279   assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
01280   assert_param(IS_FUNCTIONAL_STATE(NewState));
01281 
01282   /* Disable the write protection for RTC registers */
01283   RTC->WPR = 0xCA;
01284   RTC->WPR = 0x53;
01285 
01286   /* Configure the Alarm state */
01287   if (NewState != DISABLE)
01288   {
01289     RTC->CR |= (uint32_t)RTC_Alarm;
01290 
01291     status = SUCCESS;    
01292   }
01293   else
01294   { 
01295     /* Disable the Alarm in RTC_CR register */
01296     RTC->CR &= (uint32_t)~RTC_Alarm;
01297    
01298     /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
01299     do
01300     {
01301       alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
01302       alarmcounter++;  
01303     } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
01304     
01305     if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
01306     {
01307       status = ERROR;
01308     } 
01309     else
01310     {
01311       status = SUCCESS;
01312     }        
01313   } 
01314 
01315   /* Enable the write protection for RTC registers */
01316   RTC->WPR = 0xFF;
01317   
01318   return status;
01319 }
01320 
01321 /**
01322   * @brief  Configures the RTC AlarmA/B Subseconds value and mask.
01323   * @note   This function is performed only when the Alarm is disabled. 
01324   * @param  RTC_Alarm: specifies the alarm to be configured.
01325   *   This parameter can be one of the following values:
01326   *     @arg RTC_Alarm_A: to select Alarm A
01327   *     @arg RTC_Alarm_B: to select Alarm B
01328   * @param  RTC_AlarmSubSecondValue: specifies the Subseconds value.
01329   *   This parameter can be a value from 0 to 0x00007FFF.
01330   * @param  RTC_AlarmSubSecondMask:  specifies the Subseconds Mask.
01331   *   This parameter can be any combination of the following values:
01332   *     @arg RTC_AlarmSubSecondMask_All    : All Alarm SS fields are masked.
01333   *                                          There is no comparison on sub seconds for Alarm.
01334   *     @arg RTC_AlarmSubSecondMask_SS14_1 : SS[14:1] are don't care in Alarm comparison.
01335   *                                          Only SS[0] is compared
01336   *     @arg RTC_AlarmSubSecondMask_SS14_2 : SS[14:2] are don't care in Alarm comparison.
01337   *                                          Only SS[1:0] are compared
01338   *     @arg RTC_AlarmSubSecondMask_SS14_3 : SS[14:3] are don't care in Alarm comparison.
01339   *                                          Only SS[2:0] are compared
01340   *     @arg RTC_AlarmSubSecondMask_SS14_4 : SS[14:4] are don't care in Alarm comparison.
01341   *                                          Only SS[3:0] are compared
01342   *     @arg RTC_AlarmSubSecondMask_SS14_5 : SS[14:5] are don't care in Alarm comparison.
01343   *                                          Only SS[4:0] are compared
01344   *     @arg RTC_AlarmSubSecondMask_SS14_6 : SS[14:6] are don't care in Alarm comparison.
01345   *                                          Only SS[5:0] are compared
01346   *     @arg RTC_AlarmSubSecondMask_SS14_7 : SS[14:7] are don't care in Alarm comparison.
01347   *                                          Only SS[6:0] are compared
01348   *     @arg RTC_AlarmSubSecondMask_SS14_8 : SS[14:8] are don't care in Alarm comparison.
01349   *                                          Only SS[7:0] are compared
01350   *     @arg RTC_AlarmSubSecondMask_SS14_9 : SS[14:9] are don't care in Alarm comparison.
01351   *                                          Only SS[8:0] are compared
01352   *     @arg RTC_AlarmSubSecondMask_SS14_10: SS[14:10] are don't care in Alarm comparison.
01353   *                                          Only SS[9:0] are compared
01354   *     @arg RTC_AlarmSubSecondMask_SS14_11: SS[14:11] are don't care in Alarm comparison.
01355   *                                          Only SS[10:0] are compared
01356   *     @arg RTC_AlarmSubSecondMask_SS14_12: SS[14:12] are don't care in Alarm comparison.
01357   *                                          Only SS[11:0] are compared
01358   *     @arg RTC_AlarmSubSecondMask_SS14_13: SS[14:13] are don't care in Alarm comparison.
01359   *                                          Only SS[12:0] are compared
01360   *     @arg RTC_AlarmSubSecondMask_SS14   : SS[14] is don't care in Alarm comparison.
01361   *                                          Only SS[13:0] are compared
01362   *     @arg RTC_AlarmSubSecondMask_None   : SS[14:0] are compared and must match
01363   *                                          to activate alarm
01364   * @retval None
01365   */
01366 void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)
01367 {
01368   uint32_t tmpreg = 0;
01369 
01370   /* Check the parameters */
01371   assert_param(IS_RTC_ALARM(RTC_Alarm));
01372   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
01373   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(RTC_AlarmSubSecondMask));
01374   
01375   /* Disable the write protection for RTC registers */
01376   RTC->WPR = 0xCA;
01377   RTC->WPR = 0x53;
01378   
01379   /* Configure the Alarm A or Alarm B SubSecond registers */
01380   tmpreg = (uint32_t) (uint32_t)(RTC_AlarmSubSecondValue) | (uint32_t)(RTC_AlarmSubSecondMask);
01381   
01382   if (RTC_Alarm == RTC_Alarm_A)
01383   {
01384     /* Configure the AlarmA SubSecond register */
01385     RTC->ALRMASSR = tmpreg;
01386   }
01387   else
01388   {
01389     /* Configure the Alarm B SubSecond register */
01390     RTC->ALRMBSSR = tmpreg;
01391   }
01392 
01393   /* Enable the write protection for RTC registers */
01394   RTC->WPR = 0xFF;
01395 
01396 }
01397 
01398 /**
01399   * @brief  Gets the RTC Alarm Subseconds value.
01400   * @param  RTC_Alarm: specifies the alarm to be read.
01401   *   This parameter can be one of the following values:
01402   *     @arg RTC_Alarm_A: to select Alarm A
01403   *     @arg RTC_Alarm_B: to select Alarm B
01404   * @param  None
01405   * @retval RTC Alarm Subseconds value.
01406   */
01407 uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
01408 {
01409   uint32_t tmpreg = 0;
01410   
01411   /* Get the RTC_ALRMxR register */
01412   if (RTC_Alarm == RTC_Alarm_A)
01413   {
01414     tmpreg = (uint32_t)((RTC->ALRMASSR) & RTC_ALRMASSR_SS);
01415   }
01416   else
01417   {
01418     tmpreg = (uint32_t)((RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
01419   } 
01420   
01421   return (tmpreg);
01422 }
01423 
01424 /**
01425   * @}
01426   */
01427 
01428 /** @defgroup RTC_Group4 WakeUp Timer configuration functions
01429  *  @brief   WakeUp Timer configuration functions 
01430  *
01431 @verbatim   
01432  ===============================================================================
01433                 ##### WakeUp Timer configuration functions #####
01434  ===============================================================================  
01435     [..] This section provide functions allowing to program and read the RTC WakeUp.
01436 
01437 @endverbatim
01438   * @{
01439   */
01440 
01441 /**
01442   * @brief  Configures the RTC Wakeup clock source.
01443   * @note   The WakeUp Clock source can only be changed when the RTC WakeUp
01444   *         is disabled (Use the RTC_WakeUpCmd(DISABLE)).
01445   * @param  RTC_WakeUpClock: Wakeup Clock source.
01446   *   This parameter can be one of the following values:
01447   *     @arg RTC_WakeUpClock_RTCCLK_Div16: RTC Wakeup Counter Clock = RTCCLK/16
01448   *     @arg RTC_WakeUpClock_RTCCLK_Div8: RTC Wakeup Counter Clock = RTCCLK/8
01449   *     @arg RTC_WakeUpClock_RTCCLK_Div4: RTC Wakeup Counter Clock = RTCCLK/4
01450   *     @arg RTC_WakeUpClock_RTCCLK_Div2: RTC Wakeup Counter Clock = RTCCLK/2
01451   *     @arg RTC_WakeUpClock_CK_SPRE_16bits: RTC Wakeup Counter Clock = CK_SPRE
01452   *     @arg RTC_WakeUpClock_CK_SPRE_17bits: RTC Wakeup Counter Clock = CK_SPRE
01453   * @retval None
01454   */
01455 void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
01456 {
01457   /* Check the parameters */
01458   assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
01459 
01460   /* Disable the write protection for RTC registers */
01461   RTC->WPR = 0xCA;
01462   RTC->WPR = 0x53;
01463 
01464   /* Clear the Wakeup Timer clock source bits in CR register */
01465   RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;
01466 
01467   /* Configure the clock source */
01468   RTC->CR |= (uint32_t)RTC_WakeUpClock;
01469   
01470   /* Enable the write protection for RTC registers */
01471   RTC->WPR = 0xFF;
01472 }
01473 
01474 /**
01475   * @brief  Configures the RTC Wakeup counter.
01476   * @note   The RTC WakeUp counter can only be written when the RTC WakeUp
01477   *         is disabled (Use the RTC_WakeUpCmd(DISABLE)).
01478   * @param  RTC_WakeUpCounter: specifies the WakeUp counter.
01479   *   This parameter can be a value from 0x0000 to 0xFFFF. 
01480   * @retval None
01481   */
01482 void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
01483 {
01484   /* Check the parameters */
01485   assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));
01486   
01487   /* Disable the write protection for RTC registers */
01488   RTC->WPR = 0xCA;
01489   RTC->WPR = 0x53;
01490   
01491   /* Configure the Wakeup Timer counter */
01492   RTC->WUTR = (uint32_t)RTC_WakeUpCounter;
01493   
01494   /* Enable the write protection for RTC registers */
01495   RTC->WPR = 0xFF;
01496 }
01497 
01498 /**
01499   * @brief  Returns the RTC WakeUp timer counter value.
01500   * @param  None
01501   * @retval The RTC WakeUp Counter value.
01502   */
01503 uint32_t RTC_GetWakeUpCounter(void)
01504 {
01505   /* Get the counter value */
01506   return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));
01507 }
01508 
01509 /**
01510   * @brief  Enables or Disables the RTC WakeUp timer.
01511   * @param  NewState: new state of the WakeUp timer.
01512   *   This parameter can be: ENABLE or DISABLE.
01513   * @retval None
01514   */
01515 ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
01516 {
01517   __IO uint32_t wutcounter = 0x00;
01518   uint32_t wutwfstatus = 0x00;
01519   ErrorStatus status = ERROR;
01520   
01521   /* Check the parameters */
01522   assert_param(IS_FUNCTIONAL_STATE(NewState));
01523 
01524   /* Disable the write protection for RTC registers */
01525   RTC->WPR = 0xCA;
01526   RTC->WPR = 0x53;
01527 
01528   if (NewState != DISABLE)
01529   {
01530     /* Enable the Wakeup Timer */
01531     RTC->CR |= (uint32_t)RTC_CR_WUTE;
01532     status = SUCCESS;    
01533   }
01534   else
01535   {
01536     /* Disable the Wakeup Timer */
01537     RTC->CR &= (uint32_t)~RTC_CR_WUTE;
01538     /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
01539     do
01540     {
01541       wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
01542       wutcounter++;  
01543     } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
01544     
01545     if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
01546     {
01547       status = ERROR;
01548     }
01549     else
01550     {
01551       status = SUCCESS;
01552     }    
01553   }
01554 
01555   /* Enable the write protection for RTC registers */
01556   RTC->WPR = 0xFF;
01557   
01558   return status;
01559 }
01560 
01561 /**
01562   * @}
01563   */
01564 
01565 /** @defgroup RTC_Group5 Daylight Saving configuration functions
01566  *  @brief   Daylight Saving configuration functions 
01567  *
01568 @verbatim   
01569  ===============================================================================
01570                 ##### Daylight Saving configuration functions #####
01571  ===============================================================================  
01572     [..] This section provide functions allowing to configure the RTC DayLight Saving.
01573 
01574 @endverbatim
01575   * @{
01576   */
01577 
01578 /**
01579   * @brief  Adds or substract one hour from the current time.
01580   * @param  RTC_DayLightSaveOperation: the value of hour adjustment. 
01581   *   This parameter can be one of the following values:
01582   *     @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)
01583   *     @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)
01584   * @param  RTC_StoreOperation: Specifies the value to be written in the BCK bit 
01585   *                            in CR register to store the operation.
01586   *   This parameter can be one of the following values:
01587   *     @arg RTC_StoreOperation_Reset: BCK Bit Reset
01588   *     @arg RTC_StoreOperation_Set: BCK Bit Set
01589   * @retval None
01590   */
01591 void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
01592 {
01593   /* Check the parameters */
01594   assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
01595   assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
01596 
01597   /* Disable the write protection for RTC registers */
01598   RTC->WPR = 0xCA;
01599   RTC->WPR = 0x53;
01600 
01601   /* Clear the bits to be configured */
01602   RTC->CR &= (uint32_t)~(RTC_CR_BCK);
01603 
01604   /* Configure the RTC_CR register */
01605   RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
01606 
01607   /* Enable the write protection for RTC registers */
01608   RTC->WPR = 0xFF;
01609 }
01610 
01611 /**
01612   * @brief  Returns the RTC Day Light Saving stored operation.
01613   * @param  None
01614   * @retval RTC Day Light Saving stored operation.
01615   *          - RTC_StoreOperation_Reset
01616   *          - RTC_StoreOperation_Set
01617   */
01618 uint32_t RTC_GetStoreOperation(void)
01619 {
01620   return (RTC->CR & RTC_CR_BCK);
01621 }
01622 
01623 /**
01624   * @}
01625   */
01626 
01627 /** @defgroup RTC_Group6 Output pin Configuration function
01628  *  @brief   Output pin Configuration function 
01629  *
01630 @verbatim   
01631  ===============================================================================
01632                    ##### Output pin Configuration function #####
01633  ===============================================================================
01634     [..] This section provide functions allowing to configure the RTC Output source.
01635 
01636 @endverbatim
01637   * @{
01638   */
01639 
01640 /**
01641   * @brief  Configures the RTC output source (AFO_ALARM).
01642   * @param  RTC_Output: Specifies which signal will be routed to the RTC output. 
01643   *   This parameter can be one of the following values:
01644   *     @arg RTC_Output_Disable: No output selected
01645   *     @arg RTC_Output_AlarmA: signal of AlarmA mapped to output
01646   *     @arg RTC_Output_AlarmB: signal of AlarmB mapped to output
01647   *     @arg RTC_Output_WakeUp: signal of WakeUp mapped to output
01648   * @param  RTC_OutputPolarity: Specifies the polarity of the output signal. 
01649   *   This parameter can be one of the following:
01650   *     @arg RTC_OutputPolarity_High: The output pin is high when the 
01651   *                                 ALRAF/ALRBF/WUTF is high (depending on OSEL)
01652   *     @arg RTC_OutputPolarity_Low: The output pin is low when the 
01653   *                                 ALRAF/ALRBF/WUTF is high (depending on OSEL)
01654   * @retval None
01655   */
01656 void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
01657 {
01658   /* Check the parameters */
01659   assert_param(IS_RTC_OUTPUT(RTC_Output));
01660   assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
01661 
01662   /* Disable the write protection for RTC registers */
01663   RTC->WPR = 0xCA;
01664   RTC->WPR = 0x53;
01665 
01666   /* Clear the bits to be configured */
01667   RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
01668 
01669   /* Configure the output selection and polarity */
01670   RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
01671 
01672   /* Enable the write protection for RTC registers */
01673   RTC->WPR = 0xFF;
01674 }
01675 
01676 /**
01677   * @}
01678   */
01679 
01680 /** @defgroup RTC_Group7 Digital Calibration configuration functions
01681  *  @brief   Digital Calibration configuration functions 
01682  *
01683 @verbatim   
01684  ===============================================================================
01685             ##### Digital Calibration configuration functions #####
01686  ===============================================================================
01687 
01688 @endverbatim
01689   * @{
01690   */
01691 
01692 /**
01693   * @brief  Enables or disables the RTC clock to be output through the relative 
01694   *         pin.
01695   * @param  NewState: new state of the digital calibration Output.
01696   *   This parameter can be: ENABLE or DISABLE.
01697   * @retval None
01698   */
01699 void RTC_CalibOutputCmd(FunctionalState NewState)
01700 {
01701   /* Check the parameters */
01702   assert_param(IS_FUNCTIONAL_STATE(NewState));
01703   
01704   /* Disable the write protection for RTC registers */
01705   RTC->WPR = 0xCA;
01706   RTC->WPR = 0x53;
01707   
01708   if (NewState != DISABLE)
01709   {
01710     /* Enable the RTC clock output */
01711     RTC->CR |= (uint32_t)RTC_CR_COE;
01712   }
01713   else
01714   { 
01715     /* Disable the RTC clock output */
01716     RTC->CR &= (uint32_t)~RTC_CR_COE;
01717   }
01718   
01719   /* Enable the write protection for RTC registers */
01720   RTC->WPR = 0xFF; 
01721 }
01722 
01723 /**
01724   * @brief  Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
01725   * @param  RTC_CalibOutput : Select the Calibration output Selection .
01726   *   This parameter can be one of the following values:
01727   *     @arg RTC_CalibOutput_512Hz: A signal has a regular waveform at 512Hz. 
01728   *     @arg RTC_CalibOutput_1Hz  : A signal has a regular waveform at 1Hz.
01729   * @retval None
01730 */
01731 void RTC_CalibOutputConfig(uint32_t RTC_CalibOutput)
01732 {
01733   /* Check the parameters */
01734   assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
01735 
01736   /* Disable the write protection for RTC registers */
01737   RTC->WPR = 0xCA;
01738   RTC->WPR = 0x53;
01739   
01740   /*clear flags before config*/
01741   RTC->CR &= (uint32_t)~(RTC_CR_COSEL);
01742 
01743   /* Configure the RTC_CR register */
01744   RTC->CR |= (uint32_t)RTC_CalibOutput;
01745 
01746   /* Enable the write protection for RTC registers */
01747   RTC->WPR = 0xFF;
01748 }
01749 
01750 /**
01751   * @brief  Configures the Smooth Calibration Settings.
01752   * @param  RTC_SmoothCalibPeriod : Select the Smooth Calibration Period.
01753   *   This parameter can be can be one of the following values:
01754   *     @arg RTC_SmoothCalibPeriod_32sec : The smooth calibration periode is 32s.
01755   *     @arg RTC_SmoothCalibPeriod_16sec : The smooth calibration periode is 16s.
01756   *     @arg RTC_SmoothCalibPeriod_8sec  : The smooth calibration periode is 8s.
01757   * @param  RTC_SmoothCalibPlusPulses : Select to Set or reset the CALP bit.
01758   *   This parameter can be one of the following values:
01759   *     @arg RTC_SmoothCalibPlusPulses_Set  : Add one RTCCLK puls every 2**11 pulses.
01760   *     @arg RTC_SmoothCalibPlusPulses_Reset: No RTCCLK pulses are added.
01761   * @param  RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
01762   *   This parameter can be one any value from 0 to 0x000001FF.
01763   * @retval An ErrorStatus enumeration value:
01764   *          - SUCCESS: RTC Calib registers are configured
01765   *          - ERROR: RTC Calib registers are not configured
01766 */
01767 ErrorStatus RTC_SmoothCalibConfig(uint32_t RTC_SmoothCalibPeriod,
01768                                   uint32_t RTC_SmoothCalibPlusPulses,
01769                                   uint32_t RTC_SmouthCalibMinusPulsesValue)
01770 {
01771   ErrorStatus status = ERROR;
01772   uint32_t recalpfcount = 0;
01773 
01774   /* Check the parameters */
01775   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod));
01776   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
01777   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
01778 
01779   /* Disable the write protection for RTC registers */
01780   RTC->WPR = 0xCA;
01781   RTC->WPR = 0x53;
01782   
01783   /* check if a calibration is pending*/
01784   if ((RTC->ISR & RTC_ISR_RECALPF) != RESET)
01785   {
01786     /* wait until the Calibration is completed*/
01787     while (((RTC->ISR & RTC_ISR_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
01788     {
01789       recalpfcount++;
01790     }
01791   }
01792 
01793   /* check if the calibration pending is completed or if there is no calibration operation at all*/
01794   if ((RTC->ISR & RTC_ISR_RECALPF) == RESET)
01795   {
01796     /* Configure the Smooth calibration settings */
01797     RTC->CALR = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
01798 
01799     status = SUCCESS;
01800   }
01801   else
01802   {
01803     status = ERROR;
01804   }
01805 
01806   /* Enable the write protection for RTC registers */
01807   RTC->WPR = 0xFF;
01808   
01809   return (ErrorStatus)(status);
01810 }
01811 
01812 /**
01813   * @}
01814   */
01815 
01816 
01817 /** @defgroup RTC_Group8 TimeStamp configuration functions
01818  *  @brief   TimeStamp configuration functions 
01819  *
01820 @verbatim   
01821  ===============================================================================
01822                 ##### TimeStamp configuration functions #####
01823  ===============================================================================  
01824 
01825 @endverbatim
01826   * @{
01827   */
01828 
01829 /**
01830   * @brief  Enables or Disables the RTC TimeStamp functionality with the 
01831   *         specified time stamp pin stimulating edge.
01832   * @param  RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is 
01833   *         activated.
01834   *   This parameter can be one of the following:
01835   *     @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising 
01836   *                                    edge of the related pin.
01837   *     @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the 
01838   *                                     falling edge of the related pin.
01839   * @param  NewState: new state of the TimeStamp.
01840   *   This parameter can be: ENABLE or DISABLE.
01841   * @retval None
01842   */
01843 void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
01844 {
01845   uint32_t tmpreg = 0;
01846 
01847   /* Check the parameters */
01848   assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
01849   assert_param(IS_FUNCTIONAL_STATE(NewState));
01850 
01851   /* Get the RTC_CR register and clear the bits to be configured */
01852   tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
01853 
01854   /* Get the new configuration */
01855   if (NewState != DISABLE)
01856   {
01857     tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
01858   }
01859   else
01860   {
01861     tmpreg |= (uint32_t)(RTC_TimeStampEdge);
01862   }
01863 
01864   /* Disable the write protection for RTC registers */
01865   RTC->WPR = 0xCA;
01866   RTC->WPR = 0x53;
01867 
01868   /* Configure the Time Stamp TSEDGE and Enable bits */
01869   RTC->CR = (uint32_t)tmpreg;
01870 
01871   /* Enable the write protection for RTC registers */
01872   RTC->WPR = 0xFF;
01873 }
01874 
01875 /**
01876   * @brief  Gets the RTC TimeStamp value and masks.
01877   * @param  RTC_Format: specifies the format of the output parameters.
01878   *   This parameter can be one of the following values:
01879   *     @arg RTC_Format_BIN: Binary data format 
01880   *     @arg RTC_Format_BCD: BCD data format
01881   * @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will 
01882   *                             contains the TimeStamp time values. 
01883   * @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will 
01884   *                             contains the TimeStamp date values.     
01885   * @retval None
01886   */
01887 void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct, 
01888                                       RTC_DateTypeDef* RTC_StampDateStruct)
01889 {
01890   uint32_t tmptime = 0, tmpdate = 0;
01891 
01892   /* Check the parameters */
01893   assert_param(IS_RTC_FORMAT(RTC_Format));
01894 
01895   /* Get the TimeStamp time and date registers values */
01896   tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);
01897   tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);
01898 
01899   /* Fill the Time structure fields with the read parameters */
01900   RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
01901   RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
01902   RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
01903   RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);  
01904 
01905   /* Fill the Date structure fields with the read parameters */
01906   RTC_StampDateStruct->RTC_Year = 0;
01907   RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
01908   RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
01909   RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
01910 
01911   /* Check the input parameters format */
01912   if (RTC_Format == RTC_Format_BIN)
01913   {
01914     /* Convert the Time structure parameters to Binary format */
01915     RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);
01916     RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);
01917     RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);
01918 
01919     /* Convert the Date structure parameters to Binary format */
01920     RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);
01921     RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);
01922     RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);
01923   }
01924 }
01925 
01926 /**
01927   * @brief  Gets the RTC timestamp Subseconds value.
01928   * @param  None
01929   * @retval RTC current timestamp Subseconds value.
01930   */
01931 uint32_t RTC_GetTimeStampSubSecond(void)
01932 {
01933   /* Get timestamp subseconds values from the correspondent registers */
01934   return (uint32_t)(RTC->TSSSR);
01935 }
01936 
01937 /**
01938   * @}
01939   */
01940 
01941 /** @defgroup RTC_Group9 Tampers configuration functions
01942  *  @brief   Tampers configuration functions 
01943  *
01944 @verbatim   
01945  ===============================================================================
01946                 ##### Tampers configuration functions #####
01947  ===============================================================================  
01948 
01949 @endverbatim
01950   * @{
01951   */
01952 
01953 /**
01954   * @brief  Configures the select Tamper pin edge.
01955   * @param  RTC_Tamper: Selected tamper pin.
01956   *   This parameter can be any combination of the following values:
01957   *     @arg RTC_Tamper_1: Select Tamper 1.
01958   *     @arg RTC_Tamper_2: Select Tamper 2.
01959   *     @arg RTC_Tamper_3: Select Tamper 3.
01960   * @param  RTC_TamperTrigger: Specifies the trigger on the tamper pin that 
01961   *                            stimulates tamper event. 
01962   *   This parameter can be one of the following values:
01963   *     @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
01964   *     @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
01965   *     @arg RTC_TamperTrigger_LowLevel: Low Level of the tamper pin causes tamper event.
01966   *     @arg RTC_TamperTrigger_HighLevel: High Level of the tamper pin causes tamper event.
01967   * @retval None
01968   */
01969 void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
01970 {
01971   /* Check the parameters */
01972   assert_param(IS_RTC_TAMPER(RTC_Tamper)); 
01973   assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
01974  
01975   /* Check if the  active level for Tamper is rising edge (Low level)*/
01976   if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
01977   {  
01978     /* Configure the RTC_TAFCR register */
01979     RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1)); 
01980   }
01981   else
01982   { 
01983     /* Configure the RTC_TAFCR register */
01984     RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);  
01985   }  
01986 }
01987 
01988 /**
01989   * @brief  Enables or Disables the Tamper detection.
01990   * @param  RTC_Tamper: Selected tamper pin.
01991   *   This parameter can be any combination of the following values:
01992   *     @arg RTC_Tamper_1: Select Tamper 1.
01993   *     @arg RTC_Tamper_2: Select Tamper 2.
01994   *     @arg RTC_Tamper_3: Select Tamper 3.
01995   * @param  NewState: new state of the tamper pin.
01996   *         This parameter can be: ENABLE or DISABLE.                   
01997   * @retval None
01998   */
01999 void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
02000 {
02001   /* Check the parameters */
02002   assert_param(IS_RTC_TAMPER(RTC_Tamper));  
02003   assert_param(IS_FUNCTIONAL_STATE(NewState));
02004   
02005   if (NewState != DISABLE)
02006   {
02007     /* Enable the selected Tamper pin */
02008     RTC->TAFCR |= (uint32_t)RTC_Tamper;
02009   }
02010   else
02011   {
02012     /* Disable the selected Tamper pin */
02013     RTC->TAFCR &= (uint32_t)~RTC_Tamper;    
02014   }  
02015 }
02016 
02017 /**
02018   * @brief  Configures the Tampers Filter.
02019   * @param  RTC_TamperFilter: Specifies the tampers filter.
02020   *   This parameter can be one of the following values:
02021   *     @arg RTC_TamperFilter_Disable: Tamper filter is disabled.
02022   *     @arg RTC_TamperFilter_2Sample: Tamper is activated after 2 consecutive 
02023   *                                    samples at the active level 
02024   *     @arg RTC_TamperFilter_4Sample: Tamper is activated after 4 consecutive 
02025   *                                    samples at the active level
02026   *     @arg RTC_TamperFilter_8Sample: Tamper is activated after 8 consecutive 
02027   *                                    samples at the active level 
02028   * @retval None
02029   */
02030 void RTC_TamperFilterConfig(uint32_t RTC_TamperFilter)
02031 {
02032   /* Check the parameters */
02033   assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));
02034    
02035   /* Clear TAMPFLT[1:0] bits in the RTC_TAFCR register */
02036   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFLT);
02037 
02038   /* Configure the RTC_TAFCR register */
02039   RTC->TAFCR |= (uint32_t)RTC_TamperFilter;
02040 }
02041 
02042 /**
02043   * @brief  Configures the Tampers Sampling Frequency.
02044   * @param  RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency.
02045   *   This parameter can be one of the following values:
02046   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div32768: Each of the tamper inputs are sampled
02047   *                                           with a frequency =  RTCCLK / 32768
02048   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div16384: Each of the tamper inputs are sampled
02049   *                                           with a frequency =  RTCCLK / 16384
02050   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div8192: Each of the tamper inputs are sampled
02051   *                                           with a frequency =  RTCCLK / 8192
02052   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div4096: Each of the tamper inputs are sampled
02053   *                                           with a frequency =  RTCCLK / 4096
02054   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div2048: Each of the tamper inputs are sampled
02055   *                                           with a frequency =  RTCCLK / 2048
02056   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div1024: Each of the tamper inputs are sampled
02057   *                                           with a frequency =  RTCCLK / 1024
02058   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div512: Each of the tamper inputs are sampled
02059   *                                           with a frequency =  RTCCLK / 512  
02060   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div256: Each of the tamper inputs are sampled
02061   *                                           with a frequency =  RTCCLK / 256  
02062   * @retval None
02063   */
02064 void RTC_TamperSamplingFreqConfig(uint32_t RTC_TamperSamplingFreq)
02065 {
02066   /* Check the parameters */
02067   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq));
02068  
02069   /* Clear TAMPFREQ[2:0] bits in the RTC_TAFCR register */
02070   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFREQ);
02071 
02072   /* Configure the RTC_TAFCR register */
02073   RTC->TAFCR |= (uint32_t)RTC_TamperSamplingFreq;
02074 }
02075 
02076 /**
02077   * @brief  Configures the Tampers Pins input Precharge Duration.
02078   * @param  RTC_TamperPrechargeDuration: Specifies the Tampers Pins input
02079   *         Precharge Duration.
02080   *   This parameter can be one of the following values:
02081   *     @arg RTC_TamperPrechargeDuration_1RTCCLK: Tamper pins are pre-charged before sampling during 1 RTCCLK cycle
02082   *     @arg RTC_TamperPrechargeDuration_2RTCCLK: Tamper pins are pre-charged before sampling during 2 RTCCLK cycle
02083   *     @arg RTC_TamperPrechargeDuration_4RTCCLK: Tamper pins are pre-charged before sampling during 4 RTCCLK cycle    
02084   *     @arg RTC_TamperPrechargeDuration_8RTCCLK: Tamper pins are pre-charged before sampling during 8 RTCCLK cycle
02085   * @retval None
02086   */
02087 void RTC_TamperPinsPrechargeDuration(uint32_t RTC_TamperPrechargeDuration)
02088 {
02089   /* Check the parameters */
02090   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(RTC_TamperPrechargeDuration));
02091    
02092   /* Clear TAMPPRCH[1:0] bits in the RTC_TAFCR register */
02093   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPPRCH);
02094 
02095   /* Configure the RTC_TAFCR register */
02096   RTC->TAFCR |= (uint32_t)RTC_TamperPrechargeDuration;
02097 }
02098 
02099 /**
02100   * @brief  Enables or Disables the TimeStamp on Tamper Detection Event.
02101   * @note   The timestamp is valid even the TSE bit in tamper control register 
02102   *         is reset.   
02103   * @param  NewState: new state of the timestamp on tamper event.
02104   *         This parameter can be: ENABLE or DISABLE.
02105   * @retval None
02106   */
02107 void RTC_TimeStampOnTamperDetectionCmd(FunctionalState NewState)
02108 {
02109   /* Check the parameters */
02110   assert_param(IS_FUNCTIONAL_STATE(NewState));
02111    
02112   if (NewState != DISABLE)
02113   {
02114     /* Save timestamp on tamper detection event */
02115     RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPTS;
02116   }
02117   else
02118   {
02119     /* Tamper detection does not cause a timestamp to be saved */
02120     RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPTS;    
02121   }
02122 }
02123 
02124 /**
02125   * @brief  Enables or Disables the Precharge of Tamper pin.
02126   * @param  NewState: new state of tamper pull up.
02127   *   This parameter can be: ENABLE or DISABLE.                   
02128   * @retval None
02129   */
02130 void RTC_TamperPullUpCmd(FunctionalState NewState)
02131 {
02132   /* Check the parameters */
02133   assert_param(IS_FUNCTIONAL_STATE(NewState));
02134   
02135  if (NewState != DISABLE)
02136   {
02137     /* Enable precharge of the selected Tamper pin */
02138     RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPPUDIS; 
02139   }
02140   else
02141   {
02142     /* Disable precharge of the selected Tamper pin */
02143     RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPPUDIS;    
02144   } 
02145 }
02146 
02147 /**
02148   * @}
02149   */
02150 
02151 /** @defgroup RTC_Group10 Backup Data Registers configuration functions
02152  *  @brief   Backup Data Registers configuration functions  
02153  *
02154 @verbatim   
02155  ===============================================================================
02156           ##### Backup Data Registers configuration functions #####
02157  ===============================================================================  
02158 
02159 @endverbatim
02160   * @{
02161   */
02162 
02163 /**
02164   * @brief  Writes a data in a specified RTC Backup data register.
02165   * @param  RTC_BKP_DR: RTC Backup data Register number.
02166   *   This parameter can be: RTC_BKP_DRx where x can be from 0 to 15 to 
02167   *                          specify the register.
02168   * @param  Data: Data to be written in the specified RTC Backup data register.                     
02169   * @retval None
02170   */
02171 void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
02172 {
02173   __IO uint32_t tmp = 0;
02174   
02175   /* Check the parameters */
02176   assert_param(IS_RTC_BKP(RTC_BKP_DR));
02177 
02178   tmp = RTC_BASE + 0x50;
02179   tmp += (RTC_BKP_DR * 4);
02180 
02181   /* Write the specified register */
02182   *(__IO uint32_t *)tmp = (uint32_t)Data;
02183 }
02184 
02185 /**
02186   * @brief  Reads data from the specified RTC Backup data Register.
02187   * @param  RTC_BKP_DR: RTC Backup data Register number.
02188   *   This parameter can be: RTC_BKP_DRx where x can be from 0 to 15 to 
02189   *                          specify the register.                   
02190   * @retval None
02191   */
02192 uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
02193 {
02194   __IO uint32_t tmp = 0;
02195   
02196   /* Check the parameters */
02197   assert_param(IS_RTC_BKP(RTC_BKP_DR));
02198 
02199   tmp = RTC_BASE + 0x50;
02200   tmp += (RTC_BKP_DR * 4);
02201   
02202   /* Read the specified register */
02203   return (*(__IO uint32_t *)tmp);
02204 }
02205 
02206 /**
02207   * @}
02208   */
02209 
02210 /** @defgroup RTC_Group11 Output Type Config configuration functions
02211  *  @brief   Output Type Config configuration functions  
02212  *
02213 @verbatim   
02214  ===============================================================================
02215             ##### Output Type Config configuration functions #####
02216  ===============================================================================  
02217 
02218 @endverbatim
02219   * @{
02220   */
02221 
02222 /**
02223   * @brief  Configures the RTC Output Pin mode. 
02224   * @param  RTC_OutputType: specifies the RTC Output (PC13) pin mode.
02225   *   This parameter can be one of the following values:
02226   *     @arg RTC_OutputType_OpenDrain: RTC Output (PC13) is configured in 
02227   *                                    Open Drain mode.
02228   *     @arg RTC_OutputType_PushPull:  RTC Output (PC13) is configured in 
02229   *                                    Push Pull mode.    
02230   * @retval None
02231   */
02232 void RTC_OutputTypeConfig(uint32_t RTC_OutputType)
02233 {
02234   /* Check the parameters */
02235   assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
02236   
02237   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);
02238   RTC->TAFCR |= (uint32_t)(RTC_OutputType);  
02239 }
02240 
02241 /**
02242   * @}
02243   */
02244 
02245 /** @defgroup RTC_Group12 Shift control synchronisation functions
02246  *  @brief   Shift control synchronisation functions 
02247  *
02248 @verbatim   
02249  ===============================================================================
02250               ##### Shift control synchronisation functions #####
02251  ===============================================================================  
02252 
02253 @endverbatim
02254   * @{
02255   */
02256 
02257 /**
02258   * @brief  Configures the Synchronization Shift Control Settings.
02259   * @note   When REFCKON is set, firmware must not write to Shift control register 
02260   * @param  RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar.
02261   *   This parameter can be one of the following values :
02262   *     @arg RTC_ShiftAdd1S_Set  : Add one second to the clock calendar. 
02263   *     @arg RTC_ShiftAdd1S_Reset: No effect.
02264   * @param  RTC_ShiftSubFS: Select the number of Second Fractions to Substitute.
02265   *         This parameter can be one any value from 0 to 0x7FFF.
02266   * @retval An ErrorStatus enumeration value:
02267   *          - SUCCESS: RTC Shift registers are configured
02268   *          - ERROR: RTC Shift registers are not configured
02269 */
02270 ErrorStatus RTC_SynchroShiftConfig(uint32_t RTC_ShiftAdd1S, uint32_t RTC_ShiftSubFS)
02271 {
02272   ErrorStatus status = ERROR;
02273   uint32_t shpfcount = 0;
02274 
02275   /* Check the parameters */
02276   assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
02277   assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));
02278 
02279   /* Disable the write protection for RTC registers */
02280   RTC->WPR = 0xCA;
02281   RTC->WPR = 0x53;
02282   
02283   /* Check if a Shift is pending*/
02284   if ((RTC->ISR & RTC_ISR_SHPF) != RESET)
02285   {
02286     /* Wait until the shift is completed*/
02287     while (((RTC->ISR & RTC_ISR_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
02288     {
02289       shpfcount++;
02290     }
02291   }
02292 
02293   /* Check if the Shift pending is completed or if there is no Shift operation at all*/
02294   if ((RTC->ISR & RTC_ISR_SHPF) == RESET)
02295   {
02296     /* check if the reference clock detection is disabled */
02297     if((RTC->CR & RTC_CR_REFCKON) == RESET)
02298     {
02299       /* Configure the Shift settings */
02300       RTC->SHIFTR = (uint32_t)(uint32_t)(RTC_ShiftSubFS) | (uint32_t)(RTC_ShiftAdd1S);
02301     
02302       if(RTC_WaitForSynchro() == ERROR)
02303       {
02304         status = ERROR;
02305       }
02306       else
02307       {
02308         status = SUCCESS;
02309       }
02310     }
02311     else
02312     {
02313       status = ERROR;
02314     }
02315   }
02316   else
02317   {
02318     status = ERROR;
02319   }
02320 
02321   /* Enable the write protection for RTC registers */
02322   RTC->WPR = 0xFF;
02323   
02324   return (ErrorStatus)(status);
02325 }
02326 
02327 /**
02328   * @}
02329   */
02330 
02331 /** @defgroup RTC_Group13 Interrupts and flags management functions
02332  *  @brief   Interrupts and flags management functions  
02333  *
02334 @verbatim   
02335  ===============================================================================
02336                 ##### Interrupts and flags management functions #####
02337  ===============================================================================  
02338     [..] All RTC interrupts are connected to the EXTI controller.
02339          (+) To enable the RTC Alarm interrupt, the following sequence is required:
02340              (++) Configure and enable the EXTI Line 17 in interrupt mode and select 
02341                   the rising edge sensitivity using the EXTI_Init() function.
02342              (++) Configure and enable the RTC_Alarm IRQ channel in the NVIC using 
02343                   the NVIC_Init() function.
02344              (++) Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B)
02345                   using the RTC_SetAlarm() and RTC_AlarmCmd() functions.
02346          (+) To enable the RTC Wakeup interrupt, the following sequence is required:
02347              (++) Configure and enable the EXTI Line 20 in interrupt mode and select 
02348                   the rising edge sensitivity using the EXTI_Init() function.
02349              (++) Configure and enable the RTC_WKUP IRQ channel in the NVIC using
02350                   the NVIC_Init() function.
02351              (++) Configure the RTC to generate the RTC wakeup timer event using the 
02352                   RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd() 
02353                   functions.
02354          (+) To enable the RTC Tamper interrupt, the following sequence is required:
02355              (++) Configure and enable the EXTI Line 19 in interrupt mode and select 
02356                   the rising edge sensitivity using the EXTI_Init() function.
02357              (++) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using 
02358                   the NVIC_Init() function.
02359              (++) Configure the RTC to detect the RTC tamper event using the 
02360                   RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
02361          (+) To enable the RTC TimeStamp interrupt, the following sequence is required:
02362              (++) Configure and enable the EXTI Line 19 in interrupt mode and select
02363                   the rising edge sensitivity using the EXTI_Init() function.
02364              (++) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using 
02365                   the NVIC_Init() function.
02366              (++) Configure the RTC to detect the RTC time-stamp event using the 
02367                   RTC_TimeStampCmd() functions.
02368 
02369 @endverbatim
02370   * @{
02371   */
02372 
02373 /**
02374   * @brief  Enables or disables the specified RTC interrupts.
02375   * @param  RTC_IT: specifies the RTC interrupt sources to be enabled or disabled. 
02376   *   This parameter can be any combination of the following values:
02377   *     @arg RTC_IT_TS:  Time Stamp interrupt mask
02378   *     @arg RTC_IT_WUT:  WakeUp Timer interrupt mask
02379   *     @arg RTC_IT_ALRB:  Alarm B interrupt mask
02380   *     @arg RTC_IT_ALRA:  Alarm A interrupt mask
02381   *     @arg RTC_IT_TAMP: Tamper event interrupt mask
02382   * @param  NewState: new state of the specified RTC interrupts.
02383   *   This parameter can be: ENABLE or DISABLE.
02384   * @retval None
02385   */
02386 void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
02387 {
02388   /* Check the parameters */
02389   assert_param(IS_RTC_CONFIG_IT(RTC_IT));
02390   assert_param(IS_FUNCTIONAL_STATE(NewState));
02391 
02392   /* Disable the write protection for RTC registers */
02393   RTC->WPR = 0xCA;
02394   RTC->WPR = 0x53;
02395 
02396   if (NewState != DISABLE)
02397   {
02398     /* Configure the Interrupts in the RTC_CR register */
02399     RTC->CR |= (uint32_t)(RTC_IT & ~RTC_TAFCR_TAMPIE);
02400     /* Configure the Tamper Interrupt in the RTC_TAFCR */
02401     RTC->TAFCR |= (uint32_t)(RTC_IT & RTC_TAFCR_TAMPIE);
02402   }
02403   else
02404   {
02405     /* Configure the Interrupts in the RTC_CR register */
02406     RTC->CR &= (uint32_t)~(RTC_IT & (uint32_t)~RTC_TAFCR_TAMPIE);
02407     /* Configure the Tamper Interrupt in the RTC_TAFCR */
02408     RTC->TAFCR &= (uint32_t)~(RTC_IT & RTC_TAFCR_TAMPIE);
02409   }
02410   /* Enable the write protection for RTC registers */
02411   RTC->WPR = 0xFF; 
02412 }
02413 
02414 /**
02415   * @brief  Checks whether the specified RTC flag is set or not.
02416   * @param  RTC_FLAG: specifies the flag to check.
02417   *   This parameter can be one of the following values:
02418   *     @arg RTC_FLAG_RECALPF: RECALPF event flag
02419   *     @arg RTC_FLAG_TAMP3F: Tamper 3 event flag
02420   *     @arg RTC_FLAG_TAMP2F: Tamper 2 event flag   
02421   *     @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
02422   *     @arg RTC_FLAG_TSOVF: Time Stamp OverFlow flag
02423   *     @arg RTC_FLAG_TSF: Time Stamp event flag
02424   *     @arg RTC_FLAG_WUTF: WakeUp Timer flag
02425   *     @arg RTC_FLAG_ALRBF: Alarm B flag
02426   *     @arg RTC_FLAG_ALRAF: Alarm A flag
02427   *     @arg RTC_FLAG_INITF: Initialization mode flag
02428   *     @arg RTC_FLAG_RSF: Registers Synchronized flag
02429   *     @arg RTC_FLAG_INITS: Registers Configured flag
02430   *     @argRTC_FLAG_SHPF  : Shift operation pending flag.  
02431   *     @arg RTC_FLAG_WUTWF: WakeUp Timer Write flag
02432   *     @arg RTC_FLAG_ALRBWF: Alarm B Write flag
02433   *     @arg RTC_FLAG_ALRAWF: Alarm A write flag
02434   * @retval The new state of RTC_FLAG (SET or RESET).
02435   */
02436 FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
02437 {
02438   FlagStatus bitstatus = RESET;
02439   uint32_t tmpreg = 0;
02440   
02441   /* Check the parameters */
02442   assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
02443   
02444   /* Get all the flags */
02445   tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);
02446   
02447   /* Return the status of the flag */
02448   if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)
02449   {
02450     bitstatus = SET;
02451   }
02452   else
02453   {
02454     bitstatus = RESET;
02455   }
02456   return bitstatus;
02457 }
02458 
02459 /**
02460   * @brief  Clears the RTC's pending flags.
02461   * @param  RTC_FLAG: specifies the RTC flag to clear.
02462   *   This parameter can be any combination of the following values:
02463   *     @arg RTC_FLAG_TAMP3F: Tamper 3 event flag
02464   *     @arg RTC_FLAG_TAMP2F: Tamper 2 event flag
02465   *     @arg RTC_FLAG_TAMP1F: Tamper 1 event flag 
02466   *     @arg RTC_FLAG_TSOVF: Time Stamp Overflow flag 
02467   *     @arg RTC_FLAG_TSF: Time Stamp event flag
02468   *     @arg RTC_FLAG_WUTF: WakeUp Timer flag
02469   *     @arg RTC_FLAG_ALRBF: Alarm B flag
02470   *     @arg RTC_FLAG_ALRAF: Alarm A flag
02471   *     @arg RTC_FLAG_RSF: Registers Synchronized flag
02472   * @retval None
02473   */
02474 void RTC_ClearFlag(uint32_t RTC_FLAG)
02475 {
02476   /* Check the parameters */
02477   assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
02478 
02479   /* Clear the Flags in the RTC_ISR register */
02480   RTC->ISR = (uint32_t)((uint32_t)(~((RTC_FLAG | RTC_ISR_INIT)& 0x0001FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));    
02481 }
02482 
02483 /**
02484   * @brief  Checks whether the specified RTC interrupt has occurred or not.
02485   * @param  RTC_IT: specifies the RTC interrupt source to check.
02486   *   This parameter can be one of the following values:
02487   *     @arg RTC_IT_TS: Time Stamp interrupt 
02488   *     @arg RTC_IT_WUT: WakeUp Timer interrupt 
02489   *     @arg RTC_IT_ALRB: Alarm B interrupt 
02490   *     @arg RTC_IT_ALRA: Alarm A interrupt 
02491   *     @arg RTC_IT_TAMP1: Tamper1 event interrupt 
02492   *     @arg RTC_IT_TAMP2: Tamper2 event interrupt 
02493   *     @arg RTC_IT_TAMP3: Tamper3 event interrupt
02494   * @retval The new state of RTC_IT (SET or RESET).
02495   */
02496 ITStatus RTC_GetITStatus(uint32_t RTC_IT)
02497 {
02498   ITStatus bitstatus = RESET;
02499   uint32_t tmpreg = 0, enablestatus = 0;
02500  
02501   /* Check the parameters */
02502   assert_param(IS_RTC_GET_IT(RTC_IT));
02503   
02504   /* Get the TAMPER Interrupt enable bit and pending bit */
02505   tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));
02506  
02507   /* Get the Interrupt enable Status */
02508   enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & ((RTC_IT >> (RTC_IT >> 18)) >> 15)));
02509   
02510   /* Get the Interrupt pending bit */
02511   tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));
02512   
02513   /* Get the status of the Interrupt */
02514   if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))
02515   {
02516     bitstatus = SET;
02517   }
02518   else
02519   {
02520     bitstatus = RESET;
02521   }
02522   return bitstatus;
02523 }
02524 
02525 /**
02526   * @brief  Clears the RTC's interrupt pending bits.
02527   * @param  RTC_IT: specifies the RTC interrupt pending bit to clear.
02528   *   This parameter can be any combination of the following values:
02529   *     @arg RTC_IT_TS: Time Stamp interrupt 
02530   *     @arg RTC_IT_WUT: WakeUp Timer interrupt 
02531   *     @arg RTC_IT_ALRB: Alarm B interrupt 
02532   *     @arg RTC_IT_ALRA: Alarm A interrupt 
02533   *     @arg RTC_IT_TAMP1: Tamper1 event interrupt
02534   *     @arg RTC_IT_TAMP2: Tamper2 event interrupt
02535   *     @arg RTC_IT_TAMP3: Tamper3 event interrupt 
02536   * @retval None
02537   */
02538 void RTC_ClearITPendingBit(uint32_t RTC_IT)
02539 {
02540   uint32_t tmpreg = 0;
02541 
02542   /* Check the parameters */
02543   assert_param(IS_RTC_CLEAR_IT(RTC_IT));
02544 
02545   /* Get the RTC_ISR Interrupt pending bits mask */
02546   tmpreg = (uint32_t)(RTC_IT >> 4);
02547 
02548   /* Clear the interrupt pending bits in the RTC_ISR register */
02549   RTC->ISR = (uint32_t)((uint32_t)(~((tmpreg | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT))); 
02550 }
02551 
02552 /**
02553   * @}
02554   */
02555 
02556 /**
02557   * @brief  Converts a 2 digit decimal to BCD format.
02558   * @param  Value: Byte to be converted.
02559   * @retval Converted byte
02560   */
02561 static uint8_t RTC_ByteToBcd2(uint8_t Value)
02562 {
02563   uint8_t bcdhigh = 0;
02564   
02565   while (Value >= 10)
02566   {
02567     bcdhigh++;
02568     Value -= 10;
02569   }
02570   
02571   return  ((uint8_t)(bcdhigh << 4) | Value);
02572 }
02573 
02574 /**
02575   * @brief  Convert from 2 digit BCD to Binary.
02576   * @param  Value: BCD value to be converted.
02577   * @retval Converted word
02578   */
02579 static uint8_t RTC_Bcd2ToByte(uint8_t Value)
02580 {
02581   uint8_t tmp = 0;
02582   tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
02583   return (tmp + (Value & (uint8_t)0x0F));
02584 }
02585 
02586 /**
02587   * @}
02588   */ 
02589 
02590 /**
02591   * @}
02592   */ 
02593 
02594 /**
02595   * @}
02596   */ 
02597 
02598 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/