2 ******************************************************************************
3 * @file stm32f4xx_rtc.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Real-Time Clock (RTC) peripheral:
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
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
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:
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)
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
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
56 * ===================================================================
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
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
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
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.
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.
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()
97 * - Use the RTC_DayLightSavingConfig() function to add or sub one
98 * hour to the RTC Calendar.
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.
107 * RTC Wakeup configuration
108 * ========================
109 * - Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
111 * - Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter()
113 * - Enable the RTC WakeUp using the RTC_WakeUpCmd() function
114 * - To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter()
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()
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()
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()
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.
154 * Tamper configuration
155 * ====================
156 * - Enable the RTC Tamper using the RTC_TamperCmd() function.
157 * - Configure the Tamper filter count using RTC_TamperFilterConfig()
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()
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.
173 * Backup Data Registers configuration
174 * ===================================
175 * - To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()
177 * - To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()
180 * ===================================================================
181 * RTC and low power modes
182 * ===================================================================
183 * The MCU can be woken up from a low power mode by an RTC alternate
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.
197 * ===================================================================
198 * Selection of RTC_AF1 alternate functions
199 * ===================================================================
200 * The RTC_AF1 pin (PC13) can be used for the following purposes:
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 * +-------------------------------------------------------------------------------------------------------------+
234 * ===================================================================
235 * Selection of RTC_AF2 alternate functions
236 * ===================================================================
237 * The RTC_AF2 pin (PI8) can be used for the following purposes:
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 * +---------------------------------------------------------------------------------------+
262 ******************************************************************************
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.
272 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
273 ******************************************************************************
276 /* Includes ------------------------------------------------------------------*/
277 #include "stm32f4xx_rtc.h"
278 #include "stm32f4xx_rcc.h"
280 /** @addtogroup STM32F4xx_StdPeriph_Driver
285 * @brief RTC driver modules
289 /* Private typedef -----------------------------------------------------------*/
290 /* Private define ------------------------------------------------------------*/
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 ))
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)
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);
313 /* Private functions ---------------------------------------------------------*/
315 /** @defgroup RTC_Private_Functions
319 /** @defgroup RTC_Group1 Initialization and Configuration functions
320 * @brief Initialization and Configuration functions
323 ===============================================================================
324 Initialization and Configuration functions
325 ===============================================================================
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.
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.
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.
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.
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).
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
363 * @retval An ErrorStatus enumeration value:
364 * - SUCCESS: RTC registers are deinitialized
365 * - ERROR: RTC registers are not deinitialized
367 ErrorStatus RTC_DeInit(void)
369 __IO uint32_t wutcounter = 0x00;
370 uint32_t wutwfstatus = 0x00;
371 ErrorStatus status = ERROR;
373 /* Disable the write protection for RTC registers */
377 /* Set Initialization mode */
378 if (RTC_EnterInitMode() == ERROR)
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;
390 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
393 wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
395 } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
397 if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
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;
411 /* Reset ISR register and exit initialization mode */
412 RTC->ISR = (uint32_t)0x00000000;
414 /* Reset Tamper and alternate functions configuration register */
415 RTC->TAFCR = 0x00000000;
417 if(RTC_WaitForSynchro() == ERROR)
428 /* Enable the write protection for RTC registers */
435 * @brief Initializes the RTC registers according to the specified parameters
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
445 ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
447 ErrorStatus status = ERROR;
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));
454 /* Disable the write protection for RTC registers */
458 /* Set Initialization mode */
459 if (RTC_EnterInitMode() == ERROR)
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));
470 /* Configure the RTC PRER */
471 RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
472 RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
474 /* Exit Initialization mode */
479 /* Enable the write protection for RTC registers */
486 * @brief Fills each RTC_InitStruct member with its default value.
487 * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be
491 void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
493 /* Initialize the RTC_HourFormat member */
494 RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
496 /* Initialize the RTC_AsynchPrediv member */
497 RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
499 /* Initialize the RTC_SynchPrediv member */
500 RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF;
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.
513 void RTC_WriteProtectionCmd(FunctionalState NewState)
515 /* Check the parameters */
516 assert_param(IS_FUNCTIONAL_STATE(NewState));
518 if (NewState != DISABLE)
520 /* Enable the write protection for RTC registers */
525 /* Disable the write protection for RTC registers */
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.
536 * @retval An ErrorStatus enumeration value:
537 * - SUCCESS: RTC is in Init mode
538 * - ERROR: RTC is not in Init mode
540 ErrorStatus RTC_EnterInitMode(void)
542 __IO uint32_t initcounter = 0x00;
543 ErrorStatus status = ERROR;
544 uint32_t initstatus = 0x00;
546 /* Check if the Initialization mode is set */
547 if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
549 /* Set the Initialization mode */
550 RTC->ISR = (uint32_t)RTC_INIT_MASK;
552 /* Wait till RTC is in INIT state and if Time out is reached exit */
555 initstatus = RTC->ISR & RTC_ISR_INITF;
557 } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
559 if ((RTC->ISR & RTC_ISR_INITF) != RESET)
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.
585 void RTC_ExitInitMode(void)
587 /* Exit Initialization mode */
588 RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
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.
603 * @retval An ErrorStatus enumeration value:
604 * - SUCCESS: RTC registers are synchronised
605 * - ERROR: RTC registers are not synchronised
607 ErrorStatus RTC_WaitForSynchro(void)
609 __IO uint32_t synchrocounter = 0;
610 ErrorStatus status = ERROR;
611 uint32_t synchrostatus = 0x00;
613 /* Disable the write protection for RTC registers */
618 RTC->ISR &= (uint32_t)RTC_RSF_MASK;
620 /* Wait the registers to be synchronised */
623 synchrostatus = RTC->ISR & RTC_ISR_RSF;
625 } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
627 if ((RTC->ISR & RTC_ISR_RSF) != RESET)
636 /* Enable the write protection for RTC registers */
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
650 ErrorStatus RTC_RefClockCmd(FunctionalState NewState)
652 ErrorStatus status = ERROR;
654 /* Check the parameters */
655 assert_param(IS_FUNCTIONAL_STATE(NewState));
657 /* Disable the write protection for RTC registers */
661 /* Set Initialization mode */
662 if (RTC_EnterInitMode() == ERROR)
668 if (NewState != DISABLE)
670 /* Enable the RTC reference clock detection */
671 RTC->CR |= RTC_CR_REFCKON;
675 /* Disable the RTC reference clock detection */
676 RTC->CR &= ~RTC_CR_REFCKON;
678 /* Exit Initialization mode */
684 /* Enable the write protection for RTC registers */
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.
698 void RTC_BypassShadowCmd(FunctionalState NewState)
700 /* Check the parameters */
701 assert_param(IS_FUNCTIONAL_STATE(NewState));
703 /* Disable the write protection for RTC registers */
707 if (NewState != DISABLE)
709 /* Set the BYPSHAD bit */
710 RTC->CR |= (uint8_t)RTC_CR_BYPSHAD;
714 /* Reset the BYPSHAD bit */
715 RTC->CR &= (uint8_t)~RTC_CR_BYPSHAD;
718 /* Enable the write protection for RTC registers */
726 /** @defgroup RTC_Group2 Time and Date configuration functions
727 * @brief Time and Date configuration functions
730 ===============================================================================
731 Time and Date configuration functions
732 ===============================================================================
734 This section provide functions allowing to program and read the RTC Calendar
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
753 ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
756 ErrorStatus status = ERROR;
758 /* Check the parameters */
759 assert_param(IS_RTC_FORMAT(RTC_Format));
761 if (RTC_Format == RTC_Format_BIN)
763 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
765 assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
766 assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
770 RTC_TimeStruct->RTC_H12 = 0x00;
771 assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
773 assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
774 assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
778 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
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));
786 RTC_TimeStruct->RTC_H12 = 0x00;
787 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
789 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
790 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
793 /* Check the input parameters format */
794 if (RTC_Format != RTC_Format_BIN)
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));
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));
809 /* Disable the write protection for RTC registers */
813 /* Set Initialization mode */
814 if (RTC_EnterInitMode() == ERROR)
820 /* Set the RTC_TR register */
821 RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
823 /* Exit Initialization mode */
826 if(RTC_WaitForSynchro() == ERROR)
836 /* Enable the write protection for RTC registers */
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
849 void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
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;
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.
868 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
872 /* Check the parameters */
873 assert_param(IS_RTC_FORMAT(RTC_Format));
875 /* Get the RTC_TR register */
876 tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
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);
884 /* Check the input parameters format */
885 if (RTC_Format == RTC_Format_BIN)
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);
895 * @brief Gets the RTC current Calendar Subseconds value.
896 * @note This function freeze the Time and Date registers after reading the
899 * @retval RTC current Calendar Subseconds value.
901 uint32_t RTC_GetSubSecond(void)
905 /* Get subseconds values from the correspondent registers*/
906 tmpreg = (uint32_t)(RTC->SSR);
908 /* Read DR register to unfroze calendar registers */
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
926 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
929 ErrorStatus status = ERROR;
931 /* Check the parameters */
932 assert_param(IS_RTC_FORMAT(RTC_Format));
934 if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
936 RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
938 if (RTC_Format == RTC_Format_BIN)
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));
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));
952 assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
954 /* Check the input parameters format */
955 if (RTC_Format != RTC_Format_BIN)
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));
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));
970 /* Disable the write protection for RTC registers */
974 /* Set Initialization mode */
975 if (RTC_EnterInitMode() == ERROR)
981 /* Set the RTC_DR register */
982 RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
984 /* Exit Initialization mode */
987 if(RTC_WaitForSynchro() == ERROR)
996 /* Enable the write protection for RTC registers */
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
1009 void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
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;
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.
1028 void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
1030 uint32_t tmpreg = 0;
1032 /* Check the parameters */
1033 assert_param(IS_RTC_FORMAT(RTC_Format));
1035 /* Get the RTC_TR register */
1036 tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK);
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);
1044 /* Check the input parameters format */
1045 if (RTC_Format == RTC_Format_BIN)
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);
1059 /** @defgroup RTC_Group3 Alarms configuration functions
1060 * @brief Alarms (Alarm A and Alarm B) configuration functions
1063 ===============================================================================
1064 Alarms (Alarm A and Alarm B) configuration functions
1065 ===============================================================================
1067 This section provide functions allowing to program and read the RTC Alarms.
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.
1089 void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1091 uint32_t tmpreg = 0;
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));
1099 if (RTC_Format == RTC_Format_BIN)
1101 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
1103 assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1104 assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
1108 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1109 assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1111 assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
1112 assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
1114 if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1116 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1120 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1125 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
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));
1133 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1134 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
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)));
1140 if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1142 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1143 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1147 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1148 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1152 /* Check the input parameters format */
1153 if (RTC_Format != RTC_Format_BIN)
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));
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));
1174 /* Disable the write protection for RTC registers */
1178 /* Configure the Alarm register */
1179 if (RTC_Alarm == RTC_Alarm_A)
1181 RTC->ALRMAR = (uint32_t)tmpreg;
1185 RTC->ALRMBR = (uint32_t)tmpreg;
1188 /* Enable the write protection for RTC registers */
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.
1200 void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
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;
1208 /* Alarm Date Settings : Date = 1st day of the month */
1209 RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
1210 RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
1212 /* Alarm Masks Settings : Mask = all fields are not masked */
1213 RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
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.
1230 void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1232 uint32_t tmpreg = 0;
1234 /* Check the parameters */
1235 assert_param(IS_RTC_FORMAT(RTC_Format));
1236 assert_param(IS_RTC_ALARM(RTC_Alarm));
1238 /* Get the RTC_ALRMxR register */
1239 if (RTC_Alarm == RTC_Alarm_A)
1241 tmpreg = (uint32_t)(RTC->ALRMAR);
1245 tmpreg = (uint32_t)(RTC->ALRMBR);
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 | \
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);
1260 if (RTC_Format == RTC_Format_BIN)
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);
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
1284 ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
1286 __IO uint32_t alarmcounter = 0x00;
1287 uint32_t alarmstatus = 0x00;
1288 ErrorStatus status = ERROR;
1290 /* Check the parameters */
1291 assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
1292 assert_param(IS_FUNCTIONAL_STATE(NewState));
1294 /* Disable the write protection for RTC registers */
1298 /* Configure the Alarm state */
1299 if (NewState != DISABLE)
1301 RTC->CR |= (uint32_t)RTC_Alarm;
1307 /* Disable the Alarm in RTC_CR register */
1308 RTC->CR &= (uint32_t)~RTC_Alarm;
1310 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1313 alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
1315 } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
1317 if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
1327 /* Enable the write protection for RTC registers */
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
1378 void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)
1380 uint32_t tmpreg = 0;
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));
1387 /* Disable the write protection for RTC registers */
1391 /* Configure the Alarm A or Alarm B SubSecond registers */
1392 tmpreg = (uint32_t) (uint32_t)(RTC_AlarmSubSecondValue) | (uint32_t)(RTC_AlarmSubSecondMask);
1394 if (RTC_Alarm == RTC_Alarm_A)
1396 /* Configure the AlarmA SubSecond register */
1397 RTC->ALRMASSR = tmpreg;
1401 /* Configure the Alarm B SubSecond register */
1402 RTC->ALRMBSSR = tmpreg;
1405 /* Enable the write protection for RTC registers */
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
1417 * @retval RTC Alarm Subseconds value.
1419 uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
1421 uint32_t tmpreg = 0;
1423 /* Get the RTC_ALRMxR register */
1424 if (RTC_Alarm == RTC_Alarm_A)
1426 tmpreg = (uint32_t)((RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1430 tmpreg = (uint32_t)((RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1440 /** @defgroup RTC_Group4 WakeUp Timer configuration functions
1441 * @brief WakeUp Timer configuration functions
1444 ===============================================================================
1445 WakeUp Timer configuration functions
1446 ===============================================================================
1448 This section provide functions allowing to program and read the RTC WakeUp.
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
1468 void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
1470 /* Check the parameters */
1471 assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
1473 /* Disable the write protection for RTC registers */
1477 /* Clear the Wakeup Timer clock source bits in CR register */
1478 RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1480 /* Configure the clock source */
1481 RTC->CR |= (uint32_t)RTC_WakeUpClock;
1483 /* Enable the write protection for RTC registers */
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.
1495 void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
1497 /* Check the parameters */
1498 assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));
1500 /* Disable the write protection for RTC registers */
1504 /* Configure the Wakeup Timer counter */
1505 RTC->WUTR = (uint32_t)RTC_WakeUpCounter;
1507 /* Enable the write protection for RTC registers */
1512 * @brief Returns the RTC WakeUp timer counter value.
1514 * @retval The RTC WakeUp Counter value.
1516 uint32_t RTC_GetWakeUpCounter(void)
1518 /* Get the counter value */
1519 return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));
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.
1528 ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
1530 __IO uint32_t wutcounter = 0x00;
1531 uint32_t wutwfstatus = 0x00;
1532 ErrorStatus status = ERROR;
1534 /* Check the parameters */
1535 assert_param(IS_FUNCTIONAL_STATE(NewState));
1537 /* Disable the write protection for RTC registers */
1541 if (NewState != DISABLE)
1543 /* Enable the Wakeup Timer */
1544 RTC->CR |= (uint32_t)RTC_CR_WUTE;
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 */
1554 wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
1556 } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
1558 if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
1568 /* Enable the write protection for RTC registers */
1578 /** @defgroup RTC_Group5 Daylight Saving configuration functions
1579 * @brief Daylight Saving configuration functions
1582 ===============================================================================
1583 Daylight Saving configuration functions
1584 ===============================================================================
1586 This section provide functions allowing to configure the RTC DayLight Saving.
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
1605 void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
1607 /* Check the parameters */
1608 assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
1609 assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
1611 /* Disable the write protection for RTC registers */
1615 /* Clear the bits to be configured */
1616 RTC->CR &= (uint32_t)~(RTC_CR_BCK);
1618 /* Configure the RTC_CR register */
1619 RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
1621 /* Enable the write protection for RTC registers */
1626 * @brief Returns the RTC Day Light Saving stored operation.
1628 * @retval RTC Day Light Saving stored operation.
1629 * - RTC_StoreOperation_Reset
1630 * - RTC_StoreOperation_Set
1632 uint32_t RTC_GetStoreOperation(void)
1634 return (RTC->CR & RTC_CR_BCK);
1641 /** @defgroup RTC_Group6 Output pin Configuration function
1642 * @brief Output pin Configuration function
1645 ===============================================================================
1646 Output pin Configuration function
1647 ===============================================================================
1649 This section provide functions allowing to configure the RTC Output source.
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)
1671 void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
1673 /* Check the parameters */
1674 assert_param(IS_RTC_OUTPUT(RTC_Output));
1675 assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
1677 /* Disable the write protection for RTC registers */
1681 /* Clear the bits to be configured */
1682 RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
1684 /* Configure the output selection and polarity */
1685 RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
1687 /* Enable the write protection for RTC registers */
1695 /** @defgroup RTC_Group7 Digital Calibration configuration functions
1696 * @brief Coarse Calibration configuration functions
1699 ===============================================================================
1700 Digital Calibration configuration functions
1701 ===============================================================================
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).
1715 * @note This Calibration value should be between 0 and 63 when using negative
1716 * sign with a 2-ppm step.
1718 * @note This Calibration value should be between 0 and 126 when using positive
1719 * sign with a 4-ppm step.
1721 * @retval An ErrorStatus enumeration value:
1722 * - SUCCESS: RTC Coarse calibration are initialized
1723 * - ERROR: RTC Coarse calibration are not initialized
1725 ErrorStatus RTC_CoarseCalibConfig(uint32_t RTC_CalibSign, uint32_t Value)
1727 ErrorStatus status = ERROR;
1729 /* Check the parameters */
1730 assert_param(IS_RTC_CALIB_SIGN(RTC_CalibSign));
1731 assert_param(IS_RTC_CALIB_VALUE(Value));
1733 /* Disable the write protection for RTC registers */
1737 /* Set Initialization mode */
1738 if (RTC_EnterInitMode() == ERROR)
1744 /* Set the coarse calibration value */
1745 RTC->CALIBR = (uint32_t)(RTC_CalibSign | Value);
1746 /* Exit Initialization mode */
1752 /* Enable the write protection for RTC registers */
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
1766 ErrorStatus RTC_CoarseCalibCmd(FunctionalState NewState)
1768 ErrorStatus status = ERROR;
1770 /* Check the parameters */
1771 assert_param(IS_FUNCTIONAL_STATE(NewState));
1773 /* Disable the write protection for RTC registers */
1777 /* Set Initialization mode */
1778 if (RTC_EnterInitMode() == ERROR)
1784 if (NewState != DISABLE)
1786 /* Enable the Coarse Calibration */
1787 RTC->CR |= (uint32_t)RTC_CR_DCE;
1791 /* Disable the Coarse Calibration */
1792 RTC->CR &= (uint32_t)~RTC_CR_DCE;
1794 /* Exit Initialization mode */
1800 /* Enable the write protection for RTC registers */
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.
1812 void RTC_CalibOutputCmd(FunctionalState NewState)
1814 /* Check the parameters */
1815 assert_param(IS_FUNCTIONAL_STATE(NewState));
1817 /* Disable the write protection for RTC registers */
1821 if (NewState != DISABLE)
1823 /* Enable the RTC clock output */
1824 RTC->CR |= (uint32_t)RTC_CR_COE;
1828 /* Disable the RTC clock output */
1829 RTC->CR &= (uint32_t)~RTC_CR_COE;
1832 /* Enable the write protection for RTC registers */
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.
1844 void RTC_CalibOutputConfig(uint32_t RTC_CalibOutput)
1846 /* Check the parameters */
1847 assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
1849 /* Disable the write protection for RTC registers */
1853 /*clear flags before config*/
1854 RTC->CR &= (uint32_t)~(RTC_CR_COSEL);
1856 /* Configure the RTC_CR register */
1857 RTC->CR |= (uint32_t)RTC_CalibOutput;
1859 /* Enable the write protection for RTC registers */
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
1880 ErrorStatus RTC_SmoothCalibConfig(uint32_t RTC_SmoothCalibPeriod,
1881 uint32_t RTC_SmoothCalibPlusPulses,
1882 uint32_t RTC_SmouthCalibMinusPulsesValue)
1884 ErrorStatus status = ERROR;
1885 uint32_t recalpfcount = 0;
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));
1892 /* Disable the write protection for RTC registers */
1896 /* check if a calibration is pending*/
1897 if ((RTC->ISR & RTC_ISR_RECALPF) != RESET)
1899 /* wait until the Calibration is completed*/
1900 while (((RTC->ISR & RTC_ISR_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
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)
1909 /* Configure the Smooth calibration settings */
1910 RTC->CALR = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
1919 /* Enable the write protection for RTC registers */
1922 return (ErrorStatus)(status);
1930 /** @defgroup RTC_Group8 TimeStamp configuration functions
1931 * @brief TimeStamp configuration functions
1934 ===============================================================================
1935 TimeStamp configuration functions
1936 ===============================================================================
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
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.
1956 void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
1958 uint32_t tmpreg = 0;
1960 /* Check the parameters */
1961 assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
1962 assert_param(IS_FUNCTIONAL_STATE(NewState));
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));
1967 /* Get the new configuration */
1968 if (NewState != DISABLE)
1970 tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
1974 tmpreg |= (uint32_t)(RTC_TimeStampEdge);
1977 /* Disable the write protection for RTC registers */
1981 /* Configure the Time Stamp TSEDGE and Enable bits */
1982 RTC->CR = (uint32_t)tmpreg;
1984 /* Enable the write protection for RTC registers */
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.
2000 void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct,
2001 RTC_DateTypeDef* RTC_StampDateStruct)
2003 uint32_t tmptime = 0, tmpdate = 0;
2005 /* Check the parameters */
2006 assert_param(IS_RTC_FORMAT(RTC_Format));
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);
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);
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);
2024 /* Check the input parameters format */
2025 if (RTC_Format == RTC_Format_BIN)
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);
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);
2040 * @brief Get the RTC timestamp Subseconds value.
2042 * @retval RTC current timestamp Subseconds value.
2044 uint32_t RTC_GetTimeStampSubSecond(void)
2046 /* Get timestamp subseconds values from the correspondent registers */
2047 return (uint32_t)(RTC->TSSSR);
2054 /** @defgroup RTC_Group9 Tampers configuration functions
2055 * @brief Tampers configuration functions
2058 ===============================================================================
2059 Tampers configuration functions
2060 ===============================================================================
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.
2079 void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
2081 /* Check the parameters */
2082 assert_param(IS_RTC_TAMPER(RTC_Tamper));
2083 assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
2085 if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
2087 /* Configure the RTC_TAFCR register */
2088 RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));
2092 /* Configure the RTC_TAFCR register */
2093 RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);
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.
2105 void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
2107 /* Check the parameters */
2108 assert_param(IS_RTC_TAMPER(RTC_Tamper));
2109 assert_param(IS_FUNCTIONAL_STATE(NewState));
2111 if (NewState != DISABLE)
2113 /* Enable the selected Tamper pin */
2114 RTC->TAFCR |= (uint32_t)RTC_Tamper;
2118 /* Disable the selected Tamper pin */
2119 RTC->TAFCR &= (uint32_t)~RTC_Tamper;
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
2136 void RTC_TamperFilterConfig(uint32_t RTC_TamperFilter)
2138 /* Check the parameters */
2139 assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));
2141 /* Clear TAMPFLT[1:0] bits in the RTC_TAFCR register */
2142 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFLT);
2144 /* Configure the RTC_TAFCR register */
2145 RTC->TAFCR |= (uint32_t)RTC_TamperFilter;
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
2170 void RTC_TamperSamplingFreqConfig(uint32_t RTC_TamperSamplingFreq)
2172 /* Check the parameters */
2173 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq));
2175 /* Clear TAMPFREQ[2:0] bits in the RTC_TAFCR register */
2176 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFREQ);
2178 /* Configure the RTC_TAFCR register */
2179 RTC->TAFCR |= (uint32_t)RTC_TamperSamplingFreq;
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
2193 void RTC_TamperPinsPrechargeDuration(uint32_t RTC_TamperPrechargeDuration)
2195 /* Check the parameters */
2196 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(RTC_TamperPrechargeDuration));
2198 /* Clear TAMPPRCH[1:0] bits in the RTC_TAFCR register */
2199 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPPRCH);
2201 /* Configure the RTC_TAFCR register */
2202 RTC->TAFCR |= (uint32_t)RTC_TamperPrechargeDuration;
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
2209 * @param NewState: new state of the timestamp on tamper event.
2210 * This parameter can be: ENABLE or DISABLE.
2213 void RTC_TimeStampOnTamperDetectionCmd(FunctionalState NewState)
2215 /* Check the parameters */
2216 assert_param(IS_FUNCTIONAL_STATE(NewState));
2218 if (NewState != DISABLE)
2220 /* Save timestamp on tamper detection event */
2221 RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPTS;
2225 /* Tamper detection does not cause a timestamp to be saved */
2226 RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPTS;
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.
2236 void RTC_TamperPullUpCmd(FunctionalState NewState)
2238 /* Check the parameters */
2239 assert_param(IS_FUNCTIONAL_STATE(NewState));
2241 if (NewState != DISABLE)
2243 /* Enable precharge of the selected Tamper pin */
2244 RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPPUDIS;
2248 /* Disable precharge of the selected Tamper pin */
2249 RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPPUDIS;
2257 /** @defgroup RTC_Group10 Backup Data Registers configuration functions
2258 * @brief Backup Data Registers configuration functions
2261 ===============================================================================
2262 Backup Data Registers configuration functions
2263 ===============================================================================
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.
2277 void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
2279 __IO uint32_t tmp = 0;
2281 /* Check the parameters */
2282 assert_param(IS_RTC_BKP(RTC_BKP_DR));
2284 tmp = RTC_BASE + 0x50;
2285 tmp += (RTC_BKP_DR * 4);
2287 /* Write the specified register */
2288 *(__IO uint32_t *)tmp = (uint32_t)Data;
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.
2298 uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
2300 __IO uint32_t tmp = 0;
2302 /* Check the parameters */
2303 assert_param(IS_RTC_BKP(RTC_BKP_DR));
2305 tmp = RTC_BASE + 0x50;
2306 tmp += (RTC_BKP_DR * 4);
2308 /* Read the specified register */
2309 return (*(__IO uint32_t *)tmp);
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
2321 ===============================================================================
2322 RTC Tamper and TimeStamp Pins Selection and Output Type Config configuration
2324 ===============================================================================
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.
2338 void RTC_TamperPinSelection(uint32_t RTC_TamperPin)
2340 /* Check the parameters */
2341 assert_param(IS_RTC_TAMPER_PIN(RTC_TamperPin));
2343 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPINSEL);
2344 RTC->TAFCR |= (uint32_t)(RTC_TamperPin);
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.
2355 void RTC_TimeStampPinSelection(uint32_t RTC_TimeStampPin)
2357 /* Check the parameters */
2358 assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
2360 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TSINSEL);
2361 RTC->TAFCR |= (uint32_t)(RTC_TimeStampPin);
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
2370 * @arg RTC_OutputType_PushPull: RTC Output (PC13) is configured in
2374 void RTC_OutputTypeConfig(uint32_t RTC_OutputType)
2376 /* Check the parameters */
2377 assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
2379 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);
2380 RTC->TAFCR |= (uint32_t)(RTC_OutputType);
2387 /** @defgroup RTC_Group12 Shift control synchronisation functions
2388 * @brief Shift control synchronisation functions
2391 ===============================================================================
2392 Shift control synchronisation functions
2393 ===============================================================================
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
2412 ErrorStatus RTC_SynchroShiftConfig(uint32_t RTC_ShiftAdd1S, uint32_t RTC_ShiftSubFS)
2414 ErrorStatus status = ERROR;
2415 uint32_t shpfcount = 0;
2417 /* Check the parameters */
2418 assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
2419 assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));
2421 /* Disable the write protection for RTC registers */
2425 /* Check if a Shift is pending*/
2426 if ((RTC->ISR & RTC_ISR_SHPF) != RESET)
2428 /* Wait until the shift is completed*/
2429 while (((RTC->ISR & RTC_ISR_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
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)
2438 /* check if the reference clock detection is disabled */
2439 if((RTC->CR & RTC_CR_REFCKON) == RESET)
2441 /* Configure the Shift settings */
2442 RTC->SHIFTR = (uint32_t)(uint32_t)(RTC_ShiftSubFS) | (uint32_t)(RTC_ShiftAdd1S);
2444 if(RTC_WaitForSynchro() == ERROR)
2463 /* Enable the write protection for RTC registers */
2466 return (ErrorStatus)(status);
2473 /** @defgroup RTC_Group13 Interrupts and flags management functions
2474 * @brief Interrupts and flags management functions
2477 ===============================================================================
2478 Interrupts and flags management functions
2479 ===============================================================================
2480 All RTC interrupts are connected to the EXTI controller.
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()
2487 - Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B) using
2488 the RTC_SetAlarm() and RTC_AlarmCmd() functions.
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()
2495 - Configure the RTC to generate the RTC wakeup timer event using the
2496 RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd() functions.
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()
2503 - Configure the RTC to detect the RTC tamper event using the
2504 RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
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()
2511 - Configure the RTC to detect the RTC time-stamp event using the
2512 RTC_TimeStampCmd() functions.
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.
2531 void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
2533 /* Check the parameters */
2534 assert_param(IS_RTC_CONFIG_IT(RTC_IT));
2535 assert_param(IS_FUNCTIONAL_STATE(NewState));
2537 /* Disable the write protection for RTC registers */
2541 if (NewState != DISABLE)
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);
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);
2555 /* Enable the write protection for RTC registers */
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).
2577 FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
2579 FlagStatus bitstatus = RESET;
2580 uint32_t tmpreg = 0;
2582 /* Check the parameters */
2583 assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
2585 /* Get all the flags */
2586 tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);
2588 /* Return the status of the flag */
2589 if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)
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
2613 void RTC_ClearFlag(uint32_t RTC_FLAG)
2615 /* Check the parameters */
2616 assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
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)));
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).
2633 ITStatus RTC_GetITStatus(uint32_t RTC_IT)
2635 ITStatus bitstatus = RESET;
2636 uint32_t tmpreg = 0, enablestatus = 0;
2638 /* Check the parameters */
2639 assert_param(IS_RTC_GET_IT(RTC_IT));
2641 /* Get the TAMPER Interrupt enable bit and pending bit */
2642 tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));
2644 /* Get the Interrupt enable Status */
2645 enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & (RTC_IT >> 15)));
2647 /* Get the Interrupt pending bit */
2648 tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));
2650 /* Get the status of the Interrupt */
2651 if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))
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
2673 void RTC_ClearITPendingBit(uint32_t RTC_IT)
2675 uint32_t tmpreg = 0;
2677 /* Check the parameters */
2678 assert_param(IS_RTC_CLEAR_IT(RTC_IT));
2680 /* Get the RTC_ISR Interrupt pending bits mask */
2681 tmpreg = (uint32_t)(RTC_IT >> 4);
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)));
2692 * @brief Converts a 2 digit decimal to BCD format.
2693 * @param Value: Byte to be converted.
2694 * @retval Converted byte
2696 static uint8_t RTC_ByteToBcd2(uint8_t Value)
2698 uint8_t bcdhigh = 0;
2706 return ((uint8_t)(bcdhigh << 4) | Value);
2710 * @brief Convert from 2 digit BCD to Binary.
2711 * @param Value: BCD value to be converted.
2712 * @retval Converted word
2714 static uint8_t RTC_Bcd2ToByte(uint8_t Value)
2717 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
2718 return (tmp + (Value & (uint8_t)0x0F));
2733 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/