fbcf67997bf79303c6635475d5ee80fbc3f2ec01
[fw/stlink] / example / libstm32l_discovery / src / stm32l1xx_rtc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_rtc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    31-December-2010\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Real-Time Clock (RTC) peripheral:           \r
9   *           - Initialization\r
10   *           - Calendar (Time and Date) configuration\r
11   *           - Alarms (Alarm A and Alarm B) configuration\r
12   *           - WakeUp Timer configuration\r
13   *           - Daylight Saving configuration\r
14   *           - Output pin Configuration\r
15   *           - Digital Calibration configuration\r
16   *           - TimeStamp configuration\r
17   *           - Tampers configuration\r
18   *           - Backup Data Registers configuration\r
19   *           - Output Type Config configuration\r
20   *           - Interrupts and flags management       \r
21   *                     \r
22   *  @verbatim\r
23   *       \r
24   *          ===================================================================      \r
25   *                                    RTC Domain Reset\r
26   *          ===================================================================          \r
27   *          After power-on reset, the RTC domain (RTC clock source configuration,\r
28   *          RTC registers and RTC Backup data registers) is reset. You can also\r
29   *          reset this domain by software using the RCC_RTCResetCmd() function.        \r
30   *         \r
31   *          ===================================================================      \r
32   *                                  RTC Operating Condition\r
33   *          ===================================================================            \r
34   *          As long as the supply voltage remains in the operating range, \r
35   *          the RTC never stops, regardless of the device status (Run mode, \r
36   *          low power modes or under reset).\r
37   *              \r
38   *          ===================================================================      \r
39   *                                   RTC Domain Access\r
40   *          ===================================================================          \r
41   *          After reset, the RTC domain (RTC clock source configuration,\r
42   *          RTC registers and RTC Backup data registers) are protected against \r
43   *          possible stray write accesses. \r
44   *          To enable access to the RTC Domain and RTC registers, proceed as follows:\r
45   *            - Enable the Power Controller (PWR) APB1 interface clock using the\r
46   *              RCC_APB1PeriphClockCmd() function.\r
47   *            - Enable access to RTC domain using the PWR_RTCAccessCmd() function.\r
48   *            - Select the RTC clock source using the RCC_RTCCLKConfig() function.\r
49   *            - Enable RTC Clock using the RCC_RTCCLKCmd() function.\r
50   *              \r
51   *          ===================================================================      \r
52   *                                   RTC Driver: how to use it\r
53   *          ===================================================================          \r
54   *            - Enable the RTC domain access (see description in the section above)\r
55   *            - Configure the RTC Prescaler (Asynchronous and Synchronous) and\r
56   *              RTC hour format using the RTC_Init() function.\r
57   *                \r
58   *          Time and Date configuration\r
59   *          ===========================    \r
60   *            - To configure the RTC Calendar (Time and Date) use the RTC_SetTime()\r
61   *              and RTC_SetDate() functions.\r
62   *            - To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()\r
63   *              functions.\r
64   *            - Use the RTC_DayLightSavingConfig() function to add or sub one\r
65   *              hour to the RTC Calendar.    \r
66   *                \r
67   *          Alarm configuration\r
68   *          ===================    \r
69   *            - To configure the RTC Alarm use the RTC_SetAlarm() function.\r
70   *            - Enable the selected RTC Alarm using the RTC_AlarmCmd() function  \r
71   *            - To read the RTC Alarm, use the RTC_GetAlarm() function.\r
72   *              \r
73   *          RTC Wakeup configuration\r
74   *          ========================    \r
75   *            - Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()\r
76   *              function.\r
77   *            - Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter() \r
78   *              function  \r
79   *            - Enable the RTC WakeUp using the RTC_WakeUpCmd() function  \r
80   *            - To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter() \r
81   *              function.\r
82   *                \r
83   *          Outputs configuration\r
84   *          =====================  \r
85   *          The RTC has 2 different outputs:\r
86   *            - AFO_ALARM: this output is used to manage the RTC Alarm A, Alarm B\r
87   *              and WaKeUp signals.          \r
88   *              To output the selected RTC signal on RTC_AF1 pin, use the \r
89   *              RTC_OutputConfig() function.                \r
90   *            - AFO_CALIB: this output is used to manage the RTC Clock divided \r
91   *              by 64 (512Hz) signal.\r
92   *              To output the RTC Clock on RTC_AF1 pin, use the RTC_CalibOutputCmd()\r
93   *              function.                \r
94   *                                                           \r
95   *          Digital Calibration configuration\r
96   *          =================================    \r
97   *            - Configure the RTC Digital Calibration Value and the corresponding\r
98   *              sign using the RTC_DigitalCalibConfig() function.\r
99   *            - Enable the RTC Digital Calibration using the RTC_DigitalCalibCmd() \r
100   *              function  \r
101   *                \r
102   *          TimeStamp configuration\r
103   *          =======================    \r
104   *            - Configure the RTC_AF1 trigger and enables the RTC TimeStamp \r
105   *              using the RTC_TimeStampCmd() function.\r
106   *            - To read the RTC TimeStamp Time and Date register, use the \r
107   *              RTC_GetTimeStamp() function.  \r
108   *\r
109   *          Tamper configuration\r
110   *          ====================    \r
111   *            - Configure the RTC Tamper trigger using the RTC_TamperConfig() \r
112   *              function.\r
113   *            - Enable the RTC Tamper using the RTC_TamperCmd() function.\r
114   *\r
115   *          Backup Data Registers configuration\r
116   *          ===================================    \r
117   *            - To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()\r
118   *              function.  \r
119   *            - To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()\r
120   *              function.  \r
121   *                                  \r
122   *          ===================================================================      \r
123   *                                  RTC and low power modes\r
124   *          =================================================================== \r
125   *           The MCU can be woken up from a low power mode by an RTC alternate \r
126   *           function.\r
127   *           The RTC alternate functions are the RTC alarms (Alarm A and Alarm B), \r
128   *           RTC wakeup, RTC tamper event detection and RTC time stamp event detection.\r
129   *           These RTC alternate functions can wake up the system from the Stop \r
130   *           and Standby lowpower modes.\r
131   *           The system can also wake up from low power modes without depending \r
132   *           on an external interrupt (Auto-wakeup mode), by using the RTC alarm \r
133   *           or the RTC wakeup events.\r
134   *           The RTC provides a programmable time base for waking up from the \r
135   *           Stop or Standby mode at regular intervals.\r
136   *           Wakeup from STOP and Standby modes is possible only when the RTC \r
137   *           clock source is LSE or LSI.\r
138   *           \r
139   *          ===================================================================      \r
140   *                            Selection of RTC_AF1 alternate functions\r
141   *          ===================================================================     \r
142   *          The RTC_AF1 pin (PC13) can be used for the following purposes:\r
143   *            - Wakeup pin 2 (WKUP2) using the PWR_WakeUpPinCmd() function.\r
144   *            - AFO_ALARM output       \r
145   *            - AFO_CALIB output\r
146   *            - AFI_TAMPER\r
147   *            - AFI_TIMESTAMP\r
148   *                         \r
149   * +------------------------------------------------------------------------------------------+     \r
150   * |     Pin         |AFO_ALARM |AFO_CALIB |AFI_TAMPER |AFI_TIMESTAMP | WKUP2  |ALARMOUTTYPE  |\r
151   * |  configuration  | ENABLED  | ENABLED  |  ENABLED  |   ENABLED    |ENABLED |  AFO_ALARM   |\r
152   * |  and function   |          |          |           |              |        |Configuration |\r
153   * |-----------------|----------|----------|-----------|--------------|--------|--------------|             \r
154   * |   Alarm out     |          |          |           |              | Don't  |              |\r
155   * |   output OD     |     1    |    0     |Don't care | Don't care   | care   |      0       |\r
156   * |-----------------|----------|----------|-----------|--------------|--------|--------------|  \r
157   * |   Alarm out     |          |          |           |              | Don't  |              |\r
158   * |   output PP     |     1    |    0     |Don't care | Don't care   | care   |      1       |\r
159   * |-----------------|----------|----------|-----------|--------------|--------|--------------|  \r
160   * | Calibration out |          |          |           |              | Don't  |              |\r
161   * |   output PP     |     0    |    1     |Don't care | Don't care   | care   |  Don't care  |\r
162   * |-----------------|----------|----------|-----------|--------------|--------|--------------|  \r
163   * |  TAMPER input   |          |          |           |              | Don't  |              | \r
164   * |   floating      |     0    |    0     |     1     |      0       | care   |  Don't care  |\r
165   * |-----------------|----------|----------|-----------|--------------|--------|--------------|  \r
166   * |  TIMESTAMP and  |          |          |           |              | Don't  |              |\r
167   * |  TAMPER input   |     0    |    0     |     1     |      1       | care   |  Don't care  |\r
168   * |   floating      |          |          |           |              |        |              |                 \r
169   * |-----------------|----------|----------|-----------|--------------|--------|--------------|  \r
170   * | TIMESTAMP input |          |          |           |              | Don't  |              | \r
171   * |    floating     |     0    |    0     |     0     |      1       | care   |  Don't care  |                 \r
172   * |-----------------|----------|----------|-----------|--------------|--------|--------------|  \r
173   * |  Wakeup Pin 2   |     0    |    0     |     0     |      0       |   1    |  Don't care  |                 \r
174   * |-----------------|----------|----------|-----------|--------------|--------|--------------|  \r
175   * |  Standard GPIO  |     0    |    0     |     0     |      0       |   0    |  Don't care  |             \r
176   * +------------------------------------------------------------------------------------------+  \r
177   *\r
178   *  @endverbatim\r
179   *                      \r
180   ******************************************************************************\r
181   * @attention\r
182   *\r
183   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
184   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
185   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
186   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
187   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
188   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
189   *\r
190   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
191   ******************************************************************************  \r
192   */ \r
193 \r
194 /* Includes ------------------------------------------------------------------*/\r
195 #include "stm32l1xx_rtc.h"\r
196 #include "stm32l1xx_rcc.h"\r
197 \r
198 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
199   * @{\r
200   */\r
201 \r
202 /** @defgroup RTC \r
203   * @brief RTC driver modules\r
204   * @{\r
205   */\r
206 \r
207 /* Private typedef -----------------------------------------------------------*/\r
208 /* Private define ------------------------------------------------------------*/\r
209 \r
210 /* Masks Definition */\r
211 #define RTC_TR_RESERVED_MASK    ((uint32_t)0x007F7F7F)\r
212 #define RTC_DR_RESERVED_MASK    ((uint32_t)0x00FFFF3F) \r
213 #define RTC_INIT_MASK           ((uint32_t)0xFFFFFFFF)  \r
214 #define RTC_RSF_MASK            ((uint32_t)0xFFFFFF5F)\r
215 #define RTC_FLAGS_MASK          ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_WUTF | \\r
216                                             RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_INITF | \\r
217                                             RTC_FLAG_RSF | RTC_FLAG_INITS | RTC_FLAG_WUTWF | \\r
218                                             RTC_FLAG_ALRBWF | RTC_FLAG_ALRAWF | RTC_FLAG_TAMP1F ))\r
219 \r
220 #define INITMODE_TIMEOUT         ((uint32_t) 0x00002000)\r
221 #define SYNCHRO_TIMEOUT          ((uint32_t) 0x00001000)\r
222 \r
223 /* Private macro -------------------------------------------------------------*/\r
224 /* Private variables ---------------------------------------------------------*/\r
225 /* Private function prototypes -----------------------------------------------*/\r
226 static uint8_t RTC_ByteToBcd2(uint8_t Value);\r
227 static uint8_t RTC_Bcd2ToByte(uint8_t Value);\r
228 \r
229 /* Private functions ---------------------------------------------------------*/\r
230 \r
231 /** @defgroup RTC_Private_Functions\r
232   * @{\r
233   */ \r
234 \r
235 /** @defgroup RTC_Group1 Initialization and Configuration functions\r
236  *  @brief   Initialization and Configuration functions \r
237  *\r
238 @verbatim   \r
239  ===============================================================================\r
240                  Initialization and Configuration functions\r
241  ===============================================================================  \r
242 \r
243   This section provide functions allowing to initialize and configure the RTC\r
244   Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers\r
245   Write protection, enter and exit the RTC initialization mode, RTC registers\r
246   synchronization check and reference clock detection enable.\r
247   \r
248   1. The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is\r
249      split into 2 programmable prescalers to minimize power consumption.\r
250      - A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.\r
251      - When both prescalers are used, it is recommended to configure the asynchronous\r
252        prescaler to a high value to minimize consumption.\r
253 \r
254   2. All RTC registers are Write protected. Writing to the RTC registers\r
255      is enabled by writing a key into the Write Protection register, RTC_WPR.\r
256 \r
257   3. To Configure the RTC Calendar, user application should enter initialization\r
258      mode. In this mode, the calendar counter is stopped and its value can be \r
259      updated. When the initialization sequence is complete, the calendar restarts \r
260      counting after 4 RTCCLK cycles.\r
261 \r
262   4. To read the calendar through the shadow registers after Calendar initialization,\r
263      calendar update or after wakeup from low power modes the software must first \r
264      clear the RSF flag. The software must then wait until it is set again before \r
265      reading the calendar, which means that the calendar registers have been \r
266      correctly copied into the RTC_TR and RTC_DR shadow registers.\r
267      The RTC_WaitForSynchro() function implements the above software sequence \r
268      (RSF clear and RSF check).\r
269 \r
270 @endverbatim\r
271   * @{\r
272   */\r
273 \r
274 /**\r
275   * @brief  Deinitializes the RTC registers to their default reset values.\r
276   * @note   This function doesn't reset the RTC Clock source and RTC Backup Data\r
277   *         registers.       \r
278   * @param  None\r
279   * @retval An ErrorStatus enumeration value:\r
280   *          - SUCCESS: RTC registers are deinitialized\r
281   *          - ERROR: RTC registers are not deinitialized\r
282   */\r
283 ErrorStatus RTC_DeInit(void)\r
284 {\r
285   __IO uint32_t wutcounter = 0x00;\r
286   uint32_t wutwfstatus = 0x00;\r
287   ErrorStatus status = ERROR;\r
288   \r
289   /* Disable the write protection for RTC registers */\r
290   RTC->WPR = 0xCA;\r
291   RTC->WPR = 0x53;\r
292 \r
293   /* Set Initialization mode */\r
294   if (RTC_EnterInitMode() == ERROR)\r
295   {\r
296     status = ERROR;\r
297   }  \r
298   else\r
299   {\r
300     /* Reset TR, DR and CR registers */\r
301     RTC->TR = (uint32_t)0x00000000;\r
302     RTC->DR = (uint32_t)0x00002101;\r
303     /* Reset All CR bits except CR[2:0] */\r
304     RTC->CR &= (uint32_t)0x00000007;\r
305   \r
306     /* Wait till RTC WUTWF flag is set and if Time out is reached exit */\r
307     do\r
308     {\r
309       wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;\r
310       wutcounter++;  \r
311     } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));\r
312     \r
313     if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)\r
314     {\r
315       status = ERROR;\r
316     }\r
317     else\r
318     {\r
319       /* Reset all RTC CR register bits */\r
320       RTC->CR &= (uint32_t)0x00000000;\r
321       RTC->WUTR = (uint32_t)0x0000FFFF;\r
322       RTC->PRER = (uint32_t)0x007F00FF;\r
323       RTC->CALIBR = (uint32_t)0x00000000;\r
324       RTC->ALRMAR = (uint32_t)0x00000000;        \r
325       RTC->ALRMBR = (uint32_t)0x00000000;\r
326       \r
327       /* Reset ISR register and exit initialization mode */\r
328       RTC->ISR = (uint32_t)0x00000000;\r
329       \r
330       /* Reset Tamper and alternate functions configuration register */\r
331       RTC->TAFCR = 0x00000000;\r
332   \r
333       if(RTC_WaitForSynchro() == ERROR)\r
334       {\r
335         status = ERROR;\r
336       }\r
337       else\r
338       {\r
339         status = SUCCESS;      \r
340       }\r
341     }\r
342   }\r
343   \r
344   /* Enable the write protection for RTC registers */\r
345   RTC->WPR = 0xFF;  \r
346   \r
347   return status;\r
348 }\r
349 \r
350 /**\r
351   * @brief  Initializes the RTC registers according to the specified parameters \r
352   *         in RTC_InitStruct.\r
353   * @param  RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains \r
354   *         the configuration information for the RTC peripheral.\r
355   * @note   The RTC Prescaler register is write protected and can be written in \r
356   *         initialization mode only.  \r
357   * @retval An ErrorStatus enumeration value:\r
358   *          - SUCCESS: RTC registers are initialized\r
359   *          - ERROR: RTC registers are not initialized  \r
360   */\r
361 ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)\r
362 {\r
363   ErrorStatus status = ERROR;\r
364   \r
365   /* Check the parameters */\r
366   assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));\r
367   assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));\r
368   assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));\r
369 \r
370   /* Disable the write protection for RTC registers */\r
371   RTC->WPR = 0xCA;\r
372   RTC->WPR = 0x53;\r
373 \r
374   /* Set Initialization mode */\r
375   if (RTC_EnterInitMode() == ERROR)\r
376   {\r
377     status = ERROR;\r
378   } \r
379   else\r
380   {\r
381     /* Clear RTC CR FMT Bit */\r
382     RTC->CR &= ((uint32_t)~(RTC_CR_FMT));\r
383     /* Set RTC_CR register */\r
384     RTC->CR |=  ((uint32_t)(RTC_InitStruct->RTC_HourFormat));\r
385   \r
386     /* Configure the RTC PRER */\r
387     RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);\r
388     RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);\r
389 \r
390     /* Exit Initialization mode */\r
391     RTC_ExitInitMode();\r
392 \r
393     status = SUCCESS;    \r
394   }\r
395   /* Enable the write protection for RTC registers */\r
396   RTC->WPR = 0xFF; \r
397   \r
398   return status;\r
399 }\r
400 \r
401 /**\r
402   * @brief  Fills each RTC_InitStruct member with its default value.\r
403   * @param  RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be \r
404   *         initialized.\r
405   * @retval None\r
406   */\r
407 void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)\r
408 {\r
409   /* Initialize the RTC_HourFormat member */\r
410   RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;\r
411     \r
412   /* Initialize the RTC_AsynchPrediv member */\r
413   RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;\r
414 \r
415   /* Initialize the RTC_SynchPrediv member */\r
416   RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF; \r
417 }\r
418 \r
419 /**\r
420   * @brief  Enables or disables the RTC registers write protection.\r
421   * @note   All the RTC registers are write protected except for RTC_ISR[13:8], \r
422   *         RTC_TAFCR and RTC_BKPxR.\r
423   * @note   Writing a wrong key reactivates the write protection.\r
424   * @note   The protection mechanism is not affected by system reset.  \r
425   * @param  NewState: new state of the write protection.\r
426   *   This parameter can be: ENABLE or DISABLE.\r
427   * @retval None\r
428   */\r
429 void RTC_WriteProtectionCmd(FunctionalState NewState)\r
430 {\r
431   /* Check the parameters */\r
432   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
433     \r
434   if (NewState != DISABLE)\r
435   {\r
436     /* Enable the write protection for RTC registers */\r
437     RTC->WPR = 0xFF;   \r
438   }\r
439   else\r
440   {\r
441     /* Disable the write protection for RTC registers */\r
442     RTC->WPR = 0xCA;\r
443     RTC->WPR = 0x53;    \r
444   }\r
445 }\r
446 \r
447 /**\r
448   * @brief  Enters the RTC Initialization mode.\r
449   * @note   The RTC Initialization mode is write protected, use the \r
450   *         RTC_WriteProtectionCmd(DISABLE) before calling this function.    \r
451   * @param  None\r
452   * @retval An ErrorStatus enumeration value:\r
453   *          - SUCCESS: RTC is in Init mode\r
454   *          - ERROR: RTC is not in Init mode  \r
455   */\r
456 ErrorStatus RTC_EnterInitMode(void)\r
457 {\r
458   __IO uint32_t initcounter = 0x00;\r
459   ErrorStatus status = ERROR;\r
460   uint32_t initstatus = 0x00;\r
461      \r
462   /* Check if the Initialization mode is set */\r
463   if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)\r
464   {\r
465     /* Set the Initialization mode */\r
466     RTC->ISR = (uint32_t)RTC_INIT_MASK;\r
467     \r
468     /* Wait till RTC is in INIT state and if Time out is reached exit */\r
469     do\r
470     {\r
471       initstatus = RTC->ISR & RTC_ISR_INITF;\r
472       initcounter++;  \r
473     } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));\r
474     \r
475     if ((RTC->ISR & RTC_ISR_INITF) != RESET)\r
476     {\r
477       status = SUCCESS;\r
478     }\r
479     else\r
480     {\r
481       status = ERROR;\r
482     }        \r
483   }\r
484   else\r
485   {\r
486     status = SUCCESS;  \r
487   } \r
488     \r
489   return (status);  \r
490 }\r
491 \r
492 /**\r
493   * @brief  Exits the RTC Initialization mode.\r
494   * @note   When the initialization sequence is complete, the calendar restarts \r
495   *         counting after 4 RTCCLK cycles.  \r
496   * @note   The RTC Initialization mode is write protected, use the \r
497   *         RTC_WriteProtectionCmd(DISABLE) before calling this function.      \r
498   * @param  None\r
499   * @retval None\r
500   */\r
501 void RTC_ExitInitMode(void)\r
502\r
503   /* Exit Initialization mode */\r
504   RTC->ISR &= (uint32_t)~RTC_ISR_INIT;  \r
505 }\r
506 \r
507 /**\r
508   * @brief  Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are \r
509   *         synchronized with RTC APB clock.\r
510   * @note   The RTC Resynchronization mode is write protected, use the \r
511   *         RTC_WriteProtectionCmd(DISABLE) before calling this function. \r
512   * @note   To read the calendar through the shadow registers after Calendar \r
513   *         initialization, calendar update or after wakeup from low power modes \r
514   *         the software must first clear the RSF flag. \r
515   *         The software must then wait until it is set again before reading \r
516   *         the calendar, which means that the calendar registers have been \r
517   *         correctly copied into the RTC_TR and RTC_DR shadow registers.   \r
518   * @param  None\r
519   * @retval An ErrorStatus enumeration value:\r
520   *          - SUCCESS: RTC registers are synchronised\r
521   *          - ERROR: RTC registers are not synchronised\r
522   */\r
523 ErrorStatus RTC_WaitForSynchro(void)\r
524 {\r
525   __IO uint32_t synchrocounter = 0;\r
526   ErrorStatus status = ERROR;\r
527   uint32_t synchrostatus = 0x00;\r
528 \r
529   /* Disable the write protection for RTC registers */\r
530   RTC->WPR = 0xCA;\r
531   RTC->WPR = 0x53;\r
532     \r
533   /* Clear RSF flag */\r
534   RTC->ISR &= (uint32_t)RTC_RSF_MASK;\r
535     \r
536   /* Wait the registers to be synchronised */\r
537   do\r
538   {\r
539     synchrostatus = RTC->ISR & RTC_ISR_RSF;\r
540     synchrocounter++;  \r
541   } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));\r
542     \r
543   if ((RTC->ISR & RTC_ISR_RSF) != RESET)\r
544   {\r
545     status = SUCCESS;\r
546   }\r
547   else\r
548   {\r
549     status = ERROR;\r
550   }        \r
551 \r
552   /* Enable the write protection for RTC registers */\r
553   RTC->WPR = 0xFF; \r
554     \r
555   return (status); \r
556 }\r
557 \r
558 /**\r
559   * @brief  Enables or disables the RTC reference clock detection.\r
560   * @param  NewState: new state of the RTC reference clock.\r
561   *   This parameter can be: ENABLE or DISABLE.\r
562   * @retval An ErrorStatus enumeration value:\r
563   *          - SUCCESS: RTC reference clock detection is enabled\r
564   *          - ERROR: RTC reference clock detection is disabled  \r
565   */\r
566 ErrorStatus RTC_RefClockCmd(FunctionalState NewState)\r
567\r
568   ErrorStatus status = ERROR;\r
569   \r
570   /* Check the parameters */\r
571   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
572   \r
573   /* Disable the write protection for RTC registers */\r
574   RTC->WPR = 0xCA;\r
575   RTC->WPR = 0x53;\r
576     \r
577   /* Set Initialization mode */\r
578   if (RTC_EnterInitMode() == ERROR)\r
579   {\r
580     status = ERROR;\r
581   } \r
582   else\r
583   {  \r
584     if (NewState != DISABLE)\r
585     {\r
586       /* Enable the RTC reference clock detection */\r
587       RTC->CR |= RTC_CR_REFCKON;   \r
588     }\r
589     else\r
590     {\r
591       /* Disable the RTC reference clock detection */\r
592       RTC->CR &= ~RTC_CR_REFCKON;    \r
593     }\r
594     /* Exit Initialization mode */\r
595     RTC_ExitInitMode();\r
596     \r
597     status = SUCCESS;\r
598   }\r
599   \r
600   /* Enable the write protection for RTC registers */\r
601   RTC->WPR = 0xFF;  \r
602   \r
603   return status; \r
604 }\r
605 \r
606 /**\r
607   * @}\r
608   */\r
609 \r
610 /** @defgroup RTC_Group2 Time and Date configuration functions\r
611  *  @brief   Time and Date configuration functions \r
612  *\r
613 @verbatim   \r
614  ===============================================================================\r
615                    Time and Date configuration functions\r
616  ===============================================================================  \r
617 \r
618   This section provide functions allowing to program and read the RTC Calendar\r
619   (Time and Date).\r
620 \r
621 @endverbatim\r
622   * @{\r
623   */\r
624 \r
625 /**\r
626   * @brief  Set the RTC current time.\r
627   * @param  RTC_Format: specifies the format of the entered parameters.\r
628   *   This parameter can be  one of the following values:\r
629   *     @arg RTC_Format_BIN:  Binary data format \r
630   *     @arg RTC_Format_BCD:  BCD data format\r
631   * @param  RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that contains \r
632   *                        the time configuration information for the RTC.     \r
633   * @retval An ErrorStatus enumeration value:\r
634   *          - SUCCESS: RTC Time register is configured\r
635   *          - ERROR: RTC Time register is not configured\r
636   */\r
637 ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)\r
638 {\r
639   uint32_t tmpreg = 0;\r
640   ErrorStatus status = ERROR;\r
641     \r
642   /* Check the parameters */\r
643   assert_param(IS_RTC_FORMAT(RTC_Format));\r
644   \r
645   if (RTC_Format == RTC_Format_BIN)\r
646   {\r
647     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)\r
648     {\r
649       assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));\r
650       assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));\r
651     } \r
652     else\r
653     {\r
654       RTC_TimeStruct->RTC_H12 = 0x00;\r
655       assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));\r
656     }\r
657     assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));\r
658     assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));\r
659   }\r
660   else\r
661   {\r
662     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)\r
663     {\r
664       tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);\r
665       assert_param(IS_RTC_HOUR12(tmpreg));\r
666       assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12)); \r
667     } \r
668     else\r
669     {\r
670       RTC_TimeStruct->RTC_H12 = 0x00;\r
671       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));\r
672     }\r
673     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));\r
674     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));\r
675   }\r
676   \r
677   /* Check the input parameters format */\r
678   if (RTC_Format != RTC_Format_BIN)\r
679   {\r
680     tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \\r
681              ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \\r
682              ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \\r
683              ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16)); \r
684   }  \r
685   else\r
686   {\r
687     tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \\r
688                    ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \\r
689                    ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \\r
690                    (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));\r
691   }  \r
692 \r
693   /* Disable the write protection for RTC registers */\r
694   RTC->WPR = 0xCA;\r
695   RTC->WPR = 0x53;\r
696 \r
697   /* Set Initialization mode */\r
698   if (RTC_EnterInitMode() == ERROR)\r
699   {\r
700     status = ERROR;\r
701   } \r
702   else\r
703   {\r
704     /* Set the RTC_TR register */\r
705     RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);\r
706 \r
707     /* Exit Initialization mode */\r
708     RTC_ExitInitMode(); \r
709 \r
710     if(RTC_WaitForSynchro() == ERROR)\r
711     {\r
712       status = ERROR;\r
713     }\r
714     else\r
715     {\r
716       status = SUCCESS;\r
717     }\r
718   \r
719   }\r
720   /* Enable the write protection for RTC registers */\r
721   RTC->WPR = 0xFF; \r
722     \r
723   return status;\r
724 }\r
725 \r
726 /**\r
727   * @brief  Fills each RTC_TimeStruct member with its default value\r
728   *         (Time = 00h:00min:00sec).\r
729   * @param  RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be \r
730   *         initialized.\r
731   * @retval None\r
732   */\r
733 void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)\r
734 {\r
735   /* Time = 00h:00min:00sec */\r
736   RTC_TimeStruct->RTC_H12 = RTC_H12_AM;\r
737   RTC_TimeStruct->RTC_Hours = 0;\r
738   RTC_TimeStruct->RTC_Minutes = 0;\r
739   RTC_TimeStruct->RTC_Seconds = 0; \r
740 }\r
741 \r
742 /**\r
743   * @brief  Get the RTC current Time.\r
744   * @param  RTC_Format: specifies the format of the returned parameters.\r
745   *   This parameter can be  one of the following values:\r
746   *     @arg RTC_Format_BIN:  Binary data format \r
747   *     @arg RTC_Format_BCD:  BCD data format\r
748   * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will \r
749   *                        contain the returned current time configuration.     \r
750   * @retval None\r
751   */\r
752 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)\r
753 {\r
754   uint32_t tmpreg = 0;\r
755 \r
756   /* Check the parameters */\r
757   assert_param(IS_RTC_FORMAT(RTC_Format));\r
758 \r
759   /* Get the RTC_TR register */\r
760   tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK); \r
761   \r
762   /* Fill the structure fields with the read parameters */\r
763   RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);\r
764   RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);\r
765   RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));\r
766   RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);  \r
767 \r
768   /* Check the input parameters format */\r
769   if (RTC_Format == RTC_Format_BIN)\r
770   {\r
771     /* Convert the structure parameters to Binary format */\r
772     RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);\r
773     RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);\r
774     RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);   \r
775   }\r
776 }\r
777 \r
778 /**\r
779   * @brief  Set the RTC current date.\r
780   * @param  RTC_Format: specifies the format of the entered parameters.\r
781   *   This parameter can be  one of the following values:\r
782   *     @arg RTC_Format_BIN:  Binary data format \r
783   *     @arg RTC_Format_BCD:  BCD data format\r
784   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains \r
785   *                         the date configuration information for the RTC.\r
786   * @retval An ErrorStatus enumeration value:\r
787   *          - SUCCESS: RTC Date register is configured\r
788   *          - ERROR: RTC Date register is not configured\r
789   */\r
790 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)\r
791 {\r
792   uint32_t tmpreg = 0;\r
793   ErrorStatus status = ERROR;\r
794   \r
795   /* Check the parameters */\r
796   assert_param(IS_RTC_FORMAT(RTC_Format));\r
797 \r
798   if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))\r
799   {\r
800     RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;\r
801   }  \r
802   if (RTC_Format == RTC_Format_BIN)\r
803   {\r
804     assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));\r
805     assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));\r
806     assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));\r
807   }\r
808   else\r
809   {\r
810     assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));\r
811     tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);\r
812     assert_param(IS_RTC_MONTH(tmpreg));\r
813     tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);\r
814     assert_param(IS_RTC_DATE(tmpreg));\r
815   }\r
816   assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));\r
817 \r
818   /* Check the input parameters format */\r
819   if (RTC_Format != RTC_Format_BIN)\r
820   {\r
821     tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \\r
822               (((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \\r
823               ((uint32_t)RTC_DateStruct->RTC_Date) | \\r
824               (((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13)); \r
825   }  \r
826   else\r
827   {\r
828     tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \\r
829               ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \\r
830               ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \\r
831               ((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));\r
832   }\r
833 \r
834   /* Disable the write protection for RTC registers */\r
835   RTC->WPR = 0xCA;\r
836   RTC->WPR = 0x53;\r
837 \r
838   /* Set Initialization mode */\r
839   if (RTC_EnterInitMode() == ERROR)\r
840   {\r
841     status = ERROR;\r
842   } \r
843   else\r
844   {\r
845     /* Set the RTC_DR register */\r
846     RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);\r
847 \r
848     /* Exit Initialization mode */\r
849     RTC_ExitInitMode(); \r
850 \r
851     if(RTC_WaitForSynchro() == ERROR)\r
852     {\r
853       status = ERROR;\r
854     }\r
855     else\r
856     {\r
857       status = SUCCESS;\r
858     }\r
859   }\r
860   /* Enable the write protection for RTC registers */\r
861   RTC->WPR = 0xFF;   \r
862   \r
863   return status;\r
864 }\r
865 \r
866 /**\r
867   * @brief  Fills each RTC_DateStruct member with its default value\r
868   *         (Monday, January 01 xx00).\r
869   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure which will be \r
870   *         initialized.\r
871   * @retval None\r
872   */\r
873 void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)\r
874 {\r
875   /* Monday, January 01 xx00 */\r
876   RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;\r
877   RTC_DateStruct->RTC_Date = 1;\r
878   RTC_DateStruct->RTC_Month = RTC_Month_January;\r
879   RTC_DateStruct->RTC_Year = 0;\r
880 }\r
881 \r
882 /**\r
883   * @brief  Get the RTC current date. \r
884   * @param  RTC_Format: specifies the format of the returned parameters.\r
885   *   This parameter can be one of the following values:\r
886   *     @arg RTC_Format_BIN: Binary data format \r
887   *     @arg RTC_Format_BCD: BCD data format\r
888   * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that will \r
889   *                        contain the returned current date configuration.     \r
890   * @retval None\r
891   */\r
892 void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)\r
893 {\r
894   uint32_t tmpreg = 0;\r
895 \r
896   /* Check the parameters */\r
897   assert_param(IS_RTC_FORMAT(RTC_Format));\r
898   \r
899   /* Get the RTC_TR register */\r
900   tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK); \r
901 \r
902   /* Fill the structure fields with the read parameters */\r
903   RTC_DateStruct->RTC_Year = (uint8_t)((tmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);\r
904   RTC_DateStruct->RTC_Month = (uint8_t)((tmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);\r
905   RTC_DateStruct->RTC_Date = (uint8_t)(tmpreg & (RTC_DR_DT | RTC_DR_DU));\r
906   RTC_DateStruct->RTC_WeekDay = (uint8_t)((tmpreg & (RTC_DR_WDU)) >> 13);  \r
907 \r
908   /* Check the input parameters format */\r
909   if (RTC_Format == RTC_Format_BIN)\r
910   {\r
911     /* Convert the structure parameters to Binary format */\r
912     RTC_DateStruct->RTC_Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year);\r
913     RTC_DateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);\r
914     RTC_DateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);\r
915     RTC_DateStruct->RTC_WeekDay = (uint8_t)(RTC_DateStruct->RTC_WeekDay);   \r
916   }\r
917 }\r
918 \r
919 /**\r
920   * @}\r
921   */\r
922 \r
923 /** @defgroup RTC_Group3 Alarms configuration functions\r
924  *  @brief   Alarms (Alarm A and Alarm B) configuration functions \r
925  *\r
926 @verbatim   \r
927  ===============================================================================\r
928               Alarms (Alarm A and Alarm B) configuration functions\r
929  ===============================================================================  \r
930 \r
931   This section provide functions allowing to program and read the RTC Alarms.\r
932 \r
933 @endverbatim\r
934   * @{\r
935   */\r
936 \r
937 /**\r
938   * @brief  Set the specified RTC Alarm.\r
939   * @note   The Alarm register can only be written when the corresponding Alarm\r
940   *         is disabled (Use the RTC_AlarmCmd(DISABLE)).    \r
941   * @param  RTC_Format: specifies the format of the returned parameters.\r
942   *   This parameter can be one of the following values:\r
943   *     @arg RTC_Format_BIN: Binary data format \r
944   *     @arg RTC_Format_BCD: BCD data format\r
945   * @param  RTC_Alarm: specifies the alarm to be configured.\r
946   *   This parameter can be one of the following values:\r
947   *     @arg RTC_Alarm_A: to select Alarm A\r
948   *     @arg RTC_Alarm_B: to select Alarm B  \r
949   * @param  RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that \r
950   *                          contains the alarm configuration parameters.     \r
951   * @retval None\r
952   */\r
953 void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)\r
954 {\r
955   uint32_t tmpreg = 0;\r
956   \r
957   /* Check the parameters */\r
958   assert_param(IS_RTC_FORMAT(RTC_Format));\r
959   assert_param(IS_RTC_ALARM(RTC_Alarm));\r
960   assert_param(IS_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));\r
961   assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));\r
962 \r
963   if (RTC_Format == RTC_Format_BIN)\r
964   {\r
965     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)\r
966     {\r
967       assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));\r
968       assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));\r
969     } \r
970     else\r
971     {\r
972       RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;\r
973       assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));\r
974     }\r
975     assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));\r
976     assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));\r
977     \r
978     if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)\r
979     {\r
980       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));\r
981     }\r
982     else\r
983     {\r
984       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));\r
985     }\r
986   }\r
987   else\r
988   {\r
989     if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)\r
990     {\r
991       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);\r
992       assert_param(IS_RTC_HOUR12(tmpreg));\r
993       assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));\r
994     } \r
995     else\r
996     {\r
997       RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;\r
998       assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));\r
999     }\r
1000     \r
1001     assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));\r
1002     assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));\r
1003     \r
1004     if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)\r
1005     {\r
1006       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);\r
1007       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));    \r
1008     }\r
1009     else\r
1010     {\r
1011       tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);\r
1012       assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));      \r
1013     }    \r
1014   }\r
1015 \r
1016   /* Check the input parameters format */\r
1017   if (RTC_Format != RTC_Format_BIN)\r
1018   {\r
1019     tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \\r
1020               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \\r
1021               ((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \\r
1022               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \\r
1023               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \\r
1024               ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \\r
1025               ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask)); \r
1026   }  \r
1027   else\r
1028   {\r
1029     tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \\r
1030               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \\r
1031               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \\r
1032               ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \\r
1033               ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \\r
1034               ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \\r
1035               ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask)); \r
1036   } \r
1037 \r
1038   /* Disable the write protection for RTC registers */\r
1039   RTC->WPR = 0xCA;\r
1040   RTC->WPR = 0x53;\r
1041 \r
1042   /* Configure the Alarm register */\r
1043   if (RTC_Alarm == RTC_Alarm_A)\r
1044   {\r
1045     RTC->ALRMAR = (uint32_t)tmpreg;\r
1046   }\r
1047   else\r
1048   {\r
1049     RTC->ALRMBR = (uint32_t)tmpreg;\r
1050   }\r
1051 \r
1052   /* Enable the write protection for RTC registers */\r
1053   RTC->WPR = 0xFF;   \r
1054 }\r
1055 \r
1056 /**\r
1057   * @brief  Fills each RTC_AlarmStruct member with its default value\r
1058   *         (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =\r
1059   *         all fields are masked).\r
1060   * @param  RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which\r
1061   *         will be initialized.\r
1062   * @retval None\r
1063   */\r
1064 void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)\r
1065 {\r
1066   /* Alarm Time Settings : Time = 00h:00mn:00sec */\r
1067   RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;\r
1068   RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;\r
1069   RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;\r
1070   RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;\r
1071 \r
1072   /* Alarm Date Settings : Date = 1st day of the month */\r
1073   RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;\r
1074   RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;\r
1075 \r
1076   /* Alarm Masks Settings : Mask =  all fields are not masked */\r
1077   RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;\r
1078 }\r
1079 \r
1080 /**\r
1081   * @brief  Get the RTC Alarm value and masks.\r
1082   * @param  RTC_Format: specifies the format of the output parameters.\r
1083   *   This parameter can be one of the following values:\r
1084   *     @arg RTC_Format_BIN: Binary data format \r
1085   *     @arg RTC_Format_BCD: BCD data format\r
1086   * @param  RTC_Alarm: specifies the alarm to be read.\r
1087   *   This parameter can be one of the following values:\r
1088   *     @arg RTC_Alarm_A: to select Alarm A\r
1089   *     @arg RTC_Alarm_B: to select Alarm B  \r
1090   * @param  RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will \r
1091   *                          contains the output alarm configuration values.     \r
1092   * @retval None\r
1093   */\r
1094 void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)\r
1095 {\r
1096   uint32_t tmpreg = 0;\r
1097 \r
1098   /* Check the parameters */\r
1099   assert_param(IS_RTC_FORMAT(RTC_Format));\r
1100   assert_param(IS_RTC_ALARM(RTC_Alarm)); \r
1101 \r
1102   /* Get the RTC_ALRMxR register */\r
1103   if (RTC_Alarm == RTC_Alarm_A)\r
1104   {\r
1105     tmpreg = (uint32_t)(RTC->ALRMAR);\r
1106   }\r
1107   else\r
1108   {\r
1109     tmpreg = (uint32_t)(RTC->ALRMBR);\r
1110   }\r
1111 \r
1112   /* Fill the structure with the read parameters */\r
1113   RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \\r
1114                                                      RTC_ALRMAR_HU)) >> 16);\r
1115   RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \\r
1116                                                      RTC_ALRMAR_MNU)) >> 8);\r
1117   RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \\r
1118                                                      RTC_ALRMAR_SU));\r
1119   RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);\r
1120   RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);\r
1121   RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);\r
1122   RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);\r
1123 \r
1124   if (RTC_Format == RTC_Format_BIN)\r
1125   {\r
1126     RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \\r
1127                                                         RTC_AlarmTime.RTC_Hours);\r
1128     RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \\r
1129                                                         RTC_AlarmTime.RTC_Minutes);\r
1130     RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \\r
1131                                                         RTC_AlarmTime.RTC_Seconds);\r
1132     RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);\r
1133   }  \r
1134 }\r
1135 \r
1136 /**\r
1137   * @brief  Enables or disables the specified RTC Alarm.\r
1138   * @param  RTC_Alarm: specifies the alarm to be configured.\r
1139   *   This parameter can be any combination of the following values:\r
1140   *     @arg RTC_Alarm_A: to select Alarm A\r
1141   *     @arg RTC_Alarm_B: to select Alarm B  \r
1142   * @param  NewState: new state of the specified alarm.\r
1143   *   This parameter can be: ENABLE or DISABLE.\r
1144   * @retval An ErrorStatus enumeration value:\r
1145   *          - SUCCESS: RTC Alarm is enabled/disabled\r
1146   *          - ERROR: RTC Alarm is not enabled/disabled  \r
1147   */\r
1148 ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)\r
1149 {\r
1150   __IO uint32_t alarmcounter = 0x00;\r
1151   uint32_t alarmstatus = 0x00;\r
1152   ErrorStatus status = ERROR;\r
1153     \r
1154   /* Check the parameters */\r
1155   assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));\r
1156   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1157 \r
1158   /* Disable the write protection for RTC registers */\r
1159   RTC->WPR = 0xCA;\r
1160   RTC->WPR = 0x53;\r
1161 \r
1162   /* Configure the Alarm state */\r
1163   if (NewState != DISABLE)\r
1164   {\r
1165     RTC->CR |= (uint32_t)RTC_Alarm;\r
1166 \r
1167     status = SUCCESS;    \r
1168   }\r
1169   else\r
1170   { \r
1171     /* Disable the Alarm in RTC_CR register */\r
1172     RTC->CR &= (uint32_t)~RTC_Alarm;\r
1173    \r
1174     /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */\r
1175     do\r
1176     {\r
1177       alarmstatus = RTC->ISR & (RTC_Alarm >> 8);\r
1178       alarmcounter++;  \r
1179     } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));\r
1180     \r
1181     if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)\r
1182     {\r
1183       status = ERROR;\r
1184     } \r
1185     else\r
1186     {\r
1187       status = SUCCESS;\r
1188     }        \r
1189   } \r
1190 \r
1191   /* Enable the write protection for RTC registers */\r
1192   RTC->WPR = 0xFF; \r
1193   \r
1194   return status;\r
1195 }\r
1196 \r
1197 /**\r
1198   * @}\r
1199   */\r
1200 \r
1201 /** @defgroup RTC_Group4 WakeUp Timer configuration functions\r
1202  *  @brief   WakeUp Timer configuration functions \r
1203  *\r
1204 @verbatim   \r
1205  ===============================================================================\r
1206                      WakeUp Timer configuration functions\r
1207  ===============================================================================  \r
1208 \r
1209   This section provide functions allowing to program and read the RTC WakeUp.\r
1210 \r
1211 @endverbatim\r
1212   * @{\r
1213   */\r
1214 \r
1215 /**\r
1216   * @brief  Configures the RTC Wakeup clock source.\r
1217   * @note   The WakeUp Clock source can only be changed when the RTC WakeUp\r
1218   *         is disabled (Use the RTC_WakeUpCmd(DISABLE)).      \r
1219   * @param  RTC_WakeUpClock: Wakeup Clock source.\r
1220   *   This parameter can be one of the following values:\r
1221   *     @arg RTC_WakeUpClock_RTCCLK_Div16\r
1222   *     @arg RTC_WakeUpClock_RTCCLK_Div8\r
1223   *     @arg RTC_WakeUpClock_RTCCLK_Div4\r
1224   *     @arg RTC_WakeUpClock_RTCCLK_Div2\r
1225   *     @arg RTC_WakeUpClock_CK_SPRE_16bits\r
1226   *     @arg RTC_WakeUpClock_CK_SPRE_17bits\r
1227   * @retval None\r
1228   */\r
1229 void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)\r
1230 {\r
1231   /* Check the parameters */\r
1232   assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));\r
1233 \r
1234   /* Disable the write protection for RTC registers */\r
1235   RTC->WPR = 0xCA;\r
1236   RTC->WPR = 0x53;\r
1237 \r
1238   /* Clear the Wakeup Timer clock source bits in CR register */\r
1239   RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;\r
1240 \r
1241   /* Configure the clock source */\r
1242   RTC->CR |= (uint32_t)RTC_WakeUpClock;\r
1243   \r
1244   /* Enable the write protection for RTC registers */\r
1245   RTC->WPR = 0xFF; \r
1246 }\r
1247 \r
1248 /**\r
1249   * @brief  Configures the RTC Wakeup counter.\r
1250   * @note   The RTC WakeUp counter can only be written when the RTC WakeUp\r
1251   *         is disabled (Use the RTC_WakeUpCmd(DISABLE)).        \r
1252   * @param  RTC_WakeUpCounter: specifies the WakeUp counter.\r
1253   *   This parameter can be a value from 0x0000 to 0xFFFF. \r
1254   * @retval None\r
1255   */\r
1256 void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)\r
1257 {\r
1258   /* Check the parameters */\r
1259   assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));\r
1260   \r
1261   /* Disable the write protection for RTC registers */\r
1262   RTC->WPR = 0xCA;\r
1263   RTC->WPR = 0x53;\r
1264   \r
1265   /* Configure the Wakeup Timer counter */\r
1266   RTC->WUTR = (uint32_t)RTC_WakeUpCounter;\r
1267   \r
1268   /* Enable the write protection for RTC registers */\r
1269   RTC->WPR = 0xFF; \r
1270 }\r
1271 \r
1272 /**\r
1273   * @brief  Returns the RTC WakeUp timer counter value.\r
1274   * @param  None\r
1275   * @retval The RTC WakeUp Counter value.\r
1276   */\r
1277 uint32_t RTC_GetWakeUpCounter(void)\r
1278 {\r
1279   /* Get the counter value */\r
1280   return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));\r
1281 }\r
1282 \r
1283 /**\r
1284   * @brief  Enables or Disables the RTC WakeUp timer.\r
1285   * @param  NewState: new state of the WakeUp timer.\r
1286   *   This parameter can be: ENABLE or DISABLE.\r
1287   * @retval None\r
1288   */\r
1289 ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)\r
1290 {\r
1291   __IO uint32_t wutcounter = 0x00;\r
1292   uint32_t wutwfstatus = 0x00;\r
1293   ErrorStatus status = ERROR;\r
1294   \r
1295   /* Check the parameters */\r
1296   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1297 \r
1298   /* Disable the write protection for RTC registers */\r
1299   RTC->WPR = 0xCA;\r
1300   RTC->WPR = 0x53;\r
1301 \r
1302   if (NewState != DISABLE)\r
1303   {\r
1304     /* Enable the Wakeup Timer */\r
1305     RTC->CR |= (uint32_t)RTC_CR_WUTE;\r
1306     status = SUCCESS;    \r
1307   }\r
1308   else\r
1309   {\r
1310     /* Disable the Wakeup Timer */\r
1311     RTC->CR &= (uint32_t)~RTC_CR_WUTE;\r
1312     /* Wait till RTC WUTWF flag is set and if Time out is reached exit */\r
1313     do\r
1314     {\r
1315       wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;\r
1316       wutcounter++;  \r
1317     } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));\r
1318     \r
1319     if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)\r
1320     {\r
1321       status = ERROR;\r
1322     }\r
1323     else\r
1324     {\r
1325       status = SUCCESS;\r
1326     }    \r
1327   }\r
1328 \r
1329   /* Enable the write protection for RTC registers */\r
1330   RTC->WPR = 0xFF; \r
1331   \r
1332   return status;\r
1333 }\r
1334 \r
1335 /**\r
1336   * @}\r
1337   */\r
1338 \r
1339 /** @defgroup RTC_Group5 Daylight Saving configuration functions\r
1340  *  @brief   Daylight Saving configuration functions \r
1341  *\r
1342 @verbatim   \r
1343  ===============================================================================\r
1344                     Daylight Saving configuration functions\r
1345  ===============================================================================  \r
1346 \r
1347   This section provide functions allowing to configure the RTC DayLight Saving.\r
1348 \r
1349 @endverbatim\r
1350   * @{\r
1351   */\r
1352 \r
1353 /**\r
1354   * @brief  Adds or substract one hour from the current time.\r
1355   * @param  RTC_DayLightSaveOperation: the value of hour adjustment. \r
1356   *   This parameter can be one of the following values:\r
1357   *     @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)\r
1358   *     @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)\r
1359   * @param  RTC_StoreOperation: Specifies the value to be written in the BCK bit \r
1360   *                            in CR register to store the operation.\r
1361   *   This parameter can be one of the following values:\r
1362   *     @arg RTC_StoreOperation_Reset\r
1363   *     @arg RTC_StoreOperation_Set\r
1364   * @retval None\r
1365   */\r
1366 void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)\r
1367 {\r
1368   /* Check the parameters */\r
1369   assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));\r
1370   assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));\r
1371 \r
1372   /* Disable the write protection for RTC registers */\r
1373   RTC->WPR = 0xCA;\r
1374   RTC->WPR = 0x53;\r
1375 \r
1376   /* Clear the bits to be configured */\r
1377   RTC->CR &= (uint32_t)~(RTC_CR_BCK);\r
1378 \r
1379   /* Configure the RTC_CR register */\r
1380   RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);\r
1381 \r
1382   /* Enable the write protection for RTC registers */\r
1383   RTC->WPR = 0xFF; \r
1384 }\r
1385 \r
1386 /**\r
1387   * @brief  Returns the RTC Day Light Saving stored operation.\r
1388   * @param  None\r
1389   * @retval RTC Day Light Saving stored operation.\r
1390   *          - RTC_StoreOperation_Reset\r
1391   *          - RTC_StoreOperation_Set       \r
1392   */\r
1393 uint32_t RTC_GetStoreOperation(void)\r
1394 {\r
1395   return (RTC->CR & RTC_CR_BCK);\r
1396 }\r
1397 \r
1398 /**\r
1399   * @}\r
1400   */\r
1401 \r
1402 /** @defgroup RTC_Group6 Output pin Configuration function\r
1403  *  @brief   Output pin Configuration function \r
1404  *\r
1405 @verbatim   \r
1406  ===============================================================================\r
1407                          Output pin Configuration function\r
1408  ===============================================================================  \r
1409 \r
1410   This section provide functions allowing to configure the RTC Output source.\r
1411 \r
1412 @endverbatim\r
1413   * @{\r
1414   */\r
1415 \r
1416 /**\r
1417   * @brief  Configures the RTC output source (AFO_ALARM).\r
1418   * @param  RTC_Output: Specifies which signal will be routed to the RTC output. \r
1419   *   This parameter can be one of the following values:\r
1420   *     @arg RTC_Output_Disable: No output selected\r
1421   *     @arg RTC_Output_AlarmA: signal of AlarmA mapped to output\r
1422   *     @arg RTC_Output_AlarmB: signal of AlarmB mapped to output\r
1423   *     @arg RTC_Output_WakeUp: signal of WakeUp mapped to output\r
1424   * @param  RTC_OutputPolarity: Specifies the polarity of the output signal. \r
1425   *   This parameter can be one of the following:\r
1426   *     @arg RTC_OutputPolarity_High: The output pin is high when the \r
1427   *                                 ALRAF/ALRBF/WUTF is high (depending on OSEL)\r
1428   *     @arg RTC_OutputPolarity_Low: The output pin is low when the \r
1429   *                                 ALRAF/ALRBF/WUTF is high (depending on OSEL)\r
1430   * @retval None\r
1431   */\r
1432 void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)\r
1433 {\r
1434   /* Check the parameters */\r
1435   assert_param(IS_RTC_OUTPUT(RTC_Output));\r
1436   assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));\r
1437 \r
1438   /* Disable the write protection for RTC registers */\r
1439   RTC->WPR = 0xCA;\r
1440   RTC->WPR = 0x53;\r
1441 \r
1442   /* Clear the bits to be configured */\r
1443   RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);\r
1444 \r
1445   /* Configure the output selection and polarity */\r
1446   RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);\r
1447 \r
1448   /* Enable the write protection for RTC registers */\r
1449   RTC->WPR = 0xFF; \r
1450 }\r
1451 \r
1452 /**\r
1453   * @}\r
1454   */\r
1455 \r
1456 /** @defgroup RTC_Group7 Digital Calibration configuration functions\r
1457  *  @brief   Digital Calibration configuration functions \r
1458  *\r
1459 @verbatim   \r
1460  ===============================================================================\r
1461                   Digital Calibration configuration functions\r
1462  ===============================================================================  \r
1463 \r
1464 @endverbatim\r
1465   * @{\r
1466   */\r
1467 \r
1468 /**\r
1469   * @brief  Configures the digital calibration parameters.\r
1470   * @param  RTC_CalibSign: specifies the sign of the calibration value.\r
1471   *   This parameter can be  one of the following values:\r
1472   *     @arg RTC_CalibSign_Positive: The value sign is positive \r
1473   *     @arg RTC_CalibSign_Negative: The value sign is negative\r
1474   * @param  Value: value of calibration expressed in ppm (coded on 5 bits) \r
1475   *             - This value should be between 0 and 63 when using negative sign\r
1476   *               with a 2-ppm step.\r
1477   *             - This value should be between 0 and 126 when using positive sign\r
1478   *               with a 4-ppm step.\r
1479   * @retval An ErrorStatus enumeration value:\r
1480   *          - SUCCESS: RTC digital calibration are initialized\r
1481   *          - ERROR: RTC digital calibration are not initialized     \r
1482   */\r
1483 ErrorStatus RTC_DigitalCalibConfig(uint32_t RTC_CalibSign, uint32_t Value)\r
1484 {\r
1485   ErrorStatus status = ERROR;\r
1486    \r
1487   /* Check the parameters */\r
1488   assert_param(IS_RTC_CALIB_SIGN(RTC_CalibSign));\r
1489   assert_param(IS_RTC_CALIB_VALUE(Value)); \r
1490 \r
1491   /* Disable the write protection for RTC registers */\r
1492   RTC->WPR = 0xCA;\r
1493   RTC->WPR = 0x53;\r
1494 \r
1495   /* Set Initialization mode */\r
1496   if (RTC_EnterInitMode() == ERROR)\r
1497   {\r
1498     status = ERROR;\r
1499   } \r
1500   else\r
1501   {\r
1502     /* Set the calibration value */\r
1503     RTC->CALIBR = (uint32_t)(RTC_CalibSign | Value);\r
1504     /* Exit Initialization mode */\r
1505     RTC_ExitInitMode();\r
1506     \r
1507     status = SUCCESS;\r
1508   } \r
1509 \r
1510   /* Enable the write protection for RTC registers */\r
1511   RTC->WPR = 0xFF; \r
1512   \r
1513   return status;\r
1514 }\r
1515 \r
1516 /**\r
1517   * @brief  Enables or disables the digital calibration process.\r
1518   * @param  NewState: new state of the digital calibration.\r
1519   *   This parameter can be: ENABLE or DISABLE.\r
1520   * @retval An ErrorStatus enumeration value:\r
1521   *          - SUCCESS: RTC digital calibration are enabled/disabled\r
1522   *          - ERROR: RTC digital calibration are not enabled/disabled    \r
1523   */\r
1524 ErrorStatus RTC_DigitalCalibCmd(FunctionalState NewState)\r
1525 {\r
1526   ErrorStatus status = ERROR;\r
1527   \r
1528   /* Check the parameters */\r
1529   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1530 \r
1531   /* Disable the write protection for RTC registers */\r
1532   RTC->WPR = 0xCA;\r
1533   RTC->WPR = 0x53;\r
1534   \r
1535   /* Set Initialization mode */\r
1536   if (RTC_EnterInitMode() == ERROR)\r
1537   {\r
1538     status =  ERROR;\r
1539   }\r
1540   else\r
1541   {\r
1542     if (NewState != DISABLE)\r
1543     {\r
1544       /* Enable the Digital Calibration */\r
1545       RTC->CR |= (uint32_t)RTC_CR_DCE;\r
1546     }\r
1547     else\r
1548     { \r
1549       /* Disable the Digital Calibration */\r
1550       RTC->CR &= (uint32_t)~RTC_CR_DCE;\r
1551     }\r
1552     /* Exit Initialization mode */\r
1553     RTC_ExitInitMode();\r
1554     \r
1555     status = SUCCESS;\r
1556   } \r
1557   \r
1558   /* Enable the write protection for RTC registers */\r
1559   RTC->WPR = 0xFF; \r
1560   \r
1561   return status;\r
1562 }\r
1563 \r
1564 /**\r
1565   * @brief  Enables or disables the RTC clock to be output through the relative \r
1566   *         pin.\r
1567   * @param  NewState: new state of the digital calibration Output.\r
1568   *   This parameter can be: ENABLE or DISABLE.\r
1569   * @retval None\r
1570   */\r
1571 void RTC_CalibOutputCmd(FunctionalState NewState)\r
1572 {\r
1573   /* Check the parameters */\r
1574   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1575   \r
1576   /* Disable the write protection for RTC registers */\r
1577   RTC->WPR = 0xCA;\r
1578   RTC->WPR = 0x53;\r
1579   \r
1580   if (NewState != DISABLE)\r
1581   {\r
1582     /* Enable the RTC clock output */\r
1583     RTC->CR |= (uint32_t)RTC_CR_COE;\r
1584   }\r
1585   else\r
1586   { \r
1587     /* Disable the RTC clock output */\r
1588     RTC->CR &= (uint32_t)~RTC_CR_COE;\r
1589   }\r
1590   \r
1591   /* Enable the write protection for RTC registers */\r
1592   RTC->WPR = 0xFF; \r
1593 }\r
1594 \r
1595 /**\r
1596   * @}\r
1597   */\r
1598 \r
1599 \r
1600 /** @defgroup RTC_Group8 TimeStamp configuration functions\r
1601  *  @brief   TimeStamp configuration functions \r
1602  *\r
1603 @verbatim   \r
1604  ===============================================================================\r
1605                        TimeStamp configuration functions\r
1606  ===============================================================================  \r
1607 \r
1608 @endverbatim\r
1609   * @{\r
1610   */\r
1611 \r
1612 /**\r
1613   * @brief  Enables or Disables the RTC TimeStamp functionality with the \r
1614   *         specified time stamp pin stimulating edge.\r
1615   * @param  RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is \r
1616   *         activated.\r
1617   *   This parameter can be one of the following:\r
1618   *     @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising \r
1619   *                                    edge of the related pin.\r
1620   *     @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the \r
1621   *                                     falling edge of the related pin.\r
1622   * @param  NewState: new state of the TimeStamp.\r
1623   *   This parameter can be: ENABLE or DISABLE.\r
1624   * @retval None\r
1625   */\r
1626 void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)\r
1627 {\r
1628   uint32_t tmpreg = 0;\r
1629 \r
1630   /* Check the parameters */\r
1631   assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));\r
1632   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1633 \r
1634   /* Get the RTC_CR register and clear the bits to be configured */\r
1635   tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));\r
1636 \r
1637   /* Get the new configuration */\r
1638   if (NewState != DISABLE)\r
1639   {\r
1640     tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);\r
1641   }\r
1642   else\r
1643   {\r
1644     tmpreg |= (uint32_t)(RTC_TimeStampEdge);\r
1645   }\r
1646 \r
1647   /* Disable the write protection for RTC registers */\r
1648   RTC->WPR = 0xCA;\r
1649   RTC->WPR = 0x53;\r
1650 \r
1651   /* Configure the Time Stamp TSEDGE and Enable bits */\r
1652   RTC->CR = (uint32_t)tmpreg;\r
1653 \r
1654   /* Enable the write protection for RTC registers */\r
1655   RTC->WPR = 0xFF; \r
1656 }\r
1657 \r
1658 /**\r
1659   * @brief  Get the RTC TimeStamp value and masks.\r
1660   * @param  RTC_Format: specifies the format of the output parameters.\r
1661   *   This parameter can be one of the following values:\r
1662   *     @arg RTC_Format_BIN: Binary data format \r
1663   *     @arg RTC_Format_BCD: BCD data format\r
1664   * @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will \r
1665   *                             contains the TimeStamp time values. \r
1666   * @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will \r
1667   *                             contains the TimeStamp date values.     \r
1668   * @retval None\r
1669   */\r
1670 void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct, \r
1671                                       RTC_DateTypeDef* RTC_StampDateStruct)\r
1672 {\r
1673   uint32_t tmptime = 0, tmpdate = 0;\r
1674 \r
1675   /* Check the parameters */\r
1676   assert_param(IS_RTC_FORMAT(RTC_Format));\r
1677 \r
1678   /* Get the TimeStamp time and date registers values */\r
1679   tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);\r
1680   tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);\r
1681 \r
1682   /* Fill the Time structure fields with the read parameters */\r
1683   RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);\r
1684   RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);\r
1685   RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));\r
1686   RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);  \r
1687 \r
1688   /* Fill the Date structure fields with the read parameters */\r
1689   RTC_StampDateStruct->RTC_Year = 0;\r
1690   RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);\r
1691   RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));\r
1692   RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);\r
1693 \r
1694   /* Check the input parameters format */\r
1695   if (RTC_Format == RTC_Format_BIN)\r
1696   {\r
1697     /* Convert the Time structure parameters to Binary format */\r
1698     RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);\r
1699     RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);\r
1700     RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);\r
1701 \r
1702     /* Convert the Date structure parameters to Binary format */\r
1703     RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);\r
1704     RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);\r
1705     RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);\r
1706   }\r
1707 }\r
1708 \r
1709 /**\r
1710   * @}\r
1711   */\r
1712 \r
1713 /** @defgroup RTC_Group9 Tampers configuration functions\r
1714  *  @brief   Tampers configuration functions \r
1715  *\r
1716 @verbatim   \r
1717  ===============================================================================\r
1718                        Tampers configuration functions\r
1719  ===============================================================================  \r
1720 \r
1721 @endverbatim\r
1722   * @{\r
1723   */\r
1724 \r
1725 /**\r
1726   * @brief  Configures the select Tamper pin edge.\r
1727   * @param  RTC_Tamper: Selected tamper pin.\r
1728   *   This parameter can be RTC_Tamper_1.\r
1729   * @param  RTC_TamperTrigger: Specifies the trigger on the tamper pin that \r
1730   *         stimulates tamper event. \r
1731   *   This parameter can be one of the following values:\r
1732   *     @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.\r
1733   *     @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.                         \r
1734   * @retval None\r
1735   */\r
1736 void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)\r
1737 {\r
1738   /* Check the parameters */\r
1739   assert_param(IS_RTC_TAMPER(RTC_Tamper)); \r
1740   assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));\r
1741  \r
1742   if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)\r
1743   {  \r
1744     /* Configure the RTC_TAFCR register */\r
1745     RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));     \r
1746   }\r
1747   else\r
1748   { \r
1749     /* Configure the RTC_TAFCR register */\r
1750     RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);  \r
1751   }  \r
1752 }\r
1753 \r
1754 /**\r
1755   * @brief  Enables or Disables the Tamper detection.\r
1756   * @param  RTC_Tamper: Selected tamper pin.\r
1757   *   This parameter can be RTC_Tamper_1.\r
1758   * @param  NewState: new state of the tamper pin.\r
1759   *   This parameter can be: ENABLE or DISABLE.                   \r
1760   * @retval None\r
1761   */\r
1762 void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)\r
1763 {\r
1764   /* Check the parameters */\r
1765   assert_param(IS_RTC_TAMPER(RTC_Tamper));  \r
1766   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1767   \r
1768   if (NewState != DISABLE)\r
1769   {\r
1770     /* Enable the selected Tamper pin */\r
1771     RTC->TAFCR |= (uint32_t)RTC_Tamper;\r
1772   }\r
1773   else\r
1774   {\r
1775     /* Disable the selected Tamper pin */\r
1776     RTC->TAFCR &= (uint32_t)~RTC_Tamper;    \r
1777   }  \r
1778 }\r
1779 \r
1780 /**\r
1781   * @}\r
1782   */\r
1783 \r
1784 /** @defgroup RTC_Group10 Backup Data Registers configuration functions\r
1785  *  @brief   Backup Data Registers configuration functions  \r
1786  *\r
1787 @verbatim   \r
1788  ===============================================================================\r
1789                        Backup Data Registers configuration functions \r
1790  ===============================================================================  \r
1791 \r
1792 @endverbatim\r
1793   * @{\r
1794   */\r
1795 \r
1796 /**\r
1797   * @brief  Writes a data in a specified RTC Backup data register.\r
1798   * @param  RTC_BKP_DR: RTC Backup data Register number.\r
1799   *   This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to \r
1800   *                          specify the register.\r
1801   * @param  Data: Data to be written in the specified RTC Backup data register.                     \r
1802   * @retval None\r
1803   */\r
1804 void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)\r
1805 {\r
1806   __IO uint32_t tmp = 0;\r
1807   \r
1808   /* Check the parameters */\r
1809   assert_param(IS_RTC_BKP(RTC_BKP_DR));\r
1810 \r
1811   tmp = RTC_BASE + 0x50;\r
1812   tmp += (RTC_BKP_DR * 4);\r
1813 \r
1814   /* Write the specified register */\r
1815   *(__IO uint32_t *)tmp = (uint32_t)Data;\r
1816 }\r
1817 \r
1818 /**\r
1819   * @brief  Reads data from the specified RTC Backup data Register.\r
1820   * @param  RTC_BKP_DR: RTC Backup data Register number.\r
1821   *   This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to \r
1822   *                          specify the register.                   \r
1823   * @retval None\r
1824   */\r
1825 uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)\r
1826 {\r
1827   __IO uint32_t tmp = 0;\r
1828   \r
1829   /* Check the parameters */\r
1830   assert_param(IS_RTC_BKP(RTC_BKP_DR));\r
1831 \r
1832   tmp = RTC_BASE + 0x50;\r
1833   tmp += (RTC_BKP_DR * 4);\r
1834   \r
1835   /* Read the specified register */\r
1836   return (*(__IO uint32_t *)tmp);\r
1837 }\r
1838 \r
1839 /**\r
1840   * @}\r
1841   */\r
1842 \r
1843 /** @defgroup RTC_Group11 Output Type Config configuration functions\r
1844  *  @brief   Output Type Config configuration functions  \r
1845  *\r
1846 @verbatim   \r
1847  ===============================================================================\r
1848                        Output Type Config configuration functions \r
1849  ===============================================================================  \r
1850 \r
1851 @endverbatim\r
1852   * @{\r
1853   */\r
1854 \r
1855 /**\r
1856   * @brief  Configures the RTC Output Pin mode. \r
1857   * @param  RTC_OutputType: specifies the RTC Output (PC13) pin mode.\r
1858   *   This parameter can be one of the following values:\r
1859   *     @arg RTC_OutputType_OpenDrain: RTC Output (PC13) is configured in \r
1860   *                                    Open Drain mode.\r
1861   *     @arg RTC_OutputType_PushPull:  RTC Output (PC13) is configured in \r
1862   *                                    Push Pull mode.    \r
1863   * @retval None\r
1864   */\r
1865 void RTC_OutputTypeConfig(uint32_t RTC_OutputType)\r
1866 {\r
1867   /* Check the parameters */\r
1868   assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));\r
1869   \r
1870   RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);\r
1871   RTC->TAFCR |= (uint32_t)(RTC_OutputType);  \r
1872 }\r
1873 \r
1874 /**\r
1875   * @}\r
1876   */\r
1877 \r
1878 /** @defgroup RTC_Group12 Interrupts and flags management functions\r
1879  *  @brief   Interrupts and flags management functions  \r
1880  *\r
1881 @verbatim   \r
1882  ===============================================================================\r
1883                        Interrupts and flags management functions\r
1884  ===============================================================================  \r
1885  All RTC interrupts are connected to the EXTI controller.\r
1886  \r
1887  - To enable the RTC Alarm interrupt, the following sequence is required:\r
1888    - Configure and enable the EXTI Line 17 in interrupt mode and select the rising \r
1889      edge sensitivity using the EXTI_Init() function.\r
1890    - Configure and enable the RTC_Alarm IRQ channel in the NVIC using the NVIC_Init()\r
1891      function.\r
1892    - Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B) using\r
1893      the RTC_SetAlarm() and RTC_AlarmCmd() functions.\r
1894 \r
1895  - To enable the RTC Wakeup interrupt, the following sequence is required:\r
1896    - Configure and enable the EXTI Line 20 in interrupt mode and select the rising \r
1897      edge sensitivity using the EXTI_Init() function.\r
1898    - Configure and enable the RTC_WKUP IRQ channel in the NVIC using the NVIC_Init()\r
1899      function.\r
1900    - Configure the RTC to generate the RTC wakeup timer event using the \r
1901      RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions.\r
1902 \r
1903  - To enable the RTC Tamper interrupt, the following sequence is required:\r
1904    - Configure and enable the EXTI Line 19 in interrupt mode and select the rising \r
1905      edge sensitivity using the EXTI_Init() function.\r
1906    - Configure and enable the TAMP_STAMP IRQ channel in the NVIC using the NVIC_Init()\r
1907      function.\r
1908    - Configure the RTC to detect the RTC tamper event using the \r
1909      RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.\r
1910 \r
1911  - To enable the RTC TimeStamp interrupt, the following sequence is required:\r
1912    - Configure and enable the EXTI Line 19 in interrupt mode and select the rising \r
1913      edge sensitivity using the EXTI_Init() function.\r
1914    - Configure and enable the TAMP_STAMP IRQ channel in the NVIC using the NVIC_Init()\r
1915      function.\r
1916    - Configure the RTC to detect the RTC time-stamp event using the \r
1917      RTC_TimeStampCmd() functions.\r
1918 \r
1919 @endverbatim\r
1920   * @{\r
1921   */\r
1922 \r
1923 /**\r
1924   * @brief  Enables or disables the specified RTC interrupts.\r
1925   * @param  RTC_IT: specifies the RTC interrupt sources to be enabled or disabled. \r
1926   *   This parameter can be any combination of the following values:\r
1927   *     @arg RTC_IT_TS:  Time Stamp interrupt mask\r
1928   *     @arg RTC_IT_WUT:  WakeUp Timer interrupt mask\r
1929   *     @arg RTC_IT_ALRB:  Alarm B interrupt mask\r
1930   *     @arg RTC_IT_ALRA:  Alarm A interrupt mask\r
1931   *     @arg RTC_IT_TAMP: Tamper event interrupt mask\r
1932   * @param  NewState: new state of the specified RTC interrupts.\r
1933   *   This parameter can be: ENABLE or DISABLE.\r
1934   * @retval None\r
1935   */\r
1936 void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)\r
1937 {\r
1938   /* Check the parameters */\r
1939   assert_param(IS_RTC_CONFIG_IT(RTC_IT));\r
1940   assert_param(IS_FUNCTIONAL_STATE(NewState));\r
1941 \r
1942   /* Disable the write protection for RTC registers */\r
1943   RTC->WPR = 0xCA;\r
1944   RTC->WPR = 0x53;\r
1945 \r
1946   if (NewState != DISABLE)\r
1947   {\r
1948     /* Configure the Interrupts in the RTC_CR register */\r
1949     RTC->CR |= (uint32_t)(RTC_IT & ~RTC_TAFCR_TAMPIE);\r
1950     /* Configure the Tamper Interrupt in the RTC_TAFCR */\r
1951     RTC->TAFCR |= (uint32_t)(RTC_IT & RTC_TAFCR_TAMPIE);\r
1952   }\r
1953   else\r
1954   {\r
1955     /* Configure the Interrupts in the RTC_CR register */\r
1956     RTC->CR &= (uint32_t)~(RTC_IT & (uint32_t)~RTC_TAFCR_TAMPIE);\r
1957     /* Configure the Tamper Interrupt in the RTC_TAFCR */\r
1958     RTC->TAFCR &= (uint32_t)~(RTC_IT & RTC_TAFCR_TAMPIE);\r
1959   }\r
1960   /* Enable the write protection for RTC registers */\r
1961   RTC->WPR = 0xFF; \r
1962 }\r
1963 \r
1964 /**\r
1965   * @brief  Checks whether the specified RTC flag is set or not.\r
1966   * @param  RTC_FLAG: specifies the flag to check.\r
1967   *   This parameter can be one of the following values:\r
1968   *     @arg RTC_FLAG_TAMP1F: Tamper 1 event flag\r
1969   *     @arg RTC_FLAG_TSOVF: Time Stamp OverFlow flag\r
1970   *     @arg RTC_FLAG_TSF: Time Stamp event flag\r
1971   *     @arg RTC_FLAG_WUTF: WakeUp Timer flag\r
1972   *     @arg RTC_FLAG_ALRBF: Alarm B flag\r
1973   *     @arg RTC_FLAG_ALRAF: Alarm A flag\r
1974   *     @arg RTC_FLAG_INITF: Initialization mode flag\r
1975   *     @arg RTC_FLAG_RSF: Registers Synchronized flag\r
1976   *     @arg RTC_FLAG_INITS: Registers Configured flag\r
1977   *     @arg RTC_FLAG_WUTWF: WakeUp Timer Write flag\r
1978   *     @arg RTC_FLAG_ALRBWF: Alarm B Write flag\r
1979   *     @arg RTC_FLAG_ALRAWF: Alarm A write flag\r
1980   * @retval The new state of RTC_FLAG (SET or RESET).\r
1981   */\r
1982 FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)\r
1983 {\r
1984   FlagStatus bitstatus = RESET;\r
1985   uint32_t tmpreg = 0;\r
1986   \r
1987   /* Check the parameters */\r
1988   assert_param(IS_RTC_GET_FLAG(RTC_FLAG));\r
1989   \r
1990   /* Get all the flags */\r
1991   tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);\r
1992   \r
1993   /* Return the status of the flag */\r
1994   if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)\r
1995   {\r
1996     bitstatus = SET;\r
1997   }\r
1998   else\r
1999   {\r
2000     bitstatus = RESET;\r
2001   }\r
2002   return bitstatus;\r
2003 }\r
2004 \r
2005 /**\r
2006   * @brief  Clears the RTC's pending flags.\r
2007   * @param  RTC_FLAG: specifies the RTC flag to clear.\r
2008   *   This parameter can be any combination of the following values:\r
2009   *     @arg RTC_FLAG_TAMP1F: Tamper 1 event flag\r
2010   *     @arg RTC_FLAG_TSOVF: Time Stamp Overflow flag \r
2011   *     @arg RTC_FLAG_TSF: Time Stamp event flag\r
2012   *     @arg RTC_FLAG_WUTF: WakeUp Timer flag\r
2013   *     @arg RTC_FLAG_ALRBF: Alarm B flag\r
2014   *     @arg RTC_FLAG_ALRAF: Alarm A flag\r
2015   *     @arg RTC_FLAG_RSF: Registers Synchronized flag\r
2016   * @retval None\r
2017   */\r
2018 void RTC_ClearFlag(uint32_t RTC_FLAG)\r
2019 {\r
2020   /* Check the parameters */\r
2021   assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));\r
2022 \r
2023   /* Clear the Flags in the RTC_ISR register */\r
2024   RTC->ISR = (uint32_t)((uint32_t)(~((RTC_FLAG | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));  \r
2025 }\r
2026 \r
2027 /**\r
2028   * @brief  Checks whether the specified RTC interrupt has occurred or not.\r
2029   * @param  RTC_IT: specifies the RTC interrupt source to check.\r
2030   *   This parameter can be one of the following values:\r
2031   *     @arg RTC_IT_TS: Time Stamp interrupt \r
2032   *     @arg RTC_IT_WUT: WakeUp Timer interrupt \r
2033   *     @arg RTC_IT_ALRB: Alarm B interrupt \r
2034   *     @arg RTC_IT_ALRA: Alarm A interrupt \r
2035   *     @arg RTC_IT_TAMP1: Tamper 1 event interrupt \r
2036   * @retval The new state of RTC_IT (SET or RESET).\r
2037   */\r
2038 ITStatus RTC_GetITStatus(uint32_t RTC_IT)\r
2039 {\r
2040   ITStatus bitstatus = RESET;\r
2041   uint32_t tmpreg = 0, enablestatus = 0;\r
2042  \r
2043   /* Check the parameters */\r
2044   assert_param(IS_RTC_GET_IT(RTC_IT));\r
2045   \r
2046   /* Get the TAMPER Interrupt enable bit and pending bit */\r
2047   tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));\r
2048  \r
2049   /* Get the Interrupt enable Status */\r
2050   enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & (RTC_IT >> 15)));\r
2051   \r
2052   /* Get the Interrupt pending bit */\r
2053   tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));\r
2054   \r
2055   /* Get the status of the Interrupt */\r
2056   if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))\r
2057   {\r
2058     bitstatus = SET;\r
2059   }\r
2060   else\r
2061   {\r
2062     bitstatus = RESET;\r
2063   }\r
2064   return bitstatus;\r
2065 }\r
2066 \r
2067 /**\r
2068   * @brief  Clears the RTC's interrupt pending bits.\r
2069   * @param  RTC_IT: specifies the RTC interrupt pending bit to clear.\r
2070   *   This parameter can be any combination of the following values:\r
2071   *     @arg RTC_IT_TS: Time Stamp interrupt \r
2072   *     @arg RTC_IT_WUT: WakeUp Timer interrupt \r
2073   *     @arg RTC_IT_ALRB: Alarm B interrupt \r
2074   *     @arg RTC_IT_ALRA: Alarm A interrupt \r
2075   *     @arg RTC_IT_TAMP1: Tamper 1 event interrupt \r
2076   * @retval None\r
2077   */\r
2078 void RTC_ClearITPendingBit(uint32_t RTC_IT)\r
2079 {\r
2080   uint32_t tmpreg = 0;\r
2081 \r
2082   /* Check the parameters */\r
2083   assert_param(IS_RTC_CLEAR_IT(RTC_IT));\r
2084 \r
2085   /* Get the RTC_ISR Interrupt pending bits mask */\r
2086   tmpreg = (uint32_t)(RTC_IT >> 4);\r
2087 \r
2088   /* Clear the interrupt pending bits in the RTC_ISR register */\r
2089   RTC->ISR = (uint32_t)((uint32_t)(~((tmpreg | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT))); \r
2090 }\r
2091 \r
2092 /**\r
2093   * @}\r
2094   */\r
2095 \r
2096 /**\r
2097   * @brief  Converts a 2 digit decimal to BCD format.\r
2098   * @param  Value: Byte to be converted.\r
2099   * @retval Converted byte\r
2100   */\r
2101 static uint8_t RTC_ByteToBcd2(uint8_t Value)\r
2102 {\r
2103   uint8_t bcdhigh = 0;\r
2104   \r
2105   while (Value >= 10)\r
2106   {\r
2107     bcdhigh++;\r
2108     Value -= 10;\r
2109   }\r
2110   \r
2111   return  ((uint8_t)(bcdhigh << 4) | Value);\r
2112 }\r
2113 \r
2114 /**\r
2115   * @brief  Convert from 2 digit BCD to Binary.\r
2116   * @param  Value: BCD value to be converted.\r
2117   * @retval Converted word\r
2118   */\r
2119 static uint8_t RTC_Bcd2ToByte(uint8_t Value)\r
2120 {\r
2121   uint8_t tmp = 0;\r
2122   tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;\r
2123   return (tmp + (Value & (uint8_t)0x0F));\r
2124 }\r
2125 \r
2126 /**\r
2127   * @}\r
2128   */ \r
2129 \r
2130 /**\r
2131   * @}\r
2132   */ \r
2133 \r
2134 /**\r
2135   * @}\r
2136   */ \r
2137 \r
2138 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r