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

