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

grovea

New member
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/commit/9912b57a0fa05de883ae8a0953386cbaf054b37b

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()
{
}
 
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.
 
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:

}
 
@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.
 
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.
 
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?
 
Back
Top