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:: { ExternalInterruptNumber , HartIdNumber , InterruptNumber , PriorityNumber } ;
5+ use riscv_pac:: { pac_enum , ExternalInterruptNumber } ;
66
77#[ repr( u16 ) ]
8+ #[ pac_enum( unsafe HartIdNumber ) ]
89#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
910pub enum HartId {
1011 H0 = 0 ,
1112}
1213
13- unsafe impl HartIdNumber for HartId {
14- const MAX_HART_ID_NUMBER : u16 = Self :: H0 as u16 ;
15-
16- #[ inline]
17- fn number ( self ) -> u16 {
18- self as _
19- }
20-
21- #[ inline]
22- fn from_number ( number : u16 ) -> Result < Self , u16 > {
23- if number > Self :: MAX_HART_ID_NUMBER {
24- Err ( number)
25- } else {
26- // SAFETY: valid context number
27- Ok ( unsafe { core:: mem:: transmute ( number) } )
28- }
29- }
30- }
31-
32- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
3314#[ repr( u16 ) ]
15+ #[ pac_enum( unsafe InterruptNumber ) ]
16+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
3417pub enum Interrupt {
3518 WATCHDOG = 1 ,
3619 RTC = 2 ,
@@ -86,29 +69,11 @@ pub enum Interrupt {
8669 I2C0 = 52 ,
8770}
8871
89- unsafe impl InterruptNumber for Interrupt {
90- const MAX_INTERRUPT_NUMBER : u16 = Self :: I2C0 as u16 ;
91-
92- #[ inline]
93- fn number ( self ) -> u16 {
94- self as _
95- }
96-
97- #[ inline]
98- fn from_number ( number : u16 ) -> Result < Self , u16 > {
99- if number == 0 || number > Self :: MAX_INTERRUPT_NUMBER {
100- Err ( number)
101- } else {
102- // SAFETY: valid interrupt number
103- Ok ( unsafe { core:: mem:: transmute ( number) } )
104- }
105- }
106- }
107-
10872unsafe impl ExternalInterruptNumber for Interrupt { }
10973
110- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
11174#[ repr( u8 ) ]
75+ #[ pac_enum( unsafe PriorityNumber ) ]
76+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
11277pub enum Priority {
11378 P0 = 0 ,
11479 P1 = 1 ,
@@ -120,25 +85,6 @@ pub enum Priority {
12085 P7 = 7 ,
12186}
12287
123- unsafe impl PriorityNumber for Priority {
124- const MAX_PRIORITY_NUMBER : u8 = Self :: P7 as u8 ;
125-
126- #[ inline]
127- fn number ( self ) -> u8 {
128- self as _
129- }
130-
131- #[ inline]
132- fn from_number ( number : u8 ) -> Result < Self , u8 > {
133- if number > Self :: MAX_PRIORITY_NUMBER {
134- Err ( number)
135- } else {
136- // SAFETY: valid priority number
137- Ok ( unsafe { core:: mem:: transmute ( number) } )
138- }
139- }
140- }
141-
14288#[ cfg( feature = "aclint-hal-async" ) ]
14389riscv_peripheral:: clint_codegen!(
14490 base 0x0200_0000 ,
0 commit comments