SD card FAT file system

Source: Internet
Author: User
Initialize the SD card.

I currently use the SPI mode, so I will only discuss the SPI mode here. In spi mode, all commands require that the CS foot be set to 0 first. So I will not emphasize this in the future.

Swap 0 to switch the SD card from SD mode to SPI mode. Judge the return value R0. If it is not card is not ready, it indicates that there is a problem with the hardware.
Listen 8, the parameter is 0x000001aa, to determine which standard the SD card meets. If illegal command is returned, ver1.x is used; otherwise, ver2.0 is used.
Cmd1: For ver2.0 card, the parameter is 1 <30; otherwise, it is 0. Read the SD card status repeatedly until card is ready or times out. There is a problem here. We recommend that acmd1 be used in spec, which is more universal than cmd1, and cmd1 is not available in the first place. However, in actual use, I found that some old cards will return parameter error for this command. I don't know why, and cmd1 is also very useful in actual use, and there is no problem that the SD card cannot be initialized, so I am using cmd1 directly now.

The SD card Initialization is complete. Then you can read the data.

2. Read CID (limit 10) and SD card identification information. This step is not required.

The authentication information includes the producer ID, Application ID, product name, product version, product serial number, and production date.

3. Read the CSD (cost 9) and SD card information.

The returned CSD has two versions.

If (CSD [0] & 0x40 = 0x40) // ver2.0 card
Capacity = 1024l * (CSD [8] <8 + CSD [9])
This number is the total number of sectors in the SD card.

If (CSD [0] & 0x40! = 0x40) // ver1.x card
Capacity = (CSD [6] & 0x03) <10) | (CSD [7] <2) | (CSD [8] & 0xc0)> 6) + 1) * (1 <(CSD [9] & 0x03) <1) | (CSD [10] & 0x80)> 7) + 2 ))

4. Read the partition table.
Read the SD card sector 0 to Buf.
If Buf [0] is not 0xeb or 0xe9, it indicates that this is a partition table.
The four bytes starting from Buf [454] are the number of sectors before the partition. For example, 63 indicates that there are 63 sectors before this partition, and the first sector of this partition is sector 63.
The four bytes starting from Buf [458] are the number of sectors of the partition. For example, 7990000. This partition does not have 7990000 sectors. The block address of this partition is 63 ~ 7990063.
A device can have up to four partitions. The data related to the other three partitions is in Buf [470]/Buf [474], Buf [486]/Buf [490], buf [502]/Buf [506].
Note: When reading the Buf, check the best two digits, which should be 55aa.

5. Read BPB.
Well, now we know where the first partition is. What we need to do now is to read the 0 sector of the partition. That is, the 36th slice of the SD card.
The 512 bytes are detailed information about the partition.

Check the Buf [0], which should be 0xeb or 0xe9. If yes, it is a BPB table. The following important figures are:
The sectors per cluster of BUF [13] is the number of sectors of each cluster, that is, the number of sectors that you need to read at a time when reading files. For example, 8.
Reserved sectors of BUF [14-15], number of reserved sectors, which are the number of sectors between the first sector of the partition and the fat table. For example, 38.
The hidden sectors of BUF [28-31] indicates the number of hidden sectors. This hidden sector refers to the number of sectors in front of the partition. This value also exists in the partition table. Is 63.
Buf [19-20] And Buf [32-35]. The former is the number of sectors in fat16 format, and the latter is the number of sectors in FAT32 format. This value also exists in the partition table. 7990000.
Buf [16], number of fat tables, usually 2.
Buf [22-23], Buf [36-39], the number of sectors occupied by the fat table. The former is fat16, and the latter is FAT32. For example, 7793.

Now we can read the data in the FAT file system.

6. Read the fat table.
In the above example, the position of the fat table should be the first sector of the partition and then offset the number of hidden sectors, that is, 63 + 38, equal to 101. Therefore, from the 101 sector of the SD card, it is the fat table of partition 1, with a total of 7793 sectors.

7. Read the root directory.
In fat16, the root directory is independent. It should be possible to create 512 files, each occupying 32 bytes, so the total is 512*32/512 = 32 sectors.
The root directory is behind the fat table, so the first sector is 101 + 7793*2 = 15687.

For FAT32 systems, this section is ignored.

8. Read data area.
In fat16, the data area is behind the root directory, so the first sector is 15687 + 32 = 15719. It is the 15,719th slice on the SD card.

In FAT32, because there is no root directory, the data zone is directly behind the fat table, so it is 15,687th sectors. This sector is where LBA = 2. Therefore, if the upper-layer file operation function needs to write data to LBA = 1000, it is to write data to 1000 + 15687 = 16687 sectors.

At this point, we get the structure and data of the entire fat system, and then we can start to operateComposition.

For your security, please only open the URL with reliable source

Cancel website opening

From:
Http://hi.baidu.com/zcw862341709/blog/item/9c045cec1962cd4779f05574.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.