|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
CANFD Lite driver (bit-timing, TX, RX, error state). More...
Go to the source code of this file.
Data Structures | |
| struct | ra8_canfd_frame_t |
| A single CAN / CAN-FD frame as seen by the driver public API. More... | |
| struct | ra8_canfd_afl_rule_t |
| One Acceptance-Filter-List (AFL) rule: hardware ID/mask match plus an RX-FIFO routing target. More... | |
| struct | ra8_canfd_tdc_cfg_t |
| Transmitter Delay Compensation (TDC) configuration for one CANFD channel. More... | |
Typedefs | |
| typedef void(* | ra8_canfd_event_fn_t) (void *ctx, uint8_t channel, uint32_t status_mask) |
| CANFD event callback. | |
Enumerations | |
| enum | ra8_canfd_afl_capacity_t : uint8_t { k_ra8_canfd_afl_rule_capacity = 16U } |
| Per-channel Acceptance-Filter-List capacity used by ra8_canfd_set_accept_filter. More... | |
Functions | |
| ra8_err_t | ra8_canfd_init (uint8_t channel) |
| Take a CANFD channel out of reset into operation mode. | |
| ra8_err_t | ra8_canfd_deinit (uint8_t channel) |
| Put the CANFD channel back into reset. | |
| ra8_err_t | ra8_canfd_set_bitrate (uint8_t channel, uint32_t bitrate_bps, uint32_t data_bitrate_bps) |
| Programme nominal (and optional data) bit-timing registers. | |
| ra8_err_t | ra8_canfd_transmit (uint8_t channel, const ra8_canfd_frame_t *frame) |
| Queue a frame into the TX message buffer and trigger transmission. | |
| ra8_err_t | ra8_canfd_receive (uint8_t channel, ra8_canfd_frame_t *out_frame) |
| Poll the RX FIFO for the next available frame. | |
| ra8_err_t | ra8_canfd_get_error_state (uint8_t channel, uint8_t *tx_err, uint8_t *rx_err) |
| Read the TX and RX error counters. | |
| ra8_err_t | ra8_canfd_get_status (uint8_t channel, uint32_t *out_mask) |
| Read the channel status register (CFDCnSTS). | |
| ra8_err_t | ra8_canfd_clear_status (uint8_t channel, uint32_t mask) |
| Clear error flags in CFDCnERFL. | |
| ra8_err_t | ra8_canfd_attach_handler (ra8_canfd_event_fn_t fn, void *ctx) |
| Attach a callback for CANFD events (shared across channels). | |
| void | ra8_canfd_dispatch (uint8_t channel) |
| Dispatch a CANFD event for a channel – snapshot + fire callback. | |
| ra8_err_t | ra8_canfd_filter_set (uint16_t filter_id, uint32_t accept_id, uint32_t mask, uint8_t dlc) |
| Programme one Global Acceptance-Filter-List (GAFL) entry. | |
| ra8_err_t | ra8_canfd_set_accept_filter (uint8_t channel, const ra8_canfd_afl_rule_t *rules, uint8_t count) |
| Program the channel Acceptance-Filter-List from a rule array. | |
| ra8_err_t | ra8_canfd_set_brs (uint8_t channel, uint32_t fast_bitrate) |
| Configure the CAN-FD bit-rate-switch (BRS) data-phase rate. | |
| ra8_err_t | ra8_canfd_set_tdc (uint8_t channel, const ra8_canfd_tdc_cfg_t *cfg) |
| Configure Transmitter Delay Compensation on a CANFD channel. | |
| ra8_err_t | ra8_canfd_set_iso_mode (bool enable) |
| Select ISO 11898-1 vs non-ISO CAN-FD framing. | |
| ra8_err_t | ra8_canfd_set_test_mode (uint8_t channel, ra8_ctms_mode_t mode) |
| Enable a CFDC[0].CTR test mode (basic / listen-only / loopback). | |
| ra8_err_t | ra8_canfd_enter_stop (uint8_t channel) |
| Put the CANFD channel into MSTP-gated stop. | |
| ra8_err_t | ra8_canfd_exit_stop (uint8_t channel) |
| Exit MSTP-gated stop. | |
CANFD Lite driver (bit-timing, TX, RX, error state).
Minimal driver surface over the RA8D2 CANFD Lite controller – enough to bring a channel up at a chosen nominal bit rate, queue a CAN 2.0B / CAN-FD frame into the TX message buffer, poll the RX FIFO for inbound frames, and read the TX/RX error counters.
DMA hooks, interrupt delivery, and the full acceptance-filter bank are deferred until there is a real consumer that needs them.
Definition in file ra8_canfd.h.
| typedef void(* ra8_canfd_event_fn_t) (void *ctx, uint8_t channel, uint32_t status_mask) |
CANFD event callback.
Definition at line 155 of file ra8_canfd.h.
| enum ra8_canfd_afl_capacity_t : uint8_t |
Per-channel Acceptance-Filter-List capacity used by ra8_canfd_set_accept_filter.
ra8_canfd_set_accept_filter installs its rules into a single 16-entry AFL page (page 0). The CFDGAFLECTR.AFLPN page selector is 4 bits and each page exposes k_ra8_canfd_afl_page_size entries; one page is the working set the list-config API supports, so the rule count is capped here. HUM Ch 41.2.18 "CFDGAFLCFG0 : Global AFL Configuration Register 0" p 2735 (RNC field) and Ch 41.2.17 "CFDGAFLECTR" p 2734.
| Enumerator | |
|---|---|
| k_ra8_canfd_afl_rule_capacity | One 16-entry AFL page (page 0). |
Definition at line 237 of file ra8_canfd.h.
|
nodiscard |
Attach a callback for CANFD events (shared across channels).
Definition at line 585 of file ra8_canfd.c.
References k_ra8_ok, s_canfd_ctx, and s_canfd_fn.
|
nodiscard |
Clear error flags in CFDCnERFL.
Definition at line 574 of file ra8_canfd.c.
References r_canfd_t::CFDC, r_canfd_cfdc_t::ERFL, k_ra8_ok, ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
|
nodiscard |
Put the CANFD channel back into reset.
| [in] | channel | Channel index (0..1). |
Definition at line 546 of file ra8_canfd.c.
References k_ra8_chmdc_reset, k_ra8_ok, priv_ra8_canfd_internal_set_channel_mode(), ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
| void ra8_canfd_dispatch | ( | uint8_t | channel | ) |
Dispatch a CANFD event for a channel – snapshot + fire callback.
See the matching header declaration for the full contract; this site adds no behaviour beyond what the public API documents.
| [in] | channel | See header declaration for direction and constraints. |
Definition at line 593 of file ra8_canfd.c.
References r_canfd_t::CFDC, r_canfd_cfdc_t::ERFL, ra8_canfd(), s_canfd_ctx, and s_canfd_fn.
|
nodiscard |
Put the CANFD channel into MSTP-gated stop.
Definition at line 777 of file ra8_canfd.c.
References k_ra8_canfd_instance_count, k_ra8_err_invalid_arg, ra8_mstp_disable(), and s_canfd_mstp_table.
|
nodiscard |
Exit MSTP-gated stop.
Definition at line 786 of file ra8_canfd.c.
References k_ra8_canfd_instance_count, k_ra8_err_invalid_arg, ra8_mstp_enable(), and s_canfd_mstp_table.
|
nodiscard |
Programme one Global Acceptance-Filter-List (GAFL) entry.
Selects the AFL page that contains filter_id (each page holds 16 entries) by writing CFDGAFLECTR.AFLPN with the page index and unlocking the data window with CFDGAFLECTR.AFLDAE. Then writes CFDGAFL[idx].ID with the accept ID, CFDGAFL[idx].M with the bit mask, and CFDGAFL[idx].P1 with the DLC field used to gate fast RX-FIFO routing. See HUM Ch 41 "CFDGAFLECTR / CFDGAFL" pp 2702-2867.
| [in] | filter_id | Filter index (0..k_ra8_canfd_afl_total - 1). |
| [in] | accept_id | Raw arbitration ID to accept (11- or 29-bit). |
| [in] | mask | Bit mask – bits clear are "don't care". |
| [in] | dlc | DLC code packed into CFDGAFL.P1 (0..15). |
| k_ra8_ok | Filter slot programmed. |
| k_ra8_err_invalid_arg | filter_id, accept_id, or dlc out of range. |
Definition at line 689 of file ra8_canfd.c.
References r_canfd_t::CFDGAFLECTR, internal_bump_rnc0_locked(), internal_enable_rx_fifo0(), internal_set_global_mode(), internal_write_afl_slot(), k_ra8_canfd_afl_per_page, k_ra8_canfd_afl_total, k_ra8_canfd_dlc_max, k_ra8_canfd_id_ext_mask, k_ra8_err_invalid_arg, k_ra8_gaflectr_bit_afldae, k_ra8_gaflectr_mask_aflpn, k_ra8_gctr_value_operation, k_ra8_gctr_value_reset, k_ra8_ok, ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
Referenced by internal_canfd_filter_program_slots().
|
nodiscard |
Read the TX and RX error counters.
| [in] | channel | Channel index (0..1). |
| [out] | tx_err | TEC counter value. Must not be NULL. |
| [out] | rx_err | REC counter value. Must not be NULL. |
Definition at line 350 of file ra8_canfd_frame.c.
References r_canfd_t::CFDC, k_ra8_cnsts_bit_rec, k_ra8_cnsts_bit_tec, k_ra8_cnsts_mask_rec, k_ra8_cnsts_mask_tec, k_ra8_ok, ra8_canfd(), RA8_CHECK_NULL_PTR, s_tag, and r_canfd_cfdc_t::STS.
|
nodiscard |
Read the channel status register (CFDCnSTS).
Definition at line 564 of file ra8_canfd.c.
References r_canfd_t::CFDC, k_ra8_ok, ra8_canfd(), RA8_CHECK_NULL_PTR, s_tag, and r_canfd_cfdc_t::STS.
|
nodiscard |
Take a CANFD channel out of reset into operation mode.
| [in] | channel | Channel index (0..1). |
Definition at line 510 of file ra8_canfd.c.
References r_canfd_t::CFDGSTS, internal_canfd_clock_block_init(), internal_canfd_open_channel(), k_ra8_canfd_spin, k_ra8_err_invalid_arg, k_ra8_gsts_bit_graminit, k_ra8_ok, ra8_canfd(), RA8_CHECK_NULL_PTR, ra8_log_info_val, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, s_canfd_mstp_table, and s_tag.
Referenced by internal_can_demo_setup_or_halt(), internal_canfd_demo_setup_or_halt(), internal_canfd_filter_setup_or_halt(), and main().
|
nodiscard |
Poll the RX FIFO for the next available frame.
Non-blocking: on an empty FIFO the call returns k_ra8_err_no_data immediately. On success the frame is popped (by writing the pointer control register) before returning.
| [in] | channel | Channel index (0..1). |
| [out] | out_frame | Destination frame. Must not be NULL. |
Definition at line 326 of file ra8_canfd_frame.c.
References r_canfd_t::CFDRF, r_canfd_t::CFDRFPCTR, r_canfd_t::CFDRFSTS, r_canfd_cfdrf_t::FDSTS, r_canfd_cfdrf_t::ID, internal_decode_rx_header(), internal_read_rx_data(), k_ra8_canfd_rx_fifo_default, k_ra8_err_no_data, k_ra8_ok, k_ra8_rfpctr_value_ack, k_ra8_rfsts_bit_empty, r_canfd_cfdrf_t::PTR, ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
Referenced by internal_can_demo_one_round_trip(), internal_canfd_demo_one_round_trip(), internal_canfd_filter_one_round(), internal_thread_rx_entry(), and main().
|
nodiscard |
Program the channel Acceptance-Filter-List from a rule array.
Installs count hardware acceptance rules into AFL page 0 for channel, replacing the single pass-all rule that ra8_canfd_init installs. The routine follows the documented AFL edit transaction exactly:
For a rule, IDE and RTR are added to the match (CFDGAFLM.GAFLIDEM / GAFLRTRM) so extended and rtr are honoured, and CFDGAFLP1 routes the match into RX FIFO rule.target_rx.
| [in] | channel | CANFD channel index (0..1). |
| [in] | rules | Array of count rules. Must not be NULL. |
| [in] | count | Number of rules, 1..k_ra8_canfd_afl_rule_capacity. |
| k_ra8_ok | All count rules programmed and re-locked. |
| k_ra8_err_null_ptr | channel out of range or rules NULL. |
| k_ra8_err_invalid_arg | count is zero or above capacity, a rule's id/mask overflows the std/ext range, or a rule's target_rx is out of range. |
rules points to at least count valid rule descriptors. count and the first count CFDGAFL slots reflect the supplied rules. Definition at line 302 of file ra8_canfd_afl.c.
References r_canfd_t::CFDGAFLECTR, internal_afl_set_rule_count(), internal_afl_validate_all(), internal_afl_write_slot(), k_ra8_canfd_afl_page0, k_ra8_canfd_afl_rule_capacity, k_ra8_err_invalid_arg, k_ra8_gaflectr_bit_afldae, k_ra8_gaflectr_mask_aflpn, k_ra8_ok, ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
|
nodiscard |
Programme nominal (and optional data) bit-timing registers.
Uses PCLKA as the CAN source clock (HUM default). Targets a 75% sample point (TSEG1=15, TSEG2=4, SJW=min(4,TSEG2)) and rejects any bit rate that does not resolve to an integer prescaler in 1..256.
| [in] | channel | Channel index (0..1). |
| [in] | bitrate_bps | Nominal-phase bit rate in bits per second. |
| [in] | data_bitrate_bps | Data-phase bit rate (for CAN-FD). Set to bitrate_bps (or 0) for classic-only. |
Definition at line 196 of file ra8_canfd_timing.c.
References r_canfd_t::CFDC, r_canfd_t::CFDC2, r_canfd_cfdc2_t::DCFG, internal_pack_dcfg(), internal_pack_ncfg(), internal_solve_timing(), k_ra8_canfd_data_prescaler_max, k_ra8_canfd_prescaler_max, k_ra8_chmdc_operation, k_ra8_chmdc_reset, k_ra8_clock_id_pclka, k_ra8_ok, r_canfd_cfdc_t::NCFG, priv_ra8_canfd_internal_set_channel_mode(), ra8_canfd(), ra8_cgc_get_clock_hz(), RA8_CHECK_NULL_PTR, ra8_log_info_val, and s_tag.
Referenced by internal_can_demo_setup_or_halt(), internal_canfd_demo_setup_or_halt(), internal_canfd_filter_setup_or_halt(), and main().
|
nodiscard |
Configure the CAN-FD bit-rate-switch (BRS) data-phase rate.
Re-runs the data-phase timing solver against PCLKA and updates CFDC2[0].DCFG so that subsequent BRS frames sent on channel use fast_bitrate during the payload phase. See HUM Ch 41 "CFDCnDCFG" pp 2702-2867.
| [in] | channel | Channel index (0..1). |
| [in] | fast_bitrate | Data-phase bit rate in bits per second. |
| k_ra8_ok | DCFG updated. |
| k_ra8_err_null_ptr | channel out of range. |
| k_ra8_err_invalid_arg | fast_bitrate is zero or unsolvable. |
channel. fast_bitrate after BRS bit.Definition at line 296 of file ra8_canfd_timing.c.
References internal_program_data_phase(), ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
|
nodiscard |
Select ISO 11898-1 vs non-ISO CAN-FD framing.
Writes CFDGFDCFG.NISO – when the bit is clear the controller uses the ISO 11898-1 stuff-count + CRC; when set it falls back to the original Bosch non-ISO framing. See HUM Ch 41 "CFDGFDCFG" pp 2702-2867.
| [in] | enable | true -> ISO mode, false -> non-ISO mode. |
| k_ra8_ok | Mode bit updated. |
enable. Definition at line 761 of file ra8_canfd.c.
References r_canfd_t::CFDGFDCFG, k_ra8_gfdcfg_bit_niso, k_ra8_ok, ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
|
nodiscard |
Configure Transmitter Delay Compensation on a CANFD channel.
Writes the TDE, TDCOC, and TDCO fields in CFDCnFDCFG to enable or disable the transmitter delay compensation block, select the offset source (hardware-measured vs. manual), and program the TDCO offset value. All non-TDC bits (FDOE, REFE, CLOE, ESIC, EOCCFG) are preserved by a read-modify-write.
The write is bracketed by a CH_RESET / CH_OPERATION mode transition (mirrors the ra8_canfd_set_bitrate contract: FDCFG is only writable in CH_RESET or CH_HALT mode, per HUM Ch 41 "CFDCnFDCFG" p 2788). On a transition timeout the function returns the timeout error and the channel is left in the failing mode; the caller must call ra8_canfd_deinit before attempting recovery.
| [in] | channel | CANFD channel index (0..1). |
| [in] | cfg | TDC configuration descriptor. Must not be NULL. cfg->offset must be <= k_ra8_canfd_tdc_offset_max. |
| k_ra8_ok | CFDCnFDCFG updated; channel back in CH_OPERATION. |
| k_ra8_err_null_ptr | channel is out of range or cfg is NULL. |
| k_ra8_err_invalid_arg | cfg->offset exceeds the 7-bit TDCO cap (127). |
| k_ra8_err_hw_timeout | Channel mode transition stalled. |
channel. cfg. Configure Transmitter Delay Compensation on a CANFD channel.
Definition at line 37 of file ra8_canfd_tdc.c.
References r_canfd_t::CFDC2, ra8_canfd_tdc_cfg_t::enable, r_canfd_cfdc2_t::FDCFG, k_ra8_canfd_tdc_offset_max, k_ra8_chmdc_operation, k_ra8_chmdc_reset, k_ra8_err_invalid_arg, k_ra8_fdcfg_mask_tdco, k_ra8_fdcfg_mask_tdcoc, k_ra8_fdcfg_mask_tde, k_ra8_fdcfg_shift_tdco, k_ra8_ok, ra8_canfd_tdc_cfg_t::manual, ra8_canfd_tdc_cfg_t::offset, priv_ra8_canfd_internal_set_channel_mode(), ra8_canfd(), RA8_CHECK_NULL_PTR, ra8_log_info, and s_tag.
|
nodiscard |
Enable a CFDC[0].CTR test mode (basic / listen-only / loopback).
Stamps CTME (bit 24) and CTMS[1:0] (bits [26:25]) in CFDC[0].CTR. Per HUM Ch 41 "CFDCnCTR" p 2710 these bits are only writable while the channel is in CH_HALT mode, so the helper:
The internal-loopback mode (k_ra8_ctms_self_test_1, 11b) is the documented way to validate the CANFD IP without a transceiver – the controller routes every TX frame straight to its own RX acceptance filter (HUM Ch 41 "Self-test mode 1 (Internal Loopback mode)" p 2710).
| [in] | channel | Channel index (0..1). |
| [in] | mode | Desired CTMS selector (ra8_ctms_mode_t). |
| k_ra8_ok | Test mode latched, channel back in operation. |
| k_ra8_err_invalid_arg | channel or mode out of range. |
| k_ra8_err_hw_timeout | Halt/operation status bit never asserted. |
channel. channel. mode. Definition at line 731 of file ra8_canfd.c.
References r_canfd_t::CFDC, r_canfd_cfdc_t::CTR, k_ra8_chmdc_halt, k_ra8_chmdc_operation, k_ra8_cnctr_bit_ctms, k_ra8_cnctr_mask_ctme, k_ra8_cnctr_mask_ctms, k_ra8_ctms_self_test_1, k_ra8_err_invalid_arg, k_ra8_ok, priv_ra8_canfd_internal_set_channel_mode(), ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
Referenced by internal_can_demo_enable_internal_loopback(), internal_canfd_demo_enable_internal_loopback(), internal_canfd_filter_loopback(), and main().
|
nodiscard |
Queue a frame into the TX message buffer and trigger transmission.
Fire-and-forget: the routine validates every field, writes the ID / DLC / FD-status / payload into the TX message-buffer registers, then asserts TMTR in CFDTMC. No completion callback is raised.
| [in] | channel | Channel index (0..1). |
| [in] | frame | Frame descriptor. Must not be NULL. |
Definition at line 235 of file ra8_canfd_frame.c.
References r_canfd_t::CFDTM, r_canfd_t::CFDTMC, r_canfd_t::CFDTMSTS, ra8_canfd_frame_t::dlc, r_canfd_cfdtm_t::FDCTR, r_canfd_cfdtm_t::ID, internal_tx_fdctr(), internal_tx_id(), internal_validate_frame(), internal_wait_tx_complete(), internal_write_tx_data(), k_ra8_canfd_ptr_mask_dlc, k_ra8_canfd_ptr_shift_dlc, k_ra8_canfd_tmc_txreq, k_ra8_canfd_tx_mb_default, k_ra8_ok, r_canfd_cfdtm_t::PTR, ra8_canfd(), RA8_CHECK_NULL_PTR, and s_tag.
Referenced by internal_can_demo_one_round_trip(), internal_canfd_demo_one_round_trip(), internal_canfd_filter_one_round(), and internal_thread_tx_entry().