2025-10-03 6:44 AM
I need to calibrate STM32l431 RTC in software for using LSI clock.
When I compare STM32 RTC with GPS reference I get the following
GPS 1759492828 1759493307 => RTC 1759500022 1759500494
Diff GPS: 479 seconds
Diff STM32: 472 seconds
=> 7 seconds = 1.48% difference and
Prefered frequency to RTC 32473
I thought smooth calibration could be used for this kind of calibration but it seems it can only calibrate small changes for crystal.
Any ideas how this can be done? I cannot modify PCB for external pulses, I need to correct in software.
Clock will be syncronised 8, 16 and 24 hours interval, so accuracy does not need to be like a crystal.
2025-10-03 6:55 AM - edited 2025-10-03 6:58 AM
LSI is NOT 32768 but 32000 Hz
You have to change pre-scaler in RTC. I guess async prescaler should be 124 (count 125) instead of 127 (count 128).
Print RTC actual registers and let us discuss, i do not have any L4 to make a screenshot.
Please mark as solution if it works for you.
2025-10-03 7:02 AM - edited 2025-10-03 7:04 AM
Yes LSI frequency is 32000 Hz.
This is RTC init:
/* Configure RTC */
RTCHandle.Instance = RTC;
/* Set the RTC time base to 1s */
/* Configure RTC prescaler and RTC data registers as follow:
- Hour Format = Format 24
- Asynch Prediv = Value according to source clock
- Synch Prediv = Value according to source clock
- OutPut = Output Disable
- OutPutPolarity = High Polarity
- OutPutType = Open Drain */
RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
RTCHandle.Init.AsynchPrediv = 127;
RTCHandle.Init.SynchPrediv = 255;
RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&RTCHandle);
LL_RTC_TIME_SetFormat(RTC, LL_RTC_HOURFORMAT_24HOUR);
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BIN, &time);
LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BIN, &date);
rtcSet = false;
I would like to calculate the prescaler based om measured GPS diff, since it will be different on different CPU and different temperatures
2025-10-03 7:06 AM
As in previous mail:
your dividers are (127 + 1) * (255 + 1) = 32768 !
if you want to use 32kHz, move async divider to 124 (count 125) and get:
dividers (124 + 1) * (255 + 1) = 32000.
Than you can trim.
2025-10-03 7:08 AM
Aha now I get it!!!
Thanks for notifying me on dividers!!!!
I will try this!
2025-10-03 7:17 AM
Don't foget to mark as an accepted solution - will help others !