SPI slave interrupt not triggered

Hi,
I have two Arduino Mega boards and trying to make SPI work between the two.
However the master seems to be working fine, but the interrupt in the SPI slave is not getting triggered.

Im using the pins 50,51,52 and 53 on both devices. I have attached the code I'm using.

From the logic analyzer Master seems to be working fine

Anyone have an idea why this might be happening?

SPI Master code :

/*************************************
 * *******SPI MASTER******************
 **************************************/


/************************************************************************************************
* INCLUDE LIBRARIES
************************************************************************************************/
// include the SPI library functions
#include <SPI.h>

/************************************************************************************************
* MACROS
************************************************************************************************/
#define SPIMA_SPI_CS_PIN      53
#define SPISL_SPI_MISO_PIN    50
#define SPISL_SPI_MOSI_PIN    51
#define SPISL_SPI_SCK_PIN     52
#define SPIMA_SPI_SCOM_BAUD_RATE 9600
/************************************************************************************************
* Variables
************************************************************************************************/
boolean SPIMA_SpiReceived;
byte SPIMA_SPIMasterReceived, SPIMA_SPIMasterSend;
byte SPIMA_SPIEndSpiTransaction = 0;
byte Test = 0;
/************************************************************************************************
* Function prototypes
************************************************************************************************/
void SPIMA_SpiInit(void);
void SPIMA_SpiTransfer (void);
/************************************************************************************************
* Setup code
************************************************************************************************/
void setup() {
  pinMode(LED_BUILTIN,OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
 
  
  Serial.begin(9600);

  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("SPI Master Online");
   SPIMA_SpiInit();
  
  
}

/************************************************************************************************
* LOOP Code
************************************************************************************************/
void loop() 
{
  //variable to get the byte from USB serial input
  uint8_t SPIMA_Byte = 0;

  //Check if USB serial communication received any byte
  if(Serial.available( ))
  {
    //read the byte received from USB communication
    SPIMA_Byte = Serial.read();
    
    //trigger the spi transmission if the bbyte received is ascii '1'  
    
    if(SPIMA_Byte == '1')
    {
       
        SPIMA_SpiTransfer();
         //Serial.println(SPCR);
    }    
  }
}

/************************************************************************************************
* Function Name: SPIMA_SpiTransfer
* Description : SPI transfer
* in  : void
* out : None
* inout : None
* return  : void  
************************************************************************************************/
void SPIMA_SpiTransfer (void)
{
    uint8_t SPIMA_Byte1 = 0;  
    digitalWrite(SPIMA_SPI_CS_PIN, LOW);   
    SPI.transfer(0x22);
    Test = SPI.transfer(0x00);
    //SPIMA_Byte1 = SPI.transfer(0);
    digitalWrite(SPIMA_SPI_CS_PIN, HIGH);  
    Serial.println(SPIMA_Byte1); 
}
/************************************************************************************************
* Function Name: SPIMA_SpiInits
* Description : SPI Initialization
* in  : void
* out : None
* inout : None
* return  : void  
************************************************************************************************/
void SPIMA_SpiInit (void)
{
  
  SPI.begin();
  // initialize SPI:
  SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE2));
  // have to send on master in, *slave out*
  pinMode(SPISL_SPI_MISO_PIN, INPUT);
  pinMode(SPISL_SPI_MOSI_PIN, OUTPUT);
  pinMode(SPISL_SPI_SCK_PIN, OUTPUT);
  //set CS pin to OUTPUT
  pinMode(SPIMA_SPI_CS_PIN,OUTPUT);
  

}

SPI Slave code :

/*Begining of Auto generated code by Atmel studio */
#include <Arduino.h>

/*End of auto generated code by Atmel studio */

/************************************************************************************************
* INCLUDE LIBRARIES
************************************************************************************************/
// include the SPI library functions
#include <SPI.h>
//Beginning of Auto generated function prototypes by Atmel Studio
ISR(SPI_STC_vect );
void SPISL_SpiTransfer(void );
//End of Auto generated function prototypes by Atmel Studio



/************************************************************************************************
* MACROS
************************************************************************************************/
#define SPISL_SPI_CS_PIN		53
#define SPISL_SPI_MISO_PIN		50
#define SPISL_SPI_MOSI_PIN		51
#define SPISL_SPI_SCK_PIN		52

#define SPISL_SPI_SCOM_BAUD_RATE 9600
/************************************************************************************************
* Variables
************************************************************************************************/
boolean SPISL_SpiReceived = 0;
uint8_t SPISL_SpiRxByte = 0;
uint8_t SPISL_SpiSend = 0;
uint8_t SPIMA_Byte = 0;
/************************************************************************************************
* Function prototypes
************************************************************************************************/
void SPISL_SpiInit(void);
void SPISL_SpiTransfer (void);

/************************************************************************************************
* Setup code
************************************************************************************************/
void setup() {

  SPISL_SpiInit();
  
  pinMode(LED_BUILTIN,OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  
  Serial.begin(SPISL_SPI_SCOM_BAUD_RATE);
  
  while (!Serial) 
  {
   ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("SPI Slave Online");
}

/************************************************************************************************
* LOOP Code
************************************************************************************************/
void loop() 
{

  //Check if USB serial communication received any byte
  if(SPISL_SpiReceived != 0)
  {
    
    Serial.println(SPISL_SpiRxByte);
     
    //trigger the spi transmission if the byte received is ascii '1'
    SPISL_SpiTransfer();
  
    SPISL_SpiReceived = 0;
    
  }
}

/************************************************************************************************
* Function Name: SPISL_SpiInit
* Description : SPI Initialization
* in  : void
* out : None
* inout : None
* return  : void  
************************************************************************************************/
void SPISL_SpiInit (void)
{
  // initialize SPI:
  interrupts();
  //SPI.begin(); 
  //SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE2)); 
  pinMode(SPISL_SPI_CS_PIN,INPUT_PULLUP);
 
  //Set SPI to Slave mode  (MSTR bit to 0)
  SPCR = 0xCA;

  //Turn ON SPI interrupt
  //SPI.attachInterrupt();
  // have to send on master in, *slave out*
  pinMode(SPISL_SPI_MISO_PIN, OUTPUT);
  pinMode(SPISL_SPI_MOSI_PIN, INPUT);
  pinMode(SPISL_SPI_SCK_PIN, INPUT);
  //set D/CX pin to output
  pinMode(SPISL_SPI_DCX_PIN,OUTPUT);
  
  SPISL_SpiReceived = 0x00;
}

/************************************************************************************************
* ISR : SPI_STC_vect
* Description : Triggered when SPI data is received 
************************************************************************************************/
ISR (SPI_STC_vect)
{               
  SPISL_SpiReceived = 1;          
  SPISL_SpiRxByte = SPDR;
}

/************************************************************************************************
* Function Name: SPISL_SpiTransfer
* Description : SPI transfer
* in  : void
* out : None
* inout : None
* return  : void  
************************************************************************************************/
void SPISL_SpiTransfer (void)
{
    SPDR = 0xAA; 
}

Please read the sticky topics on top of the forum and present your code accordingly.

Which “slave interrupt” do you mean?

How exactly are both Megas connected?

Have you already tried the IDE master/slave examples?

Hi

I updated it, However I did not find the slave example in IDE itself though. THe two existing examples seems to be for the Master

Sorry, I seem to have confused I2C and SPI examples :frowning:

According to the data sheet the slave MISO should not be initialized as an output - it will become output automagically when the slave is selected.

The master SS has no special function because a master can address multiple slaves by arbitrary SS pins. Each pin connected to a slave SS should be initialized as output and deselect its slave (HIGH).

As your code does not show that behaviour, your code source may not be very reliable. Where did you get that code from?

Eventually your slave will work, somehow, when the master uses an IDE SPI master example.

Thanks I will try these changes.

I wanted to try to establish the communication myself for learning purposes so the code was written by me completely.

Hey it was still not working after the changes, however i found that the issue was with atmel studio. It was not setting the breakpoint at SPi interrupt. Once i fixed it it worked!

How to fix the breakpoint issue:

However only problem i found is that the SPDR is always giving the wrong value.