summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-10-30 03:07:22 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-10-30 03:07:22 +0300
commite429daa5724e5e3c5f8866742abc64eb90ded71d (patch)
treea524c6ce5b9b054884967cac27bdbe6b3a5aa425
parent76146b3d9ab0ab88ce0f0d1cfa0235ec0758a4a9 (diff)
extmod/utime_mphal: Fix implementation of new semantics of ticks_diff().
Now the function properly uses ring arithmetic to return signed value in range (inclusive): [-MICROPY_PY_UTIME_TICKS_PERIOD/2, MICROPY_PY_UTIME_TICKS_PERIOD/2-1]. That means that function can properly process 2 time values away from each other within MICROPY_PY_UTIME_TICKS_PERIOD/2 ticks, but away in both directions. For example, if tick value 'a' predates tick value 'b', ticks_diff(a, b) will return negative value, and positive value otherwise. But at positive value of MICROPY_PY_UTIME_TICKS_PERIOD/2-1, the result of the function will wrap around to negative -MICROPY_PY_UTIME_TICKS_PERIOD/2, in other words, if a follows b in more than MICROPY_PY_UTIME_TICKS_PERIOD/2 - 1 ticks, the function will "consider" a to actually predate b.
-rw-r--r--extmod/utime_mphal.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c
index 1a32180cf..9aa2f1324 100644
--- a/extmod/utime_mphal.c
+++ b/extmod/utime_mphal.c
@@ -89,7 +89,13 @@ STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
// we assume that the arguments come from ticks_xx so are small ints
uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
- return MP_OBJ_NEW_SMALL_INT((int32_t)(end - start));
+ int32_t diff = end - start;
+ if (diff < (signed)-(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) {
+ diff += MICROPY_PY_UTIME_TICKS_PERIOD;
+ } else if (diff >= (signed)(MICROPY_PY_UTIME_TICKS_PERIOD / 2)) {
+ diff -= MICROPY_PY_UTIME_TICKS_PERIOD;
+ }
+ return MP_OBJ_NEW_SMALL_INT(diff);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj, time_ticks_diff);