-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime interrupt handler binding #1121
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,7 +225,7 @@ mod peripheral_macros { | |
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! peripherals { | ||
($($(#[$cfg:meta])? $name:ident <= $from_pac:tt),*$(,)?) => { | ||
($($(#[$cfg:meta])? $name:ident <= $from_pac:tt $(($($interrupt:ident),*))? ),*$(,)?) => { | ||
|
||
/// Contains the generated peripherals which implement [`Peripheral`] | ||
mod peripherals { | ||
|
@@ -282,6 +282,18 @@ mod peripheral_macros { | |
$( | ||
pub use peripherals::$name; | ||
)* | ||
|
||
$( | ||
$( | ||
$( | ||
paste::paste!{ | ||
pub fn [<bind_ $interrupt:lower _interrupt >](_peripheral: &mut peripherals::$name, handler: unsafe extern "C" fn() -> ()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I struggle to parse macros, so I might be missing something here, should this just be a method on the peripheral itself? E.g impl GPIO {
fn bind_interrupt(&mut self, handler: extern "C" fn()) {}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's brilliant! Much better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also cfg this method away if neither of the rt features are enabled too, that would be a fun one to debug if we didn't :D. |
||
unsafe { $crate::interrupt::bind_interrupt($crate::peripherals::Interrupt::$interrupt, handler); } | ||
} | ||
} | ||
)* | ||
)* | ||
)* | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -437,6 +437,66 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { | |
.into() | ||
} | ||
|
||
#[cfg(feature = "interrupt")] | ||
#[proc_macro_attribute] | ||
pub fn handler(args: TokenStream, input: TokenStream) -> TokenStream { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this macro? Won't the type system i.e the set_interrupt_handler function how handle ensuring that the type is of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is just sugar to hide the |
||
use proc_macro::Span; | ||
use proc_macro_error::abort; | ||
use syn::{parse::Error as ParseError, spanned::Spanned, ItemFn, ReturnType, Type}; | ||
|
||
use self::interrupt::{check_attr_whitelist, WhiteListCaller}; | ||
|
||
let mut f: ItemFn = syn::parse(input).expect("`#[handler]` must be applied to a function"); | ||
|
||
let attr_args = match NestedMeta::parse_meta_list(args.into()) { | ||
Ok(v) => v, | ||
Err(e) => { | ||
return TokenStream::from(darling::Error::from(e).write_errors()); | ||
} | ||
}; | ||
|
||
if attr_args.len() > 0 { | ||
abort!(Span::call_site(), "This attribute accepts no arguments") | ||
} | ||
|
||
// XXX should we blacklist other attributes? | ||
|
||
if let Err(error) = check_attr_whitelist(&f.attrs, WhiteListCaller::Interrupt) { | ||
return error; | ||
} | ||
|
||
let valid_signature = f.sig.constness.is_none() | ||
&& f.sig.abi.is_none() | ||
&& f.sig.generics.params.is_empty() | ||
&& f.sig.generics.where_clause.is_none() | ||
&& f.sig.variadic.is_none() | ||
&& match f.sig.output { | ||
ReturnType::Default => true, | ||
ReturnType::Type(_, ref ty) => match **ty { | ||
Type::Tuple(ref tuple) => tuple.elems.is_empty(), | ||
Type::Never(..) => true, | ||
_ => false, | ||
}, | ||
} | ||
&& f.sig.inputs.len() <= 1; | ||
|
||
if !valid_signature { | ||
return ParseError::new( | ||
f.span(), | ||
"`#[handler]` handlers must have signature `[unsafe] fn([&mut Context]) [-> !]`", | ||
) | ||
.to_compile_error() | ||
.into(); | ||
} | ||
|
||
f.sig.abi = syn::parse_quote!(extern "C"); | ||
|
||
quote!( | ||
#f | ||
) | ||
.into() | ||
} | ||
|
||
/// Create an enum for erased GPIO pins, using the enum-dispatch pattern | ||
/// | ||
/// Only used internally | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ async fn main(_spawner: Spawner) { | |
esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks), | ||
); | ||
|
||
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); | ||
let io = IO::new_async(peripherals.GPIO, peripherals.IO_MUX); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does creating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we just make the async handler accessible, and document then if you want to mix async pins and interrupt pins, you must make sure to call the async handler too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yes ... type state everywhere I like the idea of letting the user mix-and-match async and blocking - on the other hand forcing users to not misuse the API at compile time feels a bit cleaner. I wonder if the freedom of mixing async and blocking is something that people would like to have. In that case it could be nice if we could at least somehow check they are calling our async handler at runtime - no idea how to do that unfortunately There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, people have requested it a few times. I've been thinking some more about this a bit, I think we really want to avoid type states in the pins. I think we can figure something out with IO mux to always call the async handler after the user handler or something like that, but this would need some thorough documentation as it would probably be quite easy to break things. |
||
// GPIO 9 as input | ||
let mut input = io.pins.gpio9.into_pull_down_input(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this probably needs to be in a critical section? maybe all the bind functions do, but especially this one as we may get a race condition updating the handler in operation 🤔