Arduino SPI Slave Program Issues

Hi, I am trying to upload this code to my Teensy, with then end goal of communicating with a Raspberry Pi over SPI, but when I try I get this error:

Arduino: 1.8.16 (Windows 10), TD: 1.55, Board: "Teensy 4.1, Serial, 600 MHz, Faster, US English"

spi:15: error: expected constructor, destructor, or type conversion before '(' token

ISR (SPI_STC_vect)

Below is the code:

#include <SPI.h>

void setup() {
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);

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

  // turn on interrupts

// SPI interrupt routine
  ISR (SPI_STC_vect)
  byte c = SPDR;

  SPDR = c+10;
}  // end of interrupt service routine (ISR) for SPI

void loop () { }

It looks like you're trying to compile AVR code for an ARM

And in your setup() routine in minimum the line


seems to be missing. This is required to intialize the SPI object (which in OOP is called a constructor). Unfortunately it is not possible to check what you want to achieve from the code snipet that you posted and in which line the error was detected by the compiler ...

Hi, this is the full code, its a very basic program to setup the board as an SPI slave, and process data received over the SPI bus.

ok, understood. With this sketch you only want to setup the Teensy as a slave device that answers with received value plus 10, correct?

The code looks ok for that. I copied the code into my Arduino 1.8.15 and it compiled successfully for Nano and UNO.

However, when I gave it a try for "ESP32 Dev Module" the same error message appeared. Plus several further error messages which I assume should be also visible in your respective IDE window?!?

Reason is that definitions/declarations for certain data are missing for the respective hardware (SPCR, SPE, 'class SPIClass' has no member named 'attachInterrupt',...).

With Windows OS you can find hardware depending definitions usually in a directory like this C:\Users\ [Your UserName] \AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7\avr\include\avr. However, you have to "dive" a little bit deeper into your microcontroller hardware if you want to create files like that ...

If you want to avoid this I recommend to have a look at the following page for Teensy which might solve your problem (hopefully :wink: ):

"begin" is NOT a constructor.
The constructor has the same name as the class.

You are right (of course :wink: ).

I looked up AVR SPI lib to see how it is implemented. Actually it does not have a SPI::SPI declaration in the library that does any initialisation.

The SPI instance created there is only developped for use as SPI-Master, the initialization of the SPI library for this purpose always requires an explicit call of SPI.begin() as far as I see it.

For SPI Slave mode the direct interference with registers and writing of an interrupt routine is required (as seen above). The include of the SPI lib just eases access to hardware specific declarations. Especially the ISR macro is again something related to the specific hardware that can only be solved by enablng the correct architecture in the development environment ... unless someone wants to do the job for his/her specific processor on his/her own...

No - it wouldn't have.

The class is called "SPIClass" - if it had a constructor, it would be "SPIClass::SPIClass".

"SPI" is simply an instance of "SPIClass"

You are more strict (and probably less misleading?) as a compiler :wink:
Did you stop at one error / warning or are there more?

Corrected: The Instantiation of the SPI object does not initialize the SPI library.
Corrected: SPIClass::SPIClass is not declared.

Do you agree with this wording?