周邊裝置存取 Crate
svd2rust 會針對 CMSIS-SVD 檔案中記憶體對映周邊裝置,產生大多是安全的 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 (系統視圖說明) 檔案通常是晶片供應商提供的 XML 檔案,描述裝置的記憶體對映。
- 這種檔案的分類依據為周邊裝置、暫存器、欄位和值,具有名稱、說明、位址等資訊。
- SVD 檔案通常有很多錯誤且不完整,因此會使用各種專案修補錯誤、新增缺少的詳細資料,以及發布產生的 Crate。
cortex-m-rt提供向量表等內容。- 如果使用
cargo install cargo-binutils,則可以執行cargo objdump --bin pac -- -d --no-show-raw-insn,查看產生的二進位檔。
使用下列指令執行範例:
cargo embed --bin pac