Finding location of first cluster of file on SDCard

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
liak
Registered User
Registered User
Posts: 195
Joined: Fri Oct 05, 2007 12:26 am

Finding location of first cluster of file on SDCard

Post by liak » Thu Jan 22, 2009 3:19 pm

Dear all,
I have been struggling a bit to understand FAT16. Hope I have a correct understanding. If I am wrong please correct me.

1. Now, I have been trying to find the first cluster location (addressed in sector count) for a file on a SDcard. I think I understood the gist of FAT16 but when I read through Steven's SDFilesystem module, I got perplexed. Can Steven or anyone point to me how to get this sector address? This information should be written in the FAT16 area of the SD card right?

2. If the card is formatted clean and with only one file written on it, can we guarantee that all the data be written in a sequential manner, simply meaning not fragmentated; one cluster after the other with no jumping in sequence?

Thanks.

Regards,
Liak

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Thu Jan 22, 2009 6:41 pm

In answer to (1), you will have to find the file entry in the root directory (these are a specified number of sectors near the start of the disk). The file entry will tell you the first cluster of the file. You will then need to convert this to a sector number (the first available cluster is number 2, and this starts at the beginning of the data area, then you need to jump on a certain number of sectors for each cluster). The position of the root directory, the start of the data area and the number of sectors in each cluster are all found from the boot sector, near the very beginning of the card.

In answer to (2), in theory, yes, the clusters used should be sequential. However, if a cluster has been marked as faulty, then the cluster in question will have been skipped. I think that this is unlikely though in an SD card, as I'm not sure that sectors can be corrupted in the same way as in a hard drive (although I could be wrong). If you are going to be sure, you will have to follow the chain of clusters by looking at the FAT table.

This isn't a detailed breakdown of the calculations involved, as it is quite complex, but there are plenty of sources of info on the web. You may find this helpful:

http://www.compuphase.com/mbr_fat.htm

Steve

normnet
Posts: 55
Joined: Mon Jan 01, 2007 6:32 pm

Post by normnet » Thu Jan 22, 2009 11:56 pm

2. If the card is formatted clean and with only one file written on it, can we guarantee that all the data be written in a sequential manner, simply meaning not fragmentated; one cluster after the other with no jumping in sequence?
Yes!
This is the means of attaining the 44k speed with a PIC for my soon to be posted
PIC wav player at (warning other Basic compiler) PIC iPod wav player.

Norm

liak
Registered User
Registered User
Posts: 195
Joined: Fri Oct 05, 2007 12:26 am

Post by liak » Fri Jan 23, 2009 12:53 am

Dear Steve and Norm,

Thanks a lot for the replies. I am going through your replies meticulously. Thank you Steve for the link provided. I've been searching through the net but couldn't find a truly authoritative article, not even at Microsoft tech forum, whom started the FAT architecture. Going through the article carefully now.
Will ask for some more help if I come up to any difficult problem.

And Norm, how do you put all different songs (.wav files) into one single file?


Thanks again.

Regards,
Liak

normnet
Posts: 55
Joined: Mon Jan 01, 2007 6:32 pm

Post by normnet » Fri Jan 23, 2009 3:20 am

And Norm, how do you put all different songs (.wav files) into one single file?
They are in separate files, each sequential following the previous, as placed on the SD card.
PIC Wav Player includes file order and loader software.

Norm

liak
Registered User
Registered User
Posts: 195
Joined: Fri Oct 05, 2007 12:26 am

Post by liak » Sat Jan 24, 2009 9:28 am

Dear Steve,

I sort of went through the SDFileSystem module file slowly, realizing how much work it must have been for you to bring it out. It's a daunting task, now I realize!!! Reading through it is already perplexing, not to mention of making it work. Thanks a thousand times for your effort and contribution :D , really appreciate it.
Just a few things I want to clarify, now that I have gotten a clearer picture.

1. Regarding the structure TFile and object File:

Code: Select all

Structure TFile
   SFR As Byte
   FileEntryDirectorySector As LongWord
   FileEntryDirectorySectorPos As Word
   [color=blue]FirstCluster As LongWord[/color]
   FATSector As LongWord
   FATSectorPos As Word
   ClusterSequenceIndex As Word
   ClusterSequenceNumber As Word
   CurrentSectorInCluster As Byte
   CurrentSector As LongWord
   CurrentSectorPos As Word
   Size As LongWord
   BytesRead As LongWord
   EOF As SFR.Booleans(0)
   IsOpen As SFR.Booleans(1)
   RW As SFR.Bits(2)                   // 0 = Write, 1 = Read
   RecordLength As Word
End Structure   
The attribute .FirstCluster denotes the first cluster location of the particular file. Am I correct?

2. To find the value of .FirstCluster of a file, I would have to use the function OpenFileRW. Am I right?

3. This is a bit tricky for me; I am still struggling to understand this. The other attribute .FATSector denotes the starting FAT record for the particular file? From here the two bytes denote the next FAT entry for the file?

4. I must say I am a bit confused over the functions of the following:
FATtoCluster
ClustertoFAT
DatatoCluster
ClustertoData

Can you explain it a bit further. The comments are a too brief and I am a bit lost in the codes.

Thanks again.

Regards,
Liak

liak
Registered User
Registered User
Posts: 195
Joined: Fri Oct 05, 2007 12:26 am

Post by liak » Sun Feb 08, 2009 4:03 am

Dear Steve,

Finally I have gotten it correct.

Thanks.
Regards,
Liak

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Feb 08, 2009 9:39 am

That's great news - sorry I forgot to reply to your previous post.

Post Reply