ペリフェラルアクセスクレート

svd2rust は、CMSIS-SVD ファイルから、メモリマップされたペリフェラル向けのほぼ安全な Rust ラッパーを生成します。

// 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 nrf52833_pac::Peripherals;

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

    // Configure GPIO 0 pins 21 and 28 as push-pull outputs.
    gpio0.pin_cnf[21].write(|w| {
        w.dir().output();
        w.input().disconnect();
        w.pull().disabled();
        w.drive().s0s1();
        w.sense().disabled();
        w
    });
    gpio0.pin_cnf[28].write(|w| {
        w.dir().output();
        w.input().disconnect();
        w.pull().disabled();
        w.drive().s0s1();
        w.sense().disabled();
        w
    });

    // Set pin 28 low and pin 21 high to turn the LED on.
    gpio0.outclr.write(|w| w.pin28().clear());
    gpio0.outset.write(|w| w.pin21().set());

    loop {}
}
  • SVD(System View Description)ファイルは、通常は半導体ベンダーから提供される XML ファイルであり、デバイスのメモリマップを記述します。
    • これらはペリフェラル、レジスタ、フィールド、値ごとに整理されており、名前、説明、アドレスなどが含まれます。
    • SVD ファイルにはしばしばバグがあり、不完全でもあるため、誤りを修正し、不足している詳細を追加して、生成されたクレートを公開するさまざまなプロジェクトがあります。
  • cortex-m-rt は、他の機能に加えてベクタテーブルを提供します。
  • cargo install cargo-binutils を実行すると、cargo objdump --bin pac -- -d --no-show-raw-insn を実行して生成されたバイナリを確認できます。

次のコマンドでこの例を実行します。

cargo embed --bin pac