Skip to content

Commit 857d8ab

Browse files
committed
improve time.monotonic_ns() accuracy from ms to us
1 parent a63f49c commit 857d8ab

File tree

8 files changed

+46
-2
lines changed

8 files changed

+46
-2
lines changed

locale/circuitpython.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-02-05 15:55-0800\n"
11+
"POT-Creation-Date: 2020-02-07 10:02-0500\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"

ports/atmel-samd/common-hal/time/__init__.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,20 @@
2929
#include "shared-bindings/time/__init__.h"
3030

3131
#include "supervisor/shared/tick.h"
32+
#include "tick.h"
3233

3334
inline uint64_t common_hal_time_monotonic() {
3435
return supervisor_ticks_ms64();
3536
}
3637

38+
uint64_t common_hal_time_monotonic_ns() {
39+
uint64_t ms;
40+
uint32_t us_until_ms;
41+
current_tick(&ms, &us_until_ms);
42+
// us counts down.
43+
return 1000 * (ms * 1000 + (1000 - us_until_ms));
44+
}
45+
3746
void common_hal_time_delay_ms(uint32_t delay) {
3847
mp_hal_delay_ms(delay);
3948
}

ports/cxd56/common-hal/time/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <sys/time.h>
28+
2729
#include "py/mphal.h"
2830

2931
#include "supervisor/shared/tick.h"
@@ -32,6 +34,12 @@ uint64_t common_hal_time_monotonic(void) {
3234
return supervisor_ticks_ms64();
3335
}
3436

37+
uint64_t common_hal_time_monotonic_ns() {
38+
struct timeval tv;
39+
gettimeofday(&tv, NULL);
40+
return 1000 * ((uint64_t) tv.tv_sec * 1000000 + (uint64_t) tv.tv_usec);
41+
}
42+
3543
void common_hal_time_delay_ms(uint32_t delay) {
3644
mp_hal_delay_ms(delay);
3745
}

ports/mimxrt10xx/common-hal/time/__init__.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,20 @@
3030

3131
#include "supervisor/shared/tick.h"
3232

33+
#include "tick.h"
34+
3335
inline uint64_t common_hal_time_monotonic() {
3436
return supervisor_ticks_ms64();
3537
}
3638

39+
uint64_t common_hal_time_monotonic_ns() {
40+
uint64_t ms;
41+
uint32_t us_until_ms;
42+
current_tick(&ms, &us_until_ms);
43+
// us counts down.
44+
return 1000 * (ms * 1000 + (1000 - us_until_ms));
45+
}
46+
3747
void common_hal_time_delay_ms(uint32_t delay) {
3848
mp_hal_delay_ms(delay);
3949
}

ports/nrf/common-hal/time/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ uint64_t common_hal_time_monotonic(void) {
3232
return supervisor_ticks_ms64();
3333
}
3434

35+
uint64_t common_hal_time_monotonic_ns() {
36+
uint64_t ms;
37+
uint32_t us_until_ms;
38+
current_tick(&ms, &us_until_ms);
39+
// us counts down.
40+
return 1000 * (ms * 1000 + (1000 - us_until_ms));
41+
}
42+
3543
void common_hal_time_delay_ms(uint32_t delay) {
3644
mp_hal_delay_ms(delay);
3745
}

ports/stm32f4/common-hal/time/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ uint64_t common_hal_time_monotonic(void) {
3232
return supervisor_ticks_ms64();
3333
}
3434

35+
uint64_t common_hal_time_monotonic_ns() {
36+
uint64_t ms;
37+
uint32_t us_until_ms;
38+
current_tick(&ms, &us_until_ms);
39+
// us counts down.
40+
return 1000 * (ms * 1000 + (1000 - us_until_ms));
41+
}
42+
3543
void common_hal_time_delay_ms(uint32_t delay) {
3644
mp_hal_delay_ms(delay);
3745
}

shared-bindings/time/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
216216
//| :rtype: int
217217
//|
218218
STATIC mp_obj_t time_monotonic_ns(void) {
219-
uint64_t time64 = common_hal_time_monotonic() * 1000000llu;
219+
uint64_t time64 = common_hal_time_monotonic_ns();
220220
return mp_obj_new_int_from_ll((long long) time64);
221221
}
222222
MP_DEFINE_CONST_FUN_OBJ_0(time_monotonic_ns_obj, time_monotonic_ns);

shared-bindings/time/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm);
3636
extern void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm);
3737

3838
extern uint64_t common_hal_time_monotonic(void);
39+
extern uint64_t common_hal_time_monotonic_ns(void);
3940
extern void common_hal_time_delay_ms(uint32_t);
4041

4142
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_TIME___INIT___H

0 commit comments

Comments
 (0)