Hello, I hope I'm at the right spot for my question.
I'm unable to get the mask and filter part to work.
I can send CAN frames, I can receive CAN frames, but I can't get the filter to work.
(Which also holds for the filter example)
Setup
I'm using an ATmega328 µC with the MCP2515 CAN Controller and an MCP2562 CAN Tranceiver.
Both the µC and the Controller run with a 16MHz crystal at 5V DC.
The CAN library is cloned from GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library and I'm on commit 870c43ea from 2017-03-24. (Edit 1; corrected commit date from 2017-03-17)
Arduino IDE is in Version 1.8.3 (for what it is worth).
THE MCP2515 is connected via SPI and the /INT signal has also been connected to pin 2.
I'm using two other MCP2515 & MCP2562 combinations on two Raspberry Pi's, one to transmit frames manually and one to monitor whats actually going over the wires.
The CAN interface has been configured with 'sudo ip link set can0 up type can bitrate 500000'
Idea
- for my little experiment the Arduino should only receive CAN frames
- only standard frames are being send
- each frame has a payload ≥ 4 byte
- the payload data has no meaning for the filter
- which frames get signaled to the µC depends upon the CAN ID
- for whichever reason only frames with the ID 0b001001xxxxx or 0b100001xxxxx shall be allowed
Problem
Frankly, I can't the filters to work; every frame gets accepted regardless of it's ID.
(by which I mean the /INT produces an interrupt which should not happen if the filters were working)
A bit more detail
I'm running 'candump any' on the second Raspberry Pi to monitor what is going over the wires.
Using the first Raspberry Pi 'cansend can0 120#DEADBEEF' should get accepted by the CAN controller.
I get the candump output 'can0 120 [4] DE AD BE EF' so sending went fine.
And indeed, the MCP2515 signals a new frame has been accepted via /INT.
Now for the second case, a non matching ID.
Using the first Raspberry Pi 'cansend can0 220#DEADBEEF' should NOT get accepted by the CAN controller.
I get the candump output 'can0 220 [4] DE AD BE EF' so sending went fine.
But why does the MCP2515 signal a new frame has been accepted?
Arduino Code
A lot has been omitted, see attachments for full code. (sorry if the horizontal layout is off)
I set the masks based upon the previous example in this thread.
...
byte _canState = CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ);
// 0b000|MSB...LSB|xxxxxxxxxxxxxxxxxx
CAN0.init_Mask(0, 0b00011111100000000000000000000000);
CAN0.init_Filt(0, 0b00000100100000000000000000000000); // allow data type 1 for Node 1 (ID = 0x12.)
CAN0.init_Filt(1, 0b00010000100000000000000000000000); // allow data type 2 for Node 1 (ID = 0x42.)
// 0b000|MSB...LSB|xxxxxxxxxxxxxxxxxx
CAN0.init_Mask(1, 0b00011111111111000000000000000000); // since we don't need them, set all bits
CAN0.init_Filt(2, 0b00011111111111000000000000000000); // to 1 which is a frame that will never
CAN0.init_Filt(3, 0b00011111111111000000000000000000); // occur in this application
CAN0.init_Filt(4, 0b00011111111111000000000000000000); // *
CAN0.init_Filt(5, 0b00011111111111000000000000000000); // *
...
Conclusion
I have no Idea why the filters aren't working.
My C is bad enough, from what I could understand the library should do its job just fine. The register addresses for the MCP2515 given in 'mcp_can_dfs.h' check out to the data sheet. The mode gets switch to config and back…
I have no Idea which simple little thing I have overlooked.
If somebody is be able to point me into the right direction would be welcome
MCP-Filter.ino (2.12 KB)