Skip to main content

Hardware Abstraction Layer

The Hardware Abstraction Layer (HAL) provides a uniform interface for interacting with microcontroller peripherals through HIL LISP. The HAL translates high-level operations into the serial wire protocol frames understood by the firmware (Hennessy & Patterson, 2017).

Abstraction Model​

Pin Types and Mapping​

HIL TypeFirmware TypeDescriptionRead RangeWrite Range
DDigitalBinary I/O0 or 10 or 1
AAnalogADC input0–1023—
PPWMPulse-width modulation—0–255

Pin Numbering​

ATmega328P (Arduino Uno/Nano):

Digital PinsPWM PinsAnalog Pins
D0–D13D3, D5, D6, D9, D10, D11A0–A5
; Digital write to built-in LED
(WRITE D 13 1)

; Read analog sensor on A0
(SET val (READ A 0))

; PWM output on pin 9
(WRITE P 9 128) ; 50% duty cycle

Hardware Operations​

Digital I/O​

; Configure pin mode (1 = OUTPUT, 0 = INPUT)
(PIN-MODE D 13 1)
(PIN-MODE D 2 0)

; Write digital value
(WRITE D 13 1) ; HIGH
(WRITE D 13 0) ; LOW

; Read digital value
(SET btn (READ D 2)) ; 0 or 1

Analog Input​

; Read 10-bit ADC value (0–1023)
(SET sensor (READ A 0))

; Map to a different range
(SET voltage (MAP (READ A 0) 0 1023 0 5000))
(LOG "Voltage: " voltage "mV")

; Convert to temperature (LM35: 10mV/°C)
(SET temp (/ (MAP (READ A 0) 0 1023 0 5000) 10))
(LOG "Temperature: " temp "C")

PWM Output​

; Set PWM duty cycle (0-255)
(WRITE P 9 0) ; 0% duty cycle (off)
(WRITE P 9 128) ; 50% duty cycle
(WRITE P 9 255) ; 100% duty cycle (full)

; Motor speed control
(SET speed (MAP (READ A 0) 0 1023 0 255))
(WRITE P 9 speed)

Tone Generation​

; Play a 1kHz tone for 200ms
(TONE D 8 1000 200)

; Play different notes
(TONE D 8 262 200) ; C4
(TONE D 8 294 200) ; D4
(TONE D 8 330 200) ; E4

Pulse Measurement​

; Measure pulse width on pin 2
(SET width (PULSE D 2 1000))
(LOG "Pulse width: " width "us")

Common Sensor Patterns​

LM35 Temperature Sensor​

; LM35 outputs 10mV per degree Celsius
(DEFUN LM35 ()
(/ (MAP (READ A 0) 0 1023 0 500) 10))

(SET temp (LM35))
(LOG "Temperature: " temp " C")

HC-SR04 Ultrasonic Sensor​

; Trigger pulse and measure echo
(DEFUN DISTANCE ()
(DO
(WRITE D 9 1)
(DELAY 0.01)
(WRITE D 9 0)
(SET echo (PULSE D 10 30000))
(/ (* echo 343) 20000))) ; Distance in mm

Servo Motor Control​

; Set servo angle (0-180 degrees)
(DEFUN SET-SERVO (angle)
(SERVO 9 angle))

; Sweep servo back and forth
(FOR angle 0 180 (SET-SERVO angle))
(FOR angle 180 0 (SET-SERVO angle))

Autonomous Pin Control (Autopilot)​

When running in autopilot mode, the firmware can manage pins without host intervention. The autopilot engine executes a stored script that runs in a continuous loop on the microcontroller (Rashwan, 2026).


References

Hennessy, J. L., & Patterson, D. A. (2017). Computer architecture: A quantitative approach (6th ed.). Morgan Kaufmann.

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