@@ -4,6 +4,7 @@ use anyhow::Result;
44use proc_macro2:: { Ident , Span , TokenStream } ;
55use quote:: quote;
66
7+ use crate :: generate:: Target ;
78use crate :: ir:: * ;
89use crate :: util:: { self , StringExt } ;
910
@@ -22,6 +23,7 @@ pub fn render(opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<
2223 let span = Span :: call_site ( ) ;
2324
2425 let mut interrupts = TokenStream :: new ( ) ;
26+ let mut interrupt_match = TokenStream :: new ( ) ;
2527 let mut peripherals = TokenStream :: new ( ) ;
2628 let mut vectors = TokenStream :: new ( ) ;
2729 let mut names = vec ! [ ] ;
@@ -52,10 +54,14 @@ pub fn render(opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<
5254 #[ doc = #description]
5355 #name_uc = #value,
5456 } ) ;
57+ interrupt_match. extend ( quote ! ( #value => Ok ( Interrupt :: #name_uc) , ) ) ;
58+
5559 vectors. extend ( quote ! ( Vector { _handler: #name_uc } , ) ) ;
5660 names. push ( name_uc) ;
5761 }
5862
63+ let max_interrupt_number = util:: unsuffixed ( ( pos - 1 ) as u64 ) ;
64+
5965 for p in sorted ( & d. peripherals , |p| p. base_address ) {
6066 let name = Ident :: new ( & p. name , span) ;
6167 let address = util:: hex_usize ( p. base_address ) ;
@@ -90,14 +96,43 @@ pub fn render(opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<
9096 pub enum Interrupt {
9197 #interrupts
9298 }
99+ ) ) ;
93100
94- unsafe impl cortex_m:: interrupt:: InterruptNumber for Interrupt {
95- #[ inline( always) ]
96- fn number( self ) -> u16 {
97- self as u16
98- }
101+ match opts. target {
102+ Target :: Riscv => {
103+ out. extend ( quote ! (
104+ unsafe impl riscv:: InterruptNumber for Interrupt {
105+ /// Returns the number of the interrupt
106+ #[ inline( always) ]
107+ fn number( self ) -> usize {
108+ self as usize
109+ }
110+
111+ fn from_number( number: usize ) -> riscv:: result:: Result <Self > {
112+ match number {
113+ #interrupt_match
114+ _ => Err ( riscv:: result:: Error :: InvalidVariant ( number) ) ,
115+ }
116+ }
117+
118+ const MAX_INTERRUPT_NUMBER : usize = #max_interrupt_number;
119+ }
120+ ) ) ;
99121 }
122+ Target :: CortexM => {
123+ out. extend ( quote ! (
124+ unsafe impl cortex_m:: interrupt:: InterruptNumber for Interrupt {
125+ /// Returns the number of the interrupt
126+ #[ inline( always) ]
127+ fn number( self ) -> u16 {
128+ self as u16
129+ }
130+ }
131+ ) ) ;
132+ }
133+ }
100134
135+ out. extend ( quote ! (
101136 #[ cfg( feature = "rt" ) ]
102137 mod _vectors {
103138 extern "C" {
@@ -128,12 +163,23 @@ pub fn render(opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<
128163 } ) ;
129164 }
130165
131- out. extend ( quote ! {
132- #[ cfg( feature = "rt" ) ]
133- pub use cortex_m_rt:: interrupt;
134- #[ cfg( feature = "rt" ) ]
135- pub use Interrupt as interrupt;
136- } ) ;
166+ match opts. target {
167+ Target :: CortexM => {
168+ out. extend ( quote ! {
169+ #[ cfg( feature = "rt" ) ]
170+ pub use cortex_m_rt:: interrupt;
171+ #[ cfg( feature = "rt" ) ]
172+ pub use Interrupt as interrupt;
173+ } ) ;
174+ }
175+ Target :: Riscv => {
176+ // TODO: Do we need to export something from riscv_rt here?
177+ out. extend ( quote ! {
178+ #[ cfg( feature = "rt" ) ]
179+ pub use Interrupt as interrupt;
180+ } ) ;
181+ }
182+ }
137183
138184 Ok ( out)
139185}
0 commit comments