Hardware Abstraction Layer for FreeRTOS
SD Card Controller Interface
Collaboration diagram for SD Card Controller Interface:

Data Structures

struct  sd_generic
 
struct  sd_response
 
struct  sd_setting
 

Macros

#define SD_ERROR_AKE_SEQ_ERROR   -2
 
#define SD_ERROR_ERROR   -3
 
#define SD_ERROR_CC_ERROR   -4
 
#define SD_ERROR_CARD_ECC_FAIELD   -5
 
#define SD_ERROR_ILLEGAL_COMMAND   -6
 
#define SD_ERROR_COM_CRC_ERROR   -7
 
#define SD_ERROR_LOCK_UNLOCK_FAILED   -8
 
#define SD_ERROR_CARD_IS_LOCKED   -9
 
#define SD_ERROR_WP_VIOLATION   -10
 
#define SD_ERROR_ERASE_PARAM   -11
 
#define SD_ERROR_ERASE_SEQ_ERROR   -12
 
#define SD_ERROR_ERASE_LEN_ERROR   -13
 
#define SD_ERROR_ADDRESS_ERROR   -14
 
#define SD_ERROR_OUT_OF_RANGE   -15
 
#define SD_ERROR_FUNCTION_NUMBER   -16
 
#define CMD(x)   (x)
 
#define ACMD(x)   (x)
 

Enumerations

enum  sd_mode { SD_SPI, SD_SD, SD_SDIO }
 
enum  sd_block_size {
  SD_BLOCK_SIZE_1B, SD_BLOCK_SIZE_2B, SD_BLOCK_SIZE_4B, SD_BLOCK_SIZE_8B,
  SD_BLOCK_SIZE_16B, SD_BLOCK_SIZE_32B, SD_BLOCK_SIZE_64B, SD_BLOCK_SIZE_128B,
  SD_BLOCK_SIZE_256B, SD_BLOCK_SIZE_512B, SD_BLOCK_SIZE_1024B, SD_BLOCK_SIZE_2048B
}
 
enum  sd_bus_wide { SD_BusWide_1b, SD_BusWide_4b, SD_BusWide_8b }
 

Functions

struct sd * sd_init (uint32_t index, struct sd_setting *settings)
 
int32_t sd_deinit (struct sd *sd)
 
int32_t sd_setBlockSize (struct sd *sd, enum sd_block_size blockSize)
 
int32_t sd_setBusWide (struct sd *sd, enum sd_bus_wide busWide)
 
int32_t sd_setClock (struct sd *sd, uint64_t clock)
 
int32_t sd_sendCommand (struct sd *sd, uint32_t command, uint32_t argument, struct sd_response *response, TickType_t waittime)
 
int32_t sd_write (struct sd *sd, uint32_t command, uint32_t argument, size_t size, uint32_t *data, TickType_t waittime)
 
int32_t sd_read (struct sd *sd, uint32_t command, uint32_t argument, size_t size, uint32_t *data, TickType_t waittime)
 
int32_t sd_sendCommandISR (struct sd *sd, uint32_t command, uint32_t argument, struct sd_response *response)
 
int32_t sd_writeISR (struct sd *sd, uint32_t command, uint32_t argument, size_t size, uint32_t *data)
 
int32_t sd_readISR (struct sd *sd, uint32_t command, uint32_t argument, size_t size, uint32_t *data)
 

Detailed Description

#include <sd.h>

Macro Definition Documentation

◆ ACMD

#define ACMD (   x)    (x)

ACMD CMD(55) shall send before!

Parameters
xCMD ID
Returns
ACMD

◆ CMD

#define CMD (   x)    (x)

Define a CMD

Parameters
xCMD ID
Returns
command

◆ SD_ERROR_ADDRESS_ERROR

#define SD_ERROR_ADDRESS_ERROR   -14

A misaligned address which did not match the block length was used in the command.

◆ SD_ERROR_AKE_SEQ_ERROR

#define SD_ERROR_AKE_SEQ_ERROR   -2

Error in the sequence of the authentication process

◆ SD_ERROR_CARD_ECC_FAIELD

#define SD_ERROR_CARD_ECC_FAIELD   -5

Card internal ECC was applied but failed to correct the data.

◆ SD_ERROR_CARD_IS_LOCKED

