Skip to main content
JKing.8
Associate II
February 14, 2023
Solved

Stop STM32 from rerunning code when repowered

I am using an STM32H753I-EVAL2. I have the real-time clock(RTC) counting. I want it to be set to the current time. I am successfully able to do this inside of MX_RTC_Init() with:

RTC_TimeTypeDef sTime = {0}; 
sTime.Hours = 0x11;
sTime.Minutes = 0x22;
sTime.Seconds = 0x33;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) {
 Error_Handler();
}

I configured the H7 such that the RTC continues to count when the main power is disconnected, through VBAT. The issue is that STM reruns all of the code when it is repowered, causing the time to be reset to where it started. Is there any way to avoid it rerunning all code? Thanks

This topic has been closed for replies.
LCE
LCEBest answer
Principal III
February 15, 2023

Put a variable in "no-init" RAM or in flash / eeprom.

 /* define NoInitSection in linker script, or store variable in flash / eeprom */
uint32_t u32RtcSet __attribute__((section(".NoInitSection"))); 
 
/* on start check variable */
if( 0xD0ACCE55 != u32RtcSet )
{
 RTC_init();
 u32RtcSet = 0xD0ACCE55;
}

5 replies

LCE
LCEBest answer
Principal III
February 15, 2023

Put a variable in "no-init" RAM or in flash / eeprom.

 /* define NoInitSection in linker script, or store variable in flash / eeprom */
uint32_t u32RtcSet __attribute__((section(".NoInitSection"))); 
 
/* on start check variable */
if( 0xD0ACCE55 != u32RtcSet )
{
 RTC_init();
 u32RtcSet = 0xD0ACCE55;
}

Javier1
Principal
February 15, 2023

nice one

hit me up in https://www.linkedin.com/in/javiermuñoz/
waclawek.jan
Super User
February 16, 2023

Variable in RAM, in whichever section, won't "survive" VDD off (while RTC is running from VBAT).

The simplest and also probably the best thing to do is to avoid repeated RTC start/setup by looking at RCC_BDCR.RTCEN or equivalent. If it's set, RTC is already set and running, so no need to set and adjust it again.

Some look at INITS flag in RTC, but it's not always the best idea. Others write a particular value into the backup registers, but that is not necessary and wastes that register.

JW