cancel
Showing results for 
Search instead for 
Did you mean: 

NOT ABLE TO READ FILE COUNT AND FILE NAMES FROM SD CARD

Bkris.2
Associate II

we are using STM32F205 controller board, and tried to read the files in a directory in SD card.

we are able to mount the SD card successfully also we are able to create the file and read the file, however we are not able to count the number of files also the file names.

we are trying the below code

NOT WORKING CODE

FRESULT scan_files (

char* path /* Start node to be scanned (***also used as work area***) */

)

{

FRESULT res;

DIR dir;

UINT i;

static FILINFO fno;

res = f_opendir(&dir, path); /* Open the directory */

if (res == FR_OK) {

for (;;) {

res = f_readdir(&dir, &fno); /* Read a directory item */

if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */

if (fno.fattrib & AM_DIR) { /* It is a directory */

i = strlen(path);

sprintf(&path[i], "/%s", fno.fname);

res = scan_files(path); /* Enter the directory */

if (res != FR_OK) break;

path[i] = 0;

} else { /* It is a file. */

printf("%s/%s\n", path, fno.fname); --> THIS LINE IS NOT AT ALL EXECUTING EVEN THOUGH FILES ARE EXISTING IN THE SD CARD

}

}

f_closedir(&dir);

}

return res;

}

int main (void)

{

FATFS fs;

FRESULT res;

char buff[256];

res = f_mount(&fs, "", 1);

if (res == FR_OK) {

strcpy(buff, "/");

res = scan_files(buff);

}

return res;

}

WORKING CODE

 FRESULT res;

  FATFS SDFatFs; /* File system object for SD disk logical drive */

  FIL MyFile;   /* File object */

  FRESULT fr;

  char str[15];

  UINT br, bw; // File read/write count

  /**** capacity related *****/

  FATFS *pfs;

  DWORD fre_clust;

  uint32_t total, free_space;

  #define BUFFER_SIZE 128

  char buffer[BUFFER_SIZE]; // to store strings..

  int i=0;

  int bufsize (char *buf)

  {

   int i=0;

   while (*buf++ != '\0') i++;

   return i;

  }

  void clear_buffer (void)

  {

   for (int i=0; i<BUFFER_SIZE; i++) buffer[i] = '\0';

  }

 res = f_mount(&SDFatFs, (TCHAR const*)SDPath, 0);

if (res != FR_OK)

  while(1);

res= f_open(&MyFile, "STM32test.TXT", FA_WRITE);

if (res != FR_OK)

   while(1);

/* Writing text */

int ret;

ret = f_puts("Hello world", &MyFile);

f_close(&MyFile);

res = f_open(&MyFile, "STM32test.TXT", FA_READ);

if (res != FR_OK)

   while(1);

f_gets(buffer, f_size(&MyFile), &MyFile);

1 REPLY 1

Looks reasonable enough, instrument the code so you can see the failure/exit paths

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..