From 416df1c50dc31617436091142c4ca9dfa9ca028f Mon Sep 17 00:00:00 2001 From: Alistair Francis Date: Tue, 2 Jul 2024 20:18:02 +1000 Subject: [PATCH 1/3] NonArduino: Tock: Fixup the GPIO interrupt callbacks Previously we only supported a single interrupt callback function, which could cause issues. Fix this up so instead we support interrupt callbacks on all 4 GPIOs. Signed-off-by: Alistair Francis --- examples/NonArduino/Tock/libtockHal.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/NonArduino/Tock/libtockHal.h b/examples/NonArduino/Tock/libtockHal.h index 85c53920c..f4c921187 100644 --- a/examples/NonArduino/Tock/libtockHal.h +++ b/examples/NonArduino/Tock/libtockHal.h @@ -55,6 +55,8 @@ typedef void (*gpioIrqFn)(void); +gpioIrqFn gpio_funcs[4] = { NULL, NULL, NULL, NULL}; + /* * Get the the timer frequency in Hz. */ @@ -73,7 +75,7 @@ static void lora_phy_gpio_Callback (int gpioPin, __attribute__ ((unused)) int arg3, void* userdata) { - gpioIrqFn fn = *(gpioIrqFn*)(&userdata); + gpioIrqFn fn = gpio_funcs[gpioPin - 1]; if (fn != NULL ) { fn(); @@ -136,7 +138,8 @@ class TockHal : public RadioLibHal { return; } - libtock_lora_phy_gpio_command_interrupt_callback(lora_phy_gpio_Callback, &interruptCb); + gpio_funcs[interruptNum - 1] = interruptCb; + libtock_lora_phy_gpio_command_interrupt_callback(lora_phy_gpio_Callback, NULL); // set GPIO as input and enable interrupts on it libtock_lora_phy_gpio_enable_input(interruptNum, libtock_pull_down); @@ -148,6 +151,7 @@ class TockHal : public RadioLibHal { return; } + gpio_funcs[interruptNum - 1] = NULL; libtock_lora_phy_gpio_disable_interrupt(interruptNum); } From 1c429228d5d83541d8042465d71699b44b4f6f05 Mon Sep 17 00:00:00 2001 From: Alistair Francis Date: Tue, 2 Jul 2024 20:18:13 +1000 Subject: [PATCH 2/3] 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 f4c921187..b42744651 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 { From 6268e5d4c755e526d8c6a30d44d6285226f51556 Mon Sep 17 00:00:00 2001 From: Alistair Francis Date: Tue, 2 Jul 2024 20:18:19 +1000 Subject: [PATCH 3/3] NonArduiono: Tock: Support RADIOLIB_CLOCK_DRIFT_MS Signed-off-by: Alistair Francis --- examples/NonArduino/Tock/libtockHal.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/NonArduino/Tock/libtockHal.h b/examples/NonArduino/Tock/libtockHal.h index b42744651..7ae8bf2ae 100644 --- a/examples/NonArduino/Tock/libtockHal.h +++ b/examples/NonArduino/Tock/libtockHal.h @@ -157,11 +157,19 @@ class TockHal : public RadioLibHal { } void delay(unsigned long ms) override { - libtocksync_alarm_delay_ms( ms ); +#if !defined(RADIOLIB_CLOCK_DRIFT_MS) + libtocksync_alarm_delay_ms(ms); +#else + libtocksync_alarm_delay_ms(ms * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS)); +#endif } void delayMicroseconds(unsigned long us) override { - libtocksync_alarm_delay_ms( us / 1000 ); +#if !defined(RADIOLIB_CLOCK_DRIFT_MS) + libtocksync_alarm_delay_ms(us / 1000); +#else + libtocksync_alarm_delay_ms((us * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS)) / 1000); +#endif } unsigned long millis() override { @@ -176,7 +184,11 @@ class TockHal : public RadioLibHal { ms = now / (frequency / 1000); +#if !defined(RADIOLIB_CLOCK_DRIFT_MS) return ms; +#else + return ms * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS); +#endif } unsigned long micros() override {