AT89C52 interface SD Memory Card
Wed Jan 21 2009, 01:24 pm
Thu Jan 22 2009, 02:11 am
Using your memory dump above, the following can be calculated:
BytesPerSector = 0x0200
SectorsPerCluster = 0x0020;
ReservedSectors = 0x0001;
NumberOfFats = 0x0002;
RootMaxFiles = 0x0200;
TotalSectors = 0x0007999B;
SectorsPerFat = 0x003D;
/* calculate FAT starting sector, root and data area variables... */
FatStartSector = (LBA_StartAddress + ReservedSectors); // = 0x00000001
RootStartSector = FatStartSector + (NumberOfFats * SectorsPerFat); // = 0x0000007B
DirEntriesPerSector = ( BytesPerSector / DIR_ENTRY_SIZE ); // = 0x10
TotalRootSectors = RootMaxFiles/DirEntriesPerSector; // = 0x20
DataStartSector = RootStartSector + TotalRootSectors; // = 0x0000009B
/* Calculate file system ID */
NumberOfClusters = (TotalSectors - DataStartSector) / SectorsPerCluster; // = 0x00003CC8
if( NumberOfClusters < 0x00000FF5 )
fat_ID = 12;
else
if( NumberOfClusters < 0x0000FFF5 )
fat_ID = 16;
else
fat_ID = 32;
So your fat_ID would be 16...
-Dave
BytesPerSector = 0x0200
SectorsPerCluster = 0x0020;
ReservedSectors = 0x0001;
NumberOfFats = 0x0002;
RootMaxFiles = 0x0200;
TotalSectors = 0x0007999B;
SectorsPerFat = 0x003D;
/* calculate FAT starting sector, root and data area variables... */
FatStartSector = (LBA_StartAddress + ReservedSectors); // = 0x00000001
RootStartSector = FatStartSector + (NumberOfFats * SectorsPerFat); // = 0x0000007B
DirEntriesPerSector = ( BytesPerSector / DIR_ENTRY_SIZE ); // = 0x10
TotalRootSectors = RootMaxFiles/DirEntriesPerSector; // = 0x20
DataStartSector = RootStartSector + TotalRootSectors; // = 0x0000009B
/* Calculate file system ID */
NumberOfClusters = (TotalSectors - DataStartSector) / SectorsPerCluster; // = 0x00003CC8
if( NumberOfClusters < 0x00000FF5 )
fat_ID = 12;
else
if( NumberOfClusters < 0x0000FFF5 )
fat_ID = 16;
else
fat_ID = 32;
So your fat_ID would be 16...
-Dave
Thu Jan 22 2009, 04:39 am
to continue from above...
Once you have calculated the DataStartSector (0x0000009B), which I have shown above, you can find your data address:
DataStartSector X BytesPerSector = 0x9B X 0x200, or 0x13600
Now look at your second data dump, at address 0x13600, and you will see your data...
Is this starting to make sense now?
Ajay, Please feel free to comment...
-Dave
Once you have calculated the DataStartSector (0x0000009B), which I have shown above, you can find your data address:
DataStartSector X BytesPerSector = 0x9B X 0x200, or 0x13600
Now look at your second data dump, at address 0x13600, and you will see your data...
Is this starting to make sense now?
Ajay, Please feel free to comment...
-Dave
Fri Jan 23 2009, 03:25 am
Well i thought to put some light with screenshots I took them fast i was busy though
so lets start our journey from sector 0
You see the shaded area? good
now lets compare it with this one..
LBA is at offset 0x8 and its where our first partition is located.
from shaded area re-arranging those 4 bytes we get 0x100 = 256.
lets go to sector 256
voila! its the first partition or Logical sector 0 for windows
ok ok lets compare it with this.
1. Bytes Per Sector (offset 0x0B) = 0x0200 = 512 (from shaded area)
2. Sectors Per Cluster (0x0D) = 0x02
3. Number of Reserved Sectors (0x0E) = 0x0020
4. Number of FATs(0x10) = 0x02
5. Sectors Per FAT (0x24) = 0x03CC
6. Root Directory First Cluster (0x2C) = 0x00000002
Now finding first cluster:
We will take help of this
[img]http://www.pjrc.com/tech/8051/ide/fat32_layout.gif[img]
clearly from image we found that..
First_cluster_LBA = LBA_Addr + No_of_reserved_secs + No_of_Fat * Sec_per_fat
so.. putting values from above
First_Cluster_LBA= 256 + 32 + 2 * 972 (all are converted to decimal)
First_Cluster_LBA = 2232
Lets go to 2232 now
wow this is root directory named "SSN" and i can see the file name i had in there "Hello.txt" nice
so lets start our journey from sector 0
You see the shaded area? good
now lets compare it with this one..
LBA is at offset 0x8 and its where our first partition is located.
from shaded area re-arranging those 4 bytes we get 0x100 = 256.
lets go to sector 256
voila! its the first partition or Logical sector 0 for windows
ok ok lets compare it with this.
1. Bytes Per Sector (offset 0x0B) = 0x0200 = 512 (from shaded area)
2. Sectors Per Cluster (0x0D) = 0x02
3. Number of Reserved Sectors (0x0E) = 0x0020
4. Number of FATs(0x10) = 0x02
5. Sectors Per FAT (0x24) = 0x03CC
6. Root Directory First Cluster (0x2C) = 0x00000002
Now finding first cluster:
We will take help of this
[img]http://www.pjrc.com/tech/8051/ide/fat32_layout.gif[img]
clearly from image we found that..
First_cluster_LBA = LBA_Addr + No_of_reserved_secs + No_of_Fat * Sec_per_fat
so.. putting values from above
First_Cluster_LBA= 256 + 32 + 2 * 972 (all are converted to decimal)
First_Cluster_LBA = 2232
Lets go to 2232 now
wow this is root directory named "SSN" and i can see the file name i had in there "Hello.txt" nice
DavesGarage like this.
Mon Jan 26 2009, 02:01 pm
Dear Fren,
I have followed your explanation and do the exercise. I wonder why my SD cart no show the 16-byte partition entry ( http://www.pjrc.com/tech/8051/ide/partition.gif ) format.
I have calculate the First_cluster_LBA = 1976 but the data is start from 1980 sector. Please can you check for me where my mistake?
Thank you,.
I have followed your explanation and do the exercise. I wonder why my SD cart no show the 16-byte partition entry ( http://www.pjrc.com/tech/8051/ide/partition.gif ) format.
I have calculate the First_cluster_LBA = 1976 but the data is start from 1980 sector. Please can you check for me where my mistake?
Thank you,.
[ Edited Mon Jan 26 2009, 02:01 pm ]
Tue Jan 27 2009, 12:35 am
the reason u are not able to see the partition table is coz you have not opened the right drive. from the drive list you will see two lists under + sign
see the second list where physical drives are mentioned, that will take u to MBR, right now u are viewing logical sector 0 as i mentioned in my previous post.
see the second list where physical drives are mentioned, that will take u to MBR, right now u are viewing logical sector 0 as i mentioned in my previous post.
Tue Jan 27 2009, 03:46 am
I too, have a question:
Now that I've calculated all of the following:
Virtual Bood Record: 0x0001E600
FAT starting address: 0x0001E800
Root Directory starting address: 0x0005C000
Data starting address: 0x00060000
and my data looks like this:
I assume by the fact that the directory is only 1 cluster, which is 512 entries by 32 bytes each, and it is referred to as the "first" cluster, that the first data sector is cluster 2, which is 0x4000 bytes AFTER the directory starting address, or 0x60000...
Also, I see the name of the sample text file (SAMPLE.TXT), and I see it's length is 0x26 bytes long, and that it starts at cluster #2, which is the start of the data sector.
What I don't see is how the FAT entry is tied to the file information.
Now that I've calculated all of the following:
Virtual Bood Record: 0x0001E600
FAT starting address: 0x0001E800
Root Directory starting address: 0x0005C000
Data starting address: 0x00060000
and my data looks like this:
I assume by the fact that the directory is only 1 cluster, which is 512 entries by 32 bytes each, and it is referred to as the "first" cluster, that the first data sector is cluster 2, which is 0x4000 bytes AFTER the directory starting address, or 0x60000...
Also, I see the name of the sample text file (SAMPLE.TXT), and I see it's length is 0x26 bytes long, and that it starts at cluster #2, which is the start of the data sector.
What I don't see is how the FAT entry is tied to the file information.
Wed Jan 28 2009, 12:39 am
After some reading, I may have answered my own question. Ajay, perhaps you can correct me if I am wrong...
To calculate the position within the FAT, you simply look at the file definition block. Within the file definition block, at relative address 26, there is 02 00. After you switch them around, you get 0x0002, which is NOT ONLY the data cluster, but the POSITION within the FAT where the reference to the next cluster is found. If this reference is 0xFFFF, then there are no more clusters to be read.
Referring to the attached image:
The first file, "SAMPLE.TXT", has 0x0002 in position 26, which not only means the data for the file starts at cluster #2, but also means that in position #2 of the FAT, the next cluster will be listed. Since this value is 0xFFFF, this means there is no more data...
The second file, "TEST.HEX", has 0x0003 in position 26, which not only means the data for the file starts at cluster #3, but also means that IN position #3 of the FAT, the next cluster will be listed. When I look at position #3 of the FAT, I see a value of 0x0004. This means cluster #4 will have more data from the file, but also means that in position #4 of the FAT, the next cluster will be listed. When I look at position #4 of the FAT, I see a value of 0x0005.
This process repeats, but when I look in position #5 of the FAT, I see 0xFFFF, which means there isn't any more data...
Is this correct?
-db
To calculate the position within the FAT, you simply look at the file definition block. Within the file definition block, at relative address 26, there is 02 00. After you switch them around, you get 0x0002, which is NOT ONLY the data cluster, but the POSITION within the FAT where the reference to the next cluster is found. If this reference is 0xFFFF, then there are no more clusters to be read.
Referring to the attached image:
The first file, "SAMPLE.TXT", has 0x0002 in position 26, which not only means the data for the file starts at cluster #2, but also means that in position #2 of the FAT, the next cluster will be listed. Since this value is 0xFFFF, this means there is no more data...
The second file, "TEST.HEX", has 0x0003 in position 26, which not only means the data for the file starts at cluster #3, but also means that IN position #3 of the FAT, the next cluster will be listed. When I look at position #3 of the FAT, I see a value of 0x0004. This means cluster #4 will have more data from the file, but also means that in position #4 of the FAT, the next cluster will be listed. When I look at position #4 of the FAT, I see a value of 0x0005.
This process repeats, but when I look in position #5 of the FAT, I see 0xFFFF, which means there isn't any more data...
Is this correct?
-db
Sat Jan 31 2009, 01:57 am
Just to let you know, I have successfully ported Chan's FAT routines to my host, and have everything working now. Very cool indeed...
My first goal is to make a data logger...
-db
My first goal is to make a data logger...
-db
Sat Jan 31 2009, 02:55 am
wow congrats dave i missed out your last post. still glad to here.
Can you please give us some hint or guidelines for porting?
Can you please give us some hint or guidelines for porting?
Powered by e107 Forum System