Skip to content

Commit 5e211cd

Browse files
committed
added trap macros with tests in riscv-rt
1 parent 9b7d116 commit 5e211cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+657
-237
lines changed

riscv-pac/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616

1717
### Changed
1818

19-
- `InterruptNumber` trait now expects `usize`
19+
- All traits now work with `usize` data type.
2020

2121
## [v0.1.1] - 2024-02-15
2222

riscv-pac/src/lib.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub unsafe trait ExceptionNumber: Copy {
2626
fn number(self) -> usize;
2727

2828
/// Tries to convert a number to a valid exception.
29-
/// If the conversion fails, it returns an error with the number back.
3029
fn from_number(value: usize) -> Result<Self>;
3130
}
3231

@@ -51,8 +50,7 @@ pub unsafe trait InterruptNumber: Copy {
5150
/// Converts an interrupt source to its corresponding number.
5251
fn number(self) -> usize;
5352

54-
/// Tries to convert a number to a valid interrupt source.
55-
/// If the conversion fails, it returns an error with the number back.
53+
/// Tries to convert a number to a valid interrupt.
5654
fn from_number(value: usize) -> Result<Self>;
5755
}
5856

@@ -83,7 +81,7 @@ pub unsafe trait ExternalInterruptNumber: InterruptNumber {}
8381
/// Trait for enums of priority levels.
8482
///
8583
/// This trait should be implemented by a peripheral access crate (PAC) on its enum of available
86-
/// priority numbers for a specific device. Each variant must convert to a `u8` of its priority level.
84+
/// priority numbers for a specific device. Each variant must convert to a `usize` of its priority level.
8785
///
8886
/// # Safety
8987
///
@@ -95,20 +93,19 @@ pub unsafe trait ExternalInterruptNumber: InterruptNumber {}
9593
/// * `MAX_PRIORITY_NUMBER` must coincide with the highest allowed priority number.
9694
pub unsafe trait PriorityNumber: Copy {
9795
/// Number assigned to the highest priority level.
98-
const MAX_PRIORITY_NUMBER: u8;
96+
const MAX_PRIORITY_NUMBER: usize;
9997

10098
/// Converts a priority level to its corresponding number.
101-
fn number(self) -> u8;
99+
fn number(self) -> usize;
102100

103101
/// Tries to convert a number to a valid priority level.
104-
/// If the conversion fails, it returns an error with the number back.
105-
fn from_number(value: u8) -> Result<Self>;
102+
fn from_number(value: usize) -> Result<Self>;
106103
}
107104

