I. Preface
The SD card has two optional communication protocols: SD mode and SPI mode SD mode are SD card standard read/write mode, but when SD mode is used, you often need to select an MCU with an SD card controller interface or add an additional SD card control unit to support reading and writing of the SD card. However, most MCU do not integrate the SD card controller interface, if SD mode is used for communication, the hardware cost of the product is virtually increased. When the time required for reading and writing SD card data is not strict, selecting the SPI mode is the best solution because in the SPI mode, all data exchange can be completed through four lines, and many MCUs are integrated on the market.
There is a ready-made SPI interface circuit. Using the SPI mode to read and write SD cards can greatly simplify the design of the hardware circuit.
Ii. hardware circuit implementation
Taking NXP's lpc2210 arm7mcu as an example, it is an implementation board circuit developed by Zhou ligong.
Here, we use the spi0 of the lpc2210mcu for SD card control and data read/write. The pull-up resistor is applied to the two data lines of spi0 to facilitate mmccard compatibility.
Card Power Supply adopts a controllable mode, and controls the MOs to supply power through gpio ports.
The card detection circuit is also implemented using the gpio port. Read the gpio Port Data to check whether the card is write-protected and completely inserted.
For details, refer to Zhou ligong's manual.
Iii. SD card physical interface
The SD card package contains nine pins and a write protection switch:
Its pins are defined as follows:
Note: 1. S: power supply; I: input; O: push-pull output; PP: push-pull I/O.
2. Extended dat lines (dat1 ~ Dat3) is in the input state after power-on. They are used as dat lines after the set_bus_width command is executed. When dat1 ~ When the dat3 line is used, the host should set its own dat1 ~ The dat3 line is in input mode. This is defined to be compatible with the mmccard.
3. After power-on, this line is an input line with a 50 K pull resistance (can be used to detect whether the card exists or select the SPI mode ). You can use the set_clr_card_detect (acmd42) command to disconnect the pull-up resistor during normal data transmission. This pin of the mmccard is reserved in SD mode and does not work in SD mode.
4. The mmccard is in SD mode: I/O/PP/OD.
5. The mmccard is in SPI mode: I/PP.
Iv. SD card Control Process
1. SD card SPI Working Mode
The SD card automatically enters the SD bus mode at the initial power-on stage. In this mode, it sends a reset command to the SD card.Limit 0.If the SD card is valid when receiving the reset command, it enters the SPI mode. Otherwise, it works in the SD bus mode.
Below is the flowchart of inserting an SD card and initializing It In spi mode :(As for what command is cmd ××, this article will be attached at the end)
After the reset is successful, you can use limit 55 and acmd41 to determine whether the current voltage is within the working range. The host can continue to read the CID register of the SD card through limit 10, and set the data block length through limit 16, the CSD register of the card is read from the CSD register through ipv9. The host can know the card capacity, supported command sets, and other important parameters.
2. read/write data blocks
After the initialization of the SD card, you can perform its read and write operations. The SD card read and write operations are performed by sending the SD card command. The SPI bus mode supports single-block (24) and Multi-block (25) write operation. Multi-block Operation refers to writing from the specified position, when the SD card receives a Stop command listen 12, the length of the data block that stops a single write operation can only be 512 bytes for a single write operation, and the command goes 24, when the response is 0, data can be written. The size of the 512-byte SD card confirms each data block sent to itself through a response command, which is 1 byte long, when the 5th bit is 00101, the data block is correctly written to the SD card.
When you need to read data from the SD card, the command word for reading the SD card is listen 17, and the correct first response command byte is 0xfe, followed by a user data block of 512 bytes, the final two-byte CRC verification code is visible. After the initialization, the read and write operations on the SD card are completed based on the SD card command and response.ProgramThe flowchart is as follows:
(1) SD card Writing Process
(2) SD card reading process
5. Set of SD card operation commands
These commands are used to perform operations on the SD card. The following command comes from Zhou licong's SD/MMC middleware. I checked a lot of places and only showed things such as limit 0 and cmd1, but no one said what these things are. Paste it here for reference,This is why I wrote this blog, because I have been searching for these commands for a long time. One sentence for SDCodeNo post...
/* Command response definition define Command's response */# Define R1 1 # define r1b 2 # define R2 3 # define R3 4
/*************************************** *************************** *******************/ /********************************* Basic command set * *************************/ /* Reset the SD card reset cards to idle state */ # Define defaults 0 0 # define defaults 0_r r1 /* Read OCR register read the OCR (MMC mode, do not use for SD cards )*/ # Define cmd1 1 # define 1_r r1 /* Read the CSD register card sends the CSD */ # Define limit 9 9 # define limit 9_r r1 /* Read CID register card sends CID */ # Define limit 10 10 # define limit 10_r r1 /* Stop data transmission when multiple read blocks stop a multiple block (Stream) read/write operation */ # Define limit 12 12 # define limit 12_r r1b /* Read the card_status register get the addressed card's Status Register */ # Define limit 13 13 # define limit 13_r r2 /**************************** Block read commands in the block READ command set *** ***********************/ /* Set the block length */ # Define limit 16 16 # define limit 16_r r1 /* Read a single block */ # Define limit 17 17 # define limit 17_r r1 /* Read multiple blocks until the host sends listen 12 until the read multiple blocks until a wait 12 */ # Define limit 18 18 # define limit 18_r r1 /**************************** Block write commands in the block write command set *** **********************/ /* Write a block of the size selected with limit 16 */ # Define limit 24 24 # define limit 24_r r1 /* Write multiple blocks write until a limit 12 */ # Define limit 25 25 # define limit 25_r r1 /* Write the CSD register program the programmable bits of the CSD */ # Define limit 27 27 # define limit 27_r r1 /***************************** Write protection ******* **********************/ /* Set the write protection bit of the addressed Group */ # Define limit 28 28 # define limit 28_r r1b /* Clear the write protection bit of the addressed Group */ # Define limit 29 29 # define limit 29_r r1b /* Ask the card for the status of the write protection bits */ # Define Limit 30 30 # define limit 30_r r1/**************************** Erase commands ****** *************************/ /* Set the start address of the erased block (for SD card only) set the address of the first write block to be erased (only for SD )*/ # Define Route 32 32 # define route 32_r r1 /* Set the end address of the erased block (for SD card only) set the address of the last write block to be erased (only for SD )*/ # Define limit 33 33 # define limit 33_r r1 /* Set the start address of the erased block (for mmccard only) set the address of the first write block to be erased (only for MMC )*/ # Define limit 35 35 # define limit 35_r r1 /* Set the end address of the erased block (for mmccard only) set the address of the last write block to be erased (only for MMC )*/ # Define limit 36 36 # define limit 36_r r1 /* Erase the selected block erase the selected write blocks */ # Define limit 38 38 # define limit 38_r r1b /**************************** Lock card command ***** **********************/ /* Set/reset the password or lock/unlock the card set/reset the password or lock/unlock the card */ # Define limit 42 42 # define limit 42_rr1b /* Commands from 42 to 54, not defined here */ /**************************** Application Command application-specific commands **** ************/ /* Disable the next command from being the application command flag that the next command is application-specific */ # Define limit 55 55 # define limit 55_r r1 /* General I/O General purpose I/O for application-specific commands */ # Define limit 56 56 # define limit 56_r r1/* Read OCR register read the OCR (SPI mode only )*/ # Define limit 58 58 # define limit 58_r r3 /* Enable or disable CRC turn CRC on or off */ # Define limit 59 59 # define limit 59_r r1 /**************************** Application Command application-specific commands **** ***********/ /* Get the SD Status Register get the SD card's status */ # Define acmd13 13 # define acmd13_r r2 /* Get the number of blocks written into the card. Get the number of written write blocks (minus errors )*/ # Define acmd22 22 # define acmd22_r r1 /* Before writing, set the number of blocks to be erased in advance to set the number of write blocks to be pre-erased before writing */ # Define acmd23 23 # define acmd23_r r1 /* Read the OCR register get the card's OCR (SD mode )*/ # Define acmd41 41 # define acmd41_r r1 /* Connect/disconnect the up-pull resistor connect or disconnect the 50 kohm internal pull-up on CD/DAT on the CD/data [3] pin */ # Define acmd42 42 # define acmd42_r r1 /* Read the SCR register get the SD configuration register */ # Define acmd51 # define acmd51_r r1
Technorati tags: SD card, SPI, command, Zhou ligong