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

HAL-крейты

HAL-крейты для многих микроконтроллеров предоставляют обёртки вокруг различной периферии. Обычно они реализуют трейты из embedded-hal.

// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0

#![no_main]
#![no_std]

extern crate panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::OutputPin;
use nrf52833_hal::gpio::{Level, p0};
use nrf52833_hal::pac::Peripherals;

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

    // Создаём HAL-обёртку для GPIO порта 0.
    let gpio0 = p0::Parts::new(p.P0);

    // Настраиваем выводы 21 и 28 GPIO 0 как выходы 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);

    // Установите вывод 28 в низкий уровень, а вывод 21 --- в высокий, чтобы включить светодиод.
    col1.set_low().unwrap();
    row1.set_high().unwrap();

    loop {}
}
  • set_low и set_high — методы трейта OutputPin из embedded_hal.
  • HAL-крейты существуют для многих устройств Cortex-M и RISC-V, включая различные микроконтроллеры STM32, GD32, nRF, NXP, MSP430, AVR и PIC.

Запустите пример командой:

cargo embed --bin hal