Skip to content

update svd parser #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/bors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ status = [
"ci-linux (stable, Spansion, x86_64-unknown-linux-gnu, linux)",
"ci-linux (stable, STMicro, x86_64-unknown-linux-gnu, linux)",
"ci-linux (stable, Toshiba, x86_64-unknown-linux-gnu, linux)",
"ci-linux (1.37.0, Nordic, x86_64-unknown-linux-gnu, linux)",
"ci-linux (1.40.0, Nordic, x86_64-unknown-linux-gnu, linux)",
"ci-linux (stable, x86_64-apple-darwin, osx)",
"ci-linux (stable, x86_64-pc-windows-msvc, windows)",
]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

include:
# Test MSRV
- rust: 1.37.0
- rust: 1.40.0
VENDOR: Nordic
TARGET: x86_64-unknown-linux-gnu
TRAVIS_OS_NAME: linux
Expand Down
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ inflections = "1.1"
log = { version = "~0.4", features = ["std"] }
quote = "1.0"
proc-macro2 = "1.0"
anyhow = "1.0.19"
thiserror = "1.0.5"
anyhow = "1.0"
thiserror = "1.0"

[dependencies.svd-parser]
version = "0.9"
version = "0.10"
features = ["derive-from"]

[dependencies.syn]
version = "1.0"
features = ["full","extra-traits"]

[features]
strict = ["svd-parser/strict"]
99 changes: 6 additions & 93 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ main() {

Microchip)
echo '[dependencies.bare-metal]' >> $td/Cargo.toml
echo 'version = "0.2.0"' >> $td/Cargo.toml
echo 'version = "1.0.0"' >> $td/Cargo.toml

echo '[dependencies.mips-mcu]' >> $td/Cargo.toml
echo 'version = "0.1.0"' >> $td/Cargo.toml
Expand Down Expand Up @@ -493,13 +493,13 @@ main() {
# Community-provided RISC-V SVDs
RISC-V)
echo '[dependencies.bare-metal]' >> $td/Cargo.toml
echo 'version = "0.2.0"' >> $td/Cargo.toml
echo 'version = "1.0.0"' >> $td/Cargo.toml

echo '[dependencies.riscv]' >> $td/Cargo.toml
echo 'version = "0.5.0"' >> $td/Cargo.toml
echo 'version = "0.6.0"' >> $td/Cargo.toml

echo '[dependencies.riscv-rt]' >> $td/Cargo.toml
echo 'version = "0.6.0"' >> $td/Cargo.toml
echo 'version = "0.8.0"' >> $td/Cargo.toml

test_svd_for_target riscv https://github.com/raw/riscv-rust/e310x/master/e310x.svd
test_svd_for_target riscv https://github.com/raw/riscv-rust/k210-pac/master/k210.svd
Expand All @@ -524,97 +524,10 @@ main() {

Spansion)
# OK
test_svd MB9AF12xK
test_svd MB9AF12xL
test_svd MB9AF42xK
test_svd MB9AF42xL
test_svd MB9BF12xJ
test_svd MB9BF12xS
test_svd MB9BF12xT
test_svd MB9BF16xx
test_svd MB9BF32xS
test_svd MB9BF32xT
# See Fujitsu for other chips
test_svd MB9BF36xx
test_svd MB9BF42xS
test_svd MB9BF42xT
test_svd MB9BF46xx
test_svd MB9BF52xS
test_svd MB9BF52xT
test_svd MB9BF56xx

