The SDCARD driver provide card initialization/read/write/erase interface.
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;
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;
uint32_t blockCount;
uint32_t blockSize;
Part of The variables above is user configurable,
- SDMMCHOST_CONFIG host Application need to provide host controller base address and the host's source clock frequency.
- 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.
- 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.
- 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.
- sd_driver_strength_t driverStrength Choose a valid card driver strength if application required and call SD_SetDriverStrength in application.
- 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
~~~~~{.c} /* 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 = card->usrParam.pwr = /* intial the host controller */ SD_HostInit(card); /*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*/ SD_CardInit(card);
/* 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); */
/*!
|
status_t | SD_Init (sd_card_t *card) |
| Initializes the card on a specific host controller. More...
|
|
void | SD_Deinit (sd_card_t *card) |
| Deinitializes the card. More...
|
|
status_t | SD_CardInit (sd_card_t *card) |
| Initializes the card. More...
|
|
void | SD_CardDeinit (sd_card_t *card) |
| Deinitializes the card. More...
|
|
status_t | SD_HostInit (sd_card_t *card) |
| initialize the host. More...
|
|
void | SD_HostDeinit (sd_card_t *card) |
| Deinitializes the host. More...
|
|
void | SD_HostReset (SDMMCHOST_CONFIG *host) |
| reset the host. More...
|
|
void | SD_PowerOnCard (SDMMCHOST_TYPE *base, const sdmmchost_pwr_card_t *pwr) |
| power on card. More...
|
|
void | SD_PowerOffCard (SDMMCHOST_TYPE *base, const sdmmchost_pwr_card_t *pwr) |
| power off card. More...
|
|
status_t | SD_WaitCardDetectStatus (SDMMCHOST_TYPE *hostBase, const sdmmchost_detect_card_t *cd, bool waitCardStatus) |
| sd wait card detect function. More...
|
|
bool | SD_IsCardPresent (sd_card_t *card) |
| sd card present check function. More...
|
|
bool | SD_CheckReadOnly (sd_card_t *card) |
| Checks whether the card is write-protected. More...
|
|
status_t | SD_SelectCard (sd_card_t *card, bool isSelected) |
| Send SELECT_CARD command to set the card to be transfer state or not. More...
|
|
status_t | SD_ReadStatus (sd_card_t *card) |
| Send ACMD13 to get the card current status. More...
|
|
status_t | SD_ReadBlocks (sd_card_t *card, uint8_t *buffer, uint32_t startBlock, uint32_t blockCount) |
| Reads blocks from the specific card. More...
|
|
status_t | SD_WriteBlocks (sd_card_t *card, const uint8_t *buffer, uint32_t startBlock, uint32_t blockCount) |
| Writes blocks of data to the specific card. More...
|
|
status_t | SD_EraseBlocks (sd_card_t *card, uint32_t startBlock, uint32_t blockCount) |
| Erases blocks of the specific card. More...
|
|
status_t | SD_SetDriverStrength (sd_card_t *card, sd_driver_strength_t driverStrength) |
| select card driver strength select card driver strength More...
|
|
status_t | SD_SetMaxCurrent (sd_card_t *card, sd_max_current_t maxCurrent) |
| select max current select max operation current More...
|
|
Define the card structure including the necessary fields to identify and describe the card.
bool sd_card_t::noInteralAlign |
If disable, sdmmc will not make sure the data buffer address is word align, otherwise all the transfer are align to low level driver
Enumerator |
---|
kSD_SupportHighCapacityFlag |
Support high capacity.
|
kSD_Support4BitWidthFlag |
Support 4-bit data width.
|
kSD_SupportSdhcFlag |
Card is SDHC.
|
kSD_SupportSdxcFlag |
Card is SDXC.
|
kSD_SupportVoltage180v |
card support 1.8v voltage
|
kSD_SupportSetBlockCountCmd |
card support cmd23 flag
|
kSD_SupportSpeedClassControlCmd |
card support speed class control flag
|
This function initializes the card on a specific host controller, it is consist of host init, card detect, card init function, however user can ignore this high level function, instead of use the low level function, such as SD_CardInit, SD_HostInit, SD_CardDetect.
- Parameters
-
- Return values
-
kStatus_SDMMC_HostNotReady | host is not ready. |
kStatus_SDMMC_GoIdleFailed | Go idle failed. |
kStatus_SDMMC_NotSupportYet | Card not support. |
kStatus_SDMMC_SendOperationConditionFailed | Send operation condition failed. |
kStatus_SDMMC_AllSendCidFailed | Send CID failed. |
kStatus_SDMMC_SendRelativeAddressFailed | Send relative address failed. |
kStatus_SDMMC_SendCsdFailed | Send CSD failed. |
kStatus_SDMMC_SelectCardFailed | Send SELECT_CARD command failed. |
kStatus_SDMMC_SendScrFailed | Send SCR failed. |
kStatus_SDMMC_SetBusWidthFailed | Set bus width failed. |
kStatus_SDMMC_SwitchHighSpeedFailed | Switch high speed failed. |
kStatus_SDMMC_SetCardBlockSizeFailed | Set card block size failed. |
kStatus_Success | Operate successfully. |
This function initializes the card only, make sure the host is ready when call this function, otherwise it will return kStatus_SDMMC_HostNotReady.
- Parameters
-
- Return values
-
kStatus_SDMMC_HostNotReady | host is not ready. |
kStatus_SDMMC_GoIdleFailed | Go idle failed. |
kStatus_SDMMC_NotSupportYet | Card not support. |
kStatus_SDMMC_SendOperationConditionFailed | Send operation condition failed. |
kStatus_SDMMC_AllSendCidFailed | Send CID failed. |
kStatus_SDMMC_SendRelativeAddressFailed | Send relative address failed. |
kStatus_SDMMC_SendCsdFailed | Send CSD failed. |
kStatus_SDMMC_SelectCardFailed | Send SELECT_CARD command failed. |
kStatus_SDMMC_SendScrFailed | Send SCR failed. |
kStatus_SDMMC_SetBusWidthFailed | Set bus width failed. |
kStatus_SDMMC_SwitchHighSpeedFailed | Switch high speed failed. |
kStatus_SDMMC_SetCardBlockSizeFailed | Set card block size failed. |
kStatus_Success | Operate successfully. |
This function deinitializes the specific card.
- Parameters
-
This function deinitializes the specific host.
- Parameters
-
This function deinitializes the host.
- Parameters
-
void SD_HostReset |
( |
SDMMCHOST_CONFIG * |
host | ) |
|
This function reset the specific host.
- Parameters
-
The power on operation depend on host or the user define power on function.
- Parameters
-
base | host base address. |
pwr | user define power control configuration |
The power off operation depend on host or the user define power on function.
- Parameters
-
base | host base address. |
pwr | user define power control configuration |
Detect card through GPIO, CD, DATA3.
- Parameters
-
card | card descriptor. |
card | detect configuration |
waitCardStatus | wait card detect status |
This function checks if the card is write-protected via the CSD register.
- Parameters
-
- Return values
-
true | Card is read only. |
false | Card isn't read only. |
- Parameters
-
card | Card descriptor. |
isSelected | True to set the card into transfer state, false to disselect. |
- Return values
-
kStatus_SDMMC_TransferFailed | Transfer failed. |
kStatus_Success | Operate successfully. |
- Parameters
-
- Return values
-
kStatus_SDMMC_TransferFailed | Transfer failed. |
kStatus_SDMMC_SendApplicationCommandFailed | send application command failed. |
kStatus_Success | Operate successfully. |
status_t SD_ReadBlocks |
( |
sd_card_t * |
card, |
|
|
uint8_t * |
buffer, |
|
|
uint32_t |
startBlock, |
|
|
uint32_t |
blockCount |
|
) |
| |
This function reads blocks from the specific card with default block size defined by the SDHC_CARD_DEFAULT_BLOCK_SIZE.
- Parameters
-
card | Card descriptor. |
buffer | The buffer to save the data read from card. |
startBlock | The start block index. |
blockCount | The number of blocks to read. |
- Return values
-
kStatus_InvalidArgument | Invalid argument. |
kStatus_SDMMC_CardNotSupport | Card not support. |
kStatus_SDMMC_NotSupportYet | Not support now. |
kStatus_SDMMC_WaitWriteCompleteFailed | Send status failed. |
kStatus_SDMMC_TransferFailed | Transfer failed. |
kStatus_SDMMC_StopTransmissionFailed | Stop transmission failed. |
kStatus_Success | Operate successfully. |
status_t SD_WriteBlocks |
( |
sd_card_t * |
card, |
|
|
const uint8_t * |
buffer, |
|
|
uint32_t |
startBlock, |
|
|
uint32_t |
blockCount |
|
) |
| |
This function writes blocks to the specific card with default block size 512 bytes.
- Parameters
-
card | Card descriptor. |
buffer | The buffer holding the data to be written to the card. |
startBlock | The start block index. |
blockCount | The number of blocks to write. |
- Return values
-
kStatus_InvalidArgument | Invalid argument. |
kStatus_SDMMC_NotSupportYet | Not support now. |
kStatus_SDMMC_CardNotSupport | Card not support. |
kStatus_SDMMC_WaitWriteCompleteFailed | Send status failed. |
kStatus_SDMMC_TransferFailed | Transfer failed. |
kStatus_SDMMC_StopTransmissionFailed | Stop transmission failed. |
kStatus_Success | Operate successfully. |
status_t SD_EraseBlocks |
( |
sd_card_t * |
card, |
|
|
uint32_t |
startBlock, |
|
|
uint32_t |
blockCount |
|
) |
| |
This function erases blocks of the specific card with default block size 512 bytes.
- Parameters
-
card | Card descriptor. |
startBlock | The start block index. |
blockCount | The number of blocks to erase. |
- Return values
-
kStatus_InvalidArgument | Invalid argument. |
kStatus_SDMMC_WaitWriteCompleteFailed | Send status failed. |
kStatus_SDMMC_TransferFailed | Transfer failed. |
kStatus_SDMMC_WaitWriteCompleteFailed | Send status failed. |
kStatus_Success | Operate successfully. |
- Parameters
-
card | Card descriptor. |
driverStrength | Driver strength |
- Parameters
-
card | Card descriptor. |
maxCurrent | Max current |