#define SD_ERROR_CARD_IS_LOCKED   -9

When set, signals that the card is locked by the host

◆ SD_ERROR_CC_ERROR

#define SD_ERROR_CC_ERROR   -4

Internal card controller error

◆ SD_ERROR_COM_CRC_ERROR

#define SD_ERROR_COM_CRC_ERROR   -7

The CRC check of the previous command failed.

◆ SD_ERROR_ERASE_LEN_ERROR

#define SD_ERROR_ERASE_LEN_ERROR   -13

The transferred block length is not allowed for this card, or the number of transferred bytes does not match the block length.

◆ SD_ERROR_ERASE_PARAM

#define SD_ERROR_ERASE_PARAM   -11

An invalid selection of write-blocks for erase occurred.

◆ SD_ERROR_ERASE_SEQ_ERROR

#define SD_ERROR_ERASE_SEQ_ERROR   -12

An error in the sequence of erase commands occurred.

◆ SD_ERROR_ERROR

#define SD_ERROR_ERROR   -3

A general or an unknown error occurred during the operation.

◆ SD_ERROR_FUNCTION_NUMBER

#define SD_ERROR_FUNCTION_NUMBER   -16

An invalid function number was requested

◆ SD_ERROR_ILLEGAL_COMMAND

#define SD_ERROR_ILLEGAL_COMMAND   -6

Command not legal for the card state

◆ SD_ERROR_LOCK_UNLOCK_FAILED

#define SD_ERROR_LOCK_UNLOCK_FAILED   -8

Set when a sequence or password error has been detected in lock/unlock card command.

◆ SD_ERROR_OUT_OF_RANGE

#define SD_ERROR_OUT_OF_RANGE   -15

The command's argument was out of the allowed range for this card.

◆ SD_ERROR_WP_VIOLATION

#define SD_ERROR_WP_VIOLATION   -10

Set when the host attempts to write to a protected block or to the temporary or permanent writeprotected card.

Enumeration Type Documentation

◆ sd_block_size

Enumerator
SD_BLOCK_SIZE_1B 
SD_BLOCK_SIZE_2B 
SD_BLOCK_SIZE_4B 
SD_BLOCK_SIZE_8B 
SD_BLOCK_SIZE_16B 
SD_BLOCK_SIZE_32B 
SD_BLOCK_SIZE_64B 
SD_BLOCK_SIZE_128B 
SD_BLOCK_SIZE_256B 
SD_BLOCK_SIZE_512B 
SD_BLOCK_SIZE_1024B 
SD_BLOCK_SIZE_2048B 

◆ sd_bus_wide

Enumerator
SD_BusWide_1b 
SD_BusWide_4b 
SD_BusWide_8b 

◆ sd_mode

enum sd_mode

Mode

Enumerator
SD_SPI 

Controller is in SPI Mode

SD_SD 

Controller is in SD Mode

SD_SDIO 

Controller is in SDIO Mode

Function Documentation

◆ sd_deinit()

int32_t sd_deinit ( struct sd *  sd)

Deinit Driver Instants

Parameters
sdInstant
Returns
0 on ok -1 on failure

◆ sd_init()

struct sd* sd_init ( uint32_t  index,
struct sd_setting settings 
)

Init Function

Parameters
indexin sds Array
settingsSettings
Returns
Example Instants or NULL

◆ sd_read()

int32_t sd_read ( struct sd *  sd,
uint32_t  command,
uint32_t  argument,
size_t  size,
uint32_t *  data,
TickType_t  waittime 
)

Read Data to SD

Parameters
sdInstants
commandCommand
argumentCommand Argument without CMD ID and CRC. CRC is Calculated by Driver or Hardware
sizeBuffer Size
dataBuffer Pointer
waittimeWaittime
Returns
< 0 on error 0 on ok -1 == Unkown Error

Is Response == R2, R3, R4 and R7 return is always 0

Is Response == R1 following error can occur (Desciption in Standart s. Card Status)

Is Response == R5 following error can occur (Desciption in Standart s. SDIO R5)

Is Response == R6 following error can occur (Desciption in Standart Response R6)

◆ sd_readISR()

int32_t sd_readISR ( struct sd *  sd,
uint32_t  command,
uint32_t  argument,
size_t  size,
uint32_t *  data 
)

Read Data to SD

