-
-
Notifications
You must be signed in to change notification settings - Fork 215
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 <SdFat.h>
// 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);
}