Skip to content

Commit

Permalink
libtock-sync: alarm sleep_for: use yield-waitfor
Browse files Browse the repository at this point in the history
  • Loading branch information
alevy authored and ppannuto committed Oct 30, 2024
1 parent 1285a0e commit 3763057
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
21 changes: 8 additions & 13 deletions libtock-sync/services/alarm.c
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions libtock-sync/services/alarm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <libtock/peripherals/syscalls/alarm_syscalls.h>
#include <libtock/services/alarm.h>
#include <libtock/tock.h>

Expand Down
5 changes: 5 additions & 0 deletions libtock/peripherals/syscalls/alarm_syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions libtock/peripherals/syscalls/alarm_syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
12 changes: 12 additions & 0 deletions libtock/services/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3763057

Please sign in to comment.