108105
/// Trait for enums of HART identifiers.
109106
///
110107
/// This trait should be implemented by a peripheral access crate (PAC) on its enum of available
111-
/// HARTs for a specific device. Each variant must convert to a `u16` of its HART ID number.
108+
/// HARTs for a specific device. Each variant must convert to a `usize` of its HART ID number.
112109
///
113110
/// # Safety
114111
///
@@ -120,14 +117,13 @@ pub unsafe trait PriorityNumber: Copy {
120117
/// * `MAX_HART_ID_NUMBER` must coincide with the highest allowed HART ID number.
121118
pub unsafe trait HartIdNumber: Copy {
122119
/// Highest number assigned to a context.
123-
const MAX_HART_ID_NUMBER: u16;
120+
const MAX_HART_ID_NUMBER: usize;
124121

125122
/// Converts a HART ID to its corresponding number.
126-
fn number(self) -> u16;
123+
fn number(self) -> usize;
127124

128125
/// Tries to convert a number to a valid HART ID.
129-
/// If the conversion fails, it returns an error with the number back.
130-
fn from_number(value: u16) -> Result<Self>;
126+
fn from_number(value: usize) -> Result<Self>;
131127
}
132128

133129
#[cfg(test)]
@@ -201,40 +197,40 @@ mod test {
201197
}
202198

203199
unsafe impl PriorityNumber for Priority {
204-
const MAX_PRIORITY_NUMBER: u8 = Self::P3 as u8;
200+
const MAX_PRIORITY_NUMBER: usize = Self::P3 as usize;
205201

206202
#[inline]
207-
fn number(self) -> u8 {
203+
fn number(self) -> usize {
208204
self as _
209205
}
210206

211207
#[inline]
212-
fn from_number(number: u8) -> Result<Self> {
208+
fn from_number(number: usize) -> Result<Self> {
213209
match number {
214210
0 => Ok(Priority::P0),
215211
1 => Ok(Priority::P1),
216212
2 => Ok(Priority::P2),
217213
3 => Ok(Priority::P3),
218-
_ => Err(Error::InvalidVariant(number as _)),
214+
_ => Err(Error::InvalidVariant(number)),
219215
}
220216
}
221217
}
222218

223219
unsafe impl HartIdNumber for HartId {
224-
const MAX_HART_ID_NUMBER: u16 = Self::H2 as u16;
220+
const MAX_HART_ID_NUMBER: usize = Self::H2 as usize;
225221

226222
#[inline]
227-
fn number(self) -> u16 {
223+
fn number(self) -> usize {
228224
self as _
229225
}
230226

231227
#[inline]
232-
fn from_number(number: u16) -> Result<Self> {
228+
fn from_number(number: usize) -> Result<Self> {
233229
match number {
234230
0 => Ok(HartId::H0),
235231
1 => Ok(HartId::H1),
236232
2 => Ok(HartId::H2),
237-
_ => Err(Error::InvalidVariant(number as _)),
233+
_ => Err(Error::InvalidVariant(number)),
238234
}
239235
}
240236
}

riscv-peripheral/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313

1414
### Changed
1515

16+
- Adapt to new version of `riscv-pac` traits.
1617
- `PLIC` now expects interrupt enums to implement the `riscv_pac::ExternalInterruptNumber` trait.
1718

1819
### Fixed

riscv-peripheral/examples/e310x.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ pub enum HartId {
1313
}
1414

1515
unsafe impl HartIdNumber for HartId {
16-
const MAX_HART_ID_NUMBER: u16 = Self::H0 as u16;
16+
const MAX_HART_ID_NUMBER: usize = Self::H0 as usize;
1717

1818
#[inline]
19-
fn number(self) -> u16 {
19+
fn number(self) -> usize {
2020
self as _
2121
}
2222

2323
#[inline]
24-
fn from_number(number: u16) -> Result<Self> {
24+
fn from_number(number: usize) -> Result<Self> {
2525
match number {
2626
0 => Ok(Self::H0),
27-
_ => Err(Error::InvalidVariant(number as usize)),
27+
_ => Err(Error::InvalidVariant(number)),
2828
}
2929
}
3030
}
@@ -108,6 +108,7 @@ unsafe impl InterruptNumber for Interrupt {
108108
unsafe impl ExternalInterruptNumber for Interrupt {}
109109

110110
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
111+
#[repr(usize)]
111112
pub enum Priority {
112113
P0 = 0,
113114
P1 = 1,
@@ -120,20 +121,20 @@ pub enum Priority {
120121
}
121122

122123
unsafe impl PriorityNumber for Priority {
123-
const MAX_PRIORITY_NUMBER: u8 = Self::P7 as u8;
124+
const MAX_PRIORITY_NUMBER: usize = Self::P7 as usize;
124125

125126
#[inline]
126-
fn number(self) -> u8 {
127+
fn number(self) -> usize {
127128
self as _
128129
}
129130

130131
#[inline]
131-
fn from_number(number: u8) -> Result<Self> {
132+
fn from_number(number: usize) -> Result<Self> {
132133
if number > Self::MAX_PRIORITY_NUMBER {
133-
Err(Error::InvalidVariant(number as usize))
134+
Err(Error::InvalidVariant(number))
134135
} else {
135136
// SAFETY: valid priority number
136-
Ok(unsafe { core::mem::transmute::<u8, Priority>(number) })
137+
Ok(unsafe { core::mem::transmute::<usize, Priority>(number) })
137138
}
138139
}
139140
}

riscv-peripheral/src/aclint.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,28 @@ pub(crate) mod test {
6666
use riscv_pac::result::{Error, Result};
6767

6868
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
69-
#[repr(u16)]
69+
#[repr(usize)]
7070
pub(crate) enum HartId {
7171
H0 = 0,
7272
H1 = 1,
7373
H2 = 2,
7474
}
7575

7676
unsafe impl HartIdNumber for HartId {
77-
const MAX_HART_ID_NUMBER: u16 = 2;
77+
const MAX_HART_ID_NUMBER: usize = Self::H2 as usize;
7878

7979
#[inline]
80-
fn number(self) -> u16 {
80+
fn number(self) -> usize {
8181
self as _
8282
}
8383

8484
#[inline]
85-
fn from_number(number: u16) -> Result<Self> {
85+
fn from_number(number: usize) -> Result<Self> {
8686
if number > Self::MAX_HART_ID_NUMBER {
87-
Err(Error::InvalidVariant(number as usize))
87+
Err(Error::InvalidVariant(number))
8888
} else {
8989
// SAFETY: valid context number
90-
Ok(unsafe { core::mem::transmute::<u16, HartId>(number) })
90+
Ok(unsafe { core::mem::transmute::<usize, HartId>(number) })
9191
}
9292
}
9393
}

riscv-peripheral/src/aclint/mswi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl MSWI {
3333
#[inline]
3434
pub fn msip<H: HartIdNumber>(&self, hart_id: H) -> MSIP {
3535
// SAFETY: `hart_id` is valid for the target
36-
unsafe { MSIP::new(self.msip0.get_ptr().offset(hart_id.number() as _) as _) }
36+
unsafe { MSIP::new(self.msip0.get_ptr().add(hart_id.number()) as _) }
3737
}
3838

3939
/// Returns the `MSIP` register for the current HART.

riscv-peripheral/src/aclint/mtimer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl MTIMER {
3535
#[inline]
3636
pub fn mtimecmp<H: HartIdNumber>(&self, hart_id: H) -> MTIMECMP {
3737
// SAFETY: `hart_id` is valid for the target
38-
unsafe { MTIMECMP::new(self.mtimecmp0.get_ptr().offset(hart_id.number() as _) as _) }
38+
unsafe { MTIMECMP::new(self.mtimecmp0.get_ptr().add(hart_id.number()) as _) }
3939
}
4040

4141
/// Returns the `MTIMECMP` register for the current HART.

riscv-peripheral/src/aclint/sswi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl SSWI {
6464
#[inline]
6565
pub fn setssip<H: HartIdNumber>(&self, hart_id: H) -> SETSSIP {
6666
// SAFETY: `hart_id` is valid for the target
67-
unsafe { SETSSIP::new(self.setssip0.get_ptr().offset(hart_id.number() as _) as _) }
67+
unsafe { SETSSIP::new(self.setssip0.get_ptr().add(hart_id.number()) as _) }
6868
}
6969
}
7070

riscv-peripheral/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
///
3838
/// // Implement `HartIdNumber` for `HartId`
3939
/// unsafe impl riscv_peripheral::aclint::HartIdNumber for HartId {
40-
/// const MAX_HART_ID_NUMBER: u16 = 2;
41-
/// fn number(self) -> u16 { self as _ }
42-
/// fn from_number(number: u16) -> Result<Self> {
40+
/// const MAX_HART_ID_NUMBER: usize = Self::H2 as usize;
41+
/// fn number(self) -> usize { self as _ }
42+
/// fn from_number(number: usize) -> Result<Self> {
4343
/// match number {
4444
/// 0 => Ok(HartId::H0),
4545
/// 1 => Ok(HartId::H1),
4646
/// 2 => Ok(HartId::H2),
47-
/// _ => Err(Error::InvalidVariant(number as _)),
47+
/// _ => Err(Error::InvalidVariant(number)),
4848
/// }
4949
/// }
5050
/// }

riscv-peripheral/src/plic.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<P: Plic> PLIC<P> {
6464
#[inline]
6565
pub fn ctx<H: HartIdNumber>(hart_id: H) -> CTX<P> {
6666
// SAFETY: valid context number
67-
unsafe { CTX::new(hart_id.number()) }
67+
unsafe { CTX::new(hart_id.number() as _) }
6868
}
6969

7070
/// Returns the PLIC HART context for the current HART.
@@ -149,7 +149,6 @@ pub(crate) mod test {
149149
use riscv_pac::{ExternalInterruptNumber, HartIdNumber, InterruptNumber, PriorityNumber};
150150

151151
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
152-
#[repr(usize)]
153152
pub(crate) enum Interrupt {
154153
I1 = 1,
155154
I2 = 2,
@@ -158,7 +157,6 @@ pub(crate) mod test {
158157
}
159158

160159
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
161-
#[repr(u8)]
162160
pub(crate) enum Priority {
163161
P0 = 0,
164162
P1 = 1,
@@ -167,15 +165,14 @@ pub(crate) mod test {
167165
}
168166

169167
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
170-
#[repr(u16)]
171168
pub(crate) enum Context {
172169
C0 = 0,
173170
C1 = 1,
174171
C2 = 2,
175172
}
176173

177174
unsafe impl InterruptNumber for Interrupt {
178-
const MAX_INTERRUPT_NUMBER: usize = 4;
175+
const MAX_INTERRUPT_NUMBER: usize = Self::I4 as usize;
179176

180177
#[inline]
181178
fn number(self) -> usize {
@@ -197,40 +194,40 @@ pub(crate) mod test {
197194
unsafe impl ExternalInterruptNumber for Interrupt {}
198195

199196
unsafe impl PriorityNumber for Priority {
200-
const MAX_PRIORITY_NUMBER: u8 = 3;
197+
const MAX_PRIORITY_NUMBER: usize = Self::P3 as usize;
201198

202199
#[inline]
203-
fn number(self) -> u8 {
200+
fn number(self) -> usize {
204201
self as _
205202
}
206203

207204
#[inline]
208-
fn from_number(number: u8) -> Result<Self> {
205+
fn from_number(number: usize) -> Result<Self> {
209206
match number {
210207
0 => Ok(Priority::P0),
211208
1 => Ok(Priority::P1),
212209
2 => Ok(Priority::P2),
213210
3 => Ok(Priority::P3),
214-
_ => Err(Error::InvalidVariant(number as usize)),
211+
_ => Err(Error::InvalidVariant(number)),
215212
}
216213
}
217214
}
218215

219216
unsafe impl HartIdNumber for Context {
220-
const MAX_HART_ID_NUMBER: u16 = 2;
217+
const MAX_HART_ID_NUMBER: usize = Self::C2 as usize;
221218

222219
#[inline]
223-
fn number(self) -> u16 {
220+
fn number(self) -> usize {
224221
self as _
225222
}
226223

227224
#[inline]
228-
fn from_number(number: u16) -> Result<Self> {
225+
fn from_number(number: usize) -> Result<Self> {
229226
match number {
230227
0 => Ok(Context::C0),
231228
1 => Ok(Context::C1),
232229
2 => Ok(Context::C2),
233-
_ => Err(Error::InvalidVariant(number as usize)),
230+
_ => Err(Error::InvalidVariant(number)),
234231
}
235232
}
236233
}

riscv-rt/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Add integration tests to check that macros work as expected.
1213
- Add `no-exceptions` feature to opt-out the default implementation of `_dispatch_exception`
1314
- Add `no-interrupts` feature to opt-out the default implementation of `_dispatch_core_interrupt`
1415
- Add `pre_init_trap` to detect early errors during the boot process.
1516
- Add `v-trap` feature to enable interrupt handling in vectored mode.
1617
- Add `core_interrupt` proc macro to help defining core interrupt handlers.
18+
If `v-trap` feature is enabled, this macro also generates its corresponding trap.
1719
- Add `external_interrupt` proc macro to help defining external interrupt handlers.
1820
- Add `exception` proc macro to help defining exception handlers.
19-
If `v-trap` feature is enabled, this macro also generates its corresponding trap.
2021

2122
### Changed
2223

24+
- Use `cfg_attr` in `start_trap_rust` to allow compilation in non-riscv targets.
2325
- Moved all the assembly code to `asm.rs`
2426
- Use `weak` symbols for functions such as `_mp_hook` or `_start_trap`
2527
- `abort` is now `weak`, so it is possible to link third-party libraries including this symbol.

0 commit comments

Comments
 (0)