diff --git a/ci/script.sh b/ci/script.sh index 2e8eb961..5a42607a 100644 --- a/ci/script.sh +++ b/ci/script.sh @@ -39,6 +39,8 @@ main() { echo 'cortex-m-rt = "0.3.0"' >> $td/Cargo.toml echo 'vcell = "0.1.0"' >> $td/Cargo.toml echo 'msp430 = "0.1.0"' >> $td/Cargo.toml + echo 'riscv = "0.1.4"' >> $td/Cargo.toml + echo 'riscv-rt = "0.1.3"' >> $td/Cargo.toml echo '[profile.dev]' >> $td/Cargo.toml echo 'incremental = false' >> $td/Cargo.toml @@ -391,6 +393,9 @@ main() { cd $td && curl -LO \ https://github.com/pftbest/msp430g2553/raw/v0.1.0/msp430g2553.svd + cd $td && + curl -LO \ + https://raw.githubusercontent.com/riscv-rust/e310x/master/e310x.svd ) target/$TARGET/release/svd2rust --target msp430 -i $td/msp430g2553.svd | \ @@ -402,6 +407,11 @@ main() { ( rustfmt 2>/dev/null > $td/src/lib.rs || true ) cargo check --manifest-path $td/Cargo.toml + + target/$TARGET/release/svd2rust --target riscv -i $td/e310x.svd | \ + ( rustfmt 2>/dev/null > $td/src/lib.rs || true ) + + cargo check --manifest-path $td/Cargo.toml ;; Nordic) diff --git a/src/generate/device.rs b/src/generate/device.rs index f3f09747..150f41bf 100644 --- a/src/generate/device.rs +++ b/src/generate/device.rs @@ -42,6 +42,7 @@ pub fn render(d: &Device, target: &Target) -> Result> { #![deny(warnings)] #![allow(non_camel_case_types)] #![feature(const_fn)] + #![feature(try_from)] #![no_std] }); @@ -62,6 +63,13 @@ pub fn render(d: &Device, target: &Target) -> Result> { extern crate msp430_rt; }); } + Target::RISCV => { + out.push(quote! { + extern crate riscv; + #[cfg(feature = "rt")] + extern crate riscv_rt; + }); + } Target::None => {} } @@ -141,6 +149,7 @@ pub fn render(d: &Device, target: &Target) -> Result> { let take = match *target { Target::CortexM => Some(Ident::new("cortex_m")), Target::Msp430 => Some(Ident::new("msp430")), + Target::RISCV => Some(Ident::new("riscv")), Target::None => None, }.map(|krate| { quote! { diff --git a/src/generate/interrupt.rs b/src/generate/interrupt.rs index 98e8696f..2b3e9dfd 100644 --- a/src/generate/interrupt.rs +++ b/src/generate/interrupt.rs @@ -21,6 +21,7 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R interrupts.sort_by_key(|i| i.value); let mut arms = vec![]; + let mut from_arms = vec![]; let mut elements = vec![]; let mut names = vec![]; let mut variants = vec![]; @@ -60,6 +61,10 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R Interrupt::#name_uc => #value, }); + from_arms.push(quote! { + #value => Ok(Interrupt::#name_uc), + }); + elements.push(quote!(Some(#name_uc))); names.push(name_uc); } @@ -169,6 +174,7 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R ]; }); } + Target::RISCV => {} Target::None => {} } @@ -186,6 +192,23 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R } } } + + use core::convert::TryFrom; + + #[derive(Debug, Copy, Clone)] + pub struct TryFromInterruptError(()); + + impl TryFrom for Interrupt { + type Error = TryFromInterruptError; + + #[inline] + fn try_from(value: u8) -> Result { + match value { + #(#from_arms)* + _ => Err(TryFromInterruptError(())), + } + } + } }); if *target != Target::None { diff --git a/src/main.rs b/src/main.rs index 67b34796..b194a783 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use errors::*; pub enum Target { CortexM, Msp430, + RISCV, None, } @@ -33,6 +34,7 @@ impl Target { Ok(match s { "cortex-m" => Target::CortexM, "msp430" => Target::Msp430, + "riscv" => Target::RISCV, "none" => Target::None, _ => bail!("unknown target {}", s), })