Contiki-Inga 3.x
|
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. | |
This driver provides the following main features:
Note that multiple bock write is faster than single block write but only writes sequential block numbers
#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().
void sdcard_cmd_crc | ( | uint8_t * | cmd | ) |
This function calculates the CRC7 for SD Card commands.
*cmd | The 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 | ) |
uint8_t sdcard_erase_blocks | ( | uint32_t | startaddr, |
uint32_t | endaddr | ||
) |
Definition at line 683 of file sdcard.c.
References MICRO_SD_CS, mspi_chip_release(), mspi_chip_select(), NULL, SDCARD_CMD32, SDCARD_CMD33, SDCARD_CMD38, SDCARD_SUCCESS, and sdcard_write_cmd().
uint32_t sdcard_get_block_num | ( | ) |
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().
Definition at line 331 of file sdcard.c.
Referenced by sdcard_get_block_num().
uint64_t sdcard_get_card_size | ( | ) |
uint8_t sdcard_init | ( | void | ) |
Initializes the SD Card.
SDCARD_SUCCESS | SD-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.
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.
addr | Block address |
*buffer | Pointer to a block buffer (needs to be as long as sdcard_get_block_size()). |
SDCARD_SUCCESS | SD-Card block read was successful |
SDCARD_CMD_ERROR | CMD17 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.
*buffer | Pointer to a block buffer, MUST hold at least 16 Bytes. |
SDCARD_SUCCESS | SD-Card CSD read was successful |
SDCARD_CMD_ERROR | CMD9 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.
enable | 0 if CRC should be disabled, 1 if it should be enabled |
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.
addr | Block address |
*buffer | Pointer to a block buffer (needs to be as long as sdcard_get_block_size()). |
SDCARD_SUCCESS | SD-Card block write was successful |
SDCARD_CMD_ERROR | CMD24 failure |
SDCARD_BUSY_TIMEOUT | |
SDCARD_DATA_ERROR |
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
cmd | Command number to send |
*arg | pointer to 32bit argument (addresses etc.) |
*resp | Pointer 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. |
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.
buffer |
SDCARD_SUCCESS | Successfully wrote block |
SDCARD_BUSY_TIMEOUT | |
SDCARD_DATA_ERROR |
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.
addr | Address of first block |
num_blocks | Number 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 |
SDCARD_SUCCESS | Starting mutli lock write was successful |
SDCARD_CMD_ERROR | CMD25 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.
SDCARD_SUCCESS | successfull |
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.