【发布时间】:2019-06-05 12:43:18
【问题描述】:
我正在尝试在 ESP23 上使用来自 SJA1000 CAN 控制器的接受过滤器。 消息应在定义的区域中过滤(例如,仲裁 ID 从:0x30 到:0x35)。
过滤单个消息(0x30)的示例:
#define CAN_FILTER_CONFIG_ACCEPT() {.acceptance_code = 0x1500000, .acceptance_mask = 0xffffff, .single_filter = true}
如果我想过滤一个定义的区域(0x30 - 0x35):
int calc_mask(int startID, int endID) {
int size_of_range = endID - startID;
unsigned int acceptance_mask = 0xFFFFFFFF;
int i;
for (i = startID; i <= endID; i++ {
acceptance_mask = ~(acceptance_mask & i);
}
return acceptance_mask;
}
acceptance_code = 0x30;
acceptance_mask = calc_mask(0x30, 0x35);
0x30 : 110000
NAND 0x31:110001
与非 0x32:110010
NAND 0x33: 110011
与非 0x34:110100
与非 0x35:110101
0xF : 001111
验收代码 = 0x6000000: 0000 0110 0000 0000 0000 0000 0000 0000
接受掩码 = 0x1FFFFFF: 0000 0001 1111 1111 1111 1111 1111 1111
过滤的消息:
想要:
14:21:17.754 -> CAN 消息:30 110000
14:21:17.754 -> CAN 消息:31 110001
14:21:17.787 -> CAN 消息:32 110010
14:21:17.787 -> CAN 消息:33 110011
14:21:17.787 -> CAN 消息:34 110100
14:21:17.821 -> CAN 消息:35 110101
不想要的:
14:21:17.821 -> CAN 消息:36 110110
14:21:17.821 -> CAN 消息:37 110111
14:21:17.855 -> CAN 消息:38 111000
14:21:17.855 -> CAN 消息:39 111001
14:21:17.855 -> CAN 消息:3A 111010
14:21:17.889 -> CAN 消息:3B 111011
14:21:17.889 -> CAN 消息:3C 111100
14:21:17.889 -> CAN 消息:3D 111101
14:21:17.923 -> CAN 消息:3E 111110
14:21:17.923 -> CAN 消息:3F 111111
文档 ESP32 CAN-Controller(接受滤波器):
https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/can.html#acceptance-filter
文档 SJA1000 CAN 控制器(6.4.15 验收过滤器): https://www.nxp.com/docs/en/data-sheet/SJA1000.pdf
有人知道如何过滤掉不需要的消息吗?
【问题讨论】:
-
你可能只想做
acceptance_mask = 0x30 | 0x31 | ... | 0x35。如果需要,然后使用~将其全部反转。
标签: c filter mask can-bus esp32