Skip to content

Commit

Permalink
counter: stm32 rtc: improve reading registers
Browse files Browse the repository at this point in the history
Synchronize reading two separate registers. In some edge cases the read
registers could point different dates.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
  • Loading branch information
niedzwiecki-dawid committed Aug 21, 2023
1 parent 5782ea0 commit 85f58f7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions drivers/counter/counter_ll_stm32_rtc.c
Expand Up @@ -192,9 +192,19 @@ static uint32_t rtc_stm32_read(const struct device *dev)
#endif
ARG_UNUSED(dev);

/* Read time and date registers */
rtc_time = LL_RTC_TIME_Get(RTC);
/* Read time and date registers. Make sure value of the previous register
* hasn't been changed while reading the next one.
*/
#if !defined(COUNTER_NO_DATE)
do {
rtc_date = LL_RTC_DATE_Get(RTC);

do {
rtc_time = LL_RTC_TIME_Get(RTC);
} while (rtc_time != LL_RTC_TIME_Get(RTC));

} while (rtc_date != LL_RTC_DATE_Get(RTC));
#else
rtc_date = LL_RTC_DATE_Get(RTC);
#endif

Expand Down

0 comments on commit 85f58f7

Please sign in to comment.