Use SPI to transfer data between arduino nano iot 33

Hello!
I have spent a lot of time reading many posts, but I have not found an answer to my question...
I'd like to transfer data between two arduino nano 33 iot using SPI; many topics talk about this, but not specifically for these boards :frowning:

I used default MOSI, MISO and SCK pins (11, 12, 13) as in pinout for both sides.

SS pin is 8 master side, 9 slave side (to be able to use attachInterrupt).

Here are basic tests I've been running:

Master:

#include<SPI.h>

#define SS_PIN 8

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }
  Serial.println("OK master");
  SPI.begin();
  pinMode(SS_PIN, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  digitalWrite(SS_PIN, HIGH);
}
void loop() {
  String test = "";
  while(test == ""){
    test = Serial.readString();
  }
  if(test == "test"){

  SPI.beginTransaction(SPISettings(200000, MSBFIRST, SPI_MODE0));
  digitalWrite(SS_PIN, LOW); 

  char c;
  for (const char * p = "Hello, world\r"; c =*p ; p++) {
    byte toto = SPI.transfer(c);
  }

  digitalWrite(SS_PIN, HIGH);
  SPI.endTransaction();
  }
}

Slave:

#include <SPI.h>
#include <Arduino.h>

volatile byte indx;
volatile boolean process;

#define SS_PIN 9

void interruptCb() {
  Serial.println("receiving");
  process = true;
}


void setup() {
  Serial.begin(115200);
  SPI.begin();
  pinMode(MOSI, INPUT);
  pinMode(MISO, OUTPUT);
  pinMode(SS_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(SS_PIN), interruptCb, FALLING);
}

void loop() {
  indx = 0;
  if (process == true) {
    while(indx < 50) { //In case where data would be delayed. Probably should not exist
      byte test = SPI.transfer(0);
      Serial.println(test);
      indx++;
    }
    process = false;
  }
}

I know that slave implementation is not usual, as I should use SPDR to catch current value and put this one in a buffer, but arduino nano 33 iot does not have this value as in this post. You can find linked article in web archive , but it implies to play with SERCOM directly.

Usually I find answers on this forum, and after some tries I find a solution, but I've been stuck on this one for four days (I'm on holidays so it's really four days :sweat_smile:).

I hope someone will have the knowledge to help me!

Thank you!

Hardware UART might be a better choice.

I thought about this solution too, but it seems that UART is slower in transmission... I'd like to eventually send pictures this way... Do you think it is sufficient?

Yes, a SPI interface can generally be clocked faster than an UART. But, as you're finding, there are extra complexities.

Of course, SPDR is a register name for AVR-architectures, so there's no reason to expect it in the SAMD21 processor on the Nano 33 IOT board.

The standard Arduino SPI library for your board doesn't seem to support SPI slave mode.
So ....

that's likely what you need to do. Start with reading SPI section in the SAMD21 datasheet.

Thank you for your answers! I'll try first with UART and see if it works for what I need to do, and try to implement SPI in second time!

I let SAM D21 datasheet here for those who would want to give a try. (page 448 for SPI)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.