Trying to get two Argons to communicate via SPI1, one is master, one is slave


#1

I am unable to get the slave to respond properly. Any other eyes on this that might see the issue would be greatly appreciated. :slight_smile:

Slave code

SYSTEM_MODE(MANUAL);

uint8_t globalState = 0;

byte sending[1];
byte receive[1];

void onSend(uint8_t state) {
   if (state) {
     globalState = 1;
   }
}

void setup() {
  Serial.begin();
  SPI1.onSelect(onSend);
  SPI1.setClockSpeed(8, MHZ);
  SPI1.begin(SPI_MODE_SLAVE, A5);
  pinMode(A5, INPUT);
}

void loop() {
  if (globalState == 1) {
    globalState = 0;
    Serial.println("READING...");
    SPI1.transfer(sending, receive, 1, NULL);
    Serial.println(receive[0], HEX);
    if (receive[0] == 0) {
      Serial.println("LOW");
    } else {
      Serial.println("HIGH");
    }
  }
}

Master code

SYSTEM_MODE(MANUAL);

void setup() {
//  pinMode(A5, OUTPUT);
  SPI1.setClockSpeed(8, MHZ);
  SPI1.begin(SPI_MODE_MASTER);
}

void loop() {
  digitalWrite(A5, LOW);
  SPI1.transfer(0x01);
  digitalWrite(A5, HIGH);
  delay(1000);
  digitalWrite(A5, LOW);
  SPI1.transfer(0x01);
  digitalWrite(A5, HIGH);
  delay(1000);
}

The wiring is D2 -> D2, D3 -> D3, D4->D4 and A5->A5, both Argons are on a common ground


#2

You don’t want slave mode on the master.
You “master” code looks very much like slave code :wink:


#3

It is a good idea to have pull-ups (100K to 3V3) on everything but the clock. Also, how long are the wires? SPI really needs to be on a PCB probably maximum 15cm if wires. The comments from @Scruffr - agreed you have master as SPI_MODE_SLAVE!


#4

oops, updated. That’s what I get for rushing. :slight_smile:


#5

so using jumper wires where both argons are on the same solderless breadboard.


#6

So it appears that the following are not equivalent.

    byte sending[1];
    byte receive[1];
    SPI1.transfer(sending, receive, 1, NULL);
    digitalWrite(D6, receive[0]);
   byte value = SPI1.transfer(0);
   digitalWrite(D6, value);

#7

I think you really need some pull-up resistors as I described.

Why are you using SPI_SS/A5 with SPI1? Have you tried it using D5?

Is there a reason why you are using SPI1 - have you tried using SPI?

Can you try with sending a byte value from master to slave and seeing if that works first.


#8

SS is an entirely free choice since with multiple devices on the same bus you must choose different ones anyway.

Because SPI does not support slave mode on Gen3 devices.

While you can try that, SPI shouldnot require pull-ups - it’s not I2C. If they are needed I’d rather suspect an issue with the SPI implementation.


#9

I believe, at this point, that this is a bug and I will submit.


#10

SS is an entirely free choice since with multiple devices on the same bus you must choose different ones anyway.

I understand that - just suggesting some changes.

Because SPI does not support slave mode on Gen3 devices.

On Gen 3 devices (Argon, Boron, and Xenon), SPI slave can only be used on SPI1.

Mea Culpa. Thanks for pointing that out, clearly I wasn’t aware.

While you can try that, SPI shouldnot require pull-ups - it’s not I2C. If they are needed I’d rather suspect an issue with the SPI implementation.

Understand your point, I have been told that best practice is to implement pull-ups to ensure speed of bus. Clearly given the short wires used here this isn’t the issue.