HAL crates

HAL crates for many microcontrollers provide wrappers around various peripherals. These generally implement traits from embedded-hal.

  1. #![no_main]
  2. #![no_std]
  3. extern crate panic_halt as _;
  4. use cortex_m_rt::entry;
  5. use embedded_hal::digital::OutputPin;
  6. use nrf52833_hal::gpio::{p0, Level};
  7. use nrf52833_hal::pac::Peripherals;
  8. #[entry]
  9. fn main() -> ! {
  10.     let p = Peripherals::take().unwrap();
  11.     // Create HAL wrapper for GPIO port 0.
  12.     let gpio0 = p0::Parts::new(p.P0);
  13.     // Configure GPIO 0 pins 21 and 28 as push-pull outputs.
  14.     let mut col1 = gpio0.p0_28.into_push_pull_output(Level::High);
  15.     let mut row1 = gpio0.p0_21.into_push_pull_output(Level::Low);
  16.     // Set pin 28 low and pin 21 high to turn the LED on.
  17.     col1.set_low().unwrap();
  18.     row1.set_high().unwrap();
  19.     loop {}
  20. }
  • set_low and set_high are methods on the embedded_hal OutputPin trait.
  • HAL crates exist for many Cortex-M and RISC-V devices, including various STM32, GD32, nRF, NXP, MSP430, AVR and PIC microcontrollers.

Run the example with:

  1. cargo embed --bin hal