Skip to content

Commit

Permalink
Rename PullUpDown to Bias and set_pullupdown to set_bias
Browse files Browse the repository at this point in the history
  • Loading branch information
golemparts committed Oct 12, 2023
1 parent 9295526 commit 3743fe9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 35 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.15.0 (TBD)

* **Gpio**: (Breaking change) Rename `PullUpDown` enum to `Bias`, and `set_pullupdown` to `set_bias`.
* **Gpio**: (Breaking change) Add support for new modes `Alt6`, `Alt7` and `Alt8`.

## 0.14.1 (November 25, 2022)

* **Gpio**: Fix subtract underflow panic in software-based PWM.
Expand Down
10 changes: 5 additions & 5 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,18 +298,18 @@ impl Not for Level {

/// Built-in pull-up/pull-down resistor states.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum PullUpDown {
pub enum Bias {
Off = 0b00,
PullDown = 0b01,
PullUp = 0b10,
}

impl fmt::Display for PullUpDown {
impl fmt::Display for Bias {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
PullUpDown::Off => write!(f, "Off"),
PullUpDown::PullDown => write!(f, "PullDown"),
PullUpDown::PullUp => write!(f, "PullUp"),
Bias::Off => write!(f, "Off"),
Bias::PullDown => write!(f, "PullDown"),
Bias::PullUp => write!(f, "PullUp"),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpio/gpiomem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::gpio::{Level, Mode, PullUpDown};
use crate::gpio::{Bias, Level, Mode};

pub mod bcm;
pub mod rp1;
Expand All @@ -9,5 +9,5 @@ pub(crate) trait GpioRegisters: std::fmt::Debug + Sync + Send {
fn level(&self, pin: u8) -> Level;
fn mode(&self, pin: u8) -> Mode;
fn set_mode(&self, pin: u8, mode: Mode);
fn set_pullupdown(&self, pin: u8, pud: PullUpDown);
fn set_bias(&self, pin: u8, bias: Bias);
}
14 changes: 7 additions & 7 deletions src/gpio/gpiomem/bcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::time::Duration;
use libc::{self, c_void, off_t, size_t, MAP_FAILED, MAP_SHARED, O_SYNC, PROT_READ, PROT_WRITE};

use crate::gpio::gpiomem::GpioRegisters;
use crate::gpio::{Error, Level, Mode, PullUpDown, Result};
use crate::gpio::{Bias, Error, Level, Mode, Result};
use crate::system::{DeviceInfo, SoC};

const PATH_DEV_GPIOMEM: &str = "/dev/gpiomem";
Expand Down Expand Up @@ -248,7 +248,7 @@ impl GpioRegisters for GpioMem {
self.locks[offset].store(false, Ordering::SeqCst);
}

fn set_pullupdown(&self, pin: u8, pud: PullUpDown) {
fn set_bias(&self, pin: u8, bias: Bias) {
// Offset for register.
let offset: usize;
// Bit shift for pin position within register value.
Expand All @@ -263,10 +263,10 @@ impl GpioRegisters for GpioMem {
let lock = GPPUD_CNTRL_REG0 + pin as usize / 32;

// Pull up vs pull down has a reverse bit pattern on BCM2711 vs others.
let pud = match pud {
PullUpDown::Off => 0b00u32,
PullUpDown::PullDown => 0b10,
PullUpDown::PullUp => 0b01,
let pud = match bias {
Bias::Off => 0b00u32,
Bias::PullDown => 0b10,
Bias::PullUp => 0b01,
};

loop {
Expand Down Expand Up @@ -304,7 +304,7 @@ impl GpioRegisters for GpioMem {

// Set the control signal in GPPUD.
let reg_value = self.read(GPPUD);
self.write(GPPUD, (reg_value & !0b11) | ((pud as u32) & 0b11));
self.write(GPPUD, (reg_value & !0b11) | ((bias as u32) & 0b11));

// The datasheet mentions waiting at least 150 cycles for set-up and hold, but
// doesn't state which clock is used. This is likely the VPU clock (see
Expand Down
4 changes: 2 additions & 2 deletions src/gpio/gpiomem/rp1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ptr;

use libc::{self, c_void, size_t, MAP_FAILED, MAP_SHARED, O_SYNC, PROT_READ, PROT_WRITE};

use crate::gpio::{Error, Level, Mode, PullUpDown, Result};
use crate::gpio::{Bias, Error, Level, Mode, Result};
use crate::system::{DeviceInfo, SoC};

use super::GpioRegisters;
Expand Down Expand Up @@ -151,7 +151,7 @@ impl GpioRegisters for GpioMem {
unimplemented!()
}

fn set_pullupdown(&self, pin: u8, pud: PullUpDown) {
fn set_bias(&self, pin: u8, bias: Bias) {
unimplemented!()
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/gpio/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use std::time::Duration;

use super::soft_pwm::SoftPwm;
use crate::gpio::{interrupt::AsyncInterrupt, GpioState, Level, Mode, PullUpDown, Result, Trigger};
use crate::gpio::{interrupt::AsyncInterrupt, Bias, GpioState, Level, Mode, Result, Trigger};

const NANOS_PER_SEC: f64 = 1_000_000_000.0;

Expand Down Expand Up @@ -207,8 +207,8 @@ macro_rules! impl_drop {
self.pin.set_mode(prev_mode);
}

if self.pud_mode != PullUpDown::Off {
self.pin.set_pullupdown(PullUpDown::Off);
if self.bias != Bias::Off {
self.pin.set_bias(Bias::Off);
}
}
}
Expand Down Expand Up @@ -296,7 +296,7 @@ impl Pin {
/// [`Input`]: enum.Mode.html#variant.Input
#[inline]
pub fn into_input(self) -> InputPin {
InputPin::new(self, PullUpDown::Off)
InputPin::new(self, Bias::Off)
}

/// Consumes the `Pin` and returns an [`InputPin`]. Sets the mode to [`Input`]
Expand All @@ -310,7 +310,7 @@ impl Pin {
/// [`reset_on_drop`]: struct.InputPin.html#method.set_reset_on_drop
#[inline]
pub fn into_input_pulldown(self) -> InputPin {
InputPin::new(self, PullUpDown::PullDown)
InputPin::new(self, Bias::PullDown)
}

/// Consumes the `Pin` and returns an [`InputPin`]. Sets the mode to [`Input`]
Expand All @@ -324,7 +324,7 @@ impl Pin {
/// [`reset_on_drop`]: struct.InputPin.html#method.set_reset_on_drop
#[inline]
pub fn into_input_pullup(self) -> InputPin {
InputPin::new(self, PullUpDown::PullUp)
InputPin::new(self, Bias::PullUp)
}

/// Consumes the `Pin` and returns an [`OutputPin`]. Sets the mode to [`Mode::Output`]
Expand Down Expand Up @@ -367,8 +367,8 @@ impl Pin {
}

#[inline]
pub(crate) fn set_pullupdown(&mut self, pud: PullUpDown) {
self.gpio_state.gpio_mem.set_pullupdown(self.pin, pud);
pub(crate) fn set_bias(&mut self, bias: Bias) {
self.gpio_state.gpio_mem.set_bias(self.pin, bias);
}

#[inline]
Expand Down Expand Up @@ -424,11 +424,11 @@ pub struct InputPin {
prev_mode: Option<Mode>,
async_interrupt: Option<AsyncInterrupt>,
reset_on_drop: bool,
pud_mode: PullUpDown,
bias: Bias,
}

impl InputPin {
pub(crate) fn new(mut pin: Pin, pud_mode: PullUpDown) -> InputPin {
pub(crate) fn new(mut pin: Pin, bias: Bias) -> InputPin {
let prev_mode = pin.mode();

let prev_mode = if prev_mode == Mode::Input {
Expand All @@ -438,14 +438,14 @@ impl InputPin {
Some(prev_mode)
};

pin.set_pullupdown(pud_mode);
pin.set_bias(bias);

InputPin {
pin,
prev_mode,
async_interrupt: None,
reset_on_drop: true,
pud_mode,
bias,
}
}

Expand Down Expand Up @@ -578,7 +578,7 @@ pub struct OutputPin {
pin: Pin,
prev_mode: Option<Mode>,
reset_on_drop: bool,
pud_mode: PullUpDown,
bias: Bias,
pub(crate) soft_pwm: Option<SoftPwm>,
// Stores the softpwm frequency. Used for embedded_hal::PwmPin.
#[cfg(feature = "hal")]
Expand All @@ -603,7 +603,7 @@ impl OutputPin {
pin,
prev_mode,
reset_on_drop: true,
pud_mode: PullUpDown::Off,
bias: Bias::Off,
soft_pwm: None,
#[cfg(feature = "hal")]
frequency: 0.0,
Expand Down Expand Up @@ -671,7 +671,7 @@ pub struct IoPin {
mode: Mode,
prev_mode: Option<Mode>,
reset_on_drop: bool,
pud_mode: PullUpDown,
bias: Bias,
pub(crate) soft_pwm: Option<SoftPwm>,
// Stores the softpwm frequency. Used for embedded_hal::PwmPin.
#[cfg(feature = "hal")]
Expand All @@ -697,7 +697,7 @@ impl IoPin {
mode,
prev_mode,
reset_on_drop: true,
pud_mode: PullUpDown::Off,
bias: Bias::Off,
soft_pwm: None,
#[cfg(feature = "hal")]
frequency: 0.0,
Expand Down Expand Up @@ -730,9 +730,9 @@ impl IoPin {

/// Configures the built-in pull-up/pull-down resistors.
#[inline]
pub fn set_pullupdown(&mut self, pud: PullUpDown) {
self.pin.set_pullupdown(pud);
self.pud_mode = pud;
pub fn set_bias(&mut self, bias: Bias) {
self.pin.set_bias(bias);
self.bias = bias;
}

impl_input!();
Expand Down

0 comments on commit 3743fe9

Please sign in to comment.