Arduino Nano Every - SPI Slave not working?

I have been trying to use my Arduino nano every as an SPI slave to replace my Uno.

The code runs fine on the Uno using SPCR, but Nano every doesn’t use SPCR, instead using CTRLA according to: This & the fact it is unable to compile when I tried with SPCR.

And so i tried to move the code over by learning how to use CTRLA with This, modified to change the built in led to an on state when it recieves any SPI signal (for testing)

ISR(SPI0_INT_vect)
{
receiveData = SPI0.DATA;
SPI0.DATA = writeData;
digitalWrite(LED_BUILTIN,HIGH);
SPI0.INTFLAGS = SPI_IF_bm; /* Clear the Interrupt flag by writing 1 /
}
void setup(void){ pinMode(LED_BUILTIN,OUTPUT); pinMode(10,INPUT); digitalWrite(LED_BUILTIN,LOW); }
void loop(void)
{
SPI0_init();
sei(); /
Enable Global Interrupts */
while(1){
;
}

But it does not seem to work, I’m not sure what I am doing wrong. Am I using the interrupt wrong? Am I writing to the register wrong? I do not have the know how to decifer it further. P.S. - I am certain the issue is not the signal being send because I am able to recieve it correctly on my arduino Uno. Any help is greatly appreciated.

1 Like

Hey BeepoBoppo!

I looked into your query about the Arduino Nano Every SPI Slave issue. I did find some relevant information that might help you with setting up the Nano Every as an SPI slave.

From the Arduino forum, I found a sample code snippet for an SPI slave setup on the Nano Every. This example demonstrates how to configure the SPI0.CTRLA register for slave mode and handle the SPI interrupt service routine. The code sets up the SPI in slave mode, configures the necessary pins, and includes an ISR for handling incoming SPI data.

Here’s a simplified version of the setup and ISR:

#include <Arduino.h>
// ... other includes ...

void setup() {
    // ... Serial initialization and other setup ...

    // SPI in slave mode
    SPI0.CTRLA = SPI_DORD_bm | SPI_ENABLE_bm & (~SPI_MASTER_bm);
    SPI0.INTCTRL = SPI_IE_bm;  // SPI Interrupt enable
    sei();  // Enable global interrupts

    // Set pin modes
    pinMode(MISO, OUTPUT);
    pinMode(MOSI, INPUT);
    pinMode(SCK, INPUT);
    pinMode(SS, INPUT);
}

// SPI interrupt routine
ISR(SPI0_INT_vect) {
    byte c = SPI0_DATA;
    // Echo Reply
    SPI0_DATA = c;
    // ... rest of the ISR ...
}

Cheesy regards, :cheese:

1 Like

Hia Cheese

I did try that method but I ran into the same troubles that the poster had. While it does compile, the interrupt function is never called. I have mine written to turn on the built in led in the interrupt function but it remains dark.

Love, Lactose :cheese:

#include <Arduino.h>
#include <avr/interrupt.h> //I added this to be sure this was available it's presence/absence makes no difference

// ... other includes ...

void setup() {
    // ... Serial initialization and other setup ...

    // SPI in slave mode
    SPI0.CTRLA = SPI_DORD_bm | SPI_ENABLE_bm & (~SPI_MASTER_bm);
    SPI0.INTCTRL = SPI_IE_bm;  // SPI Interrupt enable
    sei();  // Enable global interrupts

    // Set pin modes
    pinMode(MISO, OUTPUT);
    pinMode(MOSI, INPUT);
    pinMode(SCK, INPUT);
    pinMode(SS, INPUT);
	pinMode(LED_BUILTIN,OUTPUT);
	digitalWrite(LED_BUILTIN,LOW);
}
int a=0;

// SPI interrupt routine
ISR(SPI0_INT_vect) {
    byte c = SPI0_DATA;
    // Echo Reply
	digitalWrite(LED_BUILTIN,HIGH);
    SPI0_DATA = c;
	a=1;
    // ... rest of the ISR ...
}
void loop(){
}
1 Like

Hi BeepoBoppo,

Instead of using the Nano Every it might be worth using the same processor (and same code) from the Uno: Arduino Nano V3.2 | A000005 | Core Electronics Australia

Since this sounds super deep in the Arduino realm I’d pop a post up on their forums to see if you can get some help!
Liam

Hi BeepoBoppo
I am probably wrong here but I was under the impression that the NanoEvery only had 2 external interrupts. I notice you seem to have enabled or tried to enable global interrupts. Is this correct??
Cheers Bob

1 Like

I thought that by enabling global interrupts, that would mean every possible interrupt would be enabled, a sort of catch all to make sure the interrupt function had the best chance of executing.

Also, I have realized that pin 13 or CLK is also used to control the built in LED, so when I tried to receive feedback in the form of that, I was more or less disabling the clock of SPI. I’ll see if using a different metric yields better results.

P.S. I got lost along the way and may have SPI working on the Uno Rev4 which runs a 48mhz clock so i’ll keep tinkering and throw the results on here when I’m done.

1 Like

Hi BeepoBoppo,

Glad to hear you found some luck with another board! We’re keen to see what you make (and why you’ve gone the route of an SPI slave)

Liam

I just wanted a higher sample rate of audio. The video frame-rate can’t show it but it is better. Have a gander if you’re still interested…

1 Like