Skip to content

Commit c40e4ae

Browse files
committed
unstable-riscv feature
1 parent 78d4515 commit c40e4ae

File tree

6 files changed

+129
-114
lines changed

6 files changed

+129
-114
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Implement `riscv-pac` traits via experimental `riscv` element in SVD.
11+
You must use the `unstable-riscv` feature to use this.
1012
- Fix `enumeratedValues` with `isDefault` only
1113

1214
## [v0.33.4] - 2024-06-16

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ default = ["bin", "json", "yaml"]
3535
bin = ["dep:clap", "dep:env_logger", "serde", "dep:irx-config"]
3636
json = ["dep:serde_json"]
3737
yaml = ["dep:serde_yaml"]
38+
unstable-riscv = ["svd-rs/unstable-riscv", "svd-parser/unstable-riscv"]
3839

3940
[dependencies]
4041
clap = { version = "4.0", optional = true }
@@ -58,13 +59,13 @@ url = { version = "2.5", features = ["serde"] }
5859

5960
[dependencies.svd-parser]
6061
git = "https://github.com/romancardenas/svd.git" # TODO use crates.io
61-
rev = "71b33bf"
62+
rev = "ddc3d99"
6263
features = ["expand"]
6364
# version = "0.14.5"
6465

6566
[dependencies.svd-rs]
6667
git = "https://github.com/romancardenas/svd.git" # TODO use crates.io
67-
rev = "71b33bf"
68+
rev = "ddc3d99"
6869
features = ["serde"]
6970
# version = "0.14.8"
7071

src/generate/device.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use crate::config::{Config, Target};
1111
use crate::util::{self, ident};
1212
use anyhow::{Context, Result};
1313

14-
use crate::generate::{interrupt, peripheral, riscv};
14+
#[cfg(feature = "unstable-riscv")]
15+
use crate::generate::riscv;
16+
use crate::generate::{interrupt, peripheral};
1517

1618
/// Whole device generation
1719
pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<TokenStream> {
@@ -188,16 +190,28 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
188190
}
189191

