stm32duino / STM32RTC

Arduino RTC library for STM32.
135 stars 49 forks source link

Verification that prescaler values are configured should be added #71

Closed jan019 closed 2 years ago

jan019 commented 2 years ago

RTC should contain an assertion checking if the prescaler values are configured or not. Or check if configured and if not, call the function to compute the values.

This portion of code https://github.com/stm32duino/STM32RTC/blob/95b7514045237495364b62d4faf23d484017640a/src/rtc.c#L382-L398 is executed only when the device was previously unpowered -> RTC is not initialized. For the case when RTC is not initialized the function https://github.com/stm32duino/STM32RTC/blob/95b7514045237495364b62d4faf23d484017640a/src/rtc.c#L391 is called and prescaler values are computed.

In case only a soft reset is performed, the RTC remains initialized, the above mentioned code is not executed and prescaler values are not calculated. Therefore they remain set to -1. Then when setting and RTC alarm with subseconds enabled, the following issue occurs:

This leads to reserved bits in the register being written to 1 and generally incorrect behaviour. This is not easy to debug and just a note in the readme that the function setPrediv() must be called before begin() can easly be missed.

The examples also don't call this function, which makes the issue even more hidden. Maybe some boards ensure that invalid values are not written to registers and this issue is not observed on them.

ABOSTM commented 2 years ago

@jan019 thanks for reporting this issue, and for your detailed analysis. I proposed a fix in PR #72, feel free to review it and tests it

@rozrabiak this PR should also fix your issue #70