STM32: How to use RTC and Backup Registers Tutorial

STM32 HAL Tutorial: Timekeeping and Low-Power Data Retention

Abstract

Learn how to use the STM32 RTC (Real-Time Clock) and Backup Registers with HAL and CubeMX for timekeeping, alarms, and low-power applications.

1. Introduction

STM32 RTC allows your MCU to:

  • Keep accurate time even in low-power modes
  • Wake up the MCU via alarms or periodic events
  • Store critical data in backup registers during Stop or Standby mode

This is crucial for:

  • Battery-operated IoT devices
  • Data loggers
  • Energy-efficient sensors

By the end of this episode, you’ll be able to:

  1. Configure the STM32 RTC using CubeMX and HAL
  2. Set alarms and periodic wakeups
  3. Store and retrieve data from backup registers

2. Prerequisites

  • STM32 board with RTC support
  • STM32CubeIDE installed

Basic knowledge of low-power modes and HAL

3. RTC Overview

RTC runs on LSE (32.768 kHz crystal) or LSI. It is responsible for keeping track of:

  • Time (hours, minutes, seconds)
  • Date (day, month, year)

RTC can generate alarms, wakeup timers, and tamper events.

Backup Registers are responsible for retaining data even in Standby mode.Useful for storing system state or configuration

4. CubeMX Configuration

  1. Enable RTC under Peripherals → RTC
  2. Select Clock Source: LSE (external) or LSI (internal)
  3. Enable Backup Registers
  4. Optionally, configure Alarms or Wakeup Timer
  5. Generate initialization code

5. HAL Example: RTC Initialization

Configure prescalers to match clock source frequency

Adjust time/date with:

  • HAL_RTC_SetTime()
  • HAL_RTC_SetDate()

Basic RTC initialization: 

				
					RTC_HandleTypeDef hrtc;

void MX_RTC_Init(void)
{
    hrtc.Instance = RTC;
    hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
    hrtc.Init.AsynchPrediv = 127;
    hrtc.Init.SynchPrediv = 255;
    hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
    HAL_RTC_Init(&hrtc);
}

				
			

6. Using Alarms and Wakeup Timer

6.1 Set an Alarm

				
					RTC_AlarmTypeDef sAlarm = {0};
sAlarm.AlarmTime.Hours = 6;
sAlarm.AlarmTime.Minutes = 30;
sAlarm.AlarmTime.Seconds = 0;
sAlarm.Alarm = RTC_ALARM_A;
HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN);

				
			
  • When alarm triggers, MCU wakes from Stop or Standby mode

  • Implement callback:
				
					void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
    // Wake-up action (e.g., toggle LED, start measurement)
}

				
			

6.2 Set a Wakeup Timer

				
					HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 32768, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
				
			
  • Wakes MCU periodically (e.g., every 1 second)
  • Perfect for low-power sampling

Full Source Code: hackerembedded/STM32_EP24

7. Backup Registers

Store critical data:

				
					HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x12345678);
uint32_t value = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1);

				
			

Data persists through Stop and Standby modes

Ideal for configuration, counters, or last sensor readings

8. Hands-On Lab Example

1. Configure RTC with LSE as clock source

2. Set time and date

3. Configure wake-up timer to blink LED every second

4. Add a function to show the time every wake up (1second)

				
					/* USER CODE BEGIN 0 */
int __io_putchar(int ch)
{
	HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 100);
	return ch;
}
void RTC_TimeShow(uint8_t *showtime)
{
 RTC_DateTypeDef sdatestructureget;
 RTC_TimeTypeDef stimestructureget;
 /* Get the RTC current Time */
 HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
 /* Get the RTC current Date */
 HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
 /* Display time Format : hh:mm:ss */
 sprintf((char *)showtime, "%02d:%02d:%02d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
 printf("%s",showtime);
}
/* USER CODE END 0 */

				
			
				
					/* USER CODE BEGIN WHILE */
 while (1)
 {
   /* USER CODE END WHILE */
   /* USER CODE BEGIN 3 */
	  if(u8RTCWakeupFlag)
	  {
		  u8RTCWakeupFlag = 0;
		  printf("\r\nWakeUp RTC\r\n");
		  RTC_TimeShow(aShowTime);
	  }
 }
 /* USER CODE END 3 */

				
			

5. Optional: Store a counter in backup register to persist across Standby mode

6. Observe current reduction and data retention

Tip: Combine RTC + Backup Registers + Stop/Standby mode for ultra-low-power autonomous applications

9. Advantages of RTC and Backup Registers

  • Keep accurate time even in low-power modes
  • Enable periodic or scheduled wakeups for tasks
  • Persist critical data across MCU resets or Standby mode
  • Essential for energy-efficient IoT and data-logging applications

Leave a Comment

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

Scroll to Top