@@ -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 ) ;
@@ -86,14 +92,43 @@ pub fn render(opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<
8692 pub enum Interrupt {
8793 #interrupts
8894 }
95+ ) ) ;
8996
90- unsafe impl cortex_m:: interrupt:: InterruptNumber for Interrupt {
91- #[ inline( always) ]
92- fn number( self ) -> u16 {
93- self as u16
94- }
97+ match opts. target {
98+ Target :: Riscv => {
99+ out. extend ( quote ! (
100+ unsafe impl riscv:: InterruptNumber for Interrupt {
101+ /// Returns the number of the interrupt
102+ #[ inline( always) ]
103+ fn number( self ) -> usize {
104+ self as usize
105+ }
106+
107+ fn from_number( number: usize ) -> riscv:: result:: Result <Self > {
108+ match number {
109+ #interrupt_match
110+ _ => Err ( riscv:: result:: Error :: InvalidVariant ( number) ) ,
111+ }
112+ }
113+
114+ const MAX_INTERRUPT_NUMBER : usize = #max_interrupt_number;
115+ }
116+ ) ) ;
95117 }
118+ Target :: CortexM => {
119+ out. extend ( quote ! (
120+ unsafe impl cortex_m:: interrupt:: InterruptNumber for Interrupt {
121+ /// Returns the number of the interrupt
122+ #[ inline( always) ]
123+ fn number( self ) -> u16 {
124+ self as u16
125+ }
126+ }
127+ ) ) ;
128+ }
129+ }
96130
131+ out. extend ( quote ! (
97132 #[ cfg( feature = "rt" ) ]
98133 mod _vectors {
99134 unsafe extern "C" {
@@ -124,12 +159,23 @@ pub fn render(opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<
124159 } ) ;
125160 }
126161
127- out. extend ( quote ! {
128- #[ cfg( feature = "rt" ) ]
129- pub use cortex_m_rt:: interrupt;
130- #[ cfg( feature = "rt" ) ]
131- pub use Interrupt as interrupt;
132- } ) ;
162+ match opts. target {
163+ Target :: CortexM => {
164+ out. extend ( quote ! {
165+ #[ cfg( feature = "rt" ) ]
166+ pub use cortex_m_rt:: interrupt;
167+ #[ cfg( feature = "rt" ) ]
168+ pub use Interrupt as interrupt;
169+ } ) ;
170+ }
171+ Target :: Riscv => {
172+ // TODO: Do we need to export something from riscv_rt here?
173+ out. extend ( quote ! {
174+ #[ cfg( feature = "rt" ) ]
175+ pub use Interrupt as interrupt;
176+ } ) ;
177+ }
178+ }
133179
134180 Ok ( out)
135181}
0 commit comments