MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SPI based Secure Digital Card (SDSPI)

Data Structures

struct  sdspi_host_t
 SDSPI host state. More...
 
struct  sdspi_card_t
 SD Card Structure. More...
 

Enumerations

enum  _sdspi_status {
  kStatus_SDSPI_SetFrequencyFailed = MAKE_STATUS(kStatusGroup_SDSPI, 0U),
  kStatus_SDSPI_ExchangeFailed = MAKE_STATUS(kStatusGroup_SDSPI, 1U),
  kStatus_SDSPI_WaitReadyFailed = MAKE_STATUS(kStatusGroup_SDSPI, 2U),
  kStatus_SDSPI_ResponseError = MAKE_STATUS(kStatusGroup_SDSPI, 3U),
  kStatus_SDSPI_WriteProtected = MAKE_STATUS(kStatusGroup_SDSPI, 4U),
  kStatus_SDSPI_GoIdleFailed = MAKE_STATUS(kStatusGroup_SDSPI, 5U),
  kStatus_SDSPI_SendCommandFailed = MAKE_STATUS(kStatusGroup_SDSPI, 6U),
  kStatus_SDSPI_ReadFailed = MAKE_STATUS(kStatusGroup_SDSPI, 7U),
  kStatus_SDSPI_WriteFailed = MAKE_STATUS(kStatusGroup_SDSPI, 8U),
  kStatus_SDSPI_SendInterfaceConditionFailed,
  kStatus_SDSPI_SendOperationConditionFailed,
  kStatus_SDSPI_ReadOcrFailed = MAKE_STATUS(kStatusGroup_SDSPI, 11U),
  kStatus_SDSPI_SetBlockSizeFailed = MAKE_STATUS(kStatusGroup_SDSPI, 12U),
  kStatus_SDSPI_SendCsdFailed = MAKE_STATUS(kStatusGroup_SDSPI, 13U),
  kStatus_SDSPI_SendCidFailed = MAKE_STATUS(kStatusGroup_SDSPI, 14U),
  kStatus_SDSPI_StopTransmissionFailed = MAKE_STATUS(kStatusGroup_SDSPI, 15U),
  kStatus_SDSPI_SendApplicationCommandFailed,
  kStatus_SDSPI_InvalidVoltage = MAKE_STATUS(kStatusGroup_SDSPI, 17U),
  kStatus_SDSPI_SwitchCmdFail = MAKE_STATUS(kStatusGroup_SDSPI, 18U),
  kStatus_SDSPI_NotSupportYet = MAKE_STATUS(kStatusGroup_SDSPI, 19U)
}
 SDSPI API status. More...
 
enum  _sdspi_card_flag {
  kSDSPI_SupportHighCapacityFlag = (1U << 0U),
  kSDSPI_SupportSdhcFlag = (1U << 1U),
  kSDSPI_SupportSdxcFlag = (1U << 2U),
  kSDSPI_SupportSdscFlag = (1U << 3U)
}
 SDSPI card flag. More...
 
enum  _sdspi_response_type {
  kSDSPI_ResponseTypeR1 = 0U,
  kSDSPI_ResponseTypeR1b = 1U,
  kSDSPI_ResponseTypeR2 = 2U,
  kSDSPI_ResponseTypeR3 = 3U,
  kSDSPI_ResponseTypeR7 = 4U
}
 SDSPI response type. More...
 
enum  _sdspi_cmd {
  kSDSPI_CmdGoIdle = kSDMMC_GoIdleState << 8U | kSDSPI_ResponseTypeR1,
  kSDSPI_CmdCrc = kSDSPI_CommandCrc << 8U | kSDSPI_ResponseTypeR1,
  kSDSPI_CmdSendInterfaceCondition
}
 SDSPI command type. More...
 

SDSPI Function

status_t SDSPI_Init (sdspi_card_t *card)
 Initializes the card on a specific SPI instance. More...
 
void SDSPI_Deinit (sdspi_card_t *card)
 Deinitializes the card. More...
 
bool SDSPI_CheckReadOnly (sdspi_card_t *card)
 Checks whether the card is write-protected. More...
 
status_t SDSPI_ReadBlocks (sdspi_card_t *card, uint8_t *buffer, uint32_t startBlock, uint32_t blockCount)
 Reads blocks from the specific card. More...
 
status_t SDSPI_WriteBlocks (sdspi_card_t *card, uint8_t *buffer, uint32_t startBlock, uint32_t blockCount)
 Writes blocks of data to the specific card. More...
 
status_t SDSPI_SendCid (sdspi_card_t *card)
 Send GET-CID command In our sdspi init function, this function is removed for better code size, if id information is needed, you can call it after the init function directly. More...
 
