3.27.19 Secure Digital MultiMedia Card Controller (SDMMC)

The SDMMC PLIB provides low level APIs to configure and transfer data using the SD host controller. The SDMMC PLIB supports SDMMC and eMMC modes, default and high speed data transfer, 1/4/8 bit data bus configurations. The PLIB performs the transfers in non-blocking mode. Transfers involving a data stage are performed using DMA.

Using The Library

The example code demonstrates sending a SD command to read a single block of data from the SD card. This example assumes that the SD card is already initialized by the application and is ready to accept read and write requests. The example application does not show the larger SD protocol required in order to communicate with the SD card.

uint16_t data_error;
bool isCommandCompleted = false;
bool isDataCompleted = false;
uint8_t readBuffer[512];

static void SDMMC_TransferEventHandler(
        SDMMC_XFER_STATUS xferStatus,
        uintptr_t contextHandle
)
{
    if (xferStatus & SDMMC_XFER_STATUS_CMD_COMPLETED)
    {
        // Command transfer complete, read the command error status.
        cmd_error = SDMMC1_CommandErrorGet();
        isCommandCompleted = true;
    }
    if (xferStatus & SDMMC_XFER_STATUS_DATA_COMPLETED)
    {
        // Data transfer complete, read the data error status.
        data_error = SDMMC1_DataErrorGet();
        isDataCompleted = true;
    }
}

int main(void)
{

    // Register event handler with the SDMMC PLIB. This is usually done once.
    SDMMC1_CallbackRegister(SDMMC_TransferEventHandler, (uintptr_t) 0);

    SDMMC_DataTransferFlags transferFlags;

    transferFlags.isDataPresent  = true;
    transferFlags.transferDir = SDMMC_DATA_TRANSFER_DIR_READ;
    transferFlags.transferType = SDMMC_DATA_TRANSFER_TYPE_SINGLE;

    // Set the block size to 512 bytes
    SDMMC1_BlockSizeSet(512);

    // Set the block count to 1
    SDMMC1_BlockCountSet(1);

    // Set up the DMA to read 512 btyes of data
    SDMMC1_DmaSetup(readBuffer, 512, SDMMC_DATA_TRANSFER_DIR_READ);

    // Send command to read one block of data from SD card starting at block address 100
    SDMMC1_CommandSend(17, 100, SDMMC_CMD_RESP_R1, transferFlags);

    // Check the status of isCommandCompleted and isDataCompleted

    // Other tasks ...
}

Library Interface

SD/MMC Host Controller peripheral library provides the following interfaces:

Functions

NameDescription
SDMMCx_BusWidthSetConfigures the width of the data bus
SDMMCx_SpeedModeSetSets the bus speed
SDMMCx_BlockSizeSetSets the size of the one data block of transfer
SDMMCx_BlockCountSetSets the number of blocks to transfer
SDMMCx_IsCmdLineBusyReturns the status of the command line
SDMMCx_IsDatLineBusyReturns the status of the data line
SDMMCx_IsWriteProtectedReturns the write protect switch pin level
SDMMCx_IsCardAttachedIndicates if the card is attached or detached
SDMMCx_ClockSetSets the SDMMC clock frequency
SDMMCx_ClockEnableEnable SDMMC clock
SDMMCx_ClockDisableDisable SDMMC clock
SDMMCx_CommandErrorGetReturns the errors associated with a command transfer
SDMMCx_DataErrorGetReturns the errors associated with a data transfer
SDMMCx_ErrorResetResets errors as specified by the resetType
SDMMCx_ResponseReadReads the response to a given command
SDMMCx_ModuleInitInitializes the SDMMC peripheral
SDMMCx_InitializeInitializes the SDMMC peripheral and the internal data structures used by the peripheral library
SDMMCx_CallbackRegisterThis function allows a SDMMC PLIB client to set an event handler
SDMMCx_CommandSendThis function allows the client to send a SD command on the SDMMC interface
SDMMCx_DmaSetupSets up the DMA for data tranfers

Data types and constants

NameTypeDescription
SDMMC_BUS_WIDTHEnumThe enumeration lists the bus widths for the SDMMC bus
SDMMC_SPEED_MODEEnumThe enumeration lists the bus widths for the SDMMC bus
SDMMC_READ_RESPONSE_REGEnumThe enumeration lists the response registers that can be read for a given command
SDMMC_RESET_TYPEEnumThe enumeration lists error type to reset
SDMMC_XFER_STATUSEnumThe enumeration lists the status of the transfer
SDMMC_DATA_TRANSFER_DIREnumThe enumeration lists the direction of the data transfer
SDMMC_DataTransferFlagsStructThe data structure is used by the client to provide information about the data trasnfer to the SDMMC PLIB
SDMMC_CALLBACKTypedefThe prototype of the SDMMC callback function