Skip to content

Commit

Permalink
Fix #18
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Nov 24, 2022
1 parent f17d3a0 commit 1770d4c
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 140 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Removed lifetimes on `OutputPin`, `InputPin`, `I2c`, `Spi`, and `SpiDevice` to improve ease-of-use.

## [0.13.0] - 2022-09-28
### Changed
- Updated the alpha release of `embedded-hal` from `1.0.0-alpha.8` to `1.0.0-alpha.9`.
Expand Down
2 changes: 1 addition & 1 deletion examples/spi-flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ fn main() {
while addr < 0x100 {
flash.read(addr, &mut buf).unwrap();
println!("{:02x}: {:02x?}", addr, buf);
addr += LINE as u32;
addr += LINE;
}
}
72 changes: 38 additions & 34 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@ use std::sync::{Arc, Mutex};
/// [`FtHal::ad0`]: crate::FtHal::ad0
/// [`FtHal::ad7`]: crate::FtHal::ad7
#[derive(Debug)]
pub struct OutputPin<'a, Device: MpsseCmdExecutor> {
pub struct OutputPin<Device: MpsseCmdExecutor> {
/// Parent FTDI device.
mtx: &'a Arc<Mutex<FtInner<Device>>>,
mtx: Arc<Mutex<FtInner<Device>>>,
/// GPIO pin index. 0-7 for the FT232H.
idx: u8,
}

impl<'a, Device, E> OutputPin<'a, Device>
impl<Device, E> OutputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Error<E>: From<E>,
{
pub(crate) fn new(
mtx: &'a Arc<Mutex<FtInner<Device>>>,
mtx: Arc<Mutex<FtInner<Device>>>,
idx: u8,
) -> Result<OutputPin<'a, 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.ft.send(cmd.as_slice())?;
) -> 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.ft.send(cmd.as_slice())?;
}
Ok(OutputPin { mtx, idx })
}

Expand All @@ -56,14 +58,14 @@ where
}
}

impl<'a, Device: MpsseCmdExecutor> OutputPin<'a, Device> {
impl<Device: MpsseCmdExecutor> OutputPin<Device> {
/// Convert the GPIO pin index to a pin mask
pub(crate) fn mask(&self) -> u8 {
1 << self.idx
}
}

impl<'a, Device, E> eh1::digital::ErrorType for OutputPin<'a, Device>
impl<Device, E> eh1::digital::ErrorType for OutputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand All @@ -72,7 +74,7 @@ where
type Error = Error<E>;
}

impl<'a, Device, E> eh1::digital::OutputPin for OutputPin<'a, Device>
impl<Device, E> eh1::digital::OutputPin for OutputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand All @@ -87,7 +89,7 @@ where
}
}

impl<'a, Device, E> eh0::digital::v2::OutputPin for OutputPin<'a, Device>
impl<Device, E> eh0::digital::v2::OutputPin for OutputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand All @@ -111,31 +113,33 @@ where
/// [`FtHal::adi0`]: crate::FtHal::adi0
/// [`FtHal::adi7`]: crate::FtHal::adi7
#[derive(Debug)]
pub struct InputPin<'a, Device: MpsseCmdExecutor> {
pub struct InputPin<Device: MpsseCmdExecutor> {
/// Parent FTDI device.
mtx: &'a Arc<Mutex<FtInner<Device>>>,
mtx: Arc<Mutex<FtInner<Device>>>,
/// GPIO pin index. 0-7 for the FT232H.
idx: u8,
}

impl<'a, Device, E> InputPin<'a, Device>
impl<Device, E> InputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Error<E>: From<E>,
{
pub(crate) fn new(
mtx: &'a Arc<Mutex<FtInner<Device>>>,
mtx: Arc<Mutex<FtInner<Device>>>,
idx: u8,
) -> Result<InputPin<'a, 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.ft.send(cmd.as_slice())?;
) -> 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.ft.send(cmd.as_slice())?;
}
Ok(InputPin { mtx, idx })
}

Expand All @@ -151,14 +155,14 @@ where
}
}

