Skip to content

Commit 09d5e54

Browse files
committed
adapt to master
1 parent 4b800a2 commit 09d5e54

27 files changed

+232
-282
lines changed

riscv-pac/Cargo.toml

-9
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,3 @@ targets = [
1717
"riscv32i-unknown-none-elf", "riscv32imc-unknown-none-elf", "riscv32imac-unknown-none-elf",
1818
"riscv64imac-unknown-none-elf", "riscv64gc-unknown-none-elf",
1919
]
20-
21-
[dependencies]
22-
riscv-pac-macros = { path = "macros", version = "0.1.0", optional = true }
23-
24-
[features]
25-
default = ["riscv-pac-macros"]
26-
27-
[dev-dependencies]
28-
trybuild = "1.0"

riscv-pac/src/lib.rs

+47-55
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ pub mod result;
44

55
use result::Result;
66

7-
#[cfg(feature = "riscv-pac-macros")]
8-
pub use riscv_pac_macros::*;
9-
107
/// Trait for enums of target-specific exception numbers.
118
///
129
/// This trait should be implemented by a peripheral access crate (PAC) on its enum of available
@@ -136,75 +133,69 @@ pub unsafe trait HartIdNumber: Copy {
136133
#[cfg(test)]
137134
mod test {
138135
use super::*;
136+
use crate::result::Error;
139137

140-
#[derive(Clone, Copy, Debug, Eq, PartialEq, ExceptionNumber)]
141-
#[repr(u16)]
138+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
142139
enum Exception {
143140
E1 = 1,
144141
E3 = 3,
145142
}
146143

147-
#[derive(Clone, Copy, Debug, Eq, PartialEq, InterruptNumber)]
148-
#[repr(u16)]
144+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
149145
enum Interrupt {
150146
I1 = 1,
151147
I2 = 2,
152148
I4 = 4,
153149
}
154150

155-
#[derive(Clone, Copy, Debug, Eq, PartialEq, PriorityNumber)]
156-
#[repr(u8)]
151+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
157152
enum Priority {
158153
P0 = 0,
159154
P1 = 1,
160155
P2 = 2,
161156
P3 = 3,
162157
}
163158

164-
#[derive(Clone, Copy, Debug, Eq, PartialEq, HartIdNumber)]
165-
#[repr(u16)]
166-
enum Context {
167-
C0 = 0,
168-
C1 = 1,
169-
C2 = 2,
159+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
160+
enum HartId {
161+
H0 = 0,
162+
H1 = 1,
163+
H2 = 2,
170164
}
171165

172166
unsafe impl ExceptionNumber for Exception {
173-
const MAX_EXCEPTION_NUMBER: u16 = Self::E3 as u16;
167+
const MAX_EXCEPTION_NUMBER: usize = Self::E3 as usize;
174168

175169
#[inline]
176-
fn number(self) -> u16 {
170+
fn number(self) -> usize {
177171
self as _
178172
}
179173

180174
#[inline]
181-
fn from_number(number: u16) -> Result<Self> {
182-
if number > Self::MAX_EXCEPTION_NUMBER || number == 0 {
183-
Err(number)
184-
} else if number == 1 || number == 3 {
185-
// SAFETY: valid exception number
186-
Ok(unsafe { core::mem::transmute(number) })
187-
} else {
188-
Err(number)
175+
fn from_number(number: usize) -> Result<Self> {
176+
match number {
177+
1 => Ok(Exception::E1),
178+
3 => Ok(Exception::E3),
179+
_ => Err(Error::InvalidVariant(number)),
189180
}
190181
}
191182
}
192183

193184
unsafe impl InterruptNumber for Interrupt {
194-
const MAX_INTERRUPT_NUMBER: u16 = Self::I4 as u16;
185+
const MAX_INTERRUPT_NUMBER: usize = Self::I4 as usize;
195186

196187
#[inline]
197-
fn number(self) -> u16 {
188+
fn number(self) -> usize {
198189
self as _
199190
}
200191

201192
#[inline]
202-
fn from_number(number: u16) -> Result<Self> {
203-
if number > Self::MAX_INTERRUPT_NUMBER || number == 0 {
204-
Err(number)
205-
} else {
206-
// SAFETY: valid interrupt number
207-
Ok(unsafe { core::mem::transmute(number) })
193+
fn from_number(number: usize) -> Result<Self> {
194+
match number {
195+
1 => Ok(Interrupt::I1),
196+
2 => Ok(Interrupt::I2),
197+
4 => Ok(Interrupt::I4),
198+
_ => Err(Error::InvalidVariant(number)),
208199
}
209200
}
210201
}
@@ -218,31 +209,32 @@ mod test {
218209
}
219210

220211
#[inline]
221-
fn from_number(number: u8) -> Result<Self, u8> {
222-
if number > Self::MAX_PRIORITY_NUMBER {
223-
Err(number)
224-
} else {
225-
// SAFETY: valid priority number
226-
Ok(unsafe { core::mem::transmute(number) })
212+
fn from_number(number: u8) -> Result<Self> {
213+
match number {
214+
0 => Ok(Priority::P0),
215+
1 => Ok(Priority::P1),
216+
2 => Ok(Priority::P2),
217+
3 => Ok(Priority::P3),
218+
_ => Err(Error::InvalidVariant(number as _)),
227219
}
228220
}
229221
}
230222

231-
unsafe impl HartIdNumber for Context {
232-
const MAX_HART_ID_NUMBER: u16 = Self::C2 as u16;
223+
unsafe impl HartIdNumber for HartId {
224+
const MAX_HART_ID_NUMBER: u16 = Self::H2 as u16;
233225

234226
#[inline]
235227
fn number(self) -> u16 {
236228
self as _
237229
}
238230

239231
#[inline]
240-
fn from_number(number: u16) -> Result<Self, u16> {
241-
if number > Self::MAX_HART_ID_NUMBER {
242-
Err(number)
243-
} else {
244-
// SAFETY: valid context number
245-
Ok(unsafe { core::mem::transmute(number) })
232+
fn from_number(number: u16) -> Result<Self> {
233+
match number {
234+
0 => Ok(HartId::H0),
235+
1 => Ok(HartId::H1),
236+
2 => Ok(HartId::H2),
237+
_ => Err(Error::InvalidVariant(number as _)),
246238
}
247239
}
248240
}
@@ -252,11 +244,11 @@ mod test {
252244
assert_eq!(Exception::E1.number(), 1);
253245
assert_eq!(Exception::E3.number(), 3);
254246

255-
assert_eq!(Exception::from_number(0), Err(0));
247+
assert_eq!(Exception::from_number(0), Err(Error::InvalidVariant(0)));
256248
assert_eq!(Exception::from_number(1), Ok(Exception::E1));
257-
assert_eq!(Exception::from_number(2), Err(2));
249+
assert_eq!(Exception::from_number(2), Err(Error::InvalidVariant(2)));
258250
assert_eq!(Exception::from_number(3), Ok(Exception::E3));
259-
assert_eq!(Exception::from_number(4), Err(4));
251+
assert_eq!(Exception::from_number(4), Err(Error::InvalidVariant(4)));
260252
}
261253

262254
#[test]
@@ -265,12 +257,12 @@ mod test {
265257
assert_eq!(Interrupt::I2.number(), 2);
266258
assert_eq!(Interrupt::I4.number(), 4);
267259

268-
assert_eq!(Interrupt::from_number(0), Err(0));
260+
assert_eq!(Interrupt::from_number(0), Err(Error::InvalidVariant(0)));
269261
assert_eq!(Interrupt::from_number(1), Ok(Interrupt::I1));
270262
assert_eq!(Interrupt::from_number(2), Ok(Interrupt::I2));
271-
assert_eq!(Interrupt::from_number(3), Err(3));
263+
assert_eq!(Interrupt::from_number(3), Err(Error::InvalidVariant(3)));
272264
assert_eq!(Interrupt::from_number(4), Ok(Interrupt::I4));
273-
assert_eq!(Interrupt::from_number(5), Err(5));
265+
assert_eq!(Interrupt::from_number(5), Err(Error::InvalidVariant(5)));
274266
}
275267

276268
#[test]
@@ -284,7 +276,7 @@ mod test {
284276
assert_eq!(Priority::from_number(1), Ok(Priority::P1));
285277
assert_eq!(Priority::from_number(2), Ok(Priority::P2));
286278
assert_eq!(Priority::from_number(3), Ok(Priority::P3));
287-
assert_eq!(Priority::from_number(4), Err(4));
279+
assert_eq!(Priority::from_number(4), Err(Error::InvalidVariant(4)));
288280
}
289281

290282
#[test]
@@ -296,6 +288,6 @@ mod test {
296288
assert_eq!(HartId::from_number(0), Ok(HartId::H0));
297289
assert_eq!(HartId::from_number(1), Ok(HartId::H1));
298290
assert_eq!(HartId::from_number(2), Ok(HartId::H2));
299-
assert_eq!(HartId::from_number(3), Err(3));
291+
assert_eq!(HartId::from_number(3), Err(Error::InvalidVariant(3)));
300292
}
301293
}

riscv-pac/tests/ui/fail_empty_macro.stderr

-7
This file was deleted.

riscv-pac/tests/ui/fail_no_unsafe.stderr

-5
This file was deleted.

riscv-peripheral/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ license = "ISC"
1616
[dependencies]
1717
embedded-hal = "1.0.0"
1818
embedded-hal-async = { version = "1.0.0", optional = true }
19-
riscv = { path = "../riscv", version = "0.11.2" }
20-
riscv-pac = { path = "../riscv-pac", version = "0.1.2", default-features = false }
19+
riscv = { path = "../riscv", version = "0.11.2", default-features = false }
20+
riscv-pac = { path = "../riscv-pac", version = "0.1.2" }
2121

2222
[dev-dependencies]
2323
heapless = "0.8.0"
24-
riscv-pac = { path = "../riscv-pac", version = "0.1.2", default-features = true }
2524

2625
[features]
2726
aclint-hal-async = ["embedded-hal-async"]

riscv-peripheral/examples/e310x.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//! This is a simple example of how to use the `riscv-peripheral` crate to generate
33
//! peripheral definitions for a target.
44
5-
use riscv_pac::pac_enum;
6-
use riscv_pac::result::{Error, Result};
5+
use riscv_pac::{
6+
result::{Error, Result},
7+
ExternalInterruptNumber, HartIdNumber, InterruptNumber, PriorityNumber,
8+
};
79

8-
#[repr(u16)]
9-
#[pac_enum(unsafe HartIdNumber)]
1010
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1111
pub enum HartId {
1212
H0 = 0,
@@ -22,11 +22,9 @@ unsafe impl HartIdNumber for HartId {
2222

2323
#[inline]
2424
fn from_number(number: u16) -> Result<Self> {
25-
if number > Self::MAX_HART_ID_NUMBER {
26-
Err(Error::InvalidVariant(number as usize))
27-
} else {
28-
// SAFETY: valid context number
29-
Ok(unsafe { core::mem::transmute(number) })
25+
match number {
26+
0 => Ok(HartId::H0),
27+
_ => Err(Error::InvalidVariant(number as usize)),
3028
}
3129
}
3230
}
@@ -88,28 +86,26 @@ pub enum Interrupt {
8886
}
8987

9088
unsafe impl InterruptNumber for Interrupt {
91-
const MAX_INTERRUPT_NUMBER: u16 = Self::I2C0 as u16;
89+
const MAX_INTERRUPT_NUMBER: usize = Self::I2C0 as usize;
9290

9391
#[inline]
94-
fn number(self) -> u16 {
92+
fn number(self) -> usize {
9593
self as _
9694
}
9795

9896
#[inline]
99-
fn from_number(number: u16) -> Result<Self> {
97+
fn from_number(number: usize) -> Result<Self> {
10098
if number == 0 || number > Self::MAX_INTERRUPT_NUMBER {
10199
Err(Error::InvalidVariant(number as usize))
102100
} else {
103101
// SAFETY: valid interrupt number
104-
Ok(unsafe { core::mem::transmute(number) })
102+
Ok(unsafe { core::mem::transmute(number as u8) })
105103
}
106104
}
107105
}
108106

109107
unsafe impl ExternalInterruptNumber for Interrupt {}
110108

111-
#[repr(u8)]
112-
#[pac_enum(unsafe PriorityNumber)]
113109
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
114110
pub enum Priority {
115111
P0 = 0,

riscv-peripheral/src/aclint/mswi.rs

-13
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,6 @@ impl MSWI {
3535
// SAFETY: `hart_id` is valid for the target
3636
unsafe { MSIP::new(self.msip0.get_ptr().offset(hart_id.number() as _) as _) }
3737
}
38-
39-
/// Returns the `MSIP` register for the current HART.
40-
///
41-
/// # Note
42-
///
43-
/// This function determines the current HART ID by reading the [`riscv::register::mhartid`] CSR.
44-
/// Thus, it can only be used in M-mode. For S-mode, use [`MSWI::msip`] instead.
45-
#[inline]
46-
pub fn msip_mhartid(&self) -> MSIP {
47-
let hart_id = riscv::register::mhartid::read();
48-
// SAFETY: `hart_id` is valid for the target and is the current hart
49-
unsafe { MSIP::new(self.msip0.get_ptr().add(hart_id) as _) }
50-
}
5138
}
5239

5340
unsafe_peripheral!(MSIP, u32, RW);

riscv-peripheral/src/aclint/mtimer.rs

-13
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,6 @@ impl MTIMER {
3737
// SAFETY: `hart_id` is valid for the target
3838
unsafe { MTIMECMP::new(self.mtimecmp0.get_ptr().offset(hart_id.number() as _) as _) }
3939
}
40-
41-
/// Returns the `MTIMECMP` register for the current HART.
42-
///
43-
/// # Note
44-
///
45-
/// This function determines the current HART ID by reading the [`riscv::register::mhartid`] CSR.
46-
/// Thus, it can only be used in M-mode. For S-mode, use [`MTIMER::mtimecmp`] instead.
47-
#[inline]
48-
pub fn mtimecmp_mhartid(&self) -> MTIMECMP {
49-
let hart_id = riscv::register::mhartid::read();
50-
// SAFETY: `hart_id` is valid for the target and is the current hart
51-
unsafe { MTIMECMP::new(self.mtimecmp0.get_ptr().add(hart_id) as _) }
52-
}
5340
}
5441

5542
// MTIMECMP register.

riscv-peripheral/src/aclint/sswi.rs

-31
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,6 @@ impl SSWI {
2525
}
2626
}
2727

28-
/// Returns `true` if a supervisor software interrupt is pending.
29-
#[inline]
30-
pub fn is_interrupting() -> bool {
31-
riscv::register::sip::read().ssoft()
32-
}
33-
34-
/// Returns `true` if Supervisor Software Interrupts are enabled.
35-
#[inline]
36-
pub fn is_enabled() -> bool {
37-
riscv::register::mie::read().ssoft()
38-
}
39-
40-
/// Sets the Supervisor Software Interrupt bit of the `mie` CSR.
41-
/// This bit must be set for the `SSWI` to trigger supervisor software interrupts.
42-
///
43-
/// # Safety
44-
///
45-
/// Enabling the `SSWI` may break mask-based critical sections.
46-
#[inline]
47-
pub unsafe fn enable() {
48-
riscv::register::mie::set_ssoft();
49-
}
50-
51-
/// Clears the Supervisor Software Interrupt bit of the `mie` CSR.
52-
/// When cleared, the `SSWI` cannot trigger supervisor software interrupts.
53-
#[inline]
54-
pub fn disable() {
55-
// SAFETY: it is safe to disable interrupts
56-
unsafe { riscv::register::mie::clear_ssoft() };
57-
}
58-
5928
/// Returns the `SETSSIP` register for the HART which ID is `hart_id`.
6029
///
6130
/// # Note

riscv-peripheral/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![no_std]
1010

1111
pub use riscv; // re-export riscv crate to allow macros to use it
12+
pub use riscv_pac::result; // re-export the result module
1213

1314
pub mod common; // common definitions for all peripherals
1415
pub mod hal; // trait implementations for embedded-hal

0 commit comments

Comments
 (0)