svd_generator/tree/
node.rs

1use crate::{Error, Result};
2
3/// Parses the `interrupts` property.
4///
5/// # Parameters
6///
7/// - `node`: Device Tree node to parse
8/// - `default_name`: default interrupt name (used if `interrupt-names` property is absent)
9/// - `count`: optional peripheral count if more than one of its type exists
10pub fn parse_interrupts<'b, 'a: 'b>(
11    node: &fdt::node::FdtNode<'b, 'a>,
12    default_name: &str,
13    count: Option<usize>,
14) -> Result<Vec<svd::Interrupt>> {
15    let ints_prop = node
16        .property("interrupts")
17        .ok_or(Error::DeviceTree(format!(
18            "{} missing `interrupts` property",
19            node.name
20        )))?;
21
22    let ints = ints_prop.iter_cell_size(node.interrupt_cells().unwrap_or(1).into());
23
24    // `interrupt-names` is an unspecified property as of v0.4, so it may not be present
25    let int_names = node.property("interrupt-names").map(|p| p.iter_str());
26
27    let mut i = 0;
28    let def_int_names = core::iter::from_fn(move || {
29        let num = i;
30        i += 1;
31
32        if num > 0 {
33            Some(format!("{default_name}{num}"))
34        } else {
35            Some(default_name.into())
36        }
37    });
38
39    let count_str = count.map(|c| format!("{c}")).unwrap_or_default();
40
41    match int_names {
42        Some(p) => Ok(ints
43            .zip(p)
44            .filter_map(|(int, name)| {
45                svd::Interrupt::builder()
46                    .name(format!("{}{count_str}", name.to_uppercase()))
47                    .value(int as u32)
48                    .build(svd::ValidateLevel::Strict)
49                    .ok()
50            })
51            .collect()),
52        None => Ok(ints
53            .zip(def_int_names)
54            .filter_map(|(int, name)| {
55                svd::Interrupt::builder()
56                    .name(format!("{}{count_str}", name.to_uppercase()))
57                    .value(int as u32)
58                    .build(svd::ValidateLevel::Strict)
59                    .ok()
60            })
61            .collect()),
62    }
63}