Skip to content

Commit 97b6822

Browse files
committed
Fix #18
1 parent f17d3a0 commit 97b6822

File tree

8 files changed

+166
-140
lines changed

8 files changed

+166
-140
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Changed
9+
- Removed lifetimes on `OutputPin`, `InputPin`, `I2c`, `Spi`, and `SpiDevice` to improve ease-of-use.
10+
711
## [0.13.0] - 2022-09-28
812
### Changed
913
- Updated the alpha release of `embedded-hal` from `1.0.0-alpha.8` to `1.0.0-alpha.9`.

examples/spi-flash.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ fn main() {
5959
while addr < 0x100 {
6060
flash.read(addr, &mut buf).unwrap();
6161
println!("{:02x}: {:02x?}", addr, buf);
62-
addr += LINE as u32;
62+
addr += LINE;
6363
}
6464
}

src/gpio.rs

+38-34
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,33 @@ use std::sync::{Arc, Mutex};
1010
/// [`FtHal::ad0`]: crate::FtHal::ad0
1111
/// [`FtHal::ad7`]: crate::FtHal::ad7
1212
#[derive(Debug)]
13-
pub struct OutputPin<'a, Device: MpsseCmdExecutor> {
13+
pub struct OutputPin<Device: MpsseCmdExecutor> {
1414
/// Parent FTDI device.
15-
mtx: &'a Arc<Mutex<FtInner<Device>>>,
15+
mtx: Arc<Mutex<FtInner<Device>>>,
1616
/// GPIO pin index. 0-7 for the FT232H.
1717
idx: u8,
1818
}
1919

20-
impl<'a, Device, E> OutputPin<'a, Device>
20+
impl<Device, E> OutputPin<Device>
2121
where
2222
Device: MpsseCmdExecutor<Error = E>,
2323
E: std::error::Error,
2424
Error<E>: From<E>,
2525
{
2626
pub(crate) fn new(
27-
mtx: &'a Arc<Mutex<FtInner<Device>>>,
27+
mtx: Arc<Mutex<FtInner<Device>>>,
2828
idx: u8,
29-
) -> Result<OutputPin<'a, Device>, Error<E>> {
30-
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
31-
32-
lock.direction |= 1 << idx;
33-
lock.allocate_pin(idx, PinUse::Output);
34-
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
35-
.set_gpio_lower(lock.value, lock.direction)
36-
.send_immediate();
37-
lock.ft.send(cmd.as_slice())?;
29+
) -> Result<OutputPin<Device>, Error<E>> {
30+
{
31+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
32+
33+
lock.direction |= 1 << idx;
34+
lock.allocate_pin(idx, PinUse::Output);
35+
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
36+
.set_gpio_lower(lock.value, lock.direction)
37+
.send_immediate();
38+
lock.ft.send(cmd.as_slice())?;
39+
}
3840
Ok(OutputPin { mtx, idx })
3941
}
4042

@@ -56,14 +58,14 @@ where
5658
}
5759
}
5860

59-
impl<'a, Device: MpsseCmdExecutor> OutputPin<'a, Device> {
61+
impl<Device: MpsseCmdExecutor> OutputPin<Device> {
6062
/// Convert the GPIO pin index to a pin mask
6163
pub(crate) fn mask(&self) -> u8 {
6264
1 << self.idx
6365
}
6466
}
6567

66-
impl<'a, Device, E> eh1::digital::ErrorType for OutputPin<'a, Device>
68+
impl<Device, E> eh1::digital::ErrorType for OutputPin<Device>
6769
where
6870
Device: MpsseCmdExecutor<Error = E>,
6971
E: std::error::Error,
@@ -72,7 +74,7 @@ where
7274
type Error = Error<E>;
7375
}
7476

75-
impl<'a, Device, E> eh1::digital::OutputPin for OutputPin<'a, Device>
77+
impl<Device, E> eh1::digital::OutputPin for OutputPin<Device>
7678
where
7779
Device: MpsseCmdExecutor<Error = E>,
7880
E: std::error::Error,
@@ -87,7 +89,7 @@ where
8789
}
8890
}
8991

