Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 embedded_hal::digital::OutputPin;
use nrf52833_hal::gpio::{p0, Level};
use nrf52833_hal::pac::Peripherals;

#[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