# #102 regression tests
# # NOTE it would take too long to test all these so we only test half
# test_svd MB9AF10xN
test_svd MB9AF10xR
# test_svd MB9AF11xK
test_svd MB9AF11xL
# test_svd MB9AF11xM
test_svd MB9AF11xN
# test_svd MB9AF13xK
test_svd MB9AF13xL
# test_svd MB9AF13xM
test_svd MB9AF13xN
# test_svd MB9AF14xL
test_svd MB9AF14xM
# test_svd MB9AF14xN
test_svd MB9AF15xM
# test_svd MB9AF15xN
test_svd MB9AF15xR
# test_svd MB9AF31xK
test_svd MB9AF31xL
# test_svd MB9AF31xM
test_svd MB9AF31xN
# test_svd MB9AF34xL
test_svd MB9AF34xM
# test_svd MB9AF34xN
test_svd MB9AFA3xL
# test_svd MB9AFA3xM
test_svd MB9AFA3xN
# test_svd MB9AFA4xL
test_svd MB9AFA4xM
# test_svd MB9AFA4xN
test_svd MB9AFB4xL
# test_svd MB9AFB4xM
test_svd MB9AFB4xN
# test_svd MB9BF10xN
test_svd MB9BF10xR
# test_svd MB9BF11xN
test_svd MB9BF11xR
# test_svd MB9BF11xS
test_svd MB9BF11xT
# test_svd MB9BF12xK
test_svd MB9BF12xL
# test_svd MB9BF12xM
test_svd MB9BF21xS
# test_svd MB9BF21xT
test_svd MB9BF30xN
# test_svd MB9BF30xR
test_svd MB9BF31xN
# test_svd MB9BF31xR
test_svd MB9BF31xS
# test_svd MB9BF31xT
test_svd MB9BF32xK
# test_svd MB9BF32xL
test_svd MB9BF32xM
# test_svd MB9BF40xN
test_svd MB9BF40xR
# test_svd MB9BF41xN
test_svd MB9BF41xR
# test_svd MB9BF41xS
test_svd MB9BF41xT
# test_svd MB9BF50xN
test_svd MB9BF50xR
# test_svd MB9BF51xN
test_svd MB9BF51xR
# test_svd MB9BF51xS
test_svd MB9BF51xT
# test_svd MB9BF52xK
test_svd MB9BF52xL
# test_svd MB9BF52xM
test_svd MB9BF61xS
# test_svd MB9BF61xT
test_svd MB9BFD1xS
# test_svd MB9BFD1xT
;;

STMicro)
Expand Down Expand Up @@ -670,7 +583,7 @@ main() {

Espressif)
echo '[dependencies.bare-metal]' >> $td/Cargo.toml
echo 'version = "0.2.0"' >> $td/Cargo.toml
echo 'version = "1.0.0"' >> $td/Cargo.toml

