SWRU543B January 2019 – June 2025 CC3230S , CC3230SF , CC3235MODS , CC3235MODSF , CC3235S , CC3235SF
The code that follows shows a card detection and initialization using peripheral APIs:
CardInit(CardAttrib_t *CardAttrib)
{
unsigned long ulRet;
unsigned long ulResp[4];
//
// Initialize the attributes.
//
CardAttrib->ulCardType = CARD_TYPE_UNKNOWN;
CardAttrib->ulCapClass = CARD_CAP_CLASS_SDSC;
CardAttrib->ulRCA = 0;
CardAttrib->ulVersion = CARD_VERSION_1;
//
// Send std GO IDLE command
//
if( SendCmd(CMD_GO_IDLE_STATE, 0) == 0)
{
ulRet = SendCmd(CMD_SEND_IF_COND,0x00000100);
//
// It's a SD ver 2.0 or higher card
//
if(ulRet == 0)
{
CardAttrib->ulVersion = CARD_VERSION_2;
CardAttrib->ulCardType = CARD_TYPE_SDCARD;
//
// Wait for card to become ready.
//
do
{
//
// Send ACMD41
//
SendCmd(CMD_APP_CMD,0);
ulRet = SendCmd(CMD_SD_SEND_OP_COND,0x40E00000);
//
// Response contains 32-bit OCR register
//
SDHostRespGet(SDHOST_BASE,ulResp);
}while(((ulResp[0] >> 31) == 0));
if(ulResp[0] & (1UL<<30)) {
CardAttrib->ulCapClass = CARD_CAP_CLASS_SDHC;
}
}
else //It's a MMC or SD 1.x card
{
//
// Wait for card to become ready.
//
do
{
if( (ulRet = SendCmd(CMD_APP_CMD,0)) == 0 )
{
ulRet = SendCmd(CMD_SD_SEND_OP_COND,0x00E00000);
//
// Response contains 32-bit OCR register
//
SDHostRespGet(SDHOST_BASE,ulResp);
}
}while((ulRet == 0)
&&((ulResp[0] >>31) == 0));
//
// Check the response
//
if(ulRet == 0)
{
CardAttrib->ulCardType = CARD_TYPE_SDCARD;
}
else // CMD 55 is not recognised by SDHost cards.
{
//
// Confirm if its a SDHost card
//
ulRet = SendCmd(CMD_SEND_OP_COND,0);
if( ulRet == 0)
{
CardAttrib->ulCardType = CARD_TYPE_MMC;
}
}
}
}
//
// Get the RCA of the attached card
//
if(ulRet == 0)
{
ulRet = SendCmd(CMD_ALL_SEND_CID,0);
if( ulRet == 0)
{
SendCmd(CMD_SEND_REL_ADDR,0);
SDHostRespGet(SDHOST_BASE,ulResp);
//
// Fill in the RCA
//
CardAttrib->ulRCA = (ulResp[0]
>> 16);
//
// Get tha card capacity
//
CardAttrib->ullCapacity = CardCapacityGet(CardAttrib->ulRCA);
}
}
//
// return status.
//
return ulRet;
}
The structure used in the API has the following format:
typedef struct
{
unsigned long ulCardType;
unsigned long long ullCapacity;
unsigned long ulVersion;
unsigned long ulCapClass;
unsigned short ulRCA;
}CardAttrib_t;