SDK

Esta página aún no está en español, así que se muestra en inglés.

Overview

How the Kode Dot's firmware is built: the layers of the SDK, what each one holds, and which way they talk to each other.

What it is

The SDK is the code every Kode Dot firmware is built from: kodeOS, the examples, and your own app. It lives in one repository, kodediy/kode-sdk, as a set of ESP-IDF components. You add it to a project, call kode_init(), and the whole system runs inside your image: the drivers, the owners of every chip, the console, the radio link.

Each component is a layer. A layer uses the layers under it and never one above it, so a change to the screen code cannot break a driver, and a firmware with no screen can leave the screen layers out.

The build files enforce the order. Each component names in REQUIRES the layers it may use, and a script, tools/check_layers.sh, checks every #include against the rules before every merge.

kodeOS uses the SDK the same way your app does. It has no private door into it. Its own code, the screens and the launcher, sits above the SDK in the kodeOS repository.

The ideas behind it

The whole SDK is built on a few ideas. Every other page uses their names, so they are explained here once, in plain words.

Active Objects own the chips

An Active Object, or AO, is a small program inside the firmware with three things of its own:

  • a task, so it runs by itself, side by side with the others;
  • a queue where messages for it wait their turn;
  • a state machine, which decides what to do with a message from the state the AO is in. The same button press can mean "unlock" on one screen and "go back" on another.

An AO takes one message from its queue, handles it from start to finish, and only then takes the next. Nothing else touches its data halfway, so that data needs no lock.

Every chip on the board has one owner, and the owner is an AO: AO_Power owns the charger, AO_LedMgr the LED, AO_Storage the microSD card. Nobody else touches that chip. Whoever wants something from it sends the owner a message. Two owners for one chip stop the board at boot, naming both.

Events are the messages

A message between AOs is an event. It has a name, its signal (for example BUTTON_EVT_SIG), and it may carry data: a copy, not a pointer into someone else's memory (the events too big to copy are the exceptions, listed in sdk/design-rules). An event travels one of two ways:

WayGoes toUsed for
PostOne AO, by nameA request ("set the LED blue") and its answer
PublishEvery AO that subscribed to the signalNews ("a button was pressed", "the battery changed")

The sender never waits: it leaves the event in a queue and carries on. The SDK is built on QP/C, a framework for Active Objects, which moves the events and runs the state machines.

Workers do the waiting

Some jobs make you wait: a page from the network, a file on the card, a chip that is slow to answer. An AO must never wait, because while it waits it reads no messages, and its queue fills up.

So an AO that has slow jobs gets a worker: a helper task of its own that runs one job at a time and posts the result back to its AO as an event. The worker decides nothing. Which job comes next, how long to wait, whether to try again: the AO decides all of it, in its state machine.

sequenceDiagram
autonumber
participant app as Your app
participant ao as The owner AO
participant wk as Its worker
participant chip as The chip or the network
app->>ao: request (post)
ao->>wk: one job
wk->>chip: ask, then wait
chip-->>wk: data
wk-->>ao: done event (post)
ao-->>app: answer (post or publish)
Note over ao: keeps reading its queue while the worker waits
One slow request: the AO decides, the worker waits

Your app makes calls, the facade sends events

You do not have to send events yourself. Your app calls plain functions such as kode_led_set_rgb(), and the facade, the kode and kode_sys layers, turns each call into the right event for the right owner. A task of your own includes kode.h and its calls wait for the answer. An Active Object of your own includes kode_sys.h, whose calls return at once and are answered by an event, and the examples show how.

The words, in one table

WordIn one sentenceMore
Active Object (AO)A task with its own queue and state machine, handling one event at a timesdk/kaos/overview
OwnerThe one AO that may use a chip; it claims the chip at bootsdk/design-rules
EventA message with a signal and a copy of its datasdk/events
SignalThe name of an event, such as BUTTON_EVT_SIGsdk/events
PostSend an event to one AOsdk/kqp
Publish, subscribeSend an event to every AO that asked for that signalsdk/kqp
State machineThe part of an AO that decides what an event means nowsdk/kqp
WorkerAn AO's helper task: runs one slow job, posts the result back, decides nothingsdk/kqp
FacadeThe kode_* and kode_sys_* calls an app makes; each becomes an eventsdk/kode
LayerOne component of the SDK; it uses the layers below it, never abovesdk/overview

How it is connected

block-beta
columns 6
%% focus p_pw Power
%% focus p_led LED
%% focus p_btn Buttons
%% focus p_scr Screen
%% focus p_tch Touch
%% focus p_aud Audio
%% focus p_mot Motion
%% focus p_hap Haptic
%% focus p_ir Infrared
%% focus p_nfc NFC
%% focus p_rf RFID
%% focus p_rad Radio
%% focus p_net Network
%% focus p_sto Files
%% focus p_app Apps
%% focus p_exp J3 header
%% focus p_sys System

