SD CARD

SD Card Overview

An SD (Secure Digital) card is a compact, non-volatile memory storage device widely used in consumer electronics and industrial applications. It serves as a storage medium for data such as photos, videos, documents, and software.

Usage

  1. All API functions have the first argument as the sd card information.

  2. Read and write sectors from the sd card.

  3. Erase block in sd card.

  4. Return codes of API functions are defined in header file.

Application Interface

Sd Card calls

Function

Description

rs_sdcard_init

To open the sd card

rs_sdcard_send_cmd

To send the command to the sd card

rs_sdcard_read_cid_data

To read the data from card identification register

rs_sd_wait_till_busy

To wait a certain time

rs_sd_wait_till_write_complete

To wait until write completion

rs_sd_wait_till_read_complete

To wait until read complete

rs_sdcard_reset

To reset the sdcard information

rs_sdcard_param_pack

To pack parameters to the sdcard

rs_sdcard_resp_unpack

To unpack the sdcard response

Using these ‘rs’ calls to initialize the sd card and read and write to the sd card.

Error Code

  • Every API’s for the graphic SD card returns some success or failure values. Please refer below section,

SD Card Header Details

Documentation from the relevant header as follows:

SD card based APIs.

This file contains the macros and function header for SD card features

Author

Embien RAPIDSEA Team

Copyright

Embien Technologies India Pvt. Ltd.

Defines

RS_SD_CMD_TIMEOUT
RS_SD_READREG_TIMEOUT
RS_SD_CMD0

Reset cards to idle state.

RS_SD_CMD0_R
RS_SD_CMD1

Read the OCR (MMC mode, do not use for SD cards)

RS_SD_CMD1_R
RS_SD_CMD8

Sends SD Memory Card interface condition.

RS_SD_CMD8_R
RS_SD_CMD9

Card sends the CSD.

RS_SD_CMD9_R
RS_SD_CMD10

Card sends CID.

RS_SD_CMD10_R
RS_SD_CMD12

Stop a multiple block (stream) read/write operation.

RS_SD_CMD12_R
RS_SD_CMD13

Get the addressed card’s status register.

RS_SD_CMD13_R
RS_SD_R2_2_LOCK
RS_SD_R2_2_WP_ERASE_SKIP
RS_SD_R2_2_ERR
RS_SD_R2_2_CC_ERR
RS_SD_R2_2_ECC_FAILED
RS_SD_R2_2_WP_VIOLATION
RS_SD_R2_2_ERASE_PARAM
RS_SD_R2_2_OUT_OF_RANGE
RS_SD_R7_2_CMD_VERSION_MASK
RS_SD_R7_4_VOLTAGE_MASK
RS_SD_RESP_DATA_MASK
RS_SD_RESP_DATA_ACCETPTED
RS_SD_RESP_DATA_REJECT_CRC
RS_SD_RESP_DATA_REJECT_WRITE
RS_SD_TOK_RD_START_BLOCK
RS_SD_TOK_WR_START_BLOCK
RS_SD_TOK_RD_START_BLOCK_MULTI
RS_SD_TOK_WR_START_BLOCK_MULTI
RS_SD_TOK_STOP_TRAN_MULTI
RS_SD_CRC_EN

if enable CRC

RS_SD_READ_MULTI_BLOCK_EN

if enable multi block read function

RS_SD_WRITE_MULTI_BLOCK_EN

if enable multi block write function

RS_SD_ERASE_BLOCK_EN

if enable block erase function

RS_SD_SPI_CLOCK

SD SPI max(normal) clock freq.

22MHz

RS_CARDTYPE_MMC

SD Card Types.

RS_SD_TYPE_SDSC_V1
RS_SD_TYPE_SDSC_V2
RS_SD_TYPE_SDHC_SDXC
RS_CARDTYPE_UNKNOWN
RS_SD_BLOCK_SIZE

SD Card Block Size, should be 512.

RS_SD_BLOCK_SIZE_NBITS

SD Card Block bit length, 9 means 512byte.

RS_SD_NO_ERR

