Can't get MicroSD working on ESP32-P4-Function-EV-Board

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby andromeda92 » Sat Nov 29, 2025 10:33 am

What's strange is that it works fine with SD_mmc

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby lbernstone » Sat Nov 29, 2025 6:08 pm

Hmm. Not sure what this means, but if I run the SD_MMC example first, then flash the SD_Test just after that ran, then SD works. If I then simply replug the device, it fails. Maybe something persistent is getting set in the power levels? Might need to take this to the Espressif team.

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby andromeda92 » Sat Nov 29, 2025 8:14 pm

Thank you for your reply, I don't understand either.

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby lbernstone » Sat Nov 29, 2025 9:27 pm

https://github.com/espressif/arduino-esp32/pull/12079
The core is

Code: Select all

#include "sd_pwr_ctrl_by_on_chip_ldo.h"
sd_pwr_ctrl_ldo_config_t ldo_config;
ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL;
sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL;
sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300));

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby andromeda92 » Sun Nov 30, 2025 4:17 am

The code you entered is for MMC not for SD.h
MMC with ESP-IDF and Arduino; it works very well. I'm referring to the SD_Test example for ESP32-P4 on Arduino, which doesn't work on Arduino.

Arduino 2.3.6
Core 3.3.4

The sketch below does not work, is SD_Test for ESP32-P4 on arduino.

Code: Select all

/*
 * pin 1 - not used          |  Micro SD card     |
 * pin 2 - CS (SS)           |                   /
 * pin 3 - DI (MOSI)         |                  |__
 * pin 4 - VDD (3.3V)        |                    |
 * pin 5 - SCK (SCLK)        | 8 7 6 5 4 3 2 1   /
 * pin 6 - VSS (GND)         | ▄ ▄ ▄ ▄ ▄ ▄ ▄ ▄  /
 * pin 7 - DO (MISO)         | ▀ ▀ █ ▀ █ ▀ ▀ ▀ |
 * pin 8 - not used          |_________________|
 *                             ║ ║ ║ ║ ║ ║ ║ ║
 *                     ╔═══════╝ ║ ║ ║ ║ ║ ║ ╚═════════╗
 *                     ║         ║ ║ ║ ║ ║ ╚══════╗    ║
 *                     ║   ╔═════╝ ║ ║ ║ ╚═════╗  ║    ║
 * Connections for     ║   ║   ╔═══╩═║═║═══╗   ║  ║    ║
 * full-sized          ║   ║   ║   ╔═╝ ║   ║   ║  ║    ║
 * SD card             ║   ║   ║   ║   ║   ║   ║  ║    ║
 * Pin name         |  -  DO  VSS SCK VDD VSS DI CS    -  |
 * SD pin number    |  8   7   6   5   4   3   2   1   9 /
 *                  |                                  █/
 *                  |__▍___▊___█___█___█___█___█___█___/
 *
 * Note:  The SPI pins can be manually configured by using `SPI.begin(sck, miso, mosi, cs).`
 *        Alternatively, you can change the CS pin and use the other default settings by using `SD.begin(cs)`.
 *
 * +--------------+---------+-------+----------+----------+----------+----------+----------+
 * | SPI Pin Name | ESP8266 | ESP32 | ESP32‑S2 | ESP32‑S3 | ESP32‑C3 | ESP32‑C6 | ESP32‑H2 |
 * +==============+=========+=======+==========+==========+==========+==========+==========+
 * | CS (SS)      | GPIO15  | GPIO5 | GPIO34   | GPIO10   | GPIO7    | GPIO18   | GPIO0    |
 * +--------------+---------+-------+----------+----------+----------+----------+----------+
 * | DI (MOSI)    | GPIO13  | GPIO23| GPIO35   | GPIO11   | GPIO6    | GPIO19   | GPIO25   |
 * +--------------+---------+-------+----------+----------+----------+----------+----------+
 * | DO (MISO)    | GPIO12  | GPIO19| GPIO37   | GPIO13   | GPIO5    | GPIO20   | GPIO11   |
 * +--------------+---------+-------+----------+----------+----------+----------+----------+
 * | SCK (SCLK)   | GPIO14  | GPIO18| GPIO36   | GPIO12   | GPIO4    | GPIO21   | GPIO10   |
 * +--------------+---------+-------+----------+----------+----------+----------+----------+
 *
 * For more info see file README.md in this library or on URL:
 * https://github.com/espressif/arduino-esp32/tree/master/libraries/SD
 */
#include <Arduino.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"


//Uncomment and set up if you want to use custom pins for the SPI communication
#define REASSIGN_PINS
/*int sck = 36;
int miso = 37;
int mosi = 38;
int cs = 39;*/


int sck = 43;
int miso = 39;
int mosi = 44;
int cs = 42;



/*int sck = 43;
int miso = 39;
int mosi = 40;
int cs = 42;
*/

/*
int sck = 9;
int miso = 10;
int mosi = 8;
int cs = 7;
*/
/*
int sck = 12;
int miso = 13;
int mosi = 14;
int cs = 15;
*/

void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
  Serial.printf("Listing directory: %s\n", dirname);

  File root = fs.open(dirname);
  if (!root) {
    Serial.println("Failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if (levels) {
        listDir(fs, file.path(), levels - 1);
      }
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}

void createDir(fs::FS &fs, const char *path) {
  Serial.printf("Creating Dir: %s\n", path);
  if (fs.mkdir(path)) {
    Serial.println("Dir created");
  } else {
    Serial.println("mkdir failed");
  }
}

