@@ -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
0 commit comments