Migrate a Driver From embedded-hal 0.2 to 1.0
Port a driver from embedded-hal 0.2 to 1.0 with exact trait changes for I2C, SPI, GPIO and delays, plus the embedded-hal-compat bridge for mixed crates.

embedded-hal 1.0.0 shipped in January 2024 and it is not a version bump you can take by editing a number in Cargo.toml. The 0.2 line had traits in blocking modules, generic error parameters on every impl, and a handful of traits that never reached 1.0 at all. A driver written against 0.2.7 fails to compile against 1.0 with errors such as unresolved import embedded_hal::blocking, and the fix is a set of mechanical changes plus three decisions that are not mechanical. This post lists the version boundary exactly, ports one driver, and shows how to keep working while your HAL is still on 0.2, using Rust 1.85, embedded-hal 1.0.0 and embedded-hal-compat 0.13. If you are starting fresh, read writing a portable sensor driver first, since it shows the target shape.
What changed, trait by trait
The authoritative list is the embedded-hal 1.0 changelog and migration notes. The table below is the part a driver author needs.
| Area | 0.2.7 | 1.0.0 |
|---|---|---|
| GPIO output | digital::v2::OutputPin, associated Error per trait | digital::OutputPin: ErrorType |
| GPIO input | InputPin::is_high(&self) | InputPin::is_high(&mut self) |
| Toggle | ToggleableOutputPin trait | StatefulOutputPin::toggle method |
| I2C | blocking::i2c::{Read, Write, WriteRead} | one trait, i2c::I2c |
| SPI | blocking::spi::{Transfer, Write}, spi::FullDuplex | spi::SpiBus and spi::SpiDevice |
| Delay | blocking::delay::{DelayMs<T>, DelayUs<T>} | delay::DelayNs, no generic argument |
| Serial | serial::{Read, Write} | removed; use embedded-io |
| ADC | adc::OneShot | removed, no 1.0 trait |
| Timers | timer::CountDown | removed |
| PWM | Pwm | pwm::SetDutyCycle |
Non-blocking nb | built in | separate crate embedded-hal-nb |
The shared change is the error model. Every 1.0 trait extends ErrorType, which carries one associated type, so a bus type has one Error, not a different one per trait. A driver no longer writes where I2C: Write<Error = E> + WriteRead<Error = E> with a free E; it writes I2C: I2c and uses I2C::Error.
A driver in 0.2 form
Here is a small driver for a hypothetical register-based sensor with an enable pin, in the 0.2 style. It uses the I2C, GPIO and delay traits, which covers the three most common ports.
#![no_std]
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::digital::v2::OutputPin;
const ADDR: u8 = 0x40;
const REG_ID: u8 = 0xFF;
const REG_CONFIG: u8 = 0x01;
pub enum Error<E, PinE> {
I2c(E),
Pin(PinE),
WrongChip(u8),
}
pub struct Sensor<I2C, EN> {
i2c: I2C,
enable: EN,
}
impl<I2C, EN, E, PinE> Sensor<I2C, EN>
where
I2C: Write<Error = E> + WriteRead<Error = E>,
EN: OutputPin<Error = PinE>,
{
pub fn new(i2c: I2C, enable: EN) -> Self {
Self { i2c, enable }
}
pub fn start<D: DelayMs<u8>>(&mut self, delay: &mut D) -> Result<(), Error<E, PinE>> {
self.enable.set_high().map_err(Error::Pin)?;
delay.delay_ms(10);
let mut id = [0u8];
self.i2c.write_read(ADDR, &[REG_ID], &mut id).map_err(Error::I2c)?;
if id[0] != 0x5A {
return Err(Error::WrongChip(id[0]));
}
self.i2c.write(ADDR, &[REG_CONFIG, 0x01]).map_err(Error::I2c)
}
}This is typical 0.2 code. It has two free error type parameters, three trait bounds, and a delay generic over the integer width.
The same driver in 1.0 form
#![no_std]
use embedded_hal::delay::DelayNs;
use embedded_hal::digital::OutputPin;
use embedded_hal::i2c::I2c;
const ADDR: u8 = 0x40;
const REG_ID: u8 = 0xFF;
const REG_CONFIG: u8 = 0x01;
#[derive(Debug)]
pub enum Error<E, PinE> {
I2c(E),
Pin(PinE),
WrongChip(u8),
}
pub struct Sensor<I2C, EN> {
i2c: I2C,
enable: EN,
}
impl<I2C, EN> Sensor<I2C, EN>
where
I2C: I2c,
EN: OutputPin,
{
pub fn new(i2c: I2C, enable: EN) -> Self {
Self { i2c, enable }
}
pub fn start<D: DelayNs>(
&mut self,
delay: &mut D,
) -> Result<(), Error<I2C::Error, EN::Error>> {
self.enable.set_high().map_err(Error::Pin)?;
delay.delay_ms(10);
let mut id = [0u8];
self.i2c.write_read(ADDR, &[REG_ID], &mut id).map_err(Error::I2c)?;
if id[0] != 0x5A {
return Err(Error::WrongChip(id[0]));
}
self.i2c.write(ADDR, &[REG_CONFIG, 0x01]).map_err(Error::I2c)
}
}The diff is four lines of imports, the impl header, and the return type. The logic is untouched. What the compiler gave you in exchange: no free E and PinE parameters leaking into every method, a single I2c bound instead of two, and DelayNs with no width to choose. delay_ms in 1.0 takes u32, so a call such as delay.delay_ms(10) still compiles because the literal infers.
The decisions that are not mechanical
Serial is gone. If the driver used embedded_hal::serial::{Read, Write} (a GPS or modem driver), 1.0 has no replacement in embedded-hal. The portable byte-stream traits live in embedded-io (Read, Write, and the ReadReady/WriteReady markers for non-blocking), and embedded-io-async for async. Porting a UART driver means changing its trait bound to those, which is a real API change for your callers.
ADC and timers are gone. There is no 1.0 adc::OneShot or CountDown. A driver that read an ADC channel generically has to take a closure or define its own small trait, because the working group deferred ADC design. A driver that waited on a timer should take DelayNs, or the embedded-hal async delay.
SPI splits in two. In 0.2 a driver took a bus and a chip-select pin separately and toggled the pin itself. In 1.0 SpiDevice represents a device with its own chip select, and its transaction(&mut [Operation]) method asserts chip select around the operations. A driver should take SpiDevice, not SpiBus; the bus, which has no chip select, is for the HAL and for sharing logic. Here is the shape for a register read over SPI:
use embedded_hal::spi::{Operation, SpiDevice};
pub fn read_register<S: SpiDevice>(spi: &mut S, reg: u8) -> Result<u8, S::Error> {
let mut value = [0u8];
spi.transaction(&mut [
Operation::Write(&[reg | 0x80]),
Operation::Read(&mut value),
])?;
Ok(value[0])
}Chip select stays low across both operations and goes high afterwards, with no pin handling in the driver. The set bit 0x80 is a read flag that many SPI sensors use; check your device's datasheet.
Staying compatible while the ecosystem catches up
Your driver on 1.0 cannot be given a bus from a HAL that still implements 0.2 traits. The embedded-hal-compat crate bridges them with two adapters, forward (0.2 type used where 1.0 is expected) and reverse (1.0 type used where 0.2 is expected):
use embedded_hal_compat::ForwardCompat;
// `old_i2c` implements the 0.2 blocking I2C traits (for example from a HAL not yet on 1.0).
// `forward()` returns a wrapper that implements the 1.0 `I2c` trait.
pub fn wrap_bus<I2C, E>(old_i2c: I2C) -> impl embedded_hal::i2c::I2c
where
E: core::fmt::Debug,
I2C: embedded_hal_02::blocking::i2c::Read<Error = E>
+ embedded_hal_02::blocking::i2c::Write<Error = E>
+ embedded_hal_02::blocking::i2c::WriteRead<Error = E>,
{
old_i2c.forward()
}The adapter's module paths and the renamed embedded-hal-02 dependency are set out in the compat crate's own README, so follow it when you copy this. The point is that you do not have to wait for every HAL to move.
To publish a driver that supports both, offer a Cargo feature that selects the version, or release a new major version for 1.0 and let the old one stay on crates.io. Most maintainers choose the latter, because carrying two sets of bounds doubles the test matrix.
A migration checklist
Change the dependency to
embedded-hal = "1.0", and update dev-dependencies such asembedded-hal-mockto a release with theeh1feature.Replace
blocking::*imports withi2c::I2c,delay::DelayNs,spi::SpiDevice,digital::OutputPinanddigital::InputPin.Delete free error type parameters; use
T::Error.Change
is_high(&self)callers to hold the pin mutably.Replace
ToggleableOutputPinuse withStatefulOutputPin::toggle.Rewrite serial, ADC and timer dependencies, which have no 1.0 equivalent.
Rerun host tests with the 1.0 mock types.
When not to migrate yet
If the only HAL you target is still on 0.2 and offers no 1.0 implementation, migrating costs you the ability to use that HAL directly, and the compat adapter adds an indirection layer to every call. Check your target HAL's release notes first. Most major HALs, including stm32f4xx-hal, rp2040-hal and the nRF HALs, have implemented 1.0 alongside or instead of 0.2 for some time.


