Problem with running sketch after PC rebuild

A few weeks ago the SSD on my PC died. I replaced it and reinstalled Windows 11 and all my software from the latest versions. Arduino included. I restored all the Arduino sketches and libraries from a backup. I ran a series of test on my 'Arduino test' sketches. My latest board is a JC-ESP32P4_M3_DEV from Guition. I use the ESP32P4 DEV Module from Espressif as the board identifier. The board has a TF Card reader on it. The test sketch used to read the contents of an SD card (using SD_MMC) and print a list of files on it. The sketch then ran a simple test of a stepper motor. When I compile and download it now it fails with the messages:

E (206) sdmmc_periph: sdmmc_host_init_slot: Invalid GPIO number -1, returned 0x102
E (207) vfs_fat_sdmmc: slot init failed (0x102).

In spite of spending hours in vain trying to find what has changed I hope there is someone on the forum that can point me in the right direction.

The Sketch is as follows'

#include <SD_MMC.h>

#include <Arduino.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"

// Stepper driver pin definitions
#define PIN_DIRECTION 1
#define PIN_STEP 2
#define PIN_ENABLE 3
#define PIN_MS1 4
#define PIN_MS2 5

int microStep = 32;
int stepDelay = 9574;
//int stepDelay = 451;
int steps = microStep * 200;

void setup() {
	Serial.begin(115200);
	while (!Serial) {delay(100);}

	if (!SD_MMC.begin()) {
    Serial.println("No SD card mounted");
    return;
  }
	else
	{
		Serial.println("SD card details"); 
		if (!SD_MMC.begin("/sdcard", false))
		{
			Serial.println("SD_MMC checking for 1-bit or 4bit");
			if (!SD_MMC.begin("/sdcard", true))	
			{
				Serial.println("SD_MMC 1-bit: Reformat to FAT32 for 4bit");  
				return;
			}		
			Serial.println("SD_MMC 1-bit OK");
		}
		else
		{
			Serial.println("SD_MMC 4-bit OK");
		}

		Serial.printf("SD type: %d | Size: %.2f GB\n\n",
		SD_MMC.cardType(), SD_MMC.cardSize() / ( 1024.0 * 1024.0 * 1024.0));

  if (SD_MMC.exists("/TestDir")) {
    Serial.printf("Directory '/TestDir' already exists.\n");
		File file = SD_MMC.open("/TestDir/test.txt", FILE_WRITE);
		if(!file){
			Serial.println("Failed to open file for writing");
			return;
		}
		else
		{
			if(file.println("Hello, SD_MMC! This is a test file.")){
				Serial.println("File written");
			} else {
				Serial.println("Write failed");
			}

			file.close();
			Serial.println("File closed");		
		}
  } else {
		if (SD_MMC.mkdir("/TestDir")) { // Or SD_MMC.mkdir("TestDir")
			Serial.println("Directory DATA created");
		} else {
			Serial.println("Directory TestDir already exists or creation failed");
		}
	}
		listDir(SD_MMC, "/", 2); // List the root directory

		const char* dirToDelete = "/TestDir";
		Serial.printf("Attempting to delete directory: %s\n", dirToDelete);
		if (SD_MMC.rmdir(dirToDelete)) {
			Serial.printf("Directory '%s' deleted successfully.\n", dirToDelete);
		} else {
			Serial.printf("Failed to delete directory '%s'.\n", dirToDelete);
		}
	}

	Serial.println("TMC2209 starting");  
 
	int mdelay = stepDelay / microStep;
	// Configure stepper driver pins as OUTPUTs
	pinMode(PIN_DIRECTION, OUTPUT);
	pinMode(PIN_STEP, OUTPUT);
	pinMode(PIN_ENABLE, OUTPUT);
	pinMode(PIN_MS1, OUTPUT);
	pinMode(PIN_MS2, OUTPUT);

	digitalWrite(PIN_ENABLE, LOW);

	digitalWrite(PIN_MS1, HIGH);
	digitalWrite(PIN_MS2, LOW);

	for (int i = 0; i < steps; i++)
	{
		digitalWrite(PIN_STEP, HIGH);
		delayMicroseconds(50);
		digitalWrite(PIN_STEP, LOW);
		delayMicroseconds(stepDelay);
	}

	delay(1000);
	digitalWrite(PIN_DIRECTION, HIGH);
	for (int i = 0; i < steps; i++)
	{
		digitalWrite(PIN_STEP, HIGH);
		delayMicroseconds(50);
		digitalWrite(PIN_STEP, LOW);
		delayMicroseconds(stepDelay);
	}
	digitalWrite(PIN_ENABLE, HIGH);
}

void loop() {
	Serial.println("TMC2209 Idle");  
	delay(5000);
}
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  Serial.printf("Listing of 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) {
				String sDir = String(dirname) + String(file.name());
        listDir(fs,sDir.c_str(), levels - 1); // Recursively list subdirectories
      }
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}

You might have been running on an old version of the ESP32 core and now you are on the new one, so the behavior might be a bit different ?

