All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SPI based Secured Digital Card (SDSPI)

## Overview

The KSDK provides a driver to access the Secured 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))
{
SDSPI_Deinit(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 Structures

struct | sdspi_command_t
| SDSPI command. More...
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
}

| 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_t { kSDSPI_ResponseTypeR1 = 0U, kSDSPI_ResponseTypeR1b = 1U, kSDSPI_ResponseTypeR2 = 2U, kSDSPI_ResponseTypeR3 = 3U, kSDSPI_ResponseTypeR7 = 4U }
| SDSPI response type. More...

## SDSPI Function

status_t | SDSPI_Init (sdspi_card_t *card)
| Initialize the card on a specific SPI instance. More...
void | SDSPI_Deinit (sdspi_card_t *card)
| Deinitialize the card. More...
bool | SDSPI_CheckReadOnly (sdspi_card_t *card)
| Check whether the card is write-protected. More...
status_t | SDSPI_ReadBlocks (sdspi_card_t *card, uint8_t *buffer, uint32_t startBlock, uint32_t blockCount)
| Read blocks from the specific card. More...
status_t | SDSPI_WriteBlocks (sdspi_card_t *card, uint8_t *buffer, uint32_t startBlock, uint32_t blockCount)
| Write blocks of data to the specific card. More...

## Data Structure Documentation

struct sdspi_command_t

### Data Fields

uint8_t | index
| Command index.
uint32_t | argument
| Command argument.
uint8_t | responseType
| Response type.
uint8_t | response [5U]
| Response content.

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.
uint32_t(* | getCurrentMilliseconds )(void)
| Get current time in milliseconds.

struct sdspi_card_t

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

### Data Fields

sdspi_host_t * | host
| 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

enum _sdspi_status

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.

enum _sdspi_card_flag

Enumerator
kSDSPI_SupportHighCapacityFlag | Card is high capacity.
kSDSPI_SupportSdhcFlag | Card is SDHC.
kSDSPI_SupportSdxcFlag | Card is SDXC.
kSDSPI_SupportSdscFlag | Card is SDSC.

enum sdspi_response_type_t

Enumerator
kSDSPI_ResponseTypeR1 | Response 1.
kSDSPI_ResponseTypeR1b | Response 1 with busy.
kSDSPI_ResponseTypeR2 | Response 2.
kSDSPI_ResponseTypeR3 | Response 3.
kSDSPI_ResponseTypeR7 | Response 7.

## Function Documentation

status_t SDSPI_Init | ( | sdspi_card_t * | card | ) |

This function initializes the card on a specific SPI instance.

Parameters

  card | Card descriptor

Return values

  kStatus_SDSPI_SetFrequencyFailed | Set frequency failed.
  kStatus_SDSPI_GoIdleFailed | Go idle failed.
  kStatus_SDSPI_SendInterfaceConditionFailed | Send interface condition failed.
  kStatus_SDSPI_SendOperationConditionFailed | Send operation condition failed.
  kStatus_Timeout | Send command timeout.
  kStatus_SDSPI_NotSupportYet | Not support yet.
  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_Success | Operate successfully.

void SDSPI_Deinit | ( | sdspi_card_t * | card | ) |

This function deinitializes the specific card.

Parameters

  card | Card descriptor

bool SDSPI_CheckReadOnly | ( | sdspi_card_t * | card | ) |

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

Parameters

  card | Card descriptor.

Return values

  true | Card is read only.
  false | Card 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

  card | Card descriptor.
  buffer | the buffer to hold the data read from card
  startBlock | the start block index
  blockCount | the number of blocks to read

Return values

  kStatus_SDSPI_SendCommandFailed | Send command failed.
  kStatus_SDSPI_ReadFailed | Read data failed.
  kStatus_SDSPI_StopTransmissionFailed | Stop transmission failed.
  kStatus_Success | Operate 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

  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_SDSPI_WriteProtected | Card is write protected.
  kStatus_SDSPI_SendCommandFailed | Send command failed.
  kStatus_SDSPI_ResponseError | Response is error.
  kStatus_SDSPI_WriteFailed | Write data failed.
  kStatus_SDSPI_ExchangeFailed | Exchange data over SPI failed.
  kStatus_SDSPI_WaitReadyFailed | Wait card to be ready status failed.
  kStatus_Success | Operate successfully.