Added STM32F4xx StdPeriph Driver sources
[fw/stlink] / example / stm32f4 / STM32F4xx_StdPeriph_Driver / src / stm32f4xx_rtc.c
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_rtc.c
4   * @author  MCD Application Team
5   * @version V1.0.0RC1
6   * @date    25-August-2011
7   * @brief   This file provides firmware functions to manage the following 
8   *          functionalities of the Real-Time Clock (RTC) peripheral:
9   *           - Initialization
10   *           - Calendar (Time and Date) configuration
11   *           - Alarms (Alarm A and Alarm B) configuration
12   *           - WakeUp Timer configuration
13   *           - Daylight Saving configuration
14   *           - Output pin Configuration
15   *           - Coarse digital Calibration configuration
16   *           - Smooth digital Calibration configuration
17   *           - TimeStamp configuration
18   *           - Tampers configuration
19   *           - Backup Data Registers configuration
20   *           - Shift control synchronisation    
21   *           - RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration
22   *           - Interrupts and flags management
23   *
24   *  @verbatim
25   *
26   *          ===================================================================
27   *                               Backup Domain Operating Condition
28   *          ===================================================================
29   *          The real-time clock (RTC), the RTC backup registers, and the backup 
30   *          SRAM (BKP SRAM) can be powered from the VBAT voltage when the main 
31   *          VDD supply is powered off.
32   *          To retain the content of the RTC backup registers, backup SRAM, 
33   *          and supply the RTC when VDD is turned off, VBAT pin can be connected 
34   *          to an optional standby voltage supplied by a battery or by another 
35   *          source.
36   *
37   *          To allow the RTC to operate even when the main digital supply (VDD) 
38   *          is turned off, the VBAT pin powers the following blocks:
39   *            1 - The RTC
40   *            2 - The LSE oscillator
41   *            3 - The backup SRAM when the low power backup regulator is enabled
42   *            4 - PC13 to PC15 I/Os, plus PI8 I/O (when available)
43   *
44   *          When the backup domain is supplied by VDD (analog switch connected 
45   *          to VDD), the following functions are available:
46   *            1 - PC14 and PC15 can be used as either GPIO or LSE pins
47   *            2 - PC13 can be used as a GPIO or as the RTC_AF1 pin
48   *            3 - PI8 can be used as a GPIO or as the RTC_AF2 pin
49   *
50   *          When the backup domain is supplied by VBAT (analog switch connected 
51   *          to VBAT because VDD is not present), the following functions are available:
52   *            1 - PC14 and PC15 can be used as LSE pins only
53   *            2 - PC13 can be used as the RTC_AF1 pin 
54   *            3 - PI8 can be used as the RTC_AF2 pin
55   *
56   *          ===================================================================
57   *                                    Backup Domain Reset
58   *          ===================================================================
59   *          The backup domain reset sets all RTC registers and the RCC_BDCR 
60   *          register to their reset values. The BKPSRAM is not affected by this
61   *          reset. The only way of resetting the BKPSRAM is through the Flash 
62   *          interface by requesting a protection level change from 1 to 0.
63   *          A backup domain reset is generated when one of the following events
64   *          occurs:
65   *            1 - Software reset, triggered by setting the BDRST bit in the 
66   *                RCC Backup domain control register (RCC_BDCR). You can use the
67   *                RCC_BackupResetCmd().
68   *            2 - VDD or VBAT power on, if both supplies have previously been
69   *                powered off.
70   *
71   *          ===================================================================
72   *                                   Backup Domain Access
73   *          ===================================================================
74   *          After reset, the backup domain (RTC registers, RTC backup data 
75   *          registers and backup SRAM) is protected against possible unwanted 
76   *          write accesses. 
77   *          To enable access to the RTC Domain and RTC registers, proceed as follows:
78   *            - Enable the Power Controller (PWR) APB1 interface clock using the
79   *              RCC_APB1PeriphClockCmd() function.
80   *            - Enable access to RTC domain using the PWR_BackupAccessCmd() function.
81   *            - Select the RTC clock source using the RCC_RTCCLKConfig() function.
82   *            - Enable RTC Clock using the RCC_RTCCLKCmd() function.
83   *
84   *          ===================================================================
85   *                                   RTC Driver: how to use it
86   *          ===================================================================
87   *            - Enable the RTC domain access (see description in the section above)
88   *            - Configure the RTC Prescaler (Asynchronous and Synchronous) and
89   *              RTC hour format using the RTC_Init() function.
90   *
91   *          Time and Date configuration
92   *          ===========================
93   *            - To configure the RTC Calendar (Time and Date) use the RTC_SetTime()
94   *              and RTC_SetDate() functions.
95   *            - To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()
96   *              functions.
97   *            - Use the RTC_DayLightSavingConfig() function to add or sub one
98   *              hour to the RTC Calendar.    
99   *
100   *          Alarm configuration
101   *          ===================
102   *            - To configure the RTC Alarm use the RTC_SetAlarm() function.
103   *            - Enable the selected RTC Alarm using the RTC_AlarmCmd() function
104   *            - To read the RTC Alarm, use the RTC_GetAlarm() function.
105   *            - To read the RTC alarm SubSecond, use the RTC_GetAlarmSubSecond() function.
106   *
107   *          RTC Wakeup configuration
108   *          ========================
109   *            - Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
110   *              function.
111   *            - Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter() 
112   *              function  
113   *            - Enable the RTC WakeUp using the RTC_WakeUpCmd() function  
114   *            - To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter() 
115   *              function.
116   *
117   *          Outputs configuration
118   *          =====================
119   *          The RTC has 2 different outputs:
120   *            - AFO_ALARM: this output is used to manage the RTC Alarm A, Alarm B
121   *              and WaKeUp signals.          
122   *              To output the selected RTC signal on RTC_AF1 pin, use the 
123   *              RTC_OutputConfig() function.                
124   *            - AFO_CALIB: this output is 512Hz signal or 1Hz .  
125   *              To output the RTC Clock on RTC_AF1 pin, use the RTC_CalibOutputCmd()
126   *              function.
127   *
128   *          Smooth digital Calibration configuration
129   *          =================================    
130   *            - Configure the RTC Original Digital Calibration Value and the corresponding
131   *              calibration cycle period (32s,16s and 8s) using the RTC_SmoothCalibConfig() 
132   *              function.
133   *
134   *          Coarse digital Calibration configuration
135   *          =================================
136   *            - Configure the RTC Coarse Calibration Value and the corresponding
137   *              sign using the RTC_CoarseCalibConfig() function.
138   *            - Enable the RTC Coarse Calibration using the RTC_CoarseCalibCmd() 
139   *              function  
140   *
141   *          TimeStamp configuration
142   *          =======================
143   *            - Configure the RTC_AF1 trigger and enables the RTC TimeStamp 
144   *              using the RTC_TimeStampCmd() function.
145   *            - To read the RTC TimeStamp Time and Date register, use the 
146   *              RTC_GetTimeStamp() function.
147   *            - To read the RTC TimeStamp SubSecond register, use the 
148   *              RTC_GetTimeStampSubSecond() function.
149   *            - The TAMPER1 alternate function can be mapped either to RTC_AF1(PC13)
150   *              or RTC_AF2 (PI8) depending on the value of TAMP1INSEL bit in 
151   *              RTC_TAFCR register. You can use the  RTC_TamperPinSelection()
152   *              function to select the corresponding pin.     
153   *
154   *          Tamper configuration
155   *          ====================
156   *            - Enable the RTC Tamper using the RTC_TamperCmd() function.
157   *             - Configure the Tamper filter count using RTC_TamperFilterConfig()
158   *              function. 
159   *            - Configure the RTC Tamper trigger Edge or Level according to the Tamper 
160   *              filter (if equal to 0 Edge else Level) value using the RTC_TamperConfig() function.
161   *            - Configure the Tamper sampling frequency using RTC_TamperSamplingFreqConfig()
162   *              function.
163   *            - Configure the Tamper precharge or discharge duration using 
164   *              RTC_TamperPinsPrechargeDuration() function.
165   *            - Enable the Tamper Pull-UP using RTC_TamperPullUpDisableCmd() function.
166   *            - Enable the Time stamp on Tamper detection event using  
167   *              RTC_TSOnTamperDetecCmd() function.
168   *            - The TIMESTAMP alternate function can be mapped to either RTC_AF1 
169   *              or RTC_AF2 depending on the value of the TSINSEL bit in the 
170   *              RTC_TAFCR register. You can use the  RTC_TimeStampPinSelection()
171   *              function to select the corresponding pin. 
172   *
173   *          Backup Data Registers configuration
174   *          ===================================
175   *            - To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()
176   *              function.  
177   *            - To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()
178   *              function.
179   * 
180   *          ===================================================================
181   *                                  RTC and low power modes
182   *          ===================================================================
183   *           The MCU can be woken up from a low power mode by an RTC alternate 
184   *           function.
185   *           The RTC alternate functions are the RTC alarms (Alarm A and Alarm B), 
186   *           RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
187   *           These RTC alternate functions can wake up the system from the Stop 
188   *           and Standby lowpower modes.
189   *           The system can also wake up from low power modes without depending 
190   *           on an external interrupt (Auto-wakeup mode), by using the RTC alarm 
191   *           or the RTC wakeup events.
192   *           The RTC provides a programmable time base for waking up from the 
193   *           Stop or Standby mode at regular intervals.
194   *           Wakeup from STOP and Standby modes is possible only when the RTC 
195   *           clock source is LSE or LSI.
196   *
197   *          ===================================================================
198   *                            Selection of RTC_AF1 alternate functions
199   *          ===================================================================
200   *          The RTC_AF1 pin (PC13) can be used for the following purposes:
201   *            - AFO_ALARM output
202   *            - AFO_CALIB output
203   *            - AFI_TAMPER
204   *            - AFI_TIMESTAMP
205   *
206   * +-------------------------------------------------------------------------------------------------------------+
207   * |     Pin         |AFO_ALARM |AFO_CALIB |AFI_TAMPER |AFI_TIMESTAMP | TAMP1INSEL |   TSINSEL    |ALARMOUTTYPE  |
208   * |  configuration  | ENABLED  | ENABLED  |  ENABLED  |   ENABLED    |TAMPER1 pin |TIMESTAMP pin |  AFO_ALARM   |
209   * |  and function   |          |          |           |              | selection  |  selection   |Configuration |
210   * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
211   * |   Alarm out     |          |          |           |              |    Don't   |     Don't    |              |
212   * |   output OD     |     1    |Don't care|Don't care | Don't care   |    care    |     care     |      0       |
213   * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
214   * |   Alarm out     |          |          |           |              |    Don't   |     Don't    |              |
215   * |   output PP     |     1    |Don't care|Don't care | Don't care   |    care    |     care     |      1       |
216   * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
217   * | Calibration out |          |          |           |              |    Don't   |     Don't    |              |
218   * |   output PP     |     0    |    1     |Don't care | Don't care   |    care    |     care     |  Don't care  |
219   * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
220   * |  TAMPER input   |          |          |           |              |            |     Don't    |              |
221   * |   floating      |     0    |    0     |     1     |      0       |      0     |     care     |  Don't care  |
222   * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
223   * |  TIMESTAMP and  |          |          |           |              |            |              |              |
224   * |  TAMPER input   |     0    |    0     |     1     |      1       |      0     |      0       |  Don't care  |
225   * |   floating      |          |          |           |              |            |              |              |
226   * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
227   * | TIMESTAMP input |          |          |           |              |    Don't   |              |              |
228   * |    floating     |     0    |    0     |     0     |      1       |    care    |      0       |  Don't care  |
229   * |-----------------|----------|----------|-----------|--------------|------------|--------------|--------------|
230   * |  Standard GPIO  |     0    |    0     |     0     |      0       | Don't care |  Don't care  |  Don't care  |
231   * +-------------------------------------------------------------------------------------------------------------+
232   *
233   *
234   *          ===================================================================
235   *                            Selection of RTC_AF2 alternate functions
236   *          ===================================================================
237   *          The RTC_AF2 pin (PI8) can be used for the following purposes:
238   *            - AFI_TAMPER
239   *            - AFI_TIMESTAMP
240   *
241   * +---------------------------------------------------------------------------------------+
242   * |     Pin         |AFI_TAMPER |AFI_TIMESTAMP | TAMP1INSEL |   TSINSEL    |ALARMOUTTYPE  |
243   * |  configuration  |  ENABLED  |   ENABLED    |TAMPER1 pin |TIMESTAMP pin |  AFO_ALARM   |
244   * |  and function   |           |              | selection  |  selection   |Configuration |
245   * |-----------------|-----------|--------------|------------|--------------|--------------|
246   * |  TAMPER input   |           |              |            |     Don't    |              |
247   * |   floating      |     1     |      0       |      1     |     care     |  Don't care  |
248   * |-----------------|-----------|--------------|------------|--------------|--------------|
249   * |  TIMESTAMP and  |           |              |            |              |              |
250   * |  TAMPER input   |     1     |      1       |      1     |      1       |  Don't care  |
251   * |   floating      |           |              |            |              |              |
252   * |-----------------|-----------|--------------|------------|--------------|--------------|
253   * | TIMESTAMP input |           |              |    Don't   |              |              |
254   * |    floating     |     0     |      1       |    care    |      1       |  Don't care  |
255   * |-----------------|-----------|--------------|------------|--------------|--------------|
256   * |  Standard GPIO  |     0     |      0       | Don't care |  Don't care  |  Don't care  |
257   * +---------------------------------------------------------------------------------------+
258   * 
259   *
260   *  @endverbatim
261   *
262   ******************************************************************************
263   * @attention
264   *
265   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
266   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
267   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
268   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
269   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
270   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
271   *
272   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
273   ******************************************************************************
274   */ 
275
276 /* Includes ------------------------------------------------------------------*/
277 #include "stm32f4xx_rtc.h"
278 #include "stm32f4xx_rcc.h"
279
280 /** @addtogroup STM32F4xx_StdPeriph_Driver
281   * @{
282   */
283
284 /** @defgroup RTC 
285   * @brief RTC driver modules
286   * @{
287   */
288
289 /* Private typedef -----------------------------------------------------------*/
290 /* Private define ------------------------------------------------------------*/
291
292 /* Masks Definition */
293 #define RTC_TR_RESERVED_MASK    ((uint32_t)0x007F7F7F)
294 #define RTC_DR_RESERVED_MASK    ((uint32_t)0x00FFFF3F) 
295 #define RTC_INIT_MASK           ((uint32_t)0xFFFFFFFF)  
296 #define RTC_RSF_MASK            ((uint32_t)0xFFFFFF5F)
297 #define RTC_FLAGS_MASK          ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_WUTF | \
298                                             RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_INITF | \
299                                             RTC_FLAG_RSF | RTC_FLAG_INITS | RTC_FLAG_WUTWF | \
300                                             RTC_FLAG_ALRBWF | RTC_FLAG_ALRAWF | RTC_FLAG_TAMP1F ))
301
302 #define INITMODE_TIMEOUT         ((uint32_t) 0x00010000)
303 #define SYNCHRO_TIMEOUT          ((uint32_t) 0x00020000)
304 #define RECALPF_TIMEOUT          ((uint32_t) 0x00020000)
305 #define SHPF_TIMEOUT             ((uint32_t) 0x00001000)
306
307 /* Private macro -------------------------------------------------------------*/
308 /* Private variables ---------------------------------------------------------*/
309 /* Private function prototypes -----------------------------------------------*/
310 static uint8_t RTC_ByteToBcd2(uint8_t Value);
311 static uint8_t RTC_Bcd2ToByte(uint8_t Value);
312
313 /* Private functions ---------------------------------------------------------*/
314
315 /** @defgroup RTC_Private_Functions
316   * @{
317   */ 
318
319 /** @defgroup RTC_Group1 Initialization and Configuration functions
320  *  @brief   Initialization and Configuration functions 
321  *
322 @verbatim   
323  ===============================================================================
324                  Initialization and Configuration functions
325  ===============================================================================
326
327   This section provide functions allowing to initialize and configure the RTC
328   Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers
329   Write protection, enter and exit the RTC initialization mode, RTC registers
330   synchronization check and reference clock detection enable.
331   
332   1. The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is
333      split into 2 programmable prescalers to minimize power consumption.
334      - A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
335      - When both prescalers are used, it is recommended to configure the asynchronous
336        prescaler to a high value to minimize consumption.
337
338   2. All RTC registers are Write protected. Writing to the RTC registers
339      is enabled by writing a key into the Write Protection register, RTC_WPR.
340
341   3. To Configure the RTC Calendar, user application should enter initialization
342      mode. In this mode, the calendar counter is stopped and its value can be 
343      updated. When the initialization sequence is complete, the calendar restarts 
344      counting after 4 RTCCLK cycles.
345
346   4. To read the calendar through the shadow registers after Calendar initialization,
347      calendar update or after wakeup from low power modes the software must first 
348      clear the RSF flag. The software must then wait until it is set again before 
349      reading the calendar, which means that the calendar registers have been 
350      correctly copied into the RTC_TR and RTC_DR shadow registers.
351      The RTC_WaitForSynchro() function implements the above software sequence 
352      (RSF clear and RSF check).
353
354 @endverbatim
355   * @{
356   */
357
358 /**
359   * @brief  Deinitializes the RTC registers to their default reset values.
360   * @note   This function doesn't reset the RTC Clock source and RTC Backup Data
361   *         registers.       
362   * @param  None
363   * @retval An ErrorStatus enumeration value:
364   *          - SUCCESS: RTC registers are deinitialized
365   *          - ERROR: RTC registers are not deinitialized
366   */
367 ErrorStatus RTC_DeInit(void)
368 {
369   __IO uint32_t wutcounter = 0x00;
370   uint32_t wutwfstatus = 0x00;
371   ErrorStatus status = ERROR;
372   
373   /* Disable the write protection for RTC registers */
374   RTC->WPR = 0xCA;
375   RTC->WPR = 0x53;
376
377   /* Set Initialization mode */
378   if (RTC_EnterInitMode() == ERROR)
379   {
380     status = ERROR;
381   }  
382   else
383   {
384     /* Reset TR, DR and CR registers */
385     RTC->TR = (uint32_t)0x00000000;
386     RTC->DR = (uint32_t)0x00002101;
387     /* Reset All CR bits except CR[2:0] */
388     RTC->CR &= (uint32_t)0x00000007;
389   
390     /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
391     do
392     {
393       wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
394       wutcounter++;  
395     } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
396     
397     if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
398     {
399       status = ERROR;
400     }
401     else
402     {
403       /* Reset all RTC CR register bits */
404       RTC->CR &= (uint32_t)0x00000000;
405       RTC->WUTR = (uint32_t)0x0000FFFF;
406       RTC->PRER = (uint32_t)0x007F00FF;
407       RTC->CALIBR = (uint32_t)0x00000000;
408       RTC->ALRMAR = (uint32_t)0x00000000;        
409       RTC->ALRMBR = (uint32_t)0x00000000;
410       
411       /* Reset ISR register and exit initialization mode */
412       RTC->ISR = (uint32_t)0x00000000;
413       
414       /* Reset Tamper and alternate functions configuration register */
415       RTC->TAFCR = 0x00000000;
416   
417       if(RTC_WaitForSynchro() == ERROR)
418       {
419         status = ERROR;
420       }
421       else
422       {
423         status = SUCCESS;      
424       }
425     }
426   }
427   
428   /* Enable the write protection for RTC registers */
429   RTC->WPR = 0xFF;  
430   
431   return status;
432 }
433
434 /**
435   * @brief  Initializes the RTC registers according to the specified parameters 
436   *         in RTC_InitStruct.
437   * @param  RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains 
438   *         the configuration information for the RTC peripheral.
439   * @note   The RTC Prescaler register is write protected and can be written in 
440   *         initialization mode only.  
441   * @retval An ErrorStatus enumeration value:
442   *          - SUCCESS: RTC registers are initialized
443   *          - ERROR: RTC registers are not initialized  
444   */
445 ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
446 {
447   ErrorStatus status = ERROR;
448   
449   /* Check the parameters */
450   assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
451   assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
452   assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
453
454   /* Disable the write protection for RTC registers */
455   RTC->WPR = 0xCA;
456   RTC->WPR = 0x53;
457
458   /* Set Initialization mode */
459   if (RTC_EnterInitMode() == ERROR)
460   {
461     status = ERROR;
462   } 
463   else
464   {
465     /* Clear RTC CR FMT Bit */
466     RTC->CR &= ((uint32_t)~(RTC_CR_FMT));
467     /* Set RTC_CR register */
468     RTC->CR |=  ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
469   
470     /* Configure the RTC PRER */
471     RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
472     RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
473
474     /* Exit Initialization mode */
475     RTC_ExitInitMode();
476
477     status = SUCCESS;    
478   }
479   /* Enable the write protection for RTC registers */
480   RTC->WPR = 0xFF; 
481   
482   return status;
483 }
484
485 /**
486   * @brief  Fills each RTC_InitStruct member with its default value.
487   * @param  RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be 
488   *         initialized.
489   * @retval None
490   */
491 void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
492 {
493   /* Initialize the RTC_HourFormat member */
494   RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
495     
496   /* Initialize the RTC_AsynchPrediv member */
497   RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
498
499   /* Initialize the RTC_SynchPrediv member */
500   RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF; 
501 }
502
503 /**
504   * @brief  Enables or disables the RTC registers write protection.
505   * @note   All the RTC registers are write protected except for RTC_ISR[13:8], 
506   *         RTC_TAFCR and RTC_BKPxR.
507   * @note   Writing a wrong key reactivates the write protection.
508   * @note   The protection mechanism is not affected by system reset.  
509   * @param  NewState: new state of the write protection.
510   *          This parameter can be: ENABLE or DISABLE.
511   * @retval None
512   */
513 void RTC_WriteProtectionCmd(FunctionalState NewState)
514 {
515   /* Check the parameters */
516   assert_param(IS_FUNCTIONAL_STATE(NewState));
517     
518   if (NewState != DISABLE)
519   {
520     /* Enable the write protection for RTC registers */
521     RTC->WPR = 0xFF;   
522   }
523   else
524   {
525     /* Disable the write protection for RTC registers */
526     RTC->WPR = 0xCA;
527     RTC->WPR = 0x53;    
528   }
529 }
530
531 /**
532   * @brief  Enters the RTC Initialization mode.
533   * @note   The RTC Initialization mode is write protected, use the 
534   *         RTC_WriteProtectionCmd(DISABLE) before calling this function.    
535   * @param  None
536   * @retval An ErrorStatus enumeration value:
537   *          - SUCCESS: RTC is in Init mode
538   *          - ERROR: RTC is not in Init mode  
539   */
540 ErrorStatus RTC_EnterInitMode(void)
541 {
542   __IO uint32_t initcounter = 0x00;
543   ErrorStatus status = ERROR;
544   uint32_t initstatus = 0x00;
545      
546   /* Check if the Initialization mode is set */
547   if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
548   {
549     /* Set the Initialization mode */
550     RTC->ISR = (uint32_t)RTC_INIT_MASK;
551     
552     /* Wait till RTC is in INIT state and if Time out is reached exit */
553     do
554     {
555       initstatus = RTC->ISR & RTC_ISR_INITF;
556       initcounter++;  
557     } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
558     
559     if ((RTC->ISR & RTC_ISR_INITF) != RESET)
560     {
561       status = SUCCESS;
562     }
563     else
564     {
565       status = ERROR;
566     }        
567   }
568   else
569   {
570     status = SUCCESS;  
571   } 
572     
573   return (status);  
574 }
575
576 /**
577   * @brief  Exits the RTC Initialization mode.
578   * @note   When the initialization sequence is complete, the calendar restarts 
579   *         counting after 4 RTCCLK cycles.  
580   * @note   The RTC Initialization mode is write protected, use the 
581   *         RTC_WriteProtectionCmd(DISABLE) before calling this function.      
582   * @param  None
583   * @retval None
584   */
585 void RTC_ExitInitMode(void)
586
587   /* Exit Initialization mode */
588   RTC->ISR &= (uint32_t)~RTC_ISR_INIT;  
589 }
590
591 /**
592   * @brief  Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are 
593   *         synchronized with RTC APB clock.
594   * @note   The RTC Resynchronization mode is write protected, use the 
595   *         RTC_WriteProtectionCmd(DISABLE) before calling this function. 
596   * @note   To read the calendar through the shadow registers after Calendar 
597   *         initialization, calendar update or after wakeup from low power modes 
598   *         the software must first clear the RSF flag. 
599   *         The software must then wait until it is set again before reading 
600   *         the calendar, which means that the calendar registers have been 
601   *         correctly copied into the RTC_TR and RTC_DR shadow registers.   
602   * @param  None
603   * @retval An ErrorStatus enumeration value:
604   *          - SUCCESS: RTC registers are synchronised
605   *          - ERROR: RTC registers are not synchronised
606   */
607 ErrorStatus RTC_WaitForSynchro(void)
608 {
609   __IO uint32_t synchrocounter = 0;
610   ErrorStatus status = ERROR;
611   uint32_t synchrostatus = 0x00;
612
613   /* Disable the write protection for RTC registers */
614   RTC->WPR = 0xCA;
615   RTC->WPR = 0x53;
616     
617   /* Clear RSF flag */
618   RTC->ISR &= (uint32_t)RTC_RSF_MASK;
619     
620   /* Wait the registers to be synchronised */
621   do
622   {
623     synchrostatus = RTC->ISR & RTC_ISR_RSF;
624     synchrocounter++;  
625   } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
626     
627   if ((RTC->ISR & RTC_ISR_RSF) != RESET)
628   {
629     status = SUCCESS;
630   }
631   else
632   {
633     status = ERROR;
634   }        
635
636   /* Enable the write protection for RTC registers */
637   RTC->WPR = 0xFF; 
638     
639   return (status); 
640 }
641
642 /**
643   * @brief  Enables or disables the RTC reference clock detection.
644   * @param  NewState: new state of the RTC reference clock.
645   *          This parameter can be: ENABLE or DISABLE.
646   * @retval An ErrorStatus enumeration value:
647   *          - SUCCESS: RTC reference clock detection is enabled
648   *          - ERROR: RTC reference clock detection is disabled  
649   */
650 ErrorStatus RTC_RefClockCmd(FunctionalState NewState)
651
652   ErrorStatus status = ERROR;
653   
654   /* Check the parameters */
655   assert_param(IS_FUNCTIONAL_STATE(NewState));
656   
657   /* Disable the write protection for RTC registers */
658   RTC->WPR = 0xCA;
659   RTC->WPR = 0x53;
660     
661   /* Set Initialization mode */
662   if (RTC_EnterInitMode() == ERROR)
663   {
664     status = ERROR;
665   } 
666   else
667   {  
668     if (NewState != DISABLE)
669     {
670       /* Enable the RTC reference clock detection */
671       RTC->CR |= RTC_CR_REFCKON;   
672     }
673     else
674     {
675       /* Disable the RTC reference clock detection */
676       RTC->CR &= ~RTC_CR_REFCKON;    
677     }
678     /* Exit Initialization mode */
679     RTC_ExitInitMode();
680     
681     status = SUCCESS;
682   }
683   
684   /* Enable the write protection for RTC registers */
685   RTC->WPR = 0xFF;  
686   
687   return status; 
688 }
689
690 /**
691   * @brief  Enables or Disables the Bypass Shadow feature.
692   * @note   When the Bypass Shadow is enabled the calendar value are taken 
693   *         directly from the Calendar counter.
694   * @param  NewState: new state of the Bypass Shadow feature.
695   *         This parameter can be: ENABLE or DISABLE.
696   * @retval None
697 */
698 void RTC_BypassShadowCmd(FunctionalState NewState)
699 {
700   /* Check the parameters */
701   assert_param(IS_FUNCTIONAL_STATE(NewState));
702
703   /* Disable the write protection for RTC registers */
704   RTC->WPR = 0xCA;
705   RTC->WPR = 0x53;
706   
707   if (NewState != DISABLE)
708   {
709     /* Set the BYPSHAD bit */
710     RTC->CR |= (uint8_t)RTC_CR_BYPSHAD;
711   }
712   else
713   {
714     /* Reset the BYPSHAD bit */
715     RTC->CR &= (uint8_t)~RTC_CR_BYPSHAD;
716   }
717
718   /* Enable the write protection for RTC registers */
719   RTC->WPR = 0xFF;
720 }
721
722 /**
723   * @}
724   */
725
726 /** @defgroup RTC_Group2 Time and Date configuration functions
727  *  @brief   Time and Date configuration functions 
728  *
729 @verbatim   
730  ===============================================================================
731                    Time and Date configuration functions
732  ===============================================================================  
733
734   This section provide functions allowing to program and read the RTC Calendar
735   (Time and Date).
736
737 @endverbatim
738   * @{
739   */
740
741 /**
742   * @brief  Set the RTC current time.
743   * @param  RTC_Format: specifies the format of the entered parameters.
744   *          This parameter can be  one of the following values:
745   *            @arg RTC_Format_BIN:  Binary data format 
746   *            @arg RTC_Format_BCD:  BCD data format
747   * @param  RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that contains 
748   *                        the time configuration information for the RTC.     
749   * @retval An ErrorStatus enumeration value:
750   *          - SUCCESS: RTC Time register is configured
751   *          - ERROR: RTC Time register is not configured
752   */
753 ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
754 {
755   uint32_t tmpreg = 0;
756   ErrorStatus status = ERROR;
757     
758   /* Check the parameters */
759   assert_param(IS_RTC_FORMAT(RTC_Format));
760   
761   if (RTC_Format == RTC_Format_BIN)
762   {
763     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
764     {
765       assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
766       assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
767     } 
768     else
769     {
770       RTC_TimeStruct->RTC_H12 = 0x00;
771       assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
772     }
773     assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
774     assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
775   }
776   else
777   {
778     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
779     {
780       tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
781       assert_param(IS_RTC_HOUR12(tmpreg));
782       assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12)); 
783     } 
784     else
785     {
786       RTC_TimeStruct->RTC_H12 = 0x00;
787       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
788     }
789     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
790     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
791   }
792   
793   /* Check the input parameters format */
794   if (RTC_Format != RTC_Format_BIN)
795   {
796     tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \
797              ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \
798              ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \
799              ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16)); 
800   }  
801   else
802   {
803     tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \
804                    ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \
805                    ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \
806                    (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));
807   }  
808
809   /* Disable the write protection for RTC registers */
810   RTC->WPR = 0xCA;
811   RTC->WPR = 0x53;
812
813   /* Set Initialization mode */
814   if (RTC_EnterInitMode() == ERROR)
815   {
816     status = ERROR;
817   } 
818   else
819   {
820     /* Set the RTC_TR register */
821     RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
822
823     /* Exit Initialization mode */
824     RTC_ExitInitMode(); 
825
826     if(RTC_WaitForSynchro() == ERROR)
827     {
828       status = ERROR;
829     }
830     else
831     {
832       status = SUCCESS;
833     }
834   
835   }
836   /* Enable the write protection for RTC registers */
837   RTC->WPR = 0xFF; 
838     
839   return status;
840 }
841
842 /**
843   * @brief  Fills each RTC_TimeStruct member with its default value
844   *         (Time = 00h:00min:00sec).
845   * @param  RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be 
846   *         initialized.
847   * @retval None
848   */
849 void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
850 {
851   /* Time = 00h:00min:00sec */
852   RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
853   RTC_TimeStruct->RTC_Hours = 0;
854   RTC_TimeStruct->RTC_Minutes = 0;
855   RTC_TimeStruct->RTC_Seconds = 0; 
856 }
857
858 /**
859   * @brief  Get the RTC current Time.
860   * @param  RTC_Format: specifies the format of the returned parameters.
861   *          This parameter can be  one of the following values:
862   *            @arg RTC_Format_BIN:  Binary data format 
863   *            @arg RTC_Format_BCD:  BCD data format
864   * @param  RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will 
865   *                        contain the returned current time configuration.     
866   * @retval None
867   */
868 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
869 {
870   uint32_t tmpreg = 0;
871
872   /* Check the parameters */
873   assert_param(IS_RTC_FORMAT(RTC_Format));
874
875   /* Get the RTC_TR register */
876   tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK); 
877   
878   /* Fill the structure fields with the read parameters */
879   RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
880   RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
881   RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
882   RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);  
883
884   /* Check the input parameters format */
885   if (RTC_Format == RTC_Format_BIN)
886   {
887     /* Convert the structure parameters to Binary format */
888     RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
889     RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
890     RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);   
891   }
892 }
893
894 /**
895   * @brief  Gets the RTC current Calendar Subseconds value.
896   * @note   This function freeze the Time and Date registers after reading the 
897   *         SSR register.
898   * @param  None
899   * @retval RTC current Calendar Subseconds value.
900   */
901 uint32_t RTC_GetSubSecond(void)
902 {
903   uint32_t tmpreg = 0;
904   
905   /* Get subseconds values from the correspondent registers*/
906   tmpreg = (uint32_t)(RTC->SSR);
907   
908   /* Read DR register to unfroze calendar registers */
909   (void) (RTC->DR);
910   
911   return (tmpreg);
912 }
913
914 /**
915   * @brief  Set the RTC current date.
916   * @param  RTC_Format: specifies the format of the entered parameters.
917   *          This parameter can be  one of the following values:
918   *            @arg RTC_Format_BIN:  Binary data format 
919   *            @arg RTC_Format_BCD:  BCD data format
920   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains 
921   *                         the date configuration information for the RTC.
922   * @retval An ErrorStatus enumeration value:
923   *          - SUCCESS: RTC Date register is configured
924   *          - ERROR: RTC Date register is not configured
925   */
926 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
927 {
928   uint32_t tmpreg = 0;
929   ErrorStatus status = ERROR;
930   
931   /* Check the parameters */
932   assert_param(IS_RTC_FORMAT(RTC_Format));
933
934   if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
935   {
936     RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
937   }  
938   if (RTC_Format == RTC_Format_BIN)
939   {
940     assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
941     assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
942     assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
943   }
944   else
945   {
946     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
947     tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
948     assert_param(IS_RTC_MONTH(tmpreg));
949     tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
950     assert_param(IS_RTC_DATE(tmpreg));
951   }
952   assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
953
954   /* Check the input parameters format */
955   if (RTC_Format != RTC_Format_BIN)
956   {
957     tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
958               (((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
959               ((uint32_t)RTC_DateStruct->RTC_Date) | \
960               (((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13)); 
961   }  
962   else
963   {
964     tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
965               ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
966               ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
967               ((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
968   }
969
970   /* Disable the write protection for RTC registers */
971   RTC->WPR = 0xCA;
972   RTC->WPR = 0x53;
973
974   /* Set Initialization mode */
975   if (RTC_EnterInitMode() == ERROR)
976   {
977     status = ERROR;
978   } 
979   else
980   {
981     /* Set the RTC_DR register */
982     RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
983
984     /* Exit Initialization mode */
985     RTC_ExitInitMode(); 
986
987     if(RTC_WaitForSynchro() == ERROR)
988     {
989       status = ERROR;
990     }
991     else
992     {
993       status = SUCCESS;
994     }
995   }
996   /* Enable the write protection for RTC registers */
997   RTC->WPR = 0xFF;   
998   
999   return status;
1000 }
1001
1002 /**
1003   * @brief  Fills each RTC_DateStruct member with its default value
1004   *         (Monday, January 01 xx00).
1005   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure which will be 
1006   *         initialized.
1007   * @retval None
1008   */
1009 void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
1010 {
1011   /* Monday, January 01 xx00 */
1012   RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;
1013   RTC_DateStruct->RTC_Date = 1;
1014   RTC_DateStruct->RTC_Month = RTC_Month_January;
1015   RTC_DateStruct->RTC_Year = 0;
1016 }
1017
1018 /**
1019   * @brief  Get the RTC current date. 
1020   * @param  RTC_Format: specifies the format of the returned parameters.
1021   *          This parameter can be one of the following values:
1022   *            @arg RTC_Format_BIN: Binary data format 
1023   *            @arg RTC_Format_BCD: BCD data format
1024   * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that will 
1025   *                        contain the returned current date configuration.     
1026   * @retval None
1027   */
1028 void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
1029 {
1030   uint32_t tmpreg = 0;
1031
1032   /* Check the parameters */
1033   assert_param(IS_RTC_FORMAT(RTC_Format));
1034   
1035   /* Get the RTC_TR register */
1036   tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK); 
1037
1038   /* Fill the structure fields with the read parameters */
1039   RTC_DateStruct->RTC_Year = (uint8_t)((tmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
1040   RTC_DateStruct->RTC_Month = (uint8_t)((tmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1041   RTC_DateStruct->RTC_Date = (uint8_t)(tmpreg & (RTC_DR_DT | RTC_DR_DU));
1042   RTC_DateStruct->RTC_WeekDay = (uint8_t)((tmpreg & (RTC_DR_WDU)) >> 13);  
1043
1044   /* Check the input parameters format */
1045   if (RTC_Format == RTC_Format_BIN)
1046   {
1047     /* Convert the structure parameters to Binary format */
1048     RTC_DateStruct->RTC_Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year);
1049     RTC_DateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
1050     RTC_DateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
1051     RTC_DateStruct->RTC_WeekDay = (uint8_t)(RTC_DateStruct->RTC_WeekDay);   
1052   }
1053 }
1054
1055 /**
1056   * @}
1057   */
1058
1059 /** @defgroup RTC_Group3 Alarms configuration functions
1060  *  @brief   Alarms (Alarm A and Alarm B) configuration functions 
1061  *
1062 @verbatim   
1063  ===============================================================================
1064               Alarms (Alarm A and Alarm B) configuration functions
1065  ===============================================================================  
1066
1067   This section provide functions allowing to program and read the RTC Alarms.
1068
1069 @endverbatim
1070   * @{
1071   */
1072
1073 /**
1074   * @brief  Set the specified RTC Alarm.
1075   * @note   The Alarm register can only be written when the corresponding Alarm
1076   *         is disabled (Use the RTC_AlarmCmd(DISABLE)).    
1077   * @param  RTC_Format: specifies the format of the returned parameters.
1078   *          This parameter can be one of the following values:
1079   *            @arg RTC_Format_BIN: Binary data format 
1080   *            @arg RTC_Format_BCD: BCD data format
1081   * @param  RTC_Alarm: specifies the alarm to be configured.
1082   *          This parameter can be one of the following values:
1083   *            @arg RTC_Alarm_A: to select Alarm A
1084   *            @arg RTC_Alarm_B: to select Alarm B  
1085   * @param  RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that 
1086   *                          contains the alarm configuration parameters.     
1087   * @retval None
1088   */
1089 void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1090 {
1091   uint32_t tmpreg = 0;
1092   
1093   /* Check the parameters */
1094   assert_param(IS_RTC_FORMAT(RTC_Format));
1095   assert_param(IS_RTC_ALARM(RTC_Alarm));
1096   assert_param(IS_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
1097   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
1098
1099   if (RTC_Format == RTC_Format_BIN)
1100   {
1101     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
1102     {
1103       assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1104       assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
1105     } 
1106     else
1107     {
1108       RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1109       assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1110     }
1111     assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
1112     assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
1113     
1114     if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1115     {
1116       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1117     }
1118     else
1119     {
1120       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1121     }
1122   }
1123   else
1124   {
1125     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
1126     {
1127       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
1128       assert_param(IS_RTC_HOUR12(tmpreg));
1129       assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
1130     } 
1131     else
1132     {
1133       RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1134       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
1135     }
1136     
1137     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
1138     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
1139     
1140     if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1141     {
1142       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1143       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));    
1144     }
1145     else
1146     {
1147       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1148       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));      
1149     }    
1150   }
1151
1152   /* Check the input parameters format */
1153   if (RTC_Format != RTC_Format_BIN)
1154   {
1155     tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
1156               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
1157               ((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
1158               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
1159               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
1160               ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
1161               ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask)); 
1162   }  
1163   else
1164   {
1165     tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
1166               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
1167               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
1168               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
1169               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
1170               ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
1171               ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask)); 
1172   } 
1173
1174   /* Disable the write protection for RTC registers */
1175   RTC->WPR = 0xCA;
1176   RTC->WPR = 0x53;
1177
1178   /* Configure the Alarm register */
1179   if (RTC_Alarm == RTC_Alarm_A)
1180   {
1181     RTC->ALRMAR = (uint32_t)tmpreg;
1182   }
1183   else
1184   {
1185     RTC->ALRMBR = (uint32_t)tmpreg;
1186   }
1187
1188   /* Enable the write protection for RTC registers */
1189   RTC->WPR = 0xFF;   
1190 }
1191
1192 /**
1193   * @brief  Fills each RTC_AlarmStruct member with its default value
1194   *         (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
1195   *         all fields are masked).
1196   * @param  RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
1197   *         will be initialized.
1198   * @retval None
1199   */
1200 void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
1201 {
1202   /* Alarm Time Settings : Time = 00h:00mn:00sec */
1203   RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
1204   RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
1205   RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
1206   RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
1207
1208   /* Alarm Date Settings : Date = 1st day of the month */
1209   RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
1210   RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
1211
1212   /* Alarm Masks Settings : Mask =  all fields are not masked */
1213   RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
1214 }
1215
1216 /**
1217   * @brief  Get the RTC Alarm value and masks.
1218   * @param  RTC_Format: specifies the format of the output parameters.
1219   *          This parameter can be one of the following values:
1220   *            @arg RTC_Format_BIN: Binary data format 
1221   *            @arg RTC_Format_BCD: BCD data format
1222   * @param  RTC_Alarm: specifies the alarm to be read.
1223   *          This parameter can be one of the following values:
1224   *            @arg RTC_Alarm_A: to select Alarm A
1225   *            @arg RTC_Alarm_B: to select Alarm B  
1226   * @param  RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will 
1227   *                          contains the output alarm configuration values.     
1228   * @retval None
1229   */
1230 void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1231 {
1232   uint32_t tmpreg = 0;
1233
1234   /* Check the parameters */
1235   assert_param(IS_RTC_FORMAT(RTC_Format));
1236   assert_param(IS_RTC_ALARM(RTC_Alarm)); 
1237
1238   /* Get the RTC_ALRMxR register */
1239   if (RTC_Alarm == RTC_Alarm_A)
1240   {
1241     tmpreg = (uint32_t)(RTC->ALRMAR);
1242   }
1243   else
1244   {
1245     tmpreg = (uint32_t)(RTC->ALRMBR);
1246   }
1247
1248   /* Fill the structure with the read parameters */
1249   RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \
1250                                                      RTC_ALRMAR_HU)) >> 16);
1251   RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \
1252                                                      RTC_ALRMAR_MNU)) >> 8);
1253   RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \
1254                                                      RTC_ALRMAR_SU));
1255   RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1256   RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1257   RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1258   RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);
1259
1260   if (RTC_Format == RTC_Format_BIN)
1261   {
1262     RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1263                                                         RTC_AlarmTime.RTC_Hours);
1264     RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1265                                                         RTC_AlarmTime.RTC_Minutes);
1266     RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1267                                                         RTC_AlarmTime.RTC_Seconds);
1268     RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1269   }  
1270 }
1271
1272 /**
1273   * @brief  Enables or disables the specified RTC Alarm.
1274   * @param  RTC_Alarm: specifies the alarm to be configured.
1275   *          This parameter can be any combination of the following values:
1276   *            @arg RTC_Alarm_A: to select Alarm A
1277   *            @arg RTC_Alarm_B: to select Alarm B  
1278   * @param  NewState: new state of the specified alarm.
1279   *          This parameter can be: ENABLE or DISABLE.
1280   * @retval An ErrorStatus enumeration value:
1281   *          - SUCCESS: RTC Alarm is enabled/disabled
1282   *          - ERROR: RTC Alarm is not enabled/disabled  
1283   */
1284 ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
1285 {
1286   __IO uint32_t alarmcounter = 0x00;
1287   uint32_t alarmstatus = 0x00;
1288   ErrorStatus status = ERROR;
1289     
1290   /* Check the parameters */
1291   assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
1292   assert_param(IS_FUNCTIONAL_STATE(NewState));
1293
1294   /* Disable the write protection for RTC registers */
1295   RTC->WPR = 0xCA;
1296   RTC->WPR = 0x53;
1297
1298   /* Configure the Alarm state */
1299   if (NewState != DISABLE)
1300   {
1301     RTC->CR |= (uint32_t)RTC_Alarm;
1302
1303     status = SUCCESS;    
1304   }
1305   else
1306   { 
1307     /* Disable the Alarm in RTC_CR register */
1308     RTC->CR &= (uint32_t)~RTC_Alarm;
1309    
1310     /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1311     do
1312     {
1313       alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
1314       alarmcounter++;  
1315     } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
1316     
1317     if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
1318     {
1319       status = ERROR;
1320     } 
1321     else
1322     {
1323       status = SUCCESS;
1324     }        
1325   } 
1326
1327   /* Enable the write protection for RTC registers */
1328   RTC->WPR = 0xFF; 
1329   
1330   return status;
1331 }
1332
1333 /**
1334   * @brief  Configure the RTC AlarmA/B Subseconds value and mask.*
1335   * @note   This function is performed only when the Alarm is disabled. 
1336   * @param  RTC_Alarm: specifies the alarm to be configured.
1337   *   This parameter can be one of the following values:
1338   *     @arg RTC_Alarm_A: to select Alarm A
1339   *     @arg RTC_Alarm_B: to select Alarm B
1340   * @param  RTC_AlarmSubSecondValue: specifies the Subseconds value.
1341   *   This parameter can be a value from 0 to 0x00007FFF.
1342   * @param  RTC_AlarmSubSecondMask:  specifies the Subseconds Mask.
1343   *   This parameter can be any combination of the following values:
1344   *     @arg RTC_AlarmSubSecondMask_All    : All Alarm SS fields are masked.
1345   *                                          There is no comparison on sub seconds for Alarm.
1346   *     @arg RTC_AlarmSubSecondMask_SS14_1 : SS[14:1] are don't care in Alarm comparison.
1347   *                                          Only SS[0] is compared
1348   *     @arg RTC_AlarmSubSecondMask_SS14_2 : SS[14:2] are don't care in Alarm comparison.
1349   *                                          Only SS[1:0] are compared
1350   *     @arg RTC_AlarmSubSecondMask_SS14_3 : SS[14:3] are don't care in Alarm comparison.
1351   *                                          Only SS[2:0] are compared
1352   *     @arg RTC_AlarmSubSecondMask_SS14_4 : SS[14:4] are don't care in Alarm comparison.
1353   *                                          Only SS[3:0] are compared
1354   *     @arg RTC_AlarmSubSecondMask_SS14_5 : SS[14:5] are don't care in Alarm comparison.
1355   *                                          Only SS[4:0] are compared
1356   *     @arg RTC_AlarmSubSecondMask_SS14_6 : SS[14:6] are don't care in Alarm comparison.
1357   *                                          Only SS[5:0] are compared
1358   *     @arg RTC_AlarmSubSecondMask_SS14_7 : SS[14:7] are don't care in Alarm comparison.
1359   *                                          Only SS[6:0] are compared
1360   *     @arg RTC_AlarmSubSecondMask_SS14_8 : SS[14:8] are don't care in Alarm comparison.
1361   *                                          Only SS[7:0] are compared
1362   *     @arg RTC_AlarmSubSecondMask_SS14_9 : SS[14:9] are don't care in Alarm comparison.
1363   *                                          Only SS[8:0] are compared
1364   *     @arg RTC_AlarmSubSecondMask_SS14_10: SS[14:10] are don't care in Alarm comparison.
1365   *                                          Only SS[9:0] are compared
1366   *     @arg RTC_AlarmSubSecondMask_SS14_11: SS[14:11] are don't care in Alarm comparison.
1367   *                                          Only SS[10:0] are compared
1368   *     @arg RTC_AlarmSubSecondMask_SS14_12: SS[14:12] are don't care in Alarm comparison.
1369   *                                          Only SS[11:0] are compared
1370   *     @arg RTC_AlarmSubSecondMask_SS14_13: SS[14:13] are don't care in Alarm comparison.
1371   *                                          Only SS[12:0] are compared
1372   *     @arg RTC_AlarmSubSecondMask_SS14   : SS[14] is don't care in Alarm comparison.
1373   *                                          Only SS[13:0] are compared
1374   *     @arg RTC_AlarmSubSecondMask_None   : SS[14:0] are compared and must match
1375   *                                          to activate alarm
1376   * @retval None
1377   */
1378 void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)
1379 {
1380   uint32_t tmpreg = 0;
1381
1382   /* Check the parameters */
1383   assert_param(IS_RTC_ALARM(RTC_Alarm));
1384   assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
1385   assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(RTC_AlarmSubSecondMask));
1386   
1387   /* Disable the write protection for RTC registers */
1388   RTC->WPR = 0xCA;
1389   RTC->WPR = 0x53;
1390   
1391   /* Configure the Alarm A or Alarm B SubSecond registers */
1392   tmpreg = (uint32_t) (uint32_t)(RTC_AlarmSubSecondValue) | (uint32_t)(RTC_AlarmSubSecondMask);
1393   
1394   if (RTC_Alarm == RTC_Alarm_A)
1395   {
1396     /* Configure the AlarmA SubSecond register */
1397     RTC->ALRMASSR = tmpreg;
1398   }
1399   else
1400   {
1401     /* Configure the Alarm B SubSecond register */
1402     RTC->ALRMBSSR = tmpreg;
1403   }
1404
1405   /* Enable the write protection for RTC registers */
1406   RTC->WPR = 0xFF;
1407
1408 }
1409
1410 /**
1411   * @brief  Gets the RTC Alarm Subseconds value.
1412   * @param  RTC_Alarm: specifies the alarm to be read.
1413   *   This parameter can be one of the following values:
1414   *     @arg RTC_Alarm_A: to select Alarm A
1415   *     @arg RTC_Alarm_B: to select Alarm B
1416   * @param  None
1417   * @retval RTC Alarm Subseconds value.
1418   */
1419 uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
1420 {
1421   uint32_t tmpreg = 0;
1422   
1423   /* Get the RTC_ALRMxR register */
1424   if (RTC_Alarm == RTC_Alarm_A)
1425   {
1426     tmpreg = (uint32_t)((RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1427   }
1428   else
1429   {
1430     tmpreg = (uint32_t)((RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1431   } 
1432   
1433   return (tmpreg);
1434 }
1435
1436 /**
1437   * @}
1438   */
1439
1440 /** @defgroup RTC_Group4 WakeUp Timer configuration functions
1441  *  @brief   WakeUp Timer configuration functions 
1442  *
1443 @verbatim   
1444  ===============================================================================
1445                      WakeUp Timer configuration functions
1446  ===============================================================================  
1447
1448   This section provide functions allowing to program and read the RTC WakeUp.
1449
1450 @endverbatim
1451   * @{
1452   */
1453
1454 /**
1455   * @brief  Configures the RTC Wakeup clock source.
1456   * @note   The WakeUp Clock source can only be changed when the RTC WakeUp
1457   *         is disabled (Use the RTC_WakeUpCmd(DISABLE)).      
1458   * @param  RTC_WakeUpClock: Wakeup Clock source.
1459   *          This parameter can be one of the following values:
1460   *            @arg RTC_WakeUpClock_RTCCLK_Div16: RTC Wakeup Counter Clock = RTCCLK/16
1461   *            @arg RTC_WakeUpClock_RTCCLK_Div8: RTC Wakeup Counter Clock = RTCCLK/8
1462   *            @arg RTC_WakeUpClock_RTCCLK_Div4: RTC Wakeup Counter Clock = RTCCLK/4
1463   *            @arg RTC_WakeUpClock_RTCCLK_Div2: RTC Wakeup Counter Clock = RTCCLK/2
1464   *            @arg RTC_WakeUpClock_CK_SPRE_16bits: RTC Wakeup Counter Clock = CK_SPRE
1465   *            @arg RTC_WakeUpClock_CK_SPRE_17bits: RTC Wakeup Counter Clock = CK_SPRE
1466   * @retval None
1467   */
1468 void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
1469 {
1470   /* Check the parameters */
1471   assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
1472
1473   /* Disable the write protection for RTC registers */
1474   RTC->WPR = 0xCA;
1475   RTC->WPR = 0x53;
1476
1477   /* Clear the Wakeup Timer clock source bits in CR register */
1478   RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1479
1480   /* Configure the clock source */
1481   RTC->CR |= (uint32_t)RTC_WakeUpClock;
1482   
1483   /* Enable the write protection for RTC registers */
1484   RTC->WPR = 0xFF; 
1485 }
1486
1487 /**
1488   * @brief  Configures the RTC Wakeup counter.
1489   * @note   The RTC WakeUp counter can only be written when the RTC WakeUp
1490   *         is disabled (Use the RTC_WakeUpCmd(DISABLE)).        
1491   * @param  RTC_WakeUpCounter: specifies the WakeUp counter.
1492   *          This parameter can be a value from 0x0000 to 0xFFFF. 
1493   * @retval None
1494   */
1495 void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
1496 {
1497   /* Check the parameters */
1498   assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));
1499   
1500   /* Disable the write protection for RTC registers */
1501   RTC->WPR = 0xCA;
1502   RTC->WPR = 0x53;
1503   
1504   /* Configure the Wakeup Timer counter */
1505   RTC->WUTR = (uint32_t)RTC_WakeUpCounter;
1506   
1507   /* Enable the write protection for RTC registers */
1508   RTC->WPR = 0xFF; 
1509 }
1510
1511 /**
1512   * @brief  Returns the RTC WakeUp timer counter value.
1513   * @param  None
1514   * @retval The RTC WakeUp Counter value.
1515   */
1516 uint32_t RTC_GetWakeUpCounter(void)
1517 {
1518   /* Get the counter value */
1519   return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));
1520 }
1521
1522 /**
1523   * @brief  Enables or Disables the RTC WakeUp timer.
1524   * @param  NewState: new state of the WakeUp timer.
1525   *          This parameter can be: ENABLE or DISABLE.
1526   * @retval None
1527   */
1528 ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
1529 {
1530   __IO uint32_t wutcounter = 0x00;
1531   uint32_t wutwfstatus = 0x00;
1532   ErrorStatus status = ERROR;
1533   
1534   /* Check the parameters */
1535   assert_param(IS_FUNCTIONAL_STATE(NewState));
1536
1537   /* Disable the write protection for RTC registers */
1538   RTC->WPR = 0xCA;
1539   RTC->WPR = 0x53;
1540
1541   if (NewState != DISABLE)
1542   {
1543     /* Enable the Wakeup Timer */
1544     RTC->CR |= (uint32_t)RTC_CR_WUTE;
1545     status = SUCCESS;    
1546   }
1547   else
1548   {
1549     /* Disable the Wakeup Timer */
1550     RTC->CR &= (uint32_t)~RTC_CR_WUTE;
1551     /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1552     do
1553     {
1554       wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
1555       wutcounter++;  
1556     } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
1557     
1558     if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
1559     {
1560       status = ERROR;
1561     }
1562     else
1563     {
1564       status = SUCCESS;
1565     }    
1566   }
1567
1568   /* Enable the write protection for RTC registers */
1569   RTC->WPR = 0xFF; 
1570   
1571   return status;
1572 }
1573
1574 /**
1575   * @}
1576   */
1577
1578 /** @defgroup RTC_Group5 Daylight Saving configuration functions
1579  *  @brief   Daylight Saving configuration functions 
1580  *
1581 @verbatim   
1582  ===============================================================================
1583                     Daylight Saving configuration functions
1584  ===============================================================================  
1585
1586   This section provide functions allowing to configure the RTC DayLight Saving.
1587
1588 @endverbatim
1589   * @{
1590   */
1591
1592 /**
1593   * @brief  Adds or substract one hour from the current time.
1594   * @param  RTC_DayLightSaveOperation: the value of hour adjustment. 
1595   *          This parameter can be one of the following values:
1596   *            @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)
1597   *            @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)
1598   * @param  RTC_StoreOperation: Specifies the value to be written in the BCK bit 
1599   *                            in CR register to store the operation.
1600   *          This parameter can be one of the following values:
1601   *            @arg RTC_StoreOperation_Reset: BCK Bit Reset
1602   *            @arg RTC_StoreOperation_Set: BCK Bit Set
1603   * @retval None
1604   */
1605 void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
1606 {
1607   /* Check the parameters */
1608   assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
1609   assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
1610
1611   /* Disable the write protection for RTC registers */
1612   RTC->WPR = 0xCA;
1613   RTC->WPR = 0x53;
1614
1615   /* Clear the bits to be configured */
1616   RTC->CR &= (uint32_t)~(RTC_CR_BCK);
1617
1618   /* Configure the RTC_CR register */
1619   RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
1620
1621   /* Enable the write protection for RTC registers */
1622   RTC->WPR = 0xFF; 
1623 }
1624
1625 /**
1626   * @brief  Returns the RTC Day Light Saving stored operation.
1627   * @param  None
1628   * @retval RTC Day Light Saving stored operation.
1629   *          - RTC_StoreOperation_Reset
1630   *          - RTC_StoreOperation_Set       
1631   */
1632 uint32_t RTC_GetStoreOperation(void)
1633 {
1634   return (RTC->CR & RTC_CR_BCK);
1635 }
1636
1637 /**
1638   * @}
1639   */
1640
1641 /** @defgroup RTC_Group6 Output pin Configuration function
1642  *  @brief   Output pin Configuration function 
1643  *
1644 @verbatim   
1645  ===============================================================================
1646                          Output pin Configuration function
1647  ===============================================================================  
1648
1649   This section provide functions allowing to configure the RTC Output source.
1650
1651 @endverbatim
1652   * @{
1653   */
1654
1655 /**
1656   * @brief  Configures the RTC output source (AFO_ALARM).
1657   * @param  RTC_Output: Specifies which signal will be routed to the RTC output. 
1658   *          This parameter can be one of the following values:
1659   *            @arg RTC_Output_Disable: No output selected
1660   *            @arg RTC_Output_AlarmA: signal of AlarmA mapped to output
1661   *            @arg RTC_Output_AlarmB: signal of AlarmB mapped to output
1662   *            @arg RTC_Output_WakeUp: signal of WakeUp mapped to output
1663   * @param  RTC_OutputPolarity: Specifies the polarity of the output signal. 
1664   *          This parameter can be one of the following:
1665   *            @arg RTC_OutputPolarity_High: The output pin is high when the 
1666   *                                 ALRAF/ALRBF/WUTF is high (depending on OSEL)
1667   *            @arg RTC_OutputPolarity_Low: The output pin is low when the 
1668   *                                 ALRAF/ALRBF/WUTF is high (depending on OSEL)
1669   * @retval None
1670   */
1671 void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
1672 {
1673   /* Check the parameters */
1674   assert_param(IS_RTC_OUTPUT(RTC_Output));
1675   assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
1676
1677   /* Disable the write protection for RTC registers */
1678   RTC->WPR = 0xCA;
1679   RTC->WPR = 0x53;
1680
1681   /* Clear the bits to be configured */
1682   RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
1683
1684   /* Configure the output selection and polarity */
1685   RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
1686
1687   /* Enable the write protection for RTC registers */
1688   RTC->WPR = 0xFF; 
1689 }
1690
1691 /**
1692   * @}
1693   */
1694
1695 /** @defgroup RTC_Group7 Digital Calibration configuration functions
1696  *  @brief   Coarse Calibration configuration functions 
1697  *
1698 @verbatim   
1699  ===============================================================================
1700                   Digital Calibration configuration functions
1701  ===============================================================================  
1702
1703 @endverbatim
1704   * @{
1705   */
1706
1707 /**
1708   * @brief  Configures the Coarse calibration parameters.
1709   * @param  RTC_CalibSign: specifies the sign of the coarse calibration value.
1710   *          This parameter can be  one of the following values:
1711   *            @arg RTC_CalibSign_Positive: The value sign is positive 
1712   *            @arg RTC_CalibSign_Negative: The value sign is negative
1713   * @param  Value: value of coarse calibration expressed in ppm (coded on 5 bits).
1714   *    
1715   * @note   This Calibration value should be between 0 and 63 when using negative
1716   *         sign with a 2-ppm step.
1717   *           
1718   * @note   This Calibration value should be between 0 and 126 when using positive
1719   *         sign with a 4-ppm step.
1720   *           
1721   * @retval An ErrorStatus enumeration value:
1722   *          - SUCCESS: RTC Coarse calibration are initialized
1723   *          - ERROR: RTC Coarse calibration are not initialized     
1724   */
1725 ErrorStatus RTC_CoarseCalibConfig(uint32_t RTC_CalibSign, uint32_t Value)
1726 {
1727   ErrorStatus status = ERROR;
1728    
1729   /* Check the parameters */
1730   assert_param(IS_RTC_CALIB_SIGN(RTC_CalibSign));
1731   assert_param(IS_RTC_CALIB_VALUE(Value)); 
1732
1733   /* Disable the write protection for RTC registers */
1734   RTC->WPR = 0xCA;
1735   RTC->WPR = 0x53;
1736
1737   /* Set Initialization mode */
1738   if (RTC_EnterInitMode() == ERROR)
1739   {
1740     status = ERROR;
1741   } 
1742   else
1743   {
1744     /* Set the coarse calibration value */
1745     RTC->CALIBR = (uint32_t)(RTC_CalibSign | Value);
1746     /* Exit Initialization mode */
1747     RTC_ExitInitMode();
1748     
1749     status = SUCCESS;
1750   } 
1751
1752   /* Enable the write protection for RTC registers */
1753   RTC->WPR = 0xFF; 
1754   
1755   return status;
1756 }
1757
1758 /**
1759   * @brief  Enables or disables the Coarse calibration process.
1760   * @param  NewState: new state of the Coarse calibration.
1761   *          This parameter can be: ENABLE or DISABLE.
1762   * @retval An ErrorStatus enumeration value:
1763   *          - SUCCESS: RTC Coarse calibration are enabled/disabled
1764   *          - ERROR: RTC Coarse calibration are not enabled/disabled    
1765   */
1766 ErrorStatus RTC_CoarseCalibCmd(FunctionalState NewState)
1767 {
1768   ErrorStatus status = ERROR;
1769   
1770   /* Check the parameters */
1771   assert_param(IS_FUNCTIONAL_STATE(NewState));
1772
1773   /* Disable the write protection for RTC registers */
1774   RTC->WPR = 0xCA;
1775   RTC->WPR = 0x53;
1776   
1777   /* Set Initialization mode */
1778   if (RTC_EnterInitMode() == ERROR)
1779   {
1780     status =  ERROR;
1781   }
1782   else
1783   {
1784     if (NewState != DISABLE)
1785     {
1786       /* Enable the Coarse Calibration */
1787       RTC->CR |= (uint32_t)RTC_CR_DCE;
1788     }
1789     else
1790     { 
1791       /* Disable the Coarse Calibration */
1792       RTC->CR &= (uint32_t)~RTC_CR_DCE;
1793     }
1794     /* Exit Initialization mode */
1795     RTC_ExitInitMode();
1796     
1797     status = SUCCESS;
1798   } 
1799   
1800   /* Enable the write protection for RTC registers */
1801   RTC->WPR = 0xFF; 
1802   
1803   return status;
1804 }
1805
1806 /**
1807   * @brief  Enables or disables the RTC clock to be output through the relative pin.
1808   * @param  NewState: new state of the digital calibration Output.
1809   *          This parameter can be: ENABLE or DISABLE.
1810   * @retval None
1811   */
1812 void RTC_CalibOutputCmd(FunctionalState NewState)
1813 {
1814   /* Check the parameters */
1815   assert_param(IS_FUNCTIONAL_STATE(NewState));
1816   
1817   /* Disable the write protection for RTC registers */
1818   RTC->WPR = 0xCA;
1819   RTC->WPR = 0x53;
1820   
1821   if (NewState != DISABLE)
1822   {
1823     /* Enable the RTC clock output */
1824     RTC->CR |= (uint32_t)RTC_CR_COE;
1825   }
1826   else
1827   { 
1828     /* Disable the RTC clock output */
1829     RTC->CR &= (uint32_t)~RTC_CR_COE;
1830   }
1831   
1832   /* Enable the write protection for RTC registers */
1833   RTC->WPR = 0xFF; 
1834 }
1835
1836 /**
1837   * @brief  Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1838   * @param  RTC_CalibOutput : Select the Calibration output Selection .
1839   *   This parameter can be one of the following values:
1840   *     @arg RTC_CalibOutput_512Hz: A signal has a regular waveform at 512Hz. 
1841   *     @arg RTC_CalibOutput_1Hz  : A signal has a regular waveform at 1Hz.
1842   * @retval None
1843 */
1844 void RTC_CalibOutputConfig(uint32_t RTC_CalibOutput)
1845 {
1846   /* Check the parameters */
1847   assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
1848
1849   /* Disable the write protection for RTC registers */
1850   RTC->WPR = 0xCA;
1851   RTC->WPR = 0x53;
1852   
1853   /*clear flags before config*/
1854   RTC->CR &= (uint32_t)~(RTC_CR_COSEL);
1855
1856   /* Configure the RTC_CR register */
1857   RTC->CR |= (uint32_t)RTC_CalibOutput;
1858
1859   /* Enable the write protection for RTC registers */
1860   RTC->WPR = 0xFF;
1861 }
1862
1863 /**
1864   * @brief  Configures the Smooth Calibration Settings.
1865   * @param  RTC_SmoothCalibPeriod : Select the Smooth Calibration Period.
1866   *   This parameter can be can be one of the following values:
1867   *     @arg RTC_SmoothCalibPeriod_32sec : The smooth calibration periode is 32s.
1868   *     @arg RTC_SmoothCalibPeriod_16sec : The smooth calibration periode is 16s.
1869   *     @arg RTC_SmoothCalibPeriod_8sec  : The smooth calibartion periode is 8s.
1870   * @param  RTC_SmoothCalibPlusPulses : Select to Set or reset the CALP bit.
1871   *   This parameter can be one of the following values:
1872   *     @arg RTC_SmoothCalibPlusPulses_Set  : Add one RTCCLK puls every 2**11 pulses.
1873   *     @arg RTC_SmoothCalibPlusPulses_Reset: No RTCCLK pulses are added.
1874   * @param  RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
1875   *   This parameter can be one any value from 0 to 0x000001FF.
1876   * @retval An ErrorStatus enumeration value:
1877   *          - SUCCESS: RTC Calib registers are configured
1878   *          - ERROR: RTC Calib registers are not configured
1879 */
1880 ErrorStatus RTC_SmoothCalibConfig(uint32_t RTC_SmoothCalibPeriod,
1881                                   uint32_t RTC_SmoothCalibPlusPulses,
1882                                   uint32_t RTC_SmouthCalibMinusPulsesValue)
1883 {
1884   ErrorStatus status = ERROR;
1885   uint32_t recalpfcount = 0;
1886
1887   /* Check the parameters */
1888   assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod));
1889   assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
1890   assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
1891
1892   /* Disable the write protection for RTC registers */
1893   RTC->WPR = 0xCA;
1894   RTC->WPR = 0x53;
1895   
1896   /* check if a calibration is pending*/
1897   if ((RTC->ISR & RTC_ISR_RECALPF) != RESET)
1898   {
1899     /* wait until the Calibration is completed*/
1900     while (((RTC->ISR & RTC_ISR_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
1901     {
1902       recalpfcount++;
1903     }
1904   }
1905
1906   /* check if the calibration pending is completed or if there is no calibration operation at all*/
1907   if ((RTC->ISR & RTC_ISR_RECALPF) == RESET)
1908   {
1909     /* Configure the Smooth calibration settings */
1910     RTC->CALR = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
1911
1912     status = SUCCESS;
1913   }
1914   else
1915   {
1916     status = ERROR;
1917   }
1918
1919   /* Enable the write protection for RTC registers */
1920   RTC->WPR = 0xFF;
1921   
1922   return (ErrorStatus)(status);
1923 }
1924
1925 /**
1926   * @}
1927   */
1928
1929
1930 /** @defgroup RTC_Group8 TimeStamp configuration functions
1931  *  @brief   TimeStamp configuration functions 
1932  *
1933 @verbatim   
1934  ===============================================================================
1935                        TimeStamp configuration functions
1936  ===============================================================================  
1937
1938 @endverbatim
1939   * @{
1940   */
1941
1942 /**
1943   * @brief  Enables or Disables the RTC TimeStamp functionality with the 
1944   *         specified time stamp pin stimulating edge.
1945   * @param  RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is 
1946   *         activated.
1947   *          This parameter can be one of the following:
1948   *            @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising 
1949   *                                    edge of the related pin.
1950   *            @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the 
1951   *                                     falling edge of the related pin.
1952   * @param  NewState: new state of the TimeStamp.
1953   *          This parameter can be: ENABLE or DISABLE.
1954   * @retval None
1955   */
1956 void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
1957 {
1958   uint32_t tmpreg = 0;
1959
1960   /* Check the parameters */
1961   assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
1962   assert_param(IS_FUNCTIONAL_STATE(NewState));
1963
1964   /* Get the RTC_CR register and clear the bits to be configured */
1965   tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
1966
1967   /* Get the new configuration */
1968   if (NewState != DISABLE)
1969   {
1970     tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
1971   }
1972   else
1973   {
1974     tmpreg |= (uint32_t)(RTC_TimeStampEdge);
1975   }
1976
1977   /* Disable the write protection for RTC registers */
1978   RTC->WPR = 0xCA;
1979   RTC->WPR = 0x53;
1980
1981   /* Configure the Time Stamp TSEDGE and Enable bits */
1982   RTC->CR = (uint32_t)tmpreg;
1983
1984   /* Enable the write protection for RTC registers */
1985   RTC->WPR = 0xFF; 
1986 }
1987
1988 /**
1989   * @brief  Get the RTC TimeStamp value and masks.
1990   * @param  RTC_Format: specifies the format of the output parameters.
1991   *          This parameter can be one of the following values:
1992   *            @arg RTC_Format_BIN: Binary data format 
1993   *            @arg RTC_Format_BCD: BCD data format
1994   * @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will 
1995   *                             contains the TimeStamp time values. 
1996   * @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will 
1997   *                             contains the TimeStamp date values.     
1998   * @retval None
1999   */
2000 void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct, 
2001                                       RTC_DateTypeDef* RTC_StampDateStruct)
2002 {
2003   uint32_t tmptime = 0, tmpdate = 0;
2004
2005   /* Check the parameters */
2006   assert_param(IS_RTC_FORMAT(RTC_Format));
2007
2008   /* Get the TimeStamp time and date registers values */
2009   tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);
2010   tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);
2011
2012   /* Fill the Time structure fields with the read parameters */
2013   RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
2014   RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
2015   RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
2016   RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);  
2017
2018   /* Fill the Date structure fields with the read parameters */
2019   RTC_StampDateStruct->RTC_Year = 0;
2020   RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
2021   RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
2022   RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
2023
2024   /* Check the input parameters format */
2025   if (RTC_Format == RTC_Format_BIN)
2026   {
2027     /* Convert the Time structure parameters to Binary format */
2028     RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);
2029     RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);
2030     RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);
2031
2032     /* Convert the Date structure parameters to Binary format */
2033     RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);
2034     RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);
2035     RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);
2036   }
2037 }
2038
2039 /**
2040   * @brief  Get the RTC timestamp Subseconds value.
2041   * @param  None
2042   * @retval RTC current timestamp Subseconds value.
2043   */
2044 uint32_t RTC_GetTimeStampSubSecond(void)
2045 {
2046   /* Get timestamp subseconds values from the correspondent registers */
2047   return (uint32_t)(RTC->TSSSR);
2048 }
2049
2050 /**
2051   * @}
2052   */
2053
2054 /** @defgroup RTC_Group9 Tampers configuration functions
2055  *  @brief   Tampers configuration functions 
2056  *
2057 @verbatim   
2058  ===============================================================================
2059                        Tampers configuration functions
2060  ===============================================================================  
2061
2062 @endverbatim
2063   * @{
2064   */
2065
2066 /**
2067   * @brief  Configures the select Tamper pin edge.
2068   * @param  RTC_Tamper: Selected tamper pin.
2069   *          This parameter can be RTC_Tamper_1.
2070   * @param  RTC_TamperTrigger: Specifies the trigger on the tamper pin that 
2071   *         stimulates tamper event. 
2072   *   This parameter can be one of the following values:
2073   *     @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
2074   *     @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
2075   *     @arg RTC_TamperTrigger_LowLevel: Low Level of the tamper pin causes tamper event.
2076   *     @arg RTC_TamperTrigger_HighLevel: High Level of the tamper pin causes tamper event.
2077   * @retval None
2078   */
2079 void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
2080 {
2081   /* Check the parameters */
2082   assert_param(IS_RTC_TAMPER(RTC_Tamper)); 
2083   assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
2084  
2085   if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
2086   {  
2087     /* Configure the RTC_TAFCR register */
2088     RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));     
2089   }
2090   else
2091   { 
2092     /* Configure the RTC_TAFCR register */
2093     RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);  
2094   }  
2095 }
2096
2097 /**
2098   * @brief  Enables or Disables the Tamper detection.
2099   * @param  RTC_Tamper: Selected tamper pin.
2100   *          This parameter can be RTC_Tamper_1.
2101   * @param  NewState: new state of the tamper pin.
2102   *          This parameter can be: ENABLE or DISABLE.                   
2103   * @retval None
2104   */
2105 void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
2106 {
2107   /* Check the parameters */
2108   assert_param(IS_RTC_TAMPER(RTC_Tamper));  
2109   assert_param(IS_FUNCTIONAL_STATE(NewState));
2110   
2111   if (NewState != DISABLE)
2112   {
2113     /* Enable the selected Tamper pin */
2114     RTC->TAFCR |= (uint32_t)RTC_Tamper;
2115   }
2116   else
2117   {
2118     /* Disable the selected Tamper pin */
2119     RTC->TAFCR &= (uint32_t)~RTC_Tamper;    
2120   }  
2121 }
2122
2123 /**
2124   * @brief  Configures the Tampers Filter.
2125   * @param  RTC_TamperFilter: Specifies the tampers filter.
2126   *   This parameter can be one of the following values:
2127   *     @arg RTC_TamperFilter_Disable: Tamper filter is disabled.
2128   *     @arg RTC_TamperFilter_2Sample: Tamper is activated after 2 consecutive 
2129   *                                    samples at the active level 
2130   *     @arg RTC_TamperFilter_4Sample: Tamper is activated after 4 consecutive 
2131   *                                    samples at the active level
2132   *     @arg RTC_TamperFilter_8Sample: Tamper is activated after 8 consecutive 
2133   *                                    samples at the active level 
2134   * @retval None
2135   */
2136 void RTC_TamperFilterConfig(uint32_t RTC_TamperFilter)
2137 {
2138   /* Check the parameters */
2139   assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));
2140    
2141   /* Clear TAMPFLT[1:0] bits in the RTC_TAFCR register */
2142   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFLT);
2143
2144   /* Configure the RTC_TAFCR register */
2145   RTC->TAFCR |= (uint32_t)RTC_TamperFilter;
2146 }
2147
2148 /**
2149   * @brief  Configures the Tampers Sampling Frequency.
2150   * @param  RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency.
2151   *   This parameter can be one of the following values:
2152   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div32768: Each of the tamper inputs are sampled
2153   *                                           with a frequency =  RTCCLK / 32768
2154   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div16384: Each of the tamper inputs are sampled
2155   *                                           with a frequency =  RTCCLK / 16384
2156   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div8192: Each of the tamper inputs are sampled
2157   *                                           with a frequency =  RTCCLK / 8192
2158   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div4096: Each of the tamper inputs are sampled
2159   *                                           with a frequency =  RTCCLK / 4096
2160   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div2048: Each of the tamper inputs are sampled
2161   *                                           with a frequency =  RTCCLK / 2048
2162   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div1024: Each of the tamper inputs are sampled
2163   *                                           with a frequency =  RTCCLK / 1024
2164   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div512: Each of the tamper inputs are sampled
2165   *                                           with a frequency =  RTCCLK / 512  
2166   *     @arg RTC_TamperSamplingFreq_RTCCLK_Div256: Each of the tamper inputs are sampled
2167   *                                           with a frequency =  RTCCLK / 256  
2168   * @retval None
2169   */
2170 void RTC_TamperSamplingFreqConfig(uint32_t RTC_TamperSamplingFreq)
2171 {
2172   /* Check the parameters */
2173   assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq));
2174  
2175   /* Clear TAMPFREQ[2:0] bits in the RTC_TAFCR register */
2176   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFREQ);
2177
2178   /* Configure the RTC_TAFCR register */
2179   RTC->TAFCR |= (uint32_t)RTC_TamperSamplingFreq;
2180 }
2181
2182 /**
2183   * @brief  Configures the Tampers Pins input Precharge Duration.
2184   * @param  RTC_TamperPrechargeDuration: Specifies the Tampers Pins input
2185   *         Precharge Duration.
2186   *   This parameter can be one of the following values:
2187   *     @arg RTC_TamperPrechargeDuration_1RTCCLK: Tamper pins are pre-charged before sampling during 1 RTCCLK cycle
2188   *     @arg RTC_TamperPrechargeDuration_2RTCCLK: Tamper pins are pre-charged before sampling during 2 RTCCLK cycle
2189   *     @arg RTC_TamperPrechargeDuration_4RTCCLK: Tamper pins are pre-charged before sampling during 4 RTCCLK cycle    
2190   *     @arg RTC_TamperPrechargeDuration_8RTCCLK: Tamper pins are pre-charged before sampling during 8 RTCCLK cycle
2191   * @retval None
2192   */
2193 void RTC_TamperPinsPrechargeDuration(uint32_t RTC_TamperPrechargeDuration)
2194 {
2195   /* Check the parameters */
2196   assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(RTC_TamperPrechargeDuration));
2197    
2198   /* Clear TAMPPRCH[1:0] bits in the RTC_TAFCR register */
2199   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPPRCH);
2200
2201   /* Configure the RTC_TAFCR register */
2202   RTC->TAFCR |= (uint32_t)RTC_TamperPrechargeDuration;
2203 }
2204
2205 /**
2206   * @brief  Enables or Disables the TimeStamp on Tamper Detection Event.
2207   * @note   The timestamp is valid even the TSE bit in tamper control register 
2208   *         is reset.   
2209   * @param  NewState: new state of the timestamp on tamper event.
2210   *         This parameter can be: ENABLE or DISABLE.
2211   * @retval None
2212   */
2213 void RTC_TimeStampOnTamperDetectionCmd(FunctionalState NewState)
2214 {
2215   /* Check the parameters */
2216   assert_param(IS_FUNCTIONAL_STATE(NewState));
2217    
2218   if (NewState != DISABLE)
2219   {
2220     /* Save timestamp on tamper detection event */
2221     RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPTS;
2222   }
2223   else
2224   {
2225     /* Tamper detection does not cause a timestamp to be saved */
2226     RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPTS;    
2227   }
2228 }
2229
2230 /**
2231   * @brief  Enables or Disables the Precharge of Tamper pin.
2232   * @param  NewState: new state of tamper pull up.
2233   *   This parameter can be: ENABLE or DISABLE.                   
2234   * @retval None
2235   */
2236 void RTC_TamperPullUpCmd(FunctionalState NewState)
2237 {
2238   /* Check the parameters */
2239   assert_param(IS_FUNCTIONAL_STATE(NewState));
2240   
2241  if (NewState != DISABLE)
2242   {
2243     /* Enable precharge of the selected Tamper pin */
2244     RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPPUDIS; 
2245   }
2246   else
2247   {
2248     /* Disable precharge of the selected Tamper pin */
2249     RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPPUDIS;    
2250   } 
2251 }
2252
2253 /**
2254   * @}
2255   */
2256
2257 /** @defgroup RTC_Group10 Backup Data Registers configuration functions
2258  *  @brief   Backup Data Registers configuration functions  
2259  *
2260 @verbatim   
2261  ===============================================================================
2262                        Backup Data Registers configuration functions 
2263  ===============================================================================  
2264
2265 @endverbatim
2266   * @{
2267   */
2268
2269 /**
2270   * @brief  Writes a data in a specified RTC Backup data register.
2271   * @param  RTC_BKP_DR: RTC Backup data Register number.
2272   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to 
2273   *                          specify the register.
2274   * @param  Data: Data to be written in the specified RTC Backup data register.                     
2275   * @retval None
2276   */
2277 void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
2278 {
2279   __IO uint32_t tmp = 0;
2280   
2281   /* Check the parameters */
2282   assert_param(IS_RTC_BKP(RTC_BKP_DR));
2283
2284   tmp = RTC_BASE + 0x50;
2285   tmp += (RTC_BKP_DR * 4);
2286
2287   /* Write the specified register */
2288   *(__IO uint32_t *)tmp = (uint32_t)Data;
2289 }
2290
2291 /**
2292   * @brief  Reads data from the specified RTC Backup data Register.
2293   * @param  RTC_BKP_DR: RTC Backup data Register number.
2294   *          This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to 
2295   *                          specify the register.                   
2296   * @retval None
2297   */
2298 uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
2299 {
2300   __IO uint32_t tmp = 0;
2301   
2302   /* Check the parameters */
2303   assert_param(IS_RTC_BKP(RTC_BKP_DR));
2304
2305   tmp = RTC_BASE + 0x50;
2306   tmp += (RTC_BKP_DR * 4);
2307   
2308   /* Read the specified register */
2309   return (*(__IO uint32_t *)tmp);
2310 }
2311
2312 /**
2313   * @}
2314   */
2315
2316 /** @defgroup RTC_Group11 RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration functions
2317  *  @brief   RTC Tamper and TimeStamp Pins Selection and Output Type Config 
2318  *           configuration functions  
2319  *
2320 @verbatim   
2321  ===============================================================================
2322   RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration 
2323   functions 
2324  ===============================================================================  
2325
2326 @endverbatim
2327   * @{
2328   */
2329
2330 /**
2331   * @brief  Selects the RTC Tamper Pin.
2332   * @param  RTC_TamperPin: specifies the RTC Tamper Pin.
2333   *          This parameter can be one of the following values:
2334   *            @arg RTC_TamperPin_PC13: PC13 is selected as RTC Tamper Pin.
2335   *            @arg RTC_TamperPin_PI8: PI8 is selected as RTC Tamper Pin.    
2336   * @retval None
2337   */
2338 void RTC_TamperPinSelection(uint32_t RTC_TamperPin)
2339 {
2340   /* Check the parameters */
2341   assert_param(IS_RTC_TAMPER_PIN(RTC_TamperPin));
2342   
2343   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPINSEL);
2344   RTC->TAFCR |= (uint32_t)(RTC_TamperPin);  
2345 }
2346
2347 /**
2348   * @brief  Selects the RTC TimeStamp Pin.
2349   * @param  RTC_TimeStampPin: specifies the RTC TimeStamp Pin.
2350   *          This parameter can be one of the following values:
2351   *            @arg RTC_TimeStampPin_PC13: PC13 is selected as RTC TimeStamp Pin.
2352   *            @arg RTC_TimeStampPin_PI8: PI8 is selected as RTC TimeStamp Pin.    
2353   * @retval None
2354   */
2355 void RTC_TimeStampPinSelection(uint32_t RTC_TimeStampPin)
2356 {
2357   /* Check the parameters */
2358   assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
2359   
2360   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TSINSEL);
2361   RTC->TAFCR |= (uint32_t)(RTC_TimeStampPin);  
2362 }
2363
2364 /**
2365   * @brief  Configures the RTC Output Pin mode. 
2366   * @param  RTC_OutputType: specifies the RTC Output (PC13) pin mode.
2367   *          This parameter can be one of the following values:
2368   *            @arg RTC_OutputType_OpenDrain: RTC Output (PC13) is configured in 
2369   *                                    Open Drain mode.
2370   *            @arg RTC_OutputType_PushPull:  RTC Output (PC13) is configured in 
2371   *                                    Push Pull mode.    
2372   * @retval None
2373   */
2374 void RTC_OutputTypeConfig(uint32_t RTC_OutputType)
2375 {
2376   /* Check the parameters */
2377   assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
2378   
2379   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);
2380   RTC->TAFCR |= (uint32_t)(RTC_OutputType);  
2381 }
2382
2383 /**
2384   * @}
2385   */
2386
2387 /** @defgroup RTC_Group12 Shift control synchronisation functions
2388  *  @brief   Shift control synchronisation functions 
2389  *
2390 @verbatim   
2391  ===============================================================================
2392                    Shift control synchronisation functions
2393  ===============================================================================  
2394
2395 @endverbatim
2396   * @{
2397   */
2398
2399 /**
2400   * @brief  Configures the Synchronization Shift Control Settings.
2401   * @note   When REFCKON is set, firmware must not write to Shift control register 
2402   * @param  RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar.
2403   *   This parameter can be one of the following values :
2404   *     @arg RTC_ShiftAdd1S_Set  : Add one second to the clock calendar. 
2405   *     @arg RTC_ShiftAdd1S_Reset: No effect.
2406   * @param  RTC_ShiftSubFS: Select the number of Second Fractions to Substitute.
2407   *         This parameter can be one any value from 0 to 0x7FFF.
2408   * @retval An ErrorStatus enumeration value:
2409   *          - SUCCESS: RTC Shift registers are configured
2410   *          - ERROR: RTC Shift registers are not configured
2411 */
2412 ErrorStatus RTC_SynchroShiftConfig(uint32_t RTC_ShiftAdd1S, uint32_t RTC_ShiftSubFS)
2413 {
2414   ErrorStatus status = ERROR;
2415   uint32_t shpfcount = 0;
2416
2417   /* Check the parameters */
2418   assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
2419   assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));
2420
2421   /* Disable the write protection for RTC registers */
2422   RTC->WPR = 0xCA;
2423   RTC->WPR = 0x53;
2424   
2425   /* Check if a Shift is pending*/
2426   if ((RTC->ISR & RTC_ISR_SHPF) != RESET)
2427   {
2428     /* Wait until the shift is completed*/
2429     while (((RTC->ISR & RTC_ISR_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
2430     {
2431       shpfcount++;
2432     }
2433   }
2434
2435   /* Check if the Shift pending is completed or if there is no Shift operation at all*/
2436   if ((RTC->ISR & RTC_ISR_SHPF) == RESET)
2437   {
2438     /* check if the reference clock detection is disabled */
2439     if((RTC->CR & RTC_CR_REFCKON) == RESET)
2440     {
2441       /* Configure the Shift settings */
2442       RTC->SHIFTR = (uint32_t)(uint32_t)(RTC_ShiftSubFS) | (uint32_t)(RTC_ShiftAdd1S);
2443     
2444       if(RTC_WaitForSynchro() == ERROR)
2445       {
2446         status = ERROR;
2447       }
2448       else
2449       {
2450         status = SUCCESS;
2451       }
2452     }
2453     else
2454     {
2455       status = ERROR;
2456     }
2457   }
2458   else
2459   {
2460     status = ERROR;
2461   }
2462
2463   /* Enable the write protection for RTC registers */
2464   RTC->WPR = 0xFF;
2465   
2466   return (ErrorStatus)(status);
2467 }
2468
2469 /**
2470   * @}
2471   */
2472
2473 /** @defgroup RTC_Group13 Interrupts and flags management functions
2474  *  @brief   Interrupts and flags management functions  
2475  *
2476 @verbatim   
2477  ===============================================================================
2478                        Interrupts and flags management functions
2479  ===============================================================================  
2480  All RTC interrupts are connected to the EXTI controller.
2481  
2482  - To enable the RTC Alarm interrupt, the following sequence is required:
2483    - Configure and enable the EXTI Line 17 in interrupt mode and select the rising 
2484      edge sensitivity using the EXTI_Init() function.
2485    - Configure and enable the RTC_Alarm IRQ channel in the NVIC using the NVIC_Init()
2486      function.
2487    - Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B) using
2488      the RTC_SetAlarm() and RTC_AlarmCmd() functions.
2489
2490  - To enable the RTC Wakeup interrupt, the following sequence is required:
2491    - Configure and enable the EXTI Line 22 in interrupt mode and select the rising 
2492      edge sensitivity using the EXTI_Init() function.
2493    - Configure and enable the RTC_WKUP IRQ channel in the NVIC using the NVIC_Init()
2494      function.
2495    - Configure the RTC to generate the RTC wakeup timer event using the 
2496      RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions.
2497
2498  - To enable the RTC Tamper interrupt, the following sequence is required:
2499    - Configure and enable the EXTI Line 21 in interrupt mode and select the rising 
2500      edge sensitivity using the EXTI_Init() function.
2501    - Configure and enable the TAMP_STAMP IRQ channel in the NVIC using the NVIC_Init()
2502      function.
2503    - Configure the RTC to detect the RTC tamper event using the 
2504      RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
2505
2506  - To enable the RTC TimeStamp interrupt, the following sequence is required:
2507    - Configure and enable the EXTI Line 21 in interrupt mode and select the rising 
2508      edge sensitivity using the EXTI_Init() function.
2509    - Configure and enable the TAMP_STAMP IRQ channel in the NVIC using the NVIC_Init()
2510      function.
2511    - Configure the RTC to detect the RTC time-stamp event using the 
2512      RTC_TimeStampCmd() functions.
2513
2514 @endverbatim
2515   * @{
2516   */
2517
2518 /**
2519   * @brief  Enables or disables the specified RTC interrupts.
2520   * @param  RTC_IT: specifies the RTC interrupt sources to be enabled or disabled. 
2521   *          This parameter can be any combination of the following values:
2522   *            @arg RTC_IT_TS:  Time Stamp interrupt mask
2523   *            @arg RTC_IT_WUT:  WakeUp Timer interrupt mask
2524   *            @arg RTC_IT_ALRB:  Alarm B interrupt mask
2525   *            @arg RTC_IT_ALRA:  Alarm A interrupt mask
2526   *            @arg RTC_IT_TAMP: Tamper event interrupt mask
2527   * @param  NewState: new state of the specified RTC interrupts.
2528   *          This parameter can be: ENABLE or DISABLE.
2529   * @retval None
2530   */
2531 void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
2532 {
2533   /* Check the parameters */
2534   assert_param(IS_RTC_CONFIG_IT(RTC_IT));
2535   assert_param(IS_FUNCTIONAL_STATE(NewState));
2536
2537   /* Disable the write protection for RTC registers */
2538   RTC->WPR = 0xCA;
2539   RTC->WPR = 0x53;
2540
2541   if (NewState != DISABLE)
2542   {
2543     /* Configure the Interrupts in the RTC_CR register */
2544     RTC->CR |= (uint32_t)(RTC_IT & ~RTC_TAFCR_TAMPIE);
2545     /* Configure the Tamper Interrupt in the RTC_TAFCR */
2546     RTC->TAFCR |= (uint32_t)(RTC_IT & RTC_TAFCR_TAMPIE);
2547   }
2548   else
2549   {
2550     /* Configure the Interrupts in the RTC_CR register */
2551     RTC->CR &= (uint32_t)~(RTC_IT & (uint32_t)~RTC_TAFCR_TAMPIE);
2552     /* Configure the Tamper Interrupt in the RTC_TAFCR */
2553     RTC->TAFCR &= (uint32_t)~(RTC_IT & RTC_TAFCR_TAMPIE);
2554   }
2555   /* Enable the write protection for RTC registers */
2556   RTC->WPR = 0xFF; 
2557 }
2558
2559 /**
2560   * @brief  Checks whether the specified RTC flag is set or not.
2561   * @param  RTC_FLAG: specifies the flag to check.
2562   *          This parameter can be one of the following values:
2563   *            @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
2564   *            @arg RTC_FLAG_TSOVF: Time Stamp OverFlow flag
2565   *            @arg RTC_FLAG_TSF: Time Stamp event flag
2566   *            @arg RTC_FLAG_WUTF: WakeUp Timer flag
2567   *            @arg RTC_FLAG_ALRBF: Alarm B flag
2568   *            @arg RTC_FLAG_ALRAF: Alarm A flag
2569   *            @arg RTC_FLAG_INITF: Initialization mode flag
2570   *            @arg RTC_FLAG_RSF: Registers Synchronized flag
2571   *            @arg RTC_FLAG_INITS: Registers Configured flag
2572   *            @arg RTC_FLAG_WUTWF: WakeUp Timer Write flag
2573   *            @arg RTC_FLAG_ALRBWF: Alarm B Write flag
2574   *            @arg RTC_FLAG_ALRAWF: Alarm A write flag
2575   * @retval The new state of RTC_FLAG (SET or RESET).
2576   */
2577 FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
2578 {
2579   FlagStatus bitstatus = RESET;
2580   uint32_t tmpreg = 0;
2581   
2582   /* Check the parameters */
2583   assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
2584   
2585   /* Get all the flags */
2586   tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);
2587   
2588   /* Return the status of the flag */
2589   if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)
2590   {
2591     bitstatus = SET;
2592   }
2593   else
2594   {
2595     bitstatus = RESET;
2596   }
2597   return bitstatus;
2598 }
2599
2600 /**
2601   * @brief  Clears the RTC's pending flags.
2602   * @param  RTC_FLAG: specifies the RTC flag to clear.
2603   *          This parameter can be any combination of the following values:
2604   *            @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
2605   *            @arg RTC_FLAG_TSOVF: Time Stamp Overflow flag 
2606   *            @arg RTC_FLAG_TSF: Time Stamp event flag
2607   *            @arg RTC_FLAG_WUTF: WakeUp Timer flag
2608   *            @arg RTC_FLAG_ALRBF: Alarm B flag
2609   *            @arg RTC_FLAG_ALRAF: Alarm A flag
2610   *            @arg RTC_FLAG_RSF: Registers Synchronized flag
2611   * @retval None
2612   */
2613 void RTC_ClearFlag(uint32_t RTC_FLAG)
2614 {
2615   /* Check the parameters */
2616   assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
2617
2618   /* Clear the Flags in the RTC_ISR register */
2619   RTC->ISR = (uint32_t)((uint32_t)(~((RTC_FLAG | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));  
2620 }
2621
2622 /**
2623   * @brief  Checks whether the specified RTC interrupt has occurred or not.
2624   * @param  RTC_IT: specifies the RTC interrupt source to check.
2625   *          This parameter can be one of the following values:
2626   *            @arg RTC_IT_TS: Time Stamp interrupt 
2627   *            @arg RTC_IT_WUT: WakeUp Timer interrupt 
2628   *            @arg RTC_IT_ALRB: Alarm B interrupt 
2629   *            @arg RTC_IT_ALRA: Alarm A interrupt 
2630   *            @arg RTC_IT_TAMP1: Tamper 1 event interrupt 
2631   * @retval The new state of RTC_IT (SET or RESET).
2632   */
2633 ITStatus RTC_GetITStatus(uint32_t RTC_IT)
2634 {
2635   ITStatus bitstatus = RESET;
2636   uint32_t tmpreg = 0, enablestatus = 0;
2637  
2638   /* Check the parameters */
2639   assert_param(IS_RTC_GET_IT(RTC_IT));
2640   
2641   /* Get the TAMPER Interrupt enable bit and pending bit */
2642   tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));
2643  
2644   /* Get the Interrupt enable Status */
2645   enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & (RTC_IT >> 15)));
2646   
2647   /* Get the Interrupt pending bit */
2648   tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));
2649   
2650   /* Get the status of the Interrupt */
2651   if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))
2652   {
2653     bitstatus = SET;
2654   }
2655   else
2656   {
2657     bitstatus = RESET;
2658   }
2659   return bitstatus;
2660 }
2661
2662 /**
2663   * @brief  Clears the RTC's interrupt pending bits.
2664   * @param  RTC_IT: specifies the RTC interrupt pending bit to clear.
2665   *          This parameter can be any combination of the following values:
2666   *            @arg RTC_IT_TS: Time Stamp interrupt 
2667   *            @arg RTC_IT_WUT: WakeUp Timer interrupt 
2668   *            @arg RTC_IT_ALRB: Alarm B interrupt 
2669   *            @arg RTC_IT_ALRA: Alarm A interrupt 
2670   *            @arg RTC_IT_TAMP1: Tamper 1 event interrupt 
2671   * @retval None
2672   */
2673 void RTC_ClearITPendingBit(uint32_t RTC_IT)
2674 {
2675   uint32_t tmpreg = 0;
2676
2677   /* Check the parameters */
2678   assert_param(IS_RTC_CLEAR_IT(RTC_IT));
2679
2680   /* Get the RTC_ISR Interrupt pending bits mask */
2681   tmpreg = (uint32_t)(RTC_IT >> 4);
2682
2683   /* Clear the interrupt pending bits in the RTC_ISR register */
2684   RTC->ISR = (uint32_t)((uint32_t)(~((tmpreg | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT))); 
2685 }
2686
2687 /**
2688   * @}
2689   */
2690
2691 /**
2692   * @brief  Converts a 2 digit decimal to BCD format.
2693   * @param  Value: Byte to be converted.
2694   * @retval Converted byte
2695   */
2696 static uint8_t RTC_ByteToBcd2(uint8_t Value)
2697 {
2698   uint8_t bcdhigh = 0;
2699   
2700   while (Value >= 10)
2701   {
2702     bcdhigh++;
2703     Value -= 10;
2704   }
2705   
2706   return  ((uint8_t)(bcdhigh << 4) | Value);
2707 }
2708
2709 /**
2710   * @brief  Convert from 2 digit BCD to Binary.
2711   * @param  Value: BCD value to be converted.
2712   * @retval Converted word
2713   */
2714 static uint8_t RTC_Bcd2ToByte(uint8_t Value)
2715 {
2716   uint8_t tmp = 0;
2717   tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
2718   return (tmp + (Value & (uint8_t)0x0F));
2719 }
2720
2721 /**
2722   * @}
2723   */ 
2724
2725 /**
2726   * @}
2727   */ 
2728
2729 /**
2730   * @}
2731   */ 
2732
2733 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/