Tricky SPI communication with request and response structure.

Thread Starter


Joined Dec 1, 2016
Hi Friends,

I have to implement a SPI request_response function for 8-bit microcontroller. This function has two arguments one is request command structure and other is response structure which is filled after getting response. Its 8-bytes i.e.,
typdef struct{
uint16_t data0;
uint16_t data1;
uint16_t data2;
uint16_t data3;

extern data_st *request;
extern data_st *response;

request_response(&request, &response);
There are 3-4 set of request and response. Which is sent one by one.
When request0 is sent first response must be discarded as there is 8-bytes(64 bits) gap in response from the other slave system. When request1 is sent response0 is gotten. Same way request2 is sent response1 is received. Same way request..n ...response..n-1 and request_dummy will be sent in the last to receive response..n.

I have to collect the correct response for a request. Means need to map response0 for request0 , response1 for request1 and response2 for request2 for further processing.

How to manage this? Please help.



Joined Feb 24, 2006
You do know that the whole idea of SPI is to talk to one device at a time -- right?
You also know that for each byte you send out, you get exactly one byte back at the same time -- right?
I don't think your scheme and how SPI works are going to be compatible.
Do you know something that I don't?


Joined Oct 4, 2015
If you store your replies in a buffer (maybe a circular buffer), you already can reference the reply by the request index -1.
Sometthing like
uint8_t getResponseIndex(uint8_t requestIndex)
    return (! requestIndex) ? 0 : requestIndex - 1;
// ... more code
However, you can avoid using this function since you can just track the request index. :rolleyes:
Maybe, describe better the situation and how you are working.
Last edited:

John P

Joined Oct 14, 2008
I don't understand what happens with the structure elements, which are all uint16_t's. How are these sent/received over SPI?