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)
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.
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.
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
#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(){
}
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
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.