SD card interface design

Publisher:bln898Latest update time:2011-11-24 Source: 电子产品世界 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 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 external shape 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. In different modes, the pins have different functions.

SD mode is mostly used in situations where the SD card reading and writing speeds are required to be high, while SPI mode sacrifices reading and writing speeds 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 reading and writing speeds are not required to be high, the 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 a corresponding response for different commands.
The command list of the 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 checksum 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 format of the first byte 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 uses 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 MCF51QE128 and initialize the SPI port. The code is as follows:

1 SD Card Standard
The SD card standard is a standard designed 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 external shape 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. In different modes, the pins have different functions.

SD mode is mostly used in situations where the SD card reading and writing speeds are required to be high, while SPI mode sacrifices reading and writing speeds 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 reading and writing speeds are not required to be high, the 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 a corresponding response for different commands.
The command list of the 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 checksum 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 format of the first byte 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 uses 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 MCF51QE128 and initialize the SPI port. The code is as follows:

3.5 SD card single block data reading and writing
SPI mode supports single block and multi-block data reading and writing 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 reading 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 the SD card standard and completes the design of the hardware interface and underlying communication software based on the 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

Previous article:A secure system based on FPGA single chip quad-core two-by-two
Next article:Implementing the hardware abstraction layer of EMC2

Recommended ReadingLatest update time:2024-11-16 22:47

Design of TWI interface control between clock chip SD2200ELP and AVR microcontroller
  In the design of real-time online detection of the clearance of the brake shoe of the hoist, it is necessary to save the fault time and fault data.   Clock chips are used in most instruments and meters, but many real-time clock chips (such as PCF8563) do not have power-off protection and require external crystal os
[Microcontroller]
Design of TWI interface control between clock chip SD2200ELP and AVR microcontroller
Latest Industrial Control 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号