SD SDHC & MMC device driver

The SD/MMC drivers can be used to access SD/MMC compatible storage devices such as SD/SDHC/SDXC cards, MMC/MMCplus cards and e.MMC storage devices providing an application large storage capacities at a low-cost. The SD/MMC drivers take care of the identification and initialization of the SD/MMC compatible storage device and performs data transfers very efficiently using the best possible transfer mode supported by the connected storage device. The SD/MMC drivers require only a minimal amount of ROM and RAM that allows them to be used on any resource-constrained system.

The SD/MMC compatible storage devices implement a relatively simple command based protocol where the SD/MMC driver initiates all the data exchanges. The data can be exchanged via two different operating modes: SPI mode and bus mode. emFile comes with a driver for each these operating modes:

  • SPI SD/MMC driver
  • Card mode SD/MMC driver

The SPI SD/MMC driver uses the SPI interface to exchange data to SD/MMC compatible storage devices. Using this mode allows low cost MCU to use large storage capacities at the expense of a relatively low performance. The card mode SD/MMC driver provides the best performance by exchanging the data via four separate data lines. It requires that the MCU features an SD host controller to take care of all the low-level communication.

How devices operate

MMC and SD memory cards are small form factor mass storage devices. The main design goal of these devices are to provide a very low cost mass storage product, implemented as a card with a simple controlling unit, and a compact, easy-to-implement interface. These requirements lead to a reduction of the functionality of each card to an absolute minimum. In order to have a flexible design, MMC and SD cards are designed to be used in different I/O modes.

The difference between MMC and SD cards

The difference between SD and MMC cards is that SD cards can operate at different clock frequency. In standard mode the clock range of an SD card can be between 0 and 25 MHz, whereas MMC cards can typically operate up to 26 MHz. SD and MMC cards also support a high-speed mode which doubles the maximum operating frequency. Additionally the initialization of these cards differs. They need to be initialized differently, but after initialization they behave the same way. MMC and SD cards also differ in the number of I/O pins. SD cards have more I/O pins than MMC cards. Depending in which mode they are used the pins are used or not. Additionally SD cards have a write protect switch, which can be used to protect the data on the card against inadvertent changes.

e.MMC storage devices are non-removable electronic components that integrate the flash memory and the controller. They can be soldered directly on the circuit board to provide non-volatile storage to an embedded system. e.MMC storage devices can operate at frequencies up to 200 Mhz and transfer data in single and dual rate mode.

Card mode

SPI mode

MMC Cards use a seven pin interface in MMC card mode (Command, Clock, Data and 3x Power lines).
In contrast to the MMC, SD cards use a 9 pin interface (Command, Clock, 1 or 4 Data and 3 Power lines).

Both card systems use the same the pin interface. (ChipSelect (CS), DataIn, DataOut, Clock and 3 Power lines). The Serial Peripheral Interface (SPI) bus is a very loose de facto standard for controlling almost any digital electronics that accepts a clocked serial stream of bits. SPI operates in full duplex (sending and receiving at the same time).

Pin description for MMC/SD card in card mode

Pin No.NameTypeExplanation
1CD/DAT3Input/Output using push pull driversAfter power up this line is input with 50 kOhm pull-up. This can be used for card detection. Relevant only for SD cards, pull-up resistor is disabled after initialization procedure for using this line as DATA3 line for data transfer.
2CMDPush PullThis is a bi-directional line. It is a bidirectional command channel used for card initialization and data transfer commands. The CMD signal has two operation modes: open-drain for initialization mode and push-pull for fast command transfer. Commands are sent from the MultiMediaCard bus master (card host controller) to the card and responses from the cards to the host.
3VssPower supplySupply voltage ground.
4VddPower supplySupply voltage.
5CLKInputWith each cycle of this signal an one bit transfer on the command and data lines is done. The frequency may vary between zero and the maximum clock frequency.
6Vss2Power supplySupply voltage ground.
7DAT[0]Input/Output using push pull driverDAT is a bidirectional data channel. The DAT signal operates in push-pull mode. Only one card or the host is driving this signal at a time. Relevant only for SD cards: On data transfer, this line is DATA 0.
8DAT[1]Input/Output using push pull driversOn MMC card this line does not exist. Relevant only for SD cards: On data transfer, this line is DATA 1.
9DAT[2]Input/Output using push pull driversOn MMC card this line does not exist. Relevant only for SD cards: On data transfer, this line is DATA 2.

Pin description for MMC/SD Card in SPI mode

Pin No.NameTypeExplanation
1CSInputChip Select sets the card active at low level and inactive at high level.
2Data InInputData In is seen from the card, therefore data transmitted to the card will be received from this line.
3VssSupply groundSupply voltage ground.
4VddSupply voltageSupply voltage.
5SCLKInputClock signal must be generated by the target system. The card is always in slave mode.
6Vss2Supply groundSupply voltage ground.
7Data OutOutputData Out is seen from the card. Data transferred to host will be sent by card from this line.
8ReservedNot used
9ReservedNot used

Additional information

  • Data transfer width is 8-bit.
  • Data should be output on the falling edge and must remain valid until the next period. Rising edge means data is sampled (i.e. read).
  • Bit order requires most significant bit (MSB) to be sent out first.
  • Data polarity is normal, which means a logical “1” is represented with a high level on the data line and a logical “0” is represented with low level.
  • MMC & SD cards support different voltage ranges. Initial voltage should be 3.3V.

Power control

Power control should be considered when creating designs using the MMC and/or SD Card. The ability to have software power control of the cards makes the design more flexible and robust. The host will have the ability to turn power to the card on or off independent of whether the card is inserted or removed. This can help with card initialization when there is contact bounce during card insertion. The host waits a specified time after the card is inserted before powering up the card and starting the initialization process. Also, if the card goes into an unknown state, the host can cycle the power and start the initialization process again. When card access is unnecessary, allowing the host to power-down the bus can reduce overall power consumption.

Fail-safe operation

Unexpected Reset:

The data will be preserved.

Power failure:

Power failure can be critical: If the card does not have sufficient time to complete a write operation, data may be lost. Countermeasures: In the system design make sure the power supply for the card drops slowly to allow sufficient time to save pending card data.

Wear leveling

MMC / SD memory cards are managed by an internal controller. This controller also responsible for the wear leveling. Therefore, the device driver does not need to handle wear leveling.