Error, expected constructor, destructor, or type conversion before '('

Hi all!

I'm receiving this error message and I've looked into a bit, and I'm not sure why I am receiving the message.

The code I am trying to use is Nick Gammon's SPI examples found Here

Here's the specific code I am using:

// http://www.gammon.com.au/spi
// Written by Nick Gammon
// February 2011


#include <SPI.h>

char buf [100];
volatile byte pos;
volatile boolean process_it;

void setup (void)
{
  Serial.begin (115200);   // debugging

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

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

  // get ready for an interrupt
  pos = 0;   // buffer empty
  process_it = false;

  // now turn on interrupts
  SPI.attachInterrupt();

}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;  // grab byte from SPI Data Register

  // add to buffer if room
  if (pos < (sizeof (buf) - 1))
    buf [pos++] = c;

  // example: newline means time to process buffer
  if (c == '\n')
    process_it = true;

}  // end of interrupt routine SPI_STC_vect

// main loop - wait for flag set in interrupt routine
void loop (void)
{
  if (process_it)
  {
    buf [pos] = 0;
    Serial.println (buf);
    pos = 0;
    process_it = false;
  }  // end of flag set

}  // end of loop

I'm using arduino IDE 1.8.5 and the code compiles just fine for an Arduino Uno. The problems comes when I switch to an Arduino Zero (native port) board in the IDE. When I switch to that board, the error message is:

SPI_test_Slave:33: error: expected constructor, destructor, or type conversion before '(' token

ISR (SPI_STC_vect)

^

C:\Documents and Settings\User\My Documents\Arduino\SPI_test_Slave\SPI_test_Slave.ino: In function 'void setup()':

SPI_test_Slave:17: error: 'SPCR' was not declared in this scope

SPCR |= bit (SPE);

^~~~

C:\Documents and Settings\User\My Documents\Arduino\SPI_test_Slave\SPI_test_Slave.ino:17:3: note: suggested alternative: 'SPI'

SPCR |= bit (SPE);

^~~~

SPI

In file included from sketch\SPI_test_Slave.ino.cpp:1:0:

SPI_test_Slave:17: error: 'SPE' was not declared in this scope

SPCR |= bit (SPE);

^

C:\Documents and Settings\User\Local Settings\Application Data\Arduino15\packages\arduino\hardware\samd\1.8.6\cores\arduino/Arduino.h:120:25: note: in definition of macro 'bit'

#define bit(b) (1UL << (b))

^

C:\Documents and Settings\User\My Documents\Arduino\SPI_test_Slave\SPI_test_Slave.ino:17:16: note: suggested alternative: 'SPI'

SPCR |= bit (SPE);

^

C:\Documents and Settings\User\Local Settings\Application Data\Arduino15\packages\arduino\hardware\samd\1.8.6\cores\arduino/Arduino.h:120:25: note: in definition of macro 'bit'

#define bit(b) (1UL << (b))

^

C:\Documents and Settings\User\My Documents\Arduino\SPI_test_Slave\SPI_test_Slave.ino: At global scope:

SPI_test_Slave:33: error: expected constructor, destructor, or type conversion before '(' token

ISR (SPI_STC_vect)

^

exit status 1
expected constructor, destructor, or type conversion before '(' token

I've looked into this error message, but it appears to not be from the usual causes, plus there's the fact that it compiles fine for an Uno, but not a Zero that has me stumped at the moment.

Any insight would be most welcomed!

Randy

You are doing SPI directly using registers/values instead of using the arduino Library. That's cool, but it means you loose the compatibility between boards. All these names (SPE, SPCR, ISR for ex) are specific to each MCU, and the arduino Uno has an 8-bit AVR MCU, while the Zero has a 32-bit Cortex, completely different.

Point is: either you use the arduino lib SPI, then it should do the compatibility for you, or you use direct MCU code (using the registers like your do), but then you need to write specific code for each MCU/board :slight_smile:

The tutorial you're following was written for AVR specifically. Driving the peripherals directly by writing to their control registers etc. is very specific to the microcontroller you're using. The declaration of interrupt service routines differs between platforms as well.

You'll have to find a tutorial for the SAMD21 and read the relevant section in the datasheet.
Alternatively, just use the SPI library provided by the Arduino Core: ArduinoCore-samd/SPI.h at master · arduino/ArduinoCore-samd · GitHub

Pieter

Hey, thanks @ToneArt & @PieterP

Your advice clears things up for me. I read 2 tutorials on SPI communication, the first one was very similar to Nick Gammons, and the 2nd one was Nick's.

So now I have a clear direction in what I should be looking for.

Thanks!
Randy