We recommend following the public account below to learn more about electronic technology knowledge!
01Introduction to SD Cards
SD cards (Secure Digital Memory Card) are a new generation of memory devices based on semiconductor flash memory, developed from MMC. They are widely used in portable devices such as digital cameras, personal digital assistants (PDAs), and multimedia players.SD cards were jointly developed by Panasonic, Toshiba, and SanDisk in August 1999.
SD cards are classified by capacity into three types: SD cards, SDHC cards, and SDXC cards, as shown in the table below:
The appearance and interface of SD cards are shown in the figure below:
SD cards communicate with external devices through 9 pins and support two modes: SPI and SDIO. The pin function descriptions for different modes are shown in the table below:
02Features of SD Cards
①High capacity, up to2TB (currently, there are512GB SD cards available).
② High security.
③ High speed, with current read speeds approaching 100MB/S.
④ Compact size, with a standard SD card being only the size of a postage stamp and weighing just 2g.
⑤ Simple interface, supporting both SPI and SDIO access modes.
Note: TF cards combined with a card adapter can also be used as SD cards, but many TF cards do not support the SPI access mode. Therefore, when using SPI to drive SD cards, it is advisable to choose larger cards (SD cards) rather than TF cards.
03SD Registers
There are5 registers in SD
For detailed descriptions of these registers, please refer to Chapter 5 of the “SD Card 2.0 Protocol.pdf”.
04Operations of SD Cards
4.1、Initialization of SD
The most important step in implementing SDIO to drive SD cards is the initialization of the SD card. Once the SD card is initialized, the remaining operations (read/write) become straightforward, so we will focus on the initialization of the SD card here.
Refer to the diagram in the “SD Card 2.0 Protocol.pdf” document.
From the SD card initialization process, we can see that regardless of the card type (here we classify cards into 4 types: SD2.0 high-capacity cards (SDHC, maximum 32G), SD2.0 standard capacity cards (SDSC, maximum 2G), SD1.x cards, and MMC cards), the first step is to power on the card (set SDIO_POWER
[1:0]=11). After powering on, send CMD0 to perform a soft reset on the card, then send CMD8 to distinguish SD card 2.0. Only cards 2.0 and later support CMD8; MMC cards and V1.x cards do not support this command. The CMD8 command format is shown in the table below:
When sending CMD8, we can set the VHS bit through its parameters to inform the SD card of the host’s power supply conditions, allowing the SD card to know the host’s power supply range.
The VHS bit is defined in the table below:
Here we use the parameter 0X1AA, which tells the SD card that the host power supply is between 2.7~3.6V. If the SD card supports CMD8 and this voltage range, it will respond to the host with the parameters part of CMD8’s response (R7; for SD card responses, please refer to Section 4.9 of the “SD Card 2.0 Protocol.pdf”). If it does not support CMD8 or this voltage range, it will not respond.
After sending CMD8, send ACMD41 (note: before sending ACMD41, CMD55 must be sent first) to further confirm the card’s operating voltage range and inform the SD card whether the host supports high-capacity cards (SDHC).
The ACMD41 command format is shown in the table below:
The ACMD41 command response (R3) contains the contents of the SD card OCR register, defined in the table below:
For cards that support CMD8, the host sets the HCS parameter of ACMD41 to 1, informing the SD card that the host supports SDHC cards.
For 2.0 cards, the CCS bit of OCR indicates whether it is SDHC or SDSC; for 1.x cards, this bit is ignored; for MMC cards, ACMD41 is not supported, and only CMD0 and CMD1 need to be sent to complete initialization.
CMD2 is used to obtain CID register data. The definitions of each bit in the CID register are shown in the table below:
After receiving CMD2, the SD card will return an R2 long response (136 bits), which contains 128 bits of valid data (CID register content) stored in the SDIO_RESP1~4 registers. By reading these four registers, we can obtain the CID information of the SD card.
CMD3 is used to set the card’s relative address (RCA, must be non-zero). For SD cards (not MMC cards), after receiving CMD3, a new RCA will be returned to the host for addressing convenience. The existence of RCA allows multiple SD cards to be connected to one SDIO interface, distinguishing which card the host wants to operate on. For MMC cards, the RCA is not automatically returned by the SD card; instead, the host actively sets the RCA for the MMC card through CMD3 with parameters (the high 16 bits are used for RCA setting). Similarly, MMC cards also support multiple MMC cards on one SDIO interface, but unlike SD cards, all RCAs are actively set by the host, while the RCA of SD cards is provided by the SD card to the host.
After obtaining the card’s RCA, we can send CMD9 (with RCA parameter) to obtain the contents of the SD card’s CSD register. From the CSD register, we can obtain important information such as the SD card’s capacity and sector size. We will not go into detail about the CSD register here; for a detailed introduction to the CSD register, please refer to the “SD Card 2.0 Protocol.pdf”.
At this point, the initialization of our SD card is basically complete. Finally, by sending CMD7, we can select the SD card we want to operate on and begin read/write operations on the SD card.
4.2、SD Card Read Operations
Single block data read process of SD card
Multi-block data read process of SD card
CMD16 command description
CMD17 command description
CMD18 command description
CMD12 command description
4.3、SD Card Write Operations
Single block data write process of SD card
Multi-block data write process of SD card
CMD13 command description
R1 response:
For the SD card status, please refer to page 63 of the “SD Card 2.0 Protocol.pdf” – Table 4-35.
CMD24 command description
ACMD23 command description
Note: Before sending ACMD, CMD55 must be sent first to notify the SD card that the next command is an application command (APPCMD), not a standard command.
CMD55 command description
CMD25 command description
We recommend following the public account below to learn more about electronic technology knowledge!