CAN-Bus API multiple filter configuration

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

CAN-Bus API multiple filter configuration

Postby gunar.kroeger » Wed Aug 15, 2018 2:21 pm

I see that a few days ago a new commit was made to the idf that included a driver for the CAN-bus module
I already tested it and it works well but I couldn't find out how to set multiple acceptance filters

currently I am using

Code: Select all

#define CAN_FILTER_CONFIG_ACCEPT_ALL()  {.acceptance_code = 0, .acceptance_mask = 0xFFFFFFFF, .single_filter = true}
and filtering per software.

And I understand how to use a single filter based on this. But how do I implement multiple filters, for a list of ids?

could someone provide me with a small example?

Thanks
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: CAN-Bus API multiple filter configuration

Postby ESP_Dazz » Thu Aug 16, 2018 2:30 pm

See the Acceptance Filter section of the docs.

What essentially when you set

Code: Select all

single_filter = false
is that theacceptance_code and acceptance_mask will be interpreted differently by the CAN peripheral. Therefore instead using a 32-bit value to define a single code/mask, the each 32-bit value will be used to define two codes/masks. See the docs to see how the 32-bit code/masks are interpreted under single/dual filter mode when receiving standard/extended messages.

To filter out multiple IDs, you'll need to bit wise XNOR each of your IDs to find which bit positions of the multiple IDs conflict (where one ID has a 1 in that position, and another has a 0). Then for those bit positions, set them as don't care using the acceptance mask. Then for the non conflicting bit positions, you can set it as your acceptance code.

For example
ID1 = 101 1010 0000
ID2 = 101 1010 0001
ID3 = 101 1010 0010
ID4 = 101 1010 0100
ID5 = 101 1010 1000
ID6 = 101 1110 0000
ID7 = 101 0010 0000


The XNOR would be
XNOR = 111 0011 0000
This means the zero bits of the XNOR are places which you need to set as don't care in your acceptance mask.

Hence the acceptance mask and code would be
MASK = 000 1100 1111
CODE = 101 XX10 XXXX where X means "don't care" since those bits conflict and are hence masked out by the acceptance mask

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

Re: CAN-Bus API multiple filter configuration

Postby gunar.kroeger » Mon Aug 20, 2018 9:24 pm

And is there a way to change the acceptance filter on the fly?
Or do I have to reinstall the driver?

Thanks
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: CAN-Bus API multiple filter configuration

Postby ESP_Dazz » Wed Aug 29, 2018 11:41 am

Current CAN driver implementation requires the driver to be reinstalled.
On the hardware level, the CAN must enter reset mode (will disconnect CAN peripheral from the bus) before any configuration registers can be modified.

dmaxben
Posts: 108
Joined: Thu Nov 16, 2017 6:04 pm

Re: CAN-Bus API multiple filter configuration

Postby dmaxben » Fri Aug 31, 2018 12:33 pm

Why cant you just do a simple software setup?

ie

if (rx_frame.MsgID == 0x123)
{
read the message in and do something with it.
}

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

Re: CAN-Bus API multiple filter configuration

Postby gunar.kroeger » Fri Aug 31, 2018 1:21 pm

I want to reduce the load on the cpu as much as possible so that other tasks (like logging onto the SD card, wifi and compressing data) can utilize it more.
For now I will set hw filters for all possible message ids i could receive and software filters for every id that I want to receive.

Thanks :D
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

doglike
Posts: 63
Joined: Fri Aug 18, 2017 4:21 pm

Re: CAN-Bus API multiple filter configuration

Postby doglike » Tue Jun 16, 2020 7:39 am

To be honest, after reading posts like this + the docs several time, I still didn't understand how to set the filter correctly :oops:

I have this code snipped from an example with the comment, that this is a no-filter-config.

Code: Select all

void can_setRxFilter(void)
{
   CAN_filter = { Single_Mode, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF };     //no filter
}

Code: Select all

typedef struct {
    	CAN_filter_mode_t 	FM:1;          	/**< \brief [0:0] Filter Mode */
    	uint8_t 		ACR0;		/**< \brief Acceptance Code Register ACR0 */
	uint8_t 		ACR1;		/**< \brief Acceptance Code Register ACR1 */
	uint8_t 		ACR2;		/**< \brief Acceptance Code Register ACR2 */
	uint8_t 		ACR3;		/**< \brief Acceptance Code Register ACR3 */
	uint8_t 		AMR0;		/**< \brief Acceptance Mask Register AMR0 */
	uint8_t 		AMR1;		/**< \brief Acceptance Mask Register AMR1 */
	uint8_t 		AMR2;		/**< \brief Acceptance Mask Register AMR2 */
	uint8_t 		AMR3;		/**< \brief Acceptance Mask Register AMR3 */
} CAN_filter_t;
How should I set this filter, if I only want to receive the frames with ID 0x32 and 0x67 ?
The System is connected to a vehicle CAN-BUS with 500k baud and only standard-frames.

Thank you guys in advance!

Who is online

Users browsing this forum: Bing [Bot] and 98 guests