Skip to content

Commit ac44f35

Browse files
committed
Pull resistors
1 parent 7241225 commit ac44f35

File tree

10 files changed

+144
-412
lines changed

10 files changed

+144
-412
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Changed
1111

1212
- CI updates + cache
13+
- Remove pull resistor from `Input` mode, use `Pull` enum instead
1314
- Move pin mode at the end of generics, add defaults for modes,
1415
bump MSRV to 1.59 [#418]
1516
- Move hd44780-driver to dev-dependencies

examples/analog-stopwatch-with-spi-ssd1306.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use panic_semihosting as _;
1111
use stm32f4xx_hal as hal;
1212

1313
use crate::hal::{
14-
gpio::{Edge, Input, PullDown, PA0},
14+
gpio::{Edge, Input, PA0},
1515
interrupt, pac,
1616
prelude::*,
1717
rcc::{Clocks, Rcc},
@@ -45,7 +45,7 @@ static ELAPSED_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
4545
static ELAPSED_RESET_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
4646
static TIMER_TIM2: Mutex<RefCell<Option<CounterUs<pac::TIM2>>>> = Mutex::new(RefCell::new(None));
4747
static STATE: Mutex<Cell<StopwatchState>> = Mutex::new(Cell::new(StopwatchState::Ready));
48-
static BUTTON: Mutex<RefCell<Option<PA0<Input<PullDown>>>>> = Mutex::new(RefCell::new(None));
48+
static BUTTON: Mutex<RefCell<Option<PA0<Input>>>> = Mutex::new(RefCell::new(None));
4949

5050
/// The center of the clock face
5151
const CENTER: Point = Point::new(64, 40);

examples/rtic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use panic_halt as _;
88
#[rtic::app(device = stm32f4xx_hal::pac)]
99
mod app {
1010
use stm32f4xx_hal::{
11-
gpio::{Edge, Input, Output, PullUp, PushPull, PA0, PC13},
11+
gpio::{Edge, Input, Output, PushPull, PA0, PC13},
1212
prelude::*,
1313
};
1414

@@ -17,7 +17,7 @@ mod app {
1717

1818
#[local]
1919
struct Local {
20-
button: PA0<Input<PullUp>>,
20+
button: PA0<Input>,
2121
led: PC13<Output<PushPull>>,
2222
}
2323

examples/stopwatch-with-ssd1306-and-interrupts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use panic_semihosting as _; // logs messages to the host stderr; requires a debu
2222
use stm32f4xx_hal as hal;
2323

2424
use crate::hal::{
25-
gpio::{Edge, Input, PullUp, PC13},
25+
gpio::{Edge, Input, PC13},
2626
i2c::I2c,
2727
interrupt, pac,
2828
prelude::*,
@@ -50,7 +50,7 @@ use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
5050
static ELAPSED_MS: Mutex<Cell<u32>> = Mutex::new(Cell::new(0u32));
5151
static TIMER_TIM2: Mutex<RefCell<Option<CounterUs<pac::TIM2>>>> = Mutex::new(RefCell::new(None));
5252
static STATE: Mutex<Cell<StopwatchState>> = Mutex::new(Cell::new(StopwatchState::Ready));
53-
static BUTTON: Mutex<RefCell<Option<PC13<Input<PullUp>>>>> = Mutex::new(RefCell::new(None));
53+
static BUTTON: Mutex<RefCell<Option<PC13<Input>>>> = Mutex::new(RefCell::new(None));
5454

5555
#[derive(Clone, Copy)]
5656
enum StopwatchState {

src/gpio.rs

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,19 @@ pub trait PinExt {
102102
pub struct Alternate<const A: u8, Otype = PushPull>(PhantomData<Otype>);
103103

104104
/// Input mode (type state)
105-
pub struct Input<MODE = Floating> {
106-
_mode: PhantomData<MODE>,
107-
}
108-
109-
/// Floating input (type state)
110-
pub struct Floating;
111-
112-
/// Pulled down input (type state)
113-
pub struct PullDown;
105+
pub struct Input;
114106

115-
/// Pulled up input (type state)
116-
pub struct PullUp;
107+
/// Pull setting for an input.
108+
#[derive(Debug, Eq, PartialEq)]
109+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
110+
pub enum Pull {
111+
/// Floating
112+
None = 0,
113+
/// Pulled up
114+
Up = 1,
115+
/// Pulled down
116+
Down = 2,
117+
}
117118

118119
/// Open drain input or output (type state)
119120
pub struct OpenDrain;
@@ -131,6 +132,10 @@ pub struct Analog;
131132

132133
pub type Debugger = Alternate<0, PushPull>;
133134

135+
impl sealed::Active for Input {}
136+
impl<Otype> sealed::Active for Output<Otype> {}
137+
impl<const A: u8, Otype> sealed::Active for Alternate<A, Otype> {}
138+
134139
/// GPIO Pin speed selection
135140
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
136141
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -152,11 +157,13 @@ pub enum Edge {
152157
mod sealed {
153158
/// Marker trait that show if `ExtiPin` can be implemented
154159
pub trait Interruptable {}
160+
/// Marker trait for active pin modes
161+
pub trait Active {}
155162
}
156163

157164
use sealed::Interruptable;
158165
impl<MODE> Interruptable for Output<MODE> {}
159-
impl<MODE> Interruptable for Input<MODE> {}
166+
impl Interruptable for Input {}
160167

161168
/// External Interrupt Pin
162169
pub trait ExtiPin {
@@ -262,7 +269,7 @@ where
262269
/// - `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section).
263270
/// - `P` is port name: `A` for GPIOA, `B` for GPIOB, etc.
264271
/// - `N` is pin number: from `0` to `15`.
265-
pub struct Pin<const P: char, const N: u8, MODE = Input<Floating>> {
272+
pub struct Pin<const P: char, const N: u8, MODE = Input> {
266273
_mode: PhantomData<MODE>,
267274
}
268275
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
@@ -317,34 +324,6 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, Output<MODE>> {
317324
}
318325
}
319326

320-
impl<const P: char, const N: u8> Pin<P, N, Output<OpenDrain>> {
321-
/// Enables / disables the internal pull up
322-
pub fn internal_pull_up(self, on: bool) -> Self {
323-
let offset = 2 * { N };
324-
let value = if on { 0b01 } else { 0b00 };
325-
unsafe {
326-
(*Gpio::<P>::ptr())
327-
.pupdr
328-
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (value << offset)))
329-
};
330-
331-
self
332-
}
333-
334-
/// Enables / disables the internal pull down
335-
pub fn internal_pull_down(self, on: bool) -> Self {
336-
let offset = 2 * { N };
337-
let value = if on { 0b10 } else { 0b00 };
338-
unsafe {
339-
(*Gpio::<P>::ptr())
340-
.pupdr
341-
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (value << offset)))
342-
};
343-
344-
self
345-
}
346-
}
347-
348327
impl<const P: char, const N: u8, const A: u8> Pin<P, N, Alternate<A, PushPull>> {
349328
/// Set pin speed
350329
pub fn set_speed(self, speed: Speed) -> Self {
@@ -358,11 +337,13 @@ impl<const P: char, const N: u8, const A: u8> Pin<P, N, Alternate<A, PushPull>>
358337

359338
self
360339
}
340+
}
361341

362-
/// Enables / disables the internal pull up
363-
pub fn internal_pull_up(self, on: bool) -> Self {
342+
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE> {
343+
/// Set the internal pull-up and pull-down resistor
344+
fn _internal_resistor(self, resistor: Pull) -> Self {
364345
let offset = 2 * { N };
365-
let value = if on { 0b01 } else { 0b00 };
346+
let value = resistor as u32;
366347
unsafe {
367348
(*Gpio::<P>::ptr())
368349
.pupdr
@@ -371,32 +352,33 @@ impl<const P: char, const N: u8, const A: u8> Pin<P, N, Alternate<A, PushPull>>
371352

372353
self
373354
}
355+
}
374356

375-
/// Enables / disables the internal pull down
376-
pub fn internal_pull_down(self, on: bool) -> Self {
377-
let offset = 2 * { N };
378-
let value = if on { 0b10 } else { 0b00 };
379-
unsafe {
380-
(*Gpio::<P>::ptr())
381-
.pupdr
382-
.modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (value << offset)))
383-
};
384-
385-
self
357+
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
358+
where
359+
MODE: sealed::Active,
360+
{
361+
/// Set the internal pull-up and pull-down resistor
362+
pub fn internal_resistor(self, resistor: Pull) -> Self {
363+
self._internal_resistor(resistor)
386364
}
387-
}
388365

389-
impl<const P: char, const N: u8, const A: u8> Pin<P, N, Alternate<A, PushPull>> {
390-
/// Turns pin alternate configuration pin into open drain
391-
pub fn set_open_drain(self) -> Pin<P, N, Alternate<A, OpenDrain>> {
392-
let offset = { N };
393-
unsafe {
394-
(*Gpio::<P>::ptr())
395-
.otyper
396-
.modify(|r, w| w.bits(r.bits() | (1 << offset)))
397-
};
366+
/// Enables / disables the internal pull up
367+
pub fn internal_pull_up(self, on: bool) -> Self {
368+
if on {
369+
self.internal_resistor(Pull::Up)
370+
} else {
371+
self.internal_resistor(Pull::None)
372+
}
373+
}
398374

399-
Pin::new()
375+
/// Enables / disables the internal pull down
376+
pub fn internal_pull_down(self, on: bool) -> Self {
377+
if on {
378+
self.internal_resistor(Pull::Down)
379+
} else {
380+
self.internal_resistor(Pull::None)
381+
}
400382
}
401383
}
402384

@@ -512,7 +494,7 @@ impl<const P: char, const N: u8> Pin<P, N, Output<OpenDrain>> {
512494
}
513495
}
514496

515-
impl<const P: char, const N: u8, MODE> Pin<P, N, Input<MODE>> {
497+
impl<const P: char, const N: u8> Pin<P, N, Input> {
516498
#[inline(always)]
517499
pub fn is_high(&self) -> bool {
518500
!self.is_low()
@@ -533,7 +515,7 @@ macro_rules! gpio {
533515
use crate::pac::{$GPIOX, RCC};
534516
use crate::rcc::{Enable, Reset};
535517
use super::{
536-
Floating, Input,
518+
Input,
537519
};
538520

539521
/// GPIO parts
@@ -567,7 +549,7 @@ macro_rules! gpio {
567549
pub type $PXn<MODE> = super::PEPin<$port_id, MODE>;
568550

569551
$(
570-
pub type $PXi<MODE = Input<Floating>> = super::Pin<$port_id, $i, MODE>;
552+
pub type $PXi<MODE = Input> = super::Pin<$port_id, $i, MODE>;
571553
)+
572554

573555
}

0 commit comments

Comments
 (0)