SD card interface design [with hardware circuit and program]

Publisher:心动代码Latest update time:2012-04-30 Source: OFweek Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 SD card standard

The SD card standard is a standard designed, patented and authorized by the SD Card Association for removable storage devices. It is mainly used to formulate the card's external dimensions, electrical interface and communication protocol.

1.1 SD card pin function

The appearance of the SD card is shown in Figure 1, and the pin functions are listed in Table 1. The pins of the SD card have dual functions and can work in both SD mode and SPI mode. The pins have different functions in different modes.

SD mode is mostly used in situations where the SD card read and write speed is required to be high, while SPI mode sacrifices read and write speed in exchange for better hardware interface compatibility. Since the SPI protocol is currently a widely popular communication protocol, most high-performance microcontrollers are equipped with SPI hardware interfaces, and the hardware connection is relatively simple. Therefore, when the SD card read and write speed is not required to be high, using SPI mode is undoubtedly a good choice.

1.2 SPI Mode

SPI mode is a simple command response protocol. After the main controller issues a command, the SD card returns the corresponding response for different commands.

The command list of SD card starts with CMD and ACMD, which refer to general commands and special commands respectively, followed by the command number. For example, CMD17 is a general command used to read a single block of data.

In SPI mode, commands are sent in the following 6-byte format:

Each frame command starts with "01", followed by a 6-bit command number and a 4-byte parameter (high bit first, low bit last), and finally a 7-bit CRC check and a 1-bit stop bit "1".

Each command of the SD card will return a corresponding response type. In SPI mode, there are 3 response types: R1, R2 and R3, which occupy 1, 2 and 3 bytes respectively. Only the format of the R1 response is listed here, as shown in Table 2. When the status described in the table appears, the corresponding position is 1. The first byte format of R2 and R3 is exactly the same as R1. For details, please refer to the SD card standard.

2 Hardware Design

This design uses Freescale's 32-bit low-power microcontroller MCF51QE128 and adopts SPI mode to implement the interface with the SD card.

Since MCF51QE128 is a low-power microcontroller with a typical operating voltage of 3.6 V, which is compatible with the operating voltage of the SD card, it can be directly connected to the SD card without a level conversion circuit. The second SPI port of MCF51 QE128 is used here, and the hardware connection is shown in Figure 2.

3 Software Implementation

The software part mainly implements the initialization of MCF51QE128, the underlying SPI communication, and the general write commands, initialization and single-block data reading and writing of the SD card.

3.1 Initialization of MCF51QE128

Before communicating with the SD card, you first need to configure the MCF51QE128 and initialize the SPI port. The code is as follows:

3.2 Low-level SPI communication

The underlying SPI communication is the key to achieve the final reading and writing. Since MCF51QE128 has its own SPI hardware interface, it only needs to read and write the value of the SPI data register. Here, three data types, byte, word and dword, are defined, corresponding to 8-bit, 16-bit and 32-bit data respectively. The code is as follows:

3.3 SD card general write command

Since the commands of SD card have a unified format, a general write command function can be used to send all commands. In addition, considering that the response type of most commands is R1, the response type received by the general write command function here is R1 by default. The function code is as follows:

3.4 SD card initialization

The initialization of the SD card follows certain steps. First, lower the SPI clock to 400 kHz and wait for at least 74 clock cycles. Then pull the chip select signal low and send the CMD0 command to reset the SD card and put it into SPI mode. Here, a correct CRC check is required, and the check byte is 0x95. If the SD card enters the idle state (that is, when the received response is 0x01), send the CMD1 command to activate the card initialization process. At this time, the response is 0x00. Then set the block length, which is generally 512 bytes. Finally, pull the chip select high and set the SPI clock to the maximum value to ensure the maximum read and write speed. The SD card initialization process is shown in Figure 3.

The SD card initialization code is as follows:

3.5 SD card single block data reading and writing

SPI mode supports single-block and multi-block data read and write operations, which can be achieved by sending corresponding commands. The operation process of reading single-block data is shown in Figure 4. After pulling down the chip select, the main controller MCF51QE128 first sends the single-block data read command CMD17, and then waits for the response of the SD card. After receiving the data block start flag 0xfe, it starts to read 512 bytes of data from the SD card, and finally reads the 2-byte CRC check bit.

The function code for reading a single block of data is as follows:

The operation process of writing a single block of data is similar to the read operation, as shown in Figure 5. After the chip select is pulled low, the main controller MCF51QE128 also sends a single block of data write command CMD24. After the SD card responds correctly, it sends the data block start flag 0xfe, followed by a 512-byte data block and a 2-byte CRC check.

After writing data, the SD card will send a 1-byte data response to feedback the data writing status, and its format is shown in Figure 6. When the data is correctly written to the SD card, the data response is 0x05. Finally, read the data bus and wait when writing data until the bus is high.

The function code for writing a single block of data is as follows:

Conclusion

SD card is a widely used erasable large-capacity storage device. Its interface design can be used as a general solution for storage units in various embedded systems . This paper combines the relevant technologies of SD card standards and completes the design of hardware interface and underlying communication software based on MCF51QE128 microcontroller. On this basis, a file system can be further constructed to achieve more effective management of stored data.

Reference address:SD card interface design [with hardware circuit and program]

Previous article:Application of single-ended flyback circuit in inverter power supply
Next article:Design of CAN bus NiMH battery management system

Latest Power Management Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号