90-
impl<'a, Device, E> eh0::digital::v2::OutputPin for OutputPin<'a, Device>
92+
impl<Device, E> eh0::digital::v2::OutputPin for OutputPin<Device>
9193
where
9294
Device: MpsseCmdExecutor<Error = E>,
9395
E: std::error::Error,
@@ -111,31 +113,33 @@ where
111113
/// [`FtHal::adi0`]: crate::FtHal::adi0
112114
/// [`FtHal::adi7`]: crate::FtHal::adi7
113115
#[derive(Debug)]
114-
pub struct InputPin<'a, Device: MpsseCmdExecutor> {
116+
pub struct InputPin<Device: MpsseCmdExecutor> {
115117
/// Parent FTDI device.
116-
mtx: &'a Arc<Mutex<FtInner<Device>>>,
118+
mtx: Arc<Mutex<FtInner<Device>>>,
117119
/// GPIO pin index. 0-7 for the FT232H.
118120
idx: u8,
119121
}
120122

121-
impl<'a, Device, E> InputPin<'a, Device>
123+
impl<Device, E> InputPin<Device>
122124
where
123125
Device: MpsseCmdExecutor<Error = E>,
124126
E: std::error::Error,
125127
Error<E>: From<E>,
126128
{
127129
pub(crate) fn new(
128-
mtx: &'a Arc<Mutex<FtInner<Device>>>,
130+
mtx: Arc<Mutex<FtInner<Device>>>,
129131
idx: u8,
130-
) -> Result<InputPin<'a, Device>, Error<E>> {
131-
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
132-
133-
lock.direction &= !(1 << idx);
134-
lock.allocate_pin(idx, PinUse::Input);
135-
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
136-
.set_gpio_lower(lock.value, lock.direction)
137-
.send_immediate();
138-
lock.ft.send(cmd.as_slice())?;
132+
) -> Result<InputPin<Device>, Error<E>> {
133+
{
134+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
135+
136+
lock.direction &= !(1 << idx);
137+
lock.allocate_pin(idx, PinUse::Input);
138+
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
139+
.set_gpio_lower(lock.value, lock.direction)
140+
.send_immediate();
141+
lock.ft.send(cmd.as_slice())?;
142+
}
139143
Ok(InputPin { mtx, idx })
140144
}
141145

@@ -151,14 +155,14 @@ where
151155
}
152156
}
153157

154-
impl<'a, Device: MpsseCmdExecutor> InputPin<'a, Device> {
158+
impl<Device: MpsseCmdExecutor> InputPin<Device> {
155159
/// Convert the GPIO pin index to a pin mask
156160
pub(crate) fn mask(&self) -> u8 {
157161
1 << self.idx
158162
}
159163
}
160164

161-
impl<'a, Device, E> eh1::digital::ErrorType for InputPin<'a, Device>
165+
impl<Device, E> eh1::digital::ErrorType for InputPin<Device>
162166
where
163167
Device: MpsseCmdExecutor<Error = E>,
164168
E: std::error::Error,
@@ -167,7 +171,7 @@ where
167171
type Error = Error<E>;
168172
}
169173

170-
impl<'a, Device, E> eh1::digital::InputPin for InputPin<'a, Device>
174+
impl<Device, E> eh1::digital::InputPin for InputPin<Device>
171175
where
172176
Device: MpsseCmdExecutor<Error = E>,
173177
E: std::error::Error,
@@ -182,7 +186,7 @@ where
182186
}
183187
}
184188

185-
impl<'a, Device, E> eh0::digital::v2::InputPin for InputPin<'a, Device>
189+
impl<Device, E> eh0::digital::v2::InputPin for InputPin<Device>
186190
where
187191
Device: MpsseCmdExecutor<Error = E>,
188192
E: std::error::Error,

src/i2c.rs

