Contiki-Inga 3.x
Files | Macros | Functions
MicroSD Card Interface

This driver provides the following main features: More...

Files

file  sdcard.c
 
 SD Card interface implementation

 

Macros

#define SDCARD_CMD0   0
 CMD0 – GO_IDLE_STATE.
 
#define SDCARD_CMD1   1
 CMD1 – SEND_OP_COND.
 
#define SDCARD_CMD8   8
 CMD8 – SEND_IF_COND (deprecated?)
 
#define SDCARD_CMD9   9
 CMD9 – SEND_CSD.
 
#define SDCARD_CMD10   10
 CMD10 – SEND_CID.
 
#define SDCARD_CMD13   13
 CMD13 – SEND_STATUS.
 
#define SDCARD_CMD16   16
 CMD16 – SET_BLOCKLEN.
 
#define SDCARD_CMD17   17
 CMD17 – READ_SINGLE_BLOCK.
 
#define SDCARD_CMD18   18
 CMD18 – READ_MULTIPLE_BLOCK.
 
#define SDCARD_CMD24   24
 CMD24 – WRITE_BLOCK.
 
#define SDCARD_CMD25   25
 CMD25 – WRITE_MULTIPLE_BLOCK.
 
#define SDCARD_CMD27   27
 CMD27 – PROGRAM_CSD.
 
#define SDCARD_CMD32   32
 CMD32 – ERASE_WR_BLK_START_ADDR.
 
#define SDCARD_CMD33   33
 CMD33 – ERASE_WR_BLK_END_ADDR.
 
#define SDCARD_CMD38   38
 CMD38 – ERASE.
 
#define SDCARD_CMD58   58
 CMD58 – READ_OCR.
 
#define SDCARD_CMD59   59
 CMD59 – CRC_ON_OFF.
 
#define SDCARD_CMD55   55
 CMD55 – APP_CMD.
 
#define SDCARD_ACMD23   (IS_ACMD | 23)
 ACMD23 – SET_WR_BLK_ERASE_COUNT.
 
#define SDCARD_ACMD41   (IS_ACMD | 41)
 ACMD41 – SD_SEND_OP_COND.
 
#define MICRO_SD_CS   5
 

Functions

uint64_t sdcard_get_card_size ()
 Returns card size. More...
 
uint32_t sdcard_get_block_num ()
 
uint16_t sdcard_get_block_size ()
 Returns the size of one block in bytes. More...
 
uint8_t sdcard_is_SDSC ()
 This function indicates if a card is a SDSC or SDHC/SDXC card. More...
 
uint8_t sdcard_set_CRC (uint8_t enable)
 Turns crc capabilities of the card on or off. More...
 
uint8_t sdcard_read_csd (uint8_t *buffer)
 This function will read the CSD (16 Bytes) of the SD-Card. More...
 
void sdcard_cmd_crc (uint8_t *cmd)
 This function calculates the CRC7 for SD Card commands. More...
 
uint16_t sdcard_data_crc (uint8_t *data)
 This function calculates the CRC16 for a 512 Byte data block. More...
 
uint8_t sdcard_init (void)
 Initializes the SD Card. More...
 
uint8_t sdcard_erase_blocks (uint32_t startaddr, uint32_t endaddr)
 
uint8_t sdcard_read_block (uint32_t addr, uint8_t *buffer)
 This function will read one block (512, 1024, 2048 or 4096Byte) of the SD-Card. More...
 
uint8_t sdcard_write_block (uint32_t addr, uint8_t *buffer)
 This function will write one block (512, 1024, 2048 or 4096Byte) of the SD-Card. More...
 
uint8_t sdcard_write_multi_block_start (uint32_t addr, uint32_t num_blocks)
 Prepares to write multiple blocks sequentially. More...
 
uint8_t sdcard_write_multi_block_next (uint8_t *buffer)
 Writes single of multiple sequental blocks. More...
 
uint8_t sdcard_write_multi_block_stop ()
 Stops multiple block write. More...
 
uint8_t sdcard_write_cmd (uint8_t cmd, uint32_t *arg, uint8_t *resp)
 This function sends a command via SPI to the SD-Card. More...
 

Interface return codes

#define SDCARD_SUCCESS   0
 Successfully completed operation.
 
#define SDCARD_REJECTED   2
 Indicates the host cannot handle this card, maybe due to rejected voltage range.
 
#define SDCARD_CMD_TIMEOUT   4
 Timeout while trying to send command.
 
#define SDCARD_CMD_ERROR   3
 Card sent an error response.
 
#define SDCARD_DATA_TIMEOUT   6
 Card did not send a data start byte to indicate beginning of a data block.
 
#define SDCARD_DATA_ERROR   7
 Card returned error when trying to read or write data. More...
 
#define SDCARD_BUSY_TIMEOUT   8
 Busy waiting timed out (card held down data line too long)
 
#define SDCARD_CSD_ERROR   10
 Failed reading CSD register.
 

