liveBook · Manning

Device driver

As explained in Embedded Software with Rust

A device driver is software that presents an interface for controlling and communicating with a hardware device.

A device driver is software that provides an interface for controlling and communicating with a hardware device. In embedded Rust, a driver can use hardware abstraction layer (HAL) interfaces to interact with a peripheral without depending directly on one microcontroller’s details. section 3.5.4 section 1.6.4

The embedded-hal traits define common operations that chip-specific HAL types can implement. A driver written against those traits can be reused on another architecture when its HAL supplies the required implementations. The interface stays the same, but the underlying hardware operations differ. section 3.5.4

Controlling an LED pin

The OutputPin trait provides set_high and set_low operations. Both take a mutable reference to the pin and return a Result, allowing an implementation to report an error. The LedPin implementation below forwards these operations to HAL methods and uses Infallible as its error type. section 3.5.4

impl embedded_hal::digital::OutputPin for LedPin {
    type Error = core::convert::Infallible;

    fn set_high(&mut self) -> Result<(), Self::Error> {
        // Delegates to the HAL’s method for setting the pin high.
        self.pin.set_high();
        Ok(())
    }

    fn set_low(&mut self) -> Result<(), Self::Error> {
        // Delegates to the HAL’s method for clearing the pin.
        self.pin.set_low();
        Ok(())
    }
}

This implementation connects the common OutputPin interface to a HAL-provided pin. section 3.5.4

A generic blink function can accept any pin implementing OutputPin and any delay implementing DelayNs. This separates the blinking logic from the board-specific pin implementation. The example discards pin-operation results; code that needs to respond to failures must handle them instead. section 3.5.4

use embedded_hal::digital::OutputPin;
use embedded_hal::delay::DelayNs;

// This function can blink any LED that implements the OutputPin trait
fn blink_led<P, D>(led: &mut P, delay: &mut D) -> !
where
    P: OutputPin,
    D: DelayNs,
{
    loop {
        let _ = led.set_high();
        delay.delay_ms(500);

        let _ = led.set_low();
        delay.delay_ms(500);
    }
}

The same blinking logic can be used with compatible pin and delay implementations. section 3.5.4

Drivers can also coordinate access to shared devices. In an async system, a driver may offer nonblocking operations and enforce exclusive access through its API. Alternatively, contexts can take turns using a short critical section; ownership remains explicit in either approach. section 1.4.4

How Embedded Software with Rust covers it

How deep it goes
Shows trait-based pin implementations and generic peripheral code, without detailing complete register-level implementations.
Examples
Demonstrates LED pin implementations, a generic blink function, and a UART echo loop.
What you can do after
Apply embedded-hal traits to write reusable peripheral code using HAL-provided implementations.
How it connects
Connects drivers with hardware abstraction layers, traits, ownership, critical sections, and asynchronous programming.

Also explained in

Compare how 2 books explain device driver →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.