On ESP32, SD_MMC.begin() without arguments assumes the default SDMMC pins (I think they ar CLK=14, CMD=15, D0=2, D1=4, D2=12, D3=13). If your board does not have an SD slot wired to these pins, initialization fails. You could try to use the SPI interface via SD.begin() instead of SD_MMC ?


side note - calling SD_MMC.begin() multiple times as in your code feels redundant and may be (I don't know) an issue as well. Generally begin() is called once.

I have just found the pin definitions for P4
They should be set by:
SD_MMC.setPins(43,44,39,40,41,42);
But that makes no difference.
Multiple calls of SD_MM.begin did not previously cause any problems. I now can't get past the first begin :neutral_face:
I suspect it has something to do with CONFIG_IDF_TARGET_ESP32P4 but I can't find where that should be declared or altered.

does the SD card work if you use the SD library ?

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

const byte PIN_CS = 5;
const byte PIN_DIRECTION = 1;
const byte PIN_STEP = 2;
const byte PIN_ENABLE = 3;
const byte PIN_MS1 = 4;
const byte PIN_MS2 = 5;

const unsigned long MICROSTEP = 32;
const unsigned long STEP_DELAY = 9574;
const unsigned long STEPS = MICROSTEP * 200UL;

void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  Serial.printf("Listing of 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) {
        String sDir = String(dirname) + String(file.name());
        listDir(fs, sDir.c_str(), levels - 1);
      }
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}

void setupStepper() {
  pinMode(PIN_DIRECTION, OUTPUT);
  pinMode(PIN_STEP, OUTPUT);
  pinMode(PIN_ENABLE, OUTPUT);
  pinMode(PIN_MS1, OUTPUT);
  pinMode(PIN_MS2, OUTPUT);

  digitalWrite(PIN_ENABLE, LOW);
  digitalWrite(PIN_MS1, HIGH);
  digitalWrite(PIN_MS2, LOW);
}

void runStepper(unsigned long steps, unsigned long stepDelay) {
  for (unsigned long i = 0; i < steps; i++) {
    digitalWrite(PIN_STEP, HIGH);
    delayMicroseconds(50);
    digitalWrite(PIN_STEP, LOW);
    delayMicroseconds(stepDelay);
  }

  delay(1000);
  digitalWrite(PIN_DIRECTION, HIGH);

  for (unsigned long i = 0; i < steps; i++) {
    digitalWrite(PIN_STEP, HIGH);
    delayMicroseconds(50);
    digitalWrite(PIN_STEP, LOW);
    delayMicroseconds(stepDelay);
  }

  digitalWrite(PIN_ENABLE, HIGH);
}

void setup() {
  Serial.begin(115200);
  while (!Serial) { delay(100); }

  if (!SD.begin(PIN_CS)) {
    Serial.println("No SD card found");
    return;
  } else {
    Serial.println("SD card initialized");

    if (SD.exists("/TestDir")) {
      Serial.println("Directory '/TestDir' already exists");
      File file = SD.open("/TestDir/test.txt", FILE_WRITE);
      if (!file) {
        Serial.println("Failed to open file for writing");
        return;
      } else {
        if (file.println("Hello, SD! This is a test file.")) {
          Serial.println("File written");
        } else {
          Serial.println("Write failed");
        }
        file.close();
        Serial.println("File closed");
      }
    } else {
      if (SD.mkdir("/TestDir")) {
        Serial.println("Directory TestDir created");
      } else {
        Serial.println("Failed to create directory TestDir");
      }
    }

    listDir(SD, "/", 2);

    const char* dirToDelete = "/TestDir";
    Serial.printf("Attempting to delete directory: %s\n", dirToDelete);
    if (SD.rmdir(dirToDelete)) {
      Serial.printf("Directory '%s' deleted successfully.\n", dirToDelete);
    } else {
      Serial.printf("Failed to delete directory '%s'.\n", dirToDelete);
    }
  }

  Serial.println("TMC2209 starting");
  setupStepper();
  runStepper(STEPS, STEP_DELAY);
}

void loop() {
  Serial.println("TMC2209 Idle");
  delay(5000);
}

That didn't work either. I'm not sure what pin is CS. The only pinout diagram I have for the board is:

1 Like

The SD_MMC.cpp is failing on this if statement:

#if (defined(SOC_SDMMC_USE_GPIO_MATRIX) && !defined(CONFIG_IDF_TARGET_ESP32P4)) \
  || (defined(CONFIG_IDF_TARGET_ESP32P4) && ((defined(BOARD_SDMMC_SLOT) && (BOARD_SDMMC_SLOT == 1)) || !defined(BOARD_HAS_SDMMC)))

I'll work through it in the morning; it's getting a bit late here now.

Ok - without the board it’s hard to tell… good luck

Many thanks for your help the problem resolved - or at least gone away. This morning I downloaded the latest version of esp32 (3.3.5) for boards manager. I needed help to get it to download; it has grown somewhat and needed an increased network timeout to allow the download to complete.
I suspect the SD_MMC problem was caused by the short lived esp32 3.3.4 version that I downloaded when I rebuilt my PC.
All I have to do now is to get the MIPI DSI interface to work with a four or five inch screen.