m5paper-os
C++, ESP-IDF · M5Stack M5PaperS3 · in progress · mattijsf/m5paper-os · docs
PaperOS is firmware for the M5PaperS3, a small e-ink tablet with an ESP32-S3, 8 MB of PSRAM, 16 MB of flash and a 4.7” 540×960 touch screen. It has an e-book reader, a Home Assistant remote, a WhatsApp and mail inbox, an agenda, weather, Teletekst, Hacker News, a few daily games and chess.
“OS” here means a launcher, an app framework, a widget kit, a display manager tuned for e-ink and a set of system services. Apps are C++ classes compiled into one image; there is no dynamic loading. The same source also builds a desktop simulator, and that’s where most of the development happens.
brew install sdl2-compat uvuv tool install --python 3.12 platformiomake run # simulator windowmake flash # build and flash a connected PaperS3PaperOS on the M5PaperS3, filmed by the test webcam.
Layers
Section titled “Layers”Nothing in core, ui, apps or hal may include Arduino.h, SDL.h, an ESP-IDF header or
curl. Only src/platform/ can. make check builds the firmware and the simulator together, and a
platform include in shared code breaks one of the two builds.
Runtime
Section titled “Runtime”App code is single-threaded. Everything else runs beside it on two other threads.
| Thread | Where | Does |
|---|---|---|
| UI thread | core 1, 16 KB stack | input, app code, drawing into the canvas, the DisplayManager |
| Worker | core 0, 16 KB stack | blocking jobs: HTTP requests, indexing an EPUB (7–17 s) |
| EPD task | M5GFX, device only | plays the e-ink waveforms for regions the DisplayManager queues |
An app that needs something slow calls ctx.async(job, onDone). The job runs on the worker and
onDone runs later on the UI thread with the result. If the app was closed in the meantime, a
generation counter makes sure onDone never runs. The worker is pinned to core 0, away from the
UI thread, and a long book index doesn’t stall the screen.
There’s no frame rate. Each pass of the main loop:
- turns touch events into gestures and hands them to the current screen,
- runs finished worker callbacks,
- runs due timers and the network service’s tick,
- renders and commits to the display if anything was invalidated,
- goes idle until the next deadline: light sleep on the device when nothing else is going on, a short wait otherwise.
Display
Section titled “Display”The panel can update in three ways. These are measured on the device:
| Mode | Shows | Time | Used for |
|---|---|---|---|
| Fast | black and white, grays as a dither | 0.27 s for a 200×200 box, 0.5 s full screen | responding to a tap |
| Text | 16 grays, no flash | 0.9–1.4 s | settling after a Fast update |
| Quality | 16 grays, exact, with a flash | about 1.4 s full screen | clearing ghosting, images |
Widgets draw into one 8-bit canvas of 540×960 in PSRAM and mark what changed. On commit, the DisplayManager runs each changed region through the same steps:
- clip to screen
- diff against the last frame
- pick a mode
- push
- update ghost score
The last frame is kept as an exact copy, and the diff only sends pixels that really changed. Values are compared after quantising to the panel’s 16 levels, and anti-aliasing noise below one level never triggers an update.
Fast updates leave faint remains, and they add up. Every Fast pass adds the share of the screen it changed to a score, and a Text settle takes half of that back. At 8, the next screen change becomes a full Quality pass, and the flash lands during navigation. At 16 the display cleans up at the next idle moment. The reader has a separate rule and does a full clean every tenth page.
Tap latency, measured from finger up until the update reaches the panel driver, before and after tuning:
| Tap | Before | Now |
|---|---|---|
| Reader page turn | 516 ms | 105–112 ms |
| Open a screen | 807 ms | 150 ms |
| List page turn | 415 ms, three passes | 105 ms, one pass |
The battery is 1800 mAh and the device draws about 154 mA with Wi-Fi on, which would empty it in under 12 hours. An e-ink image stays on screen while the chip sleeps. The goal is to be asleep almost all the time and keep Wi-Fi off.
| State | Entered when | Woken by | Current |
|---|---|---|---|
| Active | input, a worker job, the display busy | 40–160 mA | |
| Idle, light sleep | the UI thread has nothing to do | touch, next timer, 60 s tick | est. 1–5 mA, not measured yet |
| Off | auto-off, power button, battery below 3.3 V | side button, RTC alarm | about 9 µA |
Touch is on GPIO 48, which isn’t an RTC pin. It can wake the chip from light sleep but not from deep sleep or power-off. Idle is light sleep, and there is no deep-sleep state. Off can still wake on a timer: optionally every 1 to 8 hours to redraw the sleep screen with fresh weather and messages, then power off again within 45 seconds.
Apps never turn Wi-Fi on. They call ctx.http(...) and the network service connects when needed.
It first tries the last network that worked, using its stored BSSID and channel (about 1.2 s).
If that fails, it scans once and joins the strongest known network (about 5.3 s). A request that
failed before anything was sent gets one retry. Twenty seconds after the last request, Wi-Fi goes
off again.
Memory and flash
Section titled “Memory and flash”Internal RAM is the scarce part. About 115 KB is free at idle, and a TLS read takes 32–46 KB of
that. So ordinary malloc and new go to the 8 MB of PSRAM, and internal RAM is left for stacks,
Wi-Fi and DMA.
| In PSRAM | Size |
|---|---|
| Canvas | 518 KB |
| Copy of the last frame | 518 KB |
| M5GFX e-ink buffers | about 1 MB |
| One reader chapter with layout | up to 1 MB |
| JSON from Home Assistant or the inbox | up to 256 KB |
The 16 MB of flash holds two 5 MB app slots for updates and a 5.9 MB LittleFS partition for data. The firmware is 2.55 MiB, kept under a 3 MiB budget. The device build has no C++ exceptions and never includes iostreams, regex or locale, which cost 100 to 160 KB each.
An app is one folder under src/apps/ with one class. Adding one never touches the core.
Simulator recording.
class HelloScreen : public Screen { public: void build(ui::Column& root) override { setTitle("Hello"); label_ = root.add<ui::Label>("Tap the button", ui::Type::Body); root.add<ui::Button>("Count", [this] { ++count_; label_->setText("Tapped " + std::to_string(count_) + " times"); }); }
private: ui::Label* label_ = nullptr; int count_ = 0;};
class HelloApp : public App { public: void onStart(AppContext& ctx) override { ctx.nav.push<HelloScreen>(); }};
POS_REGISTER_APP(HelloApp, {.id = "hello", .name = "Hello", .icon = Icon::Info, .order = 90});The full version of this example, with its includes and namespace, is compiled and run in the unit tests. App code follows a few rules that come from the panel and the chip: no animation, no scrolling, nothing that blocks the UI thread, and colours only from 16 grays.
Updates over Wi-Fi
Section titled “Updates over Wi-Fi”make ota builds the firmware, signs it and sends it to the device. The image comes with a
148-byte header holding the build, size, SHA-256 and time, signed with Ed25519. The device checks
the signature before it writes a single byte, and the hash before it makes the image bootable.
The image goes into the app slot that isn’t running. The new image boots as pending and is only confirmed after it has run for 15 seconds; if it resets before that, the bootloader falls back to the previous slot. A 2.5 MB update takes 25 to 47 seconds. Older images signed with the same key are accepted on purpose, because going back to an earlier build is useful during development.
Messages through Home Assistant
Section titled “Messages through Home Assistant”WhatsApp and mail reach the Paper through a companion app on my Android phone. It reads the notifications, encrypts them for the device and posts the ciphertext to Home Assistant, which only relays it.
| Part | Algorithm | On the Paper | On Android |
|---|---|---|---|
| Key agreement | X25519 | libsodium | KeyAgreement "XDH" |
| Key derivation | HKDF-SHA256 | libsodium | HMAC-SHA256 |
| Encryption | ChaCha20-Poly1305 | libsodium | Cipher "ChaCha20-Poly1305" |
The Paper uses libsodium because the mbedTLS build that ships with ESP-IDF has ChaCha20 and Poly1305 switched off. Both sides are tested against the same vectors, byte for byte.
Testing
Section titled “Testing”make check builds the simulator, runs 383 unit tests, replays the UI scripts against 334 golden
screenshots and builds the firmware. A change isn’t done until it passes. make shot renders a
screen headless to a PNG quantised to the panel’s 16 levels, and that’s how UI work gets checked
without the device.
The device is for what the simulator can’t show: refresh behaviour, touch feel and power.
make device-shot dumps the device’s frame buffer over USB, and make cam-compare films the panel
with a webcam and puts a number on the ghosting per region.

The calibration target from tools/cam.py target, seen by the webcam. It is used to find the
panel in the frame and to measure lens distortion, lighting, the 16 gray levels and sharpness.
The somdoku app is a C++ port of the web game. Because its puzzles are seeded by date, the port can be checked exactly: its generator produces the same puzzles as the web game for 500 days.
No trackers or cookies. Older designs of this site.