Detailed Description

This driver provides the following main features:

Note that multiple bock write is faster than single block write but only writes sequential block numbers

Note
CRC functionality is not fully implemented thus it sould not be used yet.
Author
Ulf Kulau kulau.nosp@m.@ibr.nosp@m..cs.t.nosp@m.u-bs.nosp@m..de Christoph Peltz peltz.nosp@m.@ibr.nosp@m..cs.t.nosp@m.u-bs.nosp@m..de Enrico Joerns joern.nosp@m.s@ib.nosp@m.r.cs..nosp@m.tu-b.nosp@m.s.de

Macro Definition Documentation

#define MICRO_SD_CS   5

SPI device order. The chip select number where the sdcard-Card is connected to the BCD-decimal decoder

Definition at line 75 of file sdcard.h.

Referenced by sdcard_erase_blocks(), sdcard_init(), sdcard_read_block(), sdcard_read_csd(), sdcard_set_CRC(), sdcard_write_block(), sdcard_write_multi_block_next(), sdcard_write_multi_block_start(), and sdcard_write_multi_block_stop().

#define SDCARD_DATA_ERROR   7

Card returned error when trying to read or write data.

Obtained from data response (write) or data error response (read) token

Definition at line 93 of file sdcard.h.

Referenced by sdcard_read_block(), sdcard_read_csd(), sdcard_write_block(), and sdcard_write_multi_block_next().

Function Documentation

void sdcard_cmd_crc ( uint8_t *  cmd)

This function calculates the CRC7 for SD Card commands.

Parameters
*cmdThe array containing the command with every byte except the CRC byte correctly set. The CRC part will be written into the cmd array (at the correct position).

Definition at line 475 of file sdcard.c.

Referenced by sdcard_write_cmd().

uint16_t sdcard_data_crc ( uint8_t *  data)

This function calculates the CRC16 for a 512 Byte data block.

Parameters
*dataThe array containing the data to be send. Must be 512 Bytes long.
Returns
The CRC16 code for the given data.

Definition at line 499 of file sdcard.c.

uint8_t sdcard_erase_blocks ( uint32_t  startaddr,
uint32_t  endaddr 
)
uint32_t sdcard_get_block_num ( )
Returns
number of blocks

Definition at line 325 of file sdcard.c.

References sdcard_get_block_size().

uint16_t sdcard_get_block_size ( )

Returns the size of one block in bytes.

Mainly used to calculated size of the SD-Card together with sdcard_get_card_block_count().

Note
It's fixed at 512 Bytes.
Returns
Number of Bytes in one Block.

Definition at line 331 of file sdcard.c.

Referenced by sdcard_get_block_num().

uint64_t sdcard_get_card_size ( )

Returns card size.

Returns
card size in bytes

Definition at line 319 of file sdcard.c.

uint8_t sdcard_init ( void  )

Initializes the SD Card.

Return values
SDCARD_SUCCESSSD-Card was initialized without an error
SDCARD_CMD_ERROR
SDCARD_CMD_TIMEOUT
SDCARD_REJECTED
SDCARD_CSD_ERROR

Definition at line 524 of file sdcard.c.

References MICRO_SD_CS, MSPI_BAUD_2MBPS, mspi_chip_release(), mspi_chip_select(), mspi_init(), MSPI_MODE_0, mspi_transceive(), NULL, process_alloc_event(), process_start(), SDCARD_ACMD41, SDCARD_CMD0, SDCARD_CMD1, SDCARD_CMD16, SDCARD_CMD58, SDCARD_CMD8, SDCARD_CMD_ERROR, SDCARD_CMD_TIMEOUT, SDCARD_CSD_ERROR, sdcard_read_csd(), SDCARD_REJECTED, SDCARD_SUCCESS, and sdcard_write_cmd().

uint8_t sdcard_is_SDSC ( )

This function indicates if a card is a SDSC or SDHC/SDXC card.

Note
sdcard_init() must be called beforehand and be successful before this functions return value has any meaning.
Returns
Not 0 if the card is SDSC and 0 if SDHC/SDXC

Definition at line 337 of file sdcard.c.

uint8_t sdcard_read_block ( uint32_t  addr,
uint8_t *  buffer 
)

This function will read one block (512, 1024, 2048 or 4096Byte) of the SD-Card.

Parameters
addrBlock address
*bufferPointer to a block buffer (needs to be as long as sdcard_get_block_size()).
Return values
SDCARD_SUCCESSSD-Card block read was successful
SDCARD_CMD_ERRORCMD17 failure
SDCARD_BUSY_TIMEOUT
SDCARD_DATA_TIMEOUT
SDCARD_DATA_ERROR

Definition at line 728 of file sdcard.c.

References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), mspi_transceive(), NULL, SDCARD_BUSY_TIMEOUT, SDCARD_CMD17, SDCARD_CMD_ERROR, SDCARD_DATA_ERROR, SDCARD_DATA_TIMEOUT, SDCARD_SUCCESS, and sdcard_write_cmd().

