-
Notifications
You must be signed in to change notification settings - Fork 69
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
GPIO refactor (Simplify macro, reduce type states, and support interrupt) #189
Conversation
Super interesting attempt. I'll take a closer look in the next few days. But this approach seems promising! |
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.
Some thoughts which came into my mind.
I've only read a small part of src.gpio.rs
so far. Will look into this in more detail later.
/// Input mode (type state) | ||
pub struct Input<MODE> { | ||
_mode: PhantomData<MODE>, | ||
/// GPIO Register interface traits private to this module |
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.
Also known as sealed traits :)
Could you please add support for GPIO interrupts? I'm modding this now to do so. I made a standalone module a while back for this (In my PR), but am switching it to GPIO to be consistent with L4 and F4 HALs. API: let btn = gpioa
.pa15
.into_floating_input(&mut gpioa.moder, &mut gpioa.pupdr);
btn.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2)
btn.enable_interrupt(&mut dp.EXTI)
btn.trigger_on_edge(&mut dp.EXTI, Edge::FALLING); https://github.com/stm32-rs/stm32l4xx-hal/blob/master/src/gpio.rs#L129 In general, keep in mind that GPIO is very similar (identical?) across the STM32 line, so there's a lot to be learned from the other crates. Take the best from each. |
103e950
to
584f11d
Compare
GPIO interrupts support added. |
Clippy warnings should be fixed by #202 |
/// GPIO Register interface traits private to this module | ||
mod private { | ||
pub trait GpioRegExt { | ||
fn is_low(&self, i: u8) -> bool; |
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.
perhaps pin_num
would be a more informative variable name than i
?
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.
Though i
is / was pretty common to refer the pin number in this file, pin_num
does not hurt.
Next few months rather. I have this on my TODO list. I'm hopeful that I find time this week :) |
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.
Wow, this PR is huge. 👀 Both in code change, but also implementation wise.
Props to you, to unclutter the macro and leveraging the rust type-system and static polymorphism techniques, to not rely too heavily on macros to hold it all together.
Your extensive use of traits not only move most of the dispatch logic out of the macro, but actually makes it easier to read through the gpio implementation itself.
The interrupt implementation is a very welcomed additions as well.
Props to you and many thanks for this implementation. 🙏
I've added mostly nits as review comments, but design wise, this looks perfect. I'm not able to find any critiques, as I couldn't have done it any better than you. As if I could even do this myself 😆
examples/gpio_interrupts.rs
Outdated
// Moving ownership to the global BUTTON so we can clear the interrupt pending bit. | ||
cortex_m::interrupt::free(|cs| *BUTTON.borrow(cs).borrow_mut() = Some(user_button)); | ||
|
||
unsafe { NVIC::unmask(Interrupt::EXTI0) }; |
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.
Is there an easy way, that the user button can give us the according EXTI interrupt line, so a manual lookup is not necessary, or is it too complicated?
This could be done in another PR though. It's a nice to have, not necessary.
Note to self: Lookup documentation for interrupt methods, if the user gets a hint, that he has to unmask the interrupts before utilizing them.
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.
nvic(&self) -> pac::Interrupt
method is added.
/// GPIO Register interface traits private to this module | ||
mod private { | ||
pub trait GpioRegExt { | ||
fn is_low(&self, i: u8) -> bool; |
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.
Though i
is / was pretty common to refer the pin number in this file, pin_num
does not hurt.
fa42567
to
76d8213
Compare
cf00e5b
to
d5358fa
Compare
d5358fa
to
b2f2fb3
Compare
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.
Minor tiny nit.
Overall this looks fantastic. Let me know if you want me to press the merge button 😀
pub struct AFRH { | ||
_0: (), | ||
} | ||
macro_rules! r_trait { |
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 see how this is macro is useful, but a comment would help to understand how it works / what its for.
Updated (add comments and move |
Nice. Let's merge it 🚂 Thanks for your hard work, again! :) |
gpio.rs
uses more generics than macro nowAn attempt to address Collapse Input<OpenDrain / PullUp / PullDown) into Input #125
Close External interrupts (example, question) #83 and Interrupt support #112