ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_canfd_afl.c
Go to the documentation of this file.
1
33
34#include <stdint.h>
35
36#include "ra8_attributes.h"
37#include "ra8_canfd.h"
38#include "ra8_canfd_regs.h"
39#include "ra8_check.h"
40#include "ra8_err.h"
41
42static const char* s_tag = "CANFD";
43
57typedef enum : uint32_t {
60
87{
88 if (rule->target_rx >= (uint8_t)k_ra8_canfd_rx_fifo_count) {
90 }
91 if (rule->extended) {
92 if ((rule->id & ~(uint32_t)k_ra8_canfd_id_ext_mask) != 0U) {
94 }
95 if ((rule->mask & ~(uint32_t)k_ra8_canfd_id_ext_mask) != 0U) {
97 }
98 return k_ra8_ok;
99 }
100 if ((rule->id & ~(uint32_t)k_ra8_canfd_id_std_mask) != 0U) {
102 }
103 if ((rule->mask & ~(uint32_t)k_ra8_canfd_id_std_mask) != 0U) {
105 }
106 return k_ra8_ok;
107}
108
131static uint32_t internal_afl_id_word(const ra8_canfd_afl_rule_t* rule)
132{
133 const uint32_t masked = rule->extended ? (rule->id & (uint32_t)k_ra8_canfd_id_ext_mask)
134 : (rule->id & (uint32_t)k_ra8_canfd_id_std_mask);
135 uint32_t word = masked;
136 if (rule->extended) {
137 word |= (uint32_t)k_ra8_canfd_id_ide;
138 }
139 if (rule->rtr) {
140 word |= (uint32_t)k_ra8_canfd_id_rtr;
141 }
142 return word;
143}
144
169{
170 const uint32_t masked = rule->extended ? (rule->mask & (uint32_t)k_ra8_canfd_id_ext_mask)
171 : (rule->mask & (uint32_t)k_ra8_canfd_id_std_mask);
172 return masked | (uint32_t)k_ra8_gaflm_bit_gaflidem | (uint32_t)k_ra8_gaflm_bit_gaflrtrm;
173}
174
198static uint32_t internal_afl_p1_word(const ra8_canfd_afl_rule_t* rule)
199{
200 return (uint32_t)k_ra8_gaflp1_bit_gaflfdp0 << rule->target_rx;
201}
202
224static void
225internal_afl_write_slot(volatile r_canfd_t* reg, uint8_t slot, const ra8_canfd_afl_rule_t* rule)
226{
227 /* HUM Ch 41.2.19 "CFDGAFLID : Acceptance Filter List ID Register" p 2735 */
228 reg->CFDGAFL[slot].ID = internal_afl_id_word(rule);
229 /* HUM Ch 41.2.20 "CFDGAFLM : Acceptance Filter List ID Mask Register" p 2736 */
230 reg->CFDGAFL[slot].M = internal_afl_mask_word(rule);
231 /* HUM Ch 41.2.21 "CFDGAFLP0 : Acceptance Filter List Pointer 0 Register" p 2738 */
232 reg->CFDGAFL[slot].P0 = 0U;
233 /* HUM Ch 41.2.22 "CFDGAFLP1 : Acceptance Filter List Pointer 1 Register" p 2740 */
234 reg->CFDGAFL[slot].P1 = internal_afl_p1_word(rule);
235}
236
257static void internal_afl_set_rule_count(volatile r_canfd_t* reg, uint8_t count)
258{
259 /* HUM Ch 41.2.18 "CFDGAFLCFG0 : Global AFL Configuration Register 0" p 2735 */
260 const uint32_t cfg0 = reg->CFDGAFLCFG0;
261 const uint32_t rnc = ((uint32_t)count & k_ra8_gaflcfg0_mask_rnc0) << k_ra8_gaflcfg0_shift_rnc0;
262 /* HUM Ch 41.2.18 "CFDGAFLCFG0 : Global AFL Configuration Register 0" p 2735 */
264}
265
291{
292 for (uint8_t i = 0U; i < count; i++) {
293 const ra8_err_t v = internal_afl_validate_rule(&rules[i]);
294 if (v != k_ra8_ok) {
295 return v;
296 }
297 }
298 return k_ra8_ok;
299}
300
302ra8_canfd_set_accept_filter(uint8_t channel, const ra8_canfd_afl_rule_t* rules, uint8_t count)
303{
304 RA8_CHECK_NULL_PTR(rules, s_tag, "rules must not be nullptr");
305 volatile r_canfd_t* reg = ra8_canfd(channel);
306 RA8_CHECK_NULL_PTR(reg, s_tag, "channel out of range");
307 if (count == 0U) {
309 }
310 if (count > (uint8_t)k_ra8_canfd_afl_rule_capacity) {
312 }
313
314 const ra8_err_t v = internal_afl_validate_all(rules, count);
315 if (v != k_ra8_ok) {
316 return v;
317 }
318
319 /* Unlock the AFL data window and select page 0 (AFLPN + AFLDAE). */
320 /* HUM Ch 41.2.17 "CFDGAFLECTR : AFL Entry Control Register" p 2734 */
321 reg->CFDGAFLECTR =
323
324 internal_afl_set_rule_count(reg, count);
325
326 for (uint8_t i = 0U; i < count; i++) {
327 internal_afl_write_slot(reg, i, &rules[i]);
328 }
329
330 /* Re-lock the AFL data window (clear AFLDAE). */
331 /* HUM Ch 41.2.17 "CFDGAFLECTR : AFL Entry Control Register" p 2734 */
332 reg->CFDGAFLECTR = 0U;
333 return k_ra8_ok;
334}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
CANFD Lite driver (bit-timing, TX, RX, error state).
@ k_ra8_canfd_afl_rule_capacity
One 16-entry AFL page (page 0).
Definition ra8_canfd.h:238
static uint32_t internal_afl_id_word(const ra8_canfd_afl_rule_t *rule)
Assemble the CFDGAFLID word: accept ID plus IDE / RTR flags.
static uint32_t internal_afl_p1_word(const ra8_canfd_afl_rule_t *rule)
Assemble the CFDGAFLP1 word: route a match into RX FIFO target_rx.
static uint32_t internal_afl_mask_word(const ra8_canfd_afl_rule_t *rule)
Assemble the CFDGAFLM word: ID mask plus IDE / RTR compare-enables.
static void internal_afl_set_rule_count(volatile r_canfd_t *reg, uint8_t count)
Set CFDGAFLCFG0.RNC0 to count so the AFL consults count rules.
static void internal_afl_write_slot(volatile r_canfd_t *reg, uint8_t slot, const ra8_canfd_afl_rule_t *rule)
Write one CFDGAFL slot (ID / M / P0 / P1) from a rule.
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.
static ra8_err_t internal_afl_validate_all(const ra8_canfd_afl_rule_t *rules, uint8_t count)
Validate every rule in rules before any register is touched.
static ra8_err_t internal_afl_validate_rule(const ra8_canfd_afl_rule_t *rule)
Range-check one ra8_canfd_afl_rule_t against the protocol limits.
ra8_canfd_afl_local_t
Local constants owned by the AFL list-config path.
@ k_ra8_canfd_afl_page0
AFLPN page index used by this API.
CANFD controller register layout for the Renesas RA8D2.
@ k_ra8_gaflp1_bit_gaflfdp0
Route to RX FIFO 0.
@ k_ra8_canfd_rx_fifo_count
CFDRFCC / CFDRFSTS array length.
@ k_ra8_canfd_id_std_mask
11-bit standard ID.
@ k_ra8_canfd_id_rtr
RTR: remote frame.
@ k_ra8_canfd_id_ext_mask
29-bit extended ID.
@ k_ra8_canfd_id_ide
IDE: extended flag.
@ k_ra8_gaflectr_mask_aflpn
RA8 gaflectr mask aflpn.
@ k_ra8_gaflectr_bit_afldae
RA8 gaflectr bit afldae.
static volatile r_canfd_t * ra8_canfd(uint8_t channel)
Get pointer to CANFD instance N (0..1).
@ k_ra8_gaflcfg0_mask_rnc0
RNC0 field is 5 bits.
@ k_ra8_gaflcfg0_shift_rnc0
Position of RNC0[4:0].
@ k_ra8_gaflm_bit_gaflidem
IDE-compare mask bit.
@ k_ra8_gaflm_bit_gaflrtrm
RTR-compare mask bit.
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
volatile uint32_t P1
+0x00C GAFLFDP.
volatile uint32_t M
+0x004 GAFLIDM + GAFLIFL1 + GAFLRTRM + IDEM.
volatile uint32_t P0
+0x008 GAFLDLC + GAFLIFL0 + GAFLRMDP + ...
volatile uint32_t ID
+0x000 GAFLID + GAFLLB + GAFLRTR + GAFLIDE.
volatile uint32_t CFDGAFLECTR
CFDGAFLECTR register (offset 0x028).
r_canfd_cfdgafl_t CFDGAFL[k_ra8_canfd_afl_page_size]
CFDGAFL register (offset 0x120 (0x100)).
volatile uint32_t CFDGAFLCFG0
CFDGAFLCFG0 register (offset 0x02C).
One Acceptance-Filter-List (AFL) rule: hardware ID/mask match plus an RX-FIFO routing target.
Definition ra8_canfd.h:265
uint32_t id
Right-aligned accept ID (11- or 29-bit).
Definition ra8_canfd.h:266
uint8_t target_rx
Destination RX FIFO index (0..1).
Definition ra8_canfd.h:270
uint32_t mask
ID bit mask: 1 = must match, 0 = don't care.
Definition ra8_canfd.h:267
bool extended
true = 29-bit extended ID, false = 11-bit std.
Definition ra8_canfd.h:268
bool rtr
true = match remote frames, false = data frames.
Definition ra8_canfd.h:269