SPI - 1byte out 4 bytes in

Using HW SPI.
I read that using DMA, TX buffer needs same size as RX buffer. This because for each byte out, there is a byte in.

So suppose I am sending 1 byte and read 4, my buffer will be 5 bytes where:
TXbuffer[5] = {cmd, dummyt, dummyt, dummyt, dummyt};

return on RXbuffer:
RXbuffer[0] = dummyr
RXbuffer[1] = byte4
RXbuffer[2] = byte3
RXbuffer[3] = byte2
RXbuffer[4] = byte1

Is that how it works?

and if I use SPI.transfer(val) for single bytes, if I pass &val I will get the RX on val?


Searching Core code I found that received = SPI.transfer(send), with byte type.
Documentation does not mention that:

@nlambuca, are you using a Core or a Photon. The SPI.transfer() functions ALWAYS returns a receive value whether you use it or not. When sending and expecting a return byte, the command is ret = SPI.transfer(val);

For DMA SPI, each outgoing byte will have a “return” value that you either ignore or not.

Correct. I went through core code and found it, but documentation does not mention it.
ST32 actually allows also 16bit transfers, but Wire implementation limits to 8bit.


1 Like