3.4 板级支持 Crate

04-板级支持 Crate — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/bare-metal/microcontrollers/board-support.html

3.4 板级支持 Crate

板级支持(board support)crate 针对具体开发板再包一层,用起来更方便。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// 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 microbit::Board;

#[entry]
fn main() -> ! {
    let mut board = Board::take().unwrap();

    board.display_pins.col1.set_low().unwrap();
    board.display_pins.row1.set_high().unwrap();

    loop {}
}
  • 本例中板级支持 crate 主要提供更有用的命名,以及一点初始化。
  • crate 也可能包含微控制器之外某些板载设备的驱动。
    • microbit-v2 包含 LED 矩阵的简单驱动。

用以下命令运行示例:

1
cargo embed --bin board_support
最后修改 August 11, 2026: 更新 (70a5af133)