8000 stm32/pyb_can: Replace CAN.initfilterbanks with CAN.init keyword arg. · micropython/micropython@5562ed3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5562ed3

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32/pyb_can: Replace CAN.initfilterbanks with CAN.init keyword arg.
The CAN.initfilterbanks() class method is removed, and its functionality is replaced with the "num_filter_banks" keyword argumen 8000 t to the CAN constructor and CAN.init(). This configures the filter bank split. This new approach provides more flexibility configuring the resources used by a given CAN instance, allowing other MCUs like H7 to fit the API. It also brings CAN closer to how other machine peripherals are configured, where everything is done in the constructor/init method. This is a breaking change to the CAN API.
1 parent a79706f commit 5562ed3

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

ports/stm32/pyb_can.c

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ STATIC void pyb_can_get_bit_timing(mp_uint_t baudrate, mp_uint_t sample_point,
217217
// init(mode, prescaler=100, *, sjw=1, bs1=6, bs2=8)
218218
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) {
219219
enum { ARG_mode, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2, ARG_auto_restart, ARG_baudrate, ARG_sample_point,
220-
ARG_brs_prescaler, ARG_brs_sjw, ARG_brs_bs1, ARG_brs_bs2, ARG_brs_baudrate, ARG_brs_sample_point };
220+
ARG_num_filter_banks, ARG_brs_prescaler, ARG_brs_sjw, ARG_brs_bs1, ARG_brs_bs2, ARG_brs_baudrate, ARG_brs_sample_point };
221221
static const mp_arg_t allowed_args[] = {
222222
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = CAN_MODE_NORMAL} },
223223
{ MP_QSTR_prescaler, MP_ARG_INT, {.u_int = CAN_DEFAULT_PRESCALER} },
@@ -227,6 +227,7 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp
227227
{ MP_QSTR_auto_restart, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
228228
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
229229
{ MP_QSTR_sample_point, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 75} }, // 75% sampling point
230+
{ MP_QSTR_num_filter_banks, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 14} },
230231
#if MICROPY_HW_ENABLE_FDCAN
231232
{ MP_QSTR_brs_prescaler, MP_ARG_INT, {.u_int = CAN_DEFAULT_PRESCALER} },
232233
{ MP_QSTR_brs_sjw, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = CAN_DEFAULT_SJW} },
@@ -267,6 +268,12 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp
267268
self->can.Init.DataSyncJumpWidth = args[ARG_brs_sjw].u_int;
268269
self->can.Init.DataTimeSeg1 = args[ARG_bs1].u_int; // DataTimeSeg1 = Propagation_segment + Phase_segment_1
269270
self->can.Init.DataTimeSeg2 = args[ARG_bs2].u_int;
271+
#else
272+
// Init filter banks for classic CAN.
273+
can2_start_bank = args[ARG_num_filter_banks].u_int;
274+
for (int f = 0; f < CAN_MAX_FILTER; f++) {
275+
can_clearfilter(self, f, can2_start_bank);
276+
}
270277
#endif
271278

272279
if (!can_init(self, args[ARG_mode].u_int, args[ARG_prescaler].u_int, args[ARG_sjw].u_int,
@@ -726,35 +733,6 @@ STATIC mp_obj_t pyb_can_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
726733
}
727734
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_can_recv_obj, 1, pyb_can_recv);
728735

729-
// initfilterbanks(n)
730-
STATIC mp_obj_t pyb_can_initfilterbanks(mp_obj_t self_in, mp_obj_t bank_in) {
731-
pyb_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
732-
#if MICROPY_HW_ENABLE_FDCAN
733-
(void)self;
734-
#if 0
735-
FDCAN_InitTypeDef *init = &self->can.Init;
736-
// Clear standard ID filters.
737-
for (int f = 0; f < init->StdFiltersNbr; ++f) {
738-
can_clearfilter(self, f, false);
739-
}
740-
// Clear extended ID filters.
741-
for (int f = 0; f < init->ExtFiltersNbr; ++f) {
742-
can_clearfilter(self, f, true);
743-
}
744-
#endif
745-
#else
746-
// NOTE: For classic CAN, this function calls HAL_CAN_ConfigFilter(NULL, &filter);
747-
// if CAN3 is defined, ConfigFilter() will dereference a NULL pointer.
748-
can2_start_bank = mp_obj_get_int(bank_in);
749-
for (int f = 0; f < CAN_MAX_FILTER; f++) {
750-
can_clearfilter(self, f, can2_start_bank);
751-
}
752-
#endif
753-
return mp_const_none;
754-
}
755-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_can_initfilterbanks_fun_obj, pyb_can_initfilterbanks);
756-
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pyb_can_initfilterbanks_obj, MP_ROM_PTR(&pyb_can_initfilterbanks_fun_obj));
757-
758736
STATIC mp_obj_t pyb_can_clearfilter(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
759737
enum { ARG_extframe };
760738
static const mp_arg_t allowed_args[] = {
@@ -1006,7 +984,6 @@ STATIC const mp_rom_map_elem_t pyb_can_locals_dict_table[] = {
1006984
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&pyb_can_any_obj) },
1007985
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&pyb_can_send_obj) },
1008986
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&pyb_can_recv_obj) },
1009-
{ MP_ROM_QSTR(MP_QSTR_initfilterbanks), MP_ROM_PTR(&pyb_can_initfilterbanks_obj) },
1010987
{ MP_ROM_QSTR(MP_QSTR_setfilter), MP_ROM_PTR(&pyb_can_setfilter_obj) },
1011988
{ MP_ROM_QSTR(MP_QSTR_clearfilter), MP_ROM_PTR(&pyb_can_clearfilter_obj) },
1012989
{ MP_ROM_QSTR(MP_QSTR_rxcallback), MP_ROM_PTR(&pyb_can_rxcallback_obj) },

0 commit comments

Comments
 (0)
0