Page 1 of 1

RTC adjust?

Posted: Thu Jul 18, 2024 2:15 pm
by STM32ardui
Hi,

again I'm using a STM32G0B1CBT6-board.
At VBAT-pin two NiMH-rechargables are connected, so ≈ 2,6 V.

First I used the simpleRTC-example to see, how accuracy is with LSE crystal.

During the first 10 minutes RTC is 11 ms too slow - comparing to timestamp of PC.
I let it run over night for 12 hours and can estimates, that RTC will be 1,5 s too slow in 24 hours. That's 17,4 ppm and inside range from datasheet.


I like to adjust RTC.
Reference manual tells me something about CALP and CALM.
And I found HAL_RTCEx_SetSmoothCalib() inside stm32g0xx_hal_rtc_ex.c ...

But it looks like it has no effect.
RTC is never going faster.

Can I mix this HAL-function with normal STM32duino-code???
Return value is 0 (HAL_StatusTypeDef = HAL_OK) Another thing I have to serach for the right file ...


Code: Select all

#include <STM32RTC.h>
#include <stm32g0xx_hal_rcc_ex.h>

uint8_t  ret;         
RTC_HandleTypeDef  hrtc;
STM32RTC& rtc = STM32RTC::getInstance();
uint64_t  t = 0;



void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.print("\n\n ----- RTC smooth calibration mit STM32G0B1CBT6 ----- \n");

  rtc.setClockSource(STM32RTC::LSE_CLOCK);
  rtc.begin(false,STM32RTC::HOUR_24);

  ret = HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, 1, 500);
  Serial.printf("ret = %d \n",ret);
}

void loop() {
  if (millis() - t > 29999) {
    Serial.printf("%02d.%02d.%02d ", rtc.getDay(), rtc.getMonth(), rtc.getYear());
    Serial.printf("%02d:%02d:%02d.%03d\n", rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds());

    t = millis();
  }  
}

Re: RTC adjust?

Posted: Thu Jul 18, 2024 3:01 pm
by fpiSTM
Use this to get the correct handle:

Code: Select all

    // Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). Use at your own risk.
    RTC_HandleTypeDef *getHandle(void)
https://github.com/stm32duino/STM32RTC/ ... #L135-L136

Re: RTC adjust?

Posted: Thu Jul 18, 2024 6:02 pm
by STM32ardui
It looks like it works only partly!

With

Code: Select all

ret = HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, 0, 511)
RTC is 311 ms too slow in an intervall of 10 minutes.
So CALM seems to have an influence.

But

Code: Select all

ret = HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, 1, 0)
or

Code: Select all

ret = HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_8SEC, 1, 0)
changes nothing.
RTC is still too slow about 15 ms - that's nearly the same value like without adjustment.

CALP should have a stronger influence than CALM, but I can't get RTC a little bit faster.

=================================================================================

[Update]
I'm sure: the favourite word of all STM32-HAL-Programmers is SCHNITZELJAGD (paperchase) :!:

A normal human may think, xxx_RESET is 0 and xxx_SET is 1.
But not for STM32 MCUs.

For some chips it is

Code: Select all

#define RTC_SMOOTHCALIB_PLUSPULSES_SET   0x00008000U
For a STM32G0 it is

Code: Select all

#define RTC_SMOOTHCALIB_PLUSPULSES_SET    RTC_CALR_CALP
And where RTC_CALR_CALP is defined?
Who is the mastermind behind those insanity??? :roll:


Now with

Code: Select all

HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, RTC_SMOOTHCALIB_PLUSPULSES_SET, 0);
RTC is 283 ms faster during 10 minutes.