I'm currently implementing an SPI Slave but with a different MCU (QE64/QE128). Here are some considerations to look in to:
- The SPI clock speed, which is controlled by the master. If it's too fast, the MCU won't be able to keep up and you will loose some data.
- The master expecting the slave to return a byte at a specific offset. This works good on deterministic ASIC or FPGA devices like serial PROMs but if the MCU has tasks other than just waiting for the SPI interrupt to occur, chances are you won't be able to always provide the data when the master expects it.
In order to have a perfect master and the slave communication, I had to introduce a framing concept based on Async-HDLC (RFC1662) where the slave had lots of time to prepare a transmit buffer and the master just had to send some dummy bytes (FLAG IDLE bytes to be more precise) every once and a while to retrieve the data from the slave. No timing issue.
Good luck!
Hello,
A slave SPI application will usually involve the use of interrupt on SPI receive, so that it is possible for other work to be done by the MCU whilst waiting for SPI events. If the data to be returned is determined by the byte value just received, there will be some delay until the first return byte can be written to the SPI data register, ready for interrogation by the SPI master. The master must therefore allow a suitable worst case delay before attempting to interrogate the returned data. This delay should additionally allow for the execution of any other interrupt sources.
If the worst case delay is potentially large, but may vary over a wide range, it may be more efficient to provide "handshaking" back to the master, using a GP output pin, to indicate when the return data is ready. This is in addition to the SS signal originating from the master.
When multiple bytes are to be returned by the slave, keep in mind that this is an instance where the double buffering associated with SPI send may prove very useful to minimize the delay required once the data stream has started.
Regards,
Mac