Teensy 4.1 slave mode on SPI1

EricG

New member
Hello,

I would like to use a Teensy 4.1 in SPI slave.
I want to use the SPI port appearing on pins 26, 27, 38 and 39 which must correspond to SPI1
on the other hand I cannot understand how to adapt the SPIslave_T4 or SPI_MSTransfer_T4 libraries for use of SP1 on the recovered pins.
do you have any tips or hints on this?

Thanks
 
Sorry I have not played much with his library, but guessing:

Like in example Master.ino
Code:
SPI_MSTransfer_MASTER<&SPI, 10, 0x1234> mySPI1234;
SPI_MSTransfer_MASTER<&SPI, 10, 0x4567> mySPI4567;

void setup() {
  Serial.begin(115200);
  SPI.begin();
  pinMode(10, OUTPUT);
  digitalWrite(10, 1);
  mySPI1234.begin();
  mySPI4567.begin();
}

Code:
SPI_MSTransfer_MASTER<&SPI1, 38, 0x1234> mySPI1234;
SPI_MSTransfer_MASTER<&SPI1, 38, 0x4567> mySPI4567;

void setup() {
  Serial.begin(115200);
  SPI1.begin();
  pinMode(38, OUTPUT);
  digitalWrite(38, 1);
  SPI1.setMISO(39);
  mySPI1234.begin();
  mySPI4567.begin();
}

But the issue is that 39 is not the default MISO pin for SPI1, pin 1 is.
Not sure if this would work properly with his library, but it is what I would try
 
Thanks for answer
but this example is for Master SPI, i want Slave mode

I tried to transpose it in my code like this :

Code:
SPI_MSTransfer_T4<&SPI1, 0x1234> mySPI;

void setup() {
  Serial.println("Setup Begin ...");
  pinMode(ledpin, OUTPUT); //pin 13 to let us know if we are receiving any data
  digitalWrite(ledpin, LOW); //turn LED off
  Serial.begin(250000); //USB serial port for monitoring/debugging
    if(CommunicationMode == 2) {
      
      SPI1.begin();  
      pinMode(38, OUTPUT);
      digitalWrite(38, 1);
      SPI1.setMISO(39);
        
      mySPI.begin();
     
    }
}

If i try like this my teensy dont run and if i delete mySPI.begin(); it run but no SPI datas received

another idea ?
 
Code:
#include "SPISlave_T4.h"
SPISlave_T4<&SPI1, SPI_8_BITS> mySPI;

void setup() {
    Serial.begin(115200);
    while (!Serial);
    mySPI.begin();
    //    mySPI.onReceive(myFunc);
}

void loop() {
    Serial.print("millis: "); Serial.println(millis());
    delay(1000);
}

void myFunc() {
    Serial.println("START: ");
    uint8_t arr[] = { 3, 2, 8, 3, 10, 11, 33, 13, 14 };
    uint8_t i = 0;
    while (mySPI.active()) {
        if (mySPI.available()) {
            if (i++ > sizeof(arr)) i = 0;
            mySPI.pushr(arr[i]);
            Serial.print("VALUE: ");
            Serial.println(mySPI.popr());
        }
    }
    Serial.println("END");
}
I just had a look at the example code shown on GitHub.

Notice the Second line :- SPISlave_T4<&SPI, SPI_8_BITS> mySPI;

If you replace the &SPI with &SPI1 it compiles and links.

Give it a try and see if it works for you.
 
Last edited:
i try it again but no data received
no bug with &SPI1 on second line.
i also delele // on line // mySPI.onReceive(myFunc);

but never see START on serial monitor.
i dont know if my problem is on my master and slave code or if the library dont work properly

i need to ask to @Tonton81
 
i try it again but no data received
no bug with &SPI1 on second line.
i also delele // on line // mySPI.onReceive(myFunc);

but never see START on serial monitor.
i dont know if my problem is on my master and slave code or if the library dont work properly

i need to ask to @Tonton81


Hi EricG,

Did you get this figured out? I'd find this helpful too.
Regards
Doug
 
Back
Top