STM32F4 RTC details

1, Introduction to RTC

RTC(Real Time Clock): real time clock.
The RTC is a separate BCD timer / counter. RTC provides a calendar clock, two programmable alarm clock interrupts, and a periodic programmable wake-up flag with interrupt function. The RTC also includes an automatic wake-up unit for managing the low power consumption mode. Two 32-bit registers contain seconds, minutes, hours (12 or 24-hour system), day of the week, date, month and year in binary decimal format (BCD). In addition, binary sub second values can be provided. The system can automatically compensate the days of the month as 28, 29 (leap year), 30 and 31 days. After power on reset, all RTC registers will be protected to prevent possible abnormal write access. RTC module and clock are configured in the backup area, that is, after system reset or standby mode wake-up, the setting and time of RTC remain unchanged. As long as the power supply in the backup area is normal, RTC will work all the time. However, after the system is reset, access to the backup area and RTC will be automatically prohibited to prevent accidental operation. Therefore, the write protection of the backup area should be cancelled before setting the time.
2, Working principle block diagram of RTC

Among them, the leftmost is clock input, which can be input by LSE, HSE and LSI through RTC_ Pre (the default value is 128) divides the frequency, obtains the clock of 256Hz, and then enters the second RTC_ Pres (the default value is 256) divides the frequency to obtain A clock of 1Hz. 1Hz is used for second timing, and 256Hz is used for sub second timing. Alarm clock A and alarm clock B pass through the judge and RTC_TR and RTC_DR to compare and realize the alarm clock. Below are the Automatic wake-up clock and register.

2, RTC related common registers

RTC prescaler (rtc_prescaler)

RTC time register (RTC_TR)

RTC date register (RTC_DR)

RTC sub second register (RTC_SSR)

RTC wake up timer register (RTC_WUTR)

RTC alarm clock A/B register (RTC_ALRMAR/RTC_ALRMBR)

RTC alarm clock A/B sub second register (RTC_ALRMASSR/ RTC_ALRMBSSR)
RTC write protect register (RTC_WPR)

3, Explanation of RTC related library functions

RTC clock source and clock operation function:

void RCC_RTCCLKConfig(uint32_t  CLKSource);//Clock source selection
void RCC_RTCCLKCmd(FunctionalState NewState)//Clock enable

RTC initialization function:

ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct);
typedef struct
{ uint32_t RTC_HourFormat;   //Hour format: 24 / 12
  uint32_t RTC_AsynchPrediv;  //Asynchronous frequency division coefficient
  uint32_t RTC_SynchPrediv; //Synchronous frequency division coefficient

RTC calendar configuration related functions:

 ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct);
 void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct);
 ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct);
 void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct);
 uint32_t RTC_GetSubSecond(void);

RTC alarm clock correlation function:

ErrorStatus RTC_AlarmCmd(uint32_t RTC_Alarm, FunctionalState NewState)
void RTC_SetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm,   
                                                          RTC_AlarmTypeDef* RTC_AlarmStruct);
void RTC_GetAlarm(uint32_t RTC_Format, uint32_t RTC_Alarm, 
                                                         RTC_AlarmTypeDef* RTC_AlarmStruct);
void RTC_AlarmSubSecondConfig(uint32_t RTC_Alarm, uint32_t RTC_AlarmSubSecondValue, 
                                                               uint32_t RTC_AlarmSubSecondMask) 
uint32_t RTC_GetAlarmSubSecond(uint32_t RTC_Alarm);

RTC cycle wake-up correlation function:

void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock);
void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter);
uint32_t RTC_GetWakeUpCounter(void);
RTC_WakeUpCmd(DISABLE);//Turn off WAKE UP

RTC interrupt configuration and status related functions:

void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState);
FlagStatus RTC_GetFlagStatus(uint32_t RTC_FLAG);
void RTC_ClearFlag(uint32_t RTC_FLAG);
ITStatus RTC_GetITStatus(uint32_t RTC_IT);
void RTC_ClearITPendingBit(uint32_t RTC_IT);

RTC related constraint functions:

 void RTC_WriteProtectionCmd(FunctionalState NewState);//Unprotect write
 ErrorStatus RTC_EnterInitMode(void);//Enter configuration mode, RTC_ ISR_ The initf bit is set to 1
 void RTC_ExitInitMode(void)//Exit initialization mode.

Other related functions:

uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR);
void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)
void RTC_ITConfig(uint32_t RTC_IT, FunctionalState NewState);

4, General steps for RTC calendar configuration

  • ① Enable PWR clock:
  • ② Enable fallback register access:`
  • ③ Configure RTC clock source to enable RTC clock:
If used LSE,To open LSE: 
  • ④ Initialize RTC (synchronous / asynchronous frequency division coefficient and clock format):
RTC_Init ();`
  • ⑤ Set time:
 RTC_SetTime ();
  • ⑥ Set date:

RTC related detailed explanation is here!!!

Tags: Embedded system stm32 STM32F4

Posted on Sat, 25 Sep 2021 01:23:05 -0400 by sarabjit