app["Your app, or kodeOS: app_main(), your tasks, your Active Objects"]:6
n_call["↓  function calls: the caller waits  ↓"]:6

t_kscr["kode_screen: screens, with no LVGL in your app"]:6
g_scr["Screen"] g_wid["Widgets"] g_can["Canvas"] g_img["Image, PNG"] space:2

t_kode["kode: kode.h, calls for your tasks"]:4 t_ui["kscreen · kui: LVGL on the glass"]:2
f_sys["System, events"] f_set["Settings"] f_btn["Buttons"] f_led["LED, colours"] u_ks["kscreen: display, touch feed"]:2
f_sto["Files"] f_mot["IMU, compass"] f_hap["Vibration"] f_aud["Audio"] u_kui["kui: the UI kit"] u_kody["Kody"]
f_rad["Wi-Fi, Bluetooth"] f_net["HTTP, MQTT, UDP"] f_pw["Battery, power"] f_exp["Pins, buses"] u_lv["LVGL 9.6"] u_tvg["ThorVG"]
f_ir["Infrared"] f_nfc["NFC, RFID"] f_tch["Touch"] f_bri["Brightness"] space:2

t_ksys["kode_sys: kode_sys.h, for your own Active Objects"]:6
s_init["kode_init()"] s_cmd["commands"] s_req["requests"] s_wrk["kode_worker.h"] s_cfg["config"] s_var["variables"]
s_fn["functions"] s_man["app manifest"] s_img["images, home"] s_sto["store"] s_time["time"] s_cert["kode_cert.h"]

n_evt["↓  events posted to one owner   ·   events published to every subscriber  ↑"]:6

t_kaos["kaos: the Active Objects, one owner per device, with its priority"]:6
AO_LowPower["AO_LowPower · 24"] AO_Rfid["AO_Rfid · 23"] AO_Haptic["AO_Haptic · 22"] AO_Imu["AO_Imu · 21"] AO_Audio["AO_Audio · 20"] AO_Nfc["AO_Nfc · 19"]
AO_Ir["AO_Ir · 18"] AO_IoExp["AO_IoExp · 17"] AO_Scheduler["AO_Scheduler · 16"] AO_Display["AO_Display · 15"] AO_Touch["AO_Touch · 14"] AO_Buttons["AO_Buttons · 13"]
AO_Boot["AO_Boot · 12"] AO_Console["AO_Console · 11"] AO_Autotest["AO_Autotest · 10"] AO_AppMgr["AO_AppMgr · 9"] AO_Power["AO_Power · 8"] AO_LedMgr["AO_LedMgr · 7"]
AO_Log["AO_Log · 6"] AO_Storage["AO_Storage · 5"] AO_Radio["AO_Radio · 4"] AO_Net["AO_Net · 2"] k_wrk["worker tasks"] k_claim["kaos_claim()"]

n_drv["↓  function calls  ↓"]:6

t_kdrv["kdrv: one driver per chip"]:4 t_kqp["kqp: QP/C in this system"]:2
d_bq["kdrv_bq25896"] d_cw["kdrv_cw2217"] d_led["kdrv_led"] d_btn["kdrv_buttons"] q_sig["signals"] q_pool["event pools"]
d_tca["kdrv_tca6408a"] d_disp["kdrv_display"] d_cst["kdrv_cst820"] d_es["kdrv_es8311"] q_prio["priorities"] q_tick["tick, timers"]
d_qmi["kdrv_qmi8658"] d_qmc["kdrv_qmc5883p"] d_aw["kdrv_aw86233"] d_ir["kdrv_ir"] q_wrk["workers"] q_wd["AO watchdog"]
d_nfc["kdrv_st25r3916"] d_rfal["rfal, from ST"] d_rf["kdrv_rfid_lf"] d_radio["kdrv_radio"] q_isr["posts from ISRs"] q_err["Q_onError"]
d_c5["kdrv_c5"] d_wifi["kdrv_wifi"] d_ble["kdrv_ble"] d_net["kdrv_net"] t_qpc["qpc: QP/C itself"]:2
d_sto["kdrv_storage"] d_sd["kdrv_sd, kdrv_fs"] d_apps["kdrv_apps"] d_slot["kdrv_slot, _kfw"] q_qpc["QP/C 8.1.4"] q_port["FreeRTOS port"]
d_clk["kdrv_clocks"] d_exp["kdrv_exp_bus"] d_jpeg["kdrv_jpeg, _ppa"] d_cam["kdrv_ov5640"] space:2

