DWORD and f_size ?

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys,

I tried to display f_size on FATFs,
I want to retrieve file size.

code :

DWORD	fsize;			/* File size */

 usart_pstr(" ");
				sprintf_P( &temp_buffer, PSTR("file size => u\n"), fno.fsize );
				usart_pstr( &temp_buffer );

but the result is different with I see on file size my computer

on uart :
Scan directory mp3
AIRSUP~2.MP3
file size => 15012
AIRSUP~3.MP3
file size => 63616
AIRSUP~4.MP3
file size => 29023

at my computer

Directory of G:\mp3

07/03/2014 12:39 PM

.
07/03/2014 12:39 PM ..
08/12/2013 05:28 PM 3,881,636 airsupply3.mp3
08/12/2013 05:04 PM 8,255,616 airsupply2.mp3
08/12/2013 05:28 PM 4,354,399 airsupply1.mp3
3 File(s) 16,491,651 bytes
2 Dir(s) 978,108,416 bytes free

G:\mp3>

Any clues ?

thanks

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

Uart sizes are all less than a word. Don't you need a DWORD?

Imagecraft compiler user

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

Why yet another thread when you already have ones discussing FatFs?

Anyway look at the AVR implementation of FatFS and you will find:

/* These types must be 32-bit integer */
typedef long			LONG;
typedef unsigned long	ULONG;
typedef unsigned long	DWORD;

(if I ever had an argument with FatFs it is his invention of these types rather than using !)

Anyway DWORD is an "unsigned long" on an AVR. That makes it 32 bits or 4 bytes.

You cannot therefore use %u in a printf() format string for it. You need to use %lu where the additional l (that is lower case L for Lima) says "long".

To explain the results you got (that effectively truncated 32 bits into 16 bits) consider a size such as 3,881,636 which in hex is 0x003B3AA4. If you truncate that to 16 bits you just get 0x3AA4 and 3AA4 is 15,012 in decimal - which is what you saw.

If you ever suspect truncation you can easily do this. Convert the "big number" to hex then chop the digits to half the number, take the lower half and convert back to decimal. If that is the value you are seeing then you are suffering from truncation.

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

Gosh that was a nice calm explanation. I'll give it a like when the New Voting System appears.

Imagecraft compiler user