stm32-rs / stm32f1xx-hal

A Rust embedded-hal HAL impl for the STM32F1 family based on japarics stm32f103xx-hal
Apache License 2.0
564 stars 177 forks source link

RTC 'calibration' #469

Open Rutherther opened 1 year ago

Rutherther commented 1 year ago

It's possible that if LSE crystal is used, the crystal won't have the exact frequency of 32.768 kHz. It looks to me that Rtc::select_frequency is not enough, as it allows only to set whole number frequencies. If I want to have RTC with 1 Hz, and the clock gets ahead by a second or two per day, the prescaler for RTC should be different than 1 Hz based precisely on 32.768 kHz. (should not be 32.768 / 1 - 1)

This HAL currently does not support adding or subtracting from the prescaler. This may be solved by writing to prlh and prll directly, but I would say that implementing support for some kind of a calibration like this is not out of scope of this HAL and could be implemented in Rtc.

What do you think about this? And what do you think the implementation should be? The best I came up with is making something like Rtc::select_precise_frequency(&mut self, frequency: Hertz, adjustment: u32), where adjustment would just be added to the prescaler.

I am up to PR this, but I am unsure 1. if this is in the scope of this HAL, 2. if the implementation I proposed makes sense, maybe it could be done better