2 Teensy 3.5, SPI Master/Slave Communication

Gripporillat

Well-known member
Hi everybody!

I'm experimenting with SPI and trying to make two Teensy 3.5 communicate with each other. Im using the tonton81's TSPISlave library. But somehow I just can't make it work.

This is my wiring:

IMG_20200501_132920.jpg

Master -> Slave
Vin Vin
GND GND
10 10
11 12
12 11
13 13

Master Code:
Code:
#include <SPI.h>  // include the SPI library:

uint8_t zero = 0;
const int slaveSelectPin = 10;

void setup() {
  Serial.begin(115200);
  delay(1000);
  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  // initialize SPI:
  SPI.begin(); 
}

void loop() {
  receiveShit();
  delay(1000);
}

int receiveShit() {
  uint8_t data;
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin,LOW);
  //  send in the address and value via SPI:
  data = SPI.transfer(zero);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin,HIGH); 
  Serial.println(data);
}


Slave Code:
Code:
#include "TSPISlave.h"

uint8_t miso = 12;
uint8_t mosi = 11;
uint8_t sck = 13;
uint8_t cs = 10;
uint8_t spimode = 8;
uint8_t counter = 0;

TSPISlave mySPI = TSPISlave(SPI, miso, mosi, sck, cs, spimode);

void setup() {
  mySPI.onReceive(myFunc);
}

void loop() {
}

void myFunc() {
  while ( mySPI.active() ) {
    if (mySPI.available()) {
      

      
      mySPI.pushr(counter);
      if (counter == 255) {
        counter = 0;
      } else {
        counter = counter++;
      }
    }
  }
}

The only output I get is "0". Any help would be highly appreciated.

Thanks in advance!
 
I've changed the code and now it works... kind of. The master receives increasing values but somehow most of them are missing. As seen here:

spi.gif

New master code:

Code:
#include <SPI.h>  // include the SPI library:
uint8_t counter = 0;
uint8_t zero = 0;
const int slaveSelectPin = 10;

void setup() {
  Serial.begin(115200);
  delay(1000);
  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  // initialize SPI:
  SPI.begin(); 
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
}

void loop() {
  receiveShit();
}

int receiveShit() {
  uint8_t data;
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin,LOW);
  
  //  send in the address and value via SPI:
  data = SPI.transfer(zero);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin,HIGH); 
  Serial.print(data);
  Serial.print(" .. ");
  counter = counter + 1;
  if (counter == 19) {
    Serial.println("");
    counter = 0;
  }
}

New slave code:

Code:
#include "TSPISlave.h"

uint8_t miso = 12;
uint8_t mosi = 11;
uint8_t sck = 13;
uint8_t cs = 10;
uint8_t spimode = 8;
  uint8_t counter = 0;


TSPISlave mySPI = TSPISlave(SPI, miso, mosi, sck, cs, spimode);

void setup() {
  

  mySPI.onReceive(myFunc);
  
} 

void loop() {
  
}

void myFunc() {
  while ( mySPI.active() ) {
    if (mySPI.available()) {
      mySPI.pushr(counter);
      
      if (counter == 255) {
        counter = 0;
      } else {
        counter = counter+1;
      }
      
    }
  }

}


Edit: The higher the baud rate the less values get skipped. This is with 20 Mhz:

spi2.gif

I don't understand it at all :)
 
Last edited:
Back
Top