mmc

The MMC driver provides support for SPI based MMC/SDcard interfaces. It exports a disk_ops struct that can be used by any FS. Currently only fatfs can run over MMC.

Initialization

int mmc_init(int spi_num, void *spi_cfg, int ss_pin)

Initializes the mmc driver to be used by a FS.

MMC uses the hal_gpio interface to access the SPI ss_pin and the hal_spi interface for the communication with the card. spi_cfg must be a hw dependent structure used by hal_spi_init to initialize the SPI subsystem.

Dependencies

To include the mmc driver on a project, just include it as a dependency in your pkg.yml:

pkg.deps:
    - hw/drivers/mmc

Returned values

MMC functions return one of the following status codes:

Return codeDescription
MMC_OKSuccess.
MMC_CARD_ERRORGeneral failure on the card.
MMC_READ_ERRORError reading from the card.
MMC_WRITE_ERRORError writing to the card.
MMC_TIMEOUTTimed out waiting for the execution of a command.
MMC_PARAM_ERRORAn invalid parameter was given to a function.
MMC_CRC_ERRORCRC error reading card.
MMC_DEVICE_ERRORTried to use an invalid device.
MMC_RESPONSE_ERRORA command received an invalid response.
MMC_VOLTAGE_ERRORThe interface doesn't support the requested voltage.
MMC_INVALID_COMMANDThe interface haven't accepted some command.
MMC_ERASE_ERRORError erasing the current card.
MMC_ADDR_ERRORTried to access an invalid address.

Header file

#include "mmc/mmc.h"

Example

This example runs on the STM32F4-Discovery and prints out a listing of the root directory on the currently installed card.

// NOTE: error handling removed for clarity!

struct stm32f4_hal_spi_cfg spi_cfg = {
    .ss_pin   = SPI_SS_PIN,
    .sck_pin  = SPI_SCK_PIN,
    .miso_pin = SPI_MISO_PIN,
    .mosi_pin = SPI_MOSI_PIN,
    .irq_prio = 2
};

mmc_init(0, &spi_cfg, spi_cfg.ss_pin);
disk_register("mmc0", "fatfs", &mmc_ops);

fs_opendir("mmc0:/", &dir);

while (1) {
    rc = fs_readdir(dir, &dirent);
    if (rc == FS_ENOENT) {
        break;
    }

    fs_dirent_name(dirent, sizeof(out_name), out_name, &u8_len);
    printf("%s\n", out_name);
}

fs_closedir(dir);