How to use "twai_filter_config_t"

nopnop2002
Posts: 112
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

How to use "twai_filter_config_t"

Postby nopnop2002 » Thu Jul 16, 2020 7:21 am

I don't know how to use "twai_filter_config_t".


TWAI_FILTER_CONFIG_ACCEPT_ALL is defined as follows:


#define TWAI_FILTER_CONFIG_ACCEPT_ALL() {.acceptance_code = 0, .acceptance_mask = 0xFFFFFFFF, .single_filter = true}


In my previous science, I understand that mask=0 and code=0 is all acceptable.



For example, please tell me the setting that allows only ID=0x100.

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

Re: How to use "twai_filter_config_t"

Postby ESP_Dazz » Thu Jul 16, 2020 9:24 am

Acceptance filter configuration is described here

nopnop2002
Posts: 112
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: How to use "twai_filter_config_t"

Postby nopnop2002 » Thu Jul 16, 2020 9:42 am

I looked at this page many times and changed various settings, but it did not work as expected.

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

Re: How to use "twai_filter_config_t"

Postby ESP_Dazz » Thu Jul 16, 2020 3:02 pm

Quoted from the documentation
The acceptance code specifies the bit sequence which a message’s ID, RTR, and data bytes must match in order for the message to be received by the TWAI controller.
This basically means, for a message to be accepted, the message's ID, RTR, and data bytes must match exactly what is specified in the acceptance code.
The acceptance mask is a bit sequence specifying which bits of the acceptance code can be ignored. This allows for a messages of different IDs to be accepted by a single acceptance code.
The acceptance mask specifies which bits of the acceptance code to ignore.

For example, to accept all Standard ID messages of ID=0x100, you can set your acceptance filter configuration as follows
  1. static const can_filter_config_t f_config = {.acceptance_code = (0x100 << 21),
  2.                                              .acceptance_mask = ~(0x7FF << 21),
  3.                                              .single_filter = true};

nopnop2002
Posts: 112
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: How to use "twai_filter_config_t"

Postby nopnop2002 » Thu Jul 16, 2020 11:15 pm

Thank you.

Figure of this page is Right side MSBit


I overlooked this.


By changing to Right side MSBit, it worked as expected.

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

Re: How to use "twai_filter_config_t"

Postby ESP_Dazz » Fri Jul 17, 2020 5:43 am

@nopnop2002

Maybe the Right Side MSB diagram is confusing. I'll add a ticket internally to update the diagram to be Left Side MSB.

kissinno
Posts: 10
Joined: Tue Apr 14, 2020 11:31 am

Re: How to use "twai_filter_config_t"

Postby kissinno » Wed May 25, 2022 3:49 am

This Right side MSBit is for sure confusing but still, I'm not able to manage Dual Filter Mode.
https://docs.espressif.com/projects/esp ... nce-filter

Anyone who could post example code for Multiple ID Filter Configuration please?

Here are my ID's to read:
ID1: 0x120 0001-0010-0000
ID2: 0x129 0001-0010-1001
ID3: 0x12B 0001-0010-1011
ID4: 0x290 0010-1001-0000
ID5: 0x540 0101-0100-0000
ID6: 0x550 0101-0101-0000

Mask Register = 0111 1111 1011 = 0x7FB (MSbit) or 0xDFE (LSbit)
"The acceptance code can be set to any one of the IDs."
Acceptance register: 0001-0010-0000 = 0x120 (MSbit) or 0x48 (LSbit)

CAN_filter_t p_filter;
#define CAN_FILTER_CONFIG_ACCEPT() {.acceptance_code = 0x120, .acceptance_mask = 0x7FB, .single_filter = FALSE}
// #define CAN_FILTER_CONFIG_ACCEPT() {.acceptance_code = 0x48, .acceptance_mask = 0xDFE, .single_filter = FALSE}

ESP32Can.CANConfigFilter(&p_filter);
// Init CAN Module
ESP32Can.CANInit(); // Init CAN Module

I tried both MSbit and LSbit but I don't get any CAN telegram :-(
Without filter applied, my ESP32 received all ID's (too many).
What do I miss please !?! Thanks for your support.

nopnop2002
Posts: 112
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: How to use "twai_filter_config_t"

Postby nopnop2002 » Wed Jun 01, 2022 8:35 am

Code: Select all

Here are my ID's to read:
ID1: 0x120 0001-0010-0000
ID2: 0x129 0001-0010-1001
ID3: 0x12B 0001-0010-1011
ID4: 0x290 0010-1001-0000
ID5: 0x540 0101-0100-0000
ID6: 0x550 0101-0101-0000
I cannot find a mask pattern that is common to all of them.

I don't think it's possible to define these IDs with two filters.

.acceptance_mask specifies the bit pattern of interest.
A bit of 0 indicates don't care.
A bit of 1 indicates that you are paying attention.

Code: Select all

ID1: 0x120 0001-0010-0000
ID2: 0x129 0001-0010-1001
ID3: 0x12B 0001-0010-1011

ID1: 0x120 0001-0010-xxxx
ID2: 0x129 0001-0010-xxxx
ID3: 0x12B 0001-0010-xxxx

.acceptance_mask = 0x1F0 << 5 (=0001-1111-0000)
.acceptance_code = 0x120 << 5 (=0001-0010-0000)

Code: Select all