190192
debug!("Rendering interrupts");
191-
if config.target != Target::RISCV {
192-
out.extend(interrupt::render(
193-
config.target,
194-
&d.peripherals,
195-
device_x,
196-
config,
197-
)?);
198-
} else {
199-
let riscv = riscv::render(d.riscv.as_ref(), &d.peripherals, device_x)?;
200-
out.extend(riscv);
193+
match config.target {
194+
#[cfg(feature = "unstable-riscv")]
195+
Target::RISCV => {
196+
if let Some(riscv) = d.riscv.as_ref() {
197+
out.extend(riscv::render(riscv, &d.peripherals, device_x)?);
198+
} else {
199+
out.extend(interrupt::render(
200+
config.target,
201+
&d.peripherals,
202+
device_x,
203+
config,
204+
)?);
205+
}
206+
}
207+
_ => {
208+
out.extend(interrupt::render(
209+
config.target,
210+
&d.peripherals,
211+
device_x,
212+
config,
213+
)?);
214+
}
201215
}
202216

203217
let feature_format = config.ident_formats.get("peripheral_feature").unwrap();

src/generate/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ pub mod device;
22
pub mod interrupt;
33
pub mod peripheral;
44
pub mod register;
5+
#[cfg(feature = "unstable-riscv")]
56
pub mod riscv;

src/generate/riscv.rs

Lines changed: 96 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ use std::{collections::HashMap, fmt::Write, str::FromStr};
99

1010
/// Whole RISC-V generation
1111
pub fn render(
12-
r: Option<&Riscv>,
12+
r: &Riscv,
1313
peripherals: &[Peripheral],
1414
device_x: &mut String, // TODO
1515
) -> Result<TokenStream> {
1616
let mut mod_items = TokenStream::new();
17-
mod_items.extend(quote! { use riscv_pac::pac_enum; });
1817

1918
let external_interrupts = peripherals
2019
.iter()
@@ -52,119 +51,117 @@ pub fn render(
5251
mod_items.extend(quote! {
5352
/// External interrupts. These interrupts are handled by the external peripherals.
5453
#[repr(usize)]
55-
#[pac_enum(unsafe ExternalInterruptNumber)]
54+
#[riscv_pac::pac_enum(unsafe ExternalInterruptNumber)]
5655
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5756
pub enum ExternalInterrupt {
5857
#(#interrupts)*
5958
}
6059
});
6160
}
6261

63-
if let Some(r) = r {
64-
if !r.core_interrupts.is_empty() {
65-
writeln!(device_x, "/* Core interrupt sources and trap handlers */")?;
66-
mod_items.extend(quote! { pub use riscv_pac::CoreInterruptNumber; });
62+
if !r.core_interrupts.is_empty() {
63+
writeln!(device_x, "/* Core interrupt sources and trap handlers */")?;
64+
mod_items.extend(quote! { pub use riscv_pac::CoreInterruptNumber; });
6765

68-
let mut interrupts = vec![];
69-
for i in r.core_interrupts.iter() {
70-
let name = TokenStream::from_str(&i.name).unwrap();
71-
let value = TokenStream::from_str(&format!("{}", i.value)).unwrap();
72-
let description = format!(
73-
"{} - {}",
74-
i.value,
75-
i.description
76-
.as_ref()
77-
.map(|s| util::respace(s))
78-
.as_ref()
79-
.map(|s| util::escape_special_chars(s))
80-
.unwrap_or_else(|| i.name.clone())
81-
);
66+
let mut interrupts = vec![];
67+
for i in r.core_interrupts.iter() {
68+
let name = TokenStream::from_str(&i.name).unwrap();
69+
let value = TokenStream::from_str(&format!("{}", i.value)).unwrap();
70+
let description = format!(
71+
"{} - {}",
72+
i.value,
73+
i.description
74+
.as_ref()
75+
.map(|s| util::respace(s))
76+
.as_ref()
77+
.map(|s| util::escape_special_chars(s))
78+
.unwrap_or_else(|| i.name.clone())
79+
);
8280

83-
writeln!(device_x, "PROVIDE({name} = DefaultHandler);")?;
84-
writeln!(
85-
device_x,
86-
"PROVIDE(_start_{name}_trap = _start_DefaultHandler_trap);"
87-
)?;
81+
writeln!(device_x, "PROVIDE({name} = DefaultHandler);")?;
82+
writeln!(
83+
device_x,
84+
"PROVIDE(_start_{name}_trap = _start_DefaultHandler_trap);"
85+
)?;
8886

89-
interrupts.push(quote! {
90-
#[doc = #description]
91-
#name = #value,
92-
});
93-
}
94-
mod_items.extend(quote! {
95-
/// Core interrupts. These interrupts are handled by the core itself.
96-
#[repr(usize)]
97-
#[pac_enum(unsafe CoreInterruptNumber)]
98-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99-
pub enum CoreInterrupt {
100-
#(#interrupts)*
101-
}
87+
interrupts.push(quote! {
88+
#[doc = #description]
89+
#name = #value,
10290
});
10391
}
92+
mod_items.extend(quote! {
93+
/// Core interrupts. These interrupts are handled by the core itself.
94+
#[repr(usize)]
95+
#[riscv_pac::pac_enum(unsafe CoreInterruptNumber)]
96+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97+
pub enum CoreInterrupt {
98+
#(#interrupts)*
99+
}
100+
});
101+
}
104102

105-
if !r.priorities.is_empty() {
106-
mod_items.extend(quote! { pub use riscv_pac::PriorityNumber; });
107-
let priorities = r.priorities.iter().map(|p| {
108-
let name = TokenStream::from_str(&p.name).unwrap();
109-
let value = TokenStream::from_str(&format!("{}", p.value)).unwrap();
110-
let description = format!(
111-
"{} - {}",
112-
p.value,
113-
p.description
114-
.as_ref()
115-
.map(|s| util::respace(s))
116-
.as_ref()
117-
.map(|s| util::escape_special_chars(s))
118-
.unwrap_or_else(|| p.name.clone())
119-
);
103+
if !r.priorities.is_empty() {
104+
mod_items.extend(quote! { pub use riscv_pac::PriorityNumber; });
105+
let priorities = r.priorities.iter().map(|p| {
106+
let name = TokenStream::from_str(&p.name).unwrap();
107+
let value = TokenStream::from_str(&format!("{}", p.value)).unwrap();
108+
let description = format!(
109+
"{} - {}",
110+
p.value,
111+
p.description
112+
.as_ref()
113+
.map(|s| util::respace(s))
114+
.as_ref()
115+
.map(|s| util::escape_special_chars(s))
116+
.unwrap_or_else(|| p.name.clone())
117+
);
120118

121-
quote! {
122-
#[doc = #description]
123-
#name = #value,
124-
}
125-
});
126-
mod_items.extend(quote! {
127-
/// Priority levels in the device
128-
#[repr(u8)]
129-
#[pac_enum(unsafe PriorityNumber)]
130-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131-
pub enum Priority {
132-
#(#priorities)*
133-
}
134-
});
135-
}
119+
quote! {
120+
#[doc = #description]
121+
#name = #value,
122+
}
123+
});
124+
mod_items.extend(quote! {
125+
/// Priority levels in the device
126+
#[repr(u8)]
127+
#[riscv_pac::pac_enum(unsafe PriorityNumber)]
128+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
129+
pub enum Priority {
130+
#(#priorities)*
131+
}
132+
});
133+
}
136134

137-
if !r.harts.is_empty() {
138-
mod_items.extend(quote! { pub use riscv_pac::HartIdNumber; });
139-
let harts = r.harts.iter().map(|h| {
140-
let name = TokenStream::from_str(&h.name).unwrap();
141-
let value = TokenStream::from_str(&format!("{}", h.value)).unwrap();
142-
let description = format!(
143-
"{} - {}",
144-
h.value,
145-
h.description
146-
.as_ref()
147-
.map(|s| util::respace(s))
148-
.as_ref()
149-
.map(|s| util::escape_special_chars(s))
150-
.unwrap_or_else(|| h.name.clone())
151-
);
135+
if !r.harts.is_empty() {
136+
mod_items.extend(quote! { pub use riscv_pac::HartIdNumber; });
137+
let harts = r.harts.iter().map(|h| {
138+
let name = TokenStream::from_str(&h.name).unwrap();
139+
let value = TokenStream::from_str(&format!("{}", h.value)).unwrap();
140+
let description = format!(
141+
"{} - {}",
142+
h.value,
143+
h.description
144+
.as_ref()
145+
.map(|s| util::respace(s))
146+
.as_ref()
147+
.map(|s| util::escape_special_chars(s))
148+
.unwrap_or_else(|| h.name.clone())
149+
);
152150

153-
quote! {
154-
#[doc = #description]
155-
#name = #value,
156-
}
157-
});
158-
mod_items.extend(quote! {
159-
/// HARTs in the device
160-
#[repr(u16)]
161-
#[pac_enum(unsafe HartIdNumber)]
162-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
163-
pub enum Hart {
164-
#(#harts)*
165-
}
166-
});
167-
}
151+
quote! {
152+
#[doc = #description]
153+
#name = #value,
154+
}
155+
});
156+
mod_items.extend(quote! {
157+
/// HARTs in the device
158+
#[repr(u16)]
159+
#[riscv_pac::pac_enum(unsafe HartIdNumber)]
160+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
161+
pub enum Hart {
162+
#(#harts)*
163+
}
164+
});
168165
}
169166

170167
Ok(quote! {

0 commit comments

Comments
 (0)