From 3763057f2cf331ff6e639e94d9a3c2c39e4c4ca7 Mon Sep 17 00:00:00 2001 From: Amit Aryeh Levy Date: Wed, 19 Jun 2024 14:11:32 -0400 Subject: [PATCH] libtock-sync: alarm sleep_for: use yield-waitfor --- libtock-sync/services/alarm.c | 21 +++++++------------ libtock-sync/services/alarm.h | 1 + libtock/peripherals/syscalls/alarm_syscalls.c | 5 +++++ libtock/peripherals/syscalls/alarm_syscalls.h | 9 ++++++++ libtock/services/alarm.c | 12 +++++++++++ 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/libtock-sync/services/alarm.c b/libtock-sync/services/alarm.c index 7eff3c24d..0e8b27a1d 100644 --- a/libtock-sync/services/alarm.c +++ b/libtock-sync/services/alarm.c @@ -1,27 +1,22 @@ #include "alarm.h" +// Declare non-public export of helper from libtock. +uint32_t _ms_to_ticks(uint32_t ms); + struct alarm_cb_data { bool fired; }; -static struct alarm_cb_data delay_data = { .fired = false }; - -static void delay_cb(__attribute__ ((unused)) uint32_t now, - __attribute__ ((unused)) uint32_t scheduled, - __attribute__ ((unused)) void* opaque) { - delay_data.fired = true; -} - int libtocksync_alarm_delay_ms(uint32_t ms) { - delay_data.fired = false; - libtock_alarm_t alarm; int rc; - - if ((rc = libtock_alarm_in_ms(ms, delay_cb, NULL, &alarm)) != RETURNCODE_SUCCESS) { + uint32_t ticks = _ms_to_ticks(ms); + if ((rc = libtock_alarm_command_set_relative_blind(ticks)) != RETURNCODE_SUCCESS) { return rc; } - yield_for(&delay_data.fired); + yield_waitfor_return_t yval = yield_wait_for(DRIVER_NUM_ALARM, 1); + if (yval.data0 != RETURNCODE_SUCCESS) return yval.data0; + return rc; } diff --git a/libtock-sync/services/alarm.h b/libtock-sync/services/alarm.h index 4d0a66a78..9a4b92bea 100644 --- a/libtock-sync/services/alarm.h +++ b/libtock-sync/services/alarm.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include diff --git a/libtock/peripherals/syscalls/alarm_syscalls.c b/libtock/peripherals/syscalls/alarm_syscalls.c index 9e84a9ff3..dfe5ffbbf 100644 --- a/libtock/peripherals/syscalls/alarm_syscalls.c +++ b/libtock/peripherals/syscalls/alarm_syscalls.c @@ -29,6 +29,11 @@ int libtock_alarm_command_set_relative(uint32_t dt, uint32_t* actual) { return tock_command_return_u32_to_returncode(rval, actual); } +int libtock_alarm_command_set_relative_blind(uint32_t dt) { + syscall_return_t rval = command(DRIVER_NUM_ALARM, 5, dt, 0); + return tock_command_return_novalue_to_returncode(rval); +} + int libtock_alarm_command_set_absolute(uint32_t reference, uint32_t dt) { syscall_return_t rval = command(DRIVER_NUM_ALARM, 6, reference, dt); uint32_t unused; diff --git a/libtock/peripherals/syscalls/alarm_syscalls.h b/libtock/peripherals/syscalls/alarm_syscalls.h index f474e993b..fa1797d2b 100644 --- a/libtock/peripherals/syscalls/alarm_syscalls.h +++ b/libtock/peripherals/syscalls/alarm_syscalls.h @@ -46,6 +46,15 @@ int libtock_alarm_command_stop(void); */ int libtock_alarm_command_set_relative(uint32_t dt, uint32_t* actual); +/* + * Starts a oneshot alarm + * + * expiration - relative expiration value from when kernel handles syscall. + * + * Side-effects: cancels any existing/outstanding alarms + */ +int libtock_alarm_command_set_relative_blind(uint32_t dt); + /* * Starts a oneshot alarm * diff --git a/libtock/services/alarm.c b/libtock/services/alarm.c index 419e1cc48..b0f547c76 100644 --- a/libtock/services/alarm.c +++ b/libtock/services/alarm.c @@ -64,6 +64,18 @@ static uint32_t ms_to_ticks(uint32_t ms) { return ticks; } + +// Declare non-public export of helper for libtock-sync. +uint32_t _ms_to_ticks(uint32_t ms); +/** \brief Private export of ms->ticks helper to libtock-sync + * + * This is a non-stable, non-public interface for a helper + * function which is also useful to libtock-sync. + */ +uint32_t _ms_to_ticks(uint32_t ms) { + return ms_to_ticks(ms); +} + uint32_t libtock_alarm_ticks_to_ms(uint32_t ticks) { // `ticks_to_ms`'s conversion will be accurate to within the range // 0 to 1 milliseconds less than the exact conversion