SD/MMC card initialization and read/write process

Source: Internet
Author: User

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   cid: Card identification Register read-only 128-bit manufacturer, Product ID, production date and serial number   CSD: Some writable 128-bit card capacity, erase sector size, read and write maximum data block size, read operation of the current, voltage and so on  CSR: card Configuration register 64 bit data bit width   rca:16 bit-related card address register, Card recognition process during the master controller and the card to negotiate a 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 CRC7 End bit 1   response format: 48-bit or 136-bit   card command: Type of command:  bc:broadcast without response  unresponsive broadcast   bcr:broadcast with response  responsive broadcast   ac:address (point-to-point) command:  Point-to-point, DATA0 No data   adtc:adress (point-to-point) data Transfer Commands Point-to-point, data0~data3 data line data   cmd0 on ~DATA3 cable , 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: To know the characteristics: only one card support, no RCA, command is just the basic subset of MMC/SD   SDHC: (Support 2GB~32GB): Understand the role of CMD8, command format and response, understand CSDV2.0 register has been extended SDIO WIFI: Add CMD52, Cmd53   cmd8 can extend some of the new features of existing commands by redefining the previously reserved bits. ACMD41 extended toSupport for high-capacity SD memory card initialization   A control register and other information are described above    Green indicates different points for SD and MMCNote for calculating the capacity of the cardfor SD card can refer to simplified_physical_layer_spec v2.0.pdf manual above hasfor MMC You can refer to the jesd84-a441.pdf Note for KCAL MMC is by sending the number 8th command to get 212 to 215 bits in EXT_CSDTo get the
---------------------------------------------------------------------------Static voidSd_init (void){intretries;u8*resp;unsignedintcardaddr;/resp= Mmc_cmd (2,0, MSC_CMDAT_RESPONSE_R2, msc_cmdat_response_r2);//serial_puts ("SD carsd CID =r2="); Serial_dump_data (resp,);RESP = Mmc_cmd (3,0, MSC_CMDAT_RESPONSE_R1, msc_cmdat_response_r1); Cardaddr= (resp[4] <<8) | resp[3];//occurs 3 command come or RCARCA = Cardaddr << -;//serial_puts ("rca="); Serial_puts_hex (RCA);resp= Mmc_cmd (9, RCA, MSC_CMDAT_RESPONSE_R2, MSC_CMDAT_RESPONSE_R2); Sd2_0= (resp[ -] &0xc0) >>6;//serial_puts ("sd2_0====="); Serial_puts_hex (SD2_0);//serial_puts ("SD carsd CSD Register =r2="); Serial_dump_data (resp, +);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<<13));//waiting for programming to complete}------------------------------------------------------------//look from here .intMmc_init (void)//{intretries;u8*resp;outreg32 (A_CPM_MSCCDR, -); 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 cardRESP = Mmc_cmd (0,0, the,0);//First 80 ClocksRESP = Mmc_cmd (8,0X1AA,0x1, MSC_CMDAT_RESPONSE_R1);//SD2 (return 0x1) after the card or SD1 (return 0x5) is now basically SD2 later, so there is no judgment on the return valueRESP = Mmc_cmd ( -,0, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1);//according to Cmd55 to determine whether it is an MMC card or SD card, only the card has 55 command to correctly have a return value return value has command index =0x37=55 words is SD//serial_puts ("Application specific Commands =r1="); Serial_dump_data (RESP, 6);if(resp[5] !=0x37) {//is an MMC card//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 SD card is CMD41 because Power is a time, so we use a do{}while to delaySd_mdelay (Ten);} while(retries--&&!) (resp[4] &0x80));//The power-up was completed until 31 bits of 1 in the OCR reply value in R3if(resp[4]&0x40)//voltage verification with the data access mode bit in the last response, and 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 check whether a high-capacity card (>2G) or a standard card can actually use the CMD9 command to read the CDs to determine what the SD is doing.}#if0Serial_puts ("OCR =r3="); Serial_dump_data (RESP,6);if(resp[4]&0x80) serial_puts ("\N\NMMC Init ok\n\n");//indicates power-on completionElseSerial_puts ("\N\NMMC Init fail\n\n");#endifresp= Mmc_cmd (2,0, MSC_CMDAT_RESPONSE_R2, MSC_CMDAT_RESPONSE_R2);//Get CID//serial_puts ("CID CSD =r2="); Serial_dump_data (resp, +);RESP = Mmc_cmd (3,0x10, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1);//this thing is disgusting. In the SD card and the parameters of the MMC card is not the same as the parameters of the MMC card is set to an RCA value, but the SD card is read by the value of RCA, the above SD card is described in the initializationOUTREG16 (A_MSC_CLKRT (0),1); RESP= Mmc_cmd (7,0x10, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1);//set the new address as a valid address this enters the Tarnsport mode. Only the 7th command is sent, and the bit width CMD6 is set to occur. Read-write occurs 16 17 18, etc.RESP = Mmc_cmd (6,0x3b70101, msc_cmdat_bus_width_4bit| msc_cmdat_response_r1| Msc_cmdat_busy, MSC_CMDAT_RESPONSE_R1);//response type R1b, set bit width to 4BIT moderesp= Mmc_cmd ( -, RCA, MSC_CMDAT_RESPONSE_R1, MSC_CMDAT_RESPONSE_R1); Card_status= (resp[4] << -) | (resp[3] << -) | (resp[2] <<8) | resp[1];serial_puts ("Mmc_cmd 13\n");if((Card_status &0x900) !=0x900)//Ready && Tran{returnOpen_card_init_check_status_error;}//for MMC card often to add CMD13, otherwise often problems, CMD13 in the event of playing cmd3 can be sent at any time while(! (INREG16 (A_msc_stat (0)) & (1<< -)));//waiting for programming to complete}Else//if it is an SD cardsd_init ();return 0;

that's the initialization part I intercepted on the manual. After the CMD0 command has been power on, the system enters the idle state, CMD1 check the voltage,then enter the Ready state-->CMD2 (read CID) into the Ident state and CMD3 (set RCA) into the stand---"cmd7 into the transport state can also be used here CMD6 setting 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 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 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SD card sends the reset command CMD0, to send the version query command CMD8, the return status is generally divided into two, if the return of 0X01 indicates that the SD card accepts CMD8, that is, this SD card supports version 2 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.       theoretically requires the transmission of CMD58 to obtain the SD card voltage parameters, but the actual process because the SD card operating voltage is known beforehand, so you can omit this step to simplify the procedure. The agreement also recommends that you try not to use this order.  4. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SD Card Read-write time-out to 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. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;2GB the 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.     &nBSP; 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.       Write a block of data before writing a number of dummy data bytes, write a block of data, the host to monitor the miso data line, if the slave is in a busy state of the data line will remain low level, This allows the host to determine whether to send the next command based on the state of the data line, and the host will never be able to execute other commands until the slave has released the Miso data line, otherwise it would 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, and write 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 at least 74clock, waiting for the internal operation of the SD card to complete, in the MMC protocol is clearly stated. (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 sent cmd55+ ACMD41, until it returns 0X00, determines that the SD2.0 card is initialized successfully, enters the ready state, and then sends the CMD58 command to determine whether HCSD or SCSD, to which the SD2.0 card was initialized successfully. If CMD8 returnsError is further judged as 1.0 card or MMC card, loop send Cmd55+acmd41, return error-free, 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.    2. Read steps: (1)     send CMD17 (single block) or CMD18 (multiple blocks) Read command, return 0x00 (2)     receive data start token 0xFE (or 0XFC) + Official data 512Bytes + CRC checksum 2Bytes The default officially transmitted data length is 512Bytes and can be CMD16 set block length.  3, write 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, followed by a parameter to specify the first address to erase (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 theCard for the status of the write protection BITSCLASS7: Card lock, unlock feature command set &NBSP;CLASS8: Request a specific set of commands. class10-11: Reserved where CLASS1,CLASS3,CLASS9:SPI mode does not support  

SD/MMC card initialization and read/write process

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.