cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 CAN acceptance mask

Posted on July 27, 2015 at 14:08

Hello there,

I am trying to configure the acceptance mask of the CAN peripheral but I just cant seem to understand how does it work...

    CAN_FilterConfTypeDef sFilterConfig;

    sFilterConfig.FilterNumber = 0;

    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

    sFilterConfig.FilterIdHigh = 0x0000;

    sFilterConfig.FilterIdLow = 0x0000;

    sFilterConfig.FilterMaskIdHigh = 0x0000;

    sFilterConfig.FilterMaskIdLow = 0x0000;

    sFilterConfig.FilterFIFOAssignment = 0;

    sFilterConfig.FilterActivation = ENABLE;

    sFilterConfig.BankNumber = 14;

I need a 32 bit mask. I was trying to  start filtering with not complicated filters like 0x01 or 0xFF that I have typed in

FilterMaskIdLow

but the filter doesnt work. When 0x01 is written, it accepts everything and when 0xFF is written it doesnt accept can messages with ID 0xFF. Could someone please explain me how to use this filter? I though its like this:

32 bit filter = (

FilterMaskIdHigh

<< 16) |

FilterMaskIdLow

But its not.

#can #filter
7 REPLIES 7
Posted on July 27, 2015 at 14:39

My understanding is the High Order bits, of the High Order register are the Message ID

The Mask is a bit level mask (AND), ID compared (CMP)

if ((MSBFirst32 & Mask) == ID) then Take;

Assume the bits on the CAN bus are taken MSB first.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 27, 2015 at 14:44

For a STDID of 0x123

sFilterConfig.FilterIdHigh = 0x123 << 5; // High 11-bits

sFilterConfig.FilterIdLow = 0x0000;

sFilterConfig.FilterMaskIdHigh = 0xFFE0; // High 11-bits

sFilterConfig.FilterMaskIdLow = 0x0000;
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 27, 2015 at 18:15

Hello, thank you for answer. I have a 32 bit ID. So lets say I would like to receive all messages that contains 0xFF00 in it. How would the mask look? Do I then need to write anything to ID registers or keep them 0?

So i need the mask to be XXXXFFXX in hex, where X stands for dont care. How to understand these regs?

Posted on July 27, 2015 at 18:29

The bit mask would look like whatever bits you want to drop through to the comparator below.

And however the 32-bit ID maps into the 11-bits of STID and 18-bits of EXID (29 bits by my math)

0690X0000060337QAA.jpg

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 27, 2015 at 18:39

You're doing a comparison, if you set the ID to zero, only messages that come to ZERO will match. Otherwise you need it to MATCH (or portion thereof) the ID you're looking for.

uint32 STID = 0x123; // Some nonsensical values for explicit pattern match, pick something relevent
uint32 EXID = 0x34567;
sFilterConfig.FilterIdHigh = (uint16_t)(((STID << 
5
) & 0xFFE0) | ((EXID >> 13) & 0x001F));
sFilterConfig.FilterIdLow = (uint16_t)(((EXID << 3) & 0xFFF8));
sFilterConfig.FilterMaskIdHigh = 0xFFE0 | 0x001F; // STID[10:0] and EXID[17:13]
sFilterConfig.FilterMaskIdLow = 0xFFF8; // EXID[12:0]

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 27, 2015 at 18:56

I think I finally understand, thank you. I dont have the board on me to test this, so i will do this tommorow morning and let know here.

Posted on July 28, 2015 at 08:23

Yes, I have managed to get it working now. Thank you again.