status_t SDSPI_SendPreErase (sdspi_card_t *card, uint32_t blockCount)
 Multiple blocks write pre-erase function. More...
 
status_t SDSPI_EraseBlocks (sdspi_card_t *card, uint32_t startBlock, uint32_t blockCount)
 Block erase function. More...
 
status_t SDSPI_SwitchToHighSpeed (sdspi_card_t *card)
 Switch to high speed function. More...
 

Detailed Description

The MCUXpresso SDK provides a driver to access the Secure Digital Card based on the SPI driver.

Function groups

This function group implements the SD card functional API in the SPI mode.

Typical use case

/* SPI_Init(). */
/* Register the SDSPI driver callback. */
/* Initializes card. */
if (kStatus_Success != SDSPI_Init(card))
{
return;
}
/* Read/Write card */
memset(g_testWriteBuffer, 0x17U, sizeof(g_testWriteBuffer));
while (true)
{
memset(g_testReadBuffer, 0U, sizeof(g_testReadBuffer));
SDSPI_WriteBlocks(card, g_testWriteBuffer, TEST_START_BLOCK, TEST_BLOCK_COUNT);
SDSPI_ReadBlocks(card, g_testReadBuffer, TEST_START_BLOCK, TEST_BLOCK_COUNT);
if (memcmp(g_testReadBuffer, g_testReadBuffer, sizeof(g_testWriteBuffer)))
{
break;
}
}

Data Structure Documentation

struct sdspi_host_t

Data Fields

uint32_t busBaudRate
 Bus baud rate.
 
status_t(* setFrequency )(uint32_t frequency)
 Set frequency of SPI.
 
status_t(* exchange )(uint8_t *in, uint8_t *out, uint32_t size)
 Exchange data over SPI.
 
struct sdspi_card_t

Define the card structure including the necessary fields to identify and describe the card.

Data Fields

sdspi_host_thost
 Host state information.
 
uint32_t relativeAddress
 Relative address of the card.
 
uint32_t flags
 Flags defined in _sdspi_card_flag. More...
 
uint8_t rawCid [16U]
 Raw CID content.
 
uint8_t rawCsd [16U]
 Raw CSD content.
 
uint8_t rawScr [8U]
 Raw SCR content.
 
uint32_t ocr
 Raw OCR content.
 
sd_cid_t cid
 CID.
 
sd_csd_t csd
 CSD.
 
sd_scr_t scr
 SCR.
 
uint32_t blockCount
 Card total block number.
 
uint32_t blockSize
 Card block size.
 

Field Documentation

uint32_t sdspi_card_t::flags

Enumeration Type Documentation

Enumerator
kStatus_SDSPI_SetFrequencyFailed 

Set frequency failed.

kStatus_SDSPI_ExchangeFailed 

Exchange data on SPI bus failed.

kStatus_SDSPI_WaitReadyFailed 

Wait card ready failed.

kStatus_SDSPI_ResponseError 

Response is error.

kStatus_SDSPI_WriteProtected 

Write protected.

kStatus_SDSPI_GoIdleFailed 

Go idle failed.

kStatus_SDSPI_SendCommandFailed 

Send command failed.

kStatus_SDSPI_ReadFailed 

Read data failed.

kStatus_SDSPI_WriteFailed 

Write data failed.

kStatus_SDSPI_SendInterfaceConditionFailed 

Send interface condition failed.

kStatus_SDSPI_SendOperationConditionFailed 

Send operation condition failed.

kStatus_SDSPI_ReadOcrFailed 

Read OCR failed.

kStatus_SDSPI_SetBlockSizeFailed 

Set block size failed.

kStatus_SDSPI_SendCsdFailed 

Send CSD failed.

kStatus_SDSPI_SendCidFailed 

Send CID failed.

kStatus_SDSPI_StopTransmissionFailed 

Stop transmission failed.

kStatus_SDSPI_SendApplicationCommandFailed 

Send application command failed.

kStatus_SDSPI_InvalidVoltage 

invaild supply voltage

kStatus_SDSPI_SwitchCmdFail 

switch command crc protection on/off

kStatus_SDSPI_NotSupportYet 

not support

Enumerator
kSDSPI_SupportHighCapacityFlag 

Card is high capacity.

kSDSPI_SupportSdhcFlag 

Card is SDHC.

kSDSPI_SupportSdxcFlag 

Card is SDXC.

kSDSPI_SupportSdscFlag 

Card is SDSC.

Enumerator
kSDSPI_ResponseTypeR1 

Response 1.

kSDSPI_ResponseTypeR1b 

Response 1 with busy.

kSDSPI_ResponseTypeR2 

