Skip to content

Commit 9206925

Browse files
committed
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)
1 parent 1bc770c commit 9206925

File tree

1 file changed

+5
-5
lines changed
  • ports/esp32s2/supervisor

1 file changed

+5
-5
lines changed

ports/esp32s2/supervisor/port.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ uint32_t port_get_saved_word(void) {
191191
}
192192

193193
uint64_t port_get_raw_ticks(uint8_t* subticks) {
194-
struct timeval tv_now;
195-
gettimeofday(&tv_now, NULL);
196-
// convert usec back to ticks
197-
uint64_t all_subticks = (uint64_t)(tv_now.tv_usec * 2) / 71;
194+
// Convert microseconds to subticks of 1/32768 seconds
195+
// 32768/1000000 = 64/15625 in lowest terms
196+
// this arithmetic overflows after 570 years
197+
int64_t all_subticks = esp_timer_get_time() * 512 / 15625;
198198
if (subticks != NULL) {
199199
*subticks = all_subticks % 32;
200200
}
201-
return (uint64_t)tv_now.tv_sec * 1024L + all_subticks / 32;
201+
return all_subticks / 32;
202202
}
203203

204204
// Enable 1/1024 second tick.

0 commit comments

Comments
 (0)