From 9e202e57e9d2b4b3e806426353d6e49f9e1a4c3d Mon Sep 17 00:00:00 2001 From: Johan Lafon Date: Mon, 11 Sep 2023 16:49:11 +0200 Subject: [PATCH] drivers: rtc: stm32: avoid potential erroneous readings As shadow registers bypass is enabled, an erroneous reading may occur at each day or second increment. This commit fixes this issue. Signed-off-by: Johan Lafon --- drivers/rtc/rtc_ll_stm32.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/rtc/rtc_ll_stm32.c b/drivers/rtc/rtc_ll_stm32.c index c95c331ee97..fccbe30f1ce 100644 --- a/drivers/rtc/rtc_ll_stm32.c +++ b/drivers/rtc/rtc_ll_stm32.c @@ -182,10 +182,19 @@ static int rtc_stm32_get_time(const struct device *dev, struct rtc_time *timeptr uint32_t rtc_date, rtc_time, rtc_subsecond; - /* Read subsecond, time and date registers */ - rtc_subsecond = LL_RTC_TIME_GetSubSecond(RTC); - rtc_time = LL_RTC_TIME_Get(RTC); - rtc_date = LL_RTC_DATE_Get(RTC); + do { + /* read date, time and subseconds and relaunch if a day increment occurred + * while doing so as it will result in an erroneous result otherwise + */ + rtc_date = LL_RTC_DATE_Get(RTC); + do { + /* read time and subseconds and relaunch if a second increment occurred + * while doing so as it will result in an erroneous result otherwise + */ + rtc_time = LL_RTC_TIME_Get(RTC); + rtc_subsecond = LL_RTC_TIME_GetSubSecond(RTC); + } while (rtc_time != LL_RTC_TIME_Get(RTC)); + } while (rtc_date != LL_RTC_DATE_Get(RTC)); timeptr->tm_year = TM_TO_RTC_OFFSET + __LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_YEAR(rtc_date)); /* tm_mon allowed values are 0-11 */