STM32 RTC, Calendar.

Let's start with the fact that RTC is an abbreviation that stands for Real-time clock. In the old days, when I used AVR MCUs, I used a separate chip as the RTC and communicated with it over a specific protocol. On STM32, however, the RTC is a module implemented inside the MCU.

On STM32 the RTC has the following capabilities:
  • Automatic wakeup in all low-power modes
  • An independent BCD timer/counter. Timekeeping and the calendar are implemented in hardware, with configurable alarm interrupts.
  • A software wakeup flag with the ability to trigger an interrupt.
  • Two 32-bit registers that store seconds, minutes, hours (in 12-hour or 24-hour format), day of the week, day of the month, month and year. Seconds are stored in binary format, everything else in BCD.
  • Compensation for month length (and leap years) is performed automatically. Daylight saving time compensation is also possible.
  • Two 32-bit programmable alarm registers.
  • Calibration to compensate for crystal resonator deviation.
  • After a reset, the RTC domain is protected against accidental writes.
  • As long as the RTC supply voltage remains within the operating range, it keeps running regardless of the operating mode (Run mode, low-power mode or under reset).


And also some features I'd like to mention:
  • To improve accuracy, the clock can be synchronized to the 50 or 60Hz mains.
  • It can record the time of an event, the so-called Time-stamp.
  • Tamper detection (tamper), which resets all backup registers.

Events that can trigger interrupts:
  • Alarm A
  • Alarm B
  • Wakeup interrupt
  • Time-stamp
  • Tamper detection

As for power, for continuous operation the clock needs a separate power source, which is often a battery connected to the VBAT pin.

As an example, let's look at how to start the clock and set up the calendar, but first let's look at the internal structure of the RTC.
STM32 RTC, Calendar.

The RTC module can be clocked from one of three sources:
  • LSI — a low-frequency internal 40 KHz RC oscillator.
  • LSE — an external 32 768 Hz "watch" crystal.
  • HSE/32 — an external high-frequency crystal with a prescaler of 32.


The clock source is selected with the RTCSEL[1:0] bit field in the RCC_BDCR register
STM32 RTC, Calendar.

LSEON(LSE oscillator enable) — enables the LSE.

LSERDY(LSE oscillator ready) — the LSE ready flag.

LSEBYP(LSE oscillator bypass) — a one in this bit allows the RTC to be clocked from an external source. The external clock signal (square, sine or triangle) with a 50% duty cycle is applied only to the OSC32_IN pin, and the OSC32_OUT pin can then be used as GPIO.

LSEDRV LSE[1:0](oscillator drive capability) — sets the crystal drive level.
  • 00: low
  • 01: medium low
  • 10: medium high
  • 11: high, the default value


RTCSEL[1:0]( RTC clock source selection) — selects the clock source for RTCCLK.
  • 01: LSE is used as the source.
  • 10: LSI is used as the source.
  • 11: HSE/32 is used as the source.

RTCEN(RTC clock enable) — a one in this bit enables RTC clocking.

BDRST(Backup domain software reset) — a one in this bit fully resets the Backup domain.

Once the clock source has been selected, you need to make sure that the RTC counter registers are clocked at 1Hz. Two prescalers, synchronous and asynchronous, are provided to reduce the clock frequency to 1Hz.
STM32 RTC, Calendar.

Their values are calculated using the formula.
STM32 RTC, Calendar.

By default PREDIV_A = 127 and PREDIV_S = 255, which gives 1 Hz from 32768 Hz.

One 32-bit register, divided into bit fields, is used to set the time.
STM32 RTC, Calendar.

The data in it is stored in BCD format, with units and tens in separate bit fields. I think only the PM bit, which is responsible for the hour format, needs explanation. If it is zero, the hours will tick from 0 to 24; if it is one, from 1 to 12.

The date works the same way: it is stored in a 32-bit register divided into bit fields.
STM32 RTC, Calendar.

Below is the code for configuring and starting the clock on the STM32F303.

#include "stm32f3xx.h"

//define a new type that contains a structure with bit fields
typedef struct sRTC_struct{
		 unsigned year : 6; 
		 unsigned month : 4;
		 unsigned week : 3; 
		 unsigned date : 5;
		 unsigned hour : 5;
		 unsigned minute :6;
		 unsigned sec : 6;
}RTC_struct;
//create an instance of this type
volatile RTC_struct rtc;

void RTC_Init(RTC_struct volatile *value)
{
	// Enable LSI
	RCC->CSR |= RCC_CSR_LSION;
	// Wait until it is ready
	while ( !(RCC->CSR & RCC_CSR_LSIRDY) ){}
				  
	// Enable clocking of the PWR module
	RCC->APB1ENR |= RCC_APB1ENR_PWREN;
	// After a reset, the RTC registers are in a protected domain
	// To enable writing, set the flag in PWR->CR
	PWR->CR |= PWR_CR_DBP;
	
	// Select the RTC clock source (the low-speed internal LSI source (40 kHz))
	RCC->BDCR |= RCC_BDCR_RTCSEL_LSI;
	// Enable RTC clocking
	RCC->BDCR |= RCC_BDCR_RTCEN;
	
	RTC_Update(value);	
}

void RTC_Update(RTC_struct volatile *value)
{
	uint32_t TR, DR;
	// Disable write protection
	RTC->WPR = 0xCA;
	RTC->WPR = 0x53;
	
	// Enter initialization mode
	RTC->ISR |= RTC_ISR_INIT;
	// Wait for confirmation of entering initialization mode
	while( !(RTC->ISR & RTC_ISR_INITF) ){};   
	
	// Set the asynchronous prescaler to 100(99+1).
	RTC->PRER = (uint32_t)(99 << 16);
	// Set the synchronous prescaler to 400(399+1).
	RTC->PRER |= (uint32_t)399;
	
	// Set the time
	TR = ( ((uint32_t)(value->hour/10*16 + value->hour%10) << 16) | ((uint32_t)(value->minute/10*16 + value->minute%10) << 8) | ((uint32_t)value->sec/10*16 + value->sec%10) );
	RTC->TR = TR;
	// Set the date
	DR = (uint32_t)(value->year/10*16 + value->year%10) << 16  | (uint32_t)(value->week/10*16 + value->week%10) << 13 | (uint32_t)(value->month/10*16 + value->month%10) << 8 | (uint32_t)(value->date/10*16 + value->date%10);
	RTC->DR = DR;
	
	// Exit initialization mode
	RTC->ISR &= ~(RTC_ISR_INIT);
	// Enable write protection
	RTC->WPR = 0xFF;
}		

If the RTC is clocked from an external crystal, the LSI initialization must be replaced with the following code.

		//Check whether the clock is ticking
                if(RTC->ISR & RTC_ISR_INITS) return;
                
               //Enable LSE
		RCC->BDCR |= RCC_BDCR_LSEON;  
		// Wait until it is ready
		while (!(RCC->BDCR & RCC_BDCR_LSEON)){} 
	       //Select LSE as the source (32768 crystal) 
		RCC->BDCR |= RCC_BDCR_RTCSEL_LSE;
               //Enable RTC clocking
		RCC->BDCR |= RCC_BDCR_RTCEN;