cancel
Showing results for 
Search instead for 
Did you mean: 

CAN FILTERS

puneethj
Associate II
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
23 REPLIES 23
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
Up vote any posts that you find helpful, it shows what's working..
puneethj
Associate II
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....
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
Up vote any posts that you find helpful, it shows what's working..
puneethj
Associate II
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 

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.

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
Up vote any posts that you find helpful, it shows what's working..
Posted on March 22, 2017 at 12:51

Well, Clive I would like to know how to calculate the mask value and the ID filter field I need if for filtering from 0x0 to 0x14 i e. I can't get how do you get 0x700, I will be grateful if you explain me some of the steps that you have follow. 

Thanks in advance.

Posted on March 22, 2017 at 14:26

The questioner wants to select messages in the range 0x100..0x1FF, and the filter is doing a Masking AND operation followed by a comparison.

ie (0x123 & 0x700) = 0x100

To get messages in the 0x000..0x01F range, I'd use a mask of 0x7E0 and match of 0x000

ie (0x012 & 0x7E0) = 0x000 and (0x123 & 0x7E0) != 0x000

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

Posted on March 22, 2017 at 14:31

// CAN Filtering Simulation - sourcer32@gmail.com
#include <windows.h>
#include <stdio.h>
#define COMP 0x100
#define MASK 0x700
int main(int argc, char **argv)
{
  int id;
  for(id=0; id<0x800; id++)
    if ((id & MASK) == COMP) printf("%03X\n", id);
  return(1);
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Edit: Fixed formatting errors introduced by sloppy forum transition.

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