Description
init_FiltRang( num, ext, id_low, id_high)
– configures the CAN-filter number num
to receive messages in the given range (standard or extended ID messages).
Syntax
MCAN0.init_FiltRang( num, ext, id_low, id_high)
Parameters
uint8_t num– filter number – Standard 11bit: FILTER_0
, FILTER_1
. . . FILTER_7
) or
– Extended 29bit: FILTER_EXT_0
, FILTER_EXT_1
. . . FILTER_EXT_7
)
uint8_t ext – extended ID – 0
for a Standard 11bit ID filter definition
– 1
for an Extended 29 bit ID filter definition
uint32_t id_low – message ID – 11 / 29bit identifier defining the start of the ID range (id_low included)
uint32_t id_high – message ID – 11 / 29bit identifier defining the end of the ID range (id_high included)
Returns
uint8_t:
CAN_OK – OK. Filter successfully configured.
CAN_FAILINIT – Error. Filter could not be configured. Check if id_low
< id_high
and filter num
in the range above.
Example Code
The following example receives messages on the CAN bus (MCAN0).
void setup()
{
while(!Serial);
Serial.println("Example filtering CAN messages");
// Add a Range filter of extended IDs
MCAN0.init_FiltRang(FILTER_EXT_0, 1 , 0x7000, 0x8000);
// Disable the pre-configured FILTER_0 to not receive all std. msg. anymore
MCAN0.disableFilter(FILTER_0, 0);
}
void loop()
{
if( MCAN0.checkReceive() )
{
CANMessage t_canMesgToRecv;
if(MCAN0.readMsgBuf( t_canMesgToRecv ) == CAN_OK)
Serial.println(t_canMesgToRecv.toString());
}
}
Notes and Warnings
At start-up, the filters FILTER_0 and FILTER_EXT_0 are pre-configured to receive all standard (FILTER_0) and all extended IDs (FILTER_EXT_0). Please, consider that if you edit one filter that the other one might still active.
Always use the given defines FILTER_x/ FILTER_EXT_x for the parameter num.
When configuring a filter for standard IDs use num: FILTER_x and set ext:0, if a filter for extended identifiers should be configured, then use num: FILTER_EXT_x and set ext:1.
Allowing all messages:
In order to let all messages be accepted again, just set two filters as follows. One (e.g. FILTER_0) for all standard IDs and another one for all extended IDs (e.g. FILTER_EXT_0):
init_FiltID(FILTER_0, 0, 0, MSG_ID_ALLOW_ALL_MASK);
init_FiltID(FILTER_EXT_0, 1, 0, MSG_ID_ALLOW_ALL_MASK);
Examples can be found in the Arduino IDE menu:
File -> Examples -> DICE -> CAN Bus ->
See also
Find more details about the CAN buses of dice devices
checkReceive()
defines
disableFilter()
init_FiltID()
SAMC21_CAN – Library
CAN operation background
users manual
BOOTLOADER SETTINGS – Library
samc21 datasheet