@@ -34,91 +34,114 @@ ESP3D_File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
3434#define SDIO_BIT_MODE SD_FOUR_BIT_MODE
3535#endif // SDIO_BIT_MODE
3636
37+ // Apply SDIO pins configuration based on settings
38+ // Note: This does nothing on standard ESP32 (pins are hard-coded in silicon)
39+ // but is required for ESP32-S2 / C3 / S3 / etc.
40+ static void applySDPins () {
41+ #if SDIO_BIT_MODE == SD_ONE_BIT_MODE
42+ #if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \
43+ (ESP_SDIO_D0_PIN != -1 )
44+ ESP3D_SD_Card.setPins (ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN);
45+ #endif
46+ #else
47+ #if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \
48+ (ESP_SDIO_D0_PIN != -1 ) || (ESP_SDIO_D1_PIN != -1 ) || \
49+ (ESP_SDIO_D2_PIN != -1 ) || (ESP_SDIO_D3_PIN != -1 )
50+ ESP3D_SD_Card.setPins (ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN,
51+ ESP_SDIO_D1_PIN, ESP_SDIO_D2_PIN, ESP_SDIO_D3_PIN);
52+ #endif
53+ #endif // SD_ONE_BIT_MODE
54+ }
55+
3756uint8_t ESP_SD::getState (bool refresh) {
38- static bool lastinitok = false ;
39- #ifdef SDMMC_FORCE_BEGIN
40- lastinitok = false ;
41- #endif // SDMMC_LIGHT_CHECK
57+ static uint32_t lastReinitTime = 0 ;
58+
4259#if defined(ESP_SD_DETECT_PIN) && ESP_SD_DETECT_PIN != -1
43- // no need to go further if SD detect is not correct
44- if (!((digitalRead (ESP_SD_DETECT_PIN) == ESP_SD_DETECT_VALUE) ? true
45- : false )) {
60+ if (digitalRead (ESP_SD_DETECT_PIN) != ESP_SD_DETECT_VALUE) {
4661 _state = ESP_SDCARD_NOT_PRESENT;
4762 return _state;
4863 }
49- #endif // ESP_SD_DETECT_PIN
50- // if busy doing something return state
51- if (!((_state == ESP_SDCARD_NOT_PRESENT) || _state == ESP_SDCARD_IDLE)) {
64+ #endif
65+
66+ if (_state == ESP_SDCARD_BUSY) return _state;
67+ if (!refresh) return _state;
68+
69+ #ifdef SDMMC_FORCE_BEGIN
70+ if (millis () - lastReinitTime > 250 ) { // anti-spam cooldown
71+ esp3d_log (" SDMMC_FORCE_BEGIN → full re-init" );
72+
73+ ESP3D_SD_Card.end ();
74+ delay (80 ); // very important for 4-bit stability
75+
76+ applySDPins ();
77+
78+ bool ok = ESP3D_SD_Card.begin (" /sdcard" , SDIO_BIT_MODE);
79+
80+ if (ok && ESP3D_SD_Card.cardType () != CARD_NONE) {
81+ _state = ESP_SDCARD_IDLE;
82+ esp3d_log (" Re-init OK (new card detected)" );
83+
84+ // <<<=== FIX HERE ===>>>
85+ // Force refresh of size cache when a new card is inserted
86+ totalBytes (true ); // force refresh
87+ usedBytes (true ); // force refresh
88+ _sizechanged = true ; // also mark for refreshStats()
89+
90+ } else {
91+ _state = ESP_SDCARD_NOT_PRESENT;
92+ esp3d_log_e (" Re-init failed" );
93+ }
94+
95+ lastReinitTime = millis ();
5296 return _state;
5397 }
54- if (!refresh) {
55- return _state; // to avoid refresh=true + busy to reset SD and waste time
98+ #endif
99+
100+ // Soft check (fast path)
101+ if (ESP3D_SD_Card.cardType () != CARD_NONE) {
102+ _state = ESP_SDCARD_IDLE;
103+ return _state;
56104 }
57- // SD is idle or not detected, let see if still the case
58- _state = ESP_SDCARD_NOT_PRESENT;
59- // refresh content if card was removed
60- if (!lastinitok) {
61- esp3d_log (" last init was failed try sd_mmc begin" );
105+
106+ // Sanity / fallback re-init
107+ if (millis () - lastReinitTime > 500 ) {
62108 ESP3D_SD_Card.end ();
63- if (ESP3D_SD_Card.begin (" /sdcard" , SDIO_BIT_MODE)) {
64- esp3d_log (" sd_mmc begin succeed" );
65- if (ESP3D_SD_Card.cardType () != CARD_NONE) {
66- _state = ESP_SDCARD_IDLE;
67- lastinitok = true ;
68- esp3d_log (" sd_mmc card type succeed" );
69- } else {
70- esp3d_log_e (" sd_mmc card type failed" );
71- }
72- } else {
73- esp3d_log_e (" sd_mmc begin failed" );
74- }
75- } else {
76- esp3d_log (" last init was ok try card type" );
77- if (ESP3D_SD_Card.cardType () != CARD_NONE) {
78- esp3d_log (" checking sd_mmc card type succeed" );
109+ delay (50 );
110+ applySDPins ();
111+
112+ if (ESP3D_SD_Card.begin (" /sdcard" , SDIO_BIT_MODE) && ESP3D_SD_Card.cardType () != CARD_NONE) {
79113 _state = ESP_SDCARD_IDLE;
114+ totalBytes (true );
115+ usedBytes (true );
116+ _sizechanged = true ;
80117 } else {
81- lastinitok = false ;
82- esp3d_log_e (" Soft sd check failed" );
83- ESP3D_SD_Card.end ();
84- if (ESP3D_SD_Card.begin (" /sdcard" , SDIO_BIT_MODE)) {
85- esp3d_log (" new sd_mmc begin succeed" );
86- if (ESP3D_SD_Card.cardType () != CARD_NONE) {
87- _state = ESP_SDCARD_IDLE;
88- lastinitok = true ;
89- esp3d_log (" new sd_mmc card type succeed" );
90- } else {
91- esp3d_log_e (" new sd_mmc card type failed" );
92- }
93- } else {
94- esp3d_log (" new sd_mmc begin failed" );
95- }
118+ _state = ESP_SDCARD_NOT_PRESENT;
96119 }
120+ lastReinitTime = millis ();
97121 }
122+
98123 return _state;
99124}
100125
101126bool ESP_SD::begin () {
102- #if SDIO_BIT_MODE == SD_ONE_BIT_MODE
103- #if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \
104- (ESP_SDIO_D0_PIN != -1 )
105- ESP3D_SD_Card.setPins (ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN);
106- #endif // (ESP_SDIO_CLK_PIN != -1)
107- #else
108- #if (ESP_SDIO_CLK_PIN != -1) || (ESP_SDIO_CMD_PIN != -1) || \
109- (ESP_SDIO_D0_PIN != -1 ) || (ESP_SDIO_D1_PIN != -1 ) || \
110- (ESP_SDIO_D2_PIN != -1 ) || (ESP_SDIO_D3_PIN != -1 )
111- ESP3D_SD_Card.setPins (ESP_SDIO_CLK_PIN, ESP_SDIO_CMD_PIN, ESP_SDIO_D0_PIN,
112- ESP_SDIO_D1_PIN, ESP_SDIO_D2_PIN, ESP_SDIO_D3_PIN);
113- #endif // (ESP_SDIO_CLK_PIN != -1)
114- #endif // SD_ONE_BIT_MODE
115- esp3d_log (" Begin SDIO" );
116- _started = true ;
117- #ifdef SDMMC_FORCE_BEGIN
118- _state = ESP_SDCARD_NOT_PRESENT;
119- #else
120- _state = getState (true );
121- #endif // SDMMC_FORCE_BEGIN
127+ esp3d_log (" SDIO begin - mode %s" , (SDIO_BIT_MODE == SD_ONE_BIT_MODE) ? " 1-bit" : " 4-bit" );
128+
129+ applySDPins ();
130+
131+ _started = ESP3D_SD_Card.begin (" /sdcard" , SDIO_BIT_MODE);
132+
133+ if (_started) {
134+ _state = (ESP3D_SD_Card.cardType () != CARD_NONE) ? ESP_SDCARD_IDLE : ESP_SDCARD_NOT_PRESENT;
135+
136+ // Force initial size cache refresh at boot
137+ if (_state == ESP_SDCARD_IDLE) {
138+ totalBytes (true );
139+ usedBytes (true );
140+ _sizechanged = true ;
141+ }
142+ } else {
143+ _state = ESP_SDCARD_NOT_PRESENT;
144+ }
122145
123146 return _started;
124147}
0 commit comments