Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hello everybody,

I want to calculate the free space of a SD-Card. The SD-Card is either formatted to FAT16 or
to FAT32.
The calculation is performed by a M16C uC which is connected via SPI to the card.

I have a little clue how to calculate but I don't know if my idea is high in performance.

My idea:
Reading following from parameters "Boot sector"
- Total sector (0x13/0x20)
- Sector per cluster (0x0d)
- Bytes per sector (0x0b)

Then calculating
Total sector / Sectors per cluster=> Total clusters

Then I look up the FAT and calculate how much clusters of total cluster are free

For example
free_cluster=0;
for(int i=0;i<total mode="hold" />{
  if(FAT[i]==0)//Then cluster is free
    free_cluster++;
}



Finally, I would calculate the size like this:
Size_of_cluster=Bytes per sector*Sector per cluster;
free_space=free_cluster * Size_of_cluster;


What do you think about my solution?
Is there any other way how to solve the problem?
How does Windows calculate this if I insert a SD-Card?
How would you solve it?

Thank you for helping
Posted
Updated 21-Jan-11 14:06pm
v2

You can just use GetDiskFreeSpaceEx (you need to know the drive name that Windows has mapped the SD card to).

Example:
-----------------

C++
int _tmain()
{
    ULARGE_INTEGER freeBytesAvailable;
    ULARGE_INTEGER totalNumberOfBytes;
    ULARGE_INTEGER totalNumberOfFreeBytes;
    GetDiskFreeSpaceEx(L"J:\\", &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes);
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 18:13pm    
As simple as that - my 5.
Nish Nishant 21-Jan-11 18:17pm    
A hypothetical 5 apparently, but thanks nonetheless ;-)
Sergey Alexandrovich Kryukov 21-Jan-11 19:35pm    
I agree if you say it's not that easy. It depends what you want want, because the disk is fragmented, etc. But that's would be beyond the question. Now I see, OP makes your answer totally irrelevant. But that's the (big) fault of OP, not yours (in fact, very nasty of OP to formulate a question like that, waste our time for nothing).
So, this is not a valid reason to re-vote, its the OP deserved big down-vote. :-)
Nish Nishant 21-Jan-11 21:33pm    
Kryukov, I was kidding there. I said that because you commented with "5" and yet there were no votes. It was just an attempt at humor on my part :-)
Sergey Alexandrovich Kryukov 21-Jan-11 22:05pm    
I kind of noticed your smile :-)
It's just so happened that when answering to your comment I spotted another "joke" -- by OP, and then...
Your method to calculate the free space seems fine.
I do spot one error though in the implementation:

The first two entries of the FAT don't map to clusters but hold other information. This is either 2x16 or 2x32 bits depending on if you're using FAT16 or FAT32. Although this data won't be 0 anyway, it shouldn't be used in the calculation.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900