3.22.24 SD/MMC Host Controller (SDHC)

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

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 cmd_error;
uint16_t data_error;
bool isCommandCompleted = false;
bool isDataCompleted = false;
uint8_t readBuffer[512];

static void SDHC_TransferEventHandler(
        SDHC_XFER_STATUS xferStatus,
        uintptr_t contextHandle
)
{
    if (xferStatus & SDHC_XFER_STATUS_CMD_COMPLETED)
    {
        // Command transfer complete, read the command error status.
        cmd_error = SDHC_CommandErrorGet();
        isCommandCompleted = true;
    }
    if (xferStatus & SDHC_XFER_STATUS_DATA_COMPLETED)
    {
        // Data transfer complete, read the data error status.
        data_error = SDHC_DataErrorGet();
        isDataCompleted = true;
    }
}

int main(void)
{
	/* Initialize all modules */
    SYS_Initialize ( NULL );
	
    // Register event handler with the SDHC PLIB. This is usually done once.
    SDHC_CallbackRegister(SDHC_TransferEventHandler, (uintptr_t) 0);

    SDHC_DataTransferFlags transferFlags;

    transferFlags.isDataPresent  = true;
    transferFlags.transferDir = SDHC_DATA_TRANSFER_DIR_READ;
    transferFlags.transferType = SDHC_DATA_TRANSFER_TYPE_SINGLE;

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

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

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

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

    // Check the status of isCommandCompleted and isDataCompleted

    // Other tasks ...
}

Library Interface

SDHC peripheral library provides the following interfaces:

Functions

NameDescription
SDHCx_BusWidthSetConfigures the width of the data bus
SDHCx_SpeedModeSetSets the bus speed
SDHCx_BlockSizeSetSets the size of the one data block of transfer
SDHCx_BlockCountSetSets the number of blocks to transfer
SDHCx_IsCmdLineBusyReturns the status of the command line
SDHCx_IsDatLineBusyReturns the status of the data line
SDHCx_IsWriteProtectedReturns the write protect switch pin level
SDHCx_IsCardAttachedIndicates if the card is attached or detached
SDHCx_ClockSetSets the SDHC clock frequency
SDHCx_ClockEnableEnable SDHC clock
SDHCx_ClockDisableDisable SDHC clock
SDHCx_CommandErrorGetReturns the errors associated with a command transfer
SDHCx_DataErrorGetReturns the errors associated with a data transfer
SDHCx_ErrorResetResets errors as specified by the resetType
SDHCx_ResponseReadReads the response to a given command
SDHCx_ModuleInitInitializes the SDHC peripheral
SDHCx_InitializeInitializes the SDHC peripheral and the internal data structures used by the peripheral library
SDHCx_CallbackRegisterThis function allows a SDHC PLIB client to set an event handler
SDHCx_CommandSendThis function allows the client to send a SD command on the SDHC interface
SDHCx_DmaSetupSets up the DMA for data transfers
SDHC_CardDetectEnableEnables the use of SD Card Detect Pin
SDHC_CardDetectDisableDisables the use of SD Card Detect Pin
SDHC_WriteProtectEnableEnables the use of SD Card Write Protect Pin
SDHC_WriteProtectDisableDisables the use of SD Card Write Protect Pin

Data types and constants

NameTypeDescription
SDHC_BUS_WIDTHEnumThe enumeration lists the bus widths for the SDHC bus
SDHC_SPEED_MODEEnumThe enumeration lists the bus widths for the SDHC bus
SDHC_CMD_RESP_TYPEEnumThe enumeration lists the supported response types
SDHC_READ_RESPONSE_REGEnumThe enumeration lists the response registers that can be read for a given command
SDHC_RESET_TYPEEnumThe enumeration lists error type to reset
SDHC_CLK_MODEEnumThe enumeration lists the clock modes
SDHC_XFER_STATUSEnumThe enumeration lists the status of the transfer
SDHC_DATA_TRANSFER_TYPEEnumThe enumeration lists single or multi block data transfer type
SDHC_DATA_TRANSFER_DIREnumThe enumeration lists the direction of the data transfer
SDHC_DataTransferFlagsStructThe data structure is used by the client to provide information about the data transfer to the SDHC PLIB
SDHC_CALLBACKTypedefThe prototype of the SDHC callback function