SD/MMC card initialization and read/write process

Source: Internet
Author: User
Tags manual pdf manual reserved valid
Second, the MMC/SD card model and working principle pin pin, SD card bus, SD card structure, SD card register, power-up process SD card register: OCR: Operating voltage register: read-only, 32-bit 31st bit: Indicates the status bit of the card power CID: Card identification Register read-only 128-bit manufacturers, production Product ID, production date and serial number, such as CSD: some writable 128-bit card capacity, the size of the erase sector, read and write the largest data block size, the reading operation of the current, voltage and so on CSR: card configuration Register 64 bits of data bit width rca:16 bit-related card address register, card recognition process of the master Controller and card negotiation To an address three, SD card command and Response Format command and corresponding format SD card command, command type, acmd command response type, card type, card state conversion Table command format: 48-bit starting bit 0 direction bit (host to card:1, card to host:0) content C  RC7 End bit 1 · Format of the response: 48-bit or 136-bit card command: type of command: Bc:broadcast without Response unresponsive broadcast bcr:broadcast with Response responsive broadcast ac:address (p Oint-to-point) Command: Point-to-point, data0~data3 data-Line no data adtc:adress (point-to-point) Transfer Commands Point-to-point, data0~ Data on the DATA3 data line
CMD0, CMD2, CMD3, CMD55, ACMD41 commands may cause the status of the card to change response type: R1,R1B, R2, R3,R6 (SD2.0 expanded R7) extended content: SPI operating mode: Features to know: Only one card is supported, no RC A, the command is just a basic subset of the MMC/SD SDHC: (Supports 2GB~32GB): Understanding the role of CMD8, command format and response, Understanding CSDV2.0 Register made Extended SDIO WIFI: Add CMD52, CMD53
CMD8 can extend some of the new functionality of existing commands by redefining the previously reserved bits. ACMD41 extended to support initialization of high-capacity SD memory cards
The above describes a control register and other information
Green indicates different points for SD and MMC
For the capacity of the card to be noted for the SD card can refer to the Simplified_physical_layer_spec v2.0.pdf manual above for the MMC can refer to Jesd84-a441.pdf Note for the KCAL MMC is sent by 8th number command to get E 212 to 215 positions in the XT_CSD to get the

