Arduino Mega as SPI slave and continuous writing

Hi all,

I'm currently using an Arduino Mega as a SPI slave.
I want it to continuously write on MISO while the master is managing the clock. As the master doesn't pause between each byte, I need to be able to prepare the new value of SPDR register as the previous one is being sent.
My SPI interrupt is pretty simple:

ISR (SPI_STC_vect)
{
  SPDR = my_tab[++idx_rd_tab];
}

where my_tab is a 256 char table and idx_rd_tab is a byte.
When I execute this code, I get only the first element of the table. Then, I get only 0.
I'm wondering if the Arduino Mega is able to process data fast enough for this kind of application? Or is there something I'm doing wrong?

Thanks in advance,

HaroldD

I'm wondering if the Arduino Mega is able to process data fast enough for this kind of application?

What's the SPI clock speed of the SPI master?

Or is there something I'm doing wrong?

We don't know because we only see a tiny part of your code. Post the complete code and we may tell better.

Hi,

The SPI clock is 1Mhz.

I can't show you the whole code :confused:

But here is at least the setup function:

void setup(void)
{
  Serial.begin(57600);
  Serial1.begin(115200);
  Serial1.println("------------------------------"); 
  // Have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  pinMode(MOSI, INPUT);
  pinMode(SS, INPUT);
  pinMode(Attention, OUTPUT);
  digitalWrite(Attention, HIGH);
  pinMode(Reset, INPUT);
  pinMode(testPin, OUTPUT);
  digitalWrite(testPin, LOW);
  pinMode(SS_detect, INPUT);
  
  // Turn on SPI in slave mode
  SPCR |= _BV(SPE);
 
  // Turn on interrupt for Reset line
  attachInterrupt(digitalPinToInterrupt(Reset), m_reset, RISING);

  // Turn in interrupt on slave select
  // (Attached a second input for SS as we can't fix an IT on basic one)
  attachInterrupt(digitalPinToInterrupt(SS_detect), SS_detect_IT, RISING);

  // Delay a bit to let some time for initialization
  delay(1000);
  
  // Turn on interrupts for SPI
  SPI.attachInterrupt();
}

Then, most of the work of the main loop is filling "my_tab". I've checked that the table is fill fast enough by printing the values of each cell of my_tab right before sending it to SPDR.

Let me know if you need more information!

Thanks!

I can't show you the whole code

Why?

Let me know if you need more information!

Yes, at least the variable definitions and the interrupt handlers. And I repeat: without having seen the complete code I cannot tell in which part of the code the error lies.