Skip to content

Commit e3b9814

Browse files
committed
canio: mimxrt10xx: Fix Listener filters/masks initialization
1 parent 7d302b9 commit e3b9814

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

ports/mimxrt10xx/common-hal/canio/CAN.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t *
267267
config.enableLoopBack = loopback;
268268
config.enableListenOnlyMode = silent;
269269
config.maxMbNum = 64;
270-
config.enableIndividMask = true;
270+
config.enableIndividMask = true; // required to enable matching using a 'Listener'
271271
// config.disableSelfReception = true; // TODO: do we want to disable this?
272272

273273
#if (defined(MIMXRT10XX_FLEXCAN_USE_IMPROVED_TIMING_CONFIG) && MIMXRT10XX_FLEXCAN_USE_IMPROVED_TIMING_CONFIG)
@@ -290,9 +290,6 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t *
290290
FLEXCAN_TransferCreateHandle(self->data->base, &self->data->handle, mimxrt10xx_flexcan_callback, (void*)self);
291291

292292
// Set rx mask to don't care on all bits.
293-
FLEXCAN_SetRxMbGlobalMask(self->data->base, FLEXCAN_RX_MB_EXT_MASK(0x00, 0, 0));
294-
FLEXCAN_SetRxFifoGlobalMask(self->data->base, FLEXCAN_RX_MB_EXT_MASK(0x00, 0, 0));
295-
296293
flexcan_rx_fifo_config_t fifo_config;
297294
fifo_config.idFilterNum = 0;
298295
fifo_config.idFilterTable = self->data->rx_fifo_filter;

ports/mimxrt10xx/common-hal/canio/Listener.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,39 @@ void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_o
5959

6060
self->can = can;
6161

62-
for (size_t i = 0; i < nmatch; i++) {
63-
if (matches[i]->extended) {
64-
self->can->data->rx_fifo_filter[i] = FLEXCAN_RX_FIFO_STD_FILTER_TYPE_A(matches[i]->id, 0, 1);
65-
} else {
66-
self->can->data->rx_fifo_filter[i] = FLEXCAN_RX_FIFO_STD_FILTER_TYPE_A(matches[i]->id, 0, 0);
67-
}
68-
}
69-
62+
// Init configuration variables
7063
flexcan_rx_fifo_config_t fifo_config;
7164
fifo_config.idFilterNum = nmatch;
7265
fifo_config.idFilterTable = self->can->data->rx_fifo_filter;
7366
fifo_config.idFilterType = kFLEXCAN_RxFifoFilterTypeA;
7467
fifo_config.priority = kFLEXCAN_RxFifoPrioHigh;
68+
69+
if (nmatch == 0) {
70+
// If the user has provided no matches, we need to set at least one
71+
// filter that instructs the system to ignore all bits.
72+
fifo_config.idFilterNum = 1;
73+
self->can->data->rx_fifo_filter[0] = 0x0;
74+
FLEXCAN_SetRxIndividualMask(self->can->data->base, 0, 0x0);
75+
}
76+
else {
77+
// Required to touch any CAN registers
78+
FLEXCAN_EnterFreezeMode(self->can->data->base);
79+
80+
for (size_t i = 0; i < nmatch; i++) {
81+
if (matches[i]->extended) {
82+
self->can->data->rx_fifo_filter[i] = FLEXCAN_RX_FIFO_EXT_FILTER_TYPE_A(matches[i]->id, 0, 1);
83+
self->can->data->base->RXIMR[i] = FLEXCAN_RX_FIFO_EXT_MASK_TYPE_A(matches[i]->mask, 0, 1);
84+
} else {
85+
self->can->data->rx_fifo_filter[i] = FLEXCAN_RX_FIFO_STD_FILTER_TYPE_A(matches[i]->id, 0, 0);
86+
self->can->data->base->RXIMR[i] = FLEXCAN_RX_FIFO_STD_MASK_TYPE_A(matches[i]->mask, 0, 0);
87+
}
88+
}
89+
90+
// For consistency, even though FLEXCAN_SetRxFifoConfig() below will
91+
// enter and exit freeze mode again anyway
92+
FLEXCAN_ExitFreezeMode(self->can->data->base);
93+
}
94+
7595
FLEXCAN_SetRxFifoConfig(self->can->data->base, &fifo_config, true);
7696
}
7797

ports/mimxrt10xx/common-hal/canio/__init__.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ typedef uint32_t canio_can_filter_t;
5858
// Convert from frame array index to tx message buffer index.
5959
#define MIMXRT10XX_FLEXCAN_TX_ARRID_TO_MBID(x) (x + MIMXRT10XX_FLEXCAN_TX_MBID_MIN)
6060

61-
// Limits the Canio module's Listener filter complexity.
61+
// We limit the amount of filter+mask pairs to 8 because above that the filters
62+
// are impacted by the global mask rather than individual masks alone, which is
63+
// not compatible with the current canio implementation.
64+
//
65+
// See Table 44-22 of the i.MX RT1060 Processor Reference Manual, Rev. 3
66+
// for more details.
6267
#define MIMXRT10XX_FLEXCAN_RX_FILTER_COUNT (8)
6368

6469
// Enables/disables SDK calculated "improved" timing configuration.

0 commit comments

Comments
 (0)