Canbus ID filter and mask
CANBUS is a two-wire, half-duplex, bus based LAN system that is ‘collision free’. Data is BROADCAST onto the bus -THERE IS NO SUCH THNG AS A POINT TO POINT CONNECTION as with data LANs. All nodes receive all broadcast data and decide whether or not that data is relevant.
A CANBUS B frame consists of a four byte header (containing a 29-bit identifier), followed by up to 8 data bytes.
A receiving node would examine the identifier to decide if it was relevant (e.g. waiting for a frame with ID 00001567 which contains data to switch on or off a motor). It could do this via software (using a C if or case statement); in practice the Canbus interface contains firmware to carry out this task using the acceptance filter and mask value to filter out unwanted messages.
The filter mask is used to determine which bits in the identifier of the received frame are compared with the filter
If a mask bit is set to a zero, the corresponding ID bit will automatically be accepted, regardless of the value of the filter bit.
If a mask bit is set to a one, the corresponding ID bit will be compare with the value of the filter bit; if they match it is accepted otherwise the frame is rejected.
Example 1. we wish to accept only frames with ID of 00001567 (hexadecimal values)
set filter to 00001567
set mask to 1FFFFFFF
when a frame arrives its ID is compared with the filter and all bits must match; any frame that does not match ID 00001567 is rejected
Example 2. we wish to accept only frames with IDs of 00001560 thru to 0000156F
set filter to 00001560
set mask to 1FFFFFF0
when a frame arrives its ID is compared with the filter and all bits except bits 0 to 3 must match; any frame other frame is rejected
Example 3. we wish to accept only frames with IDs of 00001560 thru to 00001567
set filter to 00001560
set mask to 1FFFFFF8
when a frame arrives its ID is compared with the filter and all bits except bits 0 to 2 must match; any frame other frame is rejected
Example 4. we wish to accept any frame
set filter to 0
set mask to 0
all frames are accepted
In practice Canbus interfaces tends to have a number of filters and masks so combinations of IDs can be accepted, e.g. a module that carries out a number of different tasks.