Back to all articles
Embedded SystemsNew

Can AI Write Firmware? What Works on Real Hardware

AI coding agents now draft drivers, HAL setup, tests and host tools in minutes. They still get the clock tree, the errata, the timing and the power states wrong, and a build that passes proves none of it. A worked STM32 timer review, and the rules that keep AI-written firmware safe to ship.

December 3, 2026·11 min read·AI, agentic coding, embedded firmware, STM32, MCP, code review

Yes, for a growing share: drivers drafted from a datasheet, HAL setup, unit tests and host tools. Not unattended for the parts that touch the silicon: clock trees, timing, interrupts, errata and power states. A draft that compiles can still run a timer at twice the intended rate; the example below checks one with the STM32 Timer Calculator.

This is how inteRFace R&D uses it. Our senior engineers use AI tools where they earn their keep: the first estimate, firmware and code, and design-review prep. The rule for firmware is published on our homepage, word for word:

Agentic coding workflows draft drivers, tests, tools and back ends. An engineer reviews every change and tests it on the real hardware or system before it ships.

A worked example: reviewing an AI-drafted 1 kHz timer tick

The task: a 1 kHz update interrupt, the system tick of a control loop, on an STM32H5. Assumptions, all stated: the core and AHB run at 160 MHz, the APB bus that clocks the timer has its prescaler at 2, so the bus runs at 80 MHz. The timer is edge-aligned and counts up, so f = f_timer / ((PSC + 1) x (ARR + 1)).

The trap is the timer clock. ST's rule for the H5: when the APB prescaler is 1 the timers run at the bus clock; otherwise they run at twice it. Here that is 160 MHz, not 80 MHz. Earlier families do the same; an F103 user on ST's forum saw TIM2 run at twice the expected rate for exactly this reason.

Four timer drafts reviewed, timer clock 160 MHz (APB at 80 MHz, prescaler 2)
DraftPSCARRReal rateError
Correct, 1 MHz tick1599991,000 Hz0
Bus clock used as timer clock799992,000 Hz+100%
Divider written without the minus one1601000992.80 Hz-0.72%
1 Hz variant: 1 MHz tick on a 16-bit timer159999,999does not fitARR max 65,535

Each wrong row compiles cleanly, and each passes a code review that only reads the code. The second runs the loop twice as fast, which a scope shows in seconds and a unit test on the host never will. The third is subtler: 992.80 Hz loses 0.72%, so a clock built on the tick drifts about 26 seconds an hour, and nobody notices until a field log disagrees with a server timestamp. The fourth asks for a period count of 1,000,000 in a register that holds 65,535 at most; PSC 15999 and ARR 9999 give 1 Hz exactly on a 16-bit timer.

People make the same mistakes. The difference is speed and confidence: an agent writes a plausible init function in seconds and explains it fluently, so the review checks the clock tree against the reference manual and the rate on a pin, not the prose.

STM32 Timer Calculator
PSC and ARR for any timer frequency or period, with the real frequency, ppm error, PWM resolution in bits and the CCR for your duty cycle.
Start from these numbers: Timer clock 160 MHz, 1 kHz, 16-bit, edge-aligned
Try the calculator: STM32 Timer Calculator

Any pair whose (PSC + 1) x (ARR + 1) equals 160,000 gives exactly 1 kHz, so the table's 159 and 999 is one of several. For this case the tool offers two others: PSC 15 and ARR 9999, a round 10 MHz tick, and PSC 3 and ARR 39999, with 40,000 counts per period, which matters when the same timer drives a PWM output. The PWM to analog voltage walk-through shows that trade in full.

What does AI do well in firmware?

AI coding agents are good at work that is mostly text and convention. That is a real share of a firmware project, and the share where review is cheapest.

  • Driver skeletons from a datasheet: register definitions, init sequences, read and write helpers for an I2C or SPI sensor, with the datasheet table numbers cited in comments so a reviewer can check them.
  • HAL and RTOS setup: peripheral init, task and queue creation, linker and build files, the parts every project repeats.
  • Protocol code from a written spec: command parsers, framing, CRC checks and the state machine around them. A spec written first is what makes this reviewable; our post on designing a BLE command protocol shows what such a spec holds.
  • Unit tests for logic that runs on the host: parsers, filters, state machines, fixed-point math, with edge cases a tired engineer skips.
  • Host tools and CI: a Python script that talks to the serial console, a flashing helper, a build pipeline that compiles every target on every commit.

