Canbus ID Filter and mask

Source: Internet
Author: User

Canbus ID Filter and Mask

CANBUS is a two-wire, Half-duplex, bus based LAN System that's ' collision free '.

Data is broadcast onto the bus-there are NO SUCH thng as A Point-to-point CONNECTION as with data LANs.

All nodes receive any broadcast data and decide whether or not, data is relevant.

A CANBUS B frame consists of a four byte headers (containing a 29-bit identifier), followed by up to 8 data bytes.

A receiving node would examine the identifier to decide if it is relevant (e.g. waiting for a frame with ID 00001567 whic H contains data to switch in or off a motor).

It could do the 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 is compared with the filter

    • If a mask bit is set to a zero, the corresponding ID bit would automatically be accepted, regardless of the value of the fi Lter bit.

    • If a mask bit is set to a one, the corresponding ID bit would be compare with the value of the filter bit;
      If they match it is accepted otherwise, the frame is rejected.  

A Filter Matches, when received_can_id & Mask = = can_id & Mask--mask=1: Do care Bits

Example 1. we wish to accept only frames with ID of 00001567 (hexadecimal values)

    • Set Filter to 00001567

    • set Mask to 1FFFFFFF --- every Bit must match filter

When a frame arrives it ID is compared with the filter and all bits must match; an any frame this does not match ID 0000 1567 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 Low 4 Bits dont care

When a frame arrives it ID is compared with the filter and all bits except bits 0 to 3 must match; AME 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 Low 3 Bits dont care

When a frame arrives it ID is compared with the filter and all bits except bits 0 to 2 must match; AME is rejected

Example 4. we wish to accept Any frame

    • Set Filter to 0

    • set Mask to 0 --- every Bits dont care

All frames is accepted

In practice Canbus interfaces tends to has a number of filters and masks so combinations of IDs can be accepted,

e.g. a module, carries out a number of different tasks.//mask bit n | filter bit n | message ID bit n | result

 //  Mask Filter ID  //  //  1 0 0 Accept  //  1 0 1 reject  //  1 1 0 reject  //  1 1 1 accept  

Since This filtering is the in -hardware it is fairly primitive. Usually the calculation involves the registers a mask and a filter. The equivalent logic in C would is:

/  *  DsPIC style; mask Specifies " do Care" bits  *  /if((Arbitrationid & mask) = =filter) {    /*Message accepted; Rx Interrupt triggered*/}/*Accept All*/Mask=0; filter=0;/*Accept CANopen Default connection set (excluding SYNC and NMT)*/Mask=0x7F; filter= node_id;

/  *  SJA-style; mask Specifies " do not care" bits *  /if((Arbitrationid & ~mask) = =filter) {    /*Message accepted; Rx Interrupt triggered*/}/*Accept All*/Mask= ~0; filter=0;/*Accept CANopen Default connection set (excluding SYNC and NMT)*/Mask= ~0x7F; filter= node_id;
4.1 RAW protocol sockets with Can_filters (SOCK_RAW) Using Can_raw sockets are extensively comparable to the commonly kno WN access to CAN character devices. To meet the new possibilities provided by the multi user Socketcan approach, some reasonable defaults is set at RAW Soc  Ket binding time:-The filters is set to exactly one filter receiving everything-the socket only receives valid data  Frames (= = no error message frames)-the loopback of sent CAN frames is enabled (see Chapter 3.2)-The socket does Not receive it own sent frames (in loopback mode) These default settings could be changed before or after binding the SOC  Ket.  To use the referenced definitions of the socket options for Can_raw sockets, include <linux/can/raw.h>.  4.1.1 RAW socket Option can_raw_filter The reception of can frames using Can_raw sockets CAN be controlled by defining 0 ..  n filters with the can_raw_filter socket option. The CAN filter structure is defined in Include/linux/can.h:   struct Can_filter {canid_t can_id;    canid_t Can_mask; }; A filter matches, when <received_can_id> & mask = = can_id & Mask    Which is analogous to known CAN controllers hardware filter semantics. The filter can be inverted-semantic, when the Can_inv_filter bit is set in can_id element of the Can_filter Struc Ture. In contrast to CAN controller hardware filters the user may set 0.    n Receive filters for each open socket separately:struct can_filter rfilter[2];    rfilter[0].can_id = 0x123;    Rfilter[0].can_mask = Can_sff_mask;    rfilter[1].can_id = 0x200;    Rfilter[1].can_mask = 0x700;  SetSockOpt (S, Sol_can_raw, Can_raw_filter, &rfilter, sizeof (Rfilter));  To disable the reception of CAN frames on the selected Can_raw socket:setsockopt (S, Sol_can_raw, Can_raw_filter, NULL,  0); To set the filters to zero filters are quite obsolete as to not read data causes the raw sockets to discard the received CA N frames. But have this ' send only ' use-case we could remove the receive list in the Kernel to save a little (really a very little !)  CPU usage. 4.1.1.1 CAN Filter Usage OptimisAtion The can filters is processed in per-device filter lists at CAN frame reception time. To reduce the number of checks this need to being performed while walking through the filter lists the CAN core provides an  Optimized filter handling when the filter subscription focusses to a single CAN ID. For the possible 2048 SFF CAN identifiers The identifier is used as a index to access the corresponding subscription Lis  T without any further checks.  For the 2^29 possible eff CAN identifiers a ten bit XOR folding is used as hash function to retrieve the EFF table index. To benefit from the optimized filters for single CAN identifiers the Can_sff_mask or can_eff_mask has the to is set into CA N_filter.mask together with set Can_eff_flag and Can_rtr_flag bits. A set Can_eff_flag bit in the can_filter.mask makes clear that it matters whether a SFF or EFF CAN ID is subscribed.    e.g. in the example from above rfilter[0].can_id = 0x123;  Rfilter[0].can_mask = Can_sff_mask; Both SFF FRames with can ID 0x123 and EFF frames with 0xxxxxx123 can pass. To filter for only 0x123 (SFF) and 0x12345678 (EFF) CAN identifiers the filter have to is defined in this-to benefit F    Rom the optimized filters:struct can_filter rfilter[2];    rfilter[0].can_id = 0x123; Rfilter[0].can_mask = (Can_eff_flag | Can_rtr_flag |    Can_sff_mask); rfilter[1].can_id = 0x12345678 |    Can_eff_flag; Rfilter[1].can_mask = (Can_eff_flag | Can_rtr_flag |    Can_eff_mask);  SetSockOpt (S, Sol_can_raw, Can_raw_filter, &rfilter, sizeof (Rfilter)); 4.1.2 RAW socket option Can_raw_err_filter as described in Chapter 3.4 The can interface driver can generate so called E Rror Message Frames that can optionally is passed to the user application in the same-as other can Frames. The possible errors is divided into different error classes, may be filtered using the appropriate error mask. To register for every possible error condition Can_err_mask CAN is used as value for the error Mask.    The values for the error mask is defined in Linux/can/error.h. can_err_mask_t err_mask = (Can_err_tx_timeout |    Can_err_busoff); SetSockOpt (S, Sol_can_raw, Can_raw_err_filter, &err_mask, sizeof (Err_mask));

Canbus ID Filter and mask

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.