From 1c429228d5d83541d8042465d71699b44b4f6f05 Mon Sep 17 00:00:00 2001 From: Alistair Francis Date: Tue, 2 Jul 2024 20:18:13 +1000 Subject: [PATCH] NonArduino: Tock: Don't read the frequency every time To speed up the operation of obtaining the current time in milliseconds let's cache the frequency. Signed-off-by: Alistair Francis --- examples/NonArduino/Tock/libtockHal.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/examples/NonArduino/Tock/libtockHal.h b/examples/NonArduino/Tock/libtockHal.h index f4c92118..b4274465 100644 --- a/examples/NonArduino/Tock/libtockHal.h +++ b/examples/NonArduino/Tock/libtockHal.h @@ -56,6 +56,7 @@ typedef void (*gpioIrqFn)(void); gpioIrqFn gpio_funcs[4] = { NULL, NULL, NULL, NULL}; +uint32_t frequency = 0; /* * Get the the timer frequency in Hz. @@ -164,12 +165,18 @@ class TockHal : public RadioLibHal { } unsigned long millis() override { - uint32_t frequency, now; + uint32_t now; + unsigned long ms; + + if (frequency == 0) { + alarm_internal_frequency(&frequency); + } - alarm_internal_frequency(&frequency); alarm_internal_read(&now); - return (now / frequency) / 1000; + ms = now / (frequency / 1000); + + return ms; } unsigned long micros() override {