What does AI get wrong in embedded code?

The errors cluster where the code depends on facts the model cannot see: which exact part is on the board, how its clocks are configured, what the silicon really does, and what else is running at the same moment. A build checks syntax and types, not any of that.

  • Clock trees and register values, as in the example above: the wrong timer clock, a missing minus one, a value too wide for the register.
  • The wrong variant: code written for a sibling part, using a peripheral, a pin mapping or a memory size the chip on your board does not have. The MCU Selector is a reminder of how much those families differ.
  • Memory discipline: one developer reports that an agent asked to tune a memory arena on an ESP32 project removed it and put in dynamic allocations that would eventually crash the microcontroller.
  • Confident wrong explanations: in Semgrep's ESP32 radio-decoding experiment the model produced working code within a few prompts, then explained a failing decode loop confidently and incorrectly; a developer had to find the real cause.
  • Whole-module rewrites: the same developer saw nearly a thousand lines of working code replaced by a few dozen lines of broken logic when the request was to fix one bug.

Why errata and bring-up stay with an engineer

An errata sheet records where the silicon does not do what the reference manual says, and generated code follows the reference manual. A real example from ST's errata sheet ES0348 for the STM32F103x4/6 parts, section 2.8.7: after a power-on reset or ESD stress, the I2C analog filters can latch low while SCL and SDA are high. The BUSY flag sets, the peripheral cannot send a START, and neither a software reset nor a system reset clears it.

The documented workaround is a 15-step sequence that drives SCL and SDA as open-drain outputs to force transitions, then resets the peripheral. Code that follows the manual hangs on that board at random. An engineer who has read the errata, or who sees BUSY stuck with both lines high on a scope, knows where to look. The I2C troubleshooting guide covers the electrical causes that look the same from firmware.

Bring-up is the same kind of work: power rails and clocks verified, each peripheral proven on its own, every hardware issue written down. It happens with the board on a bench and a probe on a pin, which is why our firmware development engagements treat it as its own phase.

Concurrency and power: bugs no build will show

Two more classes of fault pass every compile and most code reviews, and only show on running hardware.

  • Shared state between an interrupt and the main loop: a counter read in two halves while the ISR updates it, a flag that is not declared volatile, a buffer index changed by a DMA callback mid-copy. These fail rarely and at random, so a test that passes once proves nothing.
  • Blocking calls in the wrong context: a delay or a mutex inside an interrupt handler, a printf in a high-rate ISR, a watchdog fed from a task that can stall while the loop it guards is stuck.
  • Low-power states: a peripheral clock left on, a pin left floating in stop mode, a wake-up source that is never enabled. The code runs correctly and the battery lasts a fraction of the budget. Only a current measurement on the real board shows it.

What is agentic coding, and how is it different from autocomplete?

Autocomplete suggests the next lines while you type. An agent takes a task, plans a change across several files, builds, runs the tests, reads the output and tries again until the checks pass. It is only as good as the feedback loop it is given: if the only check is the compiler, it will stop at code that compiles.

On a web back end the tests run where the code runs. On a microcontroller, most of what matters (rates, timing, current, recovery from a brown-out) is only observable on the board.

What is MCP, and why do hardware teams care?

The Model Context Protocol (MCP) is, in its own documentation's words, an open-source standard for connecting AI applications to external systems: data sources, tools and workflows. For a hardware team that means an agent can be handed a debug probe, a serial console, a logic analyzer, a programmable power supply or an ECAD tool as tools it can call.

That closes part of the loop: flash, run, read the console, capture a signal, power-cycle, compare. It does not decide what to measure or whether a reading is plausible. In Semgrep's experiment the sensors themselves were faulty, and their conclusion was that AI can generate correct code for a broken system, and only a person can tell the difference.

Can emulators replace the board?

Not for the hardware-facing part. A March 2026 study from Duke University ran 378 hardware-validated experiments across 3 embedded platforms, 23 peripherals and 42 tasks. The authors note that emulators such as QEMU and Wokwi have limited peripheral fidelity and incomplete timing models, that code which compiles may still fail on real devices, and that raw model capability alone is not enough for reliable embedded development.

How do you keep AI-written firmware safe to ship?

