@@ -60,6 +60,31 @@ typedef struct {
60
60
61
61
static ardu_sdcard_t * s_cards[FF_VOLUMES] = { NULL };
62
62
63
+ #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
64
+ const char * fferr2str[] = {
65
+ " (0) Succeeded" ,
66
+ " (1) A hard error occurred in the low level disk I/O layer" ,
67
+ " (2) Assertion failed" ,
68
+ " (3) The physical drive cannot work" ,
69
+ " (4) Could not find the file" ,
70
+ " (5) Could not find the path" ,
71
+ " (6) The path name format is invalid" ,
72
+ " (7) Access denied due to prohibited access or directory full" ,
73
+ " (8) Access denied due to prohibited access" ,
74
+ " (9) The file/directory object is invalid" ,
75
+ " (10) The physical drive is write protected" ,
76
+ " (11) The logical drive number is invalid" ,
77
+ " (12) The volume has no work area" ,
78
+ " (13) There is no valid FAT volume" ,
79
+ " (14) The f_mkfs() aborted due to any problem" ,
80
+ " (15) Could not get a grant to access the volume within defined period" ,
81
+ " (16) The operation is rejected according to the file sharing policy" ,
82
+ " (17) LFN working buffer could not be allocated" ,
83
+ " (18) Number of open files > FF_FS_LOCK" ,
84
+ " (19) Given parameter is invalid"
85
+ };
86
+ #endif
87
+
63
88
/*
64
89
* SD SPI
65
90
* */
@@ -73,6 +98,9 @@ bool sdWait(uint8_t pdrv, int timeout)
73
98
resp = s_cards[pdrv]->spi ->transfer (0xFF );
74
99
} while (resp == 0x00 && (millis () - start) < (unsigned int )timeout);
75
100
101
+ if (!resp) {
102
+ log_w (" Wait Failed" );
103
+ }
76
104
return (resp > 0x00 );
77
105
}
78
106
@@ -91,7 +119,10 @@ bool sdSelectCard(uint8_t pdrv)
91
119
{
92
120
ardu_sdcard_t * card = s_cards[pdrv];
93
121
digitalWrite (card->ssPin , LOW);
94
- sdWait (pdrv, 300 );
122
+ bool s = sdWait (pdrv, 300 );
123
+ if (!s) {
124
+ log_e (" Select Failed" );
125
+ }
95
126
return true ;
96
127
}
97
128
@@ -105,10 +136,11 @@ char sdCommand(uint8_t pdrv, char cmd, unsigned int arg, unsigned int* resp)
105
136
token = sdCommand (pdrv, APP_CMD, 0 , NULL );
106
137
sdDeselectCard (pdrv);
107
138
if (token > 1 ) {
108
- return token ;
139
+ break ;
109
140
}
110
141
if (!sdSelectCard (pdrv)) {
111
- return 0xFF ;
142
+ token = 0xFF ;
143
+ break ;
112
144
}
113
145
}
114
146
@@ -159,7 +191,10 @@ char sdCommand(uint8_t pdrv, char cmd, unsigned int arg, unsigned int* resp)
159
191
160
192
break ;
161
193
}
162
-
194
+ if (token == 0xFF ) {
195
+ log_e (" Card Failed! cmd: 0x%02x" , cmd);
196
+ card->status = STA_NOINIT;
197
+ }
163
198
return token;
164
199
}
165
200
@@ -215,7 +250,7 @@ bool sdReadSector(uint8_t pdrv, char* buffer, unsigned long long sector)
215
250
{
216
251
for (int f = 0 ; f < 3 ; f++) {
217
252
if (!sdSelectCard (pdrv)) {
218
- break ;
253
+ return false ;
219
254
}
220
255
if (!sdCommand (pdrv, READ_BLOCK_SINGLE, (s_cards[pdrv]->type == CARD_SDHC) ? sector : sector << 9 , NULL )) {
221
256
bool success = sdReadBytes (pdrv, buffer, 512 );
@@ -235,7 +270,7 @@ bool sdReadSectors(uint8_t pdrv, char* buffer, unsigned long long sector, int co
235
270
{
236
271
for (int f = 0 ; f < 3 ;) {
237
272
if (!sdSelectCard (pdrv)) {
238
- break ;
273
+ return false ;
239
274
}
240
275
241
276
if (!sdCommand (pdrv, READ_BLOCK_MULTIPLE, (s_cards[pdrv]->type == CARD_SDHC) ? sector : sector << 9 , NULL )) {
@@ -271,7 +306,7 @@ bool sdWriteSector(uint8_t pdrv, const char* buffer, unsigned long long sector)
271
306
{
272
307
for (int f = 0 ; f < 3 ; f++) {
273
308
if (!sdSelectCard (pdrv)) {
274
- break ;
309
+ return false ;
275
310
}
276
311
if (!sdCommand (pdrv, WRITE_BLOCK_SINGLE, (s_cards[pdrv]->type == CARD_SDHC) ? sector : sector << 9 , NULL )) {
277
312
char token = sdWriteBytes (pdrv, buffer, 0xFE );
@@ -307,12 +342,12 @@ bool sdWriteSectors(uint8_t pdrv, const char* buffer, unsigned long long sector,
307
342
for (int f = 0 ; f < 3 ;) {
308
343
if (card->type != CARD_MMC) {
309
344
if (sdTransaction (pdrv, SET_WR_BLK_ERASE_COUNT, currentCount, NULL )) {
310
- break ;
345
+ return false ;
311
346
}
312
347
}
313
348
314
349
if (!sdSelectCard (pdrv)) {
315
- break ;
350
+ return false ;
316
351
}
317
352
318
353
if (!sdCommand (pdrv, WRITE_BLOCK_MULTIPLE, (card->type == CARD_SDHC) ? currentSector : currentSector << 9 , NULL )) {
@@ -344,9 +379,8 @@ bool sdWriteSectors(uint8_t pdrv, const char* buffer, unsigned long long sector,
344
379
break ;
345
380
}
346
381
347
- sdDeselectCard (pdrv);
348
-
349
382
if (token == 0x0A ) {
383
+ sdDeselectCard (pdrv);
350
384
unsigned int writtenBlocks = 0 ;
351
385
if (card->type != CARD_MMC && sdSelectCard (pdrv)) {
352
386
if (!sdCommand (pdrv, SEND_NUM_WR_BLOCKS, 0 , NULL )) {
@@ -365,7 +399,7 @@ bool sdWriteSectors(uint8_t pdrv, const char* buffer, unsigned long long sector,
365
399
currentCount = count - writtenBlocks;
366
400
continue ;
367
401
} else {
368
- return false ;
402
+ break ;
369
403
}
370
404
}
371
405
} else {
@@ -380,7 +414,7 @@ unsigned long sdGetSectorsCount(uint8_t pdrv)
380
414
{
381
415
for (int f = 0 ; f < 3 ; f++) {
382
416
if (!sdSelectCard (pdrv)) {
383
- break ;
417
+ return false ;
384
418
}
385
419
386
420
if (!sdCommand (pdrv, SEND_CSD, 0 , NULL )) {
@@ -714,7 +748,7 @@ uint8_t sdcard_unmount(uint8_t pdrv)
714
748
return 0 ;
715
749
}
716
750
717
- bool sdcard_mount (uint8_t pdrv, const char * path, uint8_t max_files)
751
+ bool sdcard_mount (uint8_t pdrv, const char * path, uint8_t max_files, bool format_if_empty )
718
752
{
719
753
ardu_sdcard_t * card = s_cards[pdrv];
720
754
if (pdrv >= FF_VOLUMES || card == NULL ){
@@ -739,9 +773,25 @@ bool sdcard_mount(uint8_t pdrv, const char* path, uint8_t max_files)
739
773
740
774
FRESULT res = f_mount (fs, drv, 1 );
741
775
if (res != FR_OK) {
742
- log_e (" f_mount failed 0x(%x)" , res);
743
- esp_vfs_fat_unregister_path (path);
744
- return false ;
776
+ log_e (" f_mount failed: %s" , fferr2str[res]);
777
+ if (res == 13 && format_if_empty){
778
+ BYTE work[FF_MAX_SS];
779
+ res = f_mkfs (drv, FM_ANY, 0 , work, sizeof (work));
780
+ if (res != FR_OK) {
781
+ log_e (" f_mkfs failed: %s" , fferr2str[res]);
782
+ esp_vfs_fat_unregister_path (path);
783
+ return false ;
784
+ }
785
+ res = f_mount (fs, drv, 1 );
786
+ if (res != FR_OK) {
787
+ log_e (" f_mount failed: %s" , fferr2str[res]);
788
+ esp_vfs_fat_unregister_path (path);
789
+ return false ;
790
+ }
791
+ } else {
792
+ esp_vfs_fat_unregister_path (path);
793
+ return false ;
794
+ }
745
795
}
746
796
AcquireSPI lock (card);
747
797
card->sectors = sdGetSectorsCount (pdrv);
0 commit comments