I need to implement SPI Slave (@10 MHz clock) on a microcontroller (a sensor with Cortex M33 core) in software. The problem is software is not fast enough to prepare data just after the command. (also discussed here: how-to-code-spi-slaves)
I have SPI drivers with SPI-Slave mode. So, communication is okay. The question is about protocol.
Luckily, I also code the master (Cortex-M0+), so I am free to define the protocol. What are the options I have? Some ideas I have so far:
Option 1, Send response with a fixed delay
MOSI: command 0xFF 0xFF 0xFF 0xFF 0xFF ...
MISO: 0xFF dummy dummy dummy data0 data1 ...
So, master clocks for command length + response length + fixed delay. I guess it would be hard to count number of dummy bytes sent while processing and then copy data just before correct position.
Option 2, Send response with a variable delay
Similar to Option 1 but not fixed. Add a token as first byte to show where it starts.
MOSI: command 0xFF 0xFF 0xFF 0xFF 0xFF ...
MISO: 0xFF dummy dummy token data0 data1 ...
Problem is master should clock for command length + response length + max delay. This will slow-down communication depending on max delay.
Option 3, Request and response after interrupt
Essentially, half duplex. Just send command first and end communication. Wait for interrupt, then start communication again.
MOSI: command [wait for interrupt] 0xFF 0xFF 0xFF ...
MISO: 0xFF [process ] data0 data1 data2 ...
Master needs to handle timeout and it should keep track of what was sent. I guess this is the most reasonable option.