-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stm32xx-sys: configure EXTI IRQs in app.toml
This commit adds build-time code generation for the EXTI IRQ dispatch table. Now, an app.toml can define a section like this: ```toml [config.sys.gpio-irq.button] port = "C" pin = 13 owner = { name = "mytask", notification = "button" } ``` To assign PC13's EXTI IRQ to the `button` notification for task `mytask`. The dispatch table is now generated by `build/stm32xx-sys` and will include the EXTI interrupt configurations in the config.
- Loading branch information
Showing
7 changed files
with
223 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "build-stm32xx-sys" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
cfg-if = { workspace = true } | ||
convert_case = { workspace = true } | ||
proc-macro2 = { workspace = true } | ||
quote = { workspace = true } | ||
serde = { workspace = true } | ||
syn = { workspace = true } | ||
|
||
build-util = { path = "../util" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use quote::{quote, TokenStreamExt}; | ||
use serde::Deserialize; | ||
use std::collections::BTreeMap; | ||
|
||
/// This represents our _subset_ of global config and _must not_ be marked with | ||
/// `deny_unknown_fields`! | ||
#[derive(Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
struct GlobalConfig { | ||
sys: SysConfig, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "kebab-case", deny_unknown_fields)] | ||
pub struct SysConfig { | ||
/// EXTI interrupts | ||
gpio_irqs: BTreeMap<String, GpioIrqConfig>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "kebab-case", deny_unknown_fields)] | ||
struct GpioIrqConfig { | ||
port: ConfigPort, | ||
pin: u8, | ||
owner: GpioIrqOwner, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "kebab-case", deny_unknown_fields)] | ||
struct GpioIrqOwner { | ||
name: String, | ||
notification: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, Deserialize)] | ||
enum ConfigPort { | ||
A, | ||
B, | ||
C, | ||
D, | ||
E, | ||
F, | ||
G, | ||
H, | ||
I, | ||
J, | ||
K, | ||
} | ||
|
||
impl quote::ToTokens for ConfigPort { | ||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { | ||
use proc_macro2::{Ident, Punct, Spacing, Span}; | ||
// i don't like typing | ||
macro_rules! table { | ||
($($variant:ident),*) => { | ||
match self { | ||
$(ConfigPort::$variant => { | ||
tokens.append(Ident::new("Port", Span::call_site())); | ||
tokens.append(Punct::new(':', Spacing::Joint)); | ||
tokens.append(Punct::new(':', Spacing::Alone)); | ||
tokens.append(Ident::new(stringify!($variant), Span::call_site())); | ||
}),* | ||
} | ||
} | ||
} | ||
table! { A, B, C, D, E, F, G, H, I, J, K } | ||
} | ||
} | ||
|
||
impl SysConfig { | ||
pub fn load() -> anyhow::Result<Self> { | ||
Ok(build_util::config::<GlobalConfig>()?.sys) | ||
} | ||
|
||
pub fn needs_exti(&self) -> bool { | ||
!self.gpio_irqs.is_empty() | ||
} | ||
|
||
pub fn generate_exti_config( | ||
&self, | ||
) -> anyhow::Result<proc_macro2::TokenStream> { | ||
#[derive(Debug)] | ||
struct DispatchEntry<'a> { | ||
_name: &'a str, | ||
port: ConfigPort, | ||
task: syn::Ident, | ||
note: syn::Ident, | ||
} | ||
|
||
const NUM_EXTI_IRQS: usize = 16; | ||
let mut dispatch_table: [Option<DispatchEntry<'_>>; NUM_EXTI_IRQS] = | ||
Default::default(); | ||
|
||
for ( | ||
_name, | ||
&GpioIrqConfig { | ||
port, | ||
pin, | ||
ref owner, | ||
}, | ||
) in &self.gpio_irqs | ||
{ | ||
match dispatch_table.get_mut(pin as usize) { | ||
Some(Some(curr)) => { | ||
anyhow::bail!("pin {pin} is already mapped to IRQ {curr:?}") | ||
} | ||
Some(slot) => { | ||
let task = syn::parse_str(&owner.name)?; | ||
let note = quote::format_ident!( | ||
"{}_MASK", | ||
owner.notification.to_uppercase().replace('-', "_") | ||
); | ||
*slot = Some(DispatchEntry { | ||
_name, | ||
port, | ||
task, | ||
note, | ||
}) | ||
} | ||
None => anyhow::bail!( | ||
"GPIO IRQ pin numbers must be < {NUM_EXTI_IRQS}; {pin} is out of range" | ||
), | ||
} | ||
} | ||
|
||
let dispatches = dispatch_table.iter().map(|slot| match slot { | ||
Some(DispatchEntry { | ||
port, task, note, .. | ||
}) => quote! { | ||
Some(( | ||
#port, | ||
userlib::TaskId::for_index_and_gen( | ||
hubris_num_tasks::Task::#task as usize, | ||
userlib::Generation::ZERO, | ||
), | ||
crate::notifications::#task::#note, | ||
)) | ||
}, | ||
None => quote! { None }, | ||
}); | ||
|
||
Ok(quote! { | ||
pub(crate) const EXTI_DISPATCH_TABLE: [Option<(Port, TaskId, u32)>; #NUM_EXTI_IRQS] = [ | ||
#( #dispatches ),* | ||
]; | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters