dzamlo / svd

Apache License 2.0
0 stars 0 forks source link

svd

The goal of this crate is to parse System View Description (SVD) files and generate rust code to access the registers.

SVD files describes mcu and their peripherals.

Documentation

The code is not really documented at all. But the structures are pretty much the same than in an SVD file. You should reffer to the CMSIS-SVD documentation.

Usage

You will first need the .svd file describing your mcu. You can try the following location to find it:

Add the following to you Cargo.toml file:

[build-dependencies]
svd =  { git = "https://github.com/dzamlo/svd.git" }

And then something like the following as your build.rs:

extern crate svd;

use std::env;
use std::fs::File;
use std::path::Path;
use svd::codegen::rust;

const SVD_FILENAME: &'static str = "STM32F7x7.svd";
const OUT_FILENAME: &'static str = "STM32F7x7.rs";

fn main() {
    let f_in = File::open(SVD_FILENAME).unwrap();
    let out_dir = env::var("OUT_DIR").unwrap();
    let dest_path = Path::new(&out_dir).join(OUT_FILENAME);
    let mut f_out = File::create(&dest_path).unwrap();

    let d = svd::device::Device::from_reader(f_in).unwrap();
    let mut code_generator = rust::CodeGenerator::new(&mut f_out);
    code_generator.generate_device(&d).unwrap();
}

And finally in your code:

include!{concat!(env!("OUT_DIR"), "/STM32F7x7.rs")}

You can then use the various registers of your device. For exemple to enable the GPIOB on an STM32 device:

unsafe {
    let mut value = STM32F7x7::RCC::read_AHB1ENR();
    value.set_GPIOBEN(1);
    STM32F7x7::RCC::write_AHB1ENR(value);
}

Setting pin 7 of GPIOB as an output:

unsafe {
    let gpio = STM32F7x7::GPIO::GPIOB;
    let mut moder = gpio.read_MODER();
    moder.set_MODER(7, 0b01);
    gpio.write_MODER(moder);
}

Setting pin 7 of GPIOB as high:

let mut bsrr = STM32F7x7::GPIO::BSRR(0);
bsrr.set_BS(7, true);
unsafe {
    STM32F7x7::GPIO::GPIOB.write_BSRR(bsrr);
}

Setting pin 7 of GPIOB as low:

let mut bsrr = STM32F7x7::GPIO::BSRR(0);
bsrr.set_BR(7, true);
unsafe {
    STM32F7x7::GPIO::GPIOB.write_BSRR(bsrr);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.