1. SD card command format:
The SD card instruction consists of 6 bytes, as shown below:
Byte1: 0 1 x (command number, defined by the command mark. For example, if limit 39 is 100111, that is, hexadecimal 0x27, the first byte of complete limit 39 is 01100111, 0x27 + 0x40)
Byte2-5: Command Arguments, Command parameters, some commands do not have Parameters
Byte6: the first 7 bits are CRC (Cyclic Redundacy Check, Cyclic redundancy Check) Check bits, and the last bits are stopped bits 0
2. SD card command:
SD card commands are divided into 12 categories: class0 to class11,
Different SDd CARDS support different command sets based on their functions:
Class0: (card recognition, initialization, and other basic command sets)
Reset 0: reset the SD card.
CMD1: Read OCR register.
Ipv9: Read the CSD register.
Limit 10: Read CID register.
Listen 12: Stop data transmission when reading multiple blocks
Listen 13: Read the Card_Status register
Class2 (card reading command set ):
Limit 16: Set the block Length
Limit 17: read a single block.
Limit 18: read multiple blocks until the host sends limit 12.
Class4 (card writing command set ):
Limit 24: write a single block.
Limit 25: write multiple data blocks.
27: Write the CSD register.
Class5 (command set for card erasure ):
Limit 32: sets the start address of the erased block.
33: set the end address of the erased block.
38: erase the selected block.
Class6 (write Protection Command set ):
28: Set the address of the write protection block.
29th: erase the address of the write protection block.
Limit 30: Ask the card for the status of the write protection bits
Class7: Card lock, unlock FUNCTION command set
Class8: Apply for a specific command set.
Class10-11: Reserved
Class1, class3, class9: the SPI mode is not supported.
After understanding the instruction form and specific control content, the following mainly solves the communication problem between MCU and SD card. The following problems need to be paid attention to during communication:
(1) power supply voltage: It must be 3.3 V, but I use an STC ticket in actual use.
(2) Communication Mode Switching: the SD card has two communication modes: SPI mode and SD mode. The default communication mode is SD mode, but we usually use SPI mode, this requires a method to switch the mode. The specific implementation method is also introduced elsewhere. The key is to first send the reset command after the power-on delay is greater than 74 clock cycles, after the reset succeeds (the 0x01 response is received), the devices 55 and acmd41 are sent continuously until the 0x00 response is returned. At this time, the SD card has entered the SPI mode.
(3) The send reset command (reset 0) and reset 55 and acmd41 mentioned above must have specific implementation methods. What needs to be solved is the timing problem,
It is the sequence chart of the reset. As long as the operation can be performed according to the sequence, the reset will be successful.
This reset is divided into (1) power-on, (2) delay of more than 74 cycles, (3) Sending command limit 0, (4) Sending command parameter 0X0000, (5) send CRC verification 0X95, (6) Wait for response (7) response 0X01 now get correct response reset successful, otherwise repeat the above operation until successful.
This section describes how to reset a common command. Other commands are similar to the reset command, but they have different response types and follow-up operations based on their functions. The following describes the sequence of commonly used commands in a set.
(1) read CID Register time sequence diagram
Note: When the command is sent and the response 0x00 is returned, the system prepares to receive the content in the CID register. After receiving the Starting Sign 0 x Fe, the following 16 bytes are the content of the CID register.
(2) The sequence of the read CSD register content is similar to that of the read CID, but the Command sent at this time is listen 9
The SD card data is read and written in blocks. The size of each block is 512 bytes by default.
(3) One block (512 bytes) timing for reading SD cards
(4) Write A Sequence Chart (512 bytes)