impl<'a, Device: MpsseCmdExecutor> InputPin<'a, Device> {
impl<Device: MpsseCmdExecutor> InputPin<Device> {
/// Convert the GPIO pin index to a pin mask
pub(crate) fn mask(&self) -> u8 {
1 << self.idx
}
}

impl<'a, Device, E> eh1::digital::ErrorType for InputPin<'a, Device>
impl<Device, E> eh1::digital::ErrorType for InputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand All @@ -167,7 +171,7 @@ where
type Error = Error<E>;
}

impl<'a, Device, E> eh1::digital::InputPin for InputPin<'a, Device>
impl<Device, E> eh1::digital::InputPin for InputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand All @@ -182,7 +186,7 @@ where
}
}

impl<'a, Device, E> eh0::digital::v2::InputPin for InputPin<'a, Device>
impl<Device, E> eh0::digital::v2::InputPin for InputPin<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand Down
58 changes: 30 additions & 28 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const BITS_OUT: ClockBitsOut = ClockBitsOut::MsbNeg;
///
/// [`FtHal::i2c`]: crate::FtHal::i2c
#[derive(Debug)]
pub struct I2c<'a, Device: MpsseCmdExecutor> {
pub struct I2c<Device: MpsseCmdExecutor> {
/// Parent FTDI device.
mtx: &'a Arc<Mutex<FtInner<Device>>>,
mtx: Arc<Mutex<FtInner<Device>>>,
/// Length of the start, repeated start, and stop conditions.
///
/// The units for these are dimensionless number of MPSSE commands.
Expand All @@ -30,34 +30,36 @@ pub struct I2c<'a, Device: MpsseCmdExecutor> {
fast: bool,
}

impl<'a, Device, E> I2c<'a, Device>
impl<Device, E> I2c<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Error<E>: From<E>,
{
pub(crate) fn new(mtx: &Arc<Mutex<FtInner<Device>>>) -> Result<I2c<Device>, Error<E>> {
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");

lock.allocate_pin(0, PinUse::I2c);
lock.allocate_pin(1, PinUse::I2c);
lock.allocate_pin(2, PinUse::I2c);

// clear direction and value of first 3 pins

lock.direction &= !0x07;
lock.value &= !0x07;
// AD0: SCL
// AD1: SDA (master out)
// AD2: SDA (master in)
// pins are set as input (tri-stated) in idle mode

// set GPIO pins to new state
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
.set_gpio_lower(lock.value, lock.direction)
.enable_3phase_data_clocking()
.send_immediate();
lock.ft.send(cmd.as_slice())?;
pub(crate) fn new(mtx: Arc<Mutex<FtInner<Device>>>) -> Result<I2c<Device>, Error<E>> {
{
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");

lock.allocate_pin(0, PinUse::I2c);
lock.allocate_pin(1, PinUse::I2c);
lock.allocate_pin(2, PinUse::I2c);

// clear direction and value of first 3 pins

lock.direction &= !0x07;
lock.value &= !0x07;
// AD0: SCL
// AD1: SDA (master out)
// AD2: SDA (master in)
// pins are set as input (tri-stated) in idle mode

// set GPIO pins to new state
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
.set_gpio_lower(lock.value, lock.direction)
.enable_3phase_data_clocking()
.send_immediate();
lock.ft.send(cmd.as_slice())?;
}

Ok(I2c {
mtx,
Expand Down Expand Up @@ -620,7 +622,7 @@ where
}
}

impl<'a, Device, E> eh0::blocking::i2c::Read for I2c<'a, Device>
impl<Device, E> eh0::blocking::i2c::Read for I2c<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand All @@ -637,7 +639,7 @@ where
}
}

impl<'a, Device, E> eh0::blocking::i2c::Write for I2c<'a, Device>
impl<Device, E> eh0::blocking::i2c::Write for I2c<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand All @@ -654,7 +656,7 @@ where
}
}

impl<'a, Device, E> eh0::blocking::i2c::WriteRead for I2c<'a, Device>
impl<Device, E> eh0::blocking::i2c::WriteRead for I2c<Device>
where
Device: MpsseCmdExecutor<Error = E>,
E: std::error::Error,
Expand Down
Loading

0 comments on commit 1770d4c

Please sign in to comment.