Skip to main content
6 of 6
Edited a clear picture

SD Library function doesnt work while using second SPI port with ESP32

i work on a group project with an ESP32. We have a lcd display and a sd card connected via the two SPI ports of the ESP32.

The card gets initialized with no problem using this code:

 bool EKG_recording::initialisation_sd() {

  SPIClass * hspi = new SPIClass(HSPI);

  if (!SD.begin(CS_Pin, *hspi))
  {
    Serial.println("No valid SD card!");
    return 0;
  }

  else if (SD.begin(CS_Pin, *hspi))
  {
    Serial.println("SD card initialized!");
    return 1;
  }
  else return 0;
}

One function i have a problem with is used to count every file on the SD card, simple enough right?

unsigned short EKG_recording::count_files_from_SD(File dir) {
  while (true)
  {
    File entry = dir.openNextFile();
    if (!entry)
    {
      // Serial.println("Anzahl der Daten");
      return counter;
    }
    counter++;
    entry.close();
  }
}

It once worked without a problem, then we integrated the two SPI ports and now i cant figure out why its not working. This part: File entry = dir.openNextFile(); returns false on the first call even though i am 100% sure there are 3 files on the SD card.

I call the function like this :

File root = SD.open("/"); EKG_recording_1.count_files_from_SD(root);

But why? Any help is very appreciated.

This is the begin function of the display. Since i didnt write it, i know next to nothing about it.

    void EKG_display_control::begin()
{
  // Backgroundlight off
  pinMode(TFT_LED, OUTPUT);
  disable_display_backlight();
  
  // Initialise Display
  tft.begin(40000000);
  touch.begin();
  tft.setRotation(ROTATION);

  layout_start_screen();
}

THE PINOUT:

The MC+SD Adapter