51static const char*
s_tag =
"SDSPI";
368 const uint16_t actual = (uint16_t)(((uint16_t)crc_hi <<
k_sd_bit_byte) | (uint16_t)crc_lo);
369 if (expected != actual) {
418 for (uint32_t i = 0U; i < count; i++) {
471 if ((err !=
k_ra8_ok) || (r1 != 0U)) {
518 uint8_t response = 0U;
559 for (uint32_t i = 0U; i < count; i++) {
624 uint8_t acmd_r1 = 0U;
628 if ((err !=
k_ra8_ok) || (r1 != 0U)) {
709 if (buf ==
nullptr) {
715 if (lba > (uint64_t)UINT32_MAX) {
728 if (buf ==
nullptr) {
731 if (lba > (uint64_t)UINT32_MAX) {
744 if ((lba > (uint64_t)UINT32_MAX) || (count > (uint64_t)UINT32_MAX)) {
756 if ((block_count ==
nullptr) || (block_size ==
nullptr)) {
777 out_backend->
ctx =
nullptr;
static const char * s_tag
Logging / check tag.
#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_not_supported
Requested feature not compiled in, not wired, or not supported by this MCU variant.
@ k_ra8_err_crc_mismatch
CRC mismatch detected on received data.
@ 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.
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
@ k_ra8_err_protocol_error
Protocol-level error (e.g.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
PSEL codes for peripheral pin routing on the RA8D2.
@ k_ra8_psel_sci_async
00100b: SCI async serial (UART).
static ra8_err_t internal_fs_get_capacity(void *ctx, uint64_t *block_count, uint32_t *block_size)
ra8_fs capacity trampoline – map block-device caps to (count, size).
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_error(tag, message)
RA8 log error.
ra8_port_pin_t
Packed (port << 8) | pin pin identifier.
@ k_ra8_level_high
Drive or read 1.
@ k_ra8_level_low
Drive or read 0.
High-level GPIO helpers on top of the PORT + PFS register layer.
ra8_err_t ra8_gpio_output_init(ra8_port_pin_t pin, ra8_level_t init_level)
Configure a pin as a digital output and drive it to an initial level.
ra8_err_t ra8_gpio_write(ra8_port_pin_t pin, ra8_level_t level)
Drive a previously-configured output to the given level.
ra8_err_t ra8_pfs_route_peripheral(ra8_port_pin_t pin, ra8_psel_t psel, const char *owner)
Route a pin to a non-IRQ peripheral function via PFS.PSEL.
SCI Simple-SPI controller-mode driver (polling, full-duplex).
ra8_err_t ra8_sci_spi_set_clock(uint8_t channel, uint32_t baud_hz, uint32_t pclk_hz)
Re-program the bit-rate without otherwise reconfiguring.
ra8_err_t ra8_sci_spi_xfer(uint8_t channel, const uint8_t *tx, uint8_t *rx, uint32_t len)
Full-duplex multi-byte transfer.
ra8_err_t ra8_sci_spi_init(uint8_t channel, const ra8_sci_spi_cfg_t *cfg)
Bring an SCI channel up as a Simple-SPI controller.
ra8_err_t priv_sdmmc_spi_xfer_one(uint8_t tx, uint8_t *rx)
Shift out one byte and capture the response over the transport.
ra8_err_t priv_sdmmc_spi_send_acmd(sd_cmd_t acmd, uint32_t arg, uint8_t *out_r1)
Send an ACMD by prefixing it with CMD55 (APP_CMD).
sd_state_t g_sdmmc_spi_state
Single driver instance (the module's one external definition).
ra8_err_t priv_sdmmc_spi_wait_not_busy(void)
Wait for the card to release the busy token (CIPO returns to 0xFF).
ra8_err_t priv_sdmmc_spi_cs_release(void)
Drive CS high after clocking one idle byte (SD spec section 7.2.4).
ra8_err_t priv_sdmmc_spi_send_idle(uint32_t n)
Shift n idle bytes (0xFF) and discard the response.
ra8_err_t priv_sdmmc_spi_send_command(sd_cmd_t cmd, uint32_t arg, uint8_t *out_r1)
Send a command frame and capture the R1 response byte.
ra8_err_t priv_sdmmc_spi_run_init_sequence(void)
Walk the full SD identification sequence and learn capacity / type.
ra8_err_t priv_sdmmc_spi_send_stop_transmission(uint8_t *out_r1)
Send CMD12 (STOP_TRANSMISSION) and capture its R1 response byte.
ra8_err_t priv_sdmmc_spi_wait_not_busy_bounded(uint32_t max_polls)
Wait for the card to release busy (CIPO -> 0xFF), bounded by max_polls.
ra8_err_t priv_sdmmc_spi_validate_transport(const ra8_sdmmc_spi_transport_t *transport)
Validate the transport descriptor: every callback non-NULL.
ra8_err_t priv_sdmmc_spi_cs_assert(void)
Drive CS low and clock a single idle byte so CIPO is sampled.
ra8_err_t priv_sdmmc_spi_wait_data_token(void)
Poll for the data-start token (0xFE), bounded by a retry budget.
SD card driver in SPI-mode (PMOD-attached cards).
@ k_ra8_sdmmc_spi_block_size
512-byte block (SD spec PHY v9 section 7.2.2).
@ k_ra8_sdmmc_spi_clock_data_hz
Default-speed data clock.
@ k_ra8_sdmmc_spi_clock_init_hz
Mandatory init clock floor.
uint16_t ra8_sdmmc_spi_crc16(const uint8_t *data, uint32_t len)
Compute the SD-spec CRC16-CCITT over an arbitrary byte run.
ra8_sdmmc_spi_card_type_t
Detected SD card capacity / addressing class.
@ k_ra8_sdmmc_spi_type_sdhc
SDHC / SDXC v2.x (block-addressed).
@ k_ra8_sdmmc_spi_type_unknown
Not yet probed / probe failed.
Module-private cross-TU surface for the SPI-mode SD card driver.
@ k_sd_token_stop_multi
Multi-block stop.
@ k_sd_token_data_start_single
Single-block start.
@ k_sd_token_idle
Idle bus.
@ k_sd_token_data_start_multi
Multi-block start.
@ k_sd_max_erase_poll_bytes
Busy ceiling for a bulk CMD38 erase (seconds).
@ k_sd_mask_byte
Low 8 bits of a wider value.
@ k_sd_data_response_mask
SD data response mask.
@ k_sd_data_response_accepted
0b00101 – data accepted.
sd_cmd_t
Wire-byte for every command used by this driver.
@ k_sd_cmd_read_multi_block
CMD18.
@ k_sd_cmd_read_single_block
CMD17.
@ k_sd_acmd_set_wr_blk_erase_count
ACMD23.
@ k_sd_cmd_erase_wr_blk_start
CMD32.
@ k_sd_cmd_erase_wr_blk_end
CMD33.
@ k_sd_cmd_write_single_block
CMD24.
@ k_sd_cmd_write_multi_block
CMD25.
@ k_sd_bit_byte
SD bit byte.
static ra8_err_t internal_sci_transport_bringup(uint8_t channel, uint32_t pclk_hz, const ra8_sdmmc_spi_sci_pins_t *pins)
Route the four Pmod SPI pins and bring up the SCI Simple-SPI channel.
static ra8_err_t internal_prepare_init(const ra8_sdmmc_spi_transport_t *transport)
static ra8_err_t internal_cmd_require_ready(sd_cmd_t cmd, uint32_t arg)
ra8_err_t ra8_sdmmc_spi_transport_sci(uint8_t sci_channel, uint32_t pclk_hz, const ra8_sdmmc_spi_sci_pins_t *pins, ra8_sdmmc_spi_transport_t *out)
Build the standard EK-RA8D2 SCI Simple-SPI transport for an SD card.
static ra8_err_t internal_read_block_payload(uint8_t *buf)
static ra8_err_t internal_read_data_phase(uint32_t lba, uint8_t *buf)
static ra8_err_t internal_write_data_block(const uint8_t *buf, uint8_t start_token)
static ra8_err_t internal_fs_get_capacity(void *ctx, uint64_t *block_count, uint32_t *block_size)
static ra8_err_t internal_finalize_init(void)
static ra8_err_t internal_sci_transport_xfer(void *ctx, const uint8_t *tx, uint8_t *rx, uint32_t len)
Factory transport transfer shim: full-duplex byte exchange.
static ra8_err_t internal_erase_range(uint32_t lba, uint32_t count)
static sci_bus_ctx_t s_sci_ctx
Single-shot bus context backing the factory's transport callbacks.
ra8_err_t ra8_sdmmc_spi_read_blocks(uint32_t lba, uint8_t *buf, uint32_t count)
Read count contiguous 512-byte blocks in one CMD18 transaction.
static ra8_err_t internal_sci_transport_cs(void *ctx, bool asserted)
Factory transport chip-select shim: drive CS low (asserted) or high.
static ra8_err_t internal_sci_transport_set_clock(void *ctx, uint32_t hz)
Factory transport set-clock shim: retune the SCI baud divider.
static ra8_err_t internal_read_multi_stream(uint8_t *buf, uint32_t count)
ra8_err_t ra8_sdmmc_spi_erase_blocks(uint32_t lba, uint32_t count)
Bulk-erase count blocks at lba, guaranteeing a zero read-back.
static ra8_err_t internal_fs_erase_block(void *ctx, uint64_t lba, uint64_t count)
static ra8_err_t internal_read_block_crc_check(const uint8_t *buf)
ra8_err_t ra8_sdmmc_spi_write_block(uint32_t lba, const uint8_t *buf)
Write one 512-byte block at logical block address lba.
ra8_err_t ra8_sdmmc_spi_read_block(uint32_t lba, uint8_t *buf)
Read one 512-byte block at logical block address lba.
static ra8_err_t internal_fs_read_block(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf)
static ra8_err_t internal_write_multi_stream(const uint8_t *buf, uint32_t count)
ra8_err_t ra8_sdmmc_spi_get_capacity(uint32_t *out_blocks)
Return the card capacity in 512-byte blocks.
ra8_err_t ra8_sdmmc_spi_init(const ra8_sdmmc_spi_transport_t *transport)
Run the SD SPI-mode identification sequence on a card.
static uint32_t internal_lba_to_arg(uint32_t lba)
static ra8_err_t internal_fs_write_block(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf)
ra8_err_t ra8_sdmmc_spi_write_blocks(uint32_t lba, const uint8_t *buf, uint32_t count)
Write count contiguous 512-byte blocks with one CMD25 transaction.
ra8_err_t ra8_sdmmc_spi_deinit(void)
Tear down the driver and release the transport binding.
ra8_err_t ra8_sdmmc_spi_get_card_type(ra8_sdmmc_spi_card_type_t *out_type)
Return the detected card type.
static ra8_err_t internal_read_multi_stop(void)
ra8_err_t ra8_sdmmc_spi_bind_fs_backend(ra8_fs_backend_t *out_backend)
Populate an ra8_fs_backend_t that mounts onto this SD driver.
Contracts for file-local SD block-I/O helpers.
SPI_B controller driver (RA8D2 Type-B SPI peripheral).
@ k_ra8_spi_mode_0
CPOL=0, CPHA=0.
Block-device interface that ra8_fs runs on top of.
ra8_err_t(* read_block)(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf)
Read count consecutive blocks starting at lba.
ra8_err_t(* write_block)(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf)
Write count consecutive blocks starting at lba.
ra8_err_t(* erase_blocks)(void *ctx, uint64_t lba, uint64_t count)
OPTIONAL: bulk-erase count blocks at lba to all-zero bytes.
void * ctx
Caller-owned context passed back into the function pointers.
ra8_err_t(* get_capacity)(void *ctx, uint64_t *block_count, uint32_t *block_size)
Report the device size.
Configuration descriptor for ra8_sci_spi_init.
The four bus pins of an SCI Simple-SPI SD slot.
ra8_port_pin_t copi
Controller-Out / Peripheral-In pin (COPIn).
ra8_port_pin_t sck
SPI clock pin (routed to SCKn).
ra8_port_pin_t cipo
Controller-In / Peripheral-Out pin (CIPOn).
ra8_port_pin_t cs
Chip-select pin, driven as a GPIO output.
Driver-to-bus binding (Dependency Inversion seam).
void * ctx
Caller context.
ra8_sdmmc_spi_set_clock_fn_t set_clock
Set the bus clock.
ra8_sdmmc_spi_xfer_fn_t xfer
Exchange bytes.
ra8_sdmmc_spi_cs_fn_t cs
Drive chip select.
Bus context handed to the factory's transport callbacks.
ra8_port_pin_t cs
Chip-select GPIO pin (active-low).
uint8_t channel
SCI Simple-SPI channel index.
uint32_t pclka_hz
PCLKA rate (Hz) for the baud shim.