From da68f471d165893321ba7e0dd0ba1daca07276a5 Mon Sep 17 00:00:00 2001 From: jadehawk Date: Fri, 27 Nov 2026 09:32:05 -0500 Subject: [PATCH] feat: support periodic application timer events --- Makefile | 4 ++-- abi/freeink.api | 3 +++ apps/timertest.c | 15 +++++++++++++ docs/writing-apps.md | 13 ++++++++++++ freeink/DessenFreeInk.cpp | 15 ++++++++++++- freeink/DessenFreeInk.h | 11 ++++++++++ freeink/dessen_dispatch.inc | 7 +++++++ freeink/test_host.cpp | 42 ++++++++++++++++++++++++++++++++++++- sdk/dessen.rs | 3 +++ sdk/dessen.zig | 2 ++ sdk/dessen_abi.h | 1 + sdk/dessen_calls.h | 1 + sdk/dessen_sysnums.h | 1 + 13 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 apps/timertest.c diff --git a/Makefile b/Makefile index 586b963..ed7edb1 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ RISCV ?= $(HOME)/.platformio/tools/toolchain-riscv32-esp/bin/riscv32-esp-elf- # will fail to link. Add a small runtime to sdk/ when an app first needs one. APPFLAGS = -march=rv32im -mabi=ilp32 -Os -ffreestanding -nostdlib -mno-relax \ -fno-tree-loop-distribute-patterns -Wall -Isdk -T sdk/app.ld -Wl,--no-warn-rwx-segments -APPS = hello selftest oob bench drawtest lightsout dice sudoku captest nettest asynctest home minimal +APPS = hello selftest oob bench drawtest lightsout dice sudoku captest nettest asynctest home minimal timertest SD_APPS = hello lightsout dice sudoku home minimal # Rust is part of `make test` and `make sd`: the Rust SDK, the hand-linked Rust @@ -101,7 +101,7 @@ test: $(if $(WITH_RUST),rust-check) all $(if $(WITH_RUST),rust xpui) printf 'the book bytes here' > $$nr/example.test/book.bin; \ DESSEN_NET_ROOT=$$nr DESSEN_FS_ROOT=$$fs build/dessen build/nettest.bin; rc=$$?; \ rm -rf $$nr $$fs; test $$rc -eq 0 && echo 'nettest: PASS' - build/test_freeink build/hello.bin build/oob.bin build/lightsout.bin build/dice.bin build/sudoku.bin build/drawtest.bin build/asynctest.bin build/home.bin + build/test_freeink build/hello.bin build/oob.bin build/lightsout.bin build/dice.bin build/sudoku.bin build/drawtest.bin build/asynctest.bin build/home.bin build/timertest.bin @echo PASS # Fails early, with the fix, when `test`/`sd` would need Rust that is not here. diff --git a/abi/freeink.api b/abi/freeink.api index 1fa77f6..8361cb8 100644 --- a/abi/freeink.api +++ b/abi/freeink.api @@ -103,3 +103,6 @@ 101 open_home(u8 dest) -> i32 : return self.requestLaunchHome((int)dest); 103 wifi_connect() -> i32 : return self.beginWifi(); 105 book_cover(buf path, u16 height, out pixels) -> i32 : result = svcBookCover(self, path, path_len, height, pixels, pixels_len); + +# --- periodic application wake --- +109 timer_set(u32 interval_ms) -> i32 : result = self.setTimerInterval(interval_ms); diff --git a/apps/timertest.c b/apps/timertest.c new file mode 100644 index 0000000..ab91017 --- /dev/null +++ b/apps/timertest.c @@ -0,0 +1,15 @@ +#include "dessen.h" + +int main(void) { + if (dsn_timer_set(999) != DSN_ERR_INVALID || dsn_timer_set(3600001) != DSN_ERR_INVALID) return 1; + int enabled = dsn_timer_set(1000); + if (enabled == DSN_ERR_UNAVAILABLE) return 0; + if (enabled != 0) return 2; + if (dsn_wait_event() != DSN_EV_CONFIRM) return 3; // input takes priority + if (dsn_wait_event() != DSN_EV_TIMER) return 4; + if (dsn_timer_set(5000) != 0) return 5; + if (dsn_wait_event() != DSN_EV_TIMER) return 6; + if (dsn_timer_set(0) != 0) return 7; + if (dsn_wait_event() != DSN_EV_BACK) return 8; + return 0; +} diff --git a/docs/writing-apps.md b/docs/writing-apps.md index 32f2617..b11991d 100644 --- a/docs/writing-apps.md +++ b/docs/writing-apps.md @@ -156,6 +156,7 @@ surface: | `dsn_screen_width() / _height()` | Safe-area size | | `dsn_device(&info, sizeof info)` | Panel size, insets, touch/button flags, orientation, button-hint band | | `dsn_wait_event() -> e` | Block for the next event; decode with `DSN_EV_TYPE/X/Y(e)` | +| `dsn_timer_set(interval_ms) -> i32` | Request periodic wake events (1000–3600000 ms; 0 disables) | | `dsn_exit(code)` | Quit (also happens if `main` returns) | | `dsn_random() -> u32` | Hardware random number | | `dsn_clear()` | White the whole panel | @@ -804,3 +805,15 @@ shape, frontlight, a file read), you add **one line** to regenerates both the app-side wrapper and the firmware-side dispatch. See `AGENTS.md` for the recipe and the rules every new call must follow (clip, pin, bounds-check — every app value is untrusted). + +### Periodic wake events + +After `dsn_timer_set(ms)`, a supporting host schedules periodic monotonic +wake-ups while the app waits in `dsn_wait_event()`. The host calls `enableTimerEvents()` to advertise its scheduler and reads +`FreeInkHost::timerInterval()` and calls `markTimerElapsed()` immediately +before a step when its deadline is due. The binding returns `DSN_EV_TIMER` +only while waiting; real input wins, and missed ticks are not queued. +Intervals outside 1000–3600000 ms are invalid, except zero (disable). +Hosts without a scheduler return UNAVAILABLE for a nonzero interval. Reloading an app clears its timer. No background tasks or busy polling +are created by the runtime. Firmware and desktop schedulers own deadlines +and may defer wakes while suspended or asleep. diff --git a/freeink/DessenFreeInk.cpp b/freeink/DessenFreeInk.cpp index cdca867..19b4fda 100644 --- a/freeink/DessenFreeInk.cpp +++ b/freeink/DessenFreeInk.cpp @@ -315,11 +315,21 @@ uint32_t FreeInkHost::nextRandom() { bool FreeInkHost::load(uint8_t* mem, uint32_t len, uint32_t memCap) { refresh_ = false; + timerInterval_ = 0; + timerElapsed_ = false; clipped_ = clipTouched_ = sideways_ = false; state_ = dvm_load(&vm_, mem, len, memCap) ? State::Running : State::Faulted; return state_ == State::Running; } +int FreeInkHost::setTimerInterval(uint32_t interval) { + if (interval && (interval < 1000 || interval > 3600000)) return DSN_ERR_INVALID; + if (interval && !timerSupported_) return DSN_ERR_UNAVAILABLE; + timerInterval_ = interval; + timerElapsed_ = false; + return 0; +} + void FreeInkHost::randomBytesInto(uint8_t* out, uint32_t n) { if (services_ && services_->randomBytes(out, n)) return; for (uint32_t i = 0; i < n; i++) out[i] = static_cast(nextRandom()); @@ -329,13 +339,16 @@ State FreeInkHost::step(DrawTarget& target, const DeviceContext& device, const I uint32_t budget) { const bool sideways = sideways_; // about this step's input only, whatever the state sideways_ = false; + const bool timer = timerElapsed_ && timerInterval_ != 0; + timerElapsed_ = false; if (state_ == State::Exited || state_ == State::Faulted) return state_; if (state_ == State::Busy) { const int code = services_ ? services_->poll() : DSN_ERR_IO; if (code == kPending) return state_; // still working; try again next tick vm_.x[10] = static_cast(code); // result ready: resume the VM below } else if (state_ == State::Waiting) { - const uint32_t ev = eventOf(input, viewOf(device), swipe, sideways); + uint32_t ev = eventOf(input, viewOf(device), swipe, sideways); + if (!ev && timer) ev = DSN_EV_TIMER; if (!ev) return state_; vm_.x[10] = ev; // wait_event's return value } diff --git a/freeink/DessenFreeInk.h b/freeink/DessenFreeInk.h index e891b88..52f8848 100644 --- a/freeink/DessenFreeInk.h +++ b/freeink/DessenFreeInk.h @@ -51,6 +51,14 @@ class FreeInkHost { // the step consumes it whatever state the app is in. void markSideways() { sideways_ = true; } + // Host schedules monotonic wakes using this interval (0 disables). Mark + // elapsed immediately before step(); no catch-up or queued timer events. + // A host must opt in once it implements the scheduler. + void enableTimerEvents() { timerSupported_ = true; } + uint32_t timerInterval() const { return timerInterval_; } + void markTimerElapsed() { timerElapsed_ = true; } + int setTimerInterval(uint32_t interval); + // Entropy for the app's random() call, e.g. esp_random. Without one the // app gets a weak fallback sequence, fine for tests and nothing else. void setRandom(uint32_t (*source)()) { random_ = source; } @@ -111,6 +119,9 @@ class FreeInkHost { bool clipped_ = false; bool clipTouched_ = false; bool sideways_ = false; + uint32_t timerInterval_ = 0; + bool timerElapsed_ = false; + bool timerSupported_ = false; public: void randomBytesInto(uint8_t* out, uint32_t n); // fills from service or fallback diff --git a/freeink/dessen_dispatch.inc b/freeink/dessen_dispatch.inc index 5aa3ead..a9be24f 100644 --- a/freeink/dessen_dispatch.inc +++ b/freeink/dessen_dispatch.inc @@ -642,3 +642,10 @@ case 105: { // book_cover a[0] = result; break; } +case 109: { // timer_set + const uint32_t interval_ms = static_cast(a[0]); (void)interval_ms; + uint32_t result = 0; + result = self.setTimerInterval(interval_ms); + a[0] = result; + break; +} diff --git a/freeink/test_host.cpp b/freeink/test_host.cpp index f722d5e..5984c3b 100644 --- a/freeink/test_host.cpp +++ b/freeink/test_host.cpp @@ -308,8 +308,47 @@ static void testManifest() { assert(parseRequires(R"({"requires":["stylus"]})", needs) && !meets(needs, true, true, true)); } +static void testTimer(const char* path) { + // Without a scheduler, a guest can detect the missing capability. + { + dessen::FreeInkHost unsupported; + assert(unsupported.load(mem, readApp(path), sizeof mem)); + TraceTarget target; + DeviceContext dev; + dev.width = 400; + dev.height = 300; + InputSnapshot none; + assert(unsupported.step(target, dev, none) == dessen::State::Exited); + assert(unsupported.exitCode() == 0 && unsupported.timerInterval() == 0); + } + dessen::FreeInkHost host; + assert(host.load(mem, readApp(path), sizeof mem)); + TraceTarget target; + DeviceContext dev; + dev.width = 400; + dev.height = 300; + host.enableTimerEvents(); + InputSnapshot none, confirm, back; + confirm.confirm = true; + back.back = true; + auto step = [&](const InputSnapshot& in) { return host.step(target, dev, in); }; + assert(step(none) == dessen::State::Waiting && host.timerInterval() == 1000); + assert(step(none) == dessen::State::Waiting); + host.markTimerElapsed(); + assert(step(confirm) == dessen::State::Waiting); + assert(step(none) == dessen::State::Waiting && host.timerInterval() == 1000); + host.markTimerElapsed(); + assert(step(none) == dessen::State::Waiting && host.timerInterval() == 5000); + host.markTimerElapsed(); + assert(step(none) == dessen::State::Waiting && host.timerInterval() == 0); + host.markTimerElapsed(); + assert(step(none) == dessen::State::Waiting); + assert(step(back) == dessen::State::Exited && host.exitCode() == 0); + assert(host.load(mem, readApp(path), sizeof mem) && host.timerInterval() == 0); +} + int main(int argc, char** argv) { - assert(argc == 9); + assert(argc == 10); DeviceContext screen; screen.width = 480, screen.height = 800; TraceTarget target; @@ -504,6 +543,7 @@ int main(int argc, char** argv) { testDesktopStorage(); testHome(argv[8]); testManifest(); + testTimer(argv[9]); puts("freeink binding: PASS"); return 0; diff --git a/sdk/dessen.rs b/sdk/dessen.rs index bec687a..41c64c9 100644 --- a/sdk/dessen.rs +++ b/sdk/dessen.rs @@ -56,6 +56,7 @@ pub const DSN_SYS_BOOK_PROGRESS: usize = 100; pub const DSN_SYS_OPEN_HOME: usize = 101; pub const DSN_SYS_WIFI_CONNECT: usize = 103; pub const DSN_SYS_BOOK_COVER: usize = 105; +pub const DSN_SYS_TIMER_SET: usize = 109; #[inline] unsafe fn dsn_call0(n: usize) -> i32 { @@ -293,3 +294,5 @@ pub fn dsn_open_home(dest: i32) -> i32 { unsafe { dsn_call1(DSN_SYS_OPEN_HOME, ( pub fn dsn_wifi_connect() -> i32 { unsafe { dsn_call0(DSN_SYS_WIFI_CONNECT) } } #[inline] pub fn dsn_book_cover(path: &[u8], height: i32, pixels: &mut [u8]) -> i32 { unsafe { dsn_call5(DSN_SYS_BOOK_COVER, path.as_ptr() as usize, path.len(), ((height as u32 & 0xffff)) as usize, pixels.as_mut_ptr() as usize, pixels.len()) } } +#[inline] +pub fn dsn_timer_set(interval_ms: u32) -> i32 { unsafe { dsn_call1(DSN_SYS_TIMER_SET, interval_ms as usize) } } diff --git a/sdk/dessen.zig b/sdk/dessen.zig index 8d54efd..1dba82b 100644 --- a/sdk/dessen.zig +++ b/sdk/dessen.zig @@ -56,6 +56,7 @@ pub const DSN_SYS_BOOK_PROGRESS: usize = 100; pub const DSN_SYS_OPEN_HOME: usize = 101; pub const DSN_SYS_WIFI_CONNECT: usize = 103; pub const DSN_SYS_BOOK_COVER: usize = 105; +pub const DSN_SYS_TIMER_SET: usize = 109; fn call0(n: usize) i32 { return asm volatile ("ecall" @@ -189,3 +190,4 @@ pub fn book_progress(path: []const u8) i32 { return call2(DSN_SYS_BOOK_PROGRESS, pub fn open_home(dest: i32) i32 { return call1(DSN_SYS_OPEN_HOME, @as(usize, (@as(u32, @bitCast(dest)) & 0xff))); } pub fn wifi_connect() i32 { return call0(DSN_SYS_WIFI_CONNECT); } pub fn book_cover(path: []const u8, height: i32, pixels: []u8) i32 { return call5(DSN_SYS_BOOK_COVER, @intFromPtr(path.ptr), path.len, @as(usize, (@as(u32, @bitCast(height)) & 0xffff)), @intFromPtr(pixels.ptr), pixels.len); } +pub fn timer_set(interval_ms: u32) i32 { return call1(DSN_SYS_TIMER_SET, @as(usize, interval_ms)); } diff --git a/sdk/dessen_abi.h b/sdk/dessen_abi.h index 5f4ff56..1b547f8 100644 --- a/sdk/dessen_abi.h +++ b/sdk/dessen_abi.h @@ -81,6 +81,7 @@ typedef struct { // On PREV/NEXT: the key was Left or Right rather than Up or Down. Apps that // read only DSN_EV_TYPE see the same PREV/NEXT either way. #define DSN_EV_SIDEWAYS 0x100u +#define DSN_EV_TIMER 11 // opt-in periodic wake; no coordinates #define DSN_EV_TYPE(e) ((e) & 0xff) #define DSN_EV_X(e) (((e) >> 8) & 0xfff) #define DSN_EV_Y(e) (((e) >> 20) & 0xfff) diff --git a/sdk/dessen_calls.h b/sdk/dessen_calls.h index 39a5190..2e6f605 100644 --- a/sdk/dessen_calls.h +++ b/sdk/dessen_calls.h @@ -127,3 +127,4 @@ static inline int dsn_book_progress(const void *path, int path_len) { return (in static inline int dsn_open_home(int dest) { return (int)dsn_call1(DSN_SYS_OPEN_HOME, (long)(((unsigned)dest & 0xff))); } static inline int dsn_wifi_connect(void) { return (int)dsn_call0(DSN_SYS_WIFI_CONNECT); } static inline int dsn_book_cover(const void *path, int path_len, int height, void *pixels, int pixels_len) { return (int)dsn_call5(DSN_SYS_BOOK_COVER, (long)path, path_len, (long)(((unsigned)height & 0xffff)), (long)pixels, pixels_len); } +static inline int dsn_timer_set(unsigned interval_ms) { return (int)dsn_call1(DSN_SYS_TIMER_SET, (long)interval_ms); } diff --git a/sdk/dessen_sysnums.h b/sdk/dessen_sysnums.h index 22b37b9..8e9af1e 100644 --- a/sdk/dessen_sysnums.h +++ b/sdk/dessen_sysnums.h @@ -55,3 +55,4 @@ #define DSN_SYS_OPEN_HOME 101 #define DSN_SYS_WIFI_CONNECT 103 #define DSN_SYS_BOOK_COVER 105 +#define DSN_SYS_TIMER_SET 109