The embedded-hal traits define a common interface for hardware peripherals like GPIO, SPI, and I2C, allowing hardware-agnostic code to run on any compatible microcontroller. You implement these traits for your specific hardware driver and then pass the implementing type to generic functions that require the trait.
use embedded_hal::digital::OutputPin;
fn toggle_pin<P: OutputPin>(mut pin: P) -> Result<(), P::Error> {
pin.set_high()?;
pin.set_low()?;
Ok(())
}
This pattern lets you write logic once and reuse it across different boards without changing the core application code.