Peripheral Access Crates

svd2rust generates mostly-safe Rust wrappers for memory-mapped peripherals from CMSIS-SVD files.

  1. #![no_main]
  2. #![no_std]
  3. extern crate panic_halt as _;
  4. use cortex_m_rt::entry;
  5. use nrf52833_pac::Peripherals;
  6. #[entry]
  7. fn main() -> ! {
  8.     let p = Peripherals::take().unwrap();
  9.     let gpio0 = p.P0;
  10.     // Configure GPIO 0 pins 21 and 28 as push-pull outputs.
  11.     gpio0.pin_cnf[21].write(|w| {
  12.         w.dir().output();
  13.         w.input().disconnect();
  14.         w.pull().disabled();
  15.         w.drive().s0s1();
  16.         w.sense().disabled();
  17.         w
  18.     });
  19.     gpio0.pin_cnf[28].write(|w| {
  20.         w.dir().output();
  21.         w.input().disconnect();
  22.         w.pull().disabled();
  23.         w.drive().s0s1();
  24.         w.sense().disabled();
  25.         w
  26.     });
  27.     // Set pin 28 low and pin 21 high to turn the LED on.
  28.     gpio0.outclr.write(|w| w.pin28().clear());
  29.     gpio0.outset.write(|w| w.pin21().set());
  30.     loop {}
  31. }
  • SVD (System View Description) files are XML files typically provided by silicon vendors which describe the memory map of the device.
    • They are organised by peripheral, register, field and value, with names, descriptions, addresses and so on.
    • SVD files are often buggy and incomplete, so there are various projects which patch the mistakes, add missing details, and publish the generated crates.
  • cortex-m-rt provides the vector table, among other things.
  • If you cargo install cargo-binutils then you can run cargo objdump --bin pac -- -d --no-show-raw-insn to see the resulting binary.

Run the example with:

  1. cargo embed --bin pac