+30-28
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const BITS_OUT: ClockBitsOut = ClockBitsOut::MsbNeg;
1818
///
1919
/// [`FtHal::i2c`]: crate::FtHal::i2c
2020
#[derive(Debug)]
21-
pub struct I2c<'a, Device: MpsseCmdExecutor> {
21+
pub struct I2c<Device: MpsseCmdExecutor> {
2222
/// Parent FTDI device.
23-
mtx: &'a Arc<Mutex<FtInner<Device>>>,
23+
mtx: Arc<Mutex<FtInner<Device>>>,
2424
/// Length of the start, repeated start, and stop conditions.
2525
///
2626
/// The units for these are dimensionless number of MPSSE commands.
@@ -30,34 +30,36 @@ pub struct I2c<'a, Device: MpsseCmdExecutor> {
3030
fast: bool,
3131
}
3232

33-
impl<'a, Device, E> I2c<'a, Device>
33+
impl<Device, E> I2c<Device>
3434
where
3535
Device: MpsseCmdExecutor<Error = E>,
3636
E: std::error::Error,
3737
Error<E>: From<E>,
3838
{
39-
pub(crate) fn new(mtx: &Arc<Mutex<FtInner<Device>>>) -> Result<I2c<Device>, Error<E>> {
40-
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
41-
42-
lock.allocate_pin(0, PinUse::I2c);
43-
lock.allocate_pin(1, PinUse::I2c);
44-
lock.allocate_pin(2, PinUse::I2c);
45-
46-
// clear direction and value of first 3 pins
47-
48-
lock.direction &= !0x07;
49-
lock.value &= !0x07;
50-
// AD0: SCL
51-
// AD1: SDA (master out)
52-
// AD2: SDA (master in)
53-
// pins are set as input (tri-stated) in idle mode
54-
55-
// set GPIO pins to new state
56-
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
57-
.set_gpio_lower(lock.value, lock.direction)
58-
.enable_3phase_data_clocking()
59-
.send_immediate();
60-
lock.ft.send(cmd.as_slice())?;
39+
pub(crate) fn new(mtx: Arc<Mutex<FtInner<Device>>>) -> Result<I2c<Device>, Error<E>> {
40+
{
41+
let mut lock = mtx.lock().expect("Failed to aquire FTDI mutex");
42+
43+
lock.allocate_pin(0, PinUse::I2c);
44+
lock.allocate_pin(1, PinUse::I2c);
45+
lock.allocate_pin(2, PinUse::I2c);
46+
47+
// clear direction and value of first 3 pins
48+
49+
lock.direction &= !0x07;
50+
lock.value &= !0x07;
51+
// AD0: SCL
52+
// AD1: SDA (master out)
53+
// AD2: SDA (master in)
54+
// pins are set as input (tri-stated) in idle mode
55+
56+
// set GPIO pins to new state
57+
let cmd: MpsseCmdBuilder = MpsseCmdBuilder::new()
58+
.set_gpio_lower(lock.value, lock.direction)
59+
.enable_3phase_data_clocking()
60+
.send_immediate();
61+
lock.ft.send(cmd.as_slice())?;
62+
}
6163

6264
Ok(I2c {
6365
mtx,
@@ -620,7 +622,7 @@ where
620622
}
621623
}
622624

623-
impl<'a, Device, E> eh0::blocking::i2c::Read for I2c<'a, Device>
625+
impl<Device, E> eh0::blocking::i2c::Read for I2c<Device>
624626
where
625627
Device: MpsseCmdExecutor<Error = E>,
626628
E: std::error::Error,
@@ -637,7 +639,7 @@ where
637639
}
638640
}
639641

640-
impl<'a, Device, E> eh0::blocking::i2c::Write for I2c<'a, Device>
642+
impl<Device, E> eh0::blocking::i2c::Write for I2c<Device>
641643
where
642644
Device: MpsseCmdExecutor<Error = E>,
643645
E: std::error::Error,
@@ -654,7 +656,7 @@ where
654656
}
655657
}
656658

657-
impl<'a, Device, E> eh0::blocking::i2c::WriteRead for I2c<'a, Device>
659+
impl<Device, E> eh0::blocking::i2c::WriteRead for I2c<Device>
658660
where
659661
Device: MpsseCmdExecutor<Error = E>,
660662
E: std::error::Error,

0 commit comments

Comments
 (0)