Definition
A hardware abstraction layer (HAL) is a layer of software that offers one set of functions for hardware that differs from chip to chip. Below the layer, each chip has its own implementation of the functions, which writes that chip’s registers. Above it, drivers and applications call the same functions on every supported chip, for example to set a pin high or to send bytes on a serial port. A HAL guarantees exactly what its interface contract states: what each function does, what it requires, and what it promises.
The idea
A company sells one product built on two different chips. On chip A the serial port’s data register is at one address and its ready flag is bit 7 of a status register; on chip B both are elsewhere, and the flag means something slightly different. Without a HAL, every program that sends a byte exists in two versions. With a HAL, the knowledge of each chip is written once, inside that chip’s implementation, and every program above it is written once.
A travel plug adapter is a fair picture. The plug fits every socket, but the adapter does not change what comes out of the socket: 230 volts in one country, 120 in another. A HAL likewise makes calls look the same, while the behavior behind them can differ in anything the contract leaves open: how long a call takes, whether it waits, whether it copies the caller’s data or reads it later, and whether two threads may call it at once. The picture stops here: an adapter promises nothing about voltage, while a good HAL contract does fix some of these properties, and exactly those carry over from one chip to another.
HALs exist at several levels. Chip vendors ship HALs for their own product families, such as Nordic Semiconductor’s nrfx drivers. Zephyr’s §6.33 driver interfaces, such as gpio_pin_set() and uart_tx(), form a HAL across vendors, and many of Zephyr’s drivers are built on the vendors’ HALs. Arm’s CMSIS §6.43 (Common Microcontroller Software Interface Standard) standardizes how C code reaches the processor core of chips built on Arm’s Cortex-M processors.
How it works
A HAL function’s contract answers a fixed list of questions, and a caller can rely on an answer only if the contract gives it.
Effect: what the function does, and what it returns on success and on each error.
Preconditions: what must be true before the call, such as an initialized device and arguments in range.
Calling context: whether the function may be called from an interrupt service routine §6.12 (ISR), the short function that runs when hardware signals an event, or only from a thread.
Blocking: whether the call can wait, and for how long at most.
Buffer ownership: whether the function copies the caller’s data before it returns, or borrows the buffer and reads it later, and until when.
Completion: how the caller learns that the work has finished, and in which context a completion callback §14.22 runs.
Concurrency §9.1: whether two threads may call the function at the same time, and whether the function disables interrupts §6.11 or takes a lock.
Ordering: whether two requests are carried out in the order in which they were made.
Timing: an upper bound on the time a call takes, if the contract gives one. Most HAL contracts give none.
A call travels down the layer in one of two ways. In the first, the build compiles exactly one implementation, the one for the target chip, so the call is an ordinary function call. In the second, which Zephyr uses, each device carries a table of function pointers filled in by its driver, and the generic function calls through that table. Either way, the caller’s code is the same on every chip, and what runs is that chip’s implementation.
Each implementation must be a refinement of the contract: every behavior it can show must be one that the contract allows. An implementation is still free in everything the contract leaves open. One chip’s implementation may poll a status register in a loop; another may copy the data and start a DMA §4.36 (direct memory access) transfer, in which a hardware unit moves the bytes without the processor; a third may queue the request for a background thread. All three can meet one weak contract while differing in time, memory and concurrency behavior.
Worked example
Ownership. A HAL offers send(buf, 32). Chip A’s implementation copies the 32 bytes into its own memory and returns. Chip B’s implementation records the address buf, starts a DMA transfer and returns; the DMA unit then reads the bytes while they go out, which at 115200 bits per second and 10 bits per byte takes ms. A caller fills a local array §14.5, which lives in the function’s stack frame, calls send, and returns at once, so the next function call reuses that stack memory. On chip A the correct bytes go out. On chip B the DMA unit reads whatever the next function wrote into that memory. Which code is at fault depends on the contract. If the contract says that the buffer must stay valid until the completion callback, the caller is wrong on both chips, and chip A’s copy merely hid the bug. If the contract says that the data is copied before the call returns, chip B’s implementation is wrong.
Timing. A HAL function sets a pin high. On chip A the call takes 12 processor cycles at 64 MHz (megahertz): . On chip B, which is slower and checks its arguments more, the call takes 40 cycles at 16 MHz: . A program must raise an output pin no more than 1 s after it decides to, and does so with this call. It meets the requirement on chip A, with to spare, and misses it on chip B by . The contract said nothing about time, so nothing about time carried over.
Concurrency. Chip A’s implementation of send takes a mutex §9.5, a lock that makes a second caller wait; chip B’s takes no lock. Two threads that call send at the same time are safe on chip A, while on chip B their requests can interleave and corrupt the driver’s state. Chip A’s mutex has a cost of its own: a thread that finds the mutex taken waits, and Zephyr does not allow an ISR to lock a mutex, so chip A’s send must not be called from an ISR. Under one function name, the two implementations are safe for different callers.
Stated precisely
Let a contract consist of a precondition, what must hold before a call, and a guarantee, what holds after it. An implementation satisfies when every call that meets the precondition shows only behaviors that meet the guarantee. A property of a program that uses the HAL holds on every chip whose implementation satisfies , provided that the argument for uses only ’s guarantee and that the program meets ’s precondition at every call. A property whose argument uses anything else, such as a measured duration, a copy that the contract does not promise, or safety in an ISR observed on one chip, must be shown again for each implementation.
For timing in particular: if states a bound on the duration of a call, every satisfying implementation keeps each call within , and a deadline analysis may use . If states no bound, the duration measured on one implementation says nothing about another.
Limits and common mistakes
A HAL hides registers, not costs. A short call can hide a polling loop, a copy, a lock, or a stretch with interrupts disabled that delays every other interrupt.
Behavior that one implementation happens to show becomes a hidden dependency once callers rely on it. Hyrum’s law, named after the software engineer Hyrum Wright, puts it this way: with enough users of an interface, every observable behavior of it will be depended on by somebody, whatever the contract says [401]. Moving to a new chip then breaks programs that were never correct under the contract.
Some chips lack a feature. The HAL then reports that the function is not supported, as Zephyr does with the error code -ENOTSUP, and the caller must handle that answer.
Writing registers directly, past the HAL, gains speed but loses portability, and it can contradict the HAL’s own record of the device’s state.
A new version of a HAL can change timing, locking or buffering without changing any function’s name or arguments. The version is part of what a measurement depends on.
Matching function names and C types show only that the code compiles against each implementation. They say nothing about what the functions do.
Questions answered
Which timing or concurrency guarantees are preserved by the abstraction?
A hardware abstraction layer (HAL), the software layer that offers the same functions on different chips, preserves exactly the timing and concurrency guarantees that its contract states and that every implementation behind it meets, and no others. Contracts commonly state:
whether a call can block, and its timeout §9.34;
whether it may be called from an interrupt service routine (ISR) or only from a thread;
whether calls from several threads at once are safe, or must be serialized by the caller;
who owns a buffer and until when, and whether the data is copied;
how, and in which context, completion is reported;
whether requests are carried out in order.
Contracts rarely state how long a call takes, how long it keeps interrupts disabled, whether it moves data with DMA (direct memory access) or with the processor, or how it delays other code. Those properties are not preserved; they must be measured or analyzed on each chip. The test is simple: if an argument about a program uses a property, find that property in the HAL’s contract. If it is not there, the argument holds only for the implementations on which it has been checked.
Related entries: Device driver (§6.38), Helper contract (§2.21), Refinement (§3.32), CMSIS (§6.43). Further reading: [401].