MCP2515 CAN Filter/Mask

Guys - i apologize ahead of time for the fact that MANY posts have been made about this. It must be a mental block or something because i have banged my head for hours against a wall >:( >:( . I need help defining the masks and filters for a can network.... I am using coryjfowler Library. This is as far as i have gotten....

CAN0.init_Mask(0,0,0x03FE0000);
CAN0.init_Filt(0,0,0x00350000);
CAN0.init_Filt(1,0,0x00930000);

CAN0.init_Mask(1,0,0x03FA0000);
CAN0.init_Filt(2,0,0x00400000);
CAN0.init_Filt(3,0,0x01200000);
CAN0.init_Filt(4,0,0x00800000);
CAN0.init_Filt(5,0,0x00800000);

the messages i NEED to allow through are as follows (ID's) - aka i want to receive any of these messages:

CAN $79
CAN $93
CAN $35
CAN $77
CAN $12B
CAN $3E0
CAN $2A4
CAN $2F2
CAN $291
CAN $3E1
CAN $9FE
CAN $28B
CAN $3E4
CAN $3E3
CAN $3E8
CAN $3E9
CAN $3EA
CAN $332
CAN $3EB
CAN $2B1
CAN $2D0
CAN $2AD
CAN $283
CAN $289
CAN $2C5
CAN $26F
CAN $265
CAN $381
CAN $279
CAN $2A1
CAN $27B
CAN $27A
CAN $762

Now for the interesting part. this Bus is super busy - many many different ID's. I dont think i can block them all, but i ranked the ones that I want to block the "most" for the sake of speed in processing the in-buffer...

CAN $23 1
CAN $2F 2
CAN $25 3
CAN $27 4
CAN $7D 5
CAN $9B 6
CAN $91 7
CAN $AD 8
CAN $7B 9
CAN $85 10
CAN $83 11
CAN $81 12
CAN $7F 13
CAN $71 14
CAN $9D 15
CAN $A7 16
CAN $95 17
CAN $AB 18
CAN $8D 19
CAN $B3 20
CAN $87 21
CAN $8B 22
CAN $89 23
CAN $B5 24
CAN $B1 25
CAN $DD 26
CAN $1CE 27
CAN $13F 28
CAN $133 29
CAN $13D 30
CAN $125 31
CAN $121 32
CAN $143 33
CAN $157 34
CAN $14E 35
CAN $318 36
CAN $129 37
CAN $137 38
CAN $14D 39
CAN $127 40
CAN $15B 41
CAN $128 42
CAN $3C7 43
CAN $1C0 44
CAN $37
CAN $2B
CAN $296
CAN $1C8
CAN $400
CAN $230
CAN $248
CAN $401
CAN $1A1
CAN $257
CAN $1D0
CAN $402
CAN $2A7
CAN $403
CAN $1F7
CAN $407
CAN $3AA
CAN $1F6
CAN $339
CAN $3AB
CAN $1EB
CAN $1E9
CAN $3AC
CAN $25D
CAN $38A
CAN $409
CAN $3A9
CAN $38E
CAN $3AD
CAN $25F
CAN $39E
CAN $3B3
CAN $3F0
CAN $44A
CAN $3B4
CAN $3F1
CAN $44C
CAN $3F2
CAN $3F7
CAN $3AE
CAN $3D8
CAN $3F3
CAN $423
CAN $3D7
CAN $3F4
CAN $22D
CAN $3F5
CAN $350
CAN $3D5
CAN $358
CAN $3D3
CAN $3D4
CAN $3D6
CAN $3AF
CAN $35C
CAN $3C9
CAN $3F6
CAN $75E
CAN $4DE
CAN $277
CAN $259
CAN $3CD
CAN $744
CAN $4C4
CAN $4E2
CAN $740
CAN $4C0
CAN $763
CAN $4E3
CAN $7E1
CAN $7E9
CAN $743
CAN $4C3
CAN $620
CAN $504
CAN $74B
CAN $4CB
CAN $7E0
CAN $7E8
CAN $75A
CAN $4DA
CAN $747
CAN $4C7
CAN $749
CAN $4C9
CAN $4C2

so just to keep it simple - message "1" is the most common message - which i dont need - so i want to mask it out. 2 is the next most common message i want to mask as so forth.

any help would be very very very helpful.

thanks again -
D

1 Like

There are two independent sets of mask/filters.
Set 0 has a mask and 2 filters labelled 0 and 1.
Set 1 has a mask and 4 filters labelled 2, 3, 4 and 5.

Each filter is a pattern of bits that need to match the address in the CAN packet.
The mask indicates which bits in the associated filters are currently active. Therefore, the effective behaviour of a filter is set by the combination of the bit pattern in the mask and bit pattern in the filter. This can be confusing as a valid filter can be rendered ineffective by an incorrect mask.
It's probably easiest to think of the mask/filter combination as a way of accepting rather than rejecting packets.

When dealing with 29 bit addresses the init_Mask and init_Filt function need the 'ext' flag to be set.

INT8U MCP_CAN::init_Mask(INT8U num, INT8U ext, INT32U ulData)
INT8U MCP_CAN::init_Filt(INT8U num, INT8U ext, INT32U ulData)

Looking at your code

CAN0.init_Mask(0,0,0x03FE0000);               
CAN0.init_Filt(0,0,0x00350000);               
CAN0.init_Filt(1,0,0x00930000);

Firstly you have the ext flag set to zero which indicates 11 instead of 29 bit addressing, but the mask you have set has 26 bits and the filters have 22 and 24 bits.

Mask        11111111100000000000000000
First filter    1101010000000000000000
Second filter 100100110000000000000000

If you line up the mask and filters, counting from position zero on the right you will see that both filters have bit 16 rendered ineffective as the mask has this set to zero.

To begin to fix this mess first you need to figure out if you are dealing with 11 or 29 bit addressing.

Once you have basics sorted refer to this article Message filtering in CAN | Microchip for examples of masks and filters.

I would be surprised if the limited filtering abilities of the MCP2515 is going to be able to filter much of your list as there is a big overlap in IDs and therefore there will be much commonality in the bit patterns between the wanted and unwanted IDs.

1 Like