Nucleo-F446RE: a clock and alarm with the RTC
Bare-metal C on the STM32F446: set the RTC's calendar, sleep until its wake-up timer ticks each second, and light LD2 when the alarm goes off.
Firmware · Intermediate
In a real product: Bedside alarm clock (Consumer electronics): The RTC keeps the date and time on its own slow clock, so the CPU can sleep between seconds and still wake on time.
An RTC (real-time clock) is a small clock and calendar inside the chip. It runs from its own slow crystal, the 32.768 kHz LSE, and keeps the hours, minutes, seconds and date in registers. It can wake the CPU and raise an alarm, so a device can sleep nearly all the time and still know what time it is. This clock starts just before midnight on a leap day, prints the time each second, and lights LD2 when the alarm goes off.
What you'll learn
- Why the RTC is protected, and how
DBPand the twoWPRkeys open it - Setting the time in initialization mode, in BCD (0x23 means 23)
- Waiting for
RSFbefore trusting the time you read, and why readingTRholdsDR - An alarm with masks, and the wake-up timer, each reaching the CPU through its EXTI line
- Sleeping with
__WFI()until something happens
How it works
The RTC sits in the backup domain, which keeps its settings through a reset, so it is locked by default. Setting PWR->CR DBP allows writes to that domain. RCC->BDCR then starts the LSE, selects it as the RTC's clock and enables the RTC. Writing 0xCA and then 0x53 to RTC->WPR unlocks the RTC's own registers.
The calendar only takes a new time in initialization mode. INIT stops the counters and INITF says they have stopped. TR and DR hold BCD digits. Leaving initialization mode starts the clock again, and RSF says the registers you read have caught up. The two prescalers at their reset values divide 32,768 Hz by 128 and by 256, which is exactly one tick a second.
The wake-up timer counts WUTR + 1 ticks and then sets WUTF. With WUCKSEL at 100 it counts those seconds, so WUTR 0 wakes the CPU each second. The alarm compares ALRMAR with the time. A set MSK bit leaves that field out, so with the date masked it goes off once a day. Both signals reach the NVIC through EXTI: line 17 for the alarm, line 22 for the wake-up timer. Each handler must clear the RTC's flag and EXTI's pending bit.
Reading TR freezes DR until it is read, so a time and date read together always belong together, even across midnight.
Try this
- Change the alarm to 00:01:00. Remember the digits are BCD.
- Mask the seconds too (
RTC_ALRMAR_MSK1) and see the alarm go off every second. - Set
RTC_CR_FMTbefore setting the time and print a 12-hour clock with thePMbit. - Leave out the two
WPRwrites and read the warning: nothing you write to the RTC takes effect. - Add
NVIC_SystemReset()in the alarm handler. The time carries on through the reset, because the RTC is in the backup domain.
main.c
// Bare-metal C on the Nucleo-F446RE: no Arduino core and no HAL. CMSIS names every register.
// The RTC's calendar with its alarm and wake-up timer. The CPU sleeps between the seconds.
#include <stdio.h>
#include "stm32f4xx.h"
static volatile int tick; // the wake-up timer's second
static volatile int alarm; // the alarm went off
void RTC_WKUP_IRQHandler(void) {
RTC->ISR &= ~RTC_ISR_WUTF;
EXTI->PR = EXTI_PR_PR22;
tick = 1;
}
void RTC_Alarm_IRQHandler(void) {
RTC->ISR &= ~RTC_ISR_ALRAF;
EXTI->PR = EXTI_PR_PR17;
GPIOA->BSRR = GPIO_BSRR_BS5; // LD2 on
alarm = 1;
}
// printf sends each character here: USART2 on PA2, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
while (!(USART2->SR & USART_SR_TXE)) {}
USART2->DR = ch;
return ch;
}
static void rtc_start(void) {
// Open the backup domain, and run the RTC from the 32.768 kHz LSE.
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
PWR->CR |= PWR_CR_DBP;
RCC->BDCR |= RCC_BDCR_LSEON;
while (!(RCC->BDCR & RCC_BDCR_LSERDY)) {}
RCC->BDCR |= RCC_BDCR_RTCSEL_0 | RCC_BDCR_RTCEN;
// Unlock the RTC's registers, and stop the counters to set the time.
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
RTC->ISR |= RTC_ISR_INIT;
while (!(RTC->ISR & RTC_ISR_INITF)) {}
RTC->TR = 0x235955; // 23:59:55, in BCD
RTC->DR = (0x24u << 16) | (4u << 13) | (0x02u << 8) | 0x29; // 2024-02-29, a Thursday
RTC->ISR &= ~RTC_ISR_INIT;
RTC->ISR &= ~RTC_ISR_RSF;
while (!(RTC->ISR & RTC_ISR_RSF)) {}
// The alarm at 00:00:05 on any day: the date is masked.
RTC->ALRMAR = RTC_ALRMAR_MSK4 | 0x000005;
// The wake-up timer: one count of the 1 Hz clock.
RTC->WUTR = 0;
RTC->CR |= RTC_CR_WUCKSEL_2 | RTC_CR_ALRAIE | RTC_CR_ALRAE | RTC_CR_WUTIE | RTC_CR_WUTE;
RTC->WPR = 0xFF; // locked again
// Each one reaches the NVIC through its EXTI line, on a rising edge.
EXTI->RTSR |= EXTI_RTSR_TR17 | EXTI_RTSR_TR22;
EXTI->IMR |= EXTI_IMR_MR17 | EXTI_IMR_MR22;
NVIC_EnableIRQ(RTC_Alarm_IRQn);
NVIC_EnableIRQ(RTC_WKUP_IRQn);
}
int main(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER2 | GPIO_MODER_MODER5)) | GPIO_MODER_MODER2_1 | GPIO_MODER_MODER5_0;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8); // PA2: USART2 TX, AF7
USART2->BRR = SystemCoreClock / 115200;
USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
rtc_start();
while (1) {
__WFI(); // asleep until an interrupt
if (alarm) {
alarm = 0;
printf("Alarm!\r\n");
}
if (tick) {
tick = 0;
uint32_t time = RTC->TR; // read first: it holds DR for us
uint32_t date = RTC->DR;
printf("20%02lx-%02lx-%02lx %02lx:%02lx:%02lx\r\n", (date >> 16) & 0xFF, (date >> 8) & 0x1F, date & 0x3F,
(time >> 16) & 0x3F, (time >> 8) & 0x7F, time & 0x7F);
}
}
}