|
| 1 | +//! WIP CMSIS-SVD file parser |
| 2 | +//! |
| 3 | +//! Early stage. Not much documentation at the moment but here's some starting code: |
| 4 | +//! |
| 5 | +//! ``` ignore |
| 6 | +//! extern crate svd; |
| 7 | +//! |
| 8 | +//! use svd::Device; |
| 9 | +//! |
| 10 | +//! fn main() { |
| 11 | +//! println!("{:?}", Device::parse(include_str!("/path/to/file.svd"))); |
| 12 | +//! } |
| 13 | +//! ``` |
| 14 | +//! |
| 15 | +//! # References |
| 16 | +//! |
| 17 | +//! - [SVD Schema file](https://www.keil.com/pack/doc/CMSIS/SVD/html/group__schema__1__2__gr.html) |
| 18 | +//! - [SVD file database](https://github.com/posborne/cmsis-svd/tree/master/data) |
| 19 | +//! - [Sample SVD file](https://www.keil.com/pack/doc/CMSIS/SVD/html/svd__example_pg.html) |
| 20 | +
|
| 21 | +#![deny(warnings)] |
| 22 | + |
| 23 | +extern crate xmltree; |
| 24 | + |
| 25 | +use xmltree::Element; |
| 26 | + |
| 27 | +macro_rules! try { |
| 28 | + ($e:expr) => { |
| 29 | + $e.expect(concat!(file!(), ":", line!(), " ", stringify!($e))) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +mod parse; |
| 34 | + |
| 35 | +trait ElementExt { |
| 36 | + fn get_child_text<K>(&self, k: K) -> Option<String> where String: PartialEq<K>; |
| 37 | + fn debug(&self); |
| 38 | +} |
| 39 | + |
| 40 | +impl ElementExt for Element { |
| 41 | + fn get_child_text<K>(&self, k: K) -> Option<String> |
| 42 | + where String: PartialEq<K> |
| 43 | + { |
| 44 | + self.get_child(k).map(|c| try!(c.text.clone())) |
| 45 | + } |
| 46 | + |
| 47 | + fn debug(&self) { |
| 48 | + println!("<{}>", self.name); |
| 49 | + for c in &self.children { |
| 50 | + println!("{}: {:?}", c.name, c.text) |
| 51 | + } |
| 52 | + println!("</{}>", self.name); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Debug)] |
| 57 | +pub struct Device { |
| 58 | + pub name: String, |
| 59 | + pub peripherals: Vec<Peripheral>, |
| 60 | + pub defaults: Defaults, |
| 61 | +} |
| 62 | + |
| 63 | +impl Device { |
| 64 | + /// Parses a SVD file |
| 65 | + /// |
| 66 | + /// # Panics |
| 67 | + /// |
| 68 | + /// If the input is an invalid SVD file (yay, no error handling) |
| 69 | + pub fn parse(svd: &str) -> Device { |
| 70 | + let tree = &try!(Element::parse(svd.as_bytes())); |
| 71 | + |
| 72 | + Device { |
| 73 | + name: try!(tree.get_child_text("name")), |
| 74 | + peripherals: try!(tree.get_child("peripherals")) |
| 75 | + .children |
| 76 | + .iter() |
| 77 | + .map(Peripheral::parse) |
| 78 | + .collect(), |
| 79 | + defaults: Defaults::parse(tree), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[derive(Debug)] |
| 85 | +pub struct Peripheral { |
| 86 | + pub name: String, |
| 87 | + pub description: Option<String>, |
| 88 | + pub base_address: u32, |
| 89 | + pub interrupt: Option<Interrupt>, |
| 90 | + pub registers: Option<Vec<Register>>, |
| 91 | +} |
| 92 | + |
| 93 | +impl Peripheral { |
| 94 | + fn parse(tree: &Element) -> Peripheral { |
| 95 | + assert_eq!(tree.name, "peripheral"); |
| 96 | + |
| 97 | + Peripheral { |
| 98 | + name: try!(tree.get_child_text("name")), |
| 99 | + description: tree.get_child_text("description"), |
| 100 | + base_address: try!(parse::u32(try!(tree.get_child("baseAddress")))), |
| 101 | + interrupt: tree.get_child("interrupt").map(Interrupt::parse), |
| 102 | + registers: tree.get_child("registers").map(|rs| { |
| 103 | + rs.children |
| 104 | + .iter() |
| 105 | + .filter_map(Register::parse) |
| 106 | + .collect() |
| 107 | + }), |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[derive(Debug)] |
| 113 | +pub struct Interrupt { |
| 114 | + pub name: String, |
| 115 | + pub description: Option<String>, |
| 116 | + pub value: u32, |
| 117 | +} |
| 118 | + |
| 119 | +impl Interrupt { |
| 120 | + fn parse(tree: &Element) -> Interrupt { |
| 121 | + Interrupt { |
| 122 | + name: try!(tree.get_child_text("name")), |
| 123 | + description: tree.get_child_text("description"), |
| 124 | + value: try!(parse::u32(try!(tree.get_child("value")))), |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#[derive(Debug)] |
| 130 | +pub struct Register { |
| 131 | + pub name: String, |
| 132 | + pub description: String, |
| 133 | + pub address_offset: u32, |
| 134 | + pub size: Option<u32>, |
| 135 | + pub access: Option<Access>, |
| 136 | + pub reset_value: Option<u32>, |
| 137 | + pub reset_mask: Option<u32>, |
| 138 | + pub fields: Option<Vec<Field>>, |
| 139 | +} |
| 140 | + |
| 141 | +impl Register { |
| 142 | + // TODO handle "clusters", return `Register` not an `Option` |
| 143 | + fn parse(tree: &Element) -> Option<Register> { |
| 144 | + if tree.name == "cluster" { |
| 145 | + return None; |
| 146 | + } |
| 147 | + |
| 148 | + assert_eq!(tree.name, "register"); |
| 149 | + |
| 150 | + Some(Register { |
| 151 | + name: try!(tree.get_child_text("name")), |
| 152 | + description: try!(tree.get_child_text("description")), |
| 153 | + address_offset: try!(parse::u32(try!(tree.get_child("addressOffset")))), |
| 154 | + size: tree.get_child("size").map(|t| try!(parse::u32(t))), |
| 155 | + access: tree.get_child("access").map(Access::parse), |
| 156 | + reset_value: tree.get_child("resetValue").map(|t| try!(parse::u32(t))), |
| 157 | + reset_mask: tree.get_child("resetMask").map(|t| try!(parse::u32(t))), |
| 158 | + fields: tree.get_child("fields") |
| 159 | + .map(|fs| fs.children.iter().map(Field::parse).collect()), |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +#[derive(Debug)] |
| 165 | +pub enum Access { |
| 166 | + ReadOnly, |
| 167 | + ReadWrite, |
| 168 | + ReadWriteOnce, |
| 169 | + WriteOnly, |
| 170 | +} |
| 171 | + |
| 172 | +impl Access { |
| 173 | + fn parse(tree: &Element) -> Access { |
| 174 | + let text = try!(tree.text.as_ref()); |
| 175 | + |
| 176 | + match &text[..] { |
| 177 | + "read-only" => Access::ReadOnly, |
| 178 | + "read-write" => Access::ReadWrite, |
| 179 | + "read-writeOnce" => Access::ReadWriteOnce, |
| 180 | + "write-only" => Access::WriteOnly, |
| 181 | + _ => panic!("unknown access variant: {}", text), |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +#[derive(Debug)] |
| 187 | +pub struct Field { |
| 188 | + pub name: String, |
| 189 | + pub description: Option<String>, |
| 190 | + pub bit_range: BitRange, |
| 191 | +} |
| 192 | + |
| 193 | +impl Field { |
| 194 | + fn parse(tree: &Element) -> Field { |
| 195 | + assert_eq!(tree.name, "field"); |
| 196 | + |
| 197 | + Field { |
| 198 | + name: try!(tree.get_child_text("name")), |
| 199 | + description: tree.get_child_text("description"), |
| 200 | + bit_range: BitRange::parse(tree), |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +#[derive(Debug)] |
| 206 | +pub struct BitRange { |
| 207 | + pub offset: u32, |
| 208 | + pub width: u32, |
| 209 | +} |
| 210 | + |
| 211 | +impl BitRange { |
| 212 | + fn parse(tree: &Element) -> BitRange { |
| 213 | + let (start, end) = if let Some(range) = tree.get_child("bitRange") { |
| 214 | + let text = try!(range.text.as_ref()); |
| 215 | + |
| 216 | + assert!(text.starts_with('[')); |
| 217 | + assert!(text.ends_with(']')); |
| 218 | + |
| 219 | + let mut parts = text[1..text.len() - 1].split(':'); |
| 220 | + |
| 221 | + (try!(try!(parts.next()).parse()), try!(try!(parts.next()).parse())) |
| 222 | + } else if let (Some(lsb), Some(msb)) = (tree.get_child_text("lsb"), |
| 223 | + tree.get_child_text("msb")) { |
| 224 | + (try!(lsb.parse()), try!(msb.parse::<u32>())) |
| 225 | + } else { |
| 226 | + return BitRange { |
| 227 | + offset: try!(try!(tree.get_child_text("bitOffset")).parse()), |
| 228 | + width: try!(try!(tree.get_child_text("bitWidth")).parse()), |
| 229 | + }; |
| 230 | + }; |
| 231 | + |
| 232 | + BitRange { |
| 233 | + offset: start, |
| 234 | + width: end - start + 1, |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +/// Register default properties |
| 240 | +#[derive(Debug)] |
| 241 | +pub struct Defaults { |
| 242 | + pub size: Option<u32>, |
| 243 | + pub reset_value: Option<u32>, |
| 244 | + pub reset_mask: Option<u32>, |
| 245 | +} |
| 246 | + |
| 247 | +impl Defaults { |
| 248 | + fn parse(tree: &Element) -> Defaults { |
| 249 | + Defaults { |
| 250 | + size: tree.get_child("size").map(|t| try!(parse::u32(t))), |
| 251 | + reset_value: tree.get_child("resetValue").map(|t| try!(parse::u32(t))), |
| 252 | + reset_mask: tree.get_child("resetMask").map(|t| try!(parse::u32(t))), |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments