
RTOS ultraleggero per IoT con scheduling preemptive, TLS/DTLS, MQTT, CoAP, compatibilità POSIX e protezione della memoria basata su MPU. Kernel sotto i 10 KB.
Un sistema operativo real-time ultra-leggero per dispositivi IoT e embedded con risorse limitate.
Impronta del kernel inferiore a 10 KB, 2 KB di RAM minima, scheduling preemptive basato su priorità.
| Categoria | Dettagli |
|---|---|
| Kernel | Scheduling preemptive basato su priorità (256 livelli), round-robin a parità di priorità, ricerca priorità O(1) tramite bitmap, ereditarietà della priorità |
| Sincronizzazione | Mutex (con ereditarietà della priorità), semaforo, variabile di condizione, gruppi di eventi, code di messaggi |
| Timer software | One-shot e auto-reload, precisione millisecondo, modifica del periodo in esecuzione |
| Memoria | Allocatore first-fit con coalescenza immediata (heap 8 KB, allineato a 8 byte), rilevamento overflow stack, high-water mark per task |
| Shell | Shell interattiva VT100 — 23 comandi integrati, cronologia comandi (↑↓), completamento tab, editor di linea completo |
| Compatibilità POSIX | pthreads (create/join/detach/exit, mutex, cond var) · BSD socket API (socket/bind/listen/accept/connect/send/recv, inet_pton/ntop, htons/htonl) |
| File system | File system journaling su dispositivo a blocchi (WAL, crash recovery), condivisione blocchi COW, snapshot atomici, API simile a POSIX |
| Rete | Ethernet, IPv4, ICMP, UDP, TCP, HTTP client/server, DNS |
| TLS / DTLS | TLS 1.2/1.3 su TCP, DTLS 1.2 su UDP (backend mbedTLS) |
| MQTT | MQTT 3.1.1 completo — QoS 0/1/2 con tabella di ritrasmissione in-flight, coda offline, riconnessione automatica con back-off esponenziale |
| CoAP | Client/server conforme RFC 7252, pattern observe |
| OTA | Aggiornamenti firmware su partizioni A/B, verifica CRC32, rollback |
| Watchdog | Watchdog hardware e software, monitoraggio timeout per task |
| Alimentazione | Modalità Idle / Sleep / Deep-sleep, idle tickless, scaling della frequenza CPU |
| Sicurezza | Protezione memoria basata su MPU, supporto secure boot |
| HAL | Generic Hardware Abstraction Layer — ARM Cortex-M / RISC-V / AVR; selezione architettura in fase di compilazione, tabelle operative periferiche |
| Architettura | Esempi |
|---|---|
| ARM Cortex-M (M0/M0+/M3/M4/M7) | STM32, nRF52, Raspberry Pi Pico |
| RISC-V (RV32I) | ESP32-C3 |
| AVR (sperimentale) | ATmega |
sudo apt-get install -y gcc-arm-none-eabi binutils-arm-none-eabi
sudo apt-get install -y qemu-system
Verifica installazione:```bash
arm-none-eabi-gcc --version # 10.x or later
qemu-system-arm --version # 6.x or later
make
make ARCH=cortex-m0 # Cortex-M0/M0+ make ARCH=cortex-m7 # Cortex-M7 make ARCH=riscv32 # RISC-V RV32I (uses riscv32-unknown-elf-gcc) make ARCH=avr5 # AVR ATmega (uses avr-gcc)
make EXAMPLE=blink_led # LED blink + task scheduler demo make EXAMPLE=event_groups # Event group AND/OR/NOT/SYNC demo make EXAMPLE=iot_sensor # Multi-sensor IoT node make EXAMPLE=shell_demo # Interactive UART shell make EXAMPLE=mqtt_demo # MQTT publish/subscribe make EXAMPLE=condition_variable # Producer/consumer
make example-blink make example-events make example-shell make example-mqtt make example-iot
make size # Print ROM/RAM usage
### Esecuzione su QEMU
TinyOS viene eseguito sul target QEMU `mps2-an385` (ARM Cortex-M3, 4 MB di flash, 4 MB di RAM):
| File | Descrizione |
|---|---|
| `build/tinyos.elf` | Immagine ELF con simboli di debug |
| `build/tinyos.bin` | Binario grezzo per il flashing |
| `build/tinyos.map` | Mappa del linker (dimensioni dei simboli) |```bash
# Run indefinitely (Ctrl-A X to quit)
qemu-system-arm \
-machine mps2-an385 \
-cpu cortex-m3 \
-nographic \
-kernel build/tinyos.elf
# Run for a fixed duration (e.g. 10 seconds)
timeout 10 qemu-system-arm \
-machine mps2-an385 \
-cpu cortex-m3 \
-nographic \
-kernel build/tinyos.elf
# Debug: trace interrupts
qemu-system-arm \
-machine mps2-an385 \
-cpu cortex-m3 \
-nographic \
-d int \
-kernel build/tinyos.elf
Risultato atteso dalla traccia degli interrupt: righe successful exception return ripetute confermano che lo scheduler è in esecuzione, SysTick sta ticchettando e i cambi di contesto PendSV sono completati correttamente.
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg
-c "program build/tinyos.bin verify reset exit 0x08000000"
pyocd flash --target cortex_m build/tinyos.bin
### Compilazione con TLS (mbedTLS)
Il supporto TLS viene abilitato automaticamente quando mbedTLS è presente in `~/mbedtls`.
Per utilizzare un percorso diverso:```bash
# Clone and build mbedTLS
git clone https://github.com/Mbed-TLS/mbedtls ~/mbedtls
make -C ~/mbedtls
# Build TinyOS with TLS
make MBEDTLS_DIR=~/mbedtls
Esempio di attività minima:```c #include "tinyos.h"
void my_task(void param) { while (1) { / work */ os_task_delay_ms(100); } }
int main(void) { tcb_t task; os_init(); os_task_create(&task, "my_task", my_task, NULL, PRIORITY_NORMAL); os_start(); }
---
## Panoramica API
### Gestione delle Attività```c
os_task_create(tcb, name, entry, param, priority)
os_task_delete(task)
os_task_suspend(task) / os_task_resume(task)
os_task_delay(ticks) / os_task_delay_ms(ms)
os_task_set_priority(task, priority)
os_task_get_stats(task, stats)
os_task_get_stats_by_index(index, stats) /* iterate all tasks by index */
os_task_find_by_name(name) /* returns tcb_t*, NULL if not found */
os_get_system_stats(stats)
os_get_memory_stats(&free, &used, &allocs, &frees)
os_mutex_init(mutex) / os_mutex_lock(mutex, timeout) / os_mutex_unlock(mutex) os_semaphore_init(sem, count) / os_semaphore_wait(sem, timeout) / os_semaphore_post(sem) os_cond_init(cond) / os_cond_wait(cond, mutex, timeout) os_cond_signal(cond) / os_cond_broadcast(cond) os_event_group_set_bits(eg, bits) / os_event_group_wait_bits(eg, bits, opts, out, timeout) os_queue_init(q, buf, item_size, max) / os_queue_send(q, item, timeout) os_queue_receive(q, item, timeout) / os_queue_peek(q, item, timeout)
### Timer```c
os_timer_create(timer, name, type, period_ms, callback, param)
os_timer_start(timer) / os_timer_stop(timer) / os_timer_reset(timer)
os_timer_change_period(timer, ms) / os_timer_get_remaining_ms(timer)
/* Register custom commands before calling shell_start() */ shell_register_cmd(name, handler_fn, help_text)
/* Provide UART I/O callbacks and start the shell task */ shell_io_t io = { .getc = uart_getc, .puts = uart_puts }; shell_start(&io)
/* Change the prompt at any time */ shell_set_prompt("mydevice> ")
/* Execute a single line programmatically */ shell_exec(line)
**Esempio di comando personalizzato:**```c
static int cmd_led(int argc, char *argv[]) {
if (argc < 2) return 1; /* non-zero → prints usage */
bool on = (strcmp(argv[1], "on") == 0);
gpio_write(LED_PIN, on);
return 0;
}
/* In main(), before shell_start(): */
shell_register_cmd("led", cmd_led, "led <on|off> Toggle LED");