Skip to content

Commit 075d977

Browse files
ppannutoalistair23
authored andcommitted
tock: use native time getter, remove globals
Tock has direct support for querying time. The prior `millis()` method here replicated the same functionality, but missed some corner case concerns around overflow/wrapping. Instead, just use the native Tock time getter method. This also removes unneeded global variables and methods.
1 parent 1c3a24d commit 075d977

File tree

1 file changed

+7
-23
lines changed

1 file changed

+7
-23
lines changed

src/hal/Tock/libtockHal.h

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@
5656
typedef void (*gpioIrqFn)(void);
5757

5858
gpioIrqFn gpio_funcs[4] = { NULL, NULL, NULL, NULL};
59-
uint32_t frequency = 0;
60-
61-
/*
62-
* Get the the timer frequency in Hz.
63-
*/
64-
int alarm_internal_frequency(uint32_t* frequency) {
65-
syscall_return_t rval = command(0x0, 1, 0, 0);
66-
return tock_command_return_u32_to_returncode(rval, frequency);
67-
}
68-
69-
int alarm_internal_read(uint32_t* time) {
70-
syscall_return_t rval = command(0x0, 2, 0, 0);
71-
return tock_command_return_u32_to_returncode(rval, time);
72-
}
7359

7460
static void lora_phy_gpio_Callback (int gpioPin,
7561
__attribute__ ((unused)) int arg2,
@@ -174,16 +160,11 @@ class TockRadioLibHal : public RadioLibHal {
174160
}
175161

176162
unsigned long millis() override {
177-
uint32_t now;
163+
struct timeval tv;
178164
unsigned long ms;
179165

180-
if (frequency == 0) {
181-
alarm_internal_frequency(&frequency);
182-
}
183-
184-
alarm_internal_read(&now);
185-
186-
ms = now / (frequency / 1000);
166+
libtock_alarm_gettimeasticks(&tv);
167+
ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
187168

188169
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
189170
return ms;
@@ -193,7 +174,10 @@ class TockRadioLibHal : public RadioLibHal {
193174
}
194175

195176
unsigned long micros() override {
196-
return millis() / 1000;
177+
struct timeval tv;
178+
179+
libtock_alarm_gettimeasticks(&tv);
180+
return tv.tv_sec * 1000000 + tv.tv_usec;
197181
}
198182

199183
long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override {

0 commit comments

Comments
 (0)