improve time.monotonic_ns() accuracy from ms to us

This commit is contained in:
Dan Halbert 2020-02-07 10:02:50 -05:00
parent a63f49cb83
commit 857d8ab40a
8 changed files with 46 additions and 2 deletions

View File

@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"

View File

@ -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);
}

View File

@ -24,6 +24,8 @@
* THE SOFTWARE.
*/
#include <sys/time.h>
#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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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