diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2019-02-27 13:09:54 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2019-03-20 19:51:21 -0700 |
commit | 8fb1c6c52510438f77378f08d2b6d881da48dd3f (patch) | |
tree | 177592678efb8a24046fdbb4dffd62f3e2f00a7e | |
parent | 06933b4cb6dbb2a36b2d4d6139325575a558836d (diff) | |
download | chrome-ec-8fb1c6c52510438f77378f08d2b6d881da48dd3f.tar.gz |
ec: Refactor get_rtc_diff to return unsigned values
get_rtc_diff in stm32 previously returned a signed value, but the result
could never be negative. This changes the function to return an unsigned
value in a safe manner, and updates its corresponding usage.
The original patch set for this CL included a test for the get_rtc_diff
functionality. This was removed since there was no easy way to make a
host-side test of board-specific functionality. If this is going to
be solved, I will write a separate CL.
BUG=chromium:769407
BRANCH=none
TEST=make buildall -j
TEST=made unit test for get_rtc_diff; test passed (see note above)
Change-Id: Iec1b274294d8f78eaa0ee66d3bd63ce69536e747
Signed-off-by: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1493035
Reviewed-by: Jett Rink <jettrink@chromium.org>
-rw-r--r-- | board/zinger/runtime.c | 4 | ||||
-rw-r--r-- | chip/stm32/clock-f.c | 33 | ||||
-rw-r--r-- | chip/stm32/clock-f.h | 4 | ||||
-rw-r--r-- | chip/stm32/clock-stm32f0.c | 3 |
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) { |