How to: RTC clock

Real Time Clock (RTC) is a hardware peripheral in STM32 microcontroller. It is used in scheduler for task planning and also can measure real time. You can save date and time into its hardware registers and the clock is running even if you reflash or reset the processor.

Because the STM32 in LQFP48 package does not have battery backup pin, you have to keep connected at least single source of power if you would like to keep the RTC counting. So if you need to change the batterry module you can keep the Core Module connected over USB to keep the RTC running.

Core Module has 32 768 Hz crystal which is connected to the RTC peripheral.

RTC Structure

RTC structure contains year, month, date, day of the week. For time there are hours, minutes, seconds. If you read the RTC then the timestamp is fileld with proper UNIX timestamp.

 1//! @brief RTC date and time structure
 2typedef struct
 4    uint8_t seconds;     //!< Seconds parameter, from 00 to 59
 5    uint16_t subseconds; //!< Subsecond downcounter. When it reaches zero, it's reload value is the same as @ref RTC_SYNC_PREDIV
 6    uint8_t minutes;     //!< Minutes parameter, from 00 to 59
 7    uint8_t hours;       //!< Hours parameter, 24Hour mode, 00 to 23
 8    uint8_t week_day;    //!< Day in a week, from 1 to 7
 9    uint8_t date;        //!< Date in a month, 1 to 31
10    uint8_t month;       //!< Month in a year, 1 to 12
11    uint8_t year;        //!< Year parameter, 00 to 99, 00 is 2000 and 99 is 2099
12    uint32_t timestamp;  //!< Seconds from 01.01.1970 00:00:00
13} twr_rtc_t;


Initialization of RTC is automatic. The RTC is initialized everytime the YEAR register is set to zero. So if you use just the time registers you should set at least value 1 to the year register. This way the RTC is not reinitialized after reset which has a side-effect of stopping RTC counter for a second.

Year register

Year register can hold values 2000 to 2099.

Set date and time

 1twr_rtc_t rtc;
 3rtc.hours = 11;
 4rtc.minutes = 26;
 5rtc.seconds = 00;
 7rtc.year = 2019;
 8rtc.month = 5; = 16;

Read date and time

1twr_rtc_t datetime;
3twr_log_debug("$DATE: \"%d-%02d-%02dT%02d:%02d:%02dZ\"", datetime.year, datetime.month,, datetime.hours, datetime.minutes, datetime.seconds);