Internal RTC in STM32

Table of Contents

Introducing STM32 RTC

The RTC (Real-Time Clock) in STM32 microcontrollers is a highly configurable peripheral designed to keep track of the current time and date, even when the main system is powered down. It is especially useful in low-power applications where accurate timekeeping must be maintained with minimal energy consumption. The RTC can operate independently from the main processor using a dedicated low-speed clock (typically LSE or LSI), and it can retain its state across resets and power cycles using a backup power source such as a coin cell battery connected to the VBAT pin.

The RTC is often used in applications like data logging, alarms, time stamping, clock displays, and wake-up scheduling in low-power modes.

Key Features of STM32 RTC

  1. Time and Calendar Functions
    The RTC provides accurate tracking of time (hours, minutes, seconds) and calendar (date, month, year, day of the week) in either 24-hour or 12-hour format.
  2. Alarm Functionality
    Supports one or two independent alarms (Alarm A and Alarm B), which can trigger interrupts or wake up the device from low-power modes at a predefined time.
  3. Tamper and Timestamp Detection
    Capable of detecting external tamper events and logging timestamps, useful for applications requiring security or event tracking.
  4. Backup Domain and Battery Operation
    The RTC can run independently on a backup battery via the VBAT pin, ensuring continued timekeeping during power loss or system shutdown.

CUBEMX CONFIGURATION

Clock Configuration

Below is the image showing the clock configuration for this project.

I am using the Nucleo F446RE board and it has both, LSE and HSE crystals on board. The LSE is 32.768Khz and HSE is of 8MHz. The LSE crystal is used to provide the clock to the RTC whereas the HSE will be used to clock the rest of the system.

In the RCC configuration, enable the crystal oscillator for both LSE and HSE. The image above shows how the clock is configured such that the RTC gets the 32.768KHz, while the rest of the system runs at 180MHz.

If your controller board does not have this dedicated LSE crystal of 32.768KHz, you can use the Internal RC oscillator (LSI RC) too.

RTC Configuration

Below is the image showing the RTC configuration.

Activate the Clock SourceCalendar and enable at least one of the Alarm.

In the parameter configuration, I have set the 24Hr Time format and Data format is set to binary format. You can set any random values for the timedate and alarm. We will write functions to set up the values for these parameters in the code itself.

The Asynchronous and Synchronous Predivider values are used to calculate the value of the ck_spre. The values should be chosen in a way that the ck_spre value = 1. The formula to calculate ck_spre is shown below.

RTC ck_spre Formula

The RTCCLK is at 32768, so using the PREDIV_A value of 127 and PREDEV_S value of 255 in the above formula results in the ck_spre value =1.

We have also configured the Alarm, so make sure to enable the Alarm interrupt in the NVIC Tab. We will use the LED connected to pin PA5 for the Alarm signal, so let’s set the pin PA5 as output.

1 thought on “Internal RTC in STM32”

Leave a Comment

Your email address will not be published. Required fields are marked *

1 thought on “Internal RTC in STM32”

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

Interface GC9A01 Round Display with STM32

Learn how to interface the GC9A01 round TFT display with STM32 via SPI and implement LVGL for rich embedded GUIs. Step-by-step guide to wiring, code, and rendering graphics with LVGL. The project is available for download at the end of the post

Read More »

Interface SD Card via SPI (Polling & DMA)

Step-by-step guide to interface an SD card with STM32 using SPI and DMA. Tis tutorial covers CubeMX setup, FATFS init, SPI‑DMA config, and read/write file operations. The project is available to download at the end of the post.

Read More »

Internal RTC in STM32

Learn how to configure the built‑in RTC on STM32 using HAL and CubeMX. Includes setting time, date, alarms, LCD display, VBAT backup – step‑by‑step guide. The project is available for download at the end of the post.

Read More »
Scroll to Top