
Originally Posted by
tonton81
just make 2 dummy bytes maybe 0xFF, can be anything, then write them from the master before the actual payload. if you are overriding the pusr with bytes then that will work also
Hi tonton81,
I tried to use SPISlave_T4 as described above, but I have found it impossible to receive the data in correct order.
I can send/receive up to 3 bytes correctly, but then when I tried sending more data I got it 'mixed up'.
My working code is bellow:
Teensy 4.1 SPI SLAVE
main.cpp
Code:
#include <Arduino.h>
#include "TeensyThreads.h"
#include "SPISlave_T4.h"
SPISlave_T4<&SPI, SPI_8_BITS> mySPI;
volatile int distanceUSFL = 0;
volatile int distanceUSFR = 0;
volatile int distanceUSBL = 0;
volatile int distanceUSBR = 0;
void ultrasonicSensorFrontLeft(){
while (1) {
int incomingByte;
int distanceMSB;
int distanceLSB;
int distanceCRC;
int distanceUSFLT;
if (Serial1.available() > 0) {
incomingByte = Serial1.read();
if(incomingByte==255){
distanceMSB=Serial1.read();
distanceLSB=Serial1.read();
distanceCRC=Serial1.read();
distanceUSFLT=((distanceMSB*256)+distanceLSB)/10;
if(distanceUSFLT>19 && distanceUSFLT<201){
distanceUSFL=distanceUSFLT;
}
}
}
threads.yield();
}
}
void ultrasonicSensorFrontRight() {
while (1) {
int incomingByte;
int distanceMSB;
int distanceLSB;
int distanceCRC;
int distanceUSFRT;
if (Serial2.available() > 0) {
incomingByte = Serial2.read();
if(incomingByte==255){
distanceMSB=Serial2.read();
distanceLSB=Serial2.read();
distanceCRC=Serial2.read();
distanceUSFRT=((distanceMSB*256)+distanceLSB)/10;
if(distanceUSFRT>19 && distanceUSFRT<201){
distanceUSFR=distanceUSFRT;
}
}
}
threads.yield();
}
}
void ultrasonicSensorBackLeft(){
while (1) {
int incomingByte;
int distanceMSB;
int distanceLSB;
int distanceCRC;
int distanceUSBLT;
if (Serial2.available() > 0) {
incomingByte = Serial3.read();
if(incomingByte==255){
distanceMSB=Serial3.read();
distanceLSB=Serial3.read();
distanceCRC=Serial3.read();
distanceUSBLT=((distanceMSB*256)+distanceLSB)/10;
if(distanceUSBLT>19 && distanceUSBLT<201){
distanceUSBL=distanceUSBLT;
}
}
}
threads.yield();
}
}
void ultrasonicSensorBacktRight() {
while (1) {
int incomingByte;
int distanceMSB;
int distanceLSB;
int distanceCRC;
int distanceUSBRT;
if (Serial2.available() > 0) {
incomingByte = Serial4.read();
if(incomingByte==255){
distanceMSB=Serial4.read();
distanceLSB=Serial4.read();
distanceCRC=Serial4.read();
distanceUSBRT=((distanceMSB*256)+distanceLSB)/10;
if(distanceUSBRT>19 && distanceUSBRT<201){
distanceUSBR=distanceUSBRT;
}
}
}
threads.yield();
}
}
void myFunc() {
//Serial.println("START: ");
while ( mySPI.active() ) {
if (mySPI.available()) {
mySPI.pushr(1);
Serial.println(mySPI.popr());
mySPI.pushr(2);
Serial.println(mySPI.popr());
mySPI.pushr(3);
Serial.println(mySPI.popr());
}
}
//Serial.println("END");
}
void setup() {
Serial.begin(115200);
while (!Serial);
mySPI.begin();
mySPI.swapPins();
mySPI.onReceive(myFunc);
Serial.print("Begin...");
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);
Serial4.begin(9600);
threads.addThread(ultrasonicSensorFrontLeft);
threads.addThread(ultrasonicSensorFrontRight);
threads.addThread(ultrasonicSensorBackLeft);
threads.addThread(ultrasonicSensorBacktRight);
}
void loop() {
Serial.print("millis: "); Serial.println(millis());
delay(1000);
}
Raspberry PI 4 SPI MASTER
App.java
Code:
public static class Task_1 implements Runnable {
public void run() {
try
{
SPI.Init();
SPI.Refresh(true);
int ans=0;
SPI.SendByte(255);
SPI.SendByte(255);
while (true) {
SPI.SendByte(101);
ans=SPI.ReceiveByte();
System.out.println("---> " + ans + "\n");
SPI.SendByte(102);
ans=SPI.ReceiveByte();
System.out.println("---> " + ans + "\n");
SPI.SendByte(103);
ans=SPI.ReceiveByte();
System.out.println("---> " + ans + "\n");
System.out.println("--------------------------------------\n");
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
}
}
}
SPI.java
Code:
public static void Init() {
Busy=true;
int fd = Spi.wiringPiSPISetup(0, 4000000);
if (fd <= -1) {
System.out.println(" ==>> SPI SETUP FAILED");
Busy=false;
return;
}
Busy=false;
}
public static int ReceiveByte() throws InterruptedException {
Busy=true;
byte SPIbufer[] = new byte[1];
Spi.wiringPiSPIDataRW(0, SPIbufer, 1);
//Thread.sleep(5);
//System.out.println("<<<<... " + SPIbufer[0]);
Busy=false;
return (int)SPIbufer[0]&0xff;
}
public static void SendByte(int SPIByte) throws InterruptedException {
Busy=true;
byte SPIbufer[] = new byte[1];
SPIbufer[0] = (byte)SPIByte;
//System.out.println(">>>>... " + SPIbufer[0]);
Spi.wiringPiSPIDataRW(0, SPIbufer, 1);
//Thread.sleep(5);
Busy=false;
}
The above code gives the following output:

After adding a code that sends/receives 4th byte I got the following output:

Can you please advice on what the problem might be?
Best Regards
Marek