HAL Crate
許多微控制器的 HAL Crate 能為各種周邊裝置提供包裝函式。這些項目通常會實作 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();
// Create HAL wrapper for GPIO port 0.
let gpio0 = p0::Parts::new(p.P0);
// Configure GPIO 0 pins 21 and 28 as push-pull outputs.
let mut col1 = gpio0.p0_28.into_push_pull_output(Level::High);
let mut row1 = gpio0.p0_21.into_push_pull_output(Level::Low);
// Set pin 28 low and pin 21 high to turn the LED on.
col1.set_low().unwrap();
row1.set_high().unwrap();
loop {}
}
set_low和set_high是embedded_halOutputPin特徵上的方法。- 許多 Cortex-M 和 RISC-V 裝置都有 HAL Crate,包括各種 STM32、GD32、nRF、NXP、MSP430、AVR 和 PIC 微控制器。
使用下列指令執行範例:
cargo embed --bin hal