uint8_t sdcard_read_csd ( uint8_t *  buffer)

This function will read the CSD (16 Bytes) of the SD-Card.

Parameters
*bufferPointer to a block buffer, MUST hold at least 16 Bytes.
Return values
SDCARD_SUCCESSSD-Card CSD read was successful
SDCARD_CMD_ERRORCMD9 failure.
SDCARD_DATA_TIMEOUT
SDCARD_DATA_ERROR

Definition at line 362 of file sdcard.c.

References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), mspi_transceive(), NULL, SDCARD_CMD9, SDCARD_CMD_ERROR, SDCARD_DATA_ERROR, SDCARD_DATA_TIMEOUT, SDCARD_SUCCESS, and sdcard_write_cmd().

Referenced by sdcard_init().

uint8_t sdcard_set_CRC ( uint8_t  enable)

Turns crc capabilities of the card on or off.

Note
Does not work properly at this time. (FIXME)
Parameters
enable0 if CRC should be disabled, 1 if it should be enabled
Returns
0 on success, !=0 otherwise

Definition at line 343 of file sdcard.c.

References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), NULL, SDCARD_CMD59, SDCARD_CMD_ERROR, SDCARD_SUCCESS, and sdcard_write_cmd().

uint8_t sdcard_write_block ( uint32_t  addr,
uint8_t *  buffer 
)

This function will write one block (512, 1024, 2048 or 4096Byte) of the SD-Card.

Parameters
addrBlock address
*bufferPointer to a block buffer (needs to be as long as sdcard_get_block_size()).
Return values
SDCARD_SUCCESSSD-Card block write was successful
SDCARD_CMD_ERRORCMD24 failure
SDCARD_BUSY_TIMEOUT
SDCARD_DATA_ERROR
Todo:
: handle crc enabled?

Definition at line 805 of file sdcard.c.

References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), mspi_transceive(), NULL, SDCARD_BUSY_TIMEOUT, SDCARD_CMD24, SDCARD_CMD_ERROR, SDCARD_DATA_ERROR, SDCARD_SUCCESS, and sdcard_write_cmd().

uint8_t sdcard_write_cmd ( uint8_t  cmd,
uint32_t *  arg,
uint8_t *  resp 
)

This function sends a command via SPI to the SD-Card.

An SPI command consists off 6 bytes

Parameters
cmdCommand number to send
*argpointer to 32bit argument (addresses etc.)
*respPointer to the response array. Only needed for responses other than R1. May be NULL if response is R1. Otherwise resp must be long enough for the response (only R3 and R7 are supported yet) and the first byte of the response array must indicate the response that is expected. For Example the first byte should be 0x07 if response type R7 is expected.
Returns
R1 response byte or 0xFF in case of read/write timeout

Definition at line 1000 of file sdcard.c.

References mspi_transceive(), NULL, SDCARD_CMD0, SDCARD_CMD55, SDCARD_CMD8, sdcard_cmd_crc(), and watchdog_periodic().

Referenced by sdcard_erase_blocks(), sdcard_init(), sdcard_read_block(), sdcard_read_csd(), sdcard_set_CRC(), sdcard_write_block(), and sdcard_write_multi_block_start().

uint8_t sdcard_write_multi_block_next ( uint8_t *  buffer)

Writes single of multiple sequental blocks.

Parameters
buffer
Return values
SDCARD_SUCCESSSuccessfully wrote block
SDCARD_BUSY_TIMEOUT
SDCARD_DATA_ERROR
Todo:
: handle crc enabled?

Definition at line 910 of file sdcard.c.

References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), mspi_transceive(), SDCARD_BUSY_TIMEOUT, SDCARD_DATA_ERROR, and SDCARD_SUCCESS.

uint8_t sdcard_write_multi_block_start ( uint32_t  addr,
uint32_t  num_blocks 
)

Prepares to write multiple blocks sequentially.

Parameters
addrAddress of first block
num_blocksNumber of blocks that should be written (0 means not known yet). Givin a number here could speed up writing due to possible sector pre-erase
Return values
SDCARD_SUCCESSStarting mutli lock write was successful
SDCARD_CMD_ERRORCMD25 failure
SDCARD_BUSY_TIMEOUT

Definition at line 874 of file sdcard.c.

References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), NULL, SDCARD_ACMD23, SDCARD_BUSY_TIMEOUT, SDCARD_CMD25, SDCARD_CMD_ERROR, SDCARD_SUCCESS, and sdcard_write_cmd().

uint8_t sdcard_write_multi_block_stop ( )

Stops multiple block write.

Return values
SDCARD_SUCCESSsuccessfull
SDCARD_BUSY_TIMEOUT

Definition at line 960 of file sdcard.c.

References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), mspi_transceive(), SDCARD_BUSY_TIMEOUT, and SDCARD_SUCCESS.