Skip to main content
Erenburg.Evgeny
Associate III
September 28, 2018
Question

CAN Filter

  • September 28, 2018
  • 9 replies
  • 1758 views

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?

    This topic has been closed for replies.

    9 replies

    Erenburg.Evgeny
    Associate III
    October 21, 2018

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

    T J
    Senior III
    October 21, 2018

    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

    Erenburg.Evgeny
    Associate III
    October 21, 2018

    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
    Senior III
    October 21, 2018

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

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

    Erenburg.Evgeny
    Associate III
    October 21, 2018

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

    Erenburg.Evgeny
    Associate III
    October 21, 2018

    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.

    Tesla DeLorean
    Guru
    October 21, 2018

    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 VenmoUp vote any posts that you find helpful, it shows what's working..
    Erenburg.Evgeny
    Associate III
    November 4, 2018

    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
    Senior III
    November 4, 2018

    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...

    Erenburg.Evgeny
    Associate III
    November 5, 2018

    Thank you. That was my mistake. I have an extended ID.

    So if I have an extended ID it should be this way?

     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 << 3; CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; 
     CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x00FF << 3; 
     CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
     CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
     CAN_FilterInit(&CAN_FilterInitStructure);