Synchronized SPI communication



I am trying to implement interrupt based SPI communication between two
Atmel microcontrollers. The data has to be transmitted from slave to
master.

In master i continuously sends 0xff to the slave and master expects
data from slave on each transfer.

char SPI_MasterTransfer(char cData)
{
uint8_t a;
PORTB &= ~(_BV(PB4)); // Slave select
a = SPDR;
SPDR = cData;
while(!(SPSR & (1<<SPIF)));
PORTB |= (_BV(PB4));
return a;

}

I have a an array buf[3] = {10,20,30} in the slave. Whenever the
transaction is over the slave gets into the interrupt service routine
and checks if the data received is 0xff, if so then transmits buf[i]
(where i varies as 0,1,2,0,1,2....)

ISR(SPI_STC_vect)
{

uint8_t data;

data = SPDR;

if(data == 0xff){
SPDR = buf[i];
flag = 2;
i= (i+1)%3;
}

}

Now the problem is i get correct data as well as junk data at the
master side.

What i deciphered the problem is:

After the reception of 0xff from master the slave get into the ISR()
and at this time the master may again send another 0xff for which the
master gets back 0xff (ie., the last data received in the slave SPDR)
itself from slave.

I solved the problem by putting delays between each transfer in master
micro.

But the solution looks a bit (lot) awkward. Can i achieve
synchronization in any other method?? (any kind of talk back
methods...)

Thank you..

.



Relevant Pages

  • Re: Ds1302
    ... > So the slave will receive the data and not transmits at all. ... > when slave receives data from master the i2ci bit set in the I2CCON ... > so in the interrupt routine i have just compliment a port pin to ...
    (sci.electronics.design)
  • Re: Replikations Frage
    ... vom Master auf den Slave kopiert hast und dabei die Permissions auf ... Die Datenbanken auf den Slave Server laufen ohne Probleme. ... # If you want to know which options a program support, ...
    (de.comp.datenbanken.mysql)
  • Re: When caveman Uhg invented fire
    ... be a business owner. ... But even the smallest "ownership" makes one a slave. ... Any "slave" can become the master. ...
    (alt.lang.asm)
  • Re: One-sided authentication for small micros?
    ... The master has a lookup table of all 'known' slave id's. ... This would reset all the slave keys to their lookup values. ... decides that it is a valid slave and starts to communicate with the slave. ...
    (sci.crypt)
  • Re: FIXMBR redux
    ... William, I did the best I could for you under the circumstances and I did ... >> "To remove the recovery console, delete the cmdcons folder from the root ... > drive as Master and formatted it anew, as Active and Primary, ... I then jumpered it as Slave, ...
    (microsoft.public.windowsxp.basics)