rust-embedded / embedded-hal

A Hardware Abstraction Layer (HAL) for embedded systems
Apache License 2.0
2.01k stars 202 forks source link

How to read incoming data from SPI MOSI Lane? (not MISO) #624

Open Syphixs opened 3 months ago

Syphixs commented 3 months ago

I am working on a specific epd which requires to read data from the MOSI line after sending a command but switching the CS pin between each byte. It's unclear to me if this is even achievable with the SPI Bus implementation. If so some guidance would be appreciated. Some relevant parts from the documentation:

appnote1

Here it also states that a CS pulse is necessary between each read.

appnote2

I thought something like this would be working:

    pub fn read_otp(&mut self, buffer: &mut [u8]) -> Result<(), E> {
        self.bus.lock(|bus| {
            let mut bus = bus.borrow_mut();

            self.cs.set_low().ok();
            // Command mode
            self.dc.set_low().ok();

            // Send read command
            bus.write(&[0xA2])?;

            self.dc.set_high().ok(); // Data mode

            // Read data
            for byte in buffer.iter_mut() {
                self.cs.set_high().ok();
                self.cs.set_low().ok();

                // Send dummy byte and read the result
                let mut read_buf = [0u8];
                bus.transfer(&mut read_buf, &[0x00])?;
                *byte = read_buf[0];
            }

            self.cs.set_high().ok();
            Ok(())
        })
    }

but then i found out that the transfer also wants to read on the MISO line.

jannic commented 2 months ago

This looks like a non-standard variation of SPI.

While it may (or may not) be supported by the SPI hardware of your microcontroller, it's not something supported by the abstraction layer provided by embedded-hal.