Skip to content
darunr1Public

About

Python + STM32 hardware-in-the-loop validation bench for testing embedded firmware with serial commands, sensor inputs, fault injection, latency checks, and automated pass/fail reports.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

AutoHIL Validation Bench

AutoHIL is a hardware-in-the-loop validation bench for a microcontroller-based embedded device. A Python test runner communicates with firmware over USB serial, injects software faults, validates sensor and state behavior, measures latency, and generates pass/fail reports.

The MVP models a small embedded safety controller. The board reads a potentiometer as a fake analog sensor, reads a push button as an emergency input, drives a green LED in normal mode, drives a red LED in fault or safe mode, and accepts newline-terminated serial commands from a laptop.

Why This Matters

Embedded validation work is more than firmware that blinks an LED. This project demonstrates the engineering loop used on real hardware programs:

  • A device under test with ADC, GPIO, LED outputs, state, and fault handling.
  • A host-side test controller that drives the device over USB serial.
  • JSON telemetry that can be parsed by automated tests.
  • Fault injection and recovery checks.
  • Latency measurement and CSV/JSON reports.

Hardware

Version 1 targets the NUCLEO-F401RE as the recommended MVP board. An ESP32 dev board or another STM32 Nucleo can be used later if the board abstraction functions in firmware/src/board_config.h are mapped to that platform.

Required MVP hardware:

  • 1 NUCLEO-F401RE board
  • Onboard green LD2 LED on PA5
  • 1 external red LED connected to PB5 through a 220 ohm or 330 ohm resistor to GND
  • Onboard B1 user button on PC13
  • 1 potentiometer connected to PA0 / ADC1_IN0
  • USB cable for serial communication
  • Breadboard and jumper wires

CAN, I2C, SPI, RTOS, PCB, dashboard, and wireless features are intentionally out of scope for this MVP.

Wiring

The NUCLEO-F401RE MVP mapping is:

Function Pin
Green LED Onboard LD2 on PA5
Red LED External LED on PB5
Button Onboard B1 on PC13
Potentiometer Middle pin to PA0 / ADC1_IN0
Serial USART2 ST-LINK VCP, PA2 TX, PA3 RX, 115200 baud

See docs/wiring_f401re.md for exact F401RE wiring and docs/wiring.md for the generic wiring notes.

Python Setup

Create a Python environment and install dependencies:

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On Linux/macOS:

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Firmware Layout

The firmware is written as portable C modules under firmware/src. Board-specific UART, GPIO, ADC, and timing functions are declared in board_config.h.

Important files:

firmware/src/autohil_app.c              # reusable app init/poll loop
firmware/src/command_parser.c          # serial command dispatch
firmware/src/fault_manager.c           # state, faults, LEDs, button safe mode
firmware/src/sensor.c                  # ADC sensor read and voltage conversion
firmware/src/telemetry.c               # JSON response formatting
firmware/src/timing.c                  # timing wrappers
firmware/src/board_config.h            # hardware abstraction API
firmware/targets/stm32_f401re/board_config_stm32.c

firmware/src/main.c is only a portable harness. When using STM32CubeIDE, keep CubeIDE's generated Core/Src/main.c and call autohil_app_init() / autohil_app_poll_once() from it.

Detailed CubeIDE steps are in docs/stm32cubeide_setup.md. The command protocol is documented in firmware/protocol.md.

Do This First

Bring the board up in this order:

  1. Flash basic PA5 LED blink.
  2. Test USART2 PING in a serial terminal.
  3. Run the Python PING smoke test.
  4. Only then wire the potentiometer and external red LED.

The first real milestone is: Python sends PING, STM32 returns newline-terminated JSON, Python prints PASS.

See docs/bringup_sequence.md for the full sequence.

Run The Smoke Test

Windows example:

python hil_runner/main.py --port COM3

Linux/macOS example:

python hil_runner/main.py --port /dev/ttyACM0

You can also set an environment variable:

$env:AUTOHIL_PORT="COM3"
python hil_runner/main.py

The runner executes:

  • PING
  • GET_STATUS
  • GET_SENSOR
  • INJECT_FAULT SENSOR_FAIL
  • CLEAR_FAULTS

It prints a pass/fail summary and writes CSV and JSON reports to reports/.

Run Pytest

Hardware tests:

pytest tests --port COM3

If no port is provided through --port or AUTOHIL_PORT, the hardware tests skip cleanly.

Hardware-free mock serial tests:

python -m pytest tests/test_serial_client_mock.py -v

These tests validate the Python serial client without a physical STM32 board.

Reports

Reports are saved as:

  • reports/run_YYYY_MM_DD_HHMMSS.csv
  • reports/run_YYYY_MM_DD_HHMMSS.json

Each row includes timestamp, test name, command, expected result, actual result, pass/fail status, latency, fault mode, and notes.

Resume Material

Resume bullets and skills are in docs/resume_bullets.md.

Troubleshooting

Wrong COM port:

  • On Windows, check Device Manager for the ST-LINK Virtual COM Port.
  • Try python hil_runner/main.py --port COM3, replacing COM3 with the actual port.

No serial response:

  • Confirm USART2 is enabled in asynchronous mode at 115200.
  • Confirm the board is connected through the ST-LINK USB connector.
  • Confirm firmware responses are newline-terminated through board_uart_write_line().
  • Confirm the AutoHIL loop was merged into the generated CubeIDE main.c after HAL initialization.

Invalid JSON:

  • Do not print debug logs on the same UART used by AutoHIL.
  • Confirm every firmware response is a single JSON object followed by a newline.

ADC always reads 0 or 4095:

  • Confirm the potentiometer side pins are connected to 3.3V and GND.
  • Confirm the middle wiper pin is connected to PA0.
  • Confirm CubeIDE configured PA0 as ADC1_IN0.

Button logic inverted:

  • The adapter expects B1 on PC13 to read pressed as GPIO_PIN_SET.
  • If your project reads the opposite, set AUTOHIL_BUTTON_ACTIVE_LOW to 1 before compiling board_config_stm32.c.

Red LED not turning on:

  • Confirm the LED anode path is PB5 -> resistor -> LED anode, and LED cathode goes to GND.
  • Confirm PB5 is configured as GPIO output.
  • Flip the LED orientation if needed.

HIGH_LATENCY test timing out:

  • The firmware intentionally delays that response by at least 100 ms.
  • Keep the Python timeout at 2.0 seconds or higher.
  • If using a terminal, test INJECT_FAULT HIGH_LATENCY directly before running pytest.

Future Upgrades

  • Version 2: Add I2C sensor validation.
  • Version 3: Add SPI self-test.
  • Version 4: Add CAN telemetry node.
  • Version 5: Add a simple dashboard with graphs.
  • Version 6: Design a custom PCB test fixture.

About

Python + STM32 hardware-in-the-loop validation bench for testing embedded firmware with serial commands, sensor inputs, fault injection, latency checks, and automated pass/fail reports.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages