Skip to content

Commit 6112ab8

Browse files
committed
Generate interrupt dispatch code.
Useful for architectures that prefer dispatching interrupts in software.
1 parent 20249ca commit 6112ab8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/generate/device.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn render(d: &Device, target: &Target) -> Result<Vec<Tokens>> {
4242
#![deny(warnings)]
4343
#![allow(non_camel_case_types)]
4444
#![feature(const_fn)]
45+
#![feature(try_from)]
4546
#![no_std]
4647
});
4748

src/generate/interrupt.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R
2121
interrupts.sort_by_key(|i| i.value);
2222

2323
let mut arms = vec![];
24+
let mut from_arms = vec![];
2425
let mut elements = vec![];
2526
let mut names = vec![];
2627
let mut variants = vec![];
@@ -60,6 +61,10 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R
6061
Interrupt::#name_uc => #value,
6162
});
6263

64+
from_arms.push(quote! {
65+
#value => Ok(Interrupt::#name_uc),
66+
});
67+
6368
elements.push(quote!(Some(#name_uc)));
6469
names.push(name_uc);
6570
}
@@ -187,6 +192,23 @@ pub fn render(device: &Device, target: &Target, peripherals: &[Peripheral]) -> R
187192
}
188193
}
189194
}
195+
196+
use core::convert::TryFrom;
197+
198+
#[derive(Debug, Copy, Clone)]
199+
pub struct TryFromInterruptError(());
200+
201+
impl TryFrom<u8> for Interrupt {
202+
type Error = TryFromInterruptError;
203+
204+
#[inline]
205+
fn try_from(value: u8) -> Result<Self, Self::Error> {
206+
match value {
207+
#(#from_arms)*
208+
_ => Err(TryFromInterruptError(())),
209+
}
210+
}
211+
}
190212
});
191213

192214
if *target != Target::None {

0 commit comments

Comments
 (0)