CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SPI write hangs

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SoBot



Joined: 20 Apr 2010
Posts: 9

View user's profile Send private message

SPI write hangs
PostPosted: Wed Sep 01, 2010 1:28 am     Reply with quote

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: 19328

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 4:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 3:05 pm     Reply with quote

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: 19328

View user's profile Send private message

PostPosted: Thu Sep 02, 2010 2:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 7:01 am     Reply with quote

Yes it does, I also tested it with a scope.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group