Embedded Software Architecture: Layers, HAL and Drivers

Most firmware starts as a vendor example and grows. Register writes end up in the middle of application logic, a sensor driver assumes one particular I2C peripheral, and moving to a different microcontroller means touching almost every file. A layered architecture separates what the product does from how the hardware does it. This article describes the usual layers, the rules that keep them separate and what they cost.

At a glance

4 layers
Hardware access, drivers, services and application: a common and workable split.
CMSIS
Arm’s standard software interface for Cortex-M cores, used by most vendor libraries.
Downward
Each layer calls only the layers beneath it. Nothing low-level knows about the application.
Interfaces
Drivers depend on abstract bus functions, not on one vendor’s peripheral.

The problem

When the application calls hardware directly, every change in hardware ripples upward. A pin moved on the next board revision, a sensor replaced because of a shortage, or a new microcontroller family each turns into a search through the whole codebase.

Tight coupling also blocks testing. Logic that cannot run without real registers can only be tested on a board, one slow cycle at a time, so it tends not to be tested at all.

Put each hardware detail in exactly one place. Then a hardware change becomes a local edit rather than a project.

Where to draw the lines

The lowest layer is the hardware abstraction layer: vendor or in-house code that configures clocks, pins and peripherals. Above it, drivers handle external devices such as a temperature sensor or flash chip, written against a small bus interface so they work with any I2C or SPI implementation. Services such as logging, storage and communication protocols sit above the drivers, and the application sits on top.

Keep the interfaces narrow and named for what they do, such as read temperature, rather than how they do it. Board-specific facts, such as which pin drives which LED, belong in a single board configuration file.

What good looks like

Draw the layers and the allowed dependencies before writing code, and decide which vendor libraries sit at the bottom.

Define the interface each driver needs from the layer below, and write drivers against that interface instead of a specific peripheral.

Check the rules in review or with a dependency check in the build, so that shortcuts across layers are caught early.

A typical layer split
LayerContainsDepends on
ApplicationProduct behaviour, state machines, user interface logicServices and driver interfaces
ServicesLogging, storage, protocols, schedulingDriver interfaces
DriversExternal devices: sensors, memories, displaysBus and pin interfaces
Hardware abstractionClocks, pins, timers, bus peripheralsMicrocontroller registers
Board configurationPin assignments and board-level constantsNothing; read by the layers that need it
One common arrangement. Names and boundaries vary between projects.
Direct register access and a layered design
Direct accessLayered
Initial effortLowerHigher
Code sizeSmallestSlightly larger
Changing microcontrollerWidespread rewriteReplace lower layers
Testing off the targetImpracticalPractical
Replacing a componentEdits throughoutOne driver
Layering costs a little up front and pays back on every change.

What it changes

Portability

Changing microcontroller replaces the lower layers and leaves the application intact.

Testability

Logic above the hardware layer can be built and tested on a PC.

Supply Resilience

Swapping a sensor or memory for an alternative part touches one driver.

Team Scaling

Engineers can work on separate layers in parallel against agreed interfaces.

Where to go next

Do not over-abstract. A layer that wraps a single register write in three function calls adds code and confusion without making anything easier to change.

Where timing is critical, such as a motor control loop, allow a documented exception that accesses hardware directly, and keep it small.

Problems this prevents

  • Register writes scattered through application code
  • A sensor change that touches dozens of files
  • Logic that can only be tested on a board
  • Abstraction layers that add code but no flexibility

Discuss your project

Email support@aavico.com or use Get in Touch when the website finishes loading.