Skip to content
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

Merged
merged 14 commits into from
Mar 27, 2021

Conversation

Piroro-hs
Copy link
Contributor

@Piroro-hs Piroro-hs commented Dec 24, 2020

@Piroro-hs Piroro-hs changed the title Collapse Input<Floating / PullUp / PullDown> into Input GPIO refactor (Simplify macro and reduce type states) Dec 25, 2020
@Sh3Rm4n
Copy link
Member

Sh3Rm4n commented Dec 25, 2020

Super interesting attempt. I'll take a closer look in the next few days. But this approach seems promising!

@Sh3Rm4n Sh3Rm4n self-requested a review December 25, 2020 19:26
Copy link
Member

@Sh3Rm4n Sh3Rm4n left a 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
Copy link
Member

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 :)

src/gpio.rs Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
@David-OConnor
Copy link
Contributor

David-OConnor commented Jan 6, 2021

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.

@Piroro-hs Piroro-hs force-pushed the gpio-collapse branch 2 times, most recently from 103e950 to 584f11d Compare February 13, 2021 13:52
@Piroro-hs
Copy link
Contributor Author

GPIO interrupts support added.
Referred to f1xx-hal, f4xx-hal, and #200.

@Piroro-hs Piroro-hs changed the title GPIO refactor (Simplify macro and reduce type states) GPIO refactor (Simplify macro, reduce type states, and support interrupt) Feb 13, 2021
@Piroro-hs
Copy link
Contributor Author

Clippy warnings should be fixed by #202

Piroro-hs added a commit to Piroro-hs/stm32f3xx-hal that referenced this pull request Feb 13, 2021
@Piroro-hs Piroro-hs marked this pull request as ready for review February 13, 2021 16:11
examples/gpio_interrupts.rs Outdated Show resolved Hide resolved
/// GPIO Register interface traits private to this module
mod private {
pub trait GpioRegExt {
fn is_low(&self, i: u8) -> bool;
Copy link
Contributor

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?

Copy link
Member

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.

src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
@Sh3Rm4n
Copy link
Member

Sh3Rm4n commented Mar 2, 2021

I'll take a closer look in the next few days. But this approach seems promising!

Next few months rather. I have this on my TODO list. I'm hopeful that I find time this week :)

@Sh3Rm4n Sh3Rm4n self-requested a review March 2, 2021 21:27
Copy link
Member

@Sh3Rm4n Sh3Rm4n left a 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 😆

// 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) };
Copy link
Member

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.

Copy link
Contributor Author

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;
Copy link
Member

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.

src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Show resolved Hide resolved
src/gpio.rs Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/serial.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
@Piroro-hs Piroro-hs force-pushed the gpio-collapse branch 3 times, most recently from cf00e5b to d5358fa Compare March 15, 2021 06:53
Copy link
Member

@Sh3Rm4n Sh3Rm4n left a 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 {
Copy link
Member

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.

@Piroro-hs
Copy link
Contributor Author

Updated (add comments and move IntoAf.. traits to marker module).
This is ready to merge :)

@Sh3Rm4n
Copy link
Member

Sh3Rm4n commented Mar 27, 2021

Nice. Let's merge it 🚂

Thanks for your hard work, again! :)

@Sh3Rm4n Sh3Rm4n merged commit 231c959 into stm32-rs:master Mar 27, 2021
@Piroro-hs Piroro-hs deleted the gpio-collapse branch March 30, 2021 04:55
@Sh3Rm4n Sh3Rm4n mentioned this pull request Jun 8, 2021
12 tasks
@Sh3Rm4n Sh3Rm4n mentioned this pull request Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

External interrupts (example, question)
4 participants