FatFs and SD mmc ?

Go To Last Post
114 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I fixed it, pin 56 is not connected properly on PCB...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried to read a free space on FAT32 SDcard, based on :
http://elm-chan.org/fsw/ff/en/ge...

How can I use _FS_NOFSINFO ?
Because the size I got on UART was not right :
FREE SPACE ON SDCARD
Free space : 692517509 bytes Total space : 199 bytes

My SDCard is 1Gb and is being used about 30Mb

thanks
code:

/* Get volume information and free clusters of drive 1 */
	res = f_getfree("1:", &fre_clust, &fs);
	//if (res) die(res);

	/* 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) */
	printf("%10lu KiB total drive space.\n%10lu KiB available.\n",
	tot_sect / 2, fre_sect / 2);
	sprintf_P( &temp_buffer, PSTR(" Free space : %10lu bytes "), fre_sect  );
	usart_pstr( &temp_buffer );
	sprintf_P( &temp_buffer, PSTR(" Total space : %10lu bytes "), tot_sect  );
	usart_pstr( &temp_buffer );
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
res = f_getfree("1:", &fre_clust, &fs); 
if (res)
  {
  usart_pstr( "f_getfree() failed\n" );
// now print out the return value 'res'. (and maybe the values of fs->n_fatent, fs->csize, free_clust and fs->csize but they will probably contain rubbish).
  }
else
  {
  /* Get total sectors and free sectors */

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

Functions that provide return values do so for good reasons., ignore the returned values at your peril.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ups....I got :

FREE SPACE ON SDCARD
f_getfree() failed

What do I miss here ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have only one drive,
Is this right ?

res = f_getfree("1:", &fre_clust, &fs);

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Is this right ?

How could anyone possibly say without seeing the definition of fre_clust?
Quote:

What do I miss here ?

The code which would have printed out the "res" value. The whole reason Chan returns a numeric error code is so you know what the error was. Without knowing the FR_xxx code number how can anyone guess what f_getfree() in objecting too? Mike's example code even said:
Quote:
// now print out the return value 'res'. (and maybe the values of fs->n_fatent, fs->csize, free_clust and fs->csize but they will probably contain rubbish).

Did you have some reason for not following that excellent advice?

BTW have you ever considered getting a debugger? While you can achieve a lot with UART printing what you are no doubt finding is that when something is wrong you first have to determine which values might be useful to see and then to add code to print those then rebuild and re-run.

If you had a debugger you could just breakpoint the unchanged code after the return from the f_getfree() call and immediately examine what was in "res" and "fre_clust" and so on. It makes the development cycles MUCH quicker and easier, especially when working on "complex" software like FatFs.

You would even be able to step into the f_getfree() function and see what FatFs was "thinking" - which code path it takes and why, ultimately, it has chosen to return the error code.

Oh and while FR_OK is 0 and you can just:

if (res) {
}

I think I'd be tempted to:

if (res != FR_OK) {
}

just in case there's ever an issue of FatFs where FR_OK is not 0. (as long as it is 0 the optimiser will almost certainly generate identical code anyway).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the explaination Clawson, I followed the function and found the answer...

I have another question, I tried to read a file from inside of subdirectory, but always failed....

I passed the file name, but how can I read a file inside sub directory ? thanks

code :

	
fn = fno.fname;
if (fno.fattrib & AM_DIR) {                   /* It is a directory */
	sprintf(&path[i], "/s", fn);
	res = scan_files(path);
	if (res != FR_OK) break;
				path[i] = 0;
			} else {                        /* It is a file. */
		printf("s/s\n", path, fn);
				
				usart_pstr(" ");
sprintf_P( &temp_buffer, PSTR(" 9lu bytes "), fno.fsize );
usart_pstr( &temp_buffer );
byte_counter = fno.fsize / 512;
sprintf_P( &temp_buffer, PSTR(",byte counter => 9lu "), byte_counter );
usart_pstr( &temp_buffer );usart_pstr("\n");
		PlayMP3(fn);
			}

output on uart :

/CLK10OCLOCK.MP3 22150 bytes
/CLK

directory = CLK
file = 1oclock.mp3

it can see the file but not pointing correctly to the file ?...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I followed the function and found the answer...

Don't you think it would be rather nice for the next reader of this thread if you actually said what that was? Maybe someone else is trying to use f_getfree() and hits the same problem/misunderstanding/whatever and could benefit from your experience.
Quote:

but how can I read a file inside sub directory ?

I've never really understood the point of turning on directory support in FatFs. You can have 65535 files in the root directory on FAT32.

But anyway I imagine it's the same as you do in MS-DOS/Windows/whatever. You set the "current directory" to be the place you want to read/write files then use fopen/fread/write and the filename in the fopen will be conglomerated with the current directory.

Something like:

f_chdir("/CLK");
f_open(&fil, "1oclock.mp3", FA_READ);

or whatever.

However I don't know because I've never seen the need. YMMV.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I've never really understood the point of turning on directory support in FatFs. You can have 65535 files in the root directory on FAT32.

1) Indeed I was dragged kicking and screaming into FAT32, given the size of modern cards. But FAT16 does indeed give a smaller and faster implementation.

2) I started my "important" SD logging app with directories in mind, partly to avoid an overwhelming number of files in the root directory. I have /CONFIG, /BOOT, and a /LOGyymm directory for each month. IMO makes things much easier to manage when working with the gathered data.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

when I tried :

FRESULT scan_files (
char* path        /* Start node to be scanned (also used as work area) */
)
{
	FRESULT res;
	FILINFO fno;
	DIR dir;
	int i;
	char *fn;   /* This function is assuming non-Unicode cfg. */
	unsigned long int byte_counter;
	BYTE temp_buffer[100];
res = f_chdir("/MP3");

The compiler gave me :
Error 27 undefined reference to `f_chdir'
Error 2 ld returned 1 exit status collect2.exe 0 0 MP3_LCD_Apps

it's already defined :

#include "ff.h"		/* Declarations of FatFs API */

What's missing here ?
Is it because :

FRESULT scan_files 

?

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How can I read 250 bytes from the start of the file ?

I've tried :

  if (f_open(&Fil, "test.txt", FA_READ) == FR_OK);
		{
	  		f_read(&Fil, line, 250, bw);
	   
	 usart_pstr("\nReading file 250 bytes \n");
		 if (bw != 250)
		 usart_pstr(line);
		  f_close(&Fil);
	 usart_pstr("\nFinished reading 250 bytes\n");
		}

but got no luck yet, any ideas ?
thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
but got no luck yet, any ideas ?
Well, my mind-reading ability is rather poor nowadays, but that fragment of code, combined with the lack of information as to what you have observed, makes me guess that ;
- FatFs might not been properly initialised/configured.
- f_open(,,) did not return FR_OK.
- your test.txt file is not where your program expects to find it.
- f_read(,,) failed (and you don't care because you don't bother to check if it returned FR_OK)
- your test file has non-printable characters in it.
- there are not 250 bytes in the test file.
- the semicolon on the end of if (f_open(&Fil, "test.txt", FA_READ) == FR_OK); would not be helping matters.

throw us some bones of information to chew on

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can f_read read 0x00 ? It seems that I can't read 0x00 on Mp3 file after ID3 ?

Pages