t_khal["khal: what several drivers share"]:6
h_i2c["khal_i2c"] h_gpio["khal_gpio"] h_sdmmc["khal_sdmmc"] h_pwm["khal_pwm"] h_exp["khal_exp"] h_con["khal_console"]
h_nvs["khal_nvs"] h_keep["khal_keep"] h_time["khal_time"] h_clk["khal_clk"] h_sync["khal_sync"] h_wrk["khal_worker"]
h_sys["khal_system"] h_pow["khal_power"] h_temp["khal_temp"] h_id["khal_identity"] h_crc["khal_crc32"] h_zlib["khal_zlib"]

t_klog["klog: the log"]:2 t_kbsp["kbsp: the board as constants"]:2 t_kdefs["kdefs: owned by no layer"]:2
l_mac["KLOG_* macros"] l_res["rescue path"] b_pins["kbsp_pins.h"] b_board["kbsp_board.h"] e_st["kode_status_t"]:2
l_lvl["levels D I N W E"]:2 b_own["kbsp_owners"] b_cfg["sdkconfig.board"] space:2

t_idf["ESP-IDF 6.1: only kbsp, khal, kdrv and klog include it"]:6
i_rtos["FreeRTOS"] i_i2c["I2C"] i_i2s["I2S"] i_spi["SPI"] i_dsi["MIPI-DSI"] i_sdmmc["SDMMC, FATFS"]
i_hosted["esp-hosted, SDIO"] i_ble["NimBLE"] i_net["lwIP, mbedTLS"] i_flash["flash, NVS"] i_adc["LEDC, ADC"] i_usb["USB-Serial/JTAG"]

t_hw["Kode Dot: the chips"]:6
c_bq["BQ25896 · 0x6B"] c_cw["CW2217 · 0x64"] c_led["KTD2026 · 0x30"] c_tca["TCA6408A · 0x20"] c_cst["CST820 · 0x15"] c_es["ES8311 · 0x18"]
c_qmi["QMI8658A · 0x6A"] c_qmc["QMC5883P · 0x2C"] c_aw["AW86233 · 0x5A"] c_co["CO5300 AMOLED"] c_nfc["ST25R3916B"] c_c5["ESP32-C5"]
c_sd["microSD"] c_flash["32 MB flash"] c_psram["32 MB PSRAM"] c_top["TOP · GPIO35"] c_lf["125 kHz coil"] c_j3["J3 · 14 pins"]
c_spk["Mic, speaker"] c_lra["LRA"] c_usb["USB-C"] c_dock["Dock pads"] c_xtal["Crystals"] c_p4["ESP32-P4"]

class app app
class n_call,n_evt,n_drv note
class t_kscr,t_kode,t_ui,t_ksys,t_kaos,t_kdrv,t_kqp,t_qpc,t_khal,t_klog,t_kbsp,t_kdefs,t_idf,t_hw layer
class AO_LowPower,AO_Rfid,AO_Haptic,AO_Imu,AO_Audio,AO_Nfc,AO_Ir,AO_IoExp,AO_Scheduler,AO_Display,AO_Touch,AO_Buttons,AO_Boot,AO_Console,AO_Autotest,AO_AppMgr,AO_Power,AO_LedMgr,AO_Log,AO_Storage,AO_Radio,AO_Net ao
class c_bq,c_cw,c_led,c_tca,c_cst,c_es,c_qmi,c_qmc,c_aw,c_co,c_nfc,c_c5,c_sd,c_flash,c_psram,c_top,c_lf,c_j3,c_spk,c_lra,c_usb,c_dock,c_xtal,c_p4 chip
class u_lv,u_tvg,d_rfal,q_qpc,q_port,i_rtos,i_i2c,i_i2s,i_spi,i_dsi,i_sdmmc,i_hosted,i_ble,i_net,i_flash,i_adc,i_usb ext

