Description
add_IDtoFilt( num, ext, id )
– extends the filter num
settings to include the new ID id
Syntax
MCAN0.add_IDtoFilt( num, ext, id )
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
Returns
uint32_t:
returns the total number of IDs that are now covered by the setting of filter num
.
Example Code
The following example receives messages on the CAN bus (MCAN0
).
void setup()
{
while(!Serial);
Serial.println("Setting filters for CAN0");
// Disable the pre-configured FILTER_EXT_0 to not receive all msg. anymore
MCAN0.disableFilter(FILTER_EXT_0, 1);
// Set Single Standard ID filter (re-config FILTER_0)
// this call make FILTER_0 a mask filter (mask: 0x7FF = all bit matter)
MCAN0.init_FiltID(FILTER_0, 0 , 0x500); // FILTER_0 covers only 1 ID
// Now lets add ID 0x401 to FILTER_0
uint32_t u32_coveredIDs = MCAN0.add_IDtoFilt(FILTER_0, 0, 0x401);
Serial.println("FILTER_0 now includes: "+String(u32_coveredIDs)+" IDs");
}
void loop()
{
// Receive CAN messages
if(MCAN0.checkReceive())
{
CANMessage t_canMesgToRecv;
if(MCAN0.readMsgBuf(t_canMesgToRecv) == CAN_OK)
Serial.println("CAN0: "+t_canMesgToRecv.toString());
}
}
Notes and Warnings
Use this function with caution. The filter still only has one filter setting after calling add_IDtoFilt and might include more IDs than expected. The more IDs are included the more processing load would be caused by their evaluation in the application.
Lets assume that FILTER_0 was previously configured to cover a range from 0x700 to 0x705 (= 6 IDs) with init_FiltRang(FILTER_0, 0 , 0x700, 0x705).
Now when we add the ID 0x709 to this filter with add_IDtoFilt( FILTER_0, 0, 0x709) then the result would be that FILTER_0 covers the range from 0x700 to 0x709 (= 10 IDs). The same is true for ID / Mask filters. The more bits of the new ID have different to the previous one, the more ‘don’t care bits’ would be declared in the resulting Mask after the add_IDtoFilt call. This would result is *2 more covered IDs per Mask bits.
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 default filter 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.
Info: By adding a true at the end of the parameter list, the function will only calculate the total number of included IDs without acutally modifying the filter settings. e.g. uint32_t u32_coveredIDs = MCAN0.add_IDtoFilt(FILTER_0, 0, 0x401, true);
Allowing all messages:
In order to let all messages be accepted again, just set any 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()
getFilterType()
disableFilter()
init_FiltID()
init_FiltRang()
SAMC21_CAN – Library
CAN operation background
users manual
BOOTLOADER SETTINGS – Library
samc21 datasheet