Description

init_FiltID( num, ext, id ) – configures a message filter for a single ID
init_FiltID( num, ext, id, mask ) – configures a mask filter
This function defines a CAN messages filter for standard or extended ID messages. If used with 3 parameters ( num, ext, id) it will define the filter number num for a single filter ID. If used with four parameters ( num, ext, id, mask) the function defines a classic mask / id filter.

Syntax

MCAN0.init_FiltID( num, ext, id )
MCAN0.init_FiltID( num, ext, id, mask )

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 – message ID – 11 / 29bit identifier filter
uint32_t mask – (optional) – message ID mask. If specified, this value works together
with the parameter id to set up a ‘classic’ CAN mask-id filter

Returns

uint8_t:
CAN_OK – OK. Filter successfully configured.
CAN_FAILINIT – Error. Filter could not be configured. Check id is range (11 / 29 bit) and filter num is in the range see above.

Example Code

The following example receives messages on the CAN bus (MCAN0).

void setup()
{
  while(!Serial);
  Serial.println("Example filtering CAN messages");
  // Set Single Standard ID filter (re-config FILTER_0)
  MCAN0.init_FiltID(FILTER_0, 0 , 0x0A5);
  // Disable the pre-configured FILTER_EXT_0 to not receive all ext. msg. anymore
  MCAN0.disableFilter(FILTER_EXT_0, 1);
  // Add a Mask ID filter for all std. msg. ending with 0x5
  MCAN0.init_FiltID(FILTER_1, 0 , 0x005, 0x00F);
}

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 only 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 the CAN bus of dice devices
checkReceive()
init_FiltRang()
disableFilter()
defines
SAMC21_CAN – Library
CAN operation background
users manual
BOOTLOADER SETTINGS – Library
samc21 datasheet