Crates HAL

Os crates HAL (Hardware Abstraction Layer) para muitos microcontroladores fornecem wrappers para vårios periféricos. Esses geralmente implementam traits de embedded-hal.

#![no_main]
#![no_std]

extern crate panic_halt as _;

use cortex_m_rt::entry;
use nrf52833_hal::gpio::{p0, Level};
use nrf52833_hal::pac::Peripherals;
use nrf52833_hal::prelude::*;

#[entry]
fn main() -> ! {
    let p = Peripherals::take().unwrap();

    // Crie um _wrapper_ HAL para a porta 0 do GPIO.
    let gpio0 = p0::Parts::new(p.P0);

    // Configure os pinos 21 e 28 do GPIO 0 como saĂ­das _push-pull_.
    let mut col1 = gpio0.p0_28.into_push_pull_output(Level::High);
    let mut row1 = gpio0.p0_21.into_push_pull_output(Level::Low);

    // Configure o pino 28 baixo e o pino 21 alto para ligar o LED.
    col1.set_low().unwrap();
    row1.set_high().unwrap();

    loop {}
}
  • set_low e set_high sĂŁo mĂ©todos do trait OutputPin do embedded_hal.
  • Existem crates HAL para vĂĄrios dispositivos Cortex-M e RISC-V, incluindo vĂĄrios microcontroladores STM32, GD32, nRF, NXP, MSP430, AVR e PIC.

Execute o exemplo com:

cargo embed --bin hal