arduino / arduino/ArduinoCore-mbed

"SDMMC on Portenta H7 does not recover after a hot-plug removal."

Open
#1,081 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
411
Forks
225
PR merge metrics
No merged PRs in 30d

Description

i am using the sd card on portenta breakout board using the Portenta H7 however "SDMMC on Portenta H7 does not recover after a hot-plug removal." I am using the following code..
#include

// For Portenta + Mid/Breakout Carrier, SD is on the built-in SDMMC slot.
SdFat SDFS;

#define ERROR_LED LEDR // Use the built-in Red LED

// Helper to control the LED (LOW = ON for Portenta)
void ERROR_LED_SET(bool on) {
digitalWrite(ERROR_LED, on ? LOW : HIGH);
}

/**
* @brief (Final Hot-Plug Fix) Verifies card presence by checking card type
* IMMEDIATELY after initialization to defeat "ghost" initializations.
*/
bool ensureSdReady() {
// This function now performs a mandatory two-step check because SDFS.begin()
// on its own can be unreliable on this hardware after a card removal.

// Step 1: Attempt to initialize the card.
if (!SDFS.begin()) {
// This is a clear and reliable failure. The card is definitely not present.
ERROR_LED_SET(true);
return false;
}

// Step 2: VERIFY the initialization.
// After a "successful" begin(), we immediately check the card's hardware type.
// A real card will have a type like SD_CARD_TYPE_SDHC.
// A "ghost" initialization on empty pins will return a type of 0.
if (SDFS.card()->type() == 0) {
// This is the critical check. begin() gave a false positive.
ERROR_LED_SET(true);
return false;
}

// If we get here, begin() succeeded AND the card reported a valid hardware type.
ERROR_LED_SET(false);
return true;
}

void setup() {
Serial.begin(115200);
while (!Serial && millis() < 3000);

pinMode(ERROR_LED, OUTPUT);
digitalWrite(ERROR_LED, HIGH); // Turn LED OFF

Serial.println("\n--- Portenta H7 Final SD Card Hot-Plug Test ---");
Serial.println("This version uses two-step verification to prevent false detections.\n");
}

void loop() {
Serial.println("--- Starting Check ---");
if (ensureSdReady()) {
if (SDFS.exists("testfile.txt")) {
Serial.println("Result: Card is PRESENT and 'testfile.txt' was FOUND.\n");
} else {
Serial.println("Result: Card is PRESENT but 'testfile.txt' was NOT FOUND.\n");
}
} else {
Serial.println("Result: Card is NOT ready or was removed.\n");
}

delay(3000);
}

Contributor guide

No contributing guide indexed for this repository

Research direction

The report provides no repository file or test; start by reproducing the supplied sketch on a Portenta H7 and tracing the SDMMC initialization reached through SDFS.begin() and SDFS.card()->type(). Confirm the behavior across card removal and reinsertion, then make the documented hot-plug failure recover reliably without relying on the sketch's extra type check.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.