Serial Wire Protocol
The serial wire protocol is a compact ASCII-based frame format for communication between the host monitor and firmware over UART. It provides reliable command delivery with CRC error detection (Stallings, 2020).
Frame Format
ACTION TYPE PIN VAL1 VAL2|CRC\r
| Field | Length | Description | Values |
|---|---|---|---|
| ACTION | 1 char | Operation type | W (write), R (read), T (tone), S (pulse), E (event) |
| TYPE | 1 char | Pin type | D (digital), A (analog), P (PWM) |
| PIN | 1-2 chars | Pin number | 0–13 (digital), 0–5 (analog) |
| VAL1 | 1-3 chars | Primary value | State (0/1), threshold (0–1023), frequency (Hz) |
| VAL2 | 1-3 chars | Secondary value | Duration (ms), edge type |
| CRC | 2 hex chars | XOR checksum | XOR of all bytes before | |
\r | 1 char | Terminator | Carriage return (0x0D) |
Command Reference
Digital Write
W D pin state|CRC\r
(WRITE D 13 1) ; → "W D 13 1 0|5D\r"
(WRITE D 13 0) ; → "W D 13 0 0|5E\r"
Digital Read
R D pin 0|CRC\r
The firmware responds with the pin state:
R D 2 0|00\r ; pin 2 is LOW
R D 2 1|01\r ; pin 2 is HIGH
Analog Read (ADC)
R A pin 0|CRC\r
Returns a 10-bit value (0–1023):
R A 0 512|02\r ; A0 reads 512
PWM Output
W P pin duty|CRC\r
(WRITE P 9 128) ; 50% duty cycle on pin 9
; → "W P 9 128 0|04\r"
Tone Generation
T pin freq duration|CRC\r
(TONE D 8 1000 500) ; 1kHz tone for 500ms
; → "T D 8 1000 500|5C\r"
Pulse Measurement
S D pin timeout|CRC\r
Measures the duration of a pulse on the given pin.
Event Arm
E type pin threshold|CRC\r
| Event Type | Meaning |
|---|---|
| 0 | Digital rising edge |
| 1 | Digital falling edge |
| 2 | Digital both edges |
| 3 | Analog threshold (above) |
| 4 | Analog threshold (below) |
CRC Computation
The CRC is computed as the XOR of every byte in the command portion (everything before the |):
def compute_crc(command_bytes):
crc = 0
for b in command_bytes:
crc ^= b
return format(crc, '02X')
Example:
Command: W D 13 1 0
Bytes: 0x57 0x44 0x31 0x33 0x20 0x31 0x20 0x30
CRC: 0x57 ^ 0x44 ^ 0x31 ^ 0x33 ^ 0x20 ^ 0x31 ^ 0x20 ^ 0x30 = 0x5D
Frame: W D 13 1 0|5D\r
Communication Sequence
Baud Rate and Timing
| Parameter | Value |
|---|---|
| Baud rate | 115200 bps |
| Data bits | 8 |
| Stop bits | 1 |
| Parity | None |
| Max frame length | 32 bytes |
| Response timeout | 100ms |
The 115200 baud rate is the standard for Arduino serial communication and provides sufficient throughput for real-time control at approximately 11,520 characters per second (Atmel, 2016).
Error Handling
If the firmware receives a frame with an invalid CRC, it silently discards the frame and does not send a response. The host monitor will retry the command after the timeout period:
Optional Compression Mode (Game-Engine Style)
Inspired by how Unreal Engine (Oodle/ZSTD) and Unity (LZ4) compress asset streaming and netcode state sync, HIL adds an optional LZ4-style compression layer for bulk serial transfer. It is fully backward compatible: nodes with older firmware simply never see compressed frames unless you opt in.
Wire Format
A compressed frame replaces one or more plain frames:
~<MODE><LZ4-block>|CRC\r
| Field | Length | Description |
|---|---|---|
~ | 1 byte | Compression magic (0x7E) |
| MODE | 1 byte | 0x01 = LZ4, 0x02 = DELTA, 0x03 = RLE |
| payload | ≤118 B | LZ4 block or RLE stream (fits the node's 128-byte line buffer) |
| CRC | 2 hex | XOR checksum over magic + mode + payload |
The firmware detects the leading ~, decompresses the payload into its scratch RAM, and re-processes each embedded command line. The decompressor is allocation-free and adds ~1 KB of flash.
Language Macros
(COMPRESS-START LZ4) ; begin compression (LZ4 | DELTA | RLE)
(BATCH-BEGIN) ; batches now auto-compress
(FOR i 0 49 (DO ...))
(BATCH-END) ; buffered commands flush as compressed frame(s)
(COMPRESS-END) ; disable
(COMPRESS-SEND "W D 13 1\n...") ; manually push bulk data through compressor
(STREAM-START 50 256) ; game-netcode style: telemetry frames flush every 50ms
(STREAM-FLUSH) ; force-flush current frame
(STREAM-STOP) ; stop and flush
(COMPRESS-STATS) ; print raw vs wire byte counters + ratio
(COMPRESS-RESET) ; zero the stats
Performance Characteristics
Measured on representative workloads:
| Workload | Raw bytes | Wire bytes | Ratio |
|---|---|---|---|
| 42-command LED batch | 512 | 70 | 13.7% (~7x) |
| Repeated sensor stream | 870 | 49 | 5.6% (~18x) |
| Long repeated runs (RLE-friendly) | 1600 | 40 | 2.5% (~40x) |
At 9600 baud this turns a ~640 ms batch transfer into ~75 ms; at 115200 baud it keeps high-frequency telemetry well under the wire ceiling.
Design Guarantees
- Optional: without
COMPRESS-STARTnothing changes on the wire. - Safe fallback: payloads that don't compress by at least 10%, or exceed the 118-byte frame budget, are sent uncompressed automatically.
- Chunked: large batches are split greedily so every frame fits the node's 128-byte RX line buffer and decompresses within its scratch RAM.
- Integrity: each frame carries the same XOR CRC as plain frames; corrupt frames report
ERR:CRC. - Standard format: the payload is a valid LZ4 block (token/literal/match encoding), so the PC side can interoperate with any standard LZ4 tooling for debugging.
ASCII Art Protocol Diagram
Host Monitor Firmware (ATmega328P)
│ │
│──── "W D 13 1 0|5D\r" ───────>│
│ │── digitalWrite(13, HIGH)
│<─────── "ACK\r" ──────────────│
│ │
│──── "R A 0 0|02\r" ──────────>│
│ │── val = analogRead(0)
│<───── "R A 0 723|02\r" ──────│
│ │
References
Atmel. (2016). ATmega328P datasheet (Rev. 8271D). Microchip Technology.
Stallings, W. (2020). Computer organization and architecture (11th ed.). Pearson.