SD Card Error Code.

RS_SD_ERR_NO_CARD
RS_SD_ERR_USER_PARAM
RS_SD_ERR_CARD_PARAM
RS_SD_ERR_VOL_NOT_SUP
RS_SD_ERR_OVER_CARD_RANGE
RS_SD_ERR_NOT_IN_IDLE
RS_SD_ERR_ACTIVATION
RS_SD_ERR_CMD_RESP_TYPE
RS_SD_ERR_CMD_TIMEOUT
RS_SD_ERR_CMD_RESP
RS_SD_ERR_DATA_CRC16
RS_SD_ERR_DATA_START_TOK
RS_SD_ERR_DATA_RESP
RS_SD_ERR_TIMEOUT_WAIT
RS_SD_ERR_TIMEOUT_READ
RS_SD_ERR_TIMEOUT_WRITE
RS_SD_ERR_TIMEOUT_ERASE
RS_SD_ERR_TIMEOUT_WAIT_IDLE
RS_SD_ERR_WRITE_BLK
RS_SD_ERR_WRITE_BLK_NUMS
RS_SD_ERR_WRITE_PROTECT
RS_SD_NUM_TRIES_INIT

Typedefs

typedef struct tag_rs_sd_card_cid_info rs_sd_card_cid_t
typedef struct tag_rs_sd_card_info rs_sd_card_info_t

Functions

rs_ret_val_t rs_sdcard_init(rs_sd_card_info_t *ptr_sdcard)

Initializate the sdcard.

This function initializate the hardware, re-power on the sdcard, do some initialization sequence, then get some sdcard info, such as block size, block number, the CID and so on.

Parameters:

None.

Returns:

Returns SD_NO_ERR indicates everything is OK, others is the error code.

rs_ret_val_t rs_sd_block_read(uint8_t *ptr_dest_buf, uint32_t u32_block_index)
rs_ret_val_t rs_sd_block_write(uint8_t *ptr_src_buf, uint32_t u32_block_index)
rs_ret_val_t rs_sd_multi_block_read(uint8_t *ptr_dest_buf, uint32_t u32_sart_block_index, uint32_t u32_read_block_number)
rs_ret_val_t rs_sd_multi_block_write(uint8_t *ptr_src_buf, uint32_t u32_start_block_index, uint32_t u32_write_block_number)
rs_ret_val_t rs_sd_multi_block_data_write(uint8_t *ptr_src_buf, uint32_t u32_start_block_index, uint32_t u32_write_block_number)
rs_ret_val_t rs_sd_block_erase(uint32_t u32_start_block_index, uint32_t u32_block_number)
rs_ret_val_t rs_sd_wait_busy(uint8_t u8_wait_type)
rs_ret_val_t rs_sdcard_read_sectors(rs_sd_card_info_t *ptr_sdcard, uint8_t *ptr_dst_buf, uint32_t u32_block_addr, uint32_t u32_num_sectors)
rs_ret_val_t rs_sdcard_write_sectors(rs_sd_card_info_t *ptr_sdcard, uint8_t *ptr_src_buf, uint32_t u32_block_addr, uint32_t u32_num_sectors)
struct tag_rs_sd_card_cid_info

Public Members

uint8_t manufacturerID
uint16_t oemID
char productName[6]
uint8_t productRevision
uint32_t productSerialNumber
uint16_t manufacturingDate
uint8_t crc
struct tag_rs_sd_card_info

Public Members

uint8_t u8_port
uint8_t u8_cmd
uint8_t u8_dummy_data
uint8_t *ptr_param
uint8_t u8_crc
uint8_t u8_resp_type
uint8_t *ptr_resp
uint8_t u8_card_power_status
uint32_t u32_card_type
uint32_t u32_block_count
rs_handle_t interface_handle

Total Block Number(Block size is 512)

rs_sd_card_cid_t cid_info
uint8_t arr_cid_reg[16]
void (*init_interface)(int)

CID register.

void (*select_card)(int)
void (*transfer_data)(char*, int, char*, int)
void (*set_bitrate)(uint32_t)