44static const char*
s_tag =
"SDCARD";
47typedef enum : uint32_t {
73typedef enum : uint8_t {
94typedef enum : uint32_t {
123typedef enum : uint32_t {
155 uint32_t rsp[4] = {0U, 0U, 0U, 0U};
221 if (csd_structure == 1U) {
225 const uint32_t c_size =
231 if (csd_structure == 0U) {
236 const uint16_t c_size =
241 const uint32_t mult = 1U << ((uint32_t)c_size_mult + 2U);
242 const uint32_t block_len = 1U << (uint32_t)read_bl_len;
243 const uint32_t blocknr = ((uint32_t)c_size + 1U) * mult;
277 uint32_t rsp[4] = {0U, 0U, 0U, 0U};
329 uint32_t rsp[4] = {0U, 0U, 0U, 0U};
383 const uint32_t rca_arg = ((uint32_t)rca) << 16U;
384 uint32_t rsp[4] = {0U, 0U, 0U, 0U};
436 if (high_capacity == 0U) {
514 uint32_t* out_blocks,
515 uint8_t* out_high_capacity)
552 uint32_t blocks = 0U;
553 uint8_t high_capacity = 0U;
621 if ((lba + count) >
s_sdcard.capacity_blocks) {
637 if ((lba + count) >
s_sdcard.capacity_blocks) {
650 *out_blocks =
s_sdcard.capacity_blocks;
669 const uint8_t inst =
s_sdcard.instance;
static const char * s_tag
Logging / check tag.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_RETURN_ON_ERROR(err, tag, message)
Early return on error, propagating the code upward.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_hw_init_failed
Hardware peripheral failed to initialise.
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_info_val(tag, message, value)
RA8 log info val.
static ra8_err_t internal_sdcard_identify(uint8_t instance)
Drive CMD0 + CMD8 to take the card from power-on into idle.
static ra8_err_t internal_decode_csd(const uint32_t *rsp, uint32_t *out_blocks)
Decode a CSD response (v1 or v2) into a capacity-in-blocks count.
ra8_err_t ra8_sdcard_write_blocks(uint32_t lba, const uint8_t *buf, uint32_t count)
Write count 512-byte blocks starting at lba.
static void internal_sdcard_negotiate_width(uint8_t instance, ra8_sdhi_bus_width_t width, uint16_t rca)
Negotiate the requested SD data-bus width after card selection.
static ra8_err_t internal_sdcard_publish_and_select(uint8_t instance, uint16_t *out_rca, uint32_t *out_blocks)
Publish the RCA, decode the CSD, and put the card in TRAN.
static ra8_err_t internal_sdcard_publish_rca(uint8_t instance, uint16_t *out_rca)
Run CMD2 + CMD3 so the card publishes its relative address.
static uint32_t internal_to_card_address(uint32_t lba)
Translate a logical block address into the SD on-card argument.
ra8_err_t ra8_sdcard_get_capacity(uint32_t *out_blocks)
Return card capacity in 512-byte blocks.
ra8_sdcard_proto_t
SD protocol-level constants.
@ k_ra8_sdcard_cmd8_pattern
VHS=0001, check=0xAA.
@ k_ra8_sdcard_init_retry_max
ACMD41 retry budget.
@ k_ra8_sdcard_default_clk_div
CLKSEL=4 -> 25 MHz at 200 MHz PCLKB.
@ k_ra8_sdcard_ocr_busy_done_mask
OCR.busy clears when ready.
@ k_ra8_sdcard_acmd41_hcs
HCS bit – request HC support.
@ k_ra8_sdcard_acmd41_voltage_win
OCR voltage window 2.7-3.6 V.
@ k_ra8_sdcard_cmd8_pattern_mask
low 12 bits echoed
@ k_ra8_sdcard_ocr_ccs_mask
OCR.CCS = 1 -> SDHC/SDXC.
static ra8_sdcard_state_t s_sdcard
Module-private state.
ra8_err_t ra8_sdcard_get_type(ra8_sdcard_card_type_t *out_type)
Return the detected card type (SDSC / SDHC / SDXC).
ra8_sdcard_csd_t
CSD register decoding constants.
@ k_ra8_sdcard_csd_v2_blocks_per_unit
512 KiB / 512 B
@ k_ra8_sdcard_sdhc_threshold_blocks
2 GiB / 512 B = SDXC boundary
static ra8_err_t internal_sdcard_card_online(uint8_t instance, uint16_t *out_rca, uint32_t *out_blocks, uint8_t *out_high_capacity)
Take an identified SDHI card from power-on to TRAN state.
ra8_err_t ra8_sdcard_init(const ra8_sdcard_cfg_t *cfg)
Bring up the SDHI block and perform full SD card initialization.
ra8_err_t ra8_sdcard_deinit(void)
Tear down the card driver and release the SDHI block.
static ra8_err_t internal_run_acmd41(uint8_t instance, uint32_t *out_ocr)
sd_csd_field_t
SD CSD register field masks, shifts and block size.
@ k_sd_block_size_bytes
SD logical block size.
@ k_sd_csd_struct_shift
CSD_STRUCTURE field shift.
@ k_sd_csize_v1_lo_shift
CSD v1 C_SIZE low-bit shift.
@ k_sd_csize_v2_mask
CSD v2 C_SIZE (22-bit).
@ k_sd_csize_v2_mid_mask
CSD v2 C_SIZE byte-1 bits.
@ k_sd_rca_mask
Relative card address (16-bit).
@ k_sd_csize_mult_mask
C_SIZE_MULT (3-bit).
@ k_sd_csize_v1_mask
CSD v1 C_SIZE (12-bit).
@ k_sd_csize_v1_shift
CSD v1 C_SIZE high-bit shift.
@ k_sd_csize_v2_lo_mask
CSD v2 C_SIZE byte-2 bits.
@ k_sd_read_bl_mask
READ_BL_LEN (4-bit).
@ k_sd_csize_mult_shift
C_SIZE_MULT shift.
@ k_sd_csize_v1_lo_mask
CSD v1 C_SIZE low 10 bits.
static ra8_sdcard_card_type_t internal_classify(uint8_t high_capacity, uint32_t blocks)
Classify card type from OCR.CCS and decoded block count.
ra8_err_t ra8_sdcard_read_blocks(uint32_t lba, uint8_t *buf, uint32_t count)
Read count 512-byte blocks starting at lba.
ra8_sdcard_cmd_t
SD Physical Layer command indices used by ra8_sdcard_init.
@ k_ra8_sdcard_cmd8_send_if_cond
CMD8 SEND_IF_COND.
@ k_ra8_sdcard_cmd7_select_card
CMD7 SELECT_CARD.
@ k_ra8_sdcard_cmd2_all_send_cid
CMD2 ALL_SEND_CID.
@ k_ra8_sdcard_acmd41_send_op_cond
ACMD41 SD_SEND_OP_COND.
@ k_ra8_sdcard_cmd0_go_idle
CMD0 GO_IDLE_STATE.
@ k_ra8_sdcard_cmd3_send_rca
CMD3 SEND_RELATIVE_ADDR.
@ k_ra8_sdcard_cmd9_send_csd
CMD9 SEND_CSD.
@ k_ra8_sdcard_cmd55_app_cmd
CMD55 APP_CMD prefix.
SD card driver layered on top of the RA8D2 SDHI peripheral.
ra8_sdcard_card_type_t
Detected SD card capacity / addressing class.
@ k_ra8_sdcard_type_sdsc
Standard-capacity, byte-addressed (<= 2 GB).
@ k_ra8_sdcard_type_sdxc
Extended-capacity, block-addressed (>32 GB).
@ k_ra8_sdcard_type_unknown
Not yet probed or no card present.
@ k_ra8_sdcard_type_sdhc
High-capacity, block-addressed (2-32 GB).
SD/MMC Host Interface (SDHI) driver scaffold.
ra8_err_t ra8_sdhi_read_block(uint8_t instance, uint32_t lba, uint8_t *buf, uint32_t block_count)
Read one or more 512-byte SD blocks via the SD_BUF0 FIFO.
ra8_err_t ra8_sdhi_write_block(uint8_t instance, uint32_t lba, const uint8_t *buf, uint32_t block_count)
Write one or more 512-byte SD blocks via the SD_BUF0 FIFO.
ra8_err_t ra8_sdhi_set_clock(uint8_t instance, uint32_t divider)
Set the SD bus clock divider (SD_CLK_CTRL).
ra8_err_t ra8_sdhi_send_command(uint8_t instance, uint32_t cmd, uint32_t arg, uint32_t *out_rsp)
Issue a single SD command and read the 4-word response.
ra8_err_t ra8_sdhi_init(uint8_t instance)
Initialise an SDHI instance.
ra8_err_t ra8_sdhi_deinit(uint8_t instance)
Tear down an SDHI instance.
ra8_err_t ra8_sdhi_set_bus_width_4bit(uint8_t instance, uint16_t rca)
Negotiate a 4-bit data bus with the card via CMD55 + ACMD6.
ra8_sdhi_bus_width_t
SD data-bus width accepted by ra8_sdhi_set_bus_width.
@ k_ra8_sdhi_bus_width_4bit
Four data lanes (SD default-speed wide).
User-supplied configuration for ra8_sdcard_init.
uint8_t instance
SDHI instance index (0 or 1).
ra8_sdhi_bus_width_t bus_width
Target bus width (0/1-bit default, 4-bit).
Internal per-driver state.
ra8_sdcard_card_type_t type
SDSC / SDHC / SDXC.
uint16_t rca
Card-assigned relative address.
uint32_t capacity_blocks
512-byte block count, 0 if not init'd
uint8_t instance
SDHI instance index.
uint8_t initialized
Non-zero once init succeeded.