1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! CMSIS-SVD parser for Drone, an Embedded Operating System.
//!
//! # Documentation
//!
//! - [Drone Book](https://book.drone-os.com/)
//! - [API documentation](https://api.drone-os.com/drone-svd/0.11/)
//!
//! # Usage
//!
//! Place the following to the Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! drone-svd = { version = "0.11.0" }
//! ```

#![feature(generator_trait)]
#![feature(generators)]
#![deny(elided_lifetimes_in_paths)]
#![warn(missing_docs)]
#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions, clippy::must_use_candidate)]

mod device;
mod field;
mod peripheral;
mod register;

pub use self::{
    device::Device,
    field::Field,
    peripheral::{Interrupt, Peripheral},
    register::Register,
};

use anyhow::Error;
use serde::{de, Deserialize, Deserializer};
use std::{
    env,
    fs::File,
    io::{prelude::*, BufReader},
    num::ParseIntError,
    ops::Range,
    path::Path,
};

/// Bit-band memory region.
pub const BIT_BAND: Range<u32> = 0x4000_0000..0x4010_0000;

/// Predefined access rights.
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
pub enum Access {
    /// Read operations have an undefined result. Write access is permitted.
    WriteOnly,
    /// Read access is permitted. Write operations have an undefined result.
    ReadOnly,
    /// Read and write accesses are permitted. Writes affect the state of the
    /// register and reads return the register value.
    ReadWrite,
    /// Read access is always permitted. Only the first write access after a
    /// reset will have an effect on the content. Other write operations have an
    /// undefined result.
    ReadWriteonce,
}

/// Parse the SVD file at `path`.
pub fn parse<P: AsRef<Path>>(path: P) -> Result<Device, Error> {
    let mut input = BufReader::new(File::open(path)?);
    let mut xml = String::new();
    input.read_to_string(&mut xml)?;
    let device = serde_xml_rs::deserialize(xml.as_bytes())?;
    Ok(device)
}

/// Instructs cargo to rerun the build script when RUSTFLAGS environment
/// variables changed.
pub fn rerun_if_env_changed() {
    for (var, _) in env::vars_os() {
        if let Some(var) = var.to_str() {
            if var.ends_with("RUSTFLAGS") {
                println!("cargo:rerun-if-env-changed={}", var);
            }
        }
    }
}

pub(crate) trait DimGroup {
    fn dim(&self) -> Option<(u32, u32)>;

    fn name(&self) -> &String;

    fn dim_group(&self) -> Vec<(String, u32)> {
        if let Some((count, step)) = self.dim() {
            if count > 1 {
                return (0..count)
                    .map(|i| (self.name().replace("[%s]", &format!("_{}", i)), i * step))
                    .collect();
            }
        }
        vec![(self.name().clone(), 0)]
    }
}

pub(crate) fn deserialize_int<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
    D: Deserializer<'de>,
{
    parse_int(&String::deserialize(deserializer)?).map_err(de::Error::custom)
}

pub(crate) fn deserialize_int_opt<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error>
where
    D: Deserializer<'de>,
{
    let s = Option::<String>::deserialize(deserializer)?;
    if let Some(s) = s {
        parse_int(&s).map(Some).map_err(de::Error::custom)
    } else {
        Ok(None)
    }
}

fn parse_int(src: &str) -> Result<u32, ParseIntError> {
    let mut range = 0..src.len();
    let radix = if src.starts_with("0x") || src.starts_with("0X") {
        range.start += 2;
        16
    } else if src.starts_with('0') && src.len() > 1 {
        range.start += 1;
        8
    } else {
        10
    };
    u32::from_str_radix(&src[range], radix)
}