View previous topic :: View next topic |
Author |
Message |
SoBot
Joined: 20 Apr 2010 Posts: 9
|
SPI write hangs |
Posted: Wed Sep 01, 2010 1:28 am |
|
|
I have a SPI link between a computer and a PIC18f2431. The computer is the master and pic is slave. The master writes one byte to the slave then waits for 1 second and then calls a read_spi 3 times with a 1 second wait in between, to start the clock signal so the slave can respond. ( I made it so long to rule out timing problems) The slave then writes 3 bytes. What happens is that the pic hangs after the first or the second byte and is stuck write there. do I need to wait a while between the writes on the pic also? This is the simplified code:
Code: |
#int_ssp
void ssp_isr(void)
{
if (SPI_RXBufferCounter < SPI_RXBufferSize)
{
SPI_RXBuffer[SPI_RXBufferCounter] = spi_read();
SPI_RXBufferCounter++;
}
} |
Code: |
While(1)
{
if (SPI_RXBufferCounter > 0)
{
spi_write(56);
spi_write(65);
spi_write(47);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Sep 01, 2010 4:15 am |
|
|
The _master_, needs to generate the clocks for it's reads.
So it needs to use something like:
value=spi_read(0);
Note the '0'.
The point is that a simple 'read' command, sits and waits, till the data arrives. This is fine on the slave (where data has been clocked to it, by the master), but on a master read, the master itself, needs to actually send a byte, and the eight clocks, in order to receive a byte. 'read(0)', says for the master to read a value, and clock 'out' a dummy '0', to get this.
Simply reading, will hang after the first byte (there will be one garbage byte in the input buffer that arrived, when the master 'wrote' the command).
Best Wishes |
|
|
SoBot
Joined: 20 Apr 2010 Posts: 9
|
|
Posted: Wed Sep 01, 2010 3:05 pm |
|
|
Thanks Ttelmah. Let me give you some more info on my situation. My master is a SBC and only has half duplex SPI. So the master SPI_Read()command does not take any parameters. This is how I understand it: I write a byte (the command byte), the slave recieves it and calls the spi_write() which puts a byte in the buffer. Then as soon as the master calls the spi_read(), it creates a clock sgnal that clocks the buffer on the slave into the master buffer. The master should do this 3 times to clock all three bytes into master buffer. Is this correct? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Thu Sep 02, 2010 2:02 am |
|
|
You need to read the paperwork for your master, and verify whether it does generate clocks for a read. If it doesn't, you will need to perform a 'dummy write', before each read.
Best Wishes |
|
|
SoBot
Joined: 20 Apr 2010 Posts: 9
|
|
Posted: Fri Sep 03, 2010 7:01 am |
|
|
Yes it does, I also tested it with a scope. |
|
|
|