Skip to content

Commit 0a7e341

Browse files
Fix SpiBus for use with Polarity::IdleHigh
Instead of just storing the correct polarity, also set this polarity in the GPIO struct, so that changes that write to GPIOs (e.g. asserting CS) will maintain the correct idle level. If this is not done, the FTDI will create invalid output with clock spikes. Fixes #75
1 parent e45f446 commit 0a7e341

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/spi.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,28 @@ where
118118
/// let device = libftd2xx::Ft2232h::with_description("Dual RS232-HS A")?;
119119
/// let hal = hal::FtHal::init_freq(device, 3_000_000)?;
120120
/// let mut spi = hal.spi()?;
121-
/// spi.set_clock_polarity(Polarity::IdleLow);
121+
/// spi.set_clock_polarity(Polarity::IdleLow)?;
122122
/// # }
123123
/// # Ok::<(), std::boxed::Box<dyn std::error::Error>>(())
124124
/// ```
125125
///
126126
/// [SPI mode]: https://en.wikipedia.org/wiki/Serial_Peripheral_Interface#Mode_numbers
127-
pub fn set_clock_polarity<P: Into<Polarity>>(&mut self, cpol: P) {
128-
self.pol = cpol.into()
127+
pub fn set_clock_polarity<P: Into<Polarity>>(&mut self, cpol: P) -> Result<(), Error<E>> {
128+
self.pol = cpol.into();
129+
let mut lock = self.mtx.lock().unwrap();
130+
match self.pol.clk {
131+
ClockData::MsbNegIn | ClockData::LsbNegIn => {
132+
lock.lower.value |= 1;
133+
}
134+
ClockData::MsbPosIn | ClockData::LsbPosIn => {
135+
lock.lower.value &= !1;
136+
}
137+
}
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+
Ok(())
129143
}
130144
}
131145

@@ -465,7 +479,7 @@ where
465479
/// let device = libftd2xx::Ft2232h::with_description("Dual RS232-HS A")?;
466480
/// let hal = hal::FtHal::init_freq(device, 3_000_000)?;
467481
/// let mut spi = hal.spi_device(3)?;
468-
/// spi.set_clock_polarity(Polarity::IdleLow);
482+
/// spi.set_clock_polarity(Polarity::IdleLow)?;
469483
/// # }
470484
/// # Ok::<(), std::boxed::Box<dyn std::error::Error>>(())
471485
/// ```

0 commit comments

Comments
 (0)