From b02b2ac751670a7db99dd385bb0d9df39db8e06a Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Sun, 22 Oct 2023 23:43:20 +0200 Subject: [PATCH] stm32: Fix RTC based tick day change On starting next calendar day, result of computing time difference between current time and previous one was negative and should be corrected by whole day on sub seconds. It was corrected by one hour instead resulting in negative tick increment that caused crash at midnight. This change fixes issue by correcting time difference computation. --- hw/mcu/stm/stm32_common/src/hal_os_tick.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw/mcu/stm/stm32_common/src/hal_os_tick.c b/hw/mcu/stm/stm32_common/src/hal_os_tick.c index 4a989f076a..d56bc2dd68 100644 --- a/hw/mcu/stm/stm32_common/src/hal_os_tick.c +++ b/hw/mcu/stm/stm32_common/src/hal_os_tick.c @@ -156,7 +156,8 @@ rtc_update_time(void) now = rtc_time_to_sub_seconds(&alarm.AlarmTime); delta = now - last_rtc_time; if (delta < 0) { - delta += 3600 << SUB_SECONDS_BITS; + /* Day changed, correct delta */ + delta += (24 * 3600) << SUB_SECONDS_BITS; } alarm.AlarmTime.SubSeconds = alarm.AlarmTime.SecondFraction - (now & alarm.AlarmTime.SecondFraction); alarm.AlarmTime.SubSeconds -= sub_seconds_per_tick;