embedded-hal icon indicating copy to clipboard operation
embedded-hal copied to clipboard

How to maintain an SPI transaction while manipulating GPIO pins?

Open ciniml opened this issue 2 years ago • 21 comments
trafficstars

Hi.

Recently, the API of the SpiDevice trait has been updated to use a list of Operation instances instead of passing a closure.

https://github.com/rust-embedded/embedded-hal/blob/9ac61a73d78f071ff50198cdb8d22a992030f908/embedded-hal/src/spi.rs#L342

Before the API change, I had written code in the implementation of a driver for the ILI9341/ILI9342 SPI LCD, as shown below, to toggle the DC (Data/Command) pin while the CS is asserted.

// self.spi is a field whose type is `SpiDevice`
// Send command byte [0x04] while DC is low (which indicates command byte)
// Then change DC to high and read thee data bytes into `buffer`
let mut buffer = [0, 0, 0]; 
self.spi.transaction(|bus| {
    self.pin_dc.set_low().unwrap();
    bus.write(&[0x04])?;
    self.pin_dc.set_high().unwrap();
    bus.read(&mut buffer)?;
    Ok(())
})

After the API was changed, it appears that toggling the DC pin within a transaction has become difficult. So, my question is, how can I implement a similar function using the new API? I couldn't find any examples to do it.

ciniml avatar Jul 29 '23 18:07 ciniml