summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/stm32/fdcan.c18
-rw-r--r--ports/stm32/pyb_can.c44
2 files changed, 45 insertions, 17 deletions
diff --git a/ports/stm32/fdcan.c b/ports/stm32/fdcan.c
index 63c6c3123..bcf3e8d6a 100644
--- a/ports/stm32/fdcan.c
+++ b/ports/stm32/fdcan.c
@@ -107,9 +107,7 @@ bool can_init(pyb_can_obj_t *can_obj, uint32_t mode, uint32_t prescaler, uint32_
// identifiers, and 31 x 2 words elements for 29-bit extended identifiers.
// The total number of words reserved for the filtering per FDCAN instance is 126 words.
init->StdFiltersNbr = 64;
- // Note extended identifiers are Not used in pyb_can.c and Not handled correctly.
- // Disable the extended identifiers filters for now until this is fixed properly.
- init->ExtFiltersNbr = 0 /*31*/;
+ init->ExtFiltersNbr = 31;
// The Tx event FIFO is used to store the message ID and the timestamp of successfully
// transmitted elements. The Tx event FIFO can store a maximum of 32 (2 words) elements.
@@ -189,7 +187,11 @@ bool can_init(pyb_can_obj_t *can_obj, uint32_t mode, uint32_t prescaler, uint32_
// Reset all filters
for (int f = 0; f < init->StdFiltersNbr; ++f) {
- can_clearfilter(can_obj, f, 0);
+ can_clearfilter(can_obj, f, false);
+ }
+
+ for (int f = 0; f < init->ExtFiltersNbr; ++f) {
+ can_clearfilter(can_obj, f, true);
}
can_obj->is_enabled = true;
@@ -250,10 +252,14 @@ void can_deinit(pyb_can_obj_t *self) {
}
}
-void can_clearfilter(pyb_can_obj_t *self, uint32_t f, uint8_t bank) {
+void can_clearfilter(pyb_can_obj_t *self, uint32_t f, uint8_t extid) {
if (self && self->can.Instance) {
FDCAN_FilterTypeDef filter = {0};
- filter.IdType = FDCAN_STANDARD_ID;
+ if (extid == 1) {
+ filter.IdType = FDCAN_EXTENDED_ID;
+ } else {
+ filter.IdType = FDCAN_STANDARD_ID;
+ }
filter.FilterIndex = f;
filter.FilterConfig = FDCAN_FILTER_DISABLE;
HAL_FDCAN_ConfigFilter(&self->can, &filter);
diff --git a/ports/stm32/pyb_can.c b/ports/stm32/pyb_can.c
index 0c5c60255..323b04d69 100644
--- a/ports/stm32/pyb_can.c
+++ b/ports/stm32/pyb_can.c
@@ -41,7 +41,6 @@
#if MICROPY_HW_ENABLE_FDCAN
-#define CAN_MAX_FILTER (64)
#define CAN_MAX_DATA_FRAME (64)
#define CAN_FIFO0 FDCAN_RX_FIFO0
@@ -87,9 +86,6 @@
#define __HAL_CAN_DISABLE_IT __HAL_FDCAN_DISABLE_IT
#define __HAL_CAN_CLEAR_FLAG __HAL_FDCAN_CLEAR_FLAG
#define __HAL_CAN_MSG_PENDING HAL_FDCAN_GetRxFifoFillLevel
-
-// Both banks start at 0
-STATIC uint8_t can2_start_bank = 0;
extern const uint8_t DLCtoBytes[16];
#else
@@ -715,16 +711,29 @@ STATIC mp_obj_t pyb_can_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_can_recv_obj, 1, pyb_can_recv);
// initfilterbanks(n)
-STATIC mp_obj_t pyb_can_initfilterbanks(mp_obj_t self, mp_obj_t bank_in) {
+STATIC mp_obj_t pyb_can_initfilterbanks(mp_obj_t self_in, mp_obj_t bank_in) {
+ pyb_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
#if MICROPY_HW_ENABLE_FDCAN
- can2_start_bank = 0;
+ (void)self;
+ #if 0
+ FDCAN_InitTypeDef *init = &self->can.Init;
+ // Clear standard ID filters.
+ for (int f = 0; f < init->StdFiltersNbr; ++f) {
+ can_clearfilter(self, f, false);
+ }
+ // Clear extended ID filters.
+ for (int f = 0; f < init->ExtFiltersNbr; ++f) {
+ can_clearfilter(self, f, true);
+ }
+ #endif
#else
+ // NOTE: For classic CAN, this function calls HAL_CAN_ConfigFilter(NULL, &filter);
+ // if CAN3 is defined, ConfigFilter() will dereference a NULL pointer.
can2_start_bank = mp_obj_get_int(bank_in);
- #endif
-
for (int f = 0; f < CAN_MAX_FILTER; f++) {
- can_clearfilter(MP_OBJ_TO_PTR(self), f, can2_start_bank);
+ can_clearfilter(self, f, can2_start_bank);
}
+ #endif
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_can_initfilterbanks_fun_obj, pyb_can_initfilterbanks);
@@ -733,10 +742,14 @@ STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pyb_can_initfilterbanks_obj, MP_ROM_PTR(&
STATIC mp_obj_t pyb_can_clearfilter(mp_obj_t self_in, mp_obj_t bank_in) {
pyb_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t f = mp_obj_get_int(bank_in);
+ #if MICROPY_HW_ENABLE_FDCAN
+ can_clearfilter(self, f, self->extframe);
+ #else
if (self->can_id == 2) {
f += can2_start_bank;
}
can_clearfilter(self, f, can2_start_bank);
+ #endif
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_can_clearfilter_obj, pyb_can_clearfilter);
@@ -760,9 +773,18 @@ STATIC mp_obj_t pyb_can_setfilter(size_t n_args, const mp_obj_t *pos_args, mp_ma
#if MICROPY_HW_ENABLE_FDCAN
FDCAN_FilterTypeDef filter = {0};
- filter.IdType = FDCAN_STANDARD_ID;
- // TODO check filter index
+ if (self->extframe) {
+ filter.IdType = FDCAN_EXTENDED_ID;
+ } else {
+ filter.IdType = FDCAN_STANDARD_ID;
+ }
+
filter.FilterIndex = args[ARG_bank].u_int;
+ // Check filter index.
+ if ((filter.IdType == FDCAN_STANDARD_ID && filter.FilterIndex >= self->can.Init.StdFiltersNbr) ||
+ (filter.IdType == FDCAN_EXTENDED_ID && filter.FilterIndex >= self->can.Init.ExtFiltersNbr)) {
+ goto error;
+ }
// Check filter mode
if (((args[ARG_mode].u_int != FDCAN_FILTER_RANGE) &&