Response 2.

kSDSPI_ResponseTypeR3 

Response 3.

kSDSPI_ResponseTypeR7 

Response 7.

enum _sdspi_cmd
Enumerator
kSDSPI_CmdGoIdle 

command go idle

kSDSPI_CmdCrc 

command crc protection

kSDSPI_CmdSendInterfaceCondition 

command send interface condition

Function Documentation

status_t SDSPI_Init ( sdspi_card_t card)

This function initializes the card on a specific SPI instance.

Parameters
cardCard descriptor
Return values
kStatus_SDSPI_SetFrequencyFailedSet frequency failed.
kStatus_SDSPI_GoIdleFailedGo idle failed.
kStatus_SDSPI_SendInterfaceConditionFailedSend interface condition failed.
kStatus_SDSPI_SendOperationConditionFailedSend operation condition failed.
kStatus_TimeoutSend command timeout.
kStatus_SDSPI_NotSupportYetNot support yet.
kStatus_SDSPI_ReadOcrFailedRead OCR failed.
kStatus_SDSPI_SetBlockSizeFailedSet block size failed.
kStatus_SDSPI_SendCsdFailedSend CSD failed.
kStatus_SDSPI_SendCidFailedSend CID failed.
kStatus_SuccessOperate successfully.
void SDSPI_Deinit ( sdspi_card_t card)

This function deinitializes the specific card.

Parameters
cardCard descriptor
bool SDSPI_CheckReadOnly ( sdspi_card_t card)

This function checks if the card is write-protected via CSD register.

Parameters
cardCard descriptor.
Return values
trueCard is read only.
falseCard isn't read only.
status_t SDSPI_ReadBlocks ( sdspi_card_t card,
uint8_t *  buffer,
uint32_t  startBlock,
uint32_t  blockCount 
)

This function reads blocks from specific card.

Parameters
cardCard descriptor.
bufferthe buffer to hold the data read from card
startBlockthe start block index
blockCountthe number of blocks to read
Return values
kStatus_SDSPI_SendCommandFailedSend command failed.
kStatus_SDSPI_ReadFailedRead data failed.
kStatus_SDSPI_StopTransmissionFailedStop transmission failed.
kStatus_SuccessOperate successfully.
status_t SDSPI_WriteBlocks ( sdspi_card_t card,
uint8_t *  buffer,
uint32_t  startBlock,
uint32_t  blockCount 
)

This function writes blocks to specific card

Parameters
cardCard descriptor.
bufferthe buffer holding the data to be written to the card
startBlockthe start block index
blockCountthe number of blocks to write
Return values
kStatus_SDSPI_WriteProtectedCard is write protected.
kStatus_SDSPI_SendCommandFailedSend command failed.
kStatus_SDSPI_ResponseErrorResponse is error.
kStatus_SDSPI_WriteFailedWrite data failed.
kStatus_SDSPI_ExchangeFailedExchange data over SPI failed.
kStatus_SDSPI_WaitReadyFailedWait card to be ready status failed.
kStatus_SuccessOperate successfully.
status_t SDSPI_SendCid ( sdspi_card_t card)
Parameters
cardCard descriptor.
Return values
kStatus_SDSPI_SendCommandFailedSend command failed.
kStatus_SDSPI_ReadFailedRead data blocks failed.
kStatus_SuccessOperate successfully.
status_t SDSPI_SendPreErase ( sdspi_card_t card,
uint32_t  blockCount 
)

This function should be called before SDSPI_WriteBlocks, it is used to set the number of the write blocks to be pre-erased before writing.

Parameters
cardCard descriptor.
blockCountthe block counts to be write.
Return values
kStatus_SDSPI_SendCommandFailedSend command failed.
kStatus_SDSPI_SendApplicationCommandFailed
kStatus_SDSPI_ResponseError
kStatus_SuccessOperate successfully.
status_t SDSPI_EraseBlocks ( sdspi_card_t card,
uint32_t  startBlock,
uint32_t  blockCount 
)
Parameters
cardCard descriptor.
startBlockstart block address to be erase.
blockCountthe block counts to be erase.
Return values
kStatus_SDSPI_WaitReadyFailedWait ready failed.
kStatus_SDSPI_SendCommandFailedSend command failed.
kStatus_SuccessOperate successfully.
status_t SDSPI_SwitchToHighSpeed ( sdspi_card_t card)

This function can be called after SDSPI_Init function if target board's layout support >25MHZ spi baudrate, otherwise this function is useless.Be careful with call this function, code size and stack usage will be enlarge.

Parameters
cardCard descriptor.
Return values
kStatus_Failswitch failed.
kStatus_SuccessOperate successfully.