From b7515004daa687ff577514f89b0575df31a816e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Th=C3=A9baudeau?= Date: Tue, 3 Dec 2013 18:09:07 +0100 Subject: [PATCH] cc2538: clock: Fix secs update in clock_adjust() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Whole elapsed seconds are added to secs first, so only the remaining subsecond ticks should then be subtracted from second_countdown in order to decide whether secs should be incremented again. Otherwise, secs is not correctly updated in some cases, typically if the bit 7 of ticks is 1. E.g., with ticks = 128 (i.e. exactly 1 s elapsed) and second_countdown = 128, secs was first incremented as expected, then 128 was subtracted from second_countdown, giving 0 and triggering an unwanted second increment of secs. Or with ticks = 129 (i.e. 1 s + 1 tick) and second_countdown = 1, secs was first incremented as expected, then 129 was subtracted from second_countdown, giving 128 and missing a second increment of secs that should have occurred because second_countdown wrapped around. Signed-off-by: Benoît Thébaudeau --- cpu/cc2538/clock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu/cc2538/clock.c b/cpu/cc2538/clock.c index 20297e993..51f0c7990 100644 --- a/cpu/cc2538/clock.c +++ b/cpu/cc2538/clock.c @@ -188,7 +188,7 @@ clock_adjust(clock_time_t ticks) * Update internal second countdown so that next second change will actually * happen when it's meant to happen. */ - second_countdown -= ticks; + second_countdown -= ticks & 127; if(second_countdown == 0 || second_countdown > 128) { secs++;