void removeDir(fs::FS &fs, const char *path) {
  Serial.printf("Removing Dir: %s\n", path);
  if (fs.rmdir(path)) {
    Serial.println("Dir removed");
  } else {
    Serial.println("rmdir failed");
  }
}

void readFile(fs::FS &fs, const char *path) {
  Serial.printf("Reading file: %s\n", path);

  File file = fs.open(path);
  if (!file) {
    Serial.println("Failed to open file for reading");
    return;
  }

  Serial.print("Read from file: ");
  while (file.available()) {
    Serial.write(file.read());
  }
  file.close();
}

void writeFile(fs::FS &fs, const char *path, const char *message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

void appendFile(fs::FS &fs, const char *path, const char *message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

void renameFile(fs::FS &fs, const char *path1, const char *path2) {
  Serial.printf("Renaming file %s to %s\n", path1, path2);
  if (fs.rename(path1, path2)) {
    Serial.println("File renamed");
  } else {
    Serial.println("Rename failed");
  }
}

void deleteFile(fs::FS &fs, const char *path) {
  Serial.printf("Deleting file: %s\n", path);
  if (fs.remove(path)) {
    Serial.println("File deleted");
  } else {
    Serial.println("Delete failed");
  }
}

void testFileIO(fs::FS &fs, const char *path) {
  File file = fs.open(path);
  static uint8_t buf[512];
  size_t len = 0;
  uint32_t start = millis();
  uint32_t end = start;
  if (file) {
    len = file.size();
    size_t flen = len;
    start = millis();
    while (len) {
      size_t toRead = len;
      if (toRead > 512) {
        toRead = 512;
      }
      file.read(buf, toRead);
      len -= toRead;
    }
    end = millis() - start;
    Serial.printf("%u bytes read for %lu ms\n", flen, end);
    file.close();
  } else {
    Serial.println("Failed to open file for reading");
  }

  file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }

  size_t i;
  start = millis();
  for (i = 0; i < 2048; i++) {
    file.write(buf, 512);
  }
  end = millis() - start;
  Serial.printf("%u bytes written for %lu ms\n", 2048 * 512, end);
  file.close();
}

void setup() {
  Serial.begin(115200);

#ifdef REASSIGN_PINS


  pinMode(45, OUTPUT);
  digitalWrite(45, LOW);
  SPI.begin(sck, miso, mosi, cs);

  if (!SD.begin()) {
#else
  if (!SD.begin()) {
#endif
    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);

  listDir(SD, "/", 0);
  /*createDir(SD, "/mydir");
  listDir(SD, "/", 0);
  removeDir(SD, "/mydir");
  listDir(SD, "/", 2);
  writeFile(SD, "/hello.txt", "Hello ");
  appendFile(SD, "/hello.txt", "World!\n");
  readFile(SD, "/hello.txt");
  deleteFile(SD, "/foo.txt");
  renameFile(SD, "/hello.txt", "/foo.txt");
  readFile(SD, "/foo.txt");
  testFileIO(SD, "/test.txt");
  Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
  Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));*/
}

void loop() {}


andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby andromeda92 » Sun Nov 30, 2025 4:39 am

with this code added on SD_Test, i have

Code: Select all

#include <Arduino.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "sd_pwr_ctrl_by_on_chip_ldo.h"


sd_pwr_ctrl_ldo_config_t ldo_config;
ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL;
sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL;
sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300));

//Uncomment and set up if you want to use custom pins for the SPI communication
#define REASSIGN_PINS

int sck = 43;
int miso = 39;
int mosi = 44;
int cs = 42;

..............
Attachments
Capture.JPG
Capture.JPG (101.15 KiB) Viewed 3613 times

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby andromeda92 » Sun Nov 30, 2025 4:51 am

i have added code below on setup, it compile but i have the same problem
Card mount failed

Code: Select all


void setup() {
  Serial.begin(115200);

ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL;
sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL;
sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300);


#ifdef REASSIGN_PINS


  pinMode(45, OUTPUT);
  digitalWrite(45, LOW);
  SPI.begin(sck, miso, mosi, cs);

  if (!SD.begin()) {
#else
  if (!SD.begin()) {
#endif
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();
  
  ...........................

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby andromeda92 » Sun Nov 30, 2025 4:55 am

Thank you very much, it work fine now.

Code: Select all

void setup() {
  Serial.begin(115200);

ldo_config.ldo_chan_id = BOARD_SDMMC_POWER_CHANNEL;
sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL;
sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
sd_pwr_ctrl_set_io_voltage(pwr_ctrl_handle, 3300);


#ifdef REASSIGN_PINS


 // pinMode(45, OUTPUT);
 // digitalWrite(45, LOW);
  SPI.begin(sck, miso, mosi);

  if (!SD.begin(42)) {
#else
  if (!SD.begin()) {
#endif
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();
i commented this line

Code: Select all


 // pinMode(45, OUTPUT);
 // digitalWrite(45, LOW);
Thank you for your help and the time you spent.

andromeda92
Posts: 34
Joined: Tue Nov 18, 2025 5:24 pm

Re: Can't get MicroSD working on ESP32-P4-Function-EV-Board

Postby andromeda92 » Sun Nov 30, 2025 6:49 am

test screen
Attachments
Capture.JPG
Capture.JPG (51.98 KiB) Viewed 3608 times

Who is online

Users browsing this forum: Google [Bot], Qwantbot and 2 guests