View previous topic :: View next topic |
Author |
Message |
andro
Joined: 12 Mar 2007 Posts: 94 Location: Ljubljana
|
Posted: Fri Jun 28, 2013 12:25 pm Post subject: XMEGA SPI MASTER SLAVE |
|
|
Hi!
I have connected two Xmega32a4 via SPI. And it does work just fine with sample code as below.
But the comunication only goes from master to slave. Now I would like to be able to send some bytes from slave to master. But I really have no clue, where to start.
I'm really looking foreward for Your help.
MASTER code:
Code: | Dim Select_bit As Bit
Dim Start_of_frame As Byte
Dim Array(10) As Byte
Reset Select_bit 'We start with 0
Start_of_frame = 111
Array(1) = 1
'CONFIG SPI MASTER on Port c
Config Spic = Hard , Master = Yes , Mode = 0 , Clockdiv = Clk32 , Data_order = Msb , Ss = None
Open "SPIC" For Binary As #10
do
Incr Array(1)
'SEND 1 BYTE ARRAY TO SLAVE
Reset Cs1 'Select Slave
Print #10 , Start_of_frame ; Array(1) , 1 'SEND Start Byte then ARRAY WITH 1 BYTES
Set Cs1 'Deselect Slave
Print #8 , Array(1) ' Print B to Serial PC connection
Lcdat 8 , 1 , Array(1)
wait 1
loop |
SLAVE code:
Code: | Dim Spi_data(5) As Byte
Dim Spi_data_ready As Bit
' CONFIG SPI SLAVE on Port C
' Clock comes from SPI Master (Maximum clock rate a XMEGA Slave can handle is when MASTER Clockrate is CLK4)
Config Spic = Hard , Master = No , Mode = 0 , Data_order = Msb 'XMEGA SPI SLAVE
Open "SPIC" For Binary As #10
On Spic_int Spi_data_available
Enable Spic_int , Lo
do
If Spi_data_ready = 1 Then
Reset Spi_data_ready
Print #1 , "Received Array = " ; spi_data(1)
Lcdat 8 , 1 , Spi_data(1)
Lcdat 8 , 30 , Spi_data(2)
endif
waitms 100 |
'SPI Port C Interrupt Service Routine
Spi_data_available:
Incr I
Spi_data(i) = Spic_data 'Read the SPI Data Register
If I = 2 Then
I = 0
Set Spi_data_ready
End If
Return
Best regards
Andrej
(BASCOM-AVR version : 2.0.7.6 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Jun 28, 2013 12:33 pm Post subject: |
|
|
that is why they call it master-slave.
you can only get data returned from the slave. so if the slave want to send data, the master must send data first so the slave can shift the data out back to the master.
there are ways to solve this :
- use a pin to signal the master to do the job.
- do a contentious stream _________________ Mark |
|
Back to top |
|
|
andro
Joined: 12 Mar 2007 Posts: 94 Location: Ljubljana
|
Posted: Fri Jun 28, 2013 12:52 pm Post subject: |
|
|
Hi Mark,
yes I understand that the master should do the 'pool'. Now, I would like to get back some byts in the same 'master poll' sequence. Like in the xm128a1_spi_MASTER.bas example.
Exrtract:
Code: | 'send to slave:
Print #12, Spi_send_byte
Print #1, "print this to uart"
'read from slave
input #12, Spi_receive_byte
Print #1, "print received byte"
|
BR
Andrej |
|
Back to top |
|
|
andro
Joined: 12 Mar 2007 Posts: 94 Location: Ljubljana
|
Posted: Mon Jul 01, 2013 1:40 pm Post subject: |
|
|
Hi again,
I still did not figure it out, how to solve this isue.
Plz, help
Tnx
Andrej |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Jul 01, 2013 3:38 pm Post subject: |
|
|
did you check out the spi-slave sample from the SPI folder?
it is very simple.
if you want to return the slave some data, you must fill the SPDR (or spi data register).
And this data can only be returned to the master if the master sends some data.
So this is what happens :
- master send a byte with some value, say 123
- slave clocks out the old value of spdr, so if slave want to return value 234, it must make sure that SPDR is loaded with this value before the master sends data.
- master receives the value from the slave SPDR register.
in the help and datasheet you can see that SPI is just a 16 bit shift register. I hope this explains it. _________________ Mark |
|
Back to top |
|
|
andro
Joined: 12 Mar 2007 Posts: 94 Location: Ljubljana
|
Posted: Tue Jul 02, 2013 7:30 am Post subject: |
|
|
Hi
Mark, Your explanation make me understand this isue a bit better. I'have done a lot with SPI (SPI FRAM, IO extenders, RTC, touch senzors, etc), but in this case I realy nead help.
Is the SPDR actualy SPIC_DATA on Xmega? (if SPIC is being used)
BR
Andrej |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Jul 02, 2013 9:22 am Post subject: |
|
|
yes that is the same register in xmega. _________________ Mark |
|
Back to top |
|
|
andro
Joined: 12 Mar 2007 Posts: 94 Location: Ljubljana
|
Posted: Wed Jul 03, 2013 12:34 pm Post subject: |
|
|
Hi,
guess, what was wrong
Setting MISO to OUTPUT is needed in case of SLAVE role!!!
(I must have missed that somewhere in the
Basic data transfer now works fine (and fast).
But, I learned a lot, spending around 16 houres only in solving this problem, reading Xmega A PDF, checking bascom examples ....
Hope, that the MISO realy goes in tri-state as long as the SS pin is driven high. Since I successfully run a MCP795W20 RTC on the same SPI, I guess it is ok.
BR
Andrej |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Jul 03, 2013 12:45 pm Post subject: |
|
|
it was in the slave sample :
'First configure the MISO pin
Config Pinb.4 = Output ' MISO _________________ Mark |
|
Back to top |
|
|
andro
Joined: 12 Mar 2007 Posts: 94 Location: Ljubljana
|
Posted: Wed Jul 03, 2013 2:07 pm Post subject: |
|
|
Yes, that's where I saw that....
BR
Andrej |
|
Back to top |
|
|
andro
Joined: 12 Mar 2007 Posts: 94 Location: Ljubljana
|
Posted: Wed Jul 03, 2013 9:27 pm Post subject: |
|
|
Hi again!
SPI between two Xmega32a4 work fine but, so far, I'm only able to read a single byte from slave at once. Is it possible to read more than one, or is the slave to slow, to fill SPIC_DATA register? I use Clockdiv = Clk32 on master side.
And, I use SS=none. As I understand, if SS=auto, CS is also deactivated between each transfer. Is that correct?
BR
Andrej |
|
Back to top |
|
|
|