Skip to main content
puneethj
Associate III
November 11, 2015
Question

CAN FILTERS

  • November 11, 2015
  • 10 replies
  • 11111 views
Posted on November 11, 2015 at 17:50

Hi every one, especially Mr.Clive, today i have problem with CAN FILTERS i want to allow all IDS from 100 to 1FF in other words block all messages after 0x0200

here is my code, Please help me, Thanks in advance

void CAN_FilterConfiguration(void) {
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN filter configuration */
// CAN_FilterInitStructure.CAN_FilterNumber = 0; // CAN 1
CAN_FilterInitStructure.CAN_FilterNumber = 14; // CAN 2
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // FIFO = 0
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // Filter mode = identifier mask based filtering
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0100 << 5;
CAN_FilterInitStructure.CAN_FilterIdLow = 0;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x01ff << 5;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
//Interrrupts
CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);
CAN_FilterInit(&CAN_FilterInitStructure);
}

#!stm32f4-disco #can-filter
This topic has been closed for replies.

10 replies

Tesla DeLorean
Guru
November 11, 2015
Posted on November 11, 2015 at 18:28

I suspect this will do the job

void CAN_FilterConfiguration(void)
{
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN filter configuration */
//CAN_FilterInitStructure.CAN_FilterNumber = 0; // CAN 1 [0..13]
CAN_FilterInitStructure.CAN_FilterNumber = 14; // CAN 2 [.27]
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // FIFO = 0
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // Filter mode = identifier mask based filtering
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
/* Filter 0x.0x1FF */
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x100 << 5; //11-bit ID in top bits
CAN_FilterInitStructure.CAN_FilterIdLow = 0;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x700 << 5;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0;
/* Interrupts */

CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);
CAN_FilterInit(&CAN_FilterInitStructure);
}

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
puneethj
puneethjAuthor
Associate III
November 12, 2015
Posted on November 12, 2015 at 10:51

Wow!!! it works it works :) thank you very much clive, but can u please explain this command

CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x700 << 5;

please it would help me understand CAn filters and i need not trouble you whenver i need to change something.. Thank you once again....
Tesla DeLorean
Guru
November 12, 2015
Posted on November 12, 2015 at 11:25

It's doing a Bitwise AND and COMPARE, the ID is in the top 11-bits of the High 16-bit registers.

ie  if ((ID & MASK) == COMP) then AddToFifo

so ((ID & 0x700) == 0x100)

and the equivalent to ((ID  >= 0x100) && (ID <= 0x1FF))

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
T J
Senior III
January 28, 2017
Posted on January 28, 2017 at 01:41

My processor is the '091, the reference manual shows we are using a bxCAN peripheral,

I have found the same declarations, but with the bxCAN flavor, spelled slightly differently.

For the most concise understanding of the CAN functionality

In my ref manual for the '091 processor,  RM0091

chapter  29.7 bxCAN functional description is excellent.

Tesla DeLorean
Guru
January 28, 2017
Posted on January 28, 2017 at 01:49

Not sure if the MDK is using it's own middleware. This code example is for the SPL, but similar structures exist within the HAL

http://www.st.com/en/embedded-software/stsw-stm32048.html

 
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
puneethj
puneethjAuthor
Associate III
November 12, 2015
Posted on November 12, 2015 at 14:36

Brilliant!! Thank you sir.

Guys if anyone is looking for CAN filter explanation this is the easiest explanation ever. Hope it helps you all. See ya 

PRABU Appunu
Visitor II
November 28, 2018

hi Clive

am working on STM32F417 with CAN extended ID .

i would like to reject the ID for example 0x0500xxxx.

I need to receive all messages which is > 0x0500xxxx.The only bit i need to check is 0x0500

I checked with ID masking mode , it was not possible to reject single ID .

I am not sure that ID list will serve this purpose .

How to reject signle ID using IDmasking or list mode ?

Appreciated to get some input

Thank YOU,

Tesla DeLorean
Guru
November 28, 2018

You filter the stuff you WANT to receive, it ignores the rest

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
PRABU Appunu
Visitor II
November 28, 2018

Clive,

i supposed to receive 50 CAN devices with specific ID's.

Instead of filtering those ID's , i would like to reject one single ID from that group and received rest of them without any issue.

I just want to filter CAN ID with 0x05000xxx...rest of ID's like 0x0501XXXX ,0x0502XXXX rest of all ID's i would like to receive.

i tried in ID MASK mode

filter _id 0x05001000

mask id 0x05FFF000

here i can only receive 0x05001XXX ID's which is logical.This is working fine.

But i want to reject only 0x05000xxx ID , which i coulnd't do it with masking .

Do you see any possibility here ?

Appreciated for the help

PRABU Appunu
Visitor II
November 29, 2018

Clive,

Did you see the any logic to reject the ID 0x0500xxx?

Thanks in advance for your help

Uvess.1
Associate II
September 8, 2020

hi @Community member​ ,

I tried to understand with this examples but it's not obvious for me...

For exemple we want to accept address between 0x008 and 0x04F

So Filter_id_high = 0x008<<5

but i don't know how to calculate the mask....

EDIT1: so we've (MASK & ID) = 0x008

CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN filter configuration */
CAN_FilterInitStructure.CAN_FilterNumber = 14;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; 
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; 
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x008 << 5; //11-bit ID in top bits
CAN_FilterInitStructure.CAN_FilterIdLow = 0;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x??? << 5;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0;

Thx in advance,

Uvess

Uvess.1
Associate II
September 8, 2020

OK i found,

it's 0x7C2<<5

DHurt.1
Visitor II
February 3, 2021

I spent some hours because not inderstanding correctly how identifiers extended is formed.

Now i know that is at right of standard. No sense to me because identifier with number 1 is in differents places depending if is standard or extended format.

I made an Excel file to help how to program the filters. I hope is helpful for others.

There are 2 sheets. The first is where config the filters. The second has some examples.

BBasn.1
Visitor II
August 19, 2023

Is there any convenient method for calculating Mask value and apply to filter certain range of IDs? As in the example

void CAN_FilterConfiguration(void) {
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN filter configuration */
uint16_t StartID = 0x100, endID = 0x1FF; //range of ID to allow 0x100 to 0x1FF
uint16_t maskID = (some operation???)
// CAN_FilterInitStructure.CAN_FilterNumber = 0; // CAN 1
CAN_FilterInitStructure.CAN_FilterNumber = 14; // CAN 2
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // FIFO = 0
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // Filter mode = identifier mask based filtering
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = startID << 5;
CAN_FilterInitStructure.CAN_FilterIdLow = 0;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = maskID << 5;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
//Interrrupts
CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);
CAN_FilterInit(&CAN_FilterInitStructure);
}

Thankyou.

Tesla DeLorean
Guru
August 19, 2023

DeMorgan's Theory?

(ID & 0x700) == 0x100

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