Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: SPI1 (MOSI1, MISO1, SCK1, CS1) Not Initializing on Teensy 4.1 + Audio Shield

  1. #1

    SPI1 (MOSI1, MISO1, SCK1, CS1) Not Initializing on Teensy 4.1 + Audio Shield

    Hello All!!! I'm new to the Teensy 4.1 and this forum!

    I wish to switch between the Teensy 4.1 on board SD slot on SPI2 (MOSI2, MISO2, SCK1, CS2), the Audio Shield on board SD slot on the first SPI (MOSI, MISO, SCK, CS) and add an external third SD card slot on SPI1 (MOSI1, MISO1, SCK1, CS1). (I'd like to use all three SD cards simultaneously, but I know that's probably too much to ask.)

    I can get the first two SD card slot reading perfectly. However, I cannot get the third external SD card to be recognized on SPI1 (MOSI1, MISO1, SCK1, CS1), as it will not initialize.

    For reference only, I've tried two different SD card readers linked here:

    https://www.adafruit.com/product/4682

    https://www.adafruit.com/product/254

    Here's my simple test source code and wiring:

    Code:
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    #define SDCARD_CS_PIN    0   //CS1 ---> CS Pin
    #define SDCARD_MISO_PIN  1   //MISO1 ---> DO Pin
    #define SDCARD_MOSI_PIN  26  //MOSI1 ---> DI Pin
    #define SDCARD_SCK_PIN   27  //SCK1 ---> CLK Pin
    
    void setup() {
      // put your setup code here, to run once:
    
      Serial.begin(9600);
    
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card!");
          delay(2000);
        }
      }
      Serial.println("Successfully access the SD card!");
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }
    My gut tells me I am either missing something simple, or it's some special functionality/requirement I am completely unaware of.

    Any thoughts on this and how to get this third SD slot working you be much appreciated!

    Thank you in advance for any comments provided!
    Last edited by ForYourHealth; 01-15-2022 at 11:03 PM.

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,337
    If you are using SPI1. Pins you need to use the SPI1 object not SPI

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,214
    First, make sure you have the latest Teensyduino (version 1.56). Versions older than 1.54 can't possibly use SPI1. In Arduino, click Help > About to check.

    You'll need to use the SdFat begin() function to access SPI1. Sadly, there aren't any really good examples. The closest to at least get started in File > Examples > SD > SdFat_Usage.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,214
    I've updated the SdFat_Usage example to show how to use SPI1.

    https://github.com/PaulStoffregen/SD...386cbaf054b37b

    Here is a ready to go copy.

    Code:
    /*
      SdFat usage from SD library
    
      Starting with Teensyduino 1.54, the SD library is a thin wrapper for SdFat.
    
      You can access the main SdFat filesystem with "SD.sdfs".  You may wish to
      use SD.sdfs.begin() to cause SdFat to access the SD card using faster
      drivers than the default.  You may also wish to open files as FsFile to
      gain access to SdFat's special file functions which are not available using
      the simpler SD File.
    
      This example shows some of the ways to select optimized SdFat drivers and
      how to use special file truncation and pre-allocation for optimized data
      logging.
    
      This example code is in the public domain.
    */
    #include <SD.h>
    
    // On Teensy 2.0, SdFat's files are "File32" rather than "FsFat"
    #ifdef __AVR__
    #define FsFile File32
    #endif
    
    void setup()
    {
      //Uncomment these lines for Teensy 3.x Audio Shield (Rev C)
      //SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
      //SPI.setSCK(14);  // Audio shield has SCK on pin 14
    
      Serial.begin(9600);
      while (!Serial); // wait for Arduino Serial Monitor
    
      Serial.print("Initializing SD card...");
      bool ok;
      const int chipSelect = 10;
    
      // Instead of the usual SD.begin(pin), you can access the underlying
      // SdFat library for much more control over how the SD card is
      // accessed.  Uncomment one of these, or craft your own if you wish
      // to use SdFat's many special features.
    
      // Faster SPI frequency.  16 MHz is default for longer / messy wiring.
      //ok = SD.sdfs.begin(SdSpiConfig(chipSelect, SHARED_SPI, SD_SCK_MHZ(24)));
    
      // Very slow SPI frequency.  May be useful for hardware with slow buffers.
      //ok = SD.sdfs.begin(SdSpiConfig(chipSelect, SHARED_SPI, SD_SCK_MHZ(4)));
    
      // Different SPI port (Teensy 4.1 SPI1 is MOSI:pin26, MISO:pin1, SCL:pin27)
      ok = SD.sdfs.begin(SdSpiConfig(chipSelect, SHARED_SPI, SD_SCK_MHZ(16), &SPI1));
    
      // SdFat offers DEDICATED_SPI optimation when no other SPI chips are
      // connected.  More CPU time is used and results may vary depending on
      // interrupts, but for many cases speed is much faster.
      //ok = SD.sdfs.begin(SdSpiConfig(chipSelect, DEDICATED_SPI, SD_SCK_MHZ(16)));
    
      // Access the built in SD card on Teensy 3.5, 3.6, 4.1 using FIFO
      //ok = SD.sdfs.begin(SdioConfig(FIFO_SDIO));
    
      // Access the built in SD card on Teensy 3.5, 3.6, 4.1 using DMA (maybe faster)
      //ok = SD.sdfs.begin(SdioConfig(DMA_SDIO));
    
      if (!ok) {
        Serial.println("initialization failed!");
        return;
      }
      Serial.println("initialization done.");
      Serial.println();
    
      // After the SD card is initialized, you can access is using the ordinary
      // SD library functions, regardless of whether it was initialized by
      // SD library SD.begin() or SdFat library SD.sdfs.begin().
      //
      Serial.println("Print directory using SD functions");
      File root = SD.open("/");
      while (true) {
        File entry = root.openNextFile();
        if (!entry) break; // no more files
        Serial.print(entry.name());
        if (entry.isDirectory()) {
          Serial.println("/");
        } else {
          printSpaces(40 - strlen(entry.name()));
          Serial.print("  ");
          Serial.println(entry.size(), DEC);
        }
        entry.close();
      }
    
      // You can also access the SD card with SdFat's functions
      //
      Serial.println();
      Serial.println("Print directory using SdFat ls() function");
      SD.sdfs.ls();
    
      // You can access files using SdFat which uses "FsFile" for open files
      // FsFile offers more capability than regular SD "File".  As shown in this
      // example, you can truncate tiles.  You can also pre-allocate a file on
      // the SD card (if it does not yet have any data, the reason we truncate
      // first).  Pre-allocation impoves the speed of writes within the already
      // allocated space while data logging or performing other small writes.
      //
      Serial.println();
      Serial.println("Writing to datalog.bin using SdFat functions");
      FsFile myfile = SD.sdfs.open("datalog.bin", O_WRITE | O_CREAT);
      unsigned int len = myfile.fileSize();
      Serial.print("datalog.bin started with ");
      Serial.print(len);
      Serial.println(" bytes");
      if (len > 0) {
        // reduce the file to zero if it already had data
        myfile.truncate();
      }
      if (myfile.preAllocate(40*1024*1024)) {
        Serial.print("  Allocate 40 megabytes for datalog.bin");
      } else {
        Serial.print("  unable to preallocate this file");
      }
      myfile.print("Just some test data written to the file (by SdFat functions)");
      myfile.write('\0'); // add a null byte to mark end of string
      myfile.close();
    
      // You can also use regular SD functions, even to access the same file.  Just
      // remember to close the SdFat FsFile before opening as a regular SD File.
      //
      Serial.println();
      Serial.println("Reading to datalog.bin using SD functions");
      File f = SD.open("datalog.bin");
      if (f) {
        char mybuffer[100];
        int index = 0;
        while (f.available()) {
          char c = f.read();
          mybuffer[index] = c;
          if (c == 0) break;  // end of string
          index = index + 1;
          if (index == 99) break; // buffer full
        }
        mybuffer[index] = 0;
        Serial.print("  Read from file: ");
        Serial.println(mybuffer);
      } else {
        Serial.println("unable to open datalog.bin :(");
      }
      f.close();
    
      // When mixing SD and SdFat file access, remember for writing that
      // SD defaults to appending if you open with FILE_WRITE.  You must
      // use FILE_WRITE_BEGIN if you wish to overwrite the file from the
      // start.  With SdFat, O_WRITE or O_RDWR starts overwriting from the
      // beginning.  You must add O_AT_END if you wish to appead.
    }
    
    void loop()
    {
      // nothing happens after setup finishes.
    }
    
    
    void printSpaces(int num) {
      for (int i = 0; i < num; i++) {
        Serial.print(" ");
      }
    }
    I tested this just now on a Teensy 4.1 with SD card connected to SPI1 (but using pin 10 for CS). It definitely does work.

    Click image for larger version. 

Name:	spi1.jpg 
Views:	14 
Size:	68.1 KB 
ID:	27233

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,214
    Quote Originally Posted by ForYourHealth View Post
    For reference only, I've tried two different SD card readers linked here:

    https://www.adafruit.com/product/4682

    https://www.adafruit.com/product/254
    The 4682 product should work fine with Teensy 4.1.

    The 254 one might need slower SPI clock, because of delay from its buffer chip.

  6. #6

    Thank you for the excellent advice @KurtE and @PaulStoffregen! Everything is working as expected after integrating your suggestions!

    And thank you again @PaulStoffregen for updating the SdFat_Usage example for SPI1.

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •