SD card introduction

From below punctual atoms ofFPGA Development Guide, I thought it was good, so I moved over.

1. Introduction to SD Card

The full English name of SD card is Secure Digital Card, namely Secure Digital Card (also known as Secure Digital Card), which is developed on the basis of MMC (Multimedia Card), with two main features: higher Security and faster reading and writing speed. The length and width of the SD card and MMC card are both 32mm x 24mm. The difference is that the thickness of the SD card is 2.1mm, while the thickness of the MMC card is 1.4mm. The SD card is slightly thicker than the MMC card to accommodate larger capacity. The storage unit, meanwhile, the SD card has more contact pins than the MMC card, and there is a write-protect switch on the side. SD cards and MMC cards maintain upward compatibility, that is, MMC cards can be accessed by new SD devices, and compatibility depends on application software, but SD cards cannot be accessed by MMC devices. SD card and MMC card can be distinguished by the label on the card. In the picture below, the one with the letter "MultiMediaCard" on the left side of the picture is an MMC card, and the one with the letter "SD" on the right side of the picture is an SD card.

Insert picture description here
The SD card in the right picture above is actually an SDHC card. The SD card is divided into 3 levels in terms of storage capacity, namely: SD card, SDHC card (Secure Digital High Capacity, high-capacity secure digital card) and SDXC card (SD eXtended Capacity, a secure memory card with expanded capacity). SD card is developed on the basis of MMC card, using FAT12/FAT16 file system, SD card adopts SD1.0 protocol specification, which stipulates that the maximum storage capacity of SD card is 2GB; SDHC card is a large-capacity storage SD card, Using FAT32 file system, SDHC card adopts SD2.0 protocol specification, which stipulates that the storage capacity of SDHC card ranges from 2GB to 32GB; SDXC card is a newly proposed standard, which is different from the FAT file system used by SD cards and SDHC cards. The SDXC card uses the exFAT file system, that is, the extended FAT file system. The SDXC card adopts the SD3.0 protocol specification, which stipulates that the storage capacity of the SDXC card ranges from 32GB to 2TB (2048GB), and is generally used for mid-to-high-end SLR cameras and high-definition camcorders.

SD cards with different protocol specifications have different speed levels. In the SD1.0 protocol specification (used less now), "X" is used to indicate different speed levels; in the SD2.0 protocol specification, SpeedClass is used to indicate different speed levels; the SD3.0 protocol specification uses UHS (Ultra High Speed) means different speed levels. In the SD2.0 specification, the speed grades of SD cards are divided into ordinary cards (Class2, Class4, Class6) and high-speed cards (Class10); the SD3.0 specification divides the speed grades of SD cards into UHS speed grades 1 and 3. The read and write speeds and applications of different levels are shown in the figure below.
Insert picture description here
The SD card has 9 pin lines in total and can work in SDIO mode or SPI mode. In SDIO mode, shared to CLK, CMD, DAT[3:0] six signal lines; in SPI mode, shared to CS (SDIO_DAT[3]), CLK (SDIO_CLK), MISO (SDIO_DAT[0]), MOSI (SDIO_CMD) Four signal lines. SD card interface definition and description of each pin function are shown in Figure 39.1.3.

Insert picture description here
In addition to standard SD cards on the market, there are also MicroSD cards (formerly known as TF cards), which are extremely small flash memory cards, invented by SanDisk (SanDisk), and are mainly used in mobile phones. MicroSD card can be converted into SD card by inserting the adapter (Adapter), and its operation sequence is the same as that of SD card. The definition of the MicroSD card interface and the description of each pin function are shown in Figure 39.1.4.
Insert picture description here
In the standard SD card version 2.0, the working clock frequency can reach 50Mhz. In SDIO mode, it adopts 4-bit data width, and theoretically can reach 200Mbps (50Mx4bit) transmission rate; in SPI mode, it adopts 1-bit data width, theoretically It can reach a transmission rate of 50Mbps. Therefore, the transfer rate of the SD card in SDIO mode is faster, and its operation sequence is more complicated. For using SD card to read music files and pictures, the transmission speed in SPI mode has been able to meet our needs, so we use the SPI mode of SD card in this chapter to test the reading and writing of SD card.

2. SD card command and return data

The SD card must be initialized before the normal read and write operations. The initialization process of the SD card is to write commands to the SD to make it work in the expected working mode. When reading and writing the SD card, it is also necessary to send the write command and the read command first, so the command format of the SD card is an important content for learning SD cards. The command format of the SD card is composed of 6 bytes. When sending data, the high bit is first. The write command format of the SD card is shown in the figure below:
Insert picture description here
Byte1: The first byte of the command word is the command number (such as CMD0, CMD1, etc.) ), the format is "0 1 xxxxxx". The highest bit of the command number is always 0, which is the start bit of the command number; the second highest bit is always 1, which is the sending bit of the command number; the lower 6 bits are the specific command number (such as CMD55, 8'd55 = 8'b0011_0111, The command number is 0 1 1 1 0 1 1 1 = 0x77).

Byte2~Byte5: Command parameters. Some command parameters are reserved bits, and the content of the parameter is not defined. The reserved bits should be set to 0.

Byte6: The first 7 bits are CRC (cyclic redundancy check) check bits, and the last bit is stop bit 0. The SD card does not turn on CRC check by default in SPI mode, and turns on CRC check in SDIO mode. That is to say, in the SPI mode, the CRC check bit must be sent, but the SD card will automatically ignore it when it reads the CRC check bit, so all the check bits can be set to 1. It should be noted that the SD card is powered on by default in SDIO mode. When receiving the response command from the SD card to return CMD0, pull down the chip select CS to enter the SPI mode. So when sending the CMD0 command, the SD card is in SDIO mode, and CRC check needs to be turned on. In addition, the CRC check of CMD8 is always enabled, and CRC check also needs to be enabled. Except for these two commands, the CRC of other commands does not need to be checked.

SD card commands are divided into standard commands (such as CMD0) and application-related commands (such as ACMD41). The ACMD command is a special command, and the sending method is the same as the standard command, but before sending the application-related command, the CMD55 command must be sent first to tell the SD card that the next command is an application-related command, not a standard command. After sending the command, the SD card will return the response information. Different CMD commands will have different types of return values. The commonly used return values ​​are R1 type, R3 type and R7 type (R7 type is dedicated to CMD8 commands). The commonly used commands of SD card are described in the following table (Table 39.1.2).

Insert picture description here
Insert picture description here
The SD card return type R1 data format is shown in the figure below:
Insert picture description here
As can be seen from the above figure, the SD card return type R1 format returns 1 byte in total, the highest bit is fixed at 0, and the other bits respectively represent the flags of the corresponding state, and the high level is effective.

The SD card return type R3 data format is shown in the figure below:
Insert picture description here
As can be seen from the above figure, the SD card return type R3 format returns a total of 5 bytes. The first byte returned is the content of R1 described earlier, and the remaining bytes are the OCR (Operation Conditions Register) register. content.

The SD card return type R7 data format is shown in the figure below:
Insert picture description here
As can be seen from the above figure, the SD card return type R7 format returns a total of 5 bytes. The first byte returned is the content of R1 described above, and the remaining bytes contain SD card operating voltage information and check bytes. . Among them, the voltage range is a relatively important parameter, and its specific content is as follows:
Insert picture description here
Before the normal read and write operation of the SD card, the SD card must be initialized to make it work in the expected working mode. There is a difference between the SD card version 1.0 protocol and the version 2.0 protocol in the initialization process. Only the SD card with the SD 2.0 version protocol supports the CMD8 command, so the SD card that responds to this command can be judged as the SD2.0 version protocol card, otherwise it is SD card or MMC card with SD1.0 version protocol; for CMD8 not responding, you can send CMD55 + ACMD41 command, if it returns 0, it means the SD1.0 protocol version card is initialized successfully, if it returns an error, it is determined as MMC card ; After determining the MMC card, continue to send the CMD1 command to the card, if it returns 0, the MMC card is initialized successfully, otherwise it is judged as an error card.

3. Initialization of SD card

Since most of the SD cards on the market adopt the SD2.0 version protocol, we will only introduce the initialization process of the SD2.0 version protocol. The SD cards mentioned below all represent SDHC cards based on the SD2.0 version protocol. The detailed initialization steps are as follows:

1. After the SD card is powered on, the host FPGA first sends at least 74 synchronization clocks to the slave SD card. During the power-on synchronization period, the chip select CS pin and MOSI pin must be high (MOSI pin Except for sending commands or data, all other times are high level);

2. Pull down the CS pin of the chip select, send the command CMD0 (0x40) to reset the SD card, and wait for the SD card to return response data after the command is sent;

