Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions abi/freeink.api
Original file line number Diff line number Diff line change
Expand Up @@ -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);
15 changes: 15 additions & 0 deletions apps/timertest.c
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions docs/writing-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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.
15 changes: 14 additions & 1 deletion freeink/DessenFreeInk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(nextRandom());
Expand All @@ -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<uint32_t>(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
}
Expand Down
11 changes: 11 additions & 0 deletions freeink/DessenFreeInk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions freeink/dessen_dispatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,10 @@ case 105: { // book_cover
a[0] = result;
break;
}
case 109: { // timer_set
const uint32_t interval_ms = static_cast<uint32_t>(a[0]); (void)interval_ms;
uint32_t result = 0;
result = self.setTimerInterval(interval_ms);
a[0] = result;
break;
}
42 changes: 41 additions & 1 deletion freeink/test_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -504,6 +543,7 @@ int main(int argc, char** argv) {
testDesktopStorage();
testHome(argv[8]);
testManifest();
testTimer(argv[9]);

puts("freeink binding: PASS");
return 0;
Expand Down
3 changes: 3 additions & 0 deletions sdk/dessen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) } }
2 changes: 2 additions & 0 deletions sdk/dessen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)); }
1 change: 1 addition & 0 deletions sdk/dessen_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions sdk/dessen_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
1 change: 1 addition & 0 deletions sdk/dessen_sysnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -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