|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
SPI_B controller driver (RA8D2 Type-B SPI peripheral). More...
Go to the source code of this file.
Data Structures | |
| struct | ra8_spi_cfg_t |
| Configuration descriptor for ra8_spi_init. More... | |
Typedefs | |
| typedef void(* | ra8_spi_complete_fn_t) (void *ctx, uint8_t err_mask) |
| Transfer-complete callback signature. | |
Enumerations | |
| enum | ra8_spi_mode_t : uint8_t { k_ra8_spi_mode_0 = 0U , k_ra8_spi_mode_1 = 1U , k_ra8_spi_mode_2 = 2U , k_ra8_spi_mode_3 = 3U } |
| CPOL / CPHA combinations. More... | |
| enum | ra8_spi_bit_width_t : uint8_t { k_ra8_spi_width_8 = 7U , k_ra8_spi_width_16 = 15U , k_ra8_spi_width_32 = 31U } |
| Per-frame bit width for multi-byte transfers. More... | |
| enum | ra8_spi_err_mask_t : uint8_t { k_ra8_spi_err_none = 0x00U , k_ra8_spi_err_overrun = 0x01U , k_ra8_spi_err_mode = 0x02U , k_ra8_spi_err_parity = 0x04U , k_ra8_spi_err_underrun = 0x08U } |
| Bit mask of SPI error flags. More... | |
Functions | |
| ra8_err_t | ra8_spi_init (uint8_t channel, const ra8_spi_cfg_t *cfg) |
| Initialise an SPI channel with a full config descriptor. | |
| ra8_err_t | ra8_spi_deinit (uint8_t channel) |
| Tear down a channel. | |
| ra8_err_t | ra8_spi_controller_init (uint8_t channel) |
| Legacy init (1.9 MHz at PCLKA = 125 MHz, mode 0). | |
| ra8_err_t | ra8_spi_xfer8 (uint8_t channel, uint8_t tx, uint8_t *rx) |
| Full-duplex 8-bit exchange. | |
| ra8_err_t | ra8_spi_write (uint8_t channel, const void *tx, uint32_t len, ra8_spi_bit_width_t bit_width) |
| Multi-frame TX-only polling transfer. | |
| ra8_err_t | ra8_spi_read (uint8_t channel, void *rx, uint32_t len, ra8_spi_bit_width_t bit_width) |
| Multi-frame RX-only polling transfer. | |
| ra8_err_t | ra8_spi_write_read (uint8_t channel, const void *tx, void *rx, uint32_t len, ra8_spi_bit_width_t bit_width) |
| Multi-frame full-duplex polling transfer. | |
| ra8_err_t | ra8_spi_set_clock (uint8_t channel, uint32_t baud_hz, uint32_t pclka_hz) |
| Change the SPI clock without tearing down the channel. | |
| ra8_err_t | ra8_spi_get_errors (uint8_t channel, uint8_t *out_mask) |
| Read the SPSR error bits (OVRF, MODERF, PERF, UDRF). | |
| ra8_err_t | ra8_spi_clear_errors (uint8_t channel) |
| Clear the SPSR error flags. | |
| ra8_err_t | ra8_spi_attach_transfer_handler (uint8_t channel, ra8_spi_complete_fn_t fn, void *ctx) |
| Attach a transfer-complete callback for a channel. | |
| ra8_err_t | ra8_spi_enter_stop (uint8_t channel) |
| Put the channel into MSTP-gated stop state. | |
| ra8_err_t | ra8_spi_exit_stop (uint8_t channel) |
| Exit MSTP-gated stop state. | |
| ra8_err_t | ra8_spi_b_target_init (uint8_t channel, const ra8_spi_cfg_t *cfg) |
| Initialise an SPI_B channel in target (peripheral) mode. | |
| ra8_err_t | ra8_spi_b_target_xfer (uint8_t channel, uint8_t tx, uint8_t *rx) |
| Exchange one byte as the SPI target in a polled blocking call. | |
| ra8_err_t | ra8_spi_write_dma (uint8_t channel, const uint8_t *data, uint16_t len, ra8_dma_complete_fn_t on_complete, void *ctx, uint8_t *out_dma_channel) |
| Kick off a DMA-backed TX transfer on an SPI channel. | |
| ra8_err_t | ra8_spi_read_dma (uint8_t channel, uint8_t *out_buf, uint16_t len, ra8_dma_complete_fn_t on_complete, void *ctx, uint8_t *out_dma_channel) |
| Kick off a DMA-backed RX transfer on an SPI channel. | |
| void | ra8_spi_dispatch_spti (uint8_t channel) |
| Dispatch SPTI – advance TX state. | |
| void | ra8_spi_dispatch_spri (uint8_t channel) |
| Dispatch SPRI – advance RX state. | |
| void | ra8_spi_dispatch_spei (uint8_t channel) |
| Dispatch SPEI – collect + clear errors, fire callback. | |
SPI_B controller driver (RA8D2 Type-B SPI peripheral).
Public API for the SPI_B controller driver. Implementation lives in libs/ra8_hal/src/ra8_spi_b.c and mirrors FSP r_spi_b in polling-mode controller flow. The earlier register layout (legacy 8/16-bit SPI block) was replaced wholesale on RA8D2 – see ra8_spi_regs.h for the SPI_B register file.
API surface:
Definition in file ra8_spi.h.
| typedef void(* ra8_spi_complete_fn_t) (void *ctx, uint8_t err_mask) |
| enum ra8_spi_bit_width_t : uint8_t |
Per-frame bit width for multi-byte transfers.
Mirrors the subset of FSP spi_bit_width_t that this driver supports. The enum value is the raw SPCMDn.SPB encoding written into the SPI_B command register (HUM Ch 43.2.7 "SPCMDm" p 2893): SPB[4:0] = N - 1 where N is the frame width in bits, so 8-bit frames program SPB = 0b00111, 16-bit frames program SPB = 0b01111, and 32-bit frames program SPB = 0b11111. Matching the FSP enum value lets the value flow straight into SPCMDn.SPB without a lookup table.
| Enumerator | |
|---|---|
| k_ra8_spi_width_8 | 8-bit frame -> SPCMDn.SPB = 0b00111. |
| k_ra8_spi_width_16 | 16-bit frame -> SPCMDn.SPB = 0b01111. |
| k_ra8_spi_width_32 | 32-bit frame -> SPCMDn.SPB = 0b11111. |
| enum ra8_spi_err_mask_t : uint8_t |
| enum ra8_spi_mode_t : uint8_t |
|
nodiscard |
Attach a transfer-complete callback for a channel.
| [in] | channel | SPI channel. |
| [in] | fn | Callback fired on transfer end / error. |
| [in] | ctx | Context passed to the callback. |
Definition at line 809 of file ra8_spi_b.c.
References k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_spi_b_channel_count, and s_spi_state.
|
nodiscard |
Initialise an SPI_B channel in target (peripheral) mode.
Configures the SPI_B peripheral to act as a target device: the external controller drives RSPCK and asserts SSL (CS); the MCU responds on CIPO and receives on COPI. Implementation lives in libs/ra8_hal/src/ra8_spi_b_target.c.
Register-level effect (HUM Ch 43.2.4 "SPCR" p 2884):
The baud_hz and pclka_hz fields of cfg are accepted but ignored – the bit-rate is determined by the external controller.
| [in] | channel | SPI channel (0 or 1). |
| [in] | cfg | Configuration descriptor (non-NULL). |
| k_ra8_ok | Channel configured in target mode; awaiting external controller frames. |
| k_ra8_err_null_ptr | cfg is NULL. |
| k_ra8_err_invalid_arg | channel is out of range. |
| k_ra8_err_hw_error | ra8_mstp_enable failed. |
Definition at line 301 of file ra8_spi_b_target.c.
References internal_target_program_regs(), internal_target_spcr(), k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_spi_b_channel_count, RA8_CHECK_NULL_PTR, ra8_log_info_val, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, ra8_spi(), s_spi_mstp_table, s_tag, and r_spi_regs_t::SPCR.
|
nodiscard |
Exchange one byte as the SPI target in a polled blocking call.
Performs a full-duplex single-byte exchange in target (peripheral) role:
Under RA8_OFF_TARGET the SPSR wait reduces to a single-shot register read: pre-seed the register as shown below.
| [in] | channel | SPI channel (0 or 1). |
| [in] | tx | Byte to send on CIPO (Controller In Peripheral Out). |
| [out] | rx | Pointer to receive the COPI byte; may be NULL if the received byte is not needed. |
| k_ra8_ok | Frame exchange completed. |
| k_ra8_err_null_ptr | channel is out of range (ra8_spi() returned NULL). |
| k_ra8_err_hw_timeout | SPSR.SPTEF or SPSR.SPRF never asserted within k_spi_b_target_poll_limit iterations. |
Definition at line 332 of file ra8_spi_b_target.c.
References internal_target_wait_spsr(), k_ra8_ok, k_ra8_spsr_mask_sprf, k_ra8_spsr_mask_sptef, k_ra8_spsrc_mask_sprfc, k_ra8_spsrc_mask_sptefc, RA8_CHECK_NULL_PTR, ra8_spi(), s_tag, r_spi_regs_t::SPDR, and r_spi_regs_t::SPSRC.
|
nodiscard |
Clear the SPSR error flags.
| [in] | channel | SPI channel. |
Definition at line 794 of file ra8_spi_b.c.
References k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_spi_b_channel_count, k_ra8_spsr_mask_errs, ra8_spi(), and r_spi_regs_t::SPSRC.
Referenced by ra8_spi_dispatch_spei().
|
nodiscard |
Legacy init (1.9 MHz at PCLKA = 125 MHz, mode 0).
| [in] | channel | SPI channel (0 or 1). |
Definition at line 405 of file ra8_spi_b.c.
References k_ra8_err_null_ptr, k_ra8_spi_b_default_baud_hz, k_ra8_spi_b_default_pclka_hz, k_ra8_spi_mode_0, ra8_spi(), and ra8_spi_init().
|
nodiscard |
Tear down a channel.
| [in] | channel | SPI channel. |
Definition at line 382 of file ra8_spi_b.c.
References k_ra8_err_invalid_arg, k_ra8_spi_b_channel_count, ra8_mstp_disable(), ra8_spi(), s_spi_mstp_table, s_spi_state, and r_spi_regs_t::SPCR.
| void ra8_spi_dispatch_spei | ( | uint8_t | channel | ) |
Dispatch SPEI – collect + clear errors, fire callback.
Invoked from the SPEIn IRQ trampoline. Reads and clears the error status bits in SPSR (HUM Ch 36.2.5 "SPSR : SPI Status Register", p 1530) – mode-fault (MODF), overrun (OVRF), parity (PERF) – then invokes the registered error callback with the collected mask.
| [in] | channel | SPI channel index (0..1). |
Definition at line 871 of file ra8_spi_b.c.
References k_ra8_spi_b_channel_count, ra8_spi_clear_errors(), ra8_spi_get_errors(), and s_spi_state.
| void ra8_spi_dispatch_spri | ( | uint8_t | channel | ) |
Dispatch SPRI – advance RX state.
Invoked from the SPRIn IRQ trampoline when the receive FIFO/holding register has new data. Reads SPDR (HUM Ch 36.2.7, p 1532) into the caller-supplied RX buffer and, when the byte count reaches zero, invokes the registered completion callback.
| [in] | channel | SPI channel index (0..1). |
Definition at line 862 of file ra8_spi_b.c.
References k_ra8_spi_b_channel_count, and s_spi_state.
| void ra8_spi_dispatch_spti | ( | uint8_t | channel | ) |
Dispatch SPTI – advance TX state.
Invoked from the SPTIn IRQ trampoline when the transmit FIFO/holding register is ready for the next word. Loads the next byte from the caller-supplied TX buffer into SPDR (HUM Ch 36.2.7 "SPDR : SPI Data Register", p 1532) and decrements the remaining count. Silently returns if the channel is out of range or no transfer is in progress.
| [in] | channel | SPI channel index (0..1). |
Definition at line 853 of file ra8_spi_b.c.
References k_ra8_spi_b_channel_count, and s_spi_state.
|
nodiscard |
Put the channel into MSTP-gated stop state.
Definition at line 824 of file ra8_spi_b.c.
References k_ra8_err_invalid_arg, k_ra8_spi_b_channel_count, ra8_mstp_disable(), ra8_spi(), s_spi_mstp_table, and r_spi_regs_t::SPCR.
|
nodiscard |
Exit MSTP-gated stop state.
Definition at line 839 of file ra8_spi_b.c.
References k_ra8_err_invalid_arg, k_ra8_spi_b_channel_count, ra8_mstp_enable(), and s_spi_mstp_table.
|
nodiscard |
Read the SPSR error bits (OVRF, MODERF, PERF, UDRF).
| [in] | channel | SPI channel. |
| [out] | out_mask | OR of k_ra8_spi_err_*. |
Definition at line 766 of file ra8_spi_b.c.
References k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_spi_b_channel_count, k_ra8_spi_err_mode, k_ra8_spi_err_none, k_ra8_spi_err_overrun, k_ra8_spi_err_parity, k_ra8_spi_err_underrun, k_ra8_spsr_mask_modf, k_ra8_spsr_mask_ovrf, k_ra8_spsr_mask_perf, k_ra8_spsr_mask_udrf, RA8_CHECK_NULL_PTR, ra8_spi(), s_tag, and r_spi_regs_t::SPSR.
Referenced by ra8_spi_dispatch_spei().
|
nodiscard |
Initialise an SPI channel with a full config descriptor.
| [in] | channel | SPI channel (0 or 1). |
| [in] | cfg | Configuration descriptor. |
Definition at line 348 of file ra8_spi_b.c.
References internal_spcr_controller(), internal_spi_program_regs(), k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_spi_b_channel_count, RA8_CHECK_NULL_PTR, ra8_log_info_val, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, ra8_spi(), s_spi_mstp_table, s_spi_state, s_tag, and r_spi_regs_t::SPCR.
Referenced by ep_bringup_panel_bus(), internal_setup_or_halt(), ra8_nsc_spi_init(), and ra8_spi_controller_init().
|
nodiscard |
Multi-frame RX-only polling transfer.
Mirrors FSP R_SPI_B_Read (delegates to r_spi_b_write_read_common with p_src = NULL). For each of len units the driver writes a dummy 0xFF / 0xFFFF / 0xFFFFFFFF to SPDR (matching common SD-card / SPI-flash idle patterns) and then captures one unit into rx.
| [in] | channel | SPI channel (0 or 1). |
| [out] | rx | Destination buffer (len units of bit_width); non-NULL. |
| [in] | len | Number of units to receive; 0 is a no-op success. |
| [in] | bit_width | Per-frame width: 8, 16, or 32 bits. |
| k_ra8_ok | Transfer completed. |
| k_ra8_err_null_ptr | rx is NULL with len > 0. |
| k_ra8_err_invalid_arg | Channel or bit_width invalid. |
| k_ra8_err_hw_timeout | Polling SPSR flag never asserted. |
Definition at line 711 of file ra8_spi_b.c.
References internal_xfer_common(), and k_ra8_err_null_ptr.
Referenced by ra8_nsc_spi_read().
|
nodiscard |
Kick off a DMA-backed RX transfer on an SPI channel.
Programmes the ra8_dma substrate to copy len bytes from the channel's SPDR register into out_buf[].
| [in] | channel | SPI channel. |
| [out] | out_buf | Destination buffer. Must outlive transfer. |
| [in] | len | Number of bytes; non-zero. |
| [in] | on_complete | Completion callback. May be NULL. |
| [in] | ctx | Context passed to on_complete. |
| [out] | out_dma_channel | Allocated DMAC channel on success. |
| k_ra8_ok | Transfer armed. |
| k_ra8_err_null_ptr | out_buf / out_dma_channel NULL. |
| k_ra8_err_invalid_arg | Channel or len invalid. |
| k_ra8_err_no_mem | All DMAC channels in use. |
| k_ra8_err_hw_error | ra8_dma_request failed. |
Definition at line 234 of file ra8_spi_b_dma.c.
References ra8_dma_request_t::count, ra8_dma_request_t::ctx, ra8_dma_request_t::dst_addr, ra8_dma_request_t::dst_inc, internal_dma_args_ok(), internal_spi_dma_rx_complete(), k_ra8_dmac_width_byte, k_ra8_err_invalid_arg, k_ra8_ok, ra8_dma_request_t::on_complete, RA8_CHECK_NULL_PTR, ra8_dma_request(), ra8_spi(), ra8_spi_dma_rx_ctx_t::rx_buf, ra8_spi_dma_rx_ctx_t::rx_len, s_spi_dma_rx_ctx, s_tag, r_spi_regs_t::SPDR, ra8_dma_request_t::src_addr, ra8_dma_request_t::src_inc, ra8_dma_request_t::trigger, ra8_spi_dma_rx_ctx_t::user_ctx, ra8_spi_dma_rx_ctx_t::user_on_complete, and ra8_dma_request_t::width.
|
nodiscard |
Change the SPI clock without tearing down the channel.
| [in] | channel | SPI channel. |
| [in] | baud_hz | Target bit-rate in Hz. |
| [in] | pclka_hz | Current PCLKA frequency. |
Definition at line 741 of file ra8_spi_b.c.
References internal_spbr(), k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_spcr3_bit_spbr, k_ra8_spcr3_mask_spbr, k_ra8_spi_b_channel_count, ra8_spi(), and r_spi_regs_t::SPCR3.
Referenced by internal_spi_b_set_clock().
|
nodiscard |
Multi-frame TX-only polling transfer.
Mirrors FSP R_SPI_B_Write (delegates to r_spi_b_write_read_common with p_dest = NULL). For each of len units the driver:
Polling is bounded so the driver cannot spin forever on a stuck bus (NASA P10 Rule 2). Default timeout budget tracks k_ra8_timeout_default_ms.
| [in] | channel | SPI channel (0 or 1). |
| [in] | tx | Source buffer (len units of bit_width); must be non-NULL. |
| [in] | len | Number of units to transfer; 0 is a no-op success. |
| [in] | bit_width | Per-frame width: 8, 16, or 32 bits. |
| k_ra8_ok | Transfer completed. |
| k_ra8_err_null_ptr | tx is NULL with len > 0. |
| k_ra8_err_invalid_arg | Channel or bit_width invalid. |
| k_ra8_err_hw_timeout | SPSR.SPTEF / SPSR.SPRF never asserted. |
Definition at line 703 of file ra8_spi_b.c.
References internal_xfer_common(), and k_ra8_err_null_ptr.
Referenced by ra8_nsc_spi_write().
|
nodiscard |
Kick off a DMA-backed TX transfer on an SPI channel.
Programmes the ra8_dma substrate to copy len bytes from data[] into the channel's SPDR register. The SPI block must be configured for 8-bit frames via the cfg passed to ra8_spi_init; wider-frame DMA streaming is a future wave.
| [in] | channel | SPI channel. |
| [in] | data | Source buffer. Must outlive transfer. |
| [in] | len | Number of bytes; non-zero. |
| [in] | on_complete | Completion callback. May be NULL. |
| [in] | ctx | Context passed to on_complete. |
| [out] | out_dma_channel | Allocated DMAC channel on success. |
| k_ra8_ok | Transfer armed. |
| k_ra8_err_null_ptr | data / out_dma_channel NULL. |
| k_ra8_err_invalid_arg | Channel or len invalid. |
| k_ra8_err_no_mem | All DMAC channels in use. |
| k_ra8_err_hw_error | ra8_dma_request failed. |
Definition at line 197 of file ra8_spi_b_dma.c.
References ra8_dma_request_t::count, ra8_dma_request_t::ctx, ra8_dma_request_t::dst_addr, ra8_dma_request_t::dst_inc, internal_dma_args_ok(), internal_round_up_to_cache_line(), k_ra8_dmac_width_byte, k_ra8_err_invalid_arg, k_ra8_ok, ra8_dma_request_t::on_complete, ra8_cache_dcache_clean_by_addr(), RA8_CHECK_NULL_PTR, ra8_dma_request(), ra8_spi(), s_tag, r_spi_regs_t::SPDR, ra8_dma_request_t::src_addr, ra8_dma_request_t::src_inc, ra8_dma_request_t::trigger, and ra8_dma_request_t::width.
|
nodiscard |
Multi-frame full-duplex polling transfer.
Mirrors FSP R_SPI_B_WriteRead (delegates to r_spi_b_write_read_common with both buffers non-NULL). Per unit: wait SPTEF -> push tx[i] into SPDR -> wait SPRF -> read SPDR into rx[i]. tx and rx may be the same buffer (in-place exchange).
| [in] | channel | SPI channel (0 or 1). |
| [in] | tx | Source buffer (len units of bit_width); non-NULL. |
| [out] | rx | Destination buffer (len units of bit_width); non-NULL. |
| [in] | len | Number of units to exchange; 0 is a no-op success. |
| [in] | bit_width | Per-frame width: 8, 16, or 32 bits. |
| k_ra8_ok | Transfer completed. |
| k_ra8_err_null_ptr | tx or rx is NULL with len > 0. |
| k_ra8_err_invalid_arg | Channel or bit_width invalid. |
| k_ra8_err_hw_timeout | Polling SPSR flag never asserted. |
Definition at line 719 of file ra8_spi_b.c.
References internal_xfer_common(), and k_ra8_err_null_ptr.
Referenced by internal_spi_b_write_read(), and ra8_nsc_spi_write_read().
|
nodiscard |
Full-duplex 8-bit exchange.
| [in] | channel | SPI channel (0 or 1). |
| [in] | tx | Byte to transmit. |
| [out] | rx | Pointer to receive the shifted-in byte (may be NULL). |
Definition at line 421 of file ra8_spi_b.c.
References internal_wait_spsr(), k_ra8_ok, k_ra8_spsr_mask_sprf, k_ra8_spsr_mask_sptef, k_ra8_spsrc_mask_sprfc, k_ra8_spsrc_mask_sptefc, RA8_CHECK_NULL_PTR, ra8_spi(), s_tag, r_spi_regs_t::SPDR, and r_spi_regs_t::SPSRC.
Referenced by internal_round_trip_ok(), internal_spi_b_xfer8(), and ra8_nsc_spi_xfer8().