View previous topic :: View next topic |
Author |
Message |
nicofer
Joined: 01 May 2013 Posts: 90 Location: GRJ
|
Posted: Tue Jul 07, 2015 12:50 pm Post subject: SPI slave problems |
|
|
Hi .
A lot has been asked and said about the SPI slave mode , but after a day of reading all I can get about it I still have inconsistent data transfer.
I need to send 6 bytes from one avr to the other , the mster to slave is working , but not without issues . The solution was add byte nr 7 ( fixed value ) and get the CRC8 and then send this as byte nr 8 so the slave - if all data on slave side is fine it will pass it on.
Now at the same time I need to return 8 bytes back from the slave to the master .
I have an extra control line between the 2 chips , kind of a ' sync ' to reset slave data rx counter and receive data array index.
Working from the sample code for spi data rx , this is the spi rx and tx commands :
Spi_isr:
Select Case C
Case 1 :
Spdr = Rxpdodata(1)
Receive_array(1) = Spdr
Case 2 :
Spdr = Rxpdodata(2)
Receive_array(2) = Spdr
Case 3 :
Spdr = &H01
Receive_array(3) = Spdr
Case 4 :
Spdr = &H23
Receive_array(4) = Spdr
Case 5 :
Spdr = &H45
Receive_array(5) = Spdr
Case 6 :
Spdr = &H67
Receive_array(6) = Spdr
Case 7 :
Spdr = &H89
Receive_array(7) = Spdr
Case 8 :
Spdr = Rxpdodata(
Receive_array( = Spdr
Tempb = Crc8(receive_array(1) , 7)
If Receive_array( = Tempb Then Set Rbit
Case 9 :
C = 1
End Select
Incr C
Return
In order to send the 8 'th byte to the master , I send an extra dummy byte ( nr 9 )
My problem is that bytes 1 and 2 is received correct , byte 3 & 4 is correct most of the time and byte 5 , 6, 7 is the same data sent by the MASTER to the slave !!
For now I only use the first 2 bytes , bytes 3-7 is hard coded for testing and 8 is the crc of bytes 1-7 .
What I can not understand is why some of the data is received correct and some not ?
Here is a screen dump of the rx spi data at the master :
A6D8 0123 123456DC
A7D8 0103 123456DC
A8D8 0111 923456DC
A9D8 0111 923456DC
AAD8 0123 12345692
ABD8 0123 123456DC
ACD8 0123 123456DC
AED8 0123 1233D6DC
AFD8 0123 123456DC
B0D8 0123 123456DC
B1D8 0123 1234569B
B2D8 0123 123456DC
B3D8 0123 123456DC
B4D8 0123 123456DC
B5D8 0123 123456DC
B6D8 0111 923456DC
B7D8 0123 123456DC
B9D8 0123 123456DC
First 2 bytes are 16 bit counter , next 5 should be 0123 456789 and then the CRC8 value , but the fixed values are not fixed.
Here is the MASTER spi sending ( and receiving ) routine :
Reset Portb.0
Spidata( = Crc8(spidata(1) , 7)
Spiout Spidata(1) , 1
Spirxdat(1) = Spdr
Spiout Spidata(2) , 1
Spirxdat(1) = Spdr
Spiout Spidata(3) , 1
Spirxdat(2) = Spdr
Spiout Spidata(4) , 1
Spirxdat(3) = Spdr
Spiout Spidata(5) , 1
Spirxdat(4) = Spdr
Spiout Spidata(6) , 1
Spirxdat(5) = Spdr
Spiout Spidata(7) , 1
Spirxdat(6) = Spdr
Spiout Spidata( , 1
Spirxdat(7) = Spdr
Tempb = 0
Spiout Tempb , 1
Spirxdat( = Spdr
Set Portb.0
I tried Spirxdat(1) = Spimove(spidata(1) , , not any better .
Anybody with a solution ?
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
nicofer
Joined: 01 May 2013 Posts: 90 Location: GRJ
|
Posted: Tue Jul 07, 2015 4:17 pm Post subject: SPI slave rx tx Sorted |
|
|
The solution was to add a few micro sec delay after the byte is sent out before the new data is read from the SPDR buffer .
To make sure I only work with valid values I still use a CRC8 compare of all data that travel on the SPI bus .
SPI master TX , rx , routine : ( part of it )
Spiout Spidata(1) , 1
Spiout Spidata(2) , 1
Waitus 5
Spirxdat(1) = Spdr
Spiout Spidata(3) , 1
Waitus 5
Spirxdat(2) = Spdr
Hope this helps
Cheers |
|
Back to top |
|
|
|
|
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 You cannot attach files in this forum You cannot download files in this forum
|
|