Skip to content

Commit

Permalink
Add internal_pull_down() method
Browse files Browse the repository at this point in the history
For symmetry with internal_pull_up().
  • Loading branch information
Windfisch committed Dec 19, 2021
1 parent a817b40 commit 465d377
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Implementation of RTIC Monotonic for TIM2 & TIM5 under `rtic` feature [#380] [#390]
- `IoPin` for `Output<OpenDrain>> <-> Input<Floating>>` [#374]
- `IoPin` for `Output<PushPull>> <-> Input<PullUp>> and Input<PullDown>>` [#389]
- Add `internal_pull_down` to `Pin<Output<OpenDrain>>` and `Pin<Alternate<PushPull>>` for symmetry
with `internal_pull_up` [#399]

[#390]: https://github.com/stm32-rs/stm32f4xx-hal/pull/390
[#382]: https://github.com/stm32-rs/stm32f4xx-hal/pull/382
Expand All @@ -32,6 +34,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [breaking-change] Bump `stm32f4` to 0.14. Update RTIC based examples to use `rtic` 0.6 [#367]
- [breaking-change] Bump `bxcan` to 0.6 [#371]
- fix #362: ADC voltage conversion might be incorrect [#397]
- [breaking-change] Change `Pin<Output<OpenDrain>>::internal_pull_up` signature from `(&mut self, _: bool) -> ()`
to `(self, _: bool) -> Self`. [#399]

[#367]: https://github.com/stm32-rs/stm32f4xx-hal/pull/397
[#367]: https://github.com/stm32-rs/stm32f4xx-hal/pull/367
Expand Down
30 changes: 29 additions & 1 deletion src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,29 @@ impl<MODE, const P: char, const N: u8> Pin<Output<MODE>, P, N> {

impl<const P: char, const N: u8> Pin<Output<OpenDrain>, P, N> {
/// Enables / disables the internal pull up
pub fn internal_pull_up(&mut self, on: bool) {
pub fn internal_pull_up(self, on: bool) -> Self {
let offset = 2 * { N };
let value = if on { 0b01 } else { 0b00 };
unsafe {
(*Gpio::<P>::ptr())
.pupdr
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (value << offset)))
};

self
}

/// Enables / disables the internal pull down
pub fn internal_pull_down(self, on: bool) -> Self {
let offset = 2 * { N };
let value = if on { 0b10 } else { 0b00 };
unsafe {
(*Gpio::<P>::ptr())
.pupdr
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (value << offset)))
};

self
}
}

Expand Down Expand Up @@ -337,6 +352,19 @@ impl<const P: char, const N: u8, const A: u8> Pin<Alternate<PushPull, A>, P, N>

self
}

/// Enables / disables the internal pull down
pub fn internal_pull_down(self, on: bool) -> Self {
let offset = 2 * { N };
let value = if on { 0b10 } else { 0b00 };
unsafe {
(*Gpio::<P>::ptr())
.pupdr
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (value << offset)))
};

self
}
}

impl<const P: char, const N: u8, const A: u8> Pin<Alternate<PushPull, A>, P, N> {
Expand Down

0 comments on commit 465d377

Please sign in to comment.