周辺I/Oへアクセスするためのクレート(PACs)

svd2rustCMSIS-SVD ファイルから、メモリマップされた周辺I/Oに対するほぼ安全(mostly-safe)なRustラッパーを生成します。

#![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ファイルでデバイスのメモリマップを記述したものであり、通常シリコンベンダにより提供されます。
    • 周辺I/Oごとに、レジスタ、フィールドと値、名前、説明、アドレスなどにより構成されています。
    • SVDファイルにはよく誤りがあり、また情報が不足していることも多いので、様々なプロジェクトがそれを修正・追加し、クレートとして公開しています。
  • cortex-m-rtはベクタテーブルも提供します。
  • もしcargo install cargo-binutilsを実行していれば、cargo objdump --bin pac -- -d --no-show-raw-insnを実行することにより生成されたバイナリの中身を見ることができます。

例の実行方法:

cargo embed --bin pac