-S------------------------------------------------------------------------------------------------------------- ------------------------------static void Sd_init (void) {int retries; U8 *resp; unsigned int cardaddr;/resp = Mmc_cmd ( 2, 0, MSC_CMDAT_RESPONSE_R2, MSC_CMDAT_RESPONSE_R2);  Serial_puts ("SD carsd CID =r2="); Serial_dump_data (RESP, 15); RESP = mmc_cmd (3, 0, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1); CARDADDR = (Resp[4] << 8) |  RESP[3]; Occurs 3 commands come or RCA RCA = CARDADDR << 16; Serial_puts ("rca="); Serial_puts_hex (RCA);
RESP = Mmc_cmd (9, RCA, MSC_CMDAT_RESPONSE_R2, MSC_CMDAT_RESPONSE_R2); Sd2_0 = (resp[14] & 0xc0) >> 6; Serial_puts ("sd2_0====="); Serial_puts_hex (SD2_0);  Serial_puts ("SD carsd CSD Register =r2="); Serial_dump_data (RESP, 16);
OUTREG16 (A_MSC_CLKRT (0), 0); RESP = mmc_cmd (7, RCA, MSC_CMDAT_BUSY | MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1); RESP = Mmc_cmd (, RCA, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1); RESP = Mmc_cmd (6, 0x2, Msc_cmdat_bus_width_4bit | msc_cmdat_response_r1| Msc_cmdat_busy, MSC_CMDAT_RESPONSE_R1);//Response type R1b//while (! INREG16 (A_msc_stat (0)) & (1&LT;&LT;13));//wait for programming to complete}---------------------------------------------------------- --Start here to see int mmc_init (void)//{int retries; U8 *resp; OUTREG32 (a_cpm_msccdr,13); SETREG32 (A_CPM_CPCCR, cpccr_change_en);
Mmc_init_gpio (); __msc_reset (); Mmc_irq_mask (); OUTREG32 (A_MSC_CLKRT (0), 7),//extclk/128//outreg32 (A_MSC_LPM (0), 0x01); sd2_0 = 0; Default to standard SD card resp = mmc_cmd (0, 0, 0); //First 80 clocks   RESP = mmc_cmd (8, 0X1AA, 0x1, MSC_CMDAT_RESPONSE_R1); SD2 (return to 0x1) after the card or SD1 (return 0x5) is now basically SD2 later, so there is no judgment on the return value resp = mmc_cmd (0, MSC_CMDAT_RESPONSE_R1, msc_cmdat_ RESPONSE_R1); //According to CMD55 to determine whether it is an MMC card or an SD card, only the card has 55 command to correctly have a return value return value has command index =0x37=55 the description is sd //serial_puts ( "Application specific Commands =r1="); Serial_dump_data (RESP, 6); if (resp[5]! = 0x37) { //is an MMC//serial_puts ("mmc card found!\n"); retries = $; do{resp = mmc_cmd (1, 0x40ff8000, MSC_CMDAT_RESPONSE_R3, MSC_CMDAT_RESPONSE_R3); Matching voltage MMC card is CMD1 &NBSP;SD card is CMD41   because power on is a time, so we use a do{}while to delay sd_mdelay (10); } while (retries--&&!) ( RESP[4] & 0x80)); Until the 31 bits in the OCR reply value in R3 are 1, the power-up completes the IF (resp[4]&0x40)//voltage verification with the data access mode bit in the last answer, or, if 1, the SDHC card is based on block address access {sd2_0 = 1;  / /You can also use the 30 bits of OCR in the R3 return value to checkCheck whether the high-capacity card (&GT;2G) or standard card   can actually use the CMD9 command to read CDs to determine the SD is to do so #if 0 serial_puts ("&NBSP;OCR =r3=");  serial_dump_data (RESP, 6); if (resp[4]& 0x80)   serial_puts ("\N\NMMC init ok\n\n");//  means power-up complete else  serial_puts ("\N\NMMC Init Fail\n\n "); #endif resp = mmc_cmd (2, 0, MSC_CMDAT_RESPONSE_R2, MSC_CMDAT_RESPONSE_R2); //Get CID//serial_puts (" cid CSD =r2=");  serial_dump_data (RESP, 16); RESP = Mmc_cmd (3, 0x10, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1); //This thing is disgusting.   The parameters in the SD card and the MMC card are not the same   The parameters of the MMC card are self-setting an RCA value, but the SD card is read by the value of the RCA, the above SD card initialization has described OUTREG16 (A_MSC_CLKRT (0), 1); RESP = mmc_cmd (7, 0x10, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1); Set the new address to a valid address   This will enter Tarnsport mode. Only the 7th command is sent, and the bit width CMD6   is set to occur in the read-write occurrence 16 17 18 etc RESP = mmc_cmd (6, 0x3b70101, MS C_cmdat_bus_width_4bit| msc_cmdat_response_r1| Msc_cmdat_busy, MSC_CMDAT_RESPONSE_R1); Answer type R1B, set bit width   4BIT mode
RESP = Mmc_cmd (, RCA, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1); Card_status = (Resp[4] << 24) | (Resp[3] << 16) | (Resp[2] << 8) | RESP[1]; Serial_puts ("Mmc_cmd 13\n"); if ((Card_status & 0x900)! = 0x900)//ready && Tran {return open_card_init_check_status_error;}//For MMC cards are often added CMD13, otherwise there will often be problems, CMD13 in the event of playing cmd3 can be sent at any time when (!) INREG16 (A_msc_stat (0)) & (1<<13)); Wait for programming to complete} else//if it is SD card sd_init (); return 0;
-----------------------------------------------------------------------------------------------
The image above is the initialization part I intercepted on the manual. After the cmd0 command has been put on the power, the system enters the idle state, CMD1 checks the voltage, then enters the ready state--&GT;CMD2 (read CID) into the Ident status-- CMD3 (set RCA) enters Stand State status---"Cmd7 into transport state can also be used here CMD6 set the size of the bit width SD does not support 8BIT MMC support

When the card has finished CMD3 into standby (stand-by state), Cmd7 can let the card into the transport status,











---------------------------------------------------------------------------------------SD card debugging key points:
1. When power-up time to delay long enough time to the SD card a preparation process, in my program is 5 seconds, according to different cards set different delay time. SD card Initialization The first step is to send the cmd command, in the case of the chip selection is valid first to send at least 74 clocks, otherwise the SD card may not initialize the problem.
2. After the SD card sends the reset command CMD0, to send the version query command CMD8, the return status is generally divided into two kinds, if the return 0x01 indicates that this SD card accepts CMD8, that is, this SD card supports version 2, if return 0x05 indicates that this SD card supports version 1. Because different versions of the SD card operation requirements are not the same, so be sure to query the version number of the SD card, otherwise the SD card will not work properly.
3. It is theoretically required to send CMD58 to obtain the SD card voltage parameters, but the actual process because the SD card operating voltage is known beforehand, so can omit this step to simplify the procedure. The agreement also recommends that you try not to use this order.
4. The SD card read-write time-out should be in accordance with the Protocol specification (read timeout: 100ms; write timeout: 250ms), this value should be calculated accurately in the program, otherwise you will not be able to read and write data problems. I define a calculation formula myself: Time-out = (8/CLK) *arg.
5. Within 2GB of SD card (standard card) and more than 2GB SD card (high-capacity card) in the form of address access, this is particularly important to note, otherwise there will be no read and write data problems. If the standard card reads or writes the address domain Fu Yan value 0x10 in the read or write command token, indicates the operation of the address unit after the 16th byte (provided that the SD card supports offset read and write operation), and the Address field Fu Yan value 0x10 in the high capacity card read or write command token The 16th block reads and writes, and the bulk card only supports block read and write operations, the block size is fixed to 512 bytes, and the byte operation will error.
6. It is best to perform a erase command when writing to a piece, so that the speed of writing can be greatly improved. Either the standard card or the large capacity card is executed as a block operation when the erase operation is performed, that is, erasing at least 512 bytes at a time.
7. When a standard card is byte-operated, the start and end must be within a physical sector, or read and write operations will not be possible. Block operations are recommended during the actual operation to improve efficiency. Whether it's a standard card or a high-capacity card. A read-write command operates on only one block, and does not allow cross-physical-layer address operations.
8. Before writing the data block to write several dummy data bytes, write a block data, the host to monitor the miso data line, if the slave is busy this data line will remain low, so that the host can be based on the status of this data line to determine whether to send the next command, The host is absolutely unable to execute other commands until the slave has released the Miso data cable, otherwise it will cause errors in the data being written, and the slave will not respond to the host's commands.
9. In SPI mode, CRC check is ignored, but still require the master and slave to send CRC code, but the value can be any value, the general host CRC code is usually set to 0x00 or 0xFF.
Read multi-block operation and write multi-block operation is not the same as the transmission stop, read multiple operations with the command CMD12 to terminate the transmission, while writing a multi-block operation with stop Tran token (Stop the transmission token, a value of 0xFD) to terminate the transmission. ----------------------------------------------------------------------------------------1, initialization steps: (1) delay of at least 74clock , wait for the SD card internal operation to complete, in the MMC protocol has the explicit explanation. (2) CS Low select SD card. (3) Send CMD0, need to return to 0x01, enter the idle state (4) In order to distinguish the SD card is 2.0 or 1.0, or an MMC card, here according to the principle of the protocol upward compatibility, first send only SD2.0 command CMD8, if CMD8 return error-free, then the preliminary judgment is 2.0 cards, further send the command loop send Cmd55+acmd41, Until you return 0x00, determine if the SD2.0 card is initialized successfully, enter the ready state, and then send the CMD58 command to determine whether it is HCSD or SCSD, to which the SD2.0 card is initialized successfully. If CMD8 returns an error, it is further judged as 1.0 or MMC, looping cmd55+acmd41, returning no error, Then SD1.0 card, to this SD1.0 card initial success, if in a certain number of cycles, returned as an error, then further send CMD1 to initialize, if the return error-free, is determined to be an MMC card, if a certain number of times, returned as an error, the card is not recognized, the initial end. (5) CS pull high.

In front of the read and write we can better read the status CMD13 to check the status bit, to determine whether the last command is finished 2, read the steps: (1) Send CMD17 (single block) or CMD18 (multi-block) Read command, return 0x00 (2) Receive data start token 0xFE (or 0XFC) + official data 512Bytes + CRC checksum 2Bytes The data length of the default official transmission is 512Bytes, the block length can be set CMD16.
3, write the steps: (1) Send CMD24 (single block) or CMD25 (multi-block) Write command, return 0x00 (2) Send data start token 0xFE (or 0XFC) + official data 512Bytes + CRC check 2Bytes
4, Erase steps: (1)     send CMD32, with a parameter to specify the first to erase the starting address (SD manual is said to be a block number) (2)     send CMD33,, specify the last address (3)     send CMD38, erase the contents of the specified interval this 3-step sequence cannot be reversed. Finally say a little bit of my experience: SD card is a memory, but with the command of the way to operate, as long as we have mastered the various commands and operation mode, you can operate the SD card flexibly, in addition I understand the IC card is similar principle, there is the proposal to start looking at the MMC protocol, simple and easy to understand, With some understanding of the MMC card, it is much easier to look at the SD card protocol. SD card commands are divided into 12 categories, class0 to CLASS11, different SDD cards, the main control according to their functions, support different command set as follows CLASS0:(card recognition, initialization and other basic command set) CMD0: Reset SD card. CMD1: Read the OCR register. CMD9: Read the CSD register. CMD10: Read the CID register. CMD12: Stop reading multiple blocks when data transfer CMD13: Read Card_status register Class2 (read card command Set): CMD16: Sets the length of the block CMD17: Read a single block. CMD18: Read multiple blocks until the host sends CMD12. CLASS4 (write card command Set): CMD24: Write a single block. CMD25: Write multiple pieces. CMD27: Write CSD register. CLASS5 (Erase Card command Set): CMD32: Sets the start address of the erase block. CMD33: Sets the end address of the erase block. CMD38: Erases the selected block. CLASS6 (write Protection Command Set): CMD28: Sets the address of the write protection block. CMD29: Erase the address of the write protection block. Cmd30:ask the card for the status of the write protection bits CLASS7: Card lock, unlock feature command set   CLASS8: Request a specific set of commands. class10-11: Reserved where CLASS1,CLASS3,CLASS9:SPI mode does not support  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.