Skip to content
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added re-exports for `libftd2xx` and `ftdi` when the respective feature is used.

### Changed
- Changed the internal shared FTDI type from `Mutex<RefCell<FtInner<Device>>>` to `Arc<Mutex<FtInner<Device>>>`.

## [0.11.0] - 2022-01-18
### Added
- Added support for input pins.
Expand Down
53 changes: 25 additions & 28 deletions src/gpio.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::error::Error;
use crate::{FtInner, PinUse};
use ftdi_mpsse::{MpsseCmdBuilder, MpsseCmdExecutor};
use std::result::Result;
use std::{cell::RefCell, sync::Mutex};
use std::sync::{Arc, Mutex};

/// FTDI output pin.
///
Expand All @@ -13,7 +12,7 @@ use std::{cell::RefCell, sync::Mutex};
#[derive(Debug)]
pub struct OutputPin<'a, Device: MpsseCmdExecutor> {
/// Parent FTDI device.
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
mtx: &'a Arc<Mutex<FtInner<Device>>>,
/// GPIO pin index. 0-7 for the FT232H.
idx: u8,
}
Expand All @@ -25,34 +24,33 @@ where
Error<E>: From<E>,
{
pub(crate) fn new(
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
mtx: &'a Arc<Mutex<FtInner<Device>>>,
idx: u8,
) -> Result<OutputPin<'a, Device>, Error<E>> {
let lock = mtx.lock().expect("Failed to aquire FTDI mutex");
let mut inner = lock.borrow_mut();
inner.direction |= 1 << idx;
inner.allocate_pin(idx, PinUse::Output);
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(inner.value, inner.direction)
.set_gpio_lower(lock.value, lock.direction)
.send_immediate();
inner.ft.send(cmd.as_slice())?;
lock.ft.send(cmd.as_slice())?;
Ok(OutputPin { mtx, idx })
}

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

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

let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
.set_gpio_lower(inner.value, inner.direction)
.set_gpio_lower(lock.value, lock.direction)
.send_immediate();
inner.ft.send(cmd.as_slice())?;
lock.ft.send(cmd.as_slice())?;

Ok(())
}
Expand Down Expand Up @@ -91,7 +89,7 @@ where
#[derive(Debug)]
pub struct InputPin<'a, Device: MpsseCmdExecutor> {
/// Parent FTDI device.
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
mtx: &'a Arc<Mutex<FtInner<Device>>>,
/// GPIO pin index. 0-7 for the FT232H.
idx: u8,
}
Expand All @@ -103,28 +101,27 @@ where
Error<E>: From<E>,
{
pub(crate) fn new(
mtx: &'a Mutex<RefCell<FtInner<Device>>>,
mtx: &'a Arc<Mutex<FtInner<Device>>>,
idx: u8,
) -> Result<InputPin<'a, Device>, Error<E>> {
let lock = mtx.lock().expect("Failed to aquire FTDI mutex");
let mut inner = lock.borrow_mut();
inner.direction &= !(1 << idx);
inner.allocate_pin(idx, PinUse::Input);
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(inner.value, inner.direction)
.set_gpio_lower(lock.value, lock.direction)
.send_immediate();
inner.ft.send(cmd.as_slice())?;
lock.ft.send(cmd.as_slice())?;
Ok(InputPin { mtx, idx })
}

pub(crate) fn get(&self) -> Result<bool, Error<E>> {
let lock = self.mtx.lock().expect("Failed to aquire FTDI mutex");
let mut inner = lock.borrow_mut();
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();
inner.ft.send(cmd.as_slice())?;
inner.ft.recv(&mut buffer)?;
lock.ft.send(cmd.as_slice())?;
lock.ft.recv(&mut buffer)?;

Ok((buffer[0] & self.mask()) != 0)
}
Expand Down
Loading