Skip to main content

Firmware

The HIL firmware runs on ATmega328P microcontrollers (Arduino Uno/Nano) and is compiled using TinyGo. It bridges the serial wire protocol from the host monitor with on-chip hardware peripherals (Atmel, 2016).

Firmware Architecture​

Memory Layout​

The ATmega328P provides 32KB flash, 2KB SRAM, and 1KB EEPROM. The TinyGo runtime occupies approximately 16KB of flash, leaving sufficient space for the autopilot script engine and runtime data (Atmel, 2016).

┌──────────────────────────┐ 0x0000
│ Interrupt Vector Table │
├──────────────────────────┤ 0x0020
│ Program Code (TinyGo) │
│ - Frame Parser │
│ - Pin Controller │
│ - Autopilot Engine │
│ - Event System │
├──────────────────────────┤ ~0x4000
│ Global Variables (SRAM) │
│ - Pin State Buffers │
│ - ADC History │
│ - Event Queues │
├──────────────────────────┤
│ Stack (grows downward) │
└──────────────────────────┘ 0x08FF

Supported Hardware​

PeripheralATmega328PRP2040ESP32
Digital I/O14 pins26 pins34 pins
Analog Input6 (10-bit)3 (12-bit)18 (12-bit)
PWM6 channels16 channels16 channels
I2C1 bus2 buses2 buses
SPI1 bus2 buses2 buses
UART123
Flash32KB2MB4MB
SRAM2KB264KB520KB

Autopilot Mode​

Autopilot mode allows a HIL LISP script to run directly on the microcontroller without host intervention. This is essential for standalone deployments where the host computer is disconnected after programming (Rashwan, 2026).

Flashing a Script​

# Flash a script to run autonomously on the device
hil-cli firmware flash my-script.hil

# Flash with specific board type
hil-cli firmware flash --board nano my-script.hil

# Flash with OTA support (for later remote updates)
hil-cli firmware flash --ota-enabled my-script.hil

Autopilot Execution​

When the device boots in autopilot mode, the firmware:

  1. Initializes hardware peripherals (pins, ADC, I2C)
  2. Loads the stored script from persistent memory
  3. Executes the script in a loop
  4. Reports telemetry over serial when connected
  5. Responds to OTA update requests

Event System​

The firmware supports hardware events that trigger without polling:

; Arm a digital event on pin 2 (rising edge)
(EVT-ARM 0 2 1)

; Arm an analog threshold event on A0 (above 512)
(EVT-ARM 1 0 512)

; Poll for events
(EVT-POLL) ; returns event type + pin, or 0 if none

I2C Communication​

The firmware supports I2C for connecting sensors, displays, and other peripherals:

; Initialize I2C bus
(I2C-INIT)

; Write to device at address 0x3C (common OLED address)
(I2C-START 0x3C 0)
(I2C-WRITE 0x00) ; command mode
(I2C-WRITE 0xAF) ; display on
(I2C-STOP)

; Read 2 bytes from device 0x68 (MPU6050 accelerometer)
(I2C-START 0x68 0)
(I2C-WRITE 0x3B) ; register address
(I2C-START 0x68 1) ; restart in read mode
(SET hi (I2C-READ 1))
(SET lo (I2C-READ 0))
(I2C-STOP)

Build Process​

The firmware build pipeline compiles HIL LISP to TinyGo-compatible code:

# Transpile only (output C++ source)
hil-cli firmware build script.hil --output script.cpp

# Full build to HEX
hil-cli firmware build script.hil --output script.hex

# Build with board specification
hil-cli firmware build script.hil --board arduino-nano --output script.hex

References

Atmel. (2016). ATmega328P datasheet (Rev. 8271D). Microchip Technology.

Rashwan, S. (2026). HIL: A LISP-based hardware-in-the-loop operating system. https://github.com/SRashwan/hilp