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.
| Layer | Contains | Depends on |
|---|---|---|
| Application | Product behaviour, state machines, user interface logic | Services and driver interfaces |
| Services | Logging, storage, protocols, scheduling | Driver interfaces |
| Drivers | External devices: sensors, memories, displays | Bus and pin interfaces |
| Hardware abstraction | Clocks, pins, timers, bus peripherals | Microcontroller registers |
| Board configuration | Pin assignments and board-level constants | Nothing; read by the layers that need it |
| Direct access | Layered | |
|---|---|---|
| Initial effort | Lower | Higher |
| Code size | Smallest | Slightly larger |
| Changing microcontroller | Widespread rewrite | Replace lower layers |
| Testing off the target | Impractical | Practical |
| Replacing a component | Edits throughout | One driver |
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.