Parameters
sdInstants
commandCommand
argumentCommand Argument without CMD ID and CRC. CRC is Calculated by Driver or Hardware
sizeBuffer Size
dataBuffer Pointer
Returns
< 0 on error 0 on ok -1 == Unkown Error

Is Response == R2, R3, R4 and R7 return is always 0

Is Response == R1 following error can occur (Desciption in Standart s. Card Status)

Is Response == R5 following error can occur (Desciption in Standart s. SDIO R5)

Is Response == R6 following error can occur (Desciption in Standart Response R6)

◆ sd_sendCommand()

int32_t sd_sendCommand ( struct sd *  sd,
uint32_t  command,
uint32_t  argument,
struct sd_response response,
TickType_t  waittime 
)

Send Command

Parameters
sdInstants
commandCommand
argumentCommand Argument without CMD ID and CRC. CRC is Calculated by Driver or Hardware
responseCard Response NULL on not needed
waittimeWaittime
Returns
< 0 on error 0 on ok -1 == Unkown Error

Is Response == R2, R3, R4 and R7 return is always 0

Is Response == R1 following error can occur (Desciption in Standart s. Card Status)

Is Response == R5 following error can occur (Desciption in Standart s. SDIO R5)

Is Response == R6 following error can occur (Desciption in Standart Response R6)

◆ sd_sendCommandISR()

int32_t sd_sendCommandISR ( struct sd *  sd,
uint32_t  command,
uint32_t  argument,
struct sd_response response 
)

Send Command

Parameters
sdInstants
commandCommand
argumentCommand Argument without CMD ID and CRC. CRC is Calculated by Driver or Hardware
responseCard Response
Returns
< 0 on error 0 on ok -1 == Unkown Error

Is Response == R2, R3, R4 and R7 return is always 0

Is Response == R1 following error can occur (Desciption in Standart s. Card Status)

Is Response == R5 following error can occur (Desciption in Standart s. SDIO R5)

Is Response == R6 following error can occur (Desciption in Standart Response R6)

◆ sd_setBlockSize()

int32_t sd_setBlockSize ( struct sd *  sd,
enum sd_block_size  blockSize 
)

Set Block Size

Parameters
sdInstants
blockSizeBlock Size
Returns
0 on ok -1 on failure

◆ sd_setBusWide()

int32_t sd_setBusWide ( struct sd *  sd,
enum sd_bus_wide  busWide 
)

Set Bus Wide

Parameters
sdInstants
busWideBus Wide
Returns
0 on ok -1 on failure

◆ sd_setClock()

int32_t sd_setClock ( struct sd *  sd,
uint64_t  clock 
)

Set Clock

Parameters
sdInstants
clockClock Speed in Hz
Returns
0 on ok -1 on failure

◆ sd_write()

int32_t sd_write ( struct sd *  sd,
uint32_t  command,
uint32_t  argument,
size_t  size,
uint32_t *  data,
TickType_t  waittime 
)

Wirte Data to SD

Parameters
sdInstants
commandCommand
argumentCommand Argument without CMD ID and CRC. CRC is Calculated by Driver or Hardware
sizeBuffer Size
dataBuffer Pointer
waittimeWaittime
Returns
< 0 on error 0 on ok -1 == Unkown Error

Is Response == R2, R3, R4 and R7 return is always 0

Is Response == R1 following error can occur (Desciption in Standart s. Card Status)

Is Response == R5 following error can occur (Desciption in Standart s. SDIO R5)

Is Response == R6 following error can occur (Desciption in Standart Response R6)

◆ sd_writeISR()

int32_t sd_writeISR ( struct sd *  sd,
uint32_t  command,
uint32_t  argument,
size_t  size,
uint32_t *  data 
)

Wirte Data to SD

Parameters
sdInstants
commandCommand
argumentCommand Argument without CMD ID and CRC. CRC is Calculated by Driver or Hardware
sizeBuffer Size
dataBuffer Pointer
Returns
< 0 on error 0 on ok -1 == Unkown Error

Is Response == R2, R3, R4 and R7 return is always 0

Is Response == R1 following error can occur (Desciption in Standart s. Card Status)

Is Response == R5 following error can occur (Desciption in Standart s. SDIO R5)

Is Response == R6 following error can occur (Desciption in Standart Response R6)

sd.h