views:

83

answers:

3

Hello again, I was doing some reading and have even gotten a "master" SPI working on my microcontroller. Here is my question, basically if the master wants to initialize a write to the slave we write to the SSPBUF, how do we control what the slave responds with? The datasheet doesn't seem really clear to me the order of events in that case.

I.E. Master puts a char into the SSPBUF, this initiates the SPI module to send data to the slave, during the shift, the slave returns a byte.

In the slave side, is there something that tells you you have incoming data, and you can write to your SSPBUF first, THEN accept the data?

OR

Do you have to write to the SSPBUF the first "return value" you want sent back before the master can have an opportunity to initiate a transfer?

A: 

Whatever your microcontroller is, three is likely to be an ISR associated with receipt of SPI data, and a register where the data that has been received can be accessed and copied into a local variable.

vicatcu
There is, but it only signifies incoming data, the problem I'm having is that I don't know how to make sure I have the data in the slave that I want before the Master initiates a response. OR with every transfer that occurs, I think I've solved it but am having the occasional timing issue now, however I haven't read the master side of the channel now that i have the slave hooked up, but I'll report back when I find out more information.
onaclov2000
A: 

Typically what i have done in the past with SPI is I send 2 bytes from the master to the slave with a minimal delay in between. The master sends: "X Y" where "X" is the variable it wishes to read from the slave and "Y" is really just a dummy variable which is used to clock out the response from the slave. At the same time, the slave gets an interrupt when it recieves "X", looks up what value to put in its output buffer, and when it receives "Y", the response to its packet is clocked out to the master.

Seidleroni
+1  A: 

You want to use an interrupt on the GPIO line which handles your slave's chip-select. Ensure there is enough time for the slave to process this interrupt and load the outgoing data register before the clock starts.

Some SPI modules preclude the use of GPIO - you will need to check if your SPI module has a function to handle chip select, or only enable the module based on the GPIO activity.

Yann Ramin
actually looking at this answer it seems to make a lot of sense after revisiting the topic, if I simply wait until I see a chipselect hi, then I pass the data to the buff then I should be ready for data. I'll have to give it a try.
onaclov2000