2026-03-10 1:14 PM - last edited on 2026-03-11 1:06 AM by mƎALLEm
ST moderator has edited this post to be compliant with the community rules. In next time please use </> button to paste your code. Thank you for your undersranding.
Hello collegues.
LL library.
Can you help with RTC domain in STM32C031T6. Problem is that, RTC start and counting, I set alarm A evry 1s. This wokrs fine. I'm counting moreless about 30min. After push the pin RESET, RTC starts counting from 0, I want to continue counting ? Initialization code below:
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
LL_RTC_InitTypeDef RTC_InitStruct = {0};
LL_RTC_TimeTypeDef RTC_TimeStruct = {0};
LL_RTC_DateTypeDef RTC_DateStruct = {0};
LL_RTC_AlarmTypeDef RTC_AlarmStruct = {0};
if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE)
{
FlagStatus pwrclkchanged = RESET;
if (LL_APB1_GRP1_IsEnabledClock (LL_APB1_GRP1_PERIPH_PWR) != 1U)
{
/* Enables the PWR Clock and Enables access to the backup domain */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
pwrclkchanged = SET;
}
//LL_RCC_ForceBackupDomainReset();
//LL_RCC_ReleaseBackupDomainReset();
LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_LOW);
LL_RCC_LSE_Enable();
/* Wait till LSE is ready */
while(LL_RCC_LSE_IsReady() != 1)
{
}
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
/* Restore clock configuration if changed */
if (pwrclkchanged == SET)
{
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_PWR);
}
}
/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTC);
LL_RCC_EnableRTC();
/* RTC interrupt Init */
NVIC_SetPriority(RTC_IRQn, 0);
NVIC_EnableIRQ(RTC_IRQn);
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
RTC_InitStruct.AsynchPrescaler = 127;
RTC_InitStruct.SynchPrescaler = 255;
LL_RTC_Init(RTC, &RTC_InitStruct);
RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_MONDAY;
RTC_DateStruct.Month = LL_RTC_MONTH_JANUARY;
RTC_DateStruct.Day = 0x1;
RTC_DateStruct.Year = 0x26;
LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_DateStruct);
if(LL_RTC_IsActiveFlag_INITS(RTC) == 0){
RTC_TimeStruct.Hours = 0x0;
RTC_TimeStruct.Minutes = 0x0;
RTC_TimeStruct.Seconds = 0x0;
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
}
/** Enable the Alarm A
*/
RTC_AlarmStruct.AlarmTime.Hours = 0x0;
RTC_AlarmStruct.AlarmTime.Minutes = 0x0;
RTC_AlarmStruct.AlarmTime.Seconds = 0x0;
RTC_AlarmStruct.AlarmMask = LL_RTC_ALMA_MASK_ALL;
RTC_AlarmStruct.AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
RTC_AlarmStruct.AlarmDateWeekDay = 0x1;
LL_RTC_ALMA_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_AlarmStruct);
/* USER CODE BEGIN RTC_Init 2 */
/*Enable alram interaput, set 1s*/
LL_RTC_DisableWriteProtection(RTC);
LL_RTC_ALMA_Enable(RTC);
LL_RTC_EnableIT_ALRA(RTC);
LL_RTC_EnableWriteProtection(RTC);
}
A bit os code from main.c:
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
/* SysTick_IRQn interrupt configuration */
NVIC_SetPriority(SysTick_IRQn, 3);
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_RTC_Init();
MX_USART2_UART_Init();
LL_SYSTICK_EnableIT();
2026-03-11 6:23 AM
Hello @Tomek2026
Please refer to the knowledge article below:
How to use the STM32’s backup registers - STMicroelectronics Community
2026-03-11 1:03 PM - last edited on 2026-03-12 2:51 AM by mƎALLEm
Again please use </> button to share your code.
Hello @Saket_Om Thank for your attention and help
In main.c file I added this code:
if(LL_PWR_ReadReg(BKP0R) != 0x5F5F){
LL_PWR_WriteReg(BKP0R, 0x5F5F);
}
else
{
LL_GPIO_SetOutputPin(LD4_GREEN_LED_GPIO_Port, LD4_GREEN_LED_Pin);
}24.3.9 Resetting the RTC
The calendar shadow registers (RTC_SSR, RTC_TR and RTC_DR) and some bits of the
RTC status register (RTC_ICSR) are reset to their default values by all available system
reset sources.
On the contrary, the following registers are reset to their default values by a Power-on reset
and are not affected by a system reset: the RTC current calendar registers, the RTC control
register (RTC_CR), the prescaler register (RTC_PRER), the RTC calibration register
(RTC_CALR), the RTC shift register (RTC_SHIFTR), the RTC timestamp registers
(RTC_TSSSR, RTC_TSTR and RTC_TSDR) and the alarm A registers
(RTC_ALRMASSR/RTC_ALRMAR.
In addition, when clocked by LSE, the RTC keeps on running under system reset if the reset
source is different from the Power-on reset one (refer to RCC for details about RTC clock
sources not affected by system reset). When a Power-on reset occurs, the RTC is stopped
and all the RTC registers are set to their reset values.
2026-03-12 2:29 AM
NRST-pin-reset should not reset the backup domain. You can check that in debugger, by placing a breakpoint at the entry point to main() and checking RTC registers there.
Which part of your program - and by that I mean also any Cube/HAL you've included - resets RTC, you find out by stepping through the program and checking the RTC registers.
JW
2026-03-12 11:25 AM
@waclawek.jan @Saket_Om (Normally I'm working on VSC with offcia plug-in)
I set a simple configuration as possible. LL library (STM32 CiubeIDE):
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
LL_RTC_InitTypeDef RTC_InitStruct = {0};
if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE)
{
FlagStatus pwrclkchanged = RESET;
if (LL_APB1_GRP1_IsEnabledClock (LL_APB1_GRP1_PERIPH_PWR) != 1U)
{
/* Enables the PWR Clock and Enables access to the backup domain */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
pwrclkchanged = SET;
}
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_LOW);
LL_RCC_LSE_Enable();
/* Wait till LSE is ready */
while(LL_RCC_LSE_IsReady() != 1)
{
}
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
/* Restore clock configuration if changed */
if (pwrclkchanged == SET)
{
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_PWR);
}
}
/* Peripheral clock enable */
LL_RCC_EnableRTC();
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTC);
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
RTC_InitStruct.AsynchPrescaler = 127;
RTC_InitStruct.SynchPrescaler = 255;
LL_RTC_Init(RTC, &RTC_InitStruct);
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
In main.c I have:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
/* SysTick_IRQn interrupt configuration */
NVIC_SetPriority(SysTick_IRQn, 3);
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
__NOP();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
First photo, RTC start and counting:
Second photo, immediately after PIN RESET ( register are cleared, counting is stopped and start from beginning):
Before first brake-point, register are cleared( I didn't even call MX_RTC_init):
So, could you check, what happend and why I cann't achive continues counting after RESET or what I should set (bit register, other things)?
Thank you for your engagement in my case
Best Regards Tomek
2026-03-13 2:30 AM - edited 2026-03-13 2:31 AM
When inserting code, use the </> icon from the top of editor pane. The Moderator told you so in your initial post.
The 'C0 implements the RCC_APBENR1.RTCAPBEN bit, disabling the APB interface after reset, so you won't be able to read the RTC registers in debugger until that bit is set. You can set it in debugger, too.
JW
2026-03-13 4:59 AM
I'm sorry for a view code, I didn't notice
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
LL_RTC_InitTypeDef RTC_InitStruct = {0};
if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE)
{
FlagStatus pwrclkchanged = RESET;
if (LL_APB1_GRP1_IsEnabledClock (LL_APB1_GRP1_PERIPH_PWR) != 1U)
{
/* Enables the PWR Clock and Enables access to the backup domain */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
pwrclkchanged = SET;
}
LL_RCC_ForceBackupDomainReset();
LL_RCC_ReleaseBackupDomainReset();
LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_LOW);
LL_RCC_LSE_Enable();
/* Wait till LSE is ready */
while(LL_RCC_LSE_IsReady() != 1)
{
}
LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
/* Restore clock configuration if changed */
if (pwrclkchanged == SET)
{
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_PWR);
}
}
/* Peripheral clock enable */
LL_RCC_EnableRTC();
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTC);
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
RTC_InitStruct.AsynchPrescaler = 127;
RTC_InitStruct.SynchPrescaler = 255;
LL_RTC_Init(RTC, &RTC_InitStruct);
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}in main.c file:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
/* SysTick_IRQn interrupt configuration */
NVIC_SetPriority(SysTick_IRQn, 3);
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
__NOP();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Function SystemClock_Config()
void SystemClock_Config(void)
{
LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
/* HSE configuration and activation */
LL_RCC_HSE_Enable();
while(LL_RCC_HSE_IsReady() != 1)
{
}
/* LSE configuration and activation */
LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_LOW);
LL_RCC_LSE_Enable();
while(LL_RCC_LSE_IsReady() != 1)
{
}
/* Set AHB prescaler*/
LL_RCC_SetAHBPrescaler(LL_RCC_HCLK_DIV_1);
/* Sysclk activation on the HSE */
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSE);
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSE)
{
}
/* Set APB1 prescaler*/
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
LL_Init1msTick(48000000);
/* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
LL_SetSystemCoreClock(48000000);
}
@waclawek.jan I'm not following you. Look at picture:
Are talking about this register and bit DBG EN should be set?
In referance manual for STM32_C031 is a bit unclear on this, so I'm looking for some clarification from your side.
Thank you advance for your help
Tomek
2026-03-13 5:26 AM
After setting this bit the debugger should be able to display content of RTC registers (you may need to refresh somehow the display, I don't use the debugger you do so don't know), thus judge whether time is already changed or not. If not, step/run to next breakpoint, until it does.
JW
2026-03-13 7:57 AM
I tested also STM32G474, and the RTC domain behavior as expacted, the counting of time is continued, look at picture 1:
time in RTC-.TR register:
after pin RESET
picture 2, after pin RESET, the value in RTC is keeping and counting is continued
So, you on STM32G474 works fine after pin RESET but if in STM32C031 not working (acuuracy is enough for me), I don't what to you external device like DS3231.
Have nice weekend.
Thank you for your help
Tomek
2026-03-13 9:18 AM
Have you tried to set RCC_APBENR1.RTCAPBEN bit in the 'C0 in debugger, at the first breakpoint, to be able to observe the RTC registers?
JW