From 9206925bf8d2e3d8220b59200ee50c3e4c8a64d2 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 17 Nov 2020 17:45:12 -0600 Subject: [PATCH] esp32s2: port_get_raw_ticks: Use a more efficient, monotonic routine While trying to debug #3572, I noticed that I would frequently break in the midst of gettimeofday and that the routine get_adjusted_boot_time had to take and release locks. Furthermore, we don't want "adjusted" boot time, which could go forwards or backwards depending on the adjustment (such as setting the clock used by gettimeofday() to the network time) --- ports/esp32s2/supervisor/port.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index 47d0c7f463..876ad739d6 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -191,14 +191,14 @@ uint32_t port_get_saved_word(void) { } uint64_t port_get_raw_ticks(uint8_t* subticks) { - struct timeval tv_now; - gettimeofday(&tv_now, NULL); - // convert usec back to ticks - uint64_t all_subticks = (uint64_t)(tv_now.tv_usec * 2) / 71; + // Convert microseconds to subticks of 1/32768 seconds + // 32768/1000000 = 64/15625 in lowest terms + // this arithmetic overflows after 570 years + int64_t all_subticks = esp_timer_get_time() * 512 / 15625; if (subticks != NULL) { *subticks = all_subticks % 32; } - return (uint64_t)tv_now.tv_sec * 1024L + all_subticks / 32; + return all_subticks / 32; } // Enable 1/1024 second tick.