Match: address -> id

This commit is contained in:
Jeff Epler 2020-09-24 13:58:45 -05:00
parent 8b82c239b8
commit 79ca430ddf
5 changed files with 60 additions and 60 deletions

View File

@ -58,8 +58,8 @@ STATIC void static_assertions(void) {
MP_STATIC_ASSERT(CAN_XIDFE_0_EFEC_STF0M_Val + 1 == CAN_XIDFE_0_EFEC_STF1M_Val);
}
STATIC bool single_address_filter(canio_match_obj_t *match) {
return match->mask == 0 || match->mask == match->address;
STATIC bool single_id_filter(canio_match_obj_t *match) {
return match->mask == 0 || match->mask == match->id;
}
STATIC bool standard_filter_in_use(CanMramSidfe *filter) {
@ -76,7 +76,7 @@ STATIC size_t num_filters_needed(size_t nmatch, canio_match_obj_t **matches, boo
if (extended != matches[i]->extended) {
continue;
}
if (single_address_filter(matches[i])) {
if (single_id_filter(matches[i])) {
num_half_filters_needed += 1;
} else {
num_half_filters_needed += 2;
@ -191,7 +191,7 @@ STATIC void install_extended_filter(CanMramXidfe *extended, int id1, int id2, in
}
#define NO_ADDRESS (-1)
#define NO_ID (-1)
void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **matches) {
int fifo = self->fifo_idx;
@ -207,31 +207,31 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
CanMramSidfe *standard = next_standard_filter(self, NULL);
CanMramXidfe *extended = next_extended_filter(self, NULL);
int first_address = NO_ADDRESS;
int first_id = NO_ID;
// step 1: single address standard matches
// step 1: single id standard matches
// we have to gather up pairs and stuff them in a single filter entry
for(size_t i = 0; i<nmatch; i++) {
canio_match_obj_t *match = matches[i];
if (match->extended) {
continue;
}
if (!single_address_filter(match)) {
if (!single_id_filter(match)) {
continue;
}
if (first_address != NO_ADDRESS) {
install_standard_filter(standard, first_address, match->address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
first_address = NO_ADDRESS;
if (first_id != NO_ID) {
install_standard_filter(standard, first_id, match->id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
first_id = NO_ID;
standard = next_standard_filter(self, standard);
} else {
first_address = match->address;
first_id = match->id;
}
}
// step 1.5. odd single address standard match
if (first_address != NO_ADDRESS) {
install_standard_filter(standard, first_address, first_address, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
// step 1.5. odd single id standard match
if (first_id != NO_ID) {
install_standard_filter(standard, first_id, first_id, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_DUAL_Val);
standard = next_standard_filter(self, standard);
first_address = NO_ADDRESS;
first_id = NO_ID;
}
// step 2: standard mask filter
@ -240,36 +240,36 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
if (match->extended) {
continue;
}
if (single_address_filter(match)) {
if (single_id_filter(match)) {
continue;
}
install_standard_filter(standard, match->address, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val);
install_standard_filter(standard, match->id, match->mask, CAN_SIDFE_0_SFEC_STF0M_Val + fifo, CAN_SIDFE_0_SFT_CLASSIC_Val);
standard = next_standard_filter(self, standard);
}
// step 3: single address extended matches
// step 3: single id extended matches
// we have to gather up pairs and stuff them in a single filter entry
for(size_t i = 0; i<nmatch; i++) {
canio_match_obj_t *match = matches[i];
if (!match->extended) {
continue;
}
if (!single_address_filter(match)) {
if (!single_id_filter(match)) {
continue;
}
if (first_address != NO_ADDRESS) {
install_extended_filter(extended, first_address, match->address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
first_address = NO_ADDRESS;
if (first_id != NO_ID) {
install_extended_filter(extended, first_id, match->id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
first_id = NO_ID;
extended = next_extended_filter(self, extended);
} else {
first_address = match->address;
first_id = match->id;
}
}
// step 3.5. odd single address standard match
if (first_address != NO_ADDRESS) {
install_extended_filter(extended, first_address, first_address, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
// step 3.5. odd single id standard match
if (first_id != NO_ID) {
install_extended_filter(extended, first_id, first_id, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_DUAL_Val);
extended = next_extended_filter(self, extended);
first_address = NO_ADDRESS;
first_id = NO_ID;
}
// step 4: extended mask filters
@ -278,10 +278,10 @@ void set_filters(canio_listener_obj_t *self, size_t nmatch, canio_match_obj_t **
if (!match->extended) {
continue;
}
if (single_address_filter(match)) {
if (single_id_filter(match)) {
continue;
}
install_extended_filter(extended, match->address, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val);
install_extended_filter(extended, match->id, match->mask, CAN_XIDFE_0_EFEC_STF0M_Val + fifo, CAN_XIDFE_1_EFT_CLASSIC_Val);
extended = next_extended_filter(self, extended);
}
@ -363,7 +363,7 @@ bool common_hal_canio_listener_readinto(canio_listener_obj_t *self, canio_messag
if (message->extended) {
message->id = hw_message->rxf0.bit.ID;
} else {
message->id = hw_message->rxf0.bit.ID >> 18; // short addresses are left-justified
message->id = hw_message->rxf0.bit.ID >> 18; // short ids are left-justified
}
message->rtr = hw_message->rxf0.bit.RTR;
message->size = hw_message->rxf1.bit.DLC;

View File

@ -33,19 +33,19 @@
//| """Describe CAN bus messages to match"""
//|
//|
//| def __init__(self, address: int, *, mask: int = 0, extended: bool = False):
//| def __init__(self, id: int, *, mask: int = 0, extended: bool = False):
//| """Construct a Match with the given properties.
//|
//| If mask is nonzero, then the filter is for any sender which matches all
//| the nonzero bits in mask. Otherwise, it matches exactly the given address.
//| If extended is true then only extended addresses are matched, otherwise
//| only standard addresses are matched."""
//| If mask is nonzero, then the filter is for any id which matches all
//| the nonzero bits in mask. Otherwise, it matches exactly the given id.
//| If extended is true then only extended ids are matched, otherwise
//| only standard ids are matched."""
//|
STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_address, ARG_mask, ARG_extended, NUM_ARGS };
enum { ARG_id, ARG_mask, ARG_extended, NUM_ARGS };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_mask, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
};
@ -54,44 +54,44 @@ STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, c
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
int address_bits = args[ARG_extended].u_bool ? 0x1fffffff : 0x7ff;
int address = args[ARG_address].u_int;
int id_bits = args[ARG_extended].u_bool ? 0x1fffffff : 0x7ff;
int id = args[ARG_id].u_int;
int mask = args[ARG_mask].u_int;
if (address & ~address_bits) {
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_address);
if (id & ~id_bits) {
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_id);
}
if (mask & ~address_bits) {
if (mask & ~id_bits) {
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_mask);
}
canio_match_obj_t *self = m_new_obj(canio_match_obj_t);
self->base.type = &canio_match_type;
common_hal_canio_match_construct(self, args[ARG_address].u_int, args[ARG_mask].u_int, args[ARG_extended].u_bool);
common_hal_canio_match_construct(self, args[ARG_id].u_int, args[ARG_mask].u_int, args[ARG_extended].u_bool);
return self;
}
//| address: int
//| """The address to match"""
//| id: int
//| """The id to match"""
//|
STATIC mp_obj_t canio_match_address_get(mp_obj_t self_in) {
STATIC mp_obj_t canio_match_id_get(mp_obj_t self_in) {
canio_match_obj_t *self = self_in;
return MP_OBJ_NEW_SMALL_INT(common_hal_canio_match_get_address(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_canio_match_get_id(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(canio_match_address_get_obj, canio_match_address_get);
MP_DEFINE_CONST_FUN_OBJ_1(canio_match_id_get_obj, canio_match_id_get);
const mp_obj_property_t canio_match_address_obj = {
const mp_obj_property_t canio_match_id_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&canio_match_address_get_obj,
.proxy = {(mp_obj_t)&canio_match_id_get_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//|
//| mask: int
//| """The optional mask of addresses to match"""
//| """The optional mask of ids to match"""
//|
STATIC mp_obj_t canio_match_mask_get(mp_obj_t self_in) {
@ -108,7 +108,7 @@ const mp_obj_property_t canio_match_mask_obj = {
};
//| extended: bool
//| """True to match extended addresses, False to match standard addresses"""
//| """True to match extended ids, False to match standard ides"""
//|
STATIC mp_obj_t canio_match_extended_get(mp_obj_t self_in) {
@ -125,7 +125,7 @@ const mp_obj_property_t canio_match_extended_obj = {
};
STATIC const mp_rom_map_elem_t canio_match_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&canio_match_address_obj) },
{ MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_match_id_obj) },
{ MP_ROM_QSTR(MP_QSTR_mask), MP_ROM_PTR(&canio_match_mask_obj) },
{ MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_match_extended_obj) },
};

View File

@ -31,7 +31,7 @@
extern const mp_obj_type_t canio_match_type;
void common_hal_canio_match_construct(canio_match_obj_t *self, int address, int mask, bool extended);
int common_hal_canio_match_get_address(const canio_match_obj_t *self);
void common_hal_canio_match_construct(canio_match_obj_t *self, int id, int mask, bool extended);
int common_hal_canio_match_get_id(const canio_match_obj_t *self);
int common_hal_canio_match_get_mask(const canio_match_obj_t *self);
bool common_hal_canio_match_get_extended(const canio_match_obj_t *self);

View File

@ -26,14 +26,14 @@
#include "shared-module/canio/Match.h"
void common_hal_canio_match_construct(canio_match_obj_t *self, int address, int mask, bool extended) {
self->address = address;
void common_hal_canio_match_construct(canio_match_obj_t *self, int id, int mask, bool extended) {
self->id = id;
self->mask = mask;
self->extended = extended;
}
int common_hal_canio_match_get_address(const canio_match_obj_t *self) {
return self->address;
int common_hal_canio_match_get_id(const canio_match_obj_t *self) {
return self->id;
}
int common_hal_canio_match_get_mask(const canio_match_obj_t *self) {
return self->mask;

View File

@ -30,7 +30,7 @@
typedef struct {
mp_obj_base_t base;
int address;
int id;
int mask;
bool extended;
} canio_match_obj_t;