SPI interface with other board_AVR_ESK1100 or among Arduino itself

Graynomad:
Nick had this

// SPI interrupt routine

ISR (SPI_STC_vect)
{
  byte c = SPDR;  // what we received from the master
  SPDR = 0;    // what to return to the master
}  // end of ISR SPI_STC_vect




You have this



ISR (SPI_STC_vect)
{
  c = SPDR;
 
if(c == 0x04)
  {
    digitalWrite(SS, LOW);
  SPI.transfer(dat[0]);
  SPI.transfer(dat[1]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[2]);
  SPI.transfer(dat[3]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[4]);
  SPI.transfer(dat[5]);
   digitalWrite(SS, HIGH);
}

else if(c == 0x06)
  {
   digitalWrite(SS, LOW);
  SPI.transfer(dat[6]);
  SPI.transfer(dat[7]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[8]);
  SPI.transfer(dat[9]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[10]);
  SPI.transfer(dat[11]);
   digitalWrite(SS, HIGH);
}
  else
SPDR = 0x00;    // what to return to the master

}  // end of interrupt service routine (ISR) SPI_STC_vect




Do you see the difference?

Nick said this



> The ISR has to be fast, because if you don't change SPDR fast enough, the incorrect data will be sent on the next transfer.



You have an ISR as long as your arm that will take about three weeks to execute.

Nick said this



> you need to allow time for the slave to do whatever it needs to do, and set up a response (ie. set SPDR).



Your code has the odd delay but not after every transfer.

On the master you are toggling the for every two bytes, why?

On the slave you do similar on the SS pin. You DON'T touch the SS pin on the slave. You DON'T call SPI.transfer() on the slave(). You DON'T do nothing on the slave except put data in the SPDR register. If you need to parse the first byte as a command you'd better do it real quick and use the results to point to a different array of bytes to return to the master.


______
Rob

Hi...!!!

Thanks. More than two SPI.transfer(); does not work fine and 2 bytes of data transfer gives continuous 16clock pulse.
Following code does not work fine with SPDR = data; and I want to send back more than 10 data. And I am not able to give proper delay.
Can you please tell me how much delay I should give at Master side or Slave side.

#include "pins_arduino.h"
#include <SPI.h>
#define SS 10
int dat[24] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14};
byte c;
void ss_falling()
{
  c = 0;
}

void setup (void)
{

  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode (SPI_MODE2);
  SPI.setClockDivider(SPI_CLOCK_DIV64) ;

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

  // turn on interrupts
  SPCR |= _BV(SPIE);
  
 // disable timer interrupts
  TIMSK0 = 0;
  // interrupt for SS falling edge
  attachInterrupt (0, ss_falling, FALLING);
   // disable timer interrupts
  TIMSK0 = 0;
  
}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  c = SPDR;
  
  if(c == 0x04)
  {
    
    SPDR = dat[0];
   
 }
 
   else if(c == 0x06)
  {
    SPDR = dat[1];
   
 }
  else
 SPDR = 0x00;    // what to return to the master
 


}  // end of interrupt service routine (ISR) SPI_STC_vect

void loop (void)
{
 
}  // end of loop