FAT Component

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

FAT Component

Post by cobra1 »

Hi,

I was wondering if it were possible to detect when an SD card is full using the FAT component.

I was looking to stream continuous data to a card but i would need an alert when the card is full.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: FAT Component

Post by Benj »

Hello,

There are basically two methods to write data to the card.

1) Simple using the AppendStringToFile function.
2) More advanced but faster WriteBuffer and MoveToNextFileSector

Both functions have a return value which will be 0 for success and greater than 0 to indicate a fail which could indicate no room left on the card.

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: FAT Component

Post by cobra1 »

Thanks for the quick reply Ben.

I just did some quick number and i think i am correct that a 1gb sd card has 2,097,152 sectors.

I dont suppose its feasable to keep a count of how many sectors have been written too??

I dont fully understand how an sd card receives its data. Is it sequential?? For example if i had a few bmp images taking say 10 sectors then i create a new file to store my data stream, does it start from sector 11??

Thanks

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: FAT Component

Post by Benj »

Hello,

A sector is a collection of 512 sequential bytes.

A cluster is a set of sequential sectors (normally 32 or 64 or 128 depending on the size of the card)

A file occupies a cluster or group of clusters, even if the file is 1 byte in size it will consume the entire cluster on the disk due to the limitation of the FAT file system.

The way FAT works is that when you try and allocate a new sector it will look in the current cluster and if that is out of room or not applicable then the file system will look for the next free cluster to store the data.

E.g. if you have a full disk and then start writing a file it may start at cluster 22. If you then deleted a file and say freed up cluster 12 then once you run out of space writing to cluster 22 the next likely cluster for part 2 of the data in the file will be cluster 12. The cluster's don't have to be in order just as long as the FAT chain is valid.

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: FAT Component

Post by cobra1 »

That makes sense, thanks for the explanation Ben

:)

Post Reply