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: Support upper pins #51

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 68 additions & 24 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use crate::{FtInner, PinUse};
use ftdi_mpsse::{MpsseCmdBuilder, MpsseCmdExecutor};
use std::sync::{Arc, Mutex};

/// Pin number
#[derive(Debug, Copy, Clone)]
pub(crate) enum Pin {
Lower(u8),
Upper(u8),
}

/// FTDI output pin.
///
/// This is created by calling [`FtHal::ad0`] - [`FtHal::ad7`].
Expand All @@ -14,7 +21,7 @@ pub struct OutputPin<Device: MpsseCmdExecutor> {
/// Parent FTDI device.
mtx: Arc<Mutex<FtInner<Device>>>,
/// GPIO pin index. 0-7 for the FT232H.
idx: u8,
pin: Pin,
}

impl<Device, E> OutputPin<Device>
Expand All @@ -25,33 +32,49 @@ where
{
pub(crate) fn new(
mtx: Arc<Mutex<FtInner<Device>>>,
idx: u8,
pin: Pin,
) -> Result<OutputPin<Device>, Error<E>> {
{
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");

lock.direction |= 1 << idx;
lock.allocate_pin(idx, PinUse::Output);
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
.set_gpio_lower(lock.value, lock.direction)
.send_immediate();
lock.allocate_pin_any(pin, PinUse::Output);

let (byte, idx) = match pin {
Pin::Lower(idx) => (&mut lock.lower, idx),
Pin::Upper(idx) => (&mut lock.upper, idx),
};
byte.direction |= 1 << idx;
let cmd = MpsseCmdBuilder::new();
let cmd = match pin {
Pin::Lower(_) => cmd.set_gpio_lower(byte.value, byte.direction),
Pin::Upper(_) => cmd.set_gpio_upper(byte.value, byte.direction),
}
.send_immediate();
lock.ft.send(cmd.as_slice())?;
}
Ok(OutputPin { mtx, idx })
Ok(OutputPin { mtx, pin })
}

pub(crate) fn set(&self, state: bool) -> Result<(), Error<E>> {
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");

let byte = match self.pin {
Pin::Lower(_) => &mut lock.lower,
Pin::Upper(_) => &mut lock.upper,
};

if state {
lock.value |= self.mask();
byte.value |= self.mask();
} else {
lock.value &= !self.mask();
byte.value &= !self.mask();
};

let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
.set_gpio_lower(lock.value, lock.direction)
.send_immediate();
let cmd = MpsseCmdBuilder::new();
let cmd = match self.pin {
Pin::Lower(_) => cmd.set_gpio_lower(byte.value, byte.direction),
Pin::Upper(_) => cmd.set_gpio_upper(byte.value, byte.direction),
}
.send_immediate();
lock.ft.send(cmd.as_slice())?;

Ok(())
Expand All @@ -61,7 +84,11 @@ where
impl<Device: MpsseCmdExecutor> OutputPin<Device> {
/// Convert the GPIO pin index to a pin mask
pub(crate) fn mask(&self) -> u8 {
1 << self.idx
let idx = match self.pin {
Pin::Lower(idx) => idx,
Pin::Upper(idx) => idx,
};
1 << idx
}
}

Expand Down Expand Up @@ -117,7 +144,7 @@ pub struct InputPin<Device: MpsseCmdExecutor> {
/// Parent FTDI device.
mtx: Arc<Mutex<FtInner<Device>>>,
/// GPIO pin index. 0-7 for the FT232H.
idx: u8,
pin: Pin,
}

impl<Device, E> InputPin<Device>
Expand All @@ -128,26 +155,39 @@ where
{
pub(crate) fn new(
mtx: Arc<Mutex<FtInner<Device>>>,
idx: u8,
pin: Pin,
) -> Result<InputPin<Device>, Error<E>> {
{
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");

lock.direction &= !(1 << idx);
lock.allocate_pin(idx, PinUse::Input);
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
.set_gpio_lower(lock.value, lock.direction)
.send_immediate();
lock.allocate_pin_any(pin, PinUse::Input);

let (byte, idx) = match pin {
Pin::Lower(idx) => (&mut lock.lower, idx),
Pin::Upper(idx) => (&mut lock.upper, idx),
};
byte.direction &= !(1 << idx);
let cmd = MpsseCmdBuilder::new();
let cmd = match pin {
Pin::Lower(_) => cmd.set_gpio_lower(byte.value, byte.direction),
Pin::Upper(_) => cmd.set_gpio_upper(byte.value, byte.direction),
}
.send_immediate();
lock.ft.send(cmd.as_slice())?;
}
Ok(InputPin { mtx, idx })
Ok(InputPin { mtx, pin })
}

pub(crate) fn get(&self) -> Result<bool, Error<E>> {
let mut lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");

let mut buffer = [0u8; 1];
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new().gpio_lower().send_immediate();
let cmd = MpsseCmdBuilder::new();
let cmd = match self.pin {
Pin::Lower(_) => cmd.gpio_lower(),
Pin::Upper(_) => cmd.gpio_upper(),
}
.send_immediate();
lock.ft.send(cmd.as_slice())?;
lock.ft.recv(&mut buffer)?;

Expand All @@ -158,7 +198,11 @@ where
impl<Device: MpsseCmdExecutor> InputPin<Device> {
/// Convert the GPIO pin index to a pin mask
pub(crate) fn mask(&self) -> u8 {
1 << self.idx
let idx = match self.pin {
Pin::Lower(idx) => idx,
Pin::Upper(idx) => idx,
};
1 << idx
}
}

Expand Down
Loading