SPI provides a protocol for sending and receiving bytes, but the interface assumes nothing about the contents of the bytes being transferred. The MultiMediaCard specification defines commands to use in SPI communications. The host uses the commands to retrieve information about a card and its status, to send control information to a card, and to read and write data in the card’s storage media. This chapter introduces the MultiMediaCard commands and shows how to implement the commands in firmware. A host can use the same commands and firmware to access SD Cards.
Command and Response Formats
Each MultiMediaCard command has a defined format for the command, a response, and any additional data the command sends or requests. On the SPI bus, the DataIn line carries commands and data sent by the host, and the DataOut line carries responses and data sent by the MultiMediaCard.
The information that travels on the bus includes commands, command responses, and tokens that contain data and error indications.
Each MultiMediaCard command is 48 bits. Table 5-1 shows the format of the commands.
The command index is a number that identifies the command. Each command also has a name consisting of “CMD” followed by the index value (CMD0, CMD1, and so on) and a descriptive abbreviation. For example, the descriptive abbreviation for CMD17 is READ_SINGLE_BLOCK.
The command argument is a 32-bit value that can provide supplementary information required to carry out a command. For example, the argument
for READ_SINGLE_BLOCK is the beginning address of the block to read. For commands that don’t require arguments, the host sends stuff bits of zero.
The CRC value is seven bits that the MultiMediaCard controller can use to verify that a command arrived without error.
The transmission bit is set to 1. The start bit and end bit mark the beginning and end of the command.
On receiving a command and argument, a MultiMediaCard returns a response. The MultiMediaCard specification defines six response types with each command having a designated response type. Most commands use response type R1 or R1b. A few use R2 or R3. Only I/O cards use types R4 and R5.
These are the response types a mass-storage device uses on an SPI bus.
Response type R1 is a single byte. A value of 00h means that the command completed without error. On error, one or more of the bits are set:
Bit 0: The card is in the idle state and is initializing.
Bit 1: An erase sequence was cleared before executing the command because an out-of-erase-sequence command was received.
Table 5-1: MultiMediaCard commands have 48 bits.
Bit 2: An illegal or unsupported command code was received.
Bit 3: CRC error.
Bit 4: An error occurred in the sequence of erase commands.
Bit 5: The device received an address that doesn’t match the expected block length.
Bit 6: The command’s argument was out of range.
Bit 7: Always zero.
The RESPONSE_1 union defines a byte or eight bits that can hold an R1 response:
Response type R1b is identical to the R1 response format except the response byte is followed by one or more bytes that indicate busy status. The card returns zeroes continuously while busy and returns a non-zero byte when ready to receive another command. In other words, DataOut remains low to indicate busy and goes high when no longer busy.
In SPI mode, response type R2 is used only for the SEND_STATUS command. The response is two bytes. The first byte is identical to the R1 format. The second byte contains eight status bits. On error, one or more of the bits are set:
Bit 0: The user has locked the card.
Bit 1: The host has attempted to erase a write-protected block or a sequence or password error occurred during a card lock/unlock operation.
Bit 2: General or unknown error.
Bit 3: Internal card-controller error.
Bit 4: A card’s internal error-correction code was applied but failed to correct the data.
Bit 5: The command tried to write to a write-protected block.
Bit 6: The selection of erase groups was invalid.
Bit 7: The command argument was out of range, or the command is attempting to change the ROM section, or the command is attempting to reverse the copy bit or permanent write-protect bit in the CSD register.
The RESPONSE_2 union defines a word, bytes, or bits that can hold an R2 response:
Response type R3 is five bytes. In SPI mode, the only command that uses this response type is READ_OCR, which requests the contents of the operation
conditions register. The first byte is identical to the R1 format. The next four bytes are the contents of the register.
Commands that send or request blocks of data use structures called tokens to hold the data and MultiMediaCard responses. There are three types of tokens: data, data_response, and data_error.
Data blocks being sent to or from a MultiMediaCard’s storage media travel in data tokens. In SPI mode, each data token consists of a Start Block token (1 byte) followed by the data block (1 or more bytes) and a CRC value (2 bytes). Table 5-2 shows the values for Start Block tokens and the Stop Tran token, which a host can send to end a command that is writing multiple blocks. Note that FEh is the Start Block token for all operations except multiple block writes.
After receiving a data token, the MultiMediaCard returns a 1-byte data_response token. These are the values of the bits in the data_response token:
Bit 0: 1.
Bits 3..1: Status
010 = Data accepted
101 = CRC error
110 = Write error
Bit 4: 0.
Bits 5..7: Don’t care.
To determines if the data was accepted, firmware can logically AND the data_response byte with 0Fh. If the result is 05h, the data was accepted.
Table 5-2: A Start Block data token precedes a block of data being sent to or from the storage media. A Stop Tran data token requests to end a multiple-block write.
When unable to return a requested data token, a MultiMediaCard returns a
data-error token. These are the values of the bits in the data_error token:
Bit 0: General or unknown error.
Bit 1: Internal card controller error.
Bit 2: A card’s internal error-correction code was applied but failed to correct
Bit 3: The command’s argument was out of range.
Bit 4: The user has locked the card.
Bits 5..7: Zero.