Skip to content

Commit 1515894

Browse files
authored
Merge pull request #185 from riscv-rust/svd2rust-riscv
Add support for building the e310x crate
2 parents 231d24e + ec024c7 commit 1515894

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

ci/script.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ main() {
3939
echo 'cortex-m-rt = "0.3.0"' >> $td/Cargo.toml
4040
echo 'vcell = "0.1.0"' >> $td/Cargo.toml
4141
echo 'msp430 = "0.1.0"' >> $td/Cargo.toml
42+
echo 'riscv = "0.1.4"' >> $td/Cargo.toml
43+
echo 'riscv-rt = "0.1.3"' >> $td/Cargo.toml
4244
echo '[profile.dev]' >> $td/Cargo.toml
4345
echo 'incremental = false' >> $td/Cargo.toml
4446

@@ -391,6 +393,9 @@ main() {
391393
cd $td &&
392394
curl -LO \
393395
https://github.com/pftbest/msp430g2553/raw/v0.1.0/msp430g2553.svd
396+
cd $td &&
397+
curl -LO \
398+
https://raw.githubusercontent.com/riscv-rust/e310x/master/e310x.svd
394399
)
395400

396401
target/$TARGET/release/svd2rust --target msp430 -i $td/msp430g2553.svd | \
@@ -402,6 +407,11 @@ main() {
402407
( rustfmt 2>/dev/null > $td/src/lib.rs || true )
403408

404409
cargo check --manifest-path $td/Cargo.toml
410+
411+
target/$TARGET/release/svd2rust --target riscv -i $td/e310x.svd | \
412+
( rustfmt 2>/dev/null > $td/src/lib.rs || true )
413+
414+
cargo check --manifest-path $td/Cargo.toml
405415
;;
406416

407417
Nordic)

src/generate/device.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn render(d: &Device, target: &Target) -> Result<Vec<Tokens>> {
4242
#![deny(warnings)]
4343
#![allow(non_camel_case_types)]
4444
#![feature(const_fn)]
45+
#![feature(try_from)]
4546
#![no_std]
4647
});
4748

@@ -62,6 +63,13 @@ pub fn render(d: &Device, target: &Target) -> Result<Vec<Tokens>> {
6263
extern crate msp430_rt;
6364
});
6465
}
66+
Target::RISCV => {
67+
out.push(quote! {
68+
extern crate riscv;
69+
#[cfg(feature = "rt")]
70+
extern crate riscv_rt;
71+
});
72+
}
6573
Target::None => {}
6674
}
6775

@@ -141,6 +149,7 @@ pub fn render(d: &Device, target: &Target) -> Result<Vec<Tokens>> {
141149
let take = match *target {
142150
Target::CortexM => Some(Ident::new("cortex_m")),
143151
Target::Msp430 => Some(Ident::new("msp430")),
152+
Target::RISCV => Some(Ident::new("riscv")),
144153
Target::None => None,
145154
}.map(|krate| {
146155
quote! {

src/generate/interrupt.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R
2121
interrupts.sort_by_key(|i| i.value);
2222

2323
let mut arms = vec![];
24+
let mut from_arms = vec![];
2425
let mut elements = vec![];
2526
let mut names = vec![];
2627
let mut variants = vec![];
@@ -60,6 +61,10 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R
6061
Interrupt::#name_uc => #value,
6162
});
6263

64+
from_arms.push(quote! {
65+
#value => Ok(Interrupt::#name_uc),
66+
});
67+
6368
elements.push(quote!(Some(#name_uc)));
6469
names.push(name_uc);
6570
}
@@ -169,6 +174,7 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R
169174
];
170175
});
171176
}
177+
Target::RISCV => {}
172178
Target::None => {}
173179
}
174180

@@ -186,6 +192,23 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R
186192
}
187193
}
188194
}
195+
196+
use core::convert::TryFrom;
197+
198+
#[derive(Debug, Copy, Clone)]
199+
pub struct TryFromInterruptError(());
200+
201+
impl TryFrom<u8> for Interrupt {
202+
type Error = TryFromInterruptError;
203+
204+
#[inline]
205+
fn try_from(value: u8) -> Result<Self, Self::Error> {
206+
match value {
207+
#(#from_arms)*
208+
_ => Err(TryFromInterruptError(())),
209+
}
210+
}
211+
}
189212
});
190213

191214
if *target != Target::None {

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use errors::*;
2525
pub enum Target {
2626
CortexM,
2727
Msp430,
28+
RISCV,
2829
None,
2930
}
3031

@@ -33,6 +34,7 @@ impl Target {
3334
Ok(match s {
3435
"cortex-m" => Target::CortexM,
3536
"msp430" => Target::Msp430,
37+
"riscv" => Target::RISCV,
3638
"none" => Target::None,
3739
_ => bail!("unknown target {}", s),
3840
})

0 commit comments

Comments
 (0)