class f_pw,AO_Power,AO_LowPower,d_bq,d_cw,h_i2c,h_pow,i_i2c,c_bq,c_cw p_pw
class f_led,AO_LedMgr,d_led,h_i2c,i_i2c,c_led p_led
class f_btn,AO_Buttons,AO_IoExp,d_btn,d_tca,h_gpio,h_i2c,i_i2c,c_top,c_tca p_btn
class g_scr,g_wid,g_can,g_img,f_bri,u_ks,u_kui,u_kody,u_lv,u_tvg,AO_Display,AO_IoExp,d_disp,d_tca,d_jpeg,i_dsi,c_co,c_tca p_scr
class f_tch,u_ks,AO_Touch,d_cst,h_i2c,i_i2c,c_cst p_tch
class f_aud,AO_Audio,d_es,h_i2c,i_i2c,i_i2s,c_es,c_spk p_aud
class f_mot,AO_Imu,d_qmi,d_qmc,h_i2c,i_i2c,c_qmi,c_qmc p_mot
class f_hap,AO_Haptic,d_aw,h_i2c,i_i2c,c_aw,c_lra p_hap
class f_ir,AO_Ir,d_ir,d_radio,i_hosted,c_c5 p_ir
class f_nfc,s_cert,AO_Nfc,d_nfc,d_rfal,h_gpio,i_spi,c_nfc p_nfc
class f_nfc,s_cert,AO_Rfid,d_rf,h_pwm,i_adc,c_lf p_rf
class f_rad,AO_Radio,d_radio,d_c5,d_wifi,d_ble,h_gpio,i_hosted,i_ble,c_c5 p_rad
class f_net,AO_Net,d_net,i_net,i_hosted,c_c5 p_net
class f_sto,AO_Storage,d_sto,d_sd,h_sdmmc,h_gpio,i_sdmmc,i_flash,c_sd,c_flash p_sto
class s_img,AO_AppMgr,AO_Boot,d_apps,d_slot,i_flash,c_flash p_app
class f_exp,AO_Radio,AO_Power,d_exp,h_exp,c_j3 p_exp
class f_sys,f_set,s_init,s_cfg,s_var,s_fn,s_sto,s_time,AO_Console,AO_Log,AO_Scheduler,AO_Autotest,d_clk,h_con,h_clk,h_time,h_nvs,i_usb,c_usb,c_dock,c_xtal,c_p4 p_sys
The layers, from your app to the chips

Pick a part above the drawing, or click a box, to light what that part uses in each layer. sdk/paths follows one of those paths step by step, and lists the pages of all of them.

Two kinds of arrow run between the layers:

  • A function call goes down: the caller waits for the answer.
  • An event is a message left in a queue, and the sender does not wait.

A command goes down like this:

  1. Your app calls kode_led_set_rgb().
  2. The facade turns that into an event for AO_LedMgr, the Active Object that owns the LED.
  3. AO_LedMgr calls the LED driver.

What happens comes back up the same way. An Active Object publishes a button press, and any Active Object of yours that subscribed receives it.

Only four layers may include ESP-IDF headers (esp_*, driver/, freertos/): kbsp, khal, kdrv and klog. Everything above them sees the hardware through Kode's own types, and every public function returns kode_status_t.

The radio chip, the ESP32-C5, runs a firmware of its own. Its source is a second repository, kodediy/kode_slave_c5, included in the SDK as the submodule c5/.

The layers

One row per layer, with what it holds and the page that documents it.

LayerWhat it holdsPage
kdefsOne type, kode_status_t, that every public function returnssdk/kdefs
kbspThe board described as constants: pins, I2C addresses, screen size, the battery, who owns whatsdk/kbsp
klogThe log: macros, levels, and the path that still works inside a crashsdk/klog
khalWhat several drivers share: the I2C bus, the console bytes, time, NVS, PSRAMsdk/khal
kqp, qpcQP/C, the Active Object framework, and how this system uses it: pools, priorities, the tick. Every signal is listed in sdk/eventssdk/kqp
kdrvOne driver per chip, and the only layer that knows a part numbersdk/kdrv/overview
kaosThe Active Objects: one owner per device, each deciding when its device is usedsdk/kaos/overview
kode, kode_sysThe facade: kode_init() and the commands and requests of an Active Object (kode_sys), and the blocking calls of an app's tasks (kode)sdk/kode
kode_screenThe screen of the standard layer: widgets, canvases and PNG files, with no LVGL in your codesdk/kode
kuiThe UI kit: colours, fonts, the pill and the sheet, and Kody's playersdk/kui
kscreenLVGL on the screen, for an Active Object of your ownsdk/kscreen

Two folders in the SDK are other people's code, kept under their own names: qpc, Quantum Leaps' QP/C, and rfal, ST's library for the NFC chip. Only kqp includes the first, and only kdrv the second.

The rules that keep the layers apart, and the checks that enforce them, are in sdk/design-rules.

Where a new file goes

Ask in order. The first yes is the answer.

  1. It defines a pin or an I2C address: kbsp.
  2. Several drivers share it, or it could have a second implementation: khal.
  3. It is the code for one chip: kdrv.
  4. It owns a device or decides when something happens: kaos.
  5. It is what an app calls: kode from a task, kode_sys from an Active Object.
  6. It is a program on top of the system: a component of its own, above the SDK.

A type goes with the layer that defines it. A type no layer owns goes in kdefs, which is why kdefs exists at all.

In the SDK