-
Notifications
You must be signed in to change notification settings - Fork 64
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
WIP: add InputPin trait to output pins #88
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -546,26 +546,22 @@ macro_rules! gpio { | |||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/// Configures the pin to operate as an open drain output pin | ||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn into_open_drain_output( | ||||||||||||||||||||||||||||||||||||||||||||||||||
self, | ||||||||||||||||||||||||||||||||||||||||||||||||||
moder: &mut MODER, | ||||||||||||||||||||||||||||||||||||||||||||||||||
otyper: &mut OTYPER, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) -> $PXi<Output<OpenDrain>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
let offset = 2 * $i; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// general purpose output mode | ||||||||||||||||||||||||||||||||||||||||||||||||||
let mode = 0b01; | ||||||||||||||||||||||||||||||||||||||||||||||||||
moder.moder().modify(|r, w| unsafe { | ||||||||||||||||||||||||||||||||||||||||||||||||||
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn into_open_drain_output(self) -> $PXi<Output<OpenDrain>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
let offset = 2 * $i; | ||||||||||||||||||||||||||||||||||||||||||||||||||
unsafe { | ||||||||||||||||||||||||||||||||||||||||||||||||||
&(*$GPIOX::ptr()).pupdr.modify(|r, w| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
w.bits((r.bits() & !(0b11 << offset)) | (0b00 << offset)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
&(*$GPIOX::ptr()).otyper.modify(|r, w| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
w.bits(r.bits() | (0b1 << $i)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
&(*$GPIOX::ptr()).moder.modify(|r, w| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// open drain output | ||||||||||||||||||||||||||||||||||||||||||||||||||
otyper | ||||||||||||||||||||||||||||||||||||||||||||||||||
.otyper() | ||||||||||||||||||||||||||||||||||||||||||||||||||
.modify(|r, w| unsafe { w.bits(r.bits() | (0b1 << $i)) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
$PXi { _mode: PhantomData } | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
$PXi { _mode: PhantomData } | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/// Configures the pin to operate as an push pull output pin | ||||||||||||||||||||||||||||||||||||||||||||||||||
pub fn into_push_pull_output( | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -699,6 +695,19 @@ macro_rules! gpio { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
#[cfg(feature = "unproven")] | ||||||||||||||||||||||||||||||||||||||||||||||||||
impl<MODE> toggleable::Default for $PXi<Output<MODE>> {} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
impl<MODE> InputPin for $PXi<Output<MODE>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. Almost done, this also has to be implemented at line 231 for 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. Minor nitpick: I think I would move this below impl<MODE> OutputPin for $PXi<Output<MODE>> { at line 668, because this is another implementation for |
||||||||||||||||||||||||||||||||||||||||||||||||||
type Error = (); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
fn is_high(&self) -> Result<bool, Self::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
self.is_low().map(|v| !v) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
fn is_low(&self) -> Result<bool, Self::Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
// NOTE(unsafe) atomic read with no side effects | ||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
)+ | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+700
to
711
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.
Suggested change
Indentation is wrong. I wonder why rustfmt did not complaint 🤔 |
||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
)+ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
Breaking changes in itself are fine. But changing only this, would mean other parts which take the
moder
andotyper
register must be changed as well, to make it coherent.We had a discussion about this a while ago in #37. I've no opinion on this, but I'd rather tackle that as whole in another PR :)
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 agree, we should do this in a separate PR.
But also this implementation here is not safe. The reason we pass
&mut
references for the relevant GPIO registers in is that this way we can ensure (at compile time) that we have exclusive access to them. We need this, because otherwise race conditions can happen, as explained in #37. To make this safe we'd need something else that ensures exclusive access, like a critical section.