3. After the SD card returns the response data, first wait for 8 clock cycles and then pull up the CS signal of the chip select. At this time, judge the returned response data. If the returned data is the reset completion signal 0x01, the chip select CS is low during receiving the return information, at this time the SD card enters the SPI mode and starts the next step, if the returned value is another value, the second step is executed again. step;

4. Pull down the CS pin of the chip select and send the command CMD8 (0x48) to query the version number of the SD card. Only SD2.0 version cards support this command. After the command is sent, wait for the SD card to return the response data;

5. After the SD card returns the response data, first wait for 8 clock cycles and then pull up the CS signal of the chip select. At this time, judge the returned response data. If the returned voltage range is 4'b0001, that is, 2.7V~3.6V, it means that the SD card is version 2.0, go to the next step, otherwise go to step 4 again;

6. Pull down the CS pin of the chip select and send the command CMD55 (0x77) to tell the SD card that the next command sent is an application-related command. After the command is sent, wait for the SD card to return the response data;

7. After the SD card returns the response data, first wait for 8 clock cycles and then pull up the CS signal of the chip select. At this time, judge the returned response data. If the returned data is an idle signal 0x01, proceed to the next step, otherwise re-execute step 6.

8. Pull down the CS pin of the chip select, send the command ACMD41 (0x69) to query whether the SD card is initialized, and wait for the SD card to return response data after the command is sent;

9. After the SD card returns the response data, first wait for 8 clock cycles and then pull up the CS signal of the chip select. At this time, judge the returned response data. If the returned data is 0x00, the initialization is complete at this time, otherwise, perform step 6 again.

The sequence of SD card power-on and reset commands is shown in the figure below:
Insert picture description here

Four, SD card read and write operations

At this point, the SD card has completed the reset and initialization operations, and enters the read and write operations in the SPI mode. It should be noted that when the SD card is initialized, the clock frequency of SPI_CLK cannot exceed 400KHz. After the initialization is completed, the clock frequency of SPI_CLK is switched to the maximum clock frequency of the SD card. Although many SD cards on the market currently support initialization with a faster clock frequency, in order to be compatible with more SD cards, the clock frequency cannot exceed 400KHz when the SD card is initialized.

The data volume of SD card read and write must be an integer multiple of 512 bytes, that is, the minimum data volume for SD card read and write operations is 512 bytes. We can configure the data length of a single read and write operation through the command CMD16, so that the amount of data read and written each time is (n*512) bytes (n≥1). This SD card read and write operation uses SD The card default configuration, that is, the data volume of a single read and write operation is 512 bytes.

After the SD card is initialized, the SD card can be read and written. The SD card read and write test is to write data to the SD card first, then read the data from the SD card, and verify the correctness of the data. The timing chart of SD card write operation is shown in the figure below:

Insert picture description here
The SD card write operation process is as follows:

1. Pull down the CS pin of the chip select, send the command CMD24 (0x58) to read a single data block, and wait for the SD card to return the response data after the command is sent;

2. After the SD card returns the correct response data 0x00, wait at least 8 clock cycles before sending the data header 0xfe;

3. After sending the data header 0xfe, then start sending 512 bytes of data;

4. After the data transmission is completed, send 2 bytes of CRC check data. Since the CRC check is not performed on the data in SPI mode, just send two bytes of 0xff directly;

5. After the verification data is sent, wait for the SD card to respond;

6. After the SD card returns the response data, it will enter the write busy state (MISO pin is low), that is, other operations are not allowed at this time. When it is detected that the MISO pin is high, the SD card exits the write busy state at this time;

7. Pull the CS pin high and allow other operations after waiting for 8 clock cycles.

The timing chart of SD card read operation is shown in the figure below:
Insert picture description here
The read operation process of SD card is as follows:

1. Pull down the CS pin of the chip select, send the command CMD17 (0x51) to read a single data block, and wait for the SD card to return the response data after the command is sent;

2. After the SD card returns the correct response data 0x00, prepare to analyze the data header 0xfe returned by the SD card;

3. After parsing to the data header 0xfe, then receive the 512 bytes of data returned by the SD card;

4. After the data analysis is completed, the next two bytes of CRC check value are received. Since the CRC check is not performed on the data in SPI mode, these two bytes can be ignored directly;

5. After receiving the verification data, wait for 8 clock cycles;

6. Pull up the CS pin of the chip select and allow other operations to be performed after waiting for 8 clock cycles.

Guess you like

Origin blog.csdn.net/qq_39507748/article/details/113195336