> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Driver development

> Implement a peripheral driver from verified hardware requirements, then test its behavior in software and on the board.

Use this workflow to write or adapt a driver for one fitted part on a specific board revision. You confirm the hardware facts, agree on the driver's interface in a plan, implement it in stages with host tests, and then verify it on the board.

Start with [Rapid bring-up](/docs/solutions/rapid-bring-up.md) if the board doesn't yet build, boot, or give you a working way to observe it.

## Establish the hardware facts

1. Open the firmware workspace and run `/project` to select the project.
2. Run `/peripheral` and add the fitted part. For a part outside the catalog, [add a custom peripheral](/docs/core-concepts/add-peripheral.md#add-a-custom-peripheral) from its datasheet PDF.
3. Provide the exact datasheet revision, relevant errata, the MCU reference manual, and the complete [schematic set](/docs/core-concepts/schematics.md) for this board. To add a PDF that is already in your workspace to the project's documents, ask Inlay. It asks you to confirm before adding the file.
4. If the workspace isn't configured yet, run `/init`. Check the toolchain, SDK version, build and test commands, flash method, and monitoring setup it records in [`INLAY.md`](/docs/core-concepts/inlay-md).

A catalog match doesn't identify the populated variant or confirm the board's wiring. Check the package, supply and signal voltages, address straps or chip select, reset polarity, clock source, interrupts, and routed MCU pins against the actual design.

Reference the schematic and your source files directly in the request:

```text theme={"theme":"css-variables"}
Check the fitted sensor at @sch:component:U4 against its uploaded datasheet
and @src/board/pins.c. List the bus, address selection, pull-ups, power-up
sequence, reset timing, and interrupt wiring. Cite the relevant document
sections and schematic connections. Separate confirmed facts from conflicts
or missing information before proposing register values.
```

Replace `U4` and the source path with your own. Resolve any conflict that affects wiring, electrical limits, or initialization before you continue.

## Define the driver contract

Run `/plan` to agree on the driver's interface and limits before Inlay changes several modules. In Plan mode, Inlay reads code, documents, and schematics and writes a plan. It doesn't edit your source or run a build.

```text theme={"theme":"css-variables"}
Plan a driver for the confirmed sensor at U4. The application needs initialize,
read one sample, and report status. Use the project's existing I2C transport.
Define return values, units, maximum wait times, retry limits, and recovery
behavior. Identify required files and tests. Keep DMA and interrupt-driven
sampling out of the first implementation unless the requirements need them.
```

Make sure the plan settles these points:

| Topic | What to decide |
| - | - |
| Scope | Startup state, sampling rate, and supported operating modes. |
| Calling context | Task or interrupt context, and whether several tasks share the driver. |
| Bus ownership | Which layer owns bus locking. |
| Results | How the application tells a transport error, an invalid device response, a not-ready result, and a valid sample apart. |
| Waits and retries | A deadline for every wait and a limit on retries. |
| Failure state | What the driver leaves configured after a failed initialization or transfer. |

Retry only operations that the device lets you repeat safely. Repeating a command, clearing a status bit, or reading a FIFO can change the device's state.

## Choose reuse or a custom implementation

Ask Inlay to inspect the existing SDK, HAL, and drivers before it adds code. Prefer an existing driver when it supports the exact device and the behavior you need. Check its version, license, supported modes, and error handling. A similarly named part may use a different register map.

For a custom driver, separate device behavior from bus access so host tests can supply controlled responses. Keep board pins, bus instances, and interrupt routing in the project's board layer. Respect generated-code boundaries and the project's existing style.

Check the reference documents for the details that differ between parts:

* **MCU peripherals:** clock enables, reset order, pin multiplexing, interrupt flags, and DMA requirements, from the reference manual and errata.
* **External devices:** framing, byte order, command sequences, reserved bits, and read or write side effects, from the datasheet.

Derive constants from the exact reference. Don't copy them from a nearby device family.

## Implement and test in stages

Review the plan, then approve it so Inlay implements it in Act mode. See [Plan mode](/docs/core-concepts/modes.md#plan-review-an-approach-before-implementation) for the approval options in VS Code and the terminal UI. Start with initialization and one bounded transaction before you add buffered, interrupt, or DMA operation.

```text theme={"theme":"css-variables"}
Implement the reviewed driver using the existing transport interface. Add
host tests for the documented initialization sequence, sample conversion,
wrong device identity if the part exposes one, not-ready status, and transfer
timeout. Use datasheet values for fixtures and explain their origin. Run the
focused tests and then build the selected firmware configuration.
```

Test decoding with boundary values, sign handling, units, and invalid responses. Exercise error paths through a fake transport or the project's existing test framework. Check that a timed-out request ends, reports the intended error, and allows the documented next operation.

Fix test, compile, and link failures before you flash. Each check covers only part of the result:

| Check | Confirms | Doesn't confirm |
| - | - | - |
| Host tests | The modeled driver behavior. | Board wiring, device timing, or electrical signals. |
| Target build | The firmware compiles and links for the target. | Behavior on the board. |
| Board validation | The driver works with the fitted part under the tested conditions. | Conditions you didn't test. |

## Validate on the intended board

Confirm the board revision, fitted part, firmware file, and probe or port. Set up the [UART](/docs/debug-mode/uart.md), [logic analyzer](/docs/debug-mode/logic-analyzer.md), or [oscilloscope](/docs/debug-mode/oscilloscope.md) from its guide, including voltage limits and channel assignments.

```text theme={"theme":"css-variables"}
Build the reviewed firmware and flash the confirmed board only if the build
passes. At the project's verified bus rate, run initialization and read three
samples. Capture UART for at most 15 seconds and the corresponding I2C
transactions with the configured logic analyzer. Report startup status,
addresses, acknowledgments, decoded samples, and any timeout separately.
```

Adapt the bus and observation method to your device. Compare the transmitted sequence with the datasheet, and the returned values with known test conditions. An acknowledgment alone doesn't show that the configuration or measurement is correct.

Use an oscilloscope when the question concerns signal levels, rise time, ringing, or setup and hold timing. Choose a sample rate and trigger that suit the signal. When you measure timing, use the achieved sample rate, which can differ from the one you request. If the conclusion depends on one event appearing in several captures, use a common marker or trigger. Independent logs don't necessarily share a clock.

For a hang or unexpected driver state, switch to Debug mode with `/debug`. Keep the existing evidence, and follow the [attachment precautions](/docs/debug-mode/gdb.md#attach-to-a-running-target) before you connect a debugger. Halting changes timing, so reproduce timing-sensitive results while the firmware runs normally.

## Keep the regression repeatable

Ask Inlay to save the verified sequence as a [hardware script](/docs/debug-mode/hardware-scripts.md) under `.inlay/hardware/`, with the bench settings, bounded waits, pass conditions, and cleanup. Hardware scripts run in Act or Debug mode.

```text theme={"theme":"css-variables"}
Create a repeatable driver check for this bench. Verify initialization and
three samples against the agreed limits, report failures distinctly from
missing observations, and stop the captures when finished. Record the board
revision, firmware artifact, wiring, and test conditions with the result.
```

Keep a host regression test for each software failure you correct. Add target checks for reset, repeated initialization, power-state, or recovery behavior, following the board's approved test procedure. Run the same checks after SDK, clock, bus, or driver changes, and record any requirement that remains unverified.

To run these checks routinely, see [Automated testing](/docs/solutions/automated-testing.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.