Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

First you need to confirm that your master and slave have common value of the parameters such as SPI mode and data size. SPI mode will decide the idle polarity of SPI clock line and the data sampling edge whether it is going to be first or second. Data size will tell whether it is 8-bit or 16-bit or may be something different depending upon chip.

Now, when master selects a slave through chip/slave select pin slave becomes active and waits for clock to change it's state, once clock changes state slave waits for clock line again for 1st or 2nd edge depending upon SPI mode selection. If it is 1st edge slave samples MOSI line data and puts into it's internal shift register, if slave has to transmit data also, it has to keep data on MISO line before the selected clock edge. This process will be repeated till 8 or 16 times depending upon data size. Upon completion, master and slave can generate interrupt to their CPU to collect the data (in case of microcontroller).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top