CAN ID hardware acceptance filtering

jrveale
Posts: 9
Joined: Mon Apr 01, 2019 9:30 am

CAN ID hardware acceptance filtering

Postby jrveale » Fri Feb 26, 2021 3:35 pm

I'm having issues correctly setting the CAN acceptance filter to accept only messages with a particular ID.

Say for example, I want to set the CAN controller to only accept messages with an ID of 0x10 (that's 0b000 0001 0000 as an 11 bit CAN ID). My understanding of the documentation (particularly the "Multiple ID Filter Configuration" section) suggests that I would need to build my can_filter_config_t used in can_driver_install() as below.

Code: Select all

can_filter_config_t fConfig = {
        .acceptance_code = 0b00000010000,	//0x10
        .acceptance_mask = 0,			//For a perfect match (aka no 'don't care' bits)
        .single_filter = true,
    };
However, this results in the CAN controller not accepting messages with any ID that I've tried, including 0x10.

I've tried a number of options for the mask, each with no success.

Based on acceptance_mask and acceptance_code both being uint32_ts I tried left padding the mask with 1s so that the mask should allow any value in the bits outside of the ID range, but still care about the 11 bits of ID itself.

Code: Select all

.acceptance_mask = 0b11111111111111111111100000000000


This again results in the CAN controller not accepting messages with any ID that I've tried, including 0x10.

I then tried right padding the mask instead (which would make more sense to me than left padding based on the format of a CAN message)

Code: Select all

.acceptance_mask = 0b00000000000111111111111111111111
This results in the CAN controller accepting messages with the ID 0x10, but also accepting all other messages I tried! Close, but still not right...

Finally, just to check my understanding of the mask, I tried using

Code: Select all

.acceptance_mask = 0b11111111111111111111111111111111
As expected, this accepts all messages regardless of ID.

Any pointers on what I might be missing here? I don't know what to try next!

Otherwise I'm stuck wasting clock cycles on software filtering...

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: CAN ID hardware acceptance filtering

Postby ESP_Dazz » Fri Feb 26, 2021 4:22 pm

The 32 bit number does not correspond bit to bit to an ID due to the need to use a single filter out STD and EXTD ID. So you'll need to program the code and mask according to the bit fields illustrated in the diagrams of the Acceptance Filter section of the docs (note the right side MSB)

jrveale
Posts: 9
Joined: Mon Apr 01, 2019 9:30 am

Re: CAN ID hardware acceptance filtering

Postby jrveale » Mon Mar 01, 2021 3:49 pm

Oh I think I see what I missed - my acceptance code needs shifting to the MSB, not just the acceptance mask.

So, looking at the diagram from the documentation for a standard frame, if I want to only accept CAN frames with ID of 0x10 (aka 0b000 0001 0000).
Image
My acceptance code should be 0b0000 0010 000X XXXX XXXX XXXX XXXX XXXX (red in image, reversed), where X is don't care, and
my acceptance mask should be 0b0000 0000 0001 1111 1111 1111 1111 1111 (blue in image, reversed).

The process for a extended frame is similar.

I'll try this when I next have access to my boards.

Thanks for the help!

dmaxben
Posts: 108
Joined: Thu Nov 16, 2017 6:04 pm

Re: CAN ID hardware acceptance filtering

Postby dmaxben » Thu Mar 04, 2021 7:58 pm

Why not just filter it in software?

Who is online

Users browsing this forum: No registered users and 82 guests