Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 6 min read

"It compiles" is not "it works": how we test AI-generated firmware against the rules you wrote

When an AI writes firmware for your device, the first question is not "is the code pretty?". It is: does the device do what I asked? A firmware that compiles, passes a linter and reads well can still leave the heater off

When an AI writes firmware for your device, the first question is not "is the code pretty?". It is: does the device do
what I asked?
A firmware that compiles, passes a linter and reads well can still leave the heater off forever, never close
the gas valve, or keep a buzzer screaming after the air is clean again.

This post explains how MCUcraft checks that, what the check catches, and what it does not.

From your sentences to numbered rules

You describe the device in a chat: "Air quality monitor for my workshop, on an STM32. It measures CO2, temperature and
humidity, shows them on a small OLED display, and sounds a buzzer when CO2 goes above 1500 ppm. Powered from USB-C. I want a
custom PCB."

Before any code exists, those sentences become rules with an identifier, each one tied to the words you used: the CO2
limit at 1500 ppm, the buzzer, what happens when the sensor stops answering. Where you did not say something
(how far below 1500 ppm the alarm should clear, for example), the generator picks a value and writes it down as an
assumption
, in the code and in the project, so you can correct it.

The code has to say where each rule lives

The firmware generator does not just write code. For every rule it declares where it implemented it: which file, which
function. Then a deterministic check verifies the claim: the function exists, in that file. A rule that has no place in the
code is not silently dropped: it is reported.

On top of that, a set of checks reads the code itself: limits compared in the right direction, the state machine the
project declared, outputs that actually move, the power budget. They are useful, but they share a weakness we measured
early: they read the text of the code. Green means "not broken". It does not mean "does what you asked".

Running the whole firmware on a PC

So the last check does not read the code: it runs it.

  • The whole firmware is compiled for the PC, with a stand-in for the Arduino core.
  • The one file that talks to the hardware (the drivers) is replaced by a stub. When the firmware asks "what is the CO2?", the stub answers with the value the test script says, in physical units (ppm, Β°C, mA). Every command the firmware sends to an output (buzzer on, relay off) is recorded with its time.
  • Time is simulated, so a 15-minute rule takes milliseconds to test.

This is the stub the bench generates for the air quality monitor:

// src/parts.cpp as compiled on the PC (generated; helper names translated)
#include "parts.h"
float bench_value(const char* name);             // the scenario's value right now
void  bench_command(const char* name, float v);  // logged with the simulated time

void  parts_begin() {}
float read_co2()            { return bench_value("co2"); }
float read_temperature()    { return bench_value("temperature"); }
void  drive_buzzer(bool on) { bench_command("buzzer", on ? 1.0f : 0.0f); }

The scenarios are written from your rules, not from the code: a model reads the project's sentences and the list of
things that can be measured and driven, and writes scripts like this one, from the diesel tank example:

{
  "name": "V-01.over",
  "rules": ["A#0", "R-b8a7", "V-01"],
  "assumptions": ["fuel_level below 20 %: just beyond -> alarm"],
  "steps": [
    { "t_ms": 0,      "set": { "fuel_level": 20.0 } },
    { "t_ms": 903000, "set": { "fuel_level": 19.8 } }
  ],
  "expect": [
    { "what": "alert", "sent": true, "from_ms": 903000, "to_ms": 1806000, "about": "fuel_level" }
  ]
}

A quarter of an hour in, the level drops just below 20 %, and the alert must go out within the next 15 minutes. The model
never sees the firmware, so it cannot copy its mistakes. Each scenario also states the assumptions it made, because those
are what you might want to correct.

The timing margin is set by the judge, not by the scenario: right after a value changes, the device gets the time it needs
to read the sensor and react before anything counts as a failure.

Besides the scenarios built from your rules, the same run checks a few things every device should do: outputs in a safe
state at power-on, values kept inside their limits, no surprises under random sequences of inputs.

A pass, and a fail

Pass. The diesel tank levels example (three tanks, one level sensor each, data every 15 minutes): the firmware compiles,
and 10 of 10 scenarios pass on the PC. One rule is listed as not proven by any scenario: it describes the tanks'
geometry and the router distance, which no behaviour test can check. The verdict says so instead of hiding it.

Fail. The workshop air quality monitor. The scenario was:

at 0 s CO2 = 1500 ppm Β· at 3 s CO2 = 1515 ppm Β· at 6 s CO2 = 1350 ppm

expected: buzzer off from 6 s to 9 s

The firmware's own assumption is that the alarm clears at or below 1450 ppm, and 1350 ppm is well below it. On the PC the
buzzer stayed on. The project page shows exactly this, with the verdict "Firmware builds, some tests fail". We did not
re-run the generation until the verdict turned green and then show only that one: the example is public as it came out.

What the verdict can be

Every project shows one of four verdicts, with the reason:

verdict what it means
Firmware passes its tests on the PC it compiles and every scenario that ran passes; rules no scenario can prove are listed
Firmware builds, some tests fail it compiles, and at least one scenario fails: which one, with the script
Firmware builds, not tested yet it compiles, but some rules were not exercised by any scenario that ran
Firmware does not build the compiler's errors

Does it catch real bugs?

We measured it before turning it on. On a training glove we had checked by hand, the scenarios written by the model gave
the same verdict as ours in 12 of 12 cases, and the run showed that the exercise phase never started, although every
code-reading check was green. Then on four projects from earlier runs it found a real defect in all four, for example a
gas valve that did not close from the phone, a compressor monitor that never counted its starts, and an oven that missed a
short button press.

And one lesson that changed how we fix things. A firmware once had six write_heater(false) and no true: the heater never
turned on. The model had not made a mistake: the contract told it "when temperature leaves its declared limits, turn the
heater off"
, and on a temperature range, "leaves its limits" is true on both sides. The fix was the sentence, not the
model. The check that saw it stays, so if the same mistake is born somewhere else, it is caught there too.

What it does not prove

  • The wiring. Pins, signal levels and the conversion from raw readings to ppm or Β°C belong to the driver, which the stub replaces. Those are checked against the schematic and the parts' datasheets, not here.
  • Real hardware. Timing on the real chip, noise and a sensor that lies are outside a PC run. When a simulator of the board is available, the firmware also boots there; otherwise the verdict says it was not judged.
  • Rules without a measurable effect. "The enclosure fits on a DIN rail" is not a behaviour. These rules are listed as not proven, never counted as passed.

What you take home

The firmware as a PlatformIO project (ZIP), the parts list as an Excel file or a KiCad-style CSV, and the KiCad files of the
schematic and, when the project has one, of the board. You can open, build and change all of it without MCUcraft.

The firmware uses the Arduino framework, built with PlatformIO (the ESP32 Arduino core, STM32duino on STM32). Native ESP-IDF
projects or hand-written FreeRTOS tasks are not generated today.

Try it

The examples are public and open without an account: each one shows its parts list, board, firmware and verdict, green or
not. If you think a scenario is wrong, that is useful too: the scenario's assumptions are written next to it, and you can
correct them in the chat.

See the examples Β· How MCUcraft compares

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.