ID1: 0x120 0001-0010-0000
ID4: 0x290 0010-1001-0000
ID5: 0x540 0101-0100-0000
ID6: 0x550 0101-0101-0000

ID1: 0x120 xxxx-xxxx-0000
ID4: 0x290 xxxx-xxxx-0000
ID5: 0x540 xxxx-xxxx-0000
ID6: 0x550 xxxx-xxxx-0000
.acceptance_mask = 0x00F << 5 (=0000-0000-1111)
.acceptance_code = 0x000 << 5 (=0000-0000-0000)
Last edited by nopnop2002 on Tue May 21, 2024 2:27 pm, edited 2 times in total.

kissinno
Posts: 10
Joined: Tue Apr 14, 2020 11:31 am

Re: How to use "twai_filter_config_t"

Postby kissinno » Wed Jun 01, 2022 12:14 pm

Hi,

I finally understood how Dual Mode Filter works.

Fully tested and it works very well, I'm getting only the expected ID's :-)

Setup: ESP32 mini (mhetesp32devkit) + SN65HVD230 + PlatformIO VSCode + PEAK USB-CAN sniffer to simulate CAN messages
Lib CAN by Michael Wagner (miwagner): https://github.com/miwagner/ESP32-Arduino-CAN

// CAN bus
/* WEMOS_R05 - GPIO17:Data_CAN0_TXD #define GPIO16_Data_CAN0_TXD */
/* WEMOS_R06 - GPIO16:Data_CAN0_RXD #define GPIO17_Data_CAN0_RXD */
CAN_cfg.speed = CAN_SPEED_500KBPS;
CAN_cfg.tx_pin_id = GPIO_NUM_16; // Tx CAN SN65HVD230
CAN_cfg.rx_pin_id = GPIO_NUM_17; // Rx CAN SN65HVD230
CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));

PDO_Rec_Timeout_alarm_ref = 5; // Alarm if PDO is not received 5 times pooling time expected
PDO_Rec_Timeout_fault_ref = 15; // Fault if PDO is not received 10 times pooling time expected
CAN_all_1st_Rec_detected = CAN_Auto_Reset_Done = LOW;

// ESP32 hardware Dual Filter Mode is used
CAN_filter_t p_filter;
p_filter.FM = Dual_Mode;
// ID's received by Filter 1: 0x120, 0x129, 0x12B
p_filter.ACR0 = 0x24; // ACR0 0x24
p_filter.ACR1 = 0x00; // ACR1 0x00
p_filter.AMR0 = 0x01; // AMR0 0x01
p_filter.AMR1 = 0x7F; // AMR1 0x7F
// ID's received by Filter 2: 0x290, 0x540, 0x550
p_filter.ACR2 = 0x52; // ACR2 0x52
p_filter.ACR3 = 0x00; // ACR3 0x00
p_filter.AMR2 = 0xFA; // AMR2 0xFA
p_filter.AMR3 = 0x1F; // AMR3 0x1F
// Config Filter
ESP32Can.CANConfigFilter(&p_filter);
// Init CAN Module
ESP32Can.CANInit();

// ID1: 0x120, 20ms, CAN_Engine_Speed_rpm, CAN_Throttle_Handle_Position
// ID2: 0x129, 20ms, CAN_Gear_Position, CAN_Clutch_On
// ID3: 0x12B, 10ms, Bike_Speed_front_kmh, Bike_Speed_rear_kmh
// ID4: 0x290, 10ms, CAN_Brake_Front_signal, CAN_Brake_Rear_signal, CAN_Brake_Light
// ID5: 0x540, 10ms, CAN_Oil_Temp_DegC
// ID6: 0x550, 20ms, CAN_Water_Temp_DegC, CAN_High_Beam, CAN_Low_Beam, CAN_Turn_R, CAN_Turn_L, CAN_Turn_Hazard

I made a EXCEL to better explain.

Hoping this helps.
Attachments
ESP32 CAN dual filter config.png
ESP32 CAN dual filter config.png (30.32 KiB) Viewed 10484 times

vadissoftware
Posts: 1
Joined: Sat Dec 16, 2023 11:56 am

Re: How to use "twai_filter_config_t"

Postby vadissoftware » Sun Dec 24, 2023 2:08 pm

Hi everyone.
After checking all official info about twai_filter_config_t , specially f_config what I test is example setup for engine (0x7E8) :
void setupCan() {
...
// tested default filter and mask only to receive data from 7E8
f_config.acceptance_code = 0x7E8 << 5;
f_config.acceptance_mask = 0x7E8 << 5; // 11 bits
f_config.single_filter = false;
twai_driver_install(..);
twai_start();
...
}

- all without ESP_ERR of course.

Of course i'am interesting about receive ONLY frames from 7E8 in this test.
Actually transmitted by me periodic frames coming to me from 7E8, 7E9, 7E1

after setup I call other function :
twai_can_set_filter(0x7E1){
twoi_stop();
f_config.acceptance_code = 0x7E1 << 5;
f_config.acceptance_mask = 0x7E1 << 5; // 11 bits
f_config.single_filter = false;
twai_start();
}

Returned status for twai_start() is ESP_OK.

After this I still receive ONLY frames from 7E8.
Why ?

Here is what I sent periodically from other can bus analyser:
Image

Here is what i have received on ESP32:
Image

Why this re-setup of filter not working for me properly ??

Who is online

Users browsing this forum: No registered users and 97 guests