cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Filter

I set on a slave

    CAN_FilterInitStructure.CAN_FilterNumber = 0;
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
    CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterIdLow = 0x0064;    
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x00FF;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
    CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
    CAN_FilterInit(&CAN_FilterInitStructure);

master sends

TxMessage.ExtId = (opcode << 8) | 0х64;

but the slave dosen't get the message. without mask and filter the slave gets the message.

how should I set the mask and filter?

10 REPLIES 10

any body? no one in the community knows how the CAN filter works in STM32?

T J
Lead

here you can see some bits are high in the mask ? only these bits are tested in the filter

//##-2- Configure the CAN1 Filter ###########################################
sFilterConfig.FilterNumber   = 0;
sFilterConfig.FilterMode       = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale        = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh     = 0x400 << 5; //11-bit ID in top bits
sFilterConfig.FilterIdLow     = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x600 << 5; // resolves as 0x0400 - 0x05FF
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = 0;
sFilterConfig.FilterActivation   = ENABLE;
sFilterConfig.BankNumber      = 0;

The Mask is 0x600 // only these two bits are tested

The Filter is 0x400 // in those two bits, A9 must be high and A10 must be low,

everything else is "dont care", we only care about the Masked bits.

so anything within 0x400- 0x5FF is accepted

I see. Thank you.

But in my case - if I want to accept the messages only for ID = 0xXX64 (XX - don't care) - my settings above seems to be valid. However I don't get any messages with the filter applied.

T J
Lead

can you see my mask bits are shifted up 5 bits ?

sFilterConfig.FilterIdHigh = 0x400 << 5; //11-bit ID in top bits

Ah... I see. The mask should be shifted. Thank you.

I just can't understand - why shift 5? It should be 3.

0690X000006CDuHQAW.png

 Ah...I see. STDID is in High part of the register.

Depends on what you're matching, the top half of the diagram is the High and the bottom is the Low portion.

For the STDID you want to use the high filter with the pattern shifted left 5 bits.

Filters have been described on the forum multiple times over close to a decade. ​

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

Still can't get it working.

I want to get ID = 0x64 , the rest don't care

CAN_FilterInitStructure.CAN_FilterIdHigh =  0x0064 << 5;  
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;      
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x00FF << 5; 
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;  

But when a master sends ID = 0x1164 I don't get it with the filter applied.

T J
Lead

is that an extended ID ? or std ID ?

is that address outside the address range ? 0x1164 << 5 overruns the 16bit value...

if you are using extended IDs, then <<3 is correct...