2015-11-11 08:50 AM
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
2015-11-11 09:28 AM
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);
}
2015-11-12 01:51 AM
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....
2015-11-12 02:25 AM
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 AddToFifoso ((ID & 0x700) == 0x100) and the equivalent to ((ID >= 0x100) && (ID <= 0x1FF))2015-11-12 05:36 AM
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 ya2017-01-27 05:41 PM
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.
2017-01-27 05:49 PM
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
2017-03-22 05:51 AM
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.
2017-03-22 07:26 AM
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
2017-03-22 07:31 AM
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.