Move telemetry displayport init and cms device registering
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F30x_StdPeriph_Driver / src / stm32f30x_rtc.c
blob798d7558dc44659906bd1907ae43ab4177e91db5
1 /**
2 ******************************************************************************
3 * @file stm32f30x_rtc.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 04-April-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Real-Time Clock (RTC) peripheral:
9 * + Initialization
10 * + Calendar (Time and Date) configuration
11 * + Alarms (Alarm A and Alarm B) configuration
12 * + WakeUp Timer configuration
13 * + Daylight Saving configuration
14 * + Output pin Configuration
15 * + Smooth digital Calibration configuration
16 * + TimeStamp configuration
17 * + Tampers configuration
18 * + Backup Data Registers configuration
19 * + Output Type Config configuration
20 * + Shift control synchronisation
21 * + Interrupts and flags management
23 @verbatim
25 ===============================================================================
26 ##### RTC Operating Condition #####
27 ===============================================================================
28 [..] The real-time clock (RTC) and the RTC backup registers can be powered
29 from the VBAT voltage when the main VDD supply is powered off.
30 To retain the content of the RTC backup registers and supply the RTC
31 when VDD is turned off, VBAT pin can be connected to an optional
32 standby voltage supplied by a battery or by another source.
34 [..] To allow the RTC to operate even when the main digital supply (VDD)
35 is turned off, the VBAT pin powers the following blocks:
36 (#) The RTC
37 (#) The LSE oscillator
38 (#) PC13 to PC15 I/Os (when available)
40 [..] When the backup domain is supplied by VDD (analog switch connected
41 to VDD), the following functions are available:
42 (#) PC14 and PC15 can be used as either GPIO or LSE pins
43 (#) PC13 can be used as a GPIO or as the RTC_AF pin
45 [..] When the backup domain is supplied by VBAT (analog switch connected
46 to VBAT because VDD is not present), the following functions are available:
47 (#) PC14 and PC15 can be used as LSE pins only
48 (#) PC13 can be used as the RTC_AF pin
50 ##### Backup Domain Reset #####
51 ===============================================================================
52 [..] The backup domain reset sets all RTC registers and the RCC_BDCR
53 register to their reset values.
54 A backup domain reset is generated when one of the following events
55 occurs:
56 (#) Software reset, triggered by setting the BDRST bit in the
57 RCC Backup domain control register (RCC_BDCR). You can use the
58 RCC_BackupResetCmd().
59 (#) VDD or VBAT power on, if both supplies have previously been
60 powered off.
62 ##### Backup Domain Access #####
63 ===============================================================================
64 [..] After reset, the backup domain (RTC registers and RTC backup data
65 registers) is protected against possible unwanted write accesses.
66 [..] To enable access to the Backup Domain and RTC registers, proceed as follows:
67 (#) Enable the Power Controller (PWR) APB1 interface clock using the
68 RCC_APB1PeriphClockCmd() function.
69 (#) Enable access to Backup domain using the PWR_BackupAccessCmd() function.
70 (#) Select the RTC clock source using the RCC_RTCCLKConfig() function.
71 (#) Enable RTC Clock using the RCC_RTCCLKCmd() function.
73 ##### How to use this driver #####
74 ===============================================================================
75 [..]
76 (+) Enable the backup domain access (see description in the section above)
77 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and
78 RTC hour format using the RTC_Init() function.
80 *** Time and Date configuration ***
81 ===================================
82 [..]
83 (+) To configure the RTC Calendar (Time and Date) use the RTC_SetTime()
84 and RTC_SetDate() functions.
85 (+) To read the RTC Calendar, use the RTC_GetTime() and RTC_GetDate()
86 functions.
87 (+) To read the RTC subsecond, use the RTC_GetSubSecond() function.
88 (+) Use the RTC_DayLightSavingConfig() function to add or sub one
89 hour to the RTC Calendar.
91 *** Alarm configuration ***
92 ===========================
93 [..]
94 (+) To configure the RTC Alarm use the RTC_SetAlarm() function.
95 (+) Enable the selected RTC Alarm using the RTC_AlarmCmd() function.
96 (+) To read the RTC Alarm, use the RTC_GetAlarm() function.
97 (+) To read the RTC alarm SubSecond, use the RTC_GetAlarmSubSecond() function.
99 *** RTC Wakeup configuration ***
100 ================================
101 [..]
102 (+) Configure the RTC Wakeup Clock source use the RTC_WakeUpClockConfig()
103 function.
104 (+) Configure the RTC WakeUp Counter using the RTC_SetWakeUpCounter()
105 function
106 (+) Enable the RTC WakeUp using the RTC_WakeUpCmd() function
107 (+) To read the RTC WakeUp Counter register, use the RTC_GetWakeUpCounter()
108 function.
110 *** Outputs configuration ***
111 =============================
112 [..] The RTC has 2 different outputs:
113 (+) AFO_ALARM: this output is used to manage the RTC Alarm A, Alarm B
114 and WaKeUp signals.
115 To output the selected RTC signal on RTC_AF pin, use the
116 RTC_OutputConfig() function.
117 (+) AFO_CALIB: this output is 512Hz signal or 1Hz .
118 To output the RTC Clock on RTC_AF pin, use the RTC_CalibOutputCmd()
119 function.
121 *** Smooth digital Calibration configuration ***
122 ================================================
123 [..]
124 (+) Configure the RTC Original Digital Calibration Value and the corresponding
125 calibration cycle period (32s,16s and 8s) using the RTC_SmoothCalibConfig()
126 function.
128 *** TimeStamp configuration ***
129 ===============================
130 [..]
131 (+) Configure the RTC_AF trigger and enables the RTC TimeStamp
132 using the RTC_TimeStampCmd() function.
133 (+) To read the RTC TimeStamp Time and Date register, use the
134 RTC_GetTimeStamp() function.
135 (+) To read the RTC TimeStamp SubSecond register, use the
136 RTC_GetTimeStampSubSecond() function.
138 *** Tamper configuration ***
139 ============================
140 [..]
141 (+) Configure the Tamper filter count using RTC_TamperFilterConfig()
142 function.
143 (+) Configure the RTC Tamper trigger Edge or Level according to the Tamper
144 filter (if equal to 0 Edge else Level) value using the RTC_TamperConfig() function.
145 (+) Configure the Tamper sampling frequency using RTC_TamperSamplingFreqConfig()
146 function.
147 (+) Configure the Tamper precharge or discharge duration using
148 RTC_TamperPinsPrechargeDuration() function.
149 (+) Enable the Tamper Pull-UP using RTC_TamperPullUpDisableCmd() function.
150 (+) Enable the RTC Tamper using the RTC_TamperCmd() function.
151 (+) Enable the Time stamp on Tamper detection event using
152 RTC_TSOnTamperDetecCmd() function.
154 *** Backup Data Registers configuration ***
155 ===========================================
156 [..]
157 (+) To write to the RTC Backup Data registers, use the RTC_WriteBackupRegister()
158 function.
159 (+) To read the RTC Backup Data registers, use the RTC_ReadBackupRegister()
160 function.
162 ##### RTC and low power modes #####
163 ===============================================================================
164 [..] The MCU can be woken up from a low power mode by an RTC alternate
165 function.
166 [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
167 RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
168 These RTC alternate functions can wake up the system from the Stop
169 and Standby lowpower modes.
170 The system can also wake up from low power modes without depending
171 on an external interrupt (Auto-wakeup mode), by using the RTC alarm
172 or the RTC wakeup events.
173 [..] The RTC provides a programmable time base for waking up from the
174 Stop or Standby mode at regular intervals.
175 Wakeup from STOP and Standby modes is possible only when the RTC
176 clock source is LSE or LSI.
178 ##### Selection of RTC_AF alternate functions #####
179 ===============================================================================
180 [..] The RTC_AF pin (PC13) can be used for the following purposes:
181 (+) Wakeup pin 2 (WKUP2) using the PWR_WakeUpPinCmd() function.
182 (+) AFO_ALARM output
183 (+) AFO_CALIB output
184 (+) AFI_TAMPER
185 (+) AFI_TIMESTAMP
187 +------------------------------------------------------------------------------------------+
188 | Pin |RTC ALARM |RTC CALIB |RTC TAMPER |RTC TIMESTAMP |PC13MODE| PC13VALUE |
189 | configuration | OUTPUT | OUTPUT | INPUT | INPUT | bit | bit |
190 | and function | ENABLED | ENABLED | ENABLED | ENABLED | | |
191 |-----------------|----------|----------|-----------|--------------|--------|--------------|
192 | Alarm out | | | | | Don't | |
193 | output OD | 1 |Don't care|Don't care | Don't care | care | 0 |
194 |-----------------|----------|----------|-----------|--------------|--------|--------------|
195 | Alarm out | | | | | Don't | |
196 | output PP | 1 |Don't care|Don't care | Don't care | care | 1 |
197 |-----------------|----------|----------|-----------|--------------|--------|--------------|
198 | Calibration out | | | | | Don't | |
199 | output PP | 0 | 1 |Don't care | Don't care | care | Don't care |
200 |-----------------|----------|----------|-----------|--------------|--------|--------------|
201 | TAMPER input | | | | | Don't | |
202 | floating | 0 | 0 | 1 | 0 | care | Don't care |
203 |-----------------|----------|----------|-----------|--------------|--------|--------------|
204 | TIMESTAMP and | | | | | Don't | |
205 | TAMPER input | 0 | 0 | 1 | 1 | care | Don't care |
206 | floating | | | | | | |
207 |-----------------|----------|----------|-----------|--------------|--------|--------------|
208 | TIMESTAMP input | | | | | Don't | |
209 | floating | 0 | 0 | 0 | 1 | care | Don't care |
210 |-----------------|----------|----------|-----------|--------------|--------|--------------|
211 | Output PP | 0 | 0 | 0 | 0 | 1 | PC13 output |
212 | Forced | | | | | | |
213 |-----------------|----------|----------|-----------|--------------|--------|--------------|
214 | Wakeup Pin or | 0 | 0 | 0 | 0 | 0 | Don't care |
215 | Standard GPIO | | | | | | |
216 +------------------------------------------------------------------------------------------+
218 @endverbatim
220 ******************************************************************************
221 * @attention
223 * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
225 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
226 * You may not use this file except in compliance with the License.
227 * You may obtain a copy of the License at:
229 * http://www.st.com/software_license_agreement_liberty_v2
231 * Unless required by applicable law or agreed to in writing, software
232 * distributed under the License is distributed on an "AS IS" BASIS,
233 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
234 * See the License for the specific language governing permissions and
235 * limitations under the License.
237 ******************************************************************************
240 /* Includes ------------------------------------------------------------------*/
241 #include "stm32f30x_rtc.h"
242 #include "stm32f30x_rcc.h"
244 /** @addtogroup STM32F30x_StdPeriph_Driver
245 * @{
248 /** @defgroup RTC
249 * @brief RTC driver modules
250 * @{
253 /* Private typedef -----------------------------------------------------------*/
254 /* Private define ------------------------------------------------------------*/
256 /* Masks Definition */
257 #define RTC_TR_RESERVED_MASK ((uint32_t)0x007F7F7F)
258 #define RTC_DR_RESERVED_MASK ((uint32_t)0x00FFFF3F)
259 #define RTC_INIT_MASK ((uint32_t)0xFFFFFFFF)
260 #define RTC_RSF_MASK ((uint32_t)0xFFFFFF5F)
261 #define RTC_FLAGS_MASK ((uint32_t)(RTC_FLAG_TSOVF | RTC_FLAG_TSF | RTC_FLAG_WUTF | \
262 RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_INITF | \
263 RTC_FLAG_RSF | RTC_FLAG_INITS | RTC_FLAG_WUTWF | \
264 RTC_FLAG_ALRBWF | RTC_FLAG_ALRAWF | RTC_FLAG_TAMP1F | \
265 RTC_FLAG_TAMP2F | RTC_FLAG_TAMP3F | RTC_FLAG_RECALPF | \
266 RTC_FLAG_SHPF))
268 #define INITMODE_TIMEOUT ((uint32_t) 0x00002000)
269 #define SYNCHRO_TIMEOUT ((uint32_t) 0x00008000)
270 #define RECALPF_TIMEOUT ((uint32_t) 0x00001000)
271 #define SHPF_TIMEOUT ((uint32_t) 0x00002000)
273 /* Private macro -------------------------------------------------------------*/
274 /* Private variables ---------------------------------------------------------*/
275 /* Private function prototypes -----------------------------------------------*/
276 static uint8_t RTC_ByteToBcd2(uint8_t Value);
277 static uint8_t RTC_Bcd2ToByte(uint8_t Value);
279 /* Private functions ---------------------------------------------------------*/
281 /** @defgroup RTC_Private_Functions
282 * @{
285 /** @defgroup RTC_Group1 Initialization and Configuration functions
286 * @brief Initialization and Configuration functions
288 @verbatim
289 ===============================================================================
290 ##### Initialization and Configuration functions #####
291 ===============================================================================
292 [..] This section provide functions allowing to initialize and configure the RTC
293 Prescaler (Synchronous and Asynchronous), RTC Hour format, disable RTC registers
294 Write protection, enter and exit the RTC initialization mode, RTC registers
295 synchronization check and reference clock detection enable.
296 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base. It is
297 split into 2 programmable prescalers to minimize power consumption.
298 (++) A 7-bit asynchronous prescaler and A 13-bit synchronous prescaler.
299 (++) When both prescalers are used, it is recommended to configure the
300 asynchronous prescaler to a high value to minimize consumption.
301 (#) All RTC registers are Write protected. Writing to the RTC registers
302 is enabled by writing a key into the Write Protection register, RTC_WPR.
303 (#) To Configure the RTC Calendar, user application should enter initialization
304 mode. In this mode, the calendar counter is stopped and its value
305 can be updated. When the initialization sequence is complete, the
306 calendar restarts counting after 4 RTCCLK cycles.
307 (#) To read the calendar through the shadow registers after Calendar
308 initialization, calendar update or after wakeup from low power modes
309 the software must first clear the RSF flag. The software must then
310 wait until it is set again before reading the calendar, which means
311 that the calendar registers have been correctly copied into the RTC_TR
312 and RTC_DR shadow registers. The RTC_WaitForSynchro() function
313 implements the above software sequence (RSF clear and RSF check).
315 @endverbatim
316 * @{
320 * @brief Deinitializes the RTC registers to their default reset values.
321 * @note This function doesn't reset the RTC Clock source and RTC Backup Data
322 * registers.
323 * @param None
324 * @retval An ErrorStatus enumeration value:
325 * - SUCCESS: RTC registers are deinitialized
326 * - ERROR: RTC registers are not deinitialized
328 ErrorStatus RTC_DeInit(void)
330 __IO uint32_t wutcounter = 0x00;
331 uint32_t wutwfstatus = 0x00;
332 ErrorStatus status = ERROR;
334 /* Disable the write protection for RTC registers */
335 RTC->WPR = 0xCA;
336 RTC->WPR = 0x53;
338 /* Set Initialization mode */
339 if (RTC_EnterInitMode() == ERROR)
341 status = ERROR;
343 else
345 /* Reset TR, DR and CR registers */
346 RTC->TR = (uint32_t)0x00000000;
347 RTC->DR = (uint32_t)0x00002101;
349 /* Reset All CR bits except CR[2:0] */
350 RTC->CR &= (uint32_t)0x00000007;
352 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
355 wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
356 wutcounter++;
357 } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
359 if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
361 status = ERROR;
363 else
365 /* Reset all RTC CR register bits */
366 RTC->CR &= (uint32_t)0x00000000;
367 RTC->WUTR = (uint32_t)0x0000FFFF;
368 RTC->PRER = (uint32_t)0x007F00FF;
369 RTC->ALRMAR = (uint32_t)0x00000000;
370 RTC->ALRMBR = (uint32_t)0x00000000;
371 RTC->SHIFTR = (uint32_t)0x00000000;
372 RTC->CALR = (uint32_t)0x00000000;
373 RTC->ALRMASSR = (uint32_t)0x00000000;
374 RTC->ALRMBSSR = (uint32_t)0x00000000;
376 /* Reset ISR register and exit initialization mode */
377 RTC->ISR = (uint32_t)0x00000000;
379 /* Reset Tamper and alternate functions configuration register */
380 RTC->TAFCR = 0x00000000;
382 /* Wait till the RTC RSF flag is set */
383 if (RTC_WaitForSynchro() == ERROR)
385 status = ERROR;
387 else
389 status = SUCCESS;
394 /* Enable the write protection for RTC registers */
395 RTC->WPR = 0xFF;
397 return status;
401 * @brief Initializes the RTC registers according to the specified parameters
402 * in RTC_InitStruct.
403 * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure that contains
404 * the configuration information for the RTC peripheral.
405 * @note The RTC Prescaler register is write protected and can be written in
406 * initialization mode only.
407 * @retval An ErrorStatus enumeration value:
408 * - SUCCESS: RTC registers are initialized
409 * - ERROR: RTC registers are not initialized
411 ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct)
413 ErrorStatus status = ERROR;
415 /* Check the parameters */
416 assert_param(IS_RTC_HOUR_FORMAT(RTC_InitStruct->RTC_HourFormat));
417 assert_param(IS_RTC_ASYNCH_PREDIV(RTC_InitStruct->RTC_AsynchPrediv));
418 assert_param(IS_RTC_SYNCH_PREDIV(RTC_InitStruct->RTC_SynchPrediv));
420 /* Disable the write protection for RTC registers */
421 RTC->WPR = 0xCA;
422 RTC->WPR = 0x53;
424 /* Set Initialization mode */
425 if (RTC_EnterInitMode() == ERROR)
427 status = ERROR;
429 else
431 /* Clear RTC CR FMT Bit */
432 RTC->CR &= ((uint32_t)~(RTC_CR_FMT));
433 /* Set RTC_CR register */
434 RTC->CR |= ((uint32_t)(RTC_InitStruct->RTC_HourFormat));
436 /* Configure the RTC PRER */
437 RTC->PRER = (uint32_t)(RTC_InitStruct->RTC_SynchPrediv);
438 RTC->PRER |= (uint32_t)(RTC_InitStruct->RTC_AsynchPrediv << 16);
440 /* Exit Initialization mode */
441 RTC_ExitInitMode();
443 status = SUCCESS;
445 /* Enable the write protection for RTC registers */
446 RTC->WPR = 0xFF;
448 return status;
452 * @brief Fills each RTC_InitStruct member with its default value.
453 * @param RTC_InitStruct: pointer to a RTC_InitTypeDef structure which will be
454 * initialized.
455 * @retval None
457 void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct)
459 /* Initialize the RTC_HourFormat member */
460 RTC_InitStruct->RTC_HourFormat = RTC_HourFormat_24;
462 /* Initialize the RTC_AsynchPrediv member */
463 RTC_InitStruct->RTC_AsynchPrediv = (uint32_t)0x7F;
465 /* Initialize the RTC_SynchPrediv member */
466 RTC_InitStruct->RTC_SynchPrediv = (uint32_t)0xFF;
470 * @brief Enables or disables the RTC registers write protection.
471 * @note All the RTC registers are write protected except for RTC_ISR[13:8],
472 * RTC_TAFCR and RTC_BKPxR.
473 * @note Writing a wrong key reactivates the write protection.
474 * @note The protection mechanism is not affected by system reset.
475 * @param NewState: new state of the write protection.
476 * This parameter can be: ENABLE or DISABLE.
477 * @retval None
479 void RTC_WriteProtectionCmd(FunctionalState NewState)
481 /* Check the parameters */
482 assert_param(IS_FUNCTIONAL_STATE(NewState));
484 if (NewState != DISABLE)
486 /* Enable the write protection for RTC registers */
487 RTC->WPR = 0xFF;
489 else
491 /* Disable the write protection for RTC registers */
492 RTC->WPR = 0xCA;
493 RTC->WPR = 0x53;
498 * @brief Enters the RTC Initialization mode.
499 * @note The RTC Initialization mode is write protected, use the
500 * RTC_WriteProtectionCmd(DISABLE) before calling this function.
501 * @param None
502 * @retval An ErrorStatus enumeration value:
503 * - SUCCESS: RTC is in Init mode
504 * - ERROR: RTC is not in Init mode
506 ErrorStatus RTC_EnterInitMode(void)
508 __IO uint32_t initcounter = 0x00;
509 ErrorStatus status = ERROR;
510 uint32_t initstatus = 0x00;
512 /* Check if the Initialization mode is set */
513 if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
515 /* Set the Initialization mode */
516 RTC->ISR = (uint32_t)RTC_INIT_MASK;
518 /* Wait till RTC is in INIT state and if Time out is reached exit */
521 initstatus = RTC->ISR & RTC_ISR_INITF;
522 initcounter++;
523 } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00));
525 if ((RTC->ISR & RTC_ISR_INITF) != RESET)
527 status = SUCCESS;
529 else
531 status = ERROR;
534 else
536 status = SUCCESS;
539 return (status);
543 * @brief Exits the RTC Initialization mode.
544 * @note When the initialization sequence is complete, the calendar restarts
545 * counting after 4 RTCCLK cycles.
546 * @note The RTC Initialization mode is write protected, use the
547 * RTC_WriteProtectionCmd(DISABLE) before calling this function.
548 * @param None
549 * @retval None
551 void RTC_ExitInitMode(void)
553 /* Exit Initialization mode */
554 RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
558 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
559 * synchronized with RTC APB clock.
560 * @note The RTC Resynchronization mode is write protected, use the
561 * RTC_WriteProtectionCmd(DISABLE) before calling this function.
562 * @note To read the calendar through the shadow registers after Calendar
563 * initialization, calendar update or after wakeup from low power modes
564 * the software must first clear the RSF flag.
565 * The software must then wait until it is set again before reading
566 * the calendar, which means that the calendar registers have been
567 * correctly copied into the RTC_TR and RTC_DR shadow registers.
568 * @param None
569 * @retval An ErrorStatus enumeration value:
570 * - SUCCESS: RTC registers are synchronised
571 * - ERROR: RTC registers are not synchronised
573 ErrorStatus RTC_WaitForSynchro(void)
575 __IO uint32_t synchrocounter = 0;
576 ErrorStatus status = ERROR;
577 uint32_t synchrostatus = 0x00;
579 if ((RTC->CR & RTC_CR_BYPSHAD) != RESET)
581 /* Bypass shadow mode */
582 status = SUCCESS;
584 else
586 /* Disable the write protection for RTC registers */
587 RTC->WPR = 0xCA;
588 RTC->WPR = 0x53;
590 /* Clear RSF flag */
591 RTC->ISR &= (uint32_t)RTC_RSF_MASK;
593 /* Wait the registers to be synchronised */
596 synchrostatus = RTC->ISR & RTC_ISR_RSF;
597 synchrocounter++;
598 } while((synchrocounter != SYNCHRO_TIMEOUT) && (synchrostatus == 0x00));
600 if ((RTC->ISR & RTC_ISR_RSF) != RESET)
602 status = SUCCESS;
604 else
606 status = ERROR;
609 /* Enable the write protection for RTC registers */
610 RTC->WPR = 0xFF;
613 return (status);
617 * @brief Enables or disables the RTC reference clock detection.
618 * @param NewState: new state of the RTC reference clock.
619 * This parameter can be: ENABLE or DISABLE.
620 * @retval An ErrorStatus enumeration value:
621 * - SUCCESS: RTC reference clock detection is enabled
622 * - ERROR: RTC reference clock detection is disabled
624 ErrorStatus RTC_RefClockCmd(FunctionalState NewState)
626 ErrorStatus status = ERROR;
628 /* Check the parameters */
629 assert_param(IS_FUNCTIONAL_STATE(NewState));
631 /* Disable the write protection for RTC registers */
632 RTC->WPR = 0xCA;
633 RTC->WPR = 0x53;
635 /* Set Initialization mode */
636 if (RTC_EnterInitMode() == ERROR)
638 status = ERROR;
640 else
642 if (NewState != DISABLE)
644 /* Enable the RTC reference clock detection */
645 RTC->CR |= RTC_CR_REFCKON;
647 else
649 /* Disable the RTC reference clock detection */
650 RTC->CR &= ~RTC_CR_REFCKON;
652 /* Exit Initialization mode */
653 RTC_ExitInitMode();
655 status = SUCCESS;
658 /* Enable the write protection for RTC registers */
659 RTC->WPR = 0xFF;
661 return status;
665 * @brief Enables or Disables the Bypass Shadow feature.
666 * @note When the Bypass Shadow is enabled the calendar value are taken
667 * directly from the Calendar counter.
668 * @param NewState: new state of the Bypass Shadow feature.
669 * This parameter can be: ENABLE or DISABLE.
670 * @retval None
672 void RTC_BypassShadowCmd(FunctionalState NewState)
674 /* Check the parameters */
675 assert_param(IS_FUNCTIONAL_STATE(NewState));
677 /* Disable the write protection for RTC registers */
678 RTC->WPR = 0xCA;
679 RTC->WPR = 0x53;
681 if (NewState != DISABLE)
683 /* Set the BYPSHAD bit */
684 RTC->CR |= (uint8_t)RTC_CR_BYPSHAD;
686 else
688 /* Reset the BYPSHAD bit */
689 RTC->CR &= (uint8_t)~RTC_CR_BYPSHAD;
692 /* Enable the write protection for RTC registers */
693 RTC->WPR = 0xFF;
697 * @}
700 /** @defgroup RTC_Group2 Time and Date configuration functions
701 * @brief Time and Date configuration functions
703 @verbatim
704 ===============================================================================
705 ##### Time and Date configuration functions #####
706 ===============================================================================
707 [..] This section provide functions allowing to program and read the RTC Calendar
708 (Time and Date).
710 @endverbatim
711 * @{
715 * @brief Set the RTC current time.
716 * @param RTC_Format: specifies the format of the entered parameters.
717 * This parameter can be one of the following values:
718 * @arg RTC_Format_BIN: Binary data format
719 * @arg RTC_Format_BCD: BCD data format
720 * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that contains
721 * the time configuration information for the RTC.
722 * @retval An ErrorStatus enumeration value:
723 * - SUCCESS: RTC Time register is configured
724 * - ERROR: RTC Time register is not configured
726 ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
728 uint32_t tmpreg = 0;
729 ErrorStatus status = ERROR;
731 /* Check the parameters */
732 assert_param(IS_RTC_FORMAT(RTC_Format));
734 if (RTC_Format == RTC_Format_BIN)
736 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
738 assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));
739 assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
741 else
743 RTC_TimeStruct->RTC_H12 = 0x00;
744 assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));
746 assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));
747 assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));
749 else
751 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
753 tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
754 assert_param(IS_RTC_HOUR12(tmpreg));
755 assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));
757 else
759 RTC_TimeStruct->RTC_H12 = 0x00;
760 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));
762 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));
763 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));
766 /* Check the input parameters format */
767 if (RTC_Format != RTC_Format_BIN)
769 tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \
770 ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 8) | \
771 ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \
772 ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16));
774 else
776 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \
777 ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 8) | \
778 ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \
779 (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));
782 /* Disable the write protection for RTC registers */
783 RTC->WPR = 0xCA;
784 RTC->WPR = 0x53;
786 /* Set Initialization mode */
787 if (RTC_EnterInitMode() == ERROR)
789 status = ERROR;
791 else
793 /* Set the RTC_TR register */
794 RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
796 /* Exit Initialization mode */
797 RTC_ExitInitMode();
799 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
800 if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
802 if (RTC_WaitForSynchro() == ERROR)
804 status = ERROR;
806 else
808 status = SUCCESS;
811 else
813 status = SUCCESS;
817 /* Enable the write protection for RTC registers */
818 RTC->WPR = 0xFF;
820 return status;
824 * @brief Fills each RTC_TimeStruct member with its default value
825 * (Time = 00h:00min:00sec).
826 * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure which will be
827 * initialized.
828 * @retval None
830 void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct)
832 /* Time = 00h:00min:00sec */
833 RTC_TimeStruct->RTC_H12 = RTC_H12_AM;
834 RTC_TimeStruct->RTC_Hours = 0;
835 RTC_TimeStruct->RTC_Minutes = 0;
836 RTC_TimeStruct->RTC_Seconds = 0;
840 * @brief Get the RTC current Time.
841 * @param RTC_Format: specifies the format of the returned parameters.
842 * This parameter can be one of the following values:
843 * @arg RTC_Format_BIN: Binary data format
844 * @arg RTC_Format_BCD: BCD data format
845 * @param RTC_TimeStruct: pointer to a RTC_TimeTypeDef structure that will
846 * contain the returned current time configuration.
847 * @retval None
849 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)
851 uint32_t tmpreg = 0;
853 /* Check the parameters */
854 assert_param(IS_RTC_FORMAT(RTC_Format));
856 /* Get the RTC_TR register */
857 tmpreg = (uint32_t)(RTC->TR & RTC_TR_RESERVED_MASK);
859 /* Fill the structure fields with the read parameters */
860 RTC_TimeStruct->RTC_Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
861 RTC_TimeStruct->RTC_Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
862 RTC_TimeStruct->RTC_Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
863 RTC_TimeStruct->RTC_H12 = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
865 /* Check the input parameters format */
866 if (RTC_Format == RTC_Format_BIN)
868 /* Convert the structure parameters to Binary format */
869 RTC_TimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);
870 RTC_TimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes);
871 RTC_TimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds);
876 * @brief Gets the RTC current Calendar Subseconds value.
877 * @note This function freeze the Time and Date registers after reading the
878 * SSR register.
879 * @param None
880 * @retval RTC current Calendar Subseconds value.
882 uint32_t RTC_GetSubSecond(void)
884 uint32_t tmpreg = 0;
886 /* Get subseconds values from the correspondent registers*/
887 tmpreg = (uint32_t)(RTC->SSR);
889 /* Read DR register to unfroze calendar registers */
890 (void) (RTC->DR);
892 return (tmpreg);
896 * @brief Set the RTC current date.
897 * @param RTC_Format: specifies the format of the entered parameters.
898 * This parameter can be one of the following values:
899 * @arg RTC_Format_BIN: Binary data format
900 * @arg RTC_Format_BCD: BCD data format
901 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
902 * the date configuration information for the RTC.
903 * @retval An ErrorStatus enumeration value:
904 * - SUCCESS: RTC Date register is configured
905 * - ERROR: RTC Date register is not configured
907 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
909 uint32_t tmpreg = 0;
910 ErrorStatus status = ERROR;
912 /* Check the parameters */
913 assert_param(IS_RTC_FORMAT(RTC_Format));
915 if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
917 RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
919 if (RTC_Format == RTC_Format_BIN)
921 assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
922 assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
923 assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
925 else
927 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
928 tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
929 assert_param(IS_RTC_MONTH(tmpreg));
930 tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
931 assert_param(IS_RTC_DATE(tmpreg));
933 assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
935 /* Check the input parameters format */
936 if (RTC_Format != RTC_Format_BIN)
938 tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
939 (((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
940 ((uint32_t)RTC_DateStruct->RTC_Date) | \
941 (((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13));
943 else
945 tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
946 ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
947 ((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
948 ((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
951 /* Disable the write protection for RTC registers */
952 RTC->WPR = 0xCA;
953 RTC->WPR = 0x53;
955 /* Set Initialization mode */
956 if (RTC_EnterInitMode() == ERROR)
958 status = ERROR;
960 else
962 /* Set the RTC_DR register */
963 RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
965 /* Exit Initialization mode */
966 RTC_ExitInitMode();
968 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
969 if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)
971 if (RTC_WaitForSynchro() == ERROR)
973 status = ERROR;
975 else
977 status = SUCCESS;
980 else
982 status = SUCCESS;
985 /* Enable the write protection for RTC registers */
986 RTC->WPR = 0xFF;
988 return status;
992 * @brief Fills each RTC_DateStruct member with its default value
993 * (Monday, January 01 xx00).
994 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure which will be
995 * initialized.
996 * @retval None
998 void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct)
1000 /* Monday, January 01 xx00 */
1001 RTC_DateStruct->RTC_WeekDay = RTC_Weekday_Monday;
1002 RTC_DateStruct->RTC_Date = 1;
1003 RTC_DateStruct->RTC_Month = RTC_Month_January;
1004 RTC_DateStruct->RTC_Year = 0;
1008 * @brief Get the RTC current date.
1009 * @param RTC_Format: specifies the format of the returned parameters.
1010 * This parameter can be one of the following values:
1011 * @arg RTC_Format_BIN: Binary data format
1012 * @arg RTC_Format_BCD: BCD data format
1013 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that will
1014 * contain the returned current date configuration.
1015 * @retval None
1017 void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
1019 uint32_t tmpreg = 0;
1021 /* Check the parameters */
1022 assert_param(IS_RTC_FORMAT(RTC_Format));
1024 /* Get the RTC_TR register */
1025 tmpreg = (uint32_t)(RTC->DR & RTC_DR_RESERVED_MASK);
1027 /* Fill the structure fields with the read parameters */
1028 RTC_DateStruct->RTC_Year = (uint8_t)((tmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
1029 RTC_DateStruct->RTC_Month = (uint8_t)((tmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1030 RTC_DateStruct->RTC_Date = (uint8_t)(tmpreg & (RTC_DR_DT | RTC_DR_DU));
1031 RTC_DateStruct->RTC_WeekDay = (uint8_t)((tmpreg & (RTC_DR_WDU)) >> 13);
1033 /* Check the input parameters format */
1034 if (RTC_Format == RTC_Format_BIN)
1036 /* Convert the structure parameters to Binary format */
1037 RTC_DateStruct->RTC_Year = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year);
1038 RTC_DateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
1039 RTC_DateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
1040 RTC_DateStruct->RTC_WeekDay = (uint8_t)(RTC_DateStruct->RTC_WeekDay);
1045 * @}
1048 /** @defgroup RTC_Group3 Alarms configuration functions
1049 * @brief Alarms (Alarm A and Alarm B) configuration functions
1051 @verbatim
1052 ===============================================================================
1053 ##### Alarms (Alarm A and Alarm B) configuration functions #####
1054 ===============================================================================
1055 [..] This section provides functions allowing to program and read the RTC Alarms.
1057 @endverbatim
1058 * @{
1062 * @brief Set the specified RTC Alarm.
1063 * @note The Alarm register can only be written when the corresponding Alarm
1064 * is disabled (Use the RTC_AlarmCmd(DISABLE)).
1065 * @param RTC_Format: specifies the format of the returned parameters.
1066 * This parameter can be one of the following values:
1067 * @arg RTC_Format_BIN: Binary data format
1068 * @arg RTC_Format_BCD: BCD data format
1069 * @param RTC_Alarm: specifies the alarm to be configured.
1070 * This parameter can be one of the following values:
1071 * @arg RTC_Alarm_A: to select Alarm A
1072 * @arg RTC_Alarm_B: to select Alarm B
1073 * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that
1074 * contains the alarm configuration parameters.
1075 * @retval None
1077 void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1079 uint32_t tmpreg = 0;
1081 /* Check the parameters */
1082 assert_param(IS_RTC_FORMAT(RTC_Format));
1083 assert_param(IS_RTC_ALARM(RTC_Alarm));
1084 assert_param(IS_ALARM_MASK(RTC_AlarmStruct->RTC_AlarmMask));
1085 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel));
1087 if (RTC_Format == RTC_Format_BIN)
1089 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
1091 assert_param(IS_RTC_HOUR12(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1092 assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
1094 else
1096 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1097 assert_param(IS_RTC_HOUR24(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours));
1099 assert_param(IS_RTC_MINUTES(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes));
1100 assert_param(IS_RTC_SECONDS(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds));
1102 if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1104 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1106 else
1108 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_AlarmStruct->RTC_AlarmDateWeekDay));
1111 else
1113 if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)
1115 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
1116 assert_param(IS_RTC_HOUR12(tmpreg));
1117 assert_param(IS_RTC_H12(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12));
1119 else
1121 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = 0x00;
1122 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours)));
1125 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes)));
1126 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)));
1128 if(RTC_AlarmStruct->RTC_AlarmDateWeekDaySel == RTC_AlarmDateWeekDaySel_Date)
1130 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1131 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
1133 else
1135 tmpreg = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1136 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
1140 /* Check the input parameters format */
1141 if (RTC_Format != RTC_Format_BIN)
1143 tmpreg = (((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
1144 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
1145 ((uint32_t)RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds) | \
1146 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
1147 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
1148 ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
1149 ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
1151 else
1153 tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours) << 16) | \
1154 ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes) << 8) | \
1155 ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds)) | \
1156 ((uint32_t)(RTC_AlarmStruct->RTC_AlarmTime.RTC_H12) << 16) | \
1157 ((uint32_t)RTC_ByteToBcd2(RTC_AlarmStruct->RTC_AlarmDateWeekDay) << 24) | \
1158 ((uint32_t)RTC_AlarmStruct->RTC_AlarmDateWeekDaySel) | \
1159 ((uint32_t)RTC_AlarmStruct->RTC_AlarmMask));
1162 /* Disable the write protection for RTC registers */
1163 RTC->WPR = 0xCA;
1164 RTC->WPR = 0x53;
1166 /* Configure the Alarm register */
1167 if (RTC_Alarm == RTC_Alarm_A)
1169 RTC->ALRMAR = (uint32_t)tmpreg;
1171 else
1173 RTC->ALRMBR = (uint32_t)tmpreg;
1176 /* Enable the write protection for RTC registers */
1177 RTC->WPR = 0xFF;
1181 * @brief Fills each RTC_AlarmStruct member with its default value
1182 * (Time = 00h:00mn:00sec / Date = 1st day of the month/Mask =
1183 * all fields are masked).
1184 * @param RTC_AlarmStruct: pointer to a @ref RTC_AlarmTypeDef structure which
1185 * will be initialized.
1186 * @retval None
1188 void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
1190 /* Alarm Time Settings : Time = 00h:00mn:00sec */
1191 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
1192 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
1193 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
1194 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;
1196 /* Alarm Date Settings : Date = 1st day of the month */
1197 RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
1198 RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;
1200 /* Alarm Masks Settings : Mask = all fields are not masked */
1201 RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_None;
1205 * @brief Get the RTC Alarm value and masks.
1206 * @param RTC_Format: specifies the format of the output parameters.
1207 * This parameter can be one of the following values:
1208 * @arg RTC_Format_BIN: Binary data format
1209 * @arg RTC_Format_BCD: BCD data format
1210 * @param RTC_Alarm: specifies the alarm to be read.
1211 * This parameter can be one of the following values:
1212 * @arg RTC_Alarm_A: to select Alarm A
1213 * @arg RTC_Alarm_B: to select Alarm B
1214 * @param RTC_AlarmStruct: pointer to a RTC_AlarmTypeDef structure that will
1215 * contains the output alarm configuration values.
1216 * @retval None
1218 void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, RTC_AlarmTypeDef* RTC_AlarmStruct)
1220 uint32_t tmpreg = 0;
1222 /* Check the parameters */
1223 assert_param(IS_RTC_FORMAT(RTC_Format));
1224 assert_param(IS_RTC_ALARM(RTC_Alarm));
1226 /* Get the RTC_ALRMxR register */
1227 if (RTC_Alarm == RTC_Alarm_A)
1229 tmpreg = (uint32_t)(RTC->ALRMAR);
1231 else
1233 tmpreg = (uint32_t)(RTC->ALRMBR);
1236 /* Fill the structure with the read parameters */
1237 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | \
1238 RTC_ALRMAR_HU)) >> 16);
1239 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | \
1240 RTC_ALRMAR_MNU)) >> 8);
1241 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | \
1242 RTC_ALRMAR_SU));
1243 RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
1244 RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
1245 RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1246 RTC_AlarmStruct->RTC_AlarmMask = (uint32_t)(tmpreg & RTC_AlarmMask_All);
1248 if (RTC_Format == RTC_Format_BIN)
1250 RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1251 RTC_AlarmTime.RTC_Hours);
1252 RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1253 RTC_AlarmTime.RTC_Minutes);
1254 RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = RTC_Bcd2ToByte(RTC_AlarmStruct-> \
1255 RTC_AlarmTime.RTC_Seconds);
1256 RTC_AlarmStruct->RTC_AlarmDateWeekDay = RTC_Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
1261 * @brief Enables or disables the specified RTC Alarm.
1262 * @param RTC_Alarm: specifies the alarm to be configured.
1263 * This parameter can be any combination of the following values:
1264 * @arg RTC_Alarm_A: to select Alarm A
1265 * @arg RTC_Alarm_B: to select Alarm B
1266 * @param NewState: new state of the specified alarm.
1267 * This parameter can be: ENABLE or DISABLE.
1268 * @retval An ErrorStatus enumeration value:
1269 * - SUCCESS: RTC Alarm is enabled/disabled
1270 * - ERROR: RTC Alarm is not enabled/disabled
1272 ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
1274 __IO uint32_t alarmcounter = 0x00;
1275 uint32_t alarmstatus = 0x00;
1276 ErrorStatus status = ERROR;
1278 /* Check the parameters */
1279 assert_param(IS_RTC_CMD_ALARM(RTC_Alarm));
1280 assert_param(IS_FUNCTIONAL_STATE(NewState));
1282 /* Disable the write protection for RTC registers */
1283 RTC->WPR = 0xCA;
1284 RTC->WPR = 0x53;
1286 /* Configure the Alarm state */
1287 if (NewState != DISABLE)
1289 RTC->CR |= (uint32_t)RTC_Alarm;
1291 status = SUCCESS;
1293 else
1295 /* Disable the Alarm in RTC_CR register */
1296 RTC->CR &= (uint32_t)~RTC_Alarm;
1298 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1301 alarmstatus = RTC->ISR & (RTC_Alarm >> 8);
1302 alarmcounter++;
1303 } while((alarmcounter != INITMODE_TIMEOUT) && (alarmstatus == 0x00));
1305 if ((RTC->ISR & (RTC_Alarm >> 8)) == RESET)
1307 status = ERROR;
1309 else
1311 status = SUCCESS;
1315 /* Enable the write protection for RTC registers */
1316 RTC->WPR = 0xFF;
1318 return status;
1322 * @brief Configures the RTC AlarmA/B Subseconds value and mask.
1323 * @note This function is performed only when the Alarm is disabled.
1324 * @param RTC_Alarm: specifies the alarm to be configured.
1325 * This parameter can be one of the following values:
1326 * @arg RTC_Alarm_A: to select Alarm A
1327 * @arg RTC_Alarm_B: to select Alarm B
1328 * @param RTC_AlarmSubSecondValue: specifies the Subseconds value.
1329 * This parameter can be a value from 0 to 0x00007FFF.
1330 * @param RTC_AlarmSubSecondMask: specifies the Subseconds Mask.
1331 * This parameter can be any combination of the following values:
1332 * @arg RTC_AlarmSubSecondMask_All : All Alarm SS fields are masked.
1333 * There is no comparison on sub seconds for Alarm.
1334 * @arg RTC_AlarmSubSecondMask_SS14_1 : SS[14:1] are don't care in Alarm comparison.
1335 * Only SS[0] is compared
1336 * @arg RTC_AlarmSubSecondMask_SS14_2 : SS[14:2] are don't care in Alarm comparison.
1337 * Only SS[1:0] are compared
1338 * @arg RTC_AlarmSubSecondMask_SS14_3 : SS[14:3] are don't care in Alarm comparison.
1339 * Only SS[2:0] are compared
1340 * @arg RTC_AlarmSubSecondMask_SS14_4 : SS[14:4] are don't care in Alarm comparison.
1341 * Only SS[3:0] are compared
1342 * @arg RTC_AlarmSubSecondMask_SS14_5 : SS[14:5] are don't care in Alarm comparison.
1343 * Only SS[4:0] are compared
1344 * @arg RTC_AlarmSubSecondMask_SS14_6 : SS[14:6] are don't care in Alarm comparison.
1345 * Only SS[5:0] are compared
1346 * @arg RTC_AlarmSubSecondMask_SS14_7 : SS[14:7] are don't care in Alarm comparison.
1347 * Only SS[6:0] are compared
1348 * @arg RTC_AlarmSubSecondMask_SS14_8 : SS[14:8] are don't care in Alarm comparison.
1349 * Only SS[7:0] are compared
1350 * @arg RTC_AlarmSubSecondMask_SS14_9 : SS[14:9] are don't care in Alarm comparison.
1351 * Only SS[8:0] are compared
1352 * @arg RTC_AlarmSubSecondMask_SS14_10: SS[14:10] are don't care in Alarm comparison.
1353 * Only SS[9:0] are compared
1354 * @arg RTC_AlarmSubSecondMask_SS14_11: SS[14:11] are don't care in Alarm comparison.
1355 * Only SS[10:0] are compared
1356 * @arg RTC_AlarmSubSecondMask_SS14_12: SS[14:12] are don't care in Alarm comparison.
1357 * Only SS[11:0] are compared
1358 * @arg RTC_AlarmSubSecondMask_SS14_13: SS[14:13] are don't care in Alarm comparison.
1359 * Only SS[12:0] are compared
1360 * @arg RTC_AlarmSubSecondMask_SS14 : SS[14] is don't care in Alarm comparison.
1361 * Only SS[13:0] are compared
1362 * @arg RTC_AlarmSubSecondMask_None : SS[14:0] are compared and must match
1363 * to activate alarm
1364 * @retval None
1366 void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, uint32_t RTC_AlarmSubSecondMask)
1368 uint32_t tmpreg = 0;
1370 /* Check the parameters */
1371 assert_param(IS_RTC_ALARM(RTC_Alarm));
1372 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(RTC_AlarmSubSecondValue));
1373 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(RTC_AlarmSubSecondMask));
1375 /* Disable the write protection for RTC registers */
1376 RTC->WPR = 0xCA;
1377 RTC->WPR = 0x53;
1379 /* Configure the Alarm A or Alarm B SubSecond registers */
1380 tmpreg = (uint32_t) (uint32_t)(RTC_AlarmSubSecondValue) | (uint32_t)(RTC_AlarmSubSecondMask);
1382 if (RTC_Alarm == RTC_Alarm_A)
1384 /* Configure the AlarmA SubSecond register */
1385 RTC->ALRMASSR = tmpreg;
1387 else
1389 /* Configure the Alarm B SubSecond register */
1390 RTC->ALRMBSSR = tmpreg;
1393 /* Enable the write protection for RTC registers */
1394 RTC->WPR = 0xFF;
1399 * @brief Gets the RTC Alarm Subseconds value.
1400 * @param RTC_Alarm: specifies the alarm to be read.
1401 * This parameter can be one of the following values:
1402 * @arg RTC_Alarm_A: to select Alarm A
1403 * @arg RTC_Alarm_B: to select Alarm B
1404 * @param None
1405 * @retval RTC Alarm Subseconds value.
1407 uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm)
1409 uint32_t tmpreg = 0;
1411 /* Get the RTC_ALRMxR register */
1412 if (RTC_Alarm == RTC_Alarm_A)
1414 tmpreg = (uint32_t)((RTC->ALRMASSR) & RTC_ALRMASSR_SS);
1416 else
1418 tmpreg = (uint32_t)((RTC->ALRMBSSR) & RTC_ALRMBSSR_SS);
1421 return (tmpreg);
1425 * @}
1428 /** @defgroup RTC_Group4 WakeUp Timer configuration functions
1429 * @brief WakeUp Timer configuration functions
1431 @verbatim
1432 ===============================================================================
1433 ##### WakeUp Timer configuration functions #####
1434 ===============================================================================
1435 [..] This section provide functions allowing to program and read the RTC WakeUp.
1437 @endverbatim
1438 * @{
1442 * @brief Configures the RTC Wakeup clock source.
1443 * @note The WakeUp Clock source can only be changed when the RTC WakeUp
1444 * is disabled (Use the RTC_WakeUpCmd(DISABLE)).
1445 * @param RTC_WakeUpClock: Wakeup Clock source.
1446 * This parameter can be one of the following values:
1447 * @arg RTC_WakeUpClock_RTCCLK_Div16: RTC Wakeup Counter Clock = RTCCLK/16
1448 * @arg RTC_WakeUpClock_RTCCLK_Div8: RTC Wakeup Counter Clock = RTCCLK/8
1449 * @arg RTC_WakeUpClock_RTCCLK_Div4: RTC Wakeup Counter Clock = RTCCLK/4
1450 * @arg RTC_WakeUpClock_RTCCLK_Div2: RTC Wakeup Counter Clock = RTCCLK/2
1451 * @arg RTC_WakeUpClock_CK_SPRE_16bits: RTC Wakeup Counter Clock = CK_SPRE
1452 * @arg RTC_WakeUpClock_CK_SPRE_17bits: RTC Wakeup Counter Clock = CK_SPRE
1453 * @retval None
1455 void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock)
1457 /* Check the parameters */
1458 assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));
1460 /* Disable the write protection for RTC registers */
1461 RTC->WPR = 0xCA;
1462 RTC->WPR = 0x53;
1464 /* Clear the Wakeup Timer clock source bits in CR register */
1465 RTC->CR &= (uint32_t)~RTC_CR_WUCKSEL;
1467 /* Configure the clock source */
1468 RTC->CR |= (uint32_t)RTC_WakeUpClock;
1470 /* Enable the write protection for RTC registers */
1471 RTC->WPR = 0xFF;
1475 * @brief Configures the RTC Wakeup counter.
1476 * @note The RTC WakeUp counter can only be written when the RTC WakeUp
1477 * is disabled (Use the RTC_WakeUpCmd(DISABLE)).
1478 * @param RTC_WakeUpCounter: specifies the WakeUp counter.
1479 * This parameter can be a value from 0x0000 to 0xFFFF.
1480 * @retval None
1482 void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter)
1484 /* Check the parameters */
1485 assert_param(IS_RTC_WAKEUP_COUNTER(RTC_WakeUpCounter));
1487 /* Disable the write protection for RTC registers */
1488 RTC->WPR = 0xCA;
1489 RTC->WPR = 0x53;
1491 /* Configure the Wakeup Timer counter */
1492 RTC->WUTR = (uint32_t)RTC_WakeUpCounter;
1494 /* Enable the write protection for RTC registers */
1495 RTC->WPR = 0xFF;
1499 * @brief Returns the RTC WakeUp timer counter value.
1500 * @param None
1501 * @retval The RTC WakeUp Counter value.
1503 uint32_t RTC_GetWakeUpCounter(void)
1505 /* Get the counter value */
1506 return ((uint32_t)(RTC->WUTR & RTC_WUTR_WUT));
1510 * @brief Enables or Disables the RTC WakeUp timer.
1511 * @param NewState: new state of the WakeUp timer.
1512 * This parameter can be: ENABLE or DISABLE.
1513 * @retval None
1515 ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
1517 __IO uint32_t wutcounter = 0x00;
1518 uint32_t wutwfstatus = 0x00;
1519 ErrorStatus status = ERROR;
1521 /* Check the parameters */
1522 assert_param(IS_FUNCTIONAL_STATE(NewState));
1524 /* Disable the write protection for RTC registers */
1525 RTC->WPR = 0xCA;
1526 RTC->WPR = 0x53;
1528 if (NewState != DISABLE)
1530 /* Enable the Wakeup Timer */
1531 RTC->CR |= (uint32_t)RTC_CR_WUTE;
1532 status = SUCCESS;
1534 else
1536 /* Disable the Wakeup Timer */
1537 RTC->CR &= (uint32_t)~RTC_CR_WUTE;
1538 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1541 wutwfstatus = RTC->ISR & RTC_ISR_WUTWF;
1542 wutcounter++;
1543 } while((wutcounter != INITMODE_TIMEOUT) && (wutwfstatus == 0x00));
1545 if ((RTC->ISR & RTC_ISR_WUTWF) == RESET)
1547 status = ERROR;
1549 else
1551 status = SUCCESS;
1555 /* Enable the write protection for RTC registers */
1556 RTC->WPR = 0xFF;
1558 return status;
1562 * @}
1565 /** @defgroup RTC_Group5 Daylight Saving configuration functions
1566 * @brief Daylight Saving configuration functions
1568 @verbatim
1569 ===============================================================================
1570 ##### Daylight Saving configuration functions #####
1571 ===============================================================================
1572 [..] This section provide functions allowing to configure the RTC DayLight Saving.
1574 @endverbatim
1575 * @{
1579 * @brief Adds or substract one hour from the current time.
1580 * @param RTC_DayLightSaveOperation: the value of hour adjustment.
1581 * This parameter can be one of the following values:
1582 * @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)
1583 * @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)
1584 * @param RTC_StoreOperation: Specifies the value to be written in the BCK bit
1585 * in CR register to store the operation.
1586 * This parameter can be one of the following values:
1587 * @arg RTC_StoreOperation_Reset: BCK Bit Reset
1588 * @arg RTC_StoreOperation_Set: BCK Bit Set
1589 * @retval None
1591 void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
1593 /* Check the parameters */
1594 assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
1595 assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
1597 /* Disable the write protection for RTC registers */
1598 RTC->WPR = 0xCA;
1599 RTC->WPR = 0x53;
1601 /* Clear the bits to be configured */
1602 RTC->CR &= (uint32_t)~(RTC_CR_BCK);
1604 /* Configure the RTC_CR register */
1605 RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
1607 /* Enable the write protection for RTC registers */
1608 RTC->WPR = 0xFF;
1612 * @brief Returns the RTC Day Light Saving stored operation.
1613 * @param None
1614 * @retval RTC Day Light Saving stored operation.
1615 * - RTC_StoreOperation_Reset
1616 * - RTC_StoreOperation_Set
1618 uint32_t RTC_GetStoreOperation(void)
1620 return (RTC->CR & RTC_CR_BCK);
1624 * @}
1627 /** @defgroup RTC_Group6 Output pin Configuration function
1628 * @brief Output pin Configuration function
1630 @verbatim
1631 ===============================================================================
1632 ##### Output pin Configuration function #####
1633 ===============================================================================
1634 [..] This section provide functions allowing to configure the RTC Output source.
1636 @endverbatim
1637 * @{
1641 * @brief Configures the RTC output source (AFO_ALARM).
1642 * @param RTC_Output: Specifies which signal will be routed to the RTC output.
1643 * This parameter can be one of the following values:
1644 * @arg RTC_Output_Disable: No output selected
1645 * @arg RTC_Output_AlarmA: signal of AlarmA mapped to output
1646 * @arg RTC_Output_AlarmB: signal of AlarmB mapped to output
1647 * @arg RTC_Output_WakeUp: signal of WakeUp mapped to output
1648 * @param RTC_OutputPolarity: Specifies the polarity of the output signal.
1649 * This parameter can be one of the following:
1650 * @arg RTC_OutputPolarity_High: The output pin is high when the
1651 * ALRAF/ALRBF/WUTF is high (depending on OSEL)
1652 * @arg RTC_OutputPolarity_Low: The output pin is low when the
1653 * ALRAF/ALRBF/WUTF is high (depending on OSEL)
1654 * @retval None
1656 void RTC_OutputConfig(uint32_t RTC_Output, uint32_t RTC_OutputPolarity)
1658 /* Check the parameters */
1659 assert_param(IS_RTC_OUTPUT(RTC_Output));
1660 assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));
1662 /* Disable the write protection for RTC registers */
1663 RTC->WPR = 0xCA;
1664 RTC->WPR = 0x53;
1666 /* Clear the bits to be configured */
1667 RTC->CR &= (uint32_t)~(RTC_CR_OSEL | RTC_CR_POL);
1669 /* Configure the output selection and polarity */
1670 RTC->CR |= (uint32_t)(RTC_Output | RTC_OutputPolarity);
1672 /* Enable the write protection for RTC registers */
1673 RTC->WPR = 0xFF;
1677 * @}
1680 /** @defgroup RTC_Group7 Digital Calibration configuration functions
1681 * @brief Digital Calibration configuration functions
1683 @verbatim
1684 ===============================================================================
1685 ##### Digital Calibration configuration functions #####
1686 ===============================================================================
1688 @endverbatim
1689 * @{
1693 * @brief Enables or disables the RTC clock to be output through the relative
1694 * pin.
1695 * @param NewState: new state of the digital calibration Output.
1696 * This parameter can be: ENABLE or DISABLE.
1697 * @retval None
1699 void RTC_CalibOutputCmd(FunctionalState NewState)
1701 /* Check the parameters */
1702 assert_param(IS_FUNCTIONAL_STATE(NewState));
1704 /* Disable the write protection for RTC registers */
1705 RTC->WPR = 0xCA;
1706 RTC->WPR = 0x53;
1708 if (NewState != DISABLE)
1710 /* Enable the RTC clock output */
1711 RTC->CR |= (uint32_t)RTC_CR_COE;
1713 else
1715 /* Disable the RTC clock output */
1716 RTC->CR &= (uint32_t)~RTC_CR_COE;
1719 /* Enable the write protection for RTC registers */
1720 RTC->WPR = 0xFF;
1724 * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
1725 * @param RTC_CalibOutput : Select the Calibration output Selection .
1726 * This parameter can be one of the following values:
1727 * @arg RTC_CalibOutput_512Hz: A signal has a regular waveform at 512Hz.
1728 * @arg RTC_CalibOutput_1Hz : A signal has a regular waveform at 1Hz.
1729 * @retval None
1731 void RTC_CalibOutputConfig(uint32_t RTC_CalibOutput)
1733 /* Check the parameters */
1734 assert_param(IS_RTC_CALIB_OUTPUT(RTC_CalibOutput));
1736 /* Disable the write protection for RTC registers */
1737 RTC->WPR = 0xCA;
1738 RTC->WPR = 0x53;
1740 /*clear flags before config*/
1741 RTC->CR &= (uint32_t)~(RTC_CR_COSEL);
1743 /* Configure the RTC_CR register */
1744 RTC->CR |= (uint32_t)RTC_CalibOutput;
1746 /* Enable the write protection for RTC registers */
1747 RTC->WPR = 0xFF;
1751 * @brief Configures the Smooth Calibration Settings.
1752 * @param RTC_SmoothCalibPeriod : Select the Smooth Calibration Period.
1753 * This parameter can be can be one of the following values:
1754 * @arg RTC_SmoothCalibPeriod_32sec : The smooth calibration periode is 32s.
1755 * @arg RTC_SmoothCalibPeriod_16sec : The smooth calibration periode is 16s.
1756 * @arg RTC_SmoothCalibPeriod_8sec : The smooth calibartion periode is 8s.
1757 * @param RTC_SmoothCalibPlusPulses : Select to Set or reset the CALP bit.
1758 * This parameter can be one of the following values:
1759 * @arg RTC_SmoothCalibPlusPulses_Set : Add one RTCCLK puls every 2**11 pulses.
1760 * @arg RTC_SmoothCalibPlusPulses_Reset: No RTCCLK pulses are added.
1761 * @param RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
1762 * This parameter can be one any value from 0 to 0x000001FF.
1763 * @retval An ErrorStatus enumeration value:
1764 * - SUCCESS: RTC Calib registers are configured
1765 * - ERROR: RTC Calib registers are not configured
1767 ErrorStatus RTC_SmoothCalibConfig(uint32_t RTC_SmoothCalibPeriod,
1768 uint32_t RTC_SmoothCalibPlusPulses,
1769 uint32_t RTC_SmouthCalibMinusPulsesValue)
1771 ErrorStatus status = ERROR;
1772 uint32_t recalpfcount = 0;
1774 /* Check the parameters */
1775 assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod));
1776 assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
1777 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));
1779 /* Disable the write protection for RTC registers */
1780 RTC->WPR = 0xCA;
1781 RTC->WPR = 0x53;
1783 /* check if a calibration is pending*/
1784 if ((RTC->ISR & RTC_ISR_RECALPF) != RESET)
1786 /* wait until the Calibration is completed*/
1787 while (((RTC->ISR & RTC_ISR_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
1789 recalpfcount++;
1793 /* check if the calibration pending is completed or if there is no calibration operation at all*/
1794 if ((RTC->ISR & RTC_ISR_RECALPF) == RESET)
1796 /* Configure the Smooth calibration settings */
1797 RTC->CALR = (uint32_t)((uint32_t)RTC_SmoothCalibPeriod | (uint32_t)RTC_SmoothCalibPlusPulses | (uint32_t)RTC_SmouthCalibMinusPulsesValue);
1799 status = SUCCESS;
1801 else
1803 status = ERROR;
1806 /* Enable the write protection for RTC registers */
1807 RTC->WPR = 0xFF;
1809 return (ErrorStatus)(status);
1813 * @}
1817 /** @defgroup RTC_Group8 TimeStamp configuration functions
1818 * @brief TimeStamp configuration functions
1820 @verbatim
1821 ===============================================================================
1822 ##### TimeStamp configuration functions #####
1823 ===============================================================================
1825 @endverbatim
1826 * @{
1830 * @brief Enables or Disables the RTC TimeStamp functionality with the
1831 * specified time stamp pin stimulating edge.
1832 * @param RTC_TimeStampEdge: Specifies the pin edge on which the TimeStamp is
1833 * activated.
1834 * This parameter can be one of the following:
1835 * @arg RTC_TimeStampEdge_Rising: the Time stamp event occurs on the rising
1836 * edge of the related pin.
1837 * @arg RTC_TimeStampEdge_Falling: the Time stamp event occurs on the
1838 * falling edge of the related pin.
1839 * @param NewState: new state of the TimeStamp.
1840 * This parameter can be: ENABLE or DISABLE.
1841 * @retval None
1843 void RTC_TimeStampCmd(uint32_t RTC_TimeStampEdge, FunctionalState NewState)
1845 uint32_t tmpreg = 0;
1847 /* Check the parameters */
1848 assert_param(IS_RTC_TIMESTAMP_EDGE(RTC_TimeStampEdge));
1849 assert_param(IS_FUNCTIONAL_STATE(NewState));
1851 /* Get the RTC_CR register and clear the bits to be configured */
1852 tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
1854 /* Get the new configuration */
1855 if (NewState != DISABLE)
1857 tmpreg |= (uint32_t)(RTC_TimeStampEdge | RTC_CR_TSE);
1859 else
1861 tmpreg |= (uint32_t)(RTC_TimeStampEdge);
1864 /* Disable the write protection for RTC registers */
1865 RTC->WPR = 0xCA;
1866 RTC->WPR = 0x53;
1868 /* Configure the Time Stamp TSEDGE and Enable bits */
1869 RTC->CR = (uint32_t)tmpreg;
1871 /* Enable the write protection for RTC registers */
1872 RTC->WPR = 0xFF;
1876 * @brief Gets the RTC TimeStamp value and masks.
1877 * @param RTC_Format: specifies the format of the output parameters.
1878 * This parameter can be one of the following values:
1879 * @arg RTC_Format_BIN: Binary data format
1880 * @arg RTC_Format_BCD: BCD data format
1881 * @param RTC_StampTimeStruct: pointer to a RTC_TimeTypeDef structure that will
1882 * contains the TimeStamp time values.
1883 * @param RTC_StampDateStruct: pointer to a RTC_DateTypeDef structure that will
1884 * contains the TimeStamp date values.
1885 * @retval None
1887 void RTC_GetTimeStamp(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_StampTimeStruct,
1888 RTC_DateTypeDef* RTC_StampDateStruct)
1890 uint32_t tmptime = 0, tmpdate = 0;
1892 /* Check the parameters */
1893 assert_param(IS_RTC_FORMAT(RTC_Format));
1895 /* Get the TimeStamp time and date registers values */
1896 tmptime = (uint32_t)(RTC->TSTR & RTC_TR_RESERVED_MASK);
1897 tmpdate = (uint32_t)(RTC->TSDR & RTC_DR_RESERVED_MASK);
1899 /* Fill the Time structure fields with the read parameters */
1900 RTC_StampTimeStruct->RTC_Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
1901 RTC_StampTimeStruct->RTC_Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
1902 RTC_StampTimeStruct->RTC_Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
1903 RTC_StampTimeStruct->RTC_H12 = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
1905 /* Fill the Date structure fields with the read parameters */
1906 RTC_StampDateStruct->RTC_Year = 0;
1907 RTC_StampDateStruct->RTC_Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
1908 RTC_StampDateStruct->RTC_Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
1909 RTC_StampDateStruct->RTC_WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
1911 /* Check the input parameters format */
1912 if (RTC_Format == RTC_Format_BIN)
1914 /* Convert the Time structure parameters to Binary format */
1915 RTC_StampTimeStruct->RTC_Hours = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Hours);
1916 RTC_StampTimeStruct->RTC_Minutes = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Minutes);
1917 RTC_StampTimeStruct->RTC_Seconds = (uint8_t)RTC_Bcd2ToByte(RTC_StampTimeStruct->RTC_Seconds);
1919 /* Convert the Date structure parameters to Binary format */
1920 RTC_StampDateStruct->RTC_Month = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Month);
1921 RTC_StampDateStruct->RTC_Date = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_Date);
1922 RTC_StampDateStruct->RTC_WeekDay = (uint8_t)RTC_Bcd2ToByte(RTC_StampDateStruct->RTC_WeekDay);
1927 * @brief Gets the RTC timestamp Subseconds value.
1928 * @param None
1929 * @retval RTC current timestamp Subseconds value.
1931 uint32_t RTC_GetTimeStampSubSecond(void)
1933 /* Get timestamp subseconds values from the correspondent registers */
1934 return (uint32_t)(RTC->TSSSR);
1938 * @}
1941 /** @defgroup RTC_Group9 Tampers configuration functions
1942 * @brief Tampers configuration functions
1944 @verbatim
1945 ===============================================================================
1946 ##### Tampers configuration functions #####
1947 ===============================================================================
1949 @endverbatim
1950 * @{
1954 * @brief Configures the select Tamper pin edge.
1955 * @param RTC_Tamper: Selected tamper pin.
1956 * This parameter can be any combination of the following values:
1957 * @arg RTC_Tamper_1: Select Tamper 1.
1958 * @arg RTC_Tamper_2: Select Tamper 2.
1959 * @arg RTC_Tamper_3: Select Tamper 3.
1960 * @param RTC_TamperTrigger: Specifies the trigger on the tamper pin that
1961 * stimulates tamper event.
1962 * This parameter can be one of the following values:
1963 * @arg RTC_TamperTrigger_RisingEdge: Rising Edge of the tamper pin causes tamper event.
1964 * @arg RTC_TamperTrigger_FallingEdge: Falling Edge of the tamper pin causes tamper event.
1965 * @arg RTC_TamperTrigger_LowLevel: Low Level of the tamper pin causes tamper event.
1966 * @arg RTC_TamperTrigger_HighLevel: High Level of the tamper pin causes tamper event.
1967 * @retval None
1969 void RTC_TamperTriggerConfig(uint32_t RTC_Tamper, uint32_t RTC_TamperTrigger)
1971 /* Check the parameters */
1972 assert_param(IS_RTC_TAMPER(RTC_Tamper));
1973 assert_param(IS_RTC_TAMPER_TRIGGER(RTC_TamperTrigger));
1975 /* Check if the active level for Tamper is rising edge (Low level)*/
1976 if (RTC_TamperTrigger == RTC_TamperTrigger_RisingEdge)
1978 /* Configure the RTC_TAFCR register */
1979 RTC->TAFCR &= (uint32_t)((uint32_t)~(RTC_Tamper << 1));
1981 else
1983 /* Configure the RTC_TAFCR register */
1984 RTC->TAFCR |= (uint32_t)(RTC_Tamper << 1);
1989 * @brief Enables or Disables the Tamper detection.
1990 * @param RTC_Tamper: Selected tamper pin.
1991 * This parameter can be any combination of the following values:
1992 * @arg RTC_Tamper_1: Select Tamper 1.
1993 * @arg RTC_Tamper_2: Select Tamper 2.
1994 * @arg RTC_Tamper_3: Select Tamper 3.
1995 * @param NewState: new state of the tamper pin.
1996 * This parameter can be: ENABLE or DISABLE.
1997 * @retval None
1999 void RTC_TamperCmd(uint32_t RTC_Tamper, FunctionalState NewState)
2001 /* Check the parameters */
2002 assert_param(IS_RTC_TAMPER(RTC_Tamper));
2003 assert_param(IS_FUNCTIONAL_STATE(NewState));
2005 if (NewState != DISABLE)
2007 /* Enable the selected Tamper pin */
2008 RTC->TAFCR |= (uint32_t)RTC_Tamper;
2010 else
2012 /* Disable the selected Tamper pin */
2013 RTC->TAFCR &= (uint32_t)~RTC_Tamper;
2018 * @brief Configures the Tampers Filter.
2019 * @param RTC_TamperFilter: Specifies the tampers filter.
2020 * This parameter can be one of the following values:
2021 * @arg RTC_TamperFilter_Disable: Tamper filter is disabled.
2022 * @arg RTC_TamperFilter_2Sample: Tamper is activated after 2 consecutive
2023 * samples at the active level
2024 * @arg RTC_TamperFilter_4Sample: Tamper is activated after 4 consecutive
2025 * samples at the active level
2026 * @arg RTC_TamperFilter_8Sample: Tamper is activated after 8 consecutive
2027 * samples at the active level
2028 * @retval None
2030 void RTC_TamperFilterConfig(uint32_t RTC_TamperFilter)
2032 /* Check the parameters */
2033 assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));
2035 /* Clear TAMPFLT[1:0] bits in the RTC_TAFCR register */
2036 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFLT);
2038 /* Configure the RTC_TAFCR register */
2039 RTC->TAFCR |= (uint32_t)RTC_TamperFilter;
2043 * @brief Configures the Tampers Sampling Frequency.
2044 * @param RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency.
2045 * This parameter can be one of the following values:
2046 * @arg RTC_TamperSamplingFreq_RTCCLK_Div32768: Each of the tamper inputs are sampled
2047 * with a frequency = RTCCLK / 32768
2048 * @arg RTC_TamperSamplingFreq_RTCCLK_Div16384: Each of the tamper inputs are sampled
2049 * with a frequency = RTCCLK / 16384
2050 * @arg RTC_TamperSamplingFreq_RTCCLK_Div8192: Each of the tamper inputs are sampled
2051 * with a frequency = RTCCLK / 8192
2052 * @arg RTC_TamperSamplingFreq_RTCCLK_Div4096: Each of the tamper inputs are sampled
2053 * with a frequency = RTCCLK / 4096
2054 * @arg RTC_TamperSamplingFreq_RTCCLK_Div2048: Each of the tamper inputs are sampled
2055 * with a frequency = RTCCLK / 2048
2056 * @arg RTC_TamperSamplingFreq_RTCCLK_Div1024: Each of the tamper inputs are sampled
2057 * with a frequency = RTCCLK / 1024
2058 * @arg RTC_TamperSamplingFreq_RTCCLK_Div512: Each of the tamper inputs are sampled
2059 * with a frequency = RTCCLK / 512
2060 * @arg RTC_TamperSamplingFreq_RTCCLK_Div256: Each of the tamper inputs are sampled
2061 * with a frequency = RTCCLK / 256
2062 * @retval None
2064 void RTC_TamperSamplingFreqConfig(uint32_t RTC_TamperSamplingFreq)
2066 /* Check the parameters */
2067 assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(RTC_TamperSamplingFreq));
2069 /* Clear TAMPFREQ[2:0] bits in the RTC_TAFCR register */
2070 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPFREQ);
2072 /* Configure the RTC_TAFCR register */
2073 RTC->TAFCR |= (uint32_t)RTC_TamperSamplingFreq;
2077 * @brief Configures the Tampers Pins input Precharge Duration.
2078 * @param RTC_TamperPrechargeDuration: Specifies the Tampers Pins input
2079 * Precharge Duration.
2080 * This parameter can be one of the following values:
2081 * @arg RTC_TamperPrechargeDuration_1RTCCLK: Tamper pins are pre-charged before sampling during 1 RTCCLK cycle
2082 * @arg RTC_TamperPrechargeDuration_2RTCCLK: Tamper pins are pre-charged before sampling during 2 RTCCLK cycle
2083 * @arg RTC_TamperPrechargeDuration_4RTCCLK: Tamper pins are pre-charged before sampling during 4 RTCCLK cycle
2084 * @arg RTC_TamperPrechargeDuration_8RTCCLK: Tamper pins are pre-charged before sampling during 8 RTCCLK cycle
2085 * @retval None
2087 void RTC_TamperPinsPrechargeDuration(uint32_t RTC_TamperPrechargeDuration)
2089 /* Check the parameters */
2090 assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(RTC_TamperPrechargeDuration));
2092 /* Clear TAMPPRCH[1:0] bits in the RTC_TAFCR register */
2093 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_TAMPPRCH);
2095 /* Configure the RTC_TAFCR register */
2096 RTC->TAFCR |= (uint32_t)RTC_TamperPrechargeDuration;
2100 * @brief Enables or Disables the TimeStamp on Tamper Detection Event.
2101 * @note The timestamp is valid even the TSE bit in tamper control register
2102 * is reset.
2103 * @param NewState: new state of the timestamp on tamper event.
2104 * This parameter can be: ENABLE or DISABLE.
2105 * @retval None
2107 void RTC_TimeStampOnTamperDetectionCmd(FunctionalState NewState)
2109 /* Check the parameters */
2110 assert_param(IS_FUNCTIONAL_STATE(NewState));
2112 if (NewState != DISABLE)
2114 /* Save timestamp on tamper detection event */
2115 RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPTS;
2117 else
2119 /* Tamper detection does not cause a timestamp to be saved */
2120 RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPTS;
2125 * @brief Enables or Disables the Precharge of Tamper pin.
2126 * @param NewState: new state of tamper pull up.
2127 * This parameter can be: ENABLE or DISABLE.
2128 * @retval None
2130 void RTC_TamperPullUpCmd(FunctionalState NewState)
2132 /* Check the parameters */
2133 assert_param(IS_FUNCTIONAL_STATE(NewState));
2135 if (NewState != DISABLE)
2137 /* Enable precharge of the selected Tamper pin */
2138 RTC->TAFCR &= (uint32_t)~RTC_TAFCR_TAMPPUDIS;
2140 else
2142 /* Disable precharge of the selected Tamper pin */
2143 RTC->TAFCR |= (uint32_t)RTC_TAFCR_TAMPPUDIS;
2148 * @}
2151 /** @defgroup RTC_Group10 Backup Data Registers configuration functions
2152 * @brief Backup Data Registers configuration functions
2154 @verbatim
2155 ===============================================================================
2156 ##### Backup Data Registers configuration functions #####
2157 ===============================================================================
2159 @endverbatim
2160 * @{
2164 * @brief Writes a data in a specified RTC Backup data register.
2165 * @param RTC_BKP_DR: RTC Backup data Register number.
2166 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 15 to
2167 * specify the register.
2168 * @param Data: Data to be written in the specified RTC Backup data register.
2169 * @retval None
2171 void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
2173 __IO uint32_t tmp = 0;
2175 /* Check the parameters */
2176 assert_param(IS_RTC_BKP(RTC_BKP_DR));
2178 tmp = RTC_BASE + 0x50;
2179 tmp += (RTC_BKP_DR * 4);
2181 /* Write the specified register */
2182 *(__IO uint32_t *)tmp = (uint32_t)Data;
2186 * @brief Reads data from the specified RTC Backup data Register.
2187 * @param RTC_BKP_DR: RTC Backup data Register number.
2188 * This parameter can be: RTC_BKP_DRx where x can be from 0 to 15 to
2189 * specify the register.
2190 * @retval None
2192 uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR)
2194 __IO uint32_t tmp = 0;
2196 /* Check the parameters */
2197 assert_param(IS_RTC_BKP(RTC_BKP_DR));
2199 tmp = RTC_BASE + 0x50;
2200 tmp += (RTC_BKP_DR * 4);
2202 /* Read the specified register */
2203 return (*(__IO uint32_t *)tmp);
2207 * @}
2210 /** @defgroup RTC_Group11 Output Type Config configuration functions
2211 * @brief Output Type Config configuration functions
2213 @verbatim
2214 ===============================================================================
2215 ##### Output Type Config configuration functions #####
2216 ===============================================================================
2218 @endverbatim
2219 * @{
2223 * @brief Configures the RTC Output Pin mode.
2224 * @param RTC_OutputType: specifies the RTC Output (PC13) pin mode.
2225 * This parameter can be one of the following values:
2226 * @arg RTC_OutputType_OpenDrain: RTC Output (PC13) is configured in
2227 * Open Drain mode.
2228 * @arg RTC_OutputType_PushPull: RTC Output (PC13) is configured in
2229 * Push Pull mode.
2230 * @retval None
2232 void RTC_OutputTypeConfig(uint32_t RTC_OutputType)
2234 /* Check the parameters */
2235 assert_param(IS_RTC_OUTPUT_TYPE(RTC_OutputType));
2237 RTC->TAFCR &= (uint32_t)~(RTC_TAFCR_ALARMOUTTYPE);
2238 RTC->TAFCR |= (uint32_t)(RTC_OutputType);
2242 * @}
2245 /** @defgroup RTC_Group12 Shift control synchronisation functions
2246 * @brief Shift control synchronisation functions
2248 @verbatim
2249 ===============================================================================
2250 ##### Shift control synchronisation functions #####
2251 ===============================================================================
2253 @endverbatim
2254 * @{
2258 * @brief Configures the Synchronization Shift Control Settings.
2259 * @note When REFCKON is set, firmware must not write to Shift control register
2260 * @param RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar.
2261 * This parameter can be one of the following values :
2262 * @arg RTC_ShiftAdd1S_Set : Add one second to the clock calendar.
2263 * @arg RTC_ShiftAdd1S_Reset: No effect.
2264 * @param RTC_ShiftSubFS: Select the number of Second Fractions to Substitute.
2265 * This parameter can be one any value from 0 to 0x7FFF.
2266 * @retval An ErrorStatus enumeration value:
2267 * - SUCCESS: RTC Shift registers are configured
2268 * - ERROR: RTC Shift registers are not configured
2270 ErrorStatus RTC_SynchroShiftConfig(uint32_t RTC_ShiftAdd1S, uint32_t RTC_ShiftSubFS)
2272 ErrorStatus status = ERROR;
2273 uint32_t shpfcount = 0;
2275 /* Check the parameters */
2276 assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
2277 assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));
2279 /* Disable the write protection for RTC registers */
2280 RTC->WPR = 0xCA;
2281 RTC->WPR = 0x53;
2283 /* Check if a Shift is pending*/
2284 if ((RTC->ISR & RTC_ISR_SHPF) != RESET)
2286 /* Wait until the shift is completed*/
2287 while (((RTC->ISR & RTC_ISR_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
2289 shpfcount++;
2293 /* Check if the Shift pending is completed or if there is no Shift operation at all*/
2294 if ((RTC->ISR & RTC_ISR_SHPF) == RESET)
2296 /* check if the reference clock detection is disabled */
2297 if((RTC->CR & RTC_CR_REFCKON) == RESET)
2299 /* Configure the Shift settings */
2300 RTC->SHIFTR = (uint32_t)(uint32_t)(RTC_ShiftSubFS) | (uint32_t)(RTC_ShiftAdd1S);
2302 if(RTC_WaitForSynchro() == ERROR)
2304 status = ERROR;
2306 else
2308 status = SUCCESS;
2311 else
2313 status = ERROR;
2316 else
2318 status = ERROR;
2321 /* Enable the write protection for RTC registers */
2322 RTC->WPR = 0xFF;
2324 return (ErrorStatus)(status);
2328 * @}
2331 /** @defgroup RTC_Group13 Interrupts and flags management functions
2332 * @brief Interrupts and flags management functions
2334 @verbatim
2335 ===============================================================================
2336 ##### Interrupts and flags management functions #####
2337 ===============================================================================
2338 [..] All RTC interrupts are connected to the EXTI controller.
2339 (+) To enable the RTC Alarm interrupt, the following sequence is required:
2340 (++) Configure and enable the EXTI Line 17 in interrupt mode and select
2341 the rising edge sensitivity using the EXTI_Init() function.
2342 (++) Configure and enable the RTC_Alarm IRQ channel in the NVIC using
2343 the NVIC_Init() function.
2344 (++) Configure the RTC to generate RTC alarms (Alarm A and/or Alarm B)
2345 using the RTC_SetAlarm() and RTC_AlarmCmd() functions.
2346 (+) To enable the RTC Wakeup interrupt, the following sequence is required:
2347 (++) Configure and enable the EXTI Line 20 in interrupt mode and select
2348 the rising edge sensitivity using the EXTI_Init() function.
2349 (++) Configure and enable the RTC_WKUP IRQ channel in the NVIC using
2350 the NVIC_Init() function.
2351 (++) Configure the RTC to generate the RTC wakeup timer event using the
2352 RTC_WakeUpClockConfig(), RTC_SetWakeUpCounter() and RTC_WakeUpCmd()
2353 functions.
2354 (+) To enable the RTC Tamper interrupt, the following sequence is required:
2355 (++) Configure and enable the EXTI Line 19 in interrupt mode and select
2356 the rising edge sensitivity using the EXTI_Init() function.
2357 (++) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
2358 the NVIC_Init() function.
2359 (++) Configure the RTC to detect the RTC tamper event using the
2360 RTC_TamperTriggerConfig() and RTC_TamperCmd() functions.
2361 (+) To enable the RTC TimeStamp interrupt, the following sequence is required:
2362 (++) Configure and enable the EXTI Line 19 in interrupt mode and select
2363 the rising edge sensitivity using the EXTI_Init() function.
2364 (++) Configure and enable the TAMP_STAMP IRQ channel in the NVIC using
2365 the NVIC_Init() function.
2366 (++) Configure the RTC to detect the RTC time-stamp event using the
2367 RTC_TimeStampCmd() functions.
2369 @endverbatim
2370 * @{
2374 * @brief Enables or disables the specified RTC interrupts.
2375 * @param RTC_IT: specifies the RTC interrupt sources to be enabled or disabled.
2376 * This parameter can be any combination of the following values:
2377 * @arg RTC_IT_TS: Time Stamp interrupt mask
2378 * @arg RTC_IT_WUT: WakeUp Timer interrupt mask
2379 * @arg RTC_IT_ALRB: Alarm B interrupt mask
2380 * @arg RTC_IT_ALRA: Alarm A interrupt mask
2381 * @arg RTC_IT_TAMP: Tamper event interrupt mask
2382 * @param NewState: new state of the specified RTC interrupts.
2383 * This parameter can be: ENABLE or DISABLE.
2384 * @retval None
2386 void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState)
2388 /* Check the parameters */
2389 assert_param(IS_RTC_CONFIG_IT(RTC_IT));
2390 assert_param(IS_FUNCTIONAL_STATE(NewState));
2392 /* Disable the write protection for RTC registers */
2393 RTC->WPR = 0xCA;
2394 RTC->WPR = 0x53;
2396 if (NewState != DISABLE)
2398 /* Configure the Interrupts in the RTC_CR register */
2399 RTC->CR |= (uint32_t)(RTC_IT & ~RTC_TAFCR_TAMPIE);
2400 /* Configure the Tamper Interrupt in the RTC_TAFCR */
2401 RTC->TAFCR |= (uint32_t)(RTC_IT & RTC_TAFCR_TAMPIE);
2403 else
2405 /* Configure the Interrupts in the RTC_CR register */
2406 RTC->CR &= (uint32_t)~(RTC_IT & (uint32_t)~RTC_TAFCR_TAMPIE);
2407 /* Configure the Tamper Interrupt in the RTC_TAFCR */
2408 RTC->TAFCR &= (uint32_t)~(RTC_IT & RTC_TAFCR_TAMPIE);
2410 /* Enable the write protection for RTC registers */
2411 RTC->WPR = 0xFF;
2415 * @brief Checks whether the specified RTC flag is set or not.
2416 * @param RTC_FLAG: specifies the flag to check.
2417 * This parameter can be one of the following values:
2418 * @arg RTC_FLAG_RECALPF: RECALPF event flag
2419 * @arg RTC_FLAG_TAMP3F: Tamper 3 event flag
2420 * @arg RTC_FLAG_TAMP2F: Tamper 2 event flag
2421 * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
2422 * @arg RTC_FLAG_TSOVF: Time Stamp OverFlow flag
2423 * @arg RTC_FLAG_TSF: Time Stamp event flag
2424 * @arg RTC_FLAG_WUTF: WakeUp Timer flag
2425 * @arg RTC_FLAG_ALRBF: Alarm B flag
2426 * @arg RTC_FLAG_ALRAF: Alarm A flag
2427 * @arg RTC_FLAG_INITF: Initialization mode flag
2428 * @arg RTC_FLAG_RSF: Registers Synchronized flag
2429 * @arg RTC_FLAG_INITS: Registers Configured flag
2430 * @argRTC_FLAG_SHPF : Shift operation pending flag.
2431 * @arg RTC_FLAG_WUTWF: WakeUp Timer Write flag
2432 * @arg RTC_FLAG_ALRBWF: Alarm B Write flag
2433 * @arg RTC_FLAG_ALRAWF: Alarm A write flag
2434 * @retval The new state of RTC_FLAG (SET or RESET).
2436 FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG)
2438 FlagStatus bitstatus = RESET;
2439 uint32_t tmpreg = 0;
2441 /* Check the parameters */
2442 assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
2444 /* Get all the flags */
2445 tmpreg = (uint32_t)(RTC->ISR & RTC_FLAGS_MASK);
2447 /* Return the status of the flag */
2448 if ((tmpreg & RTC_FLAG) != (uint32_t)RESET)
2450 bitstatus = SET;
2452 else
2454 bitstatus = RESET;
2456 return bitstatus;
2460 * @brief Clears the RTC's pending flags.
2461 * @param RTC_FLAG: specifies the RTC flag to clear.
2462 * This parameter can be any combination of the following values:
2463 * @arg RTC_FLAG_TAMP3F: Tamper 3 event flag
2464 * @arg RTC_FLAG_TAMP2F: Tamper 2 event flag
2465 * @arg RTC_FLAG_TAMP1F: Tamper 1 event flag
2466 * @arg RTC_FLAG_TSOVF: Time Stamp Overflow flag
2467 * @arg RTC_FLAG_TSF: Time Stamp event flag
2468 * @arg RTC_FLAG_WUTF: WakeUp Timer flag
2469 * @arg RTC_FLAG_ALRBF: Alarm B flag
2470 * @arg RTC_FLAG_ALRAF: Alarm A flag
2471 * @arg RTC_FLAG_RSF: Registers Synchronized flag
2472 * @retval None
2474 void RTC_ClearFlag(uint32_t RTC_FLAG)
2476 /* Check the parameters */
2477 assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
2479 /* Clear the Flags in the RTC_ISR register */
2480 RTC->ISR = (uint32_t)((uint32_t)(~((RTC_FLAG | RTC_ISR_INIT)& 0x0001FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
2484 * @brief Checks whether the specified RTC interrupt has occurred or not.
2485 * @param RTC_IT: specifies the RTC interrupt source to check.
2486 * This parameter can be one of the following values:
2487 * @arg RTC_IT_TS: Time Stamp interrupt
2488 * @arg RTC_IT_WUT: WakeUp Timer interrupt
2489 * @arg RTC_IT_ALRB: Alarm B interrupt
2490 * @arg RTC_IT_ALRA: Alarm A interrupt
2491 * @arg RTC_IT_TAMP1: Tamper1 event interrupt
2492 * @arg RTC_IT_TAMP2: Tamper2 event interrupt
2493 * @arg RTC_IT_TAMP3: Tamper3 event interrupt
2494 * @retval The new state of RTC_IT (SET or RESET).
2496 ITStatus RTC_GetITStatus(uint32_t RTC_IT)
2498 ITStatus bitstatus = RESET;
2499 uint32_t tmpreg = 0, enablestatus = 0;
2501 /* Check the parameters */
2502 assert_param(IS_RTC_GET_IT(RTC_IT));
2504 /* Get the TAMPER Interrupt enable bit and pending bit */
2505 tmpreg = (uint32_t)(RTC->TAFCR & (RTC_TAFCR_TAMPIE));
2507 /* Get the Interrupt enable Status */
2508 enablestatus = (uint32_t)((RTC->CR & RTC_IT) | (tmpreg & ((RTC_IT >> (RTC_IT >> 18)) >> 15)));
2510 /* Get the Interrupt pending bit */
2511 tmpreg = (uint32_t)((RTC->ISR & (uint32_t)(RTC_IT >> 4)));
2513 /* Get the status of the Interrupt */
2514 if ((enablestatus != (uint32_t)RESET) && ((tmpreg & 0x0000FFFF) != (uint32_t)RESET))
2516 bitstatus = SET;
2518 else
2520 bitstatus = RESET;
2522 return bitstatus;
2526 * @brief Clears the RTC's interrupt pending bits.
2527 * @param RTC_IT: specifies the RTC interrupt pending bit to clear.
2528 * This parameter can be any combination of the following values:
2529 * @arg RTC_IT_TS: Time Stamp interrupt
2530 * @arg RTC_IT_WUT: WakeUp Timer interrupt
2531 * @arg RTC_IT_ALRB: Alarm B interrupt
2532 * @arg RTC_IT_ALRA: Alarm A interrupt
2533 * @arg RTC_IT_TAMP1: Tamper1 event interrupt
2534 * @arg RTC_IT_TAMP2: Tamper2 event interrupt
2535 * @arg RTC_IT_TAMP3: Tamper3 event interrupt
2536 * @retval None
2538 void RTC_ClearITPendingBit(uint32_t RTC_IT)
2540 uint32_t tmpreg = 0;
2542 /* Check the parameters */
2543 assert_param(IS_RTC_CLEAR_IT(RTC_IT));
2545 /* Get the RTC_ISR Interrupt pending bits mask */
2546 tmpreg = (uint32_t)(RTC_IT >> 4);
2548 /* Clear the interrupt pending bits in the RTC_ISR register */
2549 RTC->ISR = (uint32_t)((uint32_t)(~((tmpreg | RTC_ISR_INIT)& 0x0000FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
2553 * @}
2557 * @brief Converts a 2 digit decimal to BCD format.
2558 * @param Value: Byte to be converted.
2559 * @retval Converted byte
2561 static uint8_t RTC_ByteToBcd2(uint8_t Value)
2563 uint8_t bcdhigh = 0;
2565 while (Value >= 10)
2567 bcdhigh++;
2568 Value -= 10;
2571 return ((uint8_t)(bcdhigh << 4) | Value);
2575 * @brief Convert from 2 digit BCD to Binary.
2576 * @param Value: BCD value to be converted.
2577 * @retval Converted word
2579 static uint8_t RTC_Bcd2ToByte(uint8_t Value)
2581 uint8_t tmp = 0;
2582 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
2583 return (tmp + (Value & (uint8_t)0x0F));
2587 * @}
2591 * @}
2595 * @}
2598 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/