From 857d8ab40a6e4ef7d0cc78f481511070c1af15e3 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 7 Feb 2020 10:02:50 -0500 Subject: [PATCH 1/3] improve time.monotonic_ns() accuracy from ms to us --- locale/circuitpython.pot | 2 +- ports/atmel-samd/common-hal/time/__init__.c | 9 +++++++++ ports/cxd56/common-hal/time/__init__.c | 8 ++++++++ ports/mimxrt10xx/common-hal/time/__init__.c | 10 ++++++++++ ports/nrf/common-hal/time/__init__.c | 8 ++++++++ ports/stm32f4/common-hal/time/__init__.c | 8 ++++++++ shared-bindings/time/__init__.c | 2 +- shared-bindings/time/__init__.h | 1 + 8 files changed, 46 insertions(+), 2 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 6069bdd971..3e44130688 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-02-05 15:55-0800\n" +"POT-Creation-Date: 2020-02-07 10:02-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/ports/atmel-samd/common-hal/time/__init__.c b/ports/atmel-samd/common-hal/time/__init__.c index 2d82b3d1ad..1499964527 100644 --- a/ports/atmel-samd/common-hal/time/__init__.c +++ b/ports/atmel-samd/common-hal/time/__init__.c @@ -29,11 +29,20 @@ #include "shared-bindings/time/__init__.h" #include "supervisor/shared/tick.h" +#include "tick.h" inline uint64_t common_hal_time_monotonic() { return supervisor_ticks_ms64(); } +uint64_t common_hal_time_monotonic_ns() { + uint64_t ms; + uint32_t us_until_ms; + current_tick(&ms, &us_until_ms); + // us counts down. + return 1000 * (ms * 1000 + (1000 - us_until_ms)); +} + void common_hal_time_delay_ms(uint32_t delay) { mp_hal_delay_ms(delay); } diff --git a/ports/cxd56/common-hal/time/__init__.c b/ports/cxd56/common-hal/time/__init__.c index 6f5eedd419..fa915782df 100644 --- a/ports/cxd56/common-hal/time/__init__.c +++ b/ports/cxd56/common-hal/time/__init__.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#include + #include "py/mphal.h" #include "supervisor/shared/tick.h" @@ -32,6 +34,12 @@ uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } +uint64_t common_hal_time_monotonic_ns() { + struct timeval tv; + gettimeofday(&tv, NULL); + return 1000 * ((uint64_t) tv.tv_sec * 1000000 + (uint64_t) tv.tv_usec); +} + void common_hal_time_delay_ms(uint32_t delay) { mp_hal_delay_ms(delay); } diff --git a/ports/mimxrt10xx/common-hal/time/__init__.c b/ports/mimxrt10xx/common-hal/time/__init__.c index 2d82b3d1ad..4497d8e86c 100644 --- a/ports/mimxrt10xx/common-hal/time/__init__.c +++ b/ports/mimxrt10xx/common-hal/time/__init__.c @@ -30,10 +30,20 @@ #include "supervisor/shared/tick.h" +#include "tick.h" + inline uint64_t common_hal_time_monotonic() { return supervisor_ticks_ms64(); } +uint64_t common_hal_time_monotonic_ns() { + uint64_t ms; + uint32_t us_until_ms; + current_tick(&ms, &us_until_ms); + // us counts down. + return 1000 * (ms * 1000 + (1000 - us_until_ms)); +} + void common_hal_time_delay_ms(uint32_t delay) { mp_hal_delay_ms(delay); } diff --git a/ports/nrf/common-hal/time/__init__.c b/ports/nrf/common-hal/time/__init__.c index 976f519db2..dcfe55eca7 100644 --- a/ports/nrf/common-hal/time/__init__.c +++ b/ports/nrf/common-hal/time/__init__.c @@ -32,6 +32,14 @@ uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } +uint64_t common_hal_time_monotonic_ns() { + uint64_t ms; + uint32_t us_until_ms; + current_tick(&ms, &us_until_ms); + // us counts down. + return 1000 * (ms * 1000 + (1000 - us_until_ms)); +} + void common_hal_time_delay_ms(uint32_t delay) { mp_hal_delay_ms(delay); } diff --git a/ports/stm32f4/common-hal/time/__init__.c b/ports/stm32f4/common-hal/time/__init__.c index 976f519db2..dcfe55eca7 100644 --- a/ports/stm32f4/common-hal/time/__init__.c +++ b/ports/stm32f4/common-hal/time/__init__.c @@ -32,6 +32,14 @@ uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } +uint64_t common_hal_time_monotonic_ns() { + uint64_t ms; + uint32_t us_until_ms; + current_tick(&ms, &us_until_ms); + // us counts down. + return 1000 * (ms * 1000 + (1000 - us_until_ms)); +} + void common_hal_time_delay_ms(uint32_t delay) { mp_hal_delay_ms(delay); } diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c index b3e4604b51..2c1aebc2e7 100644 --- a/shared-bindings/time/__init__.c +++ b/shared-bindings/time/__init__.c @@ -216,7 +216,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); //| :rtype: int //| STATIC mp_obj_t time_monotonic_ns(void) { - uint64_t time64 = common_hal_time_monotonic() * 1000000llu; + uint64_t time64 = common_hal_time_monotonic_ns(); return mp_obj_new_int_from_ll((long long) time64); } MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_ns_obj, time_monotonic_ns); diff --git a/shared-bindings/time/__init__.h b/shared-bindings/time/__init__.h index c5a0b47fc1..ec96aea24f 100644 --- a/shared-bindings/time/__init__.h +++ b/shared-bindings/time/__init__.h @@ -36,6 +36,7 @@ extern mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm); extern void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm); extern uint64_t common_hal_time_monotonic(void); +extern uint64_t common_hal_time_monotonic_ns(void); extern void common_hal_time_delay_ms(uint32_t); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_TIME___INIT___H From cbd519bfa6bb3e2f56a07e977c47159059f1fc8d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 7 Feb 2020 10:24:11 -0500 Subject: [PATCH 2/3] time.sleep() rounds to nearest msec --- shared-bindings/time/__init__.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c index 2c1aebc2e7..8d7f2f3fd2 100644 --- a/shared-bindings/time/__init__.c +++ b/shared-bindings/time/__init__.c @@ -70,14 +70,16 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_obj, time_monotonic); //| STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { #if MICROPY_PY_BUILTINS_FLOAT - float seconds = mp_obj_get_float(seconds_o); + mp_float_t seconds = mp_obj_get_float(seconds_o); + mp_float_t msecs = 1000.0f * seconds + 0.5f; #else - int seconds = mp_obj_get_int(seconds_o); + mp_int_t seconds = mp_obj_get_int(seconds_o); + mp_int_t msecs = 1000 * seconds; #endif if (seconds < 0) { mp_raise_ValueError(translate("sleep length must be non-negative")); } - common_hal_time_delay_ms(1000 * seconds); + common_hal_time_delay_ms(msecs); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); From 005c4caf8c5c76deab661a1f8bc2ea743ba40c6d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 7 Feb 2020 10:32:37 -0500 Subject: [PATCH 3/3] fix function defs for compiler --- ports/atmel-samd/common-hal/time/__init__.c | 4 ++-- ports/cxd56/common-hal/time/__init__.c | 2 +- ports/mimxrt10xx/common-hal/time/__init__.c | 4 ++-- ports/nrf/common-hal/time/__init__.c | 2 +- ports/stm32f4/common-hal/time/__init__.c | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ports/atmel-samd/common-hal/time/__init__.c b/ports/atmel-samd/common-hal/time/__init__.c index 1499964527..652e455fc4 100644 --- a/ports/atmel-samd/common-hal/time/__init__.c +++ b/ports/atmel-samd/common-hal/time/__init__.c @@ -31,11 +31,11 @@ #include "supervisor/shared/tick.h" #include "tick.h" -inline uint64_t common_hal_time_monotonic() { +inline uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } -uint64_t common_hal_time_monotonic_ns() { +uint64_t common_hal_time_monotonic_ns(void) { uint64_t ms; uint32_t us_until_ms; current_tick(&ms, &us_until_ms); diff --git a/ports/cxd56/common-hal/time/__init__.c b/ports/cxd56/common-hal/time/__init__.c index fa915782df..31c63cbb29 100644 --- a/ports/cxd56/common-hal/time/__init__.c +++ b/ports/cxd56/common-hal/time/__init__.c @@ -34,7 +34,7 @@ uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } -uint64_t common_hal_time_monotonic_ns() { +uint64_t common_hal_time_monotonic_ns(void) { struct timeval tv; gettimeofday(&tv, NULL); return 1000 * ((uint64_t) tv.tv_sec * 1000000 + (uint64_t) tv.tv_usec); diff --git a/ports/mimxrt10xx/common-hal/time/__init__.c b/ports/mimxrt10xx/common-hal/time/__init__.c index 4497d8e86c..524e31d1ae 100644 --- a/ports/mimxrt10xx/common-hal/time/__init__.c +++ b/ports/mimxrt10xx/common-hal/time/__init__.c @@ -32,11 +32,11 @@ #include "tick.h" -inline uint64_t common_hal_time_monotonic() { +inline uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } -uint64_t common_hal_time_monotonic_ns() { +uint64_t common_hal_time_monotonic_ns(void) { uint64_t ms; uint32_t us_until_ms; current_tick(&ms, &us_until_ms); diff --git a/ports/nrf/common-hal/time/__init__.c b/ports/nrf/common-hal/time/__init__.c index dcfe55eca7..c85077868a 100644 --- a/ports/nrf/common-hal/time/__init__.c +++ b/ports/nrf/common-hal/time/__init__.c @@ -32,7 +32,7 @@ uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } -uint64_t common_hal_time_monotonic_ns() { +uint64_t common_hal_time_monotonic_ns(void) { uint64_t ms; uint32_t us_until_ms; current_tick(&ms, &us_until_ms); diff --git a/ports/stm32f4/common-hal/time/__init__.c b/ports/stm32f4/common-hal/time/__init__.c index dcfe55eca7..c85077868a 100644 --- a/ports/stm32f4/common-hal/time/__init__.c +++ b/ports/stm32f4/common-hal/time/__init__.c @@ -32,7 +32,7 @@ uint64_t common_hal_time_monotonic(void) { return supervisor_ticks_ms64(); } -uint64_t common_hal_time_monotonic_ns() { +uint64_t common_hal_time_monotonic_ns(void) { uint64_t ms; uint32_t us_until_ms; current_tick(&ms, &us_until_ms);