cancel
Showing results for 
Search instead for 
Did you mean: 

f_getfree API returns FR_INVALID_DRIVE error

Niyathi Shenoy
Associate II
Posted on March 15, 2018 at 10:31

Hi clive,

I am using a 32 GB SD card,

My code by using the API f_getfree() is as follows:

if (f_mount(&myFATAFS, SDPath, 1) == FR_OK) {

HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);

f_getfree('1:', &Free_clust, &myFATAFS);

tot_sect = (myFATAFS.n_fatent - 2) * myFATAFS.csize;

fre_sect = Free_clust * myFATAFS.csize;

sprintf(coeff3,'%10lu MiB total drive space.\n\r %10lu MiB available.\n\r', tot_sect/2/1024,Free_clust/2/1024);

HAL_UART_Transmit(&huart2,(uint8_t *) coeff3, 65, 50);

}

But the output that i get is as follows :

30416 MiB total drive space.

0 MiB available.

However, my SD card is formatted and there is nothing in it.Please help me here.

Also, f_getfree API returns a FR_INVALID_DRIVE error i.e., finding the logical drive function returns me the volume as -1 even if my sd card is connected.But a f_write api writes files and directories into the sd card. Please help. 

1 REPLY 1
Posted on March 15, 2018 at 14:40

This works for me

void printsize(uint32_t size, char *s)

{

  char str[32];

  if (size >= 1000000)

    sprintf(str, ' %6.1lf GB %s', (double)size / 1000000.0, s);

  else if (size >= 1000)

    sprintf(str, ' %6.1lf MB %s', (double)size / 1000.0, s);

  else

    sprintf(str,' %6.1lf KB %s', (double)size, s);

  puts(str);

}

void testfree(void)

{

  FATFS *fs;

  uint32_t fre_clust, fre_sect, tot_sect;

  char *str;

  /* Get volume information and free clusters of drive */

  res = f_getfree('', &fre_clust, &fs); /*

mailto:sourcer32@gmail.com

*/

  if (res != FR_OK)

  {

#ifdef DBG

    printf('res = %d f_getfree\n', res);

#endif

    return;

  }

  switch(fs->fs_type)

  {

    case FS_FAT12 : str = 'FAT12'; break;

    case FS_FAT16 : str = 'FAT16'; break;

    case FS_FAT32 : str = 'FAT32'; break;

#ifdef FS_EXFAT

    case FS_EXFAT : str = 'EXFAT'; break;

#endif        

    default : str = 'Unknown FAT';

  }

  puts(str);

  /* Get total sectors and free sectors */

  tot_sect = (fs->n_fatent - 2) * fs->csize;

  fre_sect = fre_clust * fs->csize;

  /* Print the free space (assuming 512 bytes/sector) */

  printsize(tot_sect / 2, 'total drive space');

  printsize(fre_sect / 2, 'available');

  printsize((tot_sect - fre_sect) / 2, 'used');

}

BTW sprintf() returns the string length, use that rather than assuming constant length

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