Alternate SPI works on Teensy 3.2 but not 4.1

Status
Not open for further replies.

Vincefarq

Member
Hi folks,

I'm having a weird issue with my Teensy 4.1 and an Adafruit Airlift. I need to use the alternate SPI pins for the Airlift because of a hardware conflict. I'm using the SPI library and had success testing on my Teensy 3.2 but when I subbed in my 4.1 (with its different set of alternates) it does not seem to respond. It completely ignores the SPI reassignments and runs on the default SPI pins instead.

Could someone please tell me if I'm missing something obvious? I've checked and triple-checked the pins and the code. I'm suspecting something's up with WiFiNINA at this point, but need a sanity check.

I'm running teensyduino 1.52 and Arduino 1.8.13. I've even tried this on older builds of Teensyduino and Arduino

Code:
#include <SPI.h>
#include <WiFiNINA.h>

// Configure the pins used for the ESP32 connection
// TEENSYDUINO
  #define SPIWIFI       SPI  // The SPI port
  #define SPIWIFI_SS     5 // 15   // Chip select pin def:5
  #define ESP32_RESETN   6 // 18   // Reset pin def:6
  #define SPIWIFI_ACK    9 // 16   // a.k.a BUSY or READY pin def:9
  #define ESP32_GPIO0   -1

void setup() {
  // SPI.setMOSI(7); SPI.setMISO(8); SPI.setSCK(14); SPI.begin(); // teensy 3.2 ?? it works!!!!
  SPI.setMOSI(26); SPI.setMISO(1); SPI.setSCK(27); SPI.begin(); // teensy 4.1 ?? NOPE :(
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("WiFi Scanning test");

  // Set up the pins!
  WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);

  // check for the WiFi module:
  while (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    delay(1000);
  }

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  printMacAddress(mac);
}

void loop() {
  // scan for existing networks:
  Serial.println("Scanning available networks...");
  listNetworks();
  delay(10000);
}

void listNetworks() {
  // scan for nearby networks:
  Serial.println("** Scan Networks **");
  int numSsid = WiFi.scanNetworks();
  if (numSsid == -1) {
    Serial.println("Couldn't get a wifi connection");
    while (true);
  }

  // print the list of networks seen:
  Serial.print("number of available networks:");
  Serial.println(numSsid);

  // print the network number and name for each network found:
  for (int thisNet = 0; thisNet < numSsid; thisNet++) {
    Serial.println(WiFi.SSID(thisNet));
  }
}



void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      Serial.print("0");
    }
    Serial.print(mac[i], HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}
 
Teensy 4.1 doesn't have alternate pins for SPI - it has a second SPI1 Bus as indicated on the card.

Those are indicated by the #1 after the labels: MOSI1, MOSI1, SCK1 that have to be used as a group. Versus those other same labels without the #1 attached.
 
Teensy 4.1 doesn't have alternate pins for SPI - it has a second SPI1 Bus as indicated on the card.

Those are indicated by the #1 after the labels: MOSI1, MOSI1, SCK1 that have to be used as a group. Versus those other same labels without the #1 attached.

Well technically, there is one alternate pin (MISO1 is normally 1, but it can be pin 39 if you use the SPI1.setMISO (39) call). But the alternate pins like the Teensy 3.x/LC have are not available.
 
Well technically, there is one alternate pin (MISO1 is normally 1, but it can be pin 39 if you use the SPI1.setMISO (39) call). But the alternate pins like the Teensy 3.x/LC have are not available.

Indeed SPI1 technically has that alternate pin - but not SPI :)
 
Well technically, there is one alternate pin (MISO1 is normally 1, but it can be pin 39 if you use the SPI1.setMISO (39) call). But the alternate pins like the Teensy 3.x/LC have are not available.

And if we want to be complete: SPI2 has alternate pins as well. However they are either on the SDCard pins or the bottom extra memory pins. So they are not terribly easy to access.
 
Aha! Thanks so much for clearing that up for me. I just relabeled SPI to SPI1 and my code has moved to the alternate SPI BUS.

Reposting my corrected code so that others may see how this works in practice:

I really appreciate this help and guidance, folks!


Code:
#include <SPI.h>
#include <WiFiNINA.h>

// Configure the pins used for the ESP32 connection
// TEENSYDUINO
  #define SPIWIFI       SPI[B]1[/B]  // The SPI port [B]CORRECTED TO USE ALT SPI BUS[/B]
  #define SPIWIFI_SS     5 // 15   // Chip select pin def:5
  #define ESP32_RESETN   6 // 18   // Reset pin def:6
  #define SPIWIFI_ACK    9 // 16   // a.k.a BUSY or READY pin def:9
  #define ESP32_GPIO0   -1

void setup() {
  // SPI.setMOSI(7); SPI.setMISO(8); SPI.setSCK(14); SPI.begin(); // teensy 3.2 ?? it works!!!!
  SPI[B]1[/B].setMOSI(26); SPI[B]1[/B].setMISO(1); SPI[B]1[/B].setSCK(27); SPI[B]1[/B].begin(); // teensy 4.1 -- [B]CORRECTED TO USE ALT SPI BUS[/B]
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("WiFi Scanning test");

  // Set up the pins!
  WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);

  // check for the WiFi module:
  while (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    delay(1000);
  }

  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  printMacAddress(mac);
}

void loop() {
  // scan for existing networks:
  Serial.println("Scanning available networks...");
  listNetworks();
  delay(10000);
}

void listNetworks() {
  // scan for nearby networks:
  Serial.println("** Scan Networks **");
  int numSsid = WiFi.scanNetworks();
  if (numSsid == -1) {
    Serial.println("Couldn't get a wifi connection");
    while (true);
  }

  // print the list of networks seen:
  Serial.print("number of available networks:");
  Serial.println(numSsid);

  // print the network number and name for each network found:
  for (int thisNet = 0; thisNet < numSsid; thisNet++) {
    Serial.println(WiFi.SSID(thisNet));
  }
}



void printMacAddress(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      Serial.print("0");
    }
    Serial.print(mac[i], HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}
 
Status
Not open for further replies.
Back
Top