Skip to content

ports/stm32/can.c: allow mixed id-type bus #3916

@adritium

Description

@adritium

Original issue here on forum

Most CAN buses can handle messages with both extended- and standard-id but the current API does not allow mixed id-type bus.

STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_mode, ARG_extframe, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2, ARG_auto_restart };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = CAN_MODE_NORMAL} },
{ MP_QSTR_extframe, MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_prescaler, MP_ARG_INT, {.u_int = 100} },
{ MP_QSTR_sjw, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_bs1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 6} },
{ MP_QSTR_bs2, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
{ MP_QSTR_auto_restart, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
};
// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
self->extframe = args[ARG_extframe].u_bool;

There could be hardware modules on the bus with varying degree of tolerance for even the presence of extended IDs:

Concerning the compatibility of standard and extended frames three types of CAN controllers exist:
˚ CAN controllers according to CAN protocol specification vers. 2.0 A, which are able to receive
and transmit standard frames
˚ CAN controllers according to CAN protocol specification vers. 2.0 B, which are able to receive
and transmit standard frames and to tolerate extended frames without detecting an error
(’extended frame passive’)
˚ CAN controllers according to CAN protocol specification vers. 2.0 B, which are able to receive
and transmit standard and extended frames (’extended frame active’).

While it does make sense to say this can bus can only accept standard-id, it doesn't make sense to say this can bus can only accept extended id because if you can accept extended then you can also accept standard.

Therefore, the flag in CAN.init() should be changed to indicate whether or not the bus allows extended ID and CAN.send() should be modified to accept a flag to indicate standard vs extended ID and throw if an extended-message is attempted to be sent on a standard-only bus.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions