summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--board/zinger/runtime.c4
-rw-r--r--chip/stm32/clock-f.c33
-rw-r--r--chip/stm32/clock-f.h4
-rw-r--r--chip/stm32/clock-stm32f0.c3
4 files changed, 26 insertions, 18 deletions
diff --git a/board/zinger/runtime.c b/board/zinger/runtime.c
index 44a93b346a..f92b919398 100644
--- a/board/zinger/runtime.c
+++ b/board/zinger/runtime.c
@@ -145,7 +145,6 @@ uint32_t task_wait_event(int timeout_us)
uint32_t evt;
timestamp_t t0, t1;
struct rtc_time_reg rtc0, rtc1;
- int rtc_diff;
t1.val = get_time().val + timeout_us;
@@ -190,8 +189,7 @@ uint32_t task_wait_event(int timeout_us)
/* fast forward timer according to RTC counter */
reset_rtc_alarm(&rtc1);
- rtc_diff = get_rtc_diff(&rtc0, &rtc1);
- t0.val = t0.val + rtc_diff;
+ t0.val += get_rtc_diff(&rtc0, &rtc1);
force_time(t0);
}
diff --git a/chip/stm32/clock-f.c b/chip/stm32/clock-f.c
index 99a23ee138..9c73e2b41f 100644
--- a/chip/stm32/clock-f.c
+++ b/chip/stm32/clock-f.c
@@ -168,19 +168,28 @@ void sec_to_rtc(uint32_t sec, struct rtc_time_reg *rtc)
rtc->rtc_ssr = 0;
}
-/* Return sub-10-sec time diff between two rtc readings */
-int32_t get_rtc_diff(const struct rtc_time_reg *rtc0,
- const struct rtc_time_reg *rtc1)
+/* Return sub-10-sec time diff between two rtc readings
+ *
+ * Note: this function assumes rtc0 was sampled before rtc1.
+ * Additionally, this function only looks at the difference mod 10
+ * seconds.
+ */
+uint32_t get_rtc_diff(const struct rtc_time_reg *rtc0,
+ const struct rtc_time_reg *rtc1)
{
- int32_t diff;
-
- /* Note: This only looks at the diff mod 10 seconds */
- diff = ((rtc1->rtc_tr & 0xf) * SECOND +
- rtcss_to_us(rtc1->rtc_ssr)) -
- ((rtc0->rtc_tr & 0xf) * SECOND +
- rtcss_to_us(rtc0->rtc_ssr));
-
- return (diff < 0) ? (diff + 10 * SECOND) : diff;
+ uint32_t rtc0_val, rtc1_val, diff;
+
+ rtc0_val = (rtc0->rtc_tr & 0xF) * SECOND + rtcss_to_us(rtc0->rtc_ssr);
+ rtc1_val = (rtc1->rtc_tr & 0xF) * SECOND + rtcss_to_us(rtc1->rtc_ssr);
+ diff = rtc1_val;
+ if (rtc1_val < rtc0_val) {
+ /* rtc_ssr has wrapped, since we assume rtc0 < rtc1, add
+ * 10 seconds to get the correct value
+ */
+ diff += 10 * SECOND;
+ }
+ diff -= rtc0_val;
+ return diff;
}
void rtc_read(struct rtc_time_reg *rtc)
diff --git a/chip/stm32/clock-f.h b/chip/stm32/clock-f.h
index 5e9acc748d..942567ec51 100644
--- a/chip/stm32/clock-f.h
+++ b/chip/stm32/clock-f.h
@@ -57,8 +57,8 @@ int32_t rtcss_to_us(uint32_t rtcss);
uint32_t us_to_rtcss(int32_t us);
/* Return sub-10-sec time diff between two rtc readings */
-int32_t get_rtc_diff(const struct rtc_time_reg *rtc0,
- const struct rtc_time_reg *rtc1);
+uint32_t get_rtc_diff(const struct rtc_time_reg *rtc0,
+ const struct rtc_time_reg *rtc1);
/* Read RTC values */
void rtc_read(struct rtc_time_reg *rtc);
diff --git a/chip/stm32/clock-stm32f0.c b/chip/stm32/clock-stm32f0.c
index 3509468056..26188d97fd 100644
--- a/chip/stm32/clock-stm32f0.c
+++ b/chip/stm32/clock-stm32f0.c
@@ -311,7 +311,8 @@ void clock_refresh_console_in_use(void)
void __idle(void)
{
timestamp_t t0;
- int next_delay, margin_us, rtc_diff;
+ uint32_t rtc_diff;
+ int next_delay, margin_us;
struct rtc_time_reg rtc0, rtc1;
while (1) {