Treat an agent's output like a capable new hire's pull request: useful, fast, and reviewed line by line against the datasheet, the reference manual and the errata, then tested on the real board. Our published rule is that a human engineer reviews, tests and owns everything delivered. The supporting practice is ordinary engineering discipline:

  1. A written spec before any code: rates, timing budgets, power states, failure behaviour. An agent with a spec produces reviewable code; one with a vague prompt produces plausible code.
  2. Small changes, one concern each, so a reviewer can check every register value against its source.
  3. Host unit tests for the logic, a hardware-in-the-loop rig for what cannot run on the host, and continuous integration that builds every target and runs static analysis on every commit. That is the test standard on our firmware development page.
  4. Measurements, not assertions: a rate on a scope, a current on a meter, a recovery after a forced brown-out.
  5. Signed, versioned releases with a rollback path, so a bad image in the field is recoverable; the OTA update guide covers the design.

Medical and safety firmware adds traceability from each requirement to its test and a named person accountable for each change. AI can assist inside that process. It cannot be the process. A January 2026 study built on focus groups with ten senior experts from four companies notes that embedded work's strict demands for determinism, reliability and traceability make generative tools harder to adopt. It reports eleven emerging practices and fourteen challenges in orchestrating these tools, governing them responsibly and adopting them sustainably.

Does AI make a firmware project cheaper or faster?

Drafting, tests and tooling get faster. Bring-up, debugging on the bench, power tuning and certification do not, and on most hardware products those phases hold much of the schedule risk. Be wary of anyone quoting a multiplier for the whole project.

Our quotes stay what they were: priced in engineering hours, with a written quote with phases before any work starts. For a planning number from a paragraph of description, try the AI Project Estimator on our homepage, which is itself an AI product we build and run.

Is my code safe when a contractor uses AI tools?

Ask before the work starts, and get the answers in writing. These questions apply to any contractor, us included:

  • Which AI tools touch the code, and under what account and terms?
  • Do those terms let the provider retain your code or prompts, or use them to train a model?
  • Does the NDA cover material shared with AI tools?
  • How is export-controlled or regulated work kept out of tools that are not cleared for it?
  • Who reviews each AI-drafted change, and how is it tested on hardware?
  • Who owns the deliverables? With us, the client keeps 100% of the IP, and a mutual NDA is signed before any technical discussion.

If you are weighing whether to build in-house or bring in a firm at all, our outsourcing guide covers that decision. For AI inside your product rather than in the workflow, see AI integration services.

Sources

Have AI-drafted firmware, or a board that behaves differently from its code? Book a call to go through it with a senior engineer.

Frequently asked questions

Can a general AI assistant write STM32 or ESP32 firmware?

It can draft much of it: peripheral setup, drivers from a datasheet, parsers and host-side tests. It cannot see your board, so clock configuration, register widths, errata and timing need checking against the reference manual and on hardware. A draft that compiles can still run a timer at twice the intended rate.

Is AI-generated firmware safe for a medical or safety-critical device?

Only inside a controlled process. Regulated firmware needs traceability from each requirement to its test and a named person accountable for every change. AI can draft code and tests within that process, but each change still needs human review, verification on the real hardware and a record that links it to a requirement.

What is an MCP server in embedded development?

The Model Context Protocol is an open-source standard for connecting AI applications to external systems. An MCP server exposes one of those systems as tools an agent can call: a debug probe, a serial console, a logic analyzer or a power supply. It lets an agent flash, run and observe a board, though judgment still stays with an engineer.

Does using AI reduce what a firmware project costs?

It shortens drafting, test writing and tooling. It does not shorten bring-up, bench debugging, power tuning or certification, which carry much of the risk on a hardware product. Be wary of a fixed multiplier. We price in engineering hours, and you get a written quote with phases before any work starts.

Who owns firmware written with AI assistance?

Ownership of deliverables is set by your contract, so read it before work starts. With inteRFace R&D the client keeps 100% of the IP, and a mutual NDA is signed before any technical discussion. Whether AI-assisted code can be copyrighted is a legal question that varies by jurisdiction; ask your counsel.

Can an emulator replace testing AI-written firmware on the board?

No. Emulators and host tests are fast and catch logic errors on every commit, but researchers note that tools such as QEMU and Wokwi model peripherals and timing incompletely. Rates, interrupt timing, current draw and recovery from a brown-out have to be measured on the real board before the firmware ships.

Wondering what your project would cost?
Two minutes, honest ranges in engineering hours, the units we price in. You get a written quote with phases before any work starts.
Try the free estimator

Building something hardware-shaped?

We help hardware founders go from sketch to certified product. Bring the spec, or just the idea, to a free 30-minute call with a senior engineer. Prefer email? A senior engineer reads every one.