echo '[dependencies.xtensa-lx]' >> $td/Cargo.toml
echo 'version = "0.3.0"' >> $td/Cargo.toml
Expand Down
6 changes: 2 additions & 4 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,12 @@ impl FieldRegions {
.filter(|r| {
r.fields.len() > 1 && (idents.iter().filter(|ident| **ident == r.ident).count() > 1)
})
.inspect(|r| {
.for_each(|r| {
warn!(
"Found type name conflict with region {:?}, renamed to {:?}",
r.ident,
r.shortest_ident()
)
})
.for_each(|r| {
);
r.ident = r.shortest_ident();
});
Ok(())
Expand Down
18 changes: 9 additions & 9 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::svd::{
RegisterProperties, Usage, WriteConstraint,
};
use cast::u64;
use core::u64;
use log::warn;
use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
use quote::{quote, ToTokens};
Expand Down Expand Up @@ -254,6 +255,7 @@ pub fn render(
Ok(out)
}

#[allow(clippy::too_many_arguments)]
pub fn fields(
fields: &[Field],
parent: &Register,
Expand Down Expand Up @@ -291,7 +293,7 @@ pub fn fields(
&& (f.access != Some(Access::WriteOnce));
let can_write = can_write && (f.access != Some(Access::ReadOnly));

let mask = 1u64.wrapping_neg() >> (64 - width);
let mask = u64::MAX >> (64 - width);
let hexmask = &util::hex(mask);
let offset = u64::from(offset);
let rv = reset_value.map(|rv| (rv >> offset) & mask);
Expand Down Expand Up @@ -749,8 +751,7 @@ pub fn fields(
fn unsafety(write_constraint: Option<&WriteConstraint>, width: u32) -> Option<Ident> {
match &write_constraint {
Some(&WriteConstraint::Range(range))
if u64::from(range.min) == 0
&& u64::from(range.max) == 1u64.wrapping_neg() >> (64 - width) =>
if range.min == 0 && range.max == u64::MAX >> (64 - width) =>
{
// the SVD has acknowledged that it's safe to write
// any value that can fit in the field
Expand Down Expand Up @@ -1052,8 +1053,7 @@ fn lookup_in_peripheral<'p>(
if let Some(register) = all_registers.iter().find(|r| r.name == base_register) {
if let Some(field) = register
.fields
.as_ref()
.map(|fs| &**fs)
.as_deref()
.unwrap_or(&[])
.iter()
.find(|f| f.name == base_field)
Expand Down Expand Up @@ -1082,7 +1082,7 @@ fn lookup_in_field<'f>(
field: &'f Field,
) -> Result<(&'f EnumeratedValues, Option<Base<'f>>)> {
for evs in &field.enumerated_values {
if evs.name.as_ref().map(|s| &**s) == Some(base_evs) {
if evs.name.as_deref() == Some(base_evs) {
return Ok((
evs,
Some(Base {
Expand All @@ -1107,11 +1107,11 @@ fn lookup_in_register<'r>(
) -> Result<(&'r EnumeratedValues, Option<Base<'r>>)> {
let mut matches = vec![];

for f in register.fields.as_ref().map(|v| &**v).unwrap_or(&[]) {
for f in register.fields.as_deref().unwrap_or(&[]) {
if let Some(evs) = f
.enumerated_values
.iter()
.find(|evs| evs.name.as_ref().map(|s| &**s) == Some(base_evs))
.find(|evs| evs.name.as_deref() == Some(base_evs))
{
matches.push((evs, &f.name))
}
Expand Down Expand Up @@ -1168,7 +1168,7 @@ fn lookup_in_peripherals<'p>(
}
}

fn periph_all_registers<'a>(p: &'a Peripheral) -> Vec<&'a Register> {
fn periph_all_registers(p: &Peripheral) -> Vec<&Register> {
let mut par: Vec<&Register> = Vec::new();
let mut rem: Vec<&RegisterCluster> = Vec::new();
if p.registers.is_none() {
Expand Down
10 changes: 2 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,16 @@ mod util;

pub use crate::util::Target;

#[non_exhaustive]
pub struct Generation {
pub lib_rs: String,
pub device_specific: Option<DeviceSpecific>,

// Reserve the right to add more fields to this struct
_extensible: (),
}

#[non_exhaustive]
pub struct DeviceSpecific {
pub device_x: String,
pub build_rs: String,

// Reserve the right to add more fields to this struct
_extensible: (),
}

use anyhow::Result;
Expand Down Expand Up @@ -534,14 +530,12 @@ pub fn generate(xml: &str, target: Target, nightly: bool) -> Result<Generation>
Some(DeviceSpecific {
device_x,
build_rs: util::build_rs().to_string(),
_extensible: (),
})
};

Ok(Generation {
lib_rs,
device_specific,
_extensible: (),
})
}

Expand Down
5 changes: 3 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub const BITS_PER_BYTE: u32 = 8;
/// that are not valid in Rust ident
const BLACKLIST_CHARS: &[char] = &['(', ')', '[', ']', '/', ' ', '-'];

#[allow(non_camel_case_types)]
#[derive(Clone, Copy, PartialEq)]
pub enum Target {
CortexM,
Expand Down Expand Up @@ -166,7 +167,7 @@ pub fn respace(s: &str) -> String {
pub fn escape_brackets(s: &str) -> String {
s.split('[')
.fold("".to_string(), |acc, x| {
if acc == "" {
if acc.is_empty() {
x.to_string()
} else if acc.ends_with('\\') {
acc + "[" + x
Expand All @@ -176,7 +177,7 @@ pub fn escape_brackets(s: &str) -> String {
})
.split(']')
.fold("".to_string(), |acc, x| {
if acc == "" {
if acc.is_empty() {
x.to_string()
} else if acc.ends_with('\\') {
acc + "]" + x
Expand Down