Skip to content

ESP32-S2 SD card SPI mode on custom pins fails to mount? #5676

@Lawrence-RedFern

Description

@Lawrence-RedFern

Hardware:

Board: ESP32-S2 Custom Board
Core Installation version: 2.0.0
IDE name: Arduino IDE/Visual Studio
Flash Frequency: 80MHz
PSRAM enabled: No
Upload Speed: 921600
Computer OS: Windows 10

Description:

I am trying to run the SD Card example on my ESP32-S2 custom board with custom assigned pins, but it does not seem to work. I have tried multiple variations of setting up the SPI but the SD card always fails to mount. Using SPI2/HSPI causes the program to hang at SPI.begin(), so I am only able to use SPI3.

I have tried doing:

SPIClass * sd_spi = NULL;
sd_spi = new SPIClass(3);
sd_spi->begin(PIN_SPI_SD_CLK, PIN_SPI_SD_MISO, PIN_SPI_SD_MOSI, -1);
pinMode(PIN_SPI_SD_CS, OUTPUT);
SPIClass * sd_spi = NULL;
sd_spi = new SPIClass(3);
sd_spi->begin(PIN_SPI_SD_CLK, PIN_SPI_SD_MISO, PIN_SPI_SD_MOSI, PIN_SPI_SD_CS);
SPIClass sd_spi = SPIClass(3);
sd_spi.begin(PIN_SPI_SD_CLK, PIN_SPI_SD_MISO, PIN_SPI_SD_MOSI, PIN_SPI_SD_CS);
SPIClass sd_spi = SPIClass(3);
sd_spi.begin(PIN_SPI_SD_CLK, PIN_SPI_SD_MISO, PIN_SPI_SD_MOSI, -1);
pinMode(PIN_SPI_SD_CS, OUTPUT);

Here is the whole sketch. I can see in the data sheet that SPI2 and SPI3 can be assigned to any GPIO pins, so I'm a little confused as to why these are not working. This sketch always returns from the line if (!SD.begin(PIN_SPI_SD_CS, sd_spi)), saying "Card Mount Failed". Upon entering this function I can debug and see that it is a mounting error, not an initialising error.

Any help on this would be appreciated.

#include "FS.h"
#include "SD.h"
#include "SPI.h"

#define PIN_SPI_SD_CS	        20
#define PIN_SPI_SD_MOSI	21
#define PIN_SPI_SD_MISO	11
#define PIN_SPI_SD_CLK	        26

SPIClass sd_spi = SPIClass(3);
// SPIClass sd_spi = SPIClass(2);
// SPIClass sd_spi = NULL;

void setup() {
    Serial.begin(115200);
    Serial.println(F("SD Card Test"));
    
    sd_spi.begin(PIN_SPI_SD_CLK, PIN_SPI_SD_MISO, PIN_SPI_SD_MOSI, -1);
    pinMode(PIN_SPI_SD_CS, OUTPUT);
    Serial.println(F("SD SPI begin"));

    if (!SD.begin(PIN_SPI_SD_CS, sd_spi))
    {
        Serial.println("Card Mount Failed");
        return;
    }
    uint8_t cardType = SD.cardType();

    if (cardType == CARD_NONE) {
        Serial.println("No SD card attached");
        return;
    }

    Serial.print("SD Card Type: ");
    if (cardType == CARD_MMC) {
        Serial.println("MMC");
    }
    else if (cardType == CARD_SD) {
        Serial.println("SDSC");
    }
    else if (cardType == CARD_SDHC) {
        Serial.println("SDHC");
    }
    else {
        Serial.println("UNKNOWN");
    }

    uint64_t cardSize = SD.cardSize() / (1024 * 1024);
    Serial.printf("SD Card Size: %lluMB\n", cardSize);

}

void loop() {

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions