MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Secure Digital Card/Embedded MultiMedia Card (CARD)

Overview

The MCUXpresso SDK provides a driver to access the Secure Digital Card and Embedded MultiMedia Card based on the SDHC/USDHC/SDIF driver.

Function groups

This function group implements the SD card functional API.

This function group implements the MMC card functional API.

Typical use case

SD CARD Operation

error log support

Lots of error log has been added to sd relate functions, if error occurs during initial/read/write, please enable the error log print functionality with #define SDMMC_ENABLE_LOG_PRINT 1 And rerun the project then user can check what kind of error happened.

User configurable

typedef struct _sd_card
{
SDMMCHOST_CONFIG host;
sdcard_usr_param_t usrParam;
bool isHostReady;
bool noInteralAlign;
uint32_t busClock_Hz;
uint32_t relativeAddress;
uint32_t version;
uint32_t flags;
uint32_t rawCid[4U];
uint32_t rawCsd[4U];
uint32_t rawScr[2U];
uint32_t ocr;
sd_cid_t cid;
sd_csd_t csd;
sd_scr_t scr;
uint32_t blockCount;
uint32_t blockSize;
sd_timing_mode_t currentTiming;
sd_driver_strength_t driverStrength;
sd_max_current_t maxCurrent;
sdmmc_operation_voltage_t operationVoltage;

Part of The variables above is user configurable,

  1. SDMMCHOST_CONFIG host Application need to provide host controller base address and the host's source clock frequency.
  2. sdcard_usr_param_t usrParam Two member in the userParam structure, a. cd-which allow application define the card insert/remove callback function, redefine the card detect timeout ms and also allow application determine how to detect card. b. pwr-which allow application redefine the power on/off function and the power on/off delay ms. However, sdmmc always use the default setting if application not define it in application. The default setting is depend on the macro defined in the board.h.
  3. bool noInteralAlign Sdmmc include an address align internal buffer(to use host controller internal DMA), to improve read/write performance while application cannot make sure the data address used to read/write is align, set it to true will achieve a better performance.
  4. sd_timing_mode_t currentTiming It is used to indicate the currentTiming the card is working on, however sdmmc also support preset timing mode, then sdmmc will try to switch to this timing first, if failed, a valid timing will switch to automatically. Generally, user may not set this variable if you don't know what kind of timing the card support, sdmmc will switch to the highest timing which the card support.
  5. sd_driver_strength_t driverStrength Choose a valid card driver strength if application required and call SD_SetDriverStrength in application.
  6. sd_max_current_t maxCurrent Choose a valid card current if application required and call SD_SetMaxCurrent in application.

Board dependency

Sdmmc depend on some board specific settings, such as card detect - which is used to detect card, the card active level is determine by the socket you are using, low level is active usually, but some socket use high as active, please set the macro #define BOARD_xxxx_CARD_INSERT_CD_LEVEL (0U) According to your board specific if you cannot detect card even if card is inserted.

power control - Macro for USDHC only, SDHC/SDIF support high speed timing only, so please ignore the power reset pin for SDHC/SDIF. which is used to reset card power for UHS card, to make UHS timing work properly, please make sure the power reset pin is configured properly in board.h. #define BOARD_SD_POWER_RESET_GPIO (GPIO1) #define BOARD_SD_POWER_RESET_GPIO_PIN (5U) #define BOARD_USDHC_SDCARD_POWER_CONTROL_INIT() \

pin configurations - Function for USDHC only. which is used to switch the signal pin configurations include driver strength/speed mode dynamiclly for different timing mode, reference the function defined in board.c void BOARD_SD_Pin_Config(uint32_t speed, uint32_t strength)

Typical use case

/* Save host information. */
card->host.base = BOARD_SDHC_BASEADDR;
card->host.sourceClock_Hz = CLOCK_GetFreq(BOARD_SDHC_CLKSRC);
/*Redefine the cd and pwr in the application if required*/
card->usrParam.cd = &s_sdCardDetect;
card->usrParam.pwr = &s_sdCardPwrCtrl;
/* intial the host controller */
/*wait card inserted, before detect card you can power off card first and power on again after card inserted, such as*/
SD_PowerOffCard(card->host.base, card->usrParam.pwr);
SD_WaitCardDetectStatus(card->host.base, card->usrParam.cd, true);
SD_PowerOnCard(card->host.base, card->usrParam.pwr);
/*call card initial function*/
/* Or you can call below function directly, it is a highlevel init function which will include host initialize,card detect, card initial */
/* Init card. */
if (SD_Init(card))
{
PRINTF("\r\nSD card init failed.\r\n");
}
/* after initialization finised, access the card with below functions. */
while (true)
{
if (kStatus_Success != SD_WriteBlocks(card, g_dataWrite, DATA_BLOCK_START, DATA_BLOCK_COUNT))
{
PRINTF("Write multiple data blocks failed.\r\n");
}
if (kStatus_Success != SD_ReadBlocks(card, g_dataRead, DATA_BLOCK_START, DATA_BLOCK_COUNT))
{
PRINTF("Read multiple data blocks failed.\r\n");
}
if (kStatus_Success != SD_EraseBlocks(card, DATA_BLOCK_START, DATA_BLOCK_COUNT))
{
PRINTF("Erase multiple data blocks failed.\r\n");
}
}
SD_Deinit(card);

MMC CARD Operation

error log support

Not support yet

User configuable

Board dependency

Typical use case

/* Save host information. */
card->host.base = BOARD_SDHC_BASEADDR;
card->host.sourceClock_Hz = CLOCK_GetFreq(BOARD_SDHC_CLKSRC);
/* MMC card VCC supply, only allow 3.3 or 1.8v, depend on your board config.
* If a power reset circuit is avaliable on you board for mmc, and 1.8v is supported,
* #define BOARD_USDHC_MMCCARD_POWER_CONTROL_INIT()
* #define BOARD_USDHC_MMCCARD_POWER_CONTROL(state)
* in board.h must be implemented.
* User can remove preset the voltage window and sdmmc will switch VCC automatically. */
card->hostVoltageWindowVCC = BOARD_MMC_VCC_SUPPLY;
/* Init card. */
if (MMC_Init(card))
{
PRINTF("\n MMC card init failed \n");
}
/* after initialization finised, access the card with below functions. */
while (true)
{
if (kStatus_Success != MMC_WriteBlocks(card, g_dataWrite, DATA_BLOCK_START, DATA_BLOCK_COUNT))
{
PRINTF("Write multiple data blocks failed.\r\n");
}
if (kStatus_Success != MMC_ReadBlocks(card, g_dataRead, DATA_BLOCK_START, DATA_BLOCK_COUNT))
{
PRINTF("Read multiple data blocks failed.\r\n");
}
}
MMC_Deinit(card);

SDIO CARD Operation

error log support

Not support yet

User configuable

Board dependency

Typical use case

Data Structures

struct  sdmmchost_detect_card_t
 sd card detect More...
 
struct  sdmmchost_pwr_card_t
 card power control More...
 

Macros

#define SDMMCHOST_NOT_SUPPORT   0U
 use this define to indicate the host not support feature
 
#define SDMMCHOST_SUPPORT   1U
 use this define to indicate the host support feature
 

Typedefs

typedef void(* sdmmchost_cd_callback_t )(bool isInserted, void *userData)
 card detect callback definition
 
typedef void(* sdmmchost_pwr_t )(void)
 card power control function pointer
 

Enumerations

enum  _sdmmchost_endian_mode {
  kSDMMCHOST_EndianModeBig = 0U,
  kSDMMCHOST_EndianModeHalfWordBig = 1U,
  kSDMMCHOST_EndianModeLittle = 2U
}
 host Endian mode corresponding to driver define More...
 
enum  sdmmchost_detect_card_type_t {
  kSDMMCHOST_DetectCardByGpioCD,
  kSDMMCHOST_DetectCardByHostCD,
  kSDMMCHOST_DetectCardByHostDATA3
}
 sd card detect type More...
 

Variables

sdmmchost_detect_card_type_t sdmmchost_detect_card_t::cdType
 card detect type
 
uint32_t sdmmchost_detect_card_t::cdTimeOut_ms
 
                  card detect timeout which allow 0 - 0xFFFFFFF, value 0 will return immediately, value

0xFFFFFFFF will block until card is insert

 
sdmmchost_cd_callback_t sdmmchost_detect_card_t::cardInserted
 card inserted callback which is meaningful for interrupt case
 
sdmmchost_cd_callback_t sdmmchost_detect_card_t::cardRemoved
 card removed callback which is meaningful for interrupt case
 
void * sdmmchost_detect_card_t::userData
 user data
 
sdmmchost_pwr_t sdmmchost_pwr_card_t::powerOn
 power on function pointer
 
uint32_t sdmmchost_pwr_card_t::powerOnDelay_ms
 power on delay
 
sdmmchost_pwr_t sdmmchost_pwr_card_t::powerOff
 power off function pointer
 
uint32_t sdmmchost_pwr_card_t::powerOffDelay_ms
 power off delay
 

adaptor function

static status_t SDMMCHOST_NotSupport (void *parameter)
 host not support function, this function is used for host not support feature More...
 
status_t SDMMCHOST_WaitCardDetectStatus (SDMMCHOST_TYPE *hostBase, const sdmmchost_detect_card_t *cd, bool waitCardStatus)
 Detect card insert, only need for SD cases. More...
 
bool SDMMCHOST_IsCardPresent (void)
 check card is present or not. More...
 
status_t SDMMCHOST_Init (SDMMCHOST_CONFIG *host, void *userData)
 Init host controller. More...
 
void SDMMCHOST_Reset (SDMMCHOST_TYPE *base)
 reset host controller. More...
 
void SDMMCHOST_ErrorRecovery (SDMMCHOST_TYPE *base)
 host controller error recovery. More...
 
void SDMMCHOST_Deinit (void *host)
 Deinit host controller. More...
 
void SDMMCHOST_PowerOffCard (SDMMCHOST_TYPE *base, const sdmmchost_pwr_card_t *pwr)
 host power off card function. More...
 
void SDMMCHOST_PowerOnCard (SDMMCHOST_TYPE *base, const sdmmchost_pwr_card_t *pwr)
 host power on card function. More...
 
void SDMMCHOST_Delay (uint32_t milliseconds)
 SDMMC host delay function. More...
 

Data Structure Documentation

struct sdmmchost_detect_card_t

Data Fields

sdmmchost_detect_card_type_t cdType
 card detect type
 
uint32_t cdTimeOut_ms
 
                  card detect timeout which allow 0 - 0xFFFFFFF, value 0 will return immediately, value

0xFFFFFFFF will block until card is insert

 
sdmmchost_cd_callback_t cardInserted
 card inserted callback which is meaningful for interrupt case
 
sdmmchost_cd_callback_t cardRemoved
 card removed callback which is meaningful for interrupt case
 
void * userData
 user data
 
struct sdmmchost_pwr_card_t

Data Fields

sdmmchost_pwr_t powerOn
 power on function pointer
 
uint32_t powerOnDelay_ms
 power on delay
 
sdmmchost_pwr_t powerOff
 power off function pointer
 
uint32_t powerOffDelay_ms
 power off delay
 

Enumeration Type Documentation

Enumerator
kSDMMCHOST_EndianModeBig 

Big endian mode.

kSDMMCHOST_EndianModeHalfWordBig 

Half word big endian mode.

kSDMMCHOST_EndianModeLittle 

Little endian mode.

Enumerator
kSDMMCHOST_DetectCardByGpioCD 

sd card detect by CD pin through GPIO

kSDMMCHOST_DetectCardByHostCD 

sd card detect by CD pin through host

kSDMMCHOST_DetectCardByHostDATA3 

sd card detect by DAT3 pin through host

Function Documentation

static status_t SDMMCHOST_NotSupport ( void *  parameter)
inlinestatic
Parameters
voidparameter ,used to avoid build warning
Return values
kStatus_Fail,hostdo not suppport
status_t SDMMCHOST_WaitCardDetectStatus ( SDMMCHOST_TYPE *  hostBase,
const sdmmchost_detect_card_t cd,
bool  waitCardStatus 
)
Parameters
basethe pointer to host base address
cdcard detect configuration
waitCardStatusstatus which user want to wait
Return values
kStatus_Successdetect card insert
kStatus_Failcard insert event fail
bool SDMMCHOST_IsCardPresent ( void  )
Return values
truecard is present
falsecard is not present
status_t SDMMCHOST_Init ( SDMMCHOST_CONFIG *  host,
void *  userData 
)
Parameters
hostthe pointer to host structure in card structure.
userDataspecific user data
Return values
kStatus_Successhost init success
kStatus_Failevent fail
void SDMMCHOST_Reset ( SDMMCHOST_TYPE *  base)
Parameters
hostbase address.
void SDMMCHOST_ErrorRecovery ( SDMMCHOST_TYPE *  base)
Parameters
hostbase address.
void SDMMCHOST_Deinit ( void *  host)
Parameters
hostthe pointer to host structure in card structure.
void SDMMCHOST_PowerOffCard ( SDMMCHOST_TYPE *  base,
const sdmmchost_pwr_card_t pwr 
)
Parameters
basehost base address.
pwrdepend on user define power configuration.
void SDMMCHOST_PowerOnCard ( SDMMCHOST_TYPE *  base,
const sdmmchost_pwr_card_t pwr 
)
Parameters
basehost base address.
pwrdepend on user define power configuration.
void SDMMCHOST_Delay ( uint32_t  milliseconds)
Parameters
millisecondsdelay counter.