Finding location of first cluster of file on SDCard
Moderators: David Barker, Jerry Messina
Finding location of first cluster of file on SDCard
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
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
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
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
Yes!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?
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
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
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
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 , 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:
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
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 , 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
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