@@ -60,6 +60,31 @@ typedef struct {
6060
6161static ardu_sdcard_t * s_cards[FF_VOLUMES] = { NULL };
6262
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+
6388/*
6489 * SD SPI
6590 * */
@@ -73,6 +98,9 @@ bool sdWait(uint8_t pdrv, int timeout)
7398 resp = s_cards[pdrv]->spi ->transfer (0xFF );
7499 } while (resp == 0x00 && (millis () - start) < (unsigned int )timeout);
75100
101+ if (!resp) {
102+ log_w (" Wait Failed" );
103+ }
76104 return (resp > 0x00 );
77105}
78106
@@ -91,7 +119,10 @@ bool sdSelectCard(uint8_t pdrv)
91119{
92120 ardu_sdcard_t * card = s_cards[pdrv];
93121 digitalWrite (card->ssPin , LOW);
94- sdWait (pdrv, 300 );
122+ bool s = sdWait (pdrv, 300 );
123+ if (!s) {
124+ log_e (" Select Failed" );
125+ }
95126 return true ;
96127}
97128
@@ -105,10 +136,11 @@ char sdCommand(uint8_t pdrv, char cmd, unsigned int arg, unsigned int* resp)
105136 token = sdCommand (pdrv, APP_CMD, 0 , NULL );
106137 sdDeselectCard (pdrv);
107138 if (token > 1 ) {
108- return token ;
139+ break ;
109140 }
110141 if (!sdSelectCard (pdrv)) {
111- return 0xFF ;
142+ token = 0xFF ;
143+ break ;
112144 }
113145 }
114146
@@ -159,7 +191,10 @@ char sdCommand(uint8_t pdrv, char cmd, unsigned int arg, unsigned int* resp)
159191
160192 break ;
161193 }
162-
194+ if (token == 0xFF ) {
195+ log_e (" Card Failed! cmd: 0x%02x" , cmd);
196+ card->status = STA_NOINIT;
197+ }
163198 return token;
164199}
165200
@@ -215,7 +250,7 @@ bool sdReadSector(uint8_t pdrv, char* buffer, unsigned long long sector)
215250{
216251 for (int f = 0 ; f < 3 ; f++) {
217252 if (!sdSelectCard (pdrv)) {
218- break ;
253+ return false ;
219254 }
220255 if (!sdCommand (pdrv, READ_BLOCK_SINGLE, (s_cards[pdrv]->type == CARD_SDHC) ? sector : sector << 9 , NULL )) {
221256 bool success = sdReadBytes (pdrv, buffer, 512 );
@@ -235,7 +270,7 @@ bool sdReadSectors(uint8_t pdrv, char* buffer, unsigned long long sector, int co
235270{
236271 for (int f = 0 ; f < 3 ;) {
237272 if (!sdSelectCard (pdrv)) {
238- break ;
273+ return false ;
239274 }
240275
241276 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)
271306{
272307 for (int f = 0 ; f < 3 ; f++) {
273308 if (!sdSelectCard (pdrv)) {
274- break ;
309+ return false ;
275310 }
276311 if (!sdCommand (pdrv, WRITE_BLOCK_SINGLE, (s_cards[pdrv]->type == CARD_SDHC) ? sector : sector << 9 , NULL )) {
277312 char token = sdWriteBytes (pdrv, buffer, 0xFE );
@@ -307,12 +342,12 @@ bool sdWriteSectors(uint8_t pdrv, const char* buffer, unsigned long long sector,
307342 for (int f = 0 ; f < 3 ;) {
308343 if (card->type != CARD_MMC) {
309344 if (sdTransaction (pdrv, SET_WR_BLK_ERASE_COUNT, currentCount, NULL )) {
310- break ;
345+ return false ;
311346 }
312347 }
313348
314349 if (!sdSelectCard (pdrv)) {
315- break ;
350+ return false ;
316351 }
317352
318353 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,
344379 break ;
345380 }
346381
347- sdDeselectCard (pdrv);
348-
349382 if (token == 0x0A ) {
383+ sdDeselectCard (pdrv);
350384 unsigned int writtenBlocks = 0 ;
351385 if (card->type != CARD_MMC && sdSelectCard (pdrv)) {
352386 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,
365399 currentCount = count - writtenBlocks;
366400 continue ;
367401 } else {
368- return false ;
402+ break ;
369403 }
370404 }
371405 } else {
@@ -380,7 +414,7 @@ unsigned long sdGetSectorsCount(uint8_t pdrv)
380414{
381415 for (int f = 0 ; f < 3 ; f++) {
382416 if (!sdSelectCard (pdrv)) {
383- break ;
417+ return false ;
384418 }
385419
386420 if (!sdCommand (pdrv, SEND_CSD, 0 , NULL )) {
@@ -714,7 +748,7 @@ uint8_t sdcard_unmount(uint8_t pdrv)
714748 return 0 ;
715749}
716750
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 )
718752{
719753 ardu_sdcard_t * card = s_cards[pdrv];
720754 if (pdrv >= FF_VOLUMES || card == NULL ){
@@ -739,9 +773,25 @@ bool sdcard_mount(uint8_t pdrv, const char* path, uint8_t max_files)
739773
740774 FRESULT res = f_mount (fs, drv, 1 );
741775 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+ }
745795 }
746796 AcquireSPI lock (card);
747797 card->sectors = sdGetSectorsCount (pdrv);
0 commit comments