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

Thread: Using Teensy4.1 with internal and external SD card. How can I select the cards ?

  1. #1
    Junior Member
    Join Date
    Jan 2023
    Posts
    1

    Using Teensy4.1 with internal and external SD card. How can I select the cards ?

    Hello,
    I would like to connect the internal SD card and a second (external) SD card to a Teensy4.1 (CS1 = Pin0 / MISO1 = Pin1 / MOSI1 = Pin26 / SCK1 = Pin27). Both SD-cards can be initialised correctly. My question is now: How can I select which SD-card I write to?
    Thanks for your help and suggestions in advance.


    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>

    int SD_chipSelect = 0;

    void setup()
    {
    // Initialisation internal SD-card
    pinMode(BUILTIN_SDCARD, OUTPUT);

    Serial.println();
    Serial.print("Initialisation internal SD-card.");

    if (SD.begin(BUILTIN_SDCARD) == false) //
    {
    Serial.println("Fault !");
    Serial.println();
    }
    else
    {
    Serial.print("Successful");
    Serial.println();
    }

    // Initialisation external SD-card
    Serial.println();
    Serial.print("Initialisation external SD-card.");

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

    if (SD.sdfs.begin(SdSpiConfig(SD_chipSelect, SHARED_SPI, SD_SCK_MHZ(16), &SPI1))== false) //
    {
    Serial.println("Fault !");
    Serial.println();
    }
    else
    {
    Serial.print("Successful");
    Serial.println();
    }
    } // end setup

    void loop()
    {
    }

  2. #2
    Hello, do you have the sollution? I have tha same task.
    The problem is, i think, that the library does not provide a class, but a ready-made object SD.

  3. #3
    I thinki founf it
    Code:
    #include <SD.h>
    
    SDClass sd2;
    
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      while (!Serial); // wait for Arduino Serial Monitor
    
      Serial.println("Start");
      bool ok;
      const int chipSelect = 43;
    
    
      if (!SD.begin(BUILTIN_SDCARD)) {
        Serial.println("SD Fail");
      }
      else {
        Serial.println("SD OK");
      }
    
      ok = sd2.sdfs.begin(SdSpiConfig(chipSelect, SHARED_SPI, SD_SCK_MHZ(16), &SPI2));
      if (!ok) {
        Serial.println("SD2 Fail");
      }
      Serial.println("SD2 OK");
    
      File file1 = SD.open("test.txt", FILE_READ);
      if (!file1) {
        Serial.println("SD File Fail");
      }
      else {
        Serial.println("SD File OK");
      }
      File file2 = sd2.open("test.txt", FILE_WRITE);
      if (!file2) {
        Serial.println("SD2 File Fail");
      }
      else {
        Serial.println("SD2 File OK");
      }
      
      while (file1.available()) {
        file2.write(file1.read());
      }
    
      file1.close();
      file2.close();
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    11,462
    Yes, that looks correct.

  5. #5
    @KurtE, is it possible to use spi2 for external sd card with buildin sd card on teensy 4.1? I tryed to use it on teensy 3.5 and it worked correct (i hope))), but with 4.1 i dont understand. On pinout diagramm i see, that builin sd use spi2.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,694
    Quote Originally Posted by gonzales View Post
    On pinout diagramm i see, that builin sd use spi2.
    Built in SD card on Teensy 4.1 uses SDIO. It does not use SPI2. Those pins do also have SPI2 function, but SPI2 alternate function is not assigned to the correct pins. Only SDIO can be used for the build in SD card on Teensy 4.1.

  7. #7
    Quote Originally Posted by PaulStoffregen View Post
    Built in SD card on Teensy 4.1 uses SDIO. It does not use SPI2. Those pins do also have SPI2 function, but SPI2 alternate function is not assigned to the correct pins. Only SDIO can be used for the build in SD card on Teensy 4.1.
    So, together it will work, am i right?

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,694
    Should work. There is certainly no hardware resource conflict.

    But to access the SPI2 signals, you will need to solder wires to the pins normally used for PSRAM. It can be done, as you can see in the photos when I tried it some time ago:

    https://forum.pjrc.com/threads/60954...l=1#post303782

  9. #9
    Quote Originally Posted by PaulStoffregen View Post
    Should work. There is certainly no hardware resource conflict.

    But to access the SPI2 signals, you will need to solder wires to the pins normally used for PSRAM. It can be done, as you can see in the photos when I tried it some time ago:

    https://forum.pjrc.com/threads/60954...l=1#post303782
    Thanks a lot, i saw this post, i will try it

Posting Permissions

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