cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F100RB Clock Skew In RTC and HAL_GetTick

ISeed
Senior

Hello, all.

I'm using the STM32F100RB , mounted on the STM32VL Discovery board.

The clock confioguration, done in STM32CUBEMX is supplied at the end of the post.

I wanted to use HAL_GetTick to get approximate millisecond resolution for the RTC.

After some tests, I noticed clock skews, so I ran a test for 1000 RTC seconds. (during which I constattly polled HAL_RTC_GetTime and HAL_RTC_GetDate).

These are the times I discovered:

Actual Time: 1,009,129 ms

RTC Time: 1,000 s

HAL_GetTick Time: 1,009,217 ms

So while the HAL_GetTick was pretty close (but still has a sizeable skew of almost 100 ms after 1000 seconds, which is 8.6 seconds a day), The RTC was way off, accumulating a deficit of 9 seconds in that span.

So, I have 3 questions:

  1. How do I fix the RTC clock skew?
  2. How do I fix the HAL_GetTick clock skew?
  3. IS there a better way to calculate milliseconds for RTC?

The STM32CUBEMX Clock Configuration. is attached below.

Thanks in advance!

0690X000009YJk7QAG.jpg

3 REPLIES 3

LSI is innacurate. Use LSE for RTC.

JW

Thanks!

I switched to LSE for RTC.

There was indeed an improvement. The Actual Time and RTC Time seem to be in a volatile lockstep (I will run it for a while to make sure).

HAL_GetTick also showed improvement. however, it seems to add 1 ms for each 10 seconds. If I'm not using it for RTC milliseconds, it's quite good.

Follow-up: I ran it for an hour. It's stable. 1 RTC second is 0.999972316 actual seconds, and HAL_GetTick second is 0.9999096 actual seconds.

  • Is there a way to make RTC even more percise?
  • is there a way to calculate milliseconds for RTC?

The F1 is pretty antiquated. You might be able to get to one of the prescaled counters, and use that for sub-ms

The prescale/dividers can be adjusted. The LSE is typically 32768 Hz, the LSI is 37-40 KHz.

Only LSE will run from battery.

Newer STM32 frequently have an HSE clocked TIM where you can measure period(s) of the LSI and LSE clock sources.

For more precise timing I'd be using the DWT CYCCNT rather than SysTick/HAL_GetTick, the latter is very crude.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..