How to Filter CAN Frames and Optimize Hardware and Software for Coral3568

How to Filter CAN Frames and Optimize Hardware and Software for Coral3568
During debugging of the CAN bus, the receiving end can focus on receiving important messages based on the importance of the data, thereby improving efficiency. The CAN message frame is filtered using an identifier or a series of identifiers. The filtering function can be implemented using CAN bus software and chip hardware filtering rules.

How to Filter CAN Frames and Optimize Hardware and Software for Coral3568 Software Filtering

struct can_filter filter[1];/* Rule: Can receive data frame with ID 0x200 and error frame */
filter[0].can_mask = CAN_SFF_MASK;filter[0].can_id  = 0x200 & CAN_SFF_MASK;filter[0].can_mask |= CAN_EFF_FLAG;
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &filter, sizeof(filter))){perror("setsockopt failed");exit(EXIT_FAILURE);}

The software configuration can be checked in the system:

root@host:/root# ls /proc/net/can/rcvlist_all  rcvlist_err  rcvlist_inv  reset_statsrcvlist_eff  rcvlist_fil  rcvlist_sff  stats
root@host:/root# cat /proc/net/can/rcvlist_filreceive list 'rx_fil':  (any: no entry)  (can0: no entry)  device   can_id   can_mask  function  userdata   matches  ident   can1      200    800007ff  0000000095327ce0  00000000674196b1         0  raw  (can2: no entry)
Here, rcvlist_* refers to the receiver in the CAN protocol: struct receiver, which contains the software filtering rules.
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568 Hardware Filtering
Chip Manual:
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568

The hardware filtering method is implemented in the driver: drivers/net/can/rockchip/rockchip_canfd.c, specific code:

static int rockchip_canfd_start(struct net_device *ndev){  ......  rockchip_canfd_write(rcan, CAN_INT_MASK, 0);
  /* RECEIVING FILTER, accept all */  rockchip_canfd_write(rcan, CAN_IDCODE, 0);  rockchip_canfd_write(rcan, CAN_IDMASK, CAN_RX_FILTER_MASK);  rockchip_canfd_write(rcan, CAN_IDCODE0, 0);  rockchip_canfd_write(rcan, CAN_IDMASK0, CAN_RX_FILTER_MASK);  rockchip_canfd_write(rcan, CAN_IDCODE1, 0);  rockchip_canfd_write(rcan, CAN_IDMASK1, CAN_RX_FILTER_MASK);  rockchip_canfd_write(rcan, CAN_IDCODE2, 0);  rockchip_canfd_write(rcan, CAN_IDMASK2, CAN_RX_FILTER_MASK);  rockchip_canfd_write(rcan, CAN_IDCODE3, 0);  rockchip_canfd_write(rcan, CAN_IDMASK3, CAN_RX_FILTER_MASK);  rockchip_canfd_write(rcan, CAN_IDCODE4, 0);  rockchip_canfd_write(rcan, CAN_IDMASK4, CAN_RX_FILTER_MASK);  ......}
From the above code, it can be seen that every time the CAN software is executed, the CAN controller registers are reconfigured to not filter any data.
Hardware filtering is achieved through two registers: CAN_IDCODEn, CAN_RX_FILTER_MASK. The RK3568 CAN controller has 6 filters, of which the first filter is enabled by default and is not controlled by a switch, while the other filters need to enable the corresponding filter.Hardware filtering can be configured while the CAN software is running,for example:
root@host:/root# ip link set can1 type can bitrate 1000000root@host:/root# ip link set can1 uproot@host:/root# candump can1 &#  only receive frames with ID 0x123, not controlled by a switchroot@host:/root# io -4 0xfe58003c 0x123root@host:/root# io -4 0xfe580040 0x0#  only receive frames with ID 0x124, need switch controlroot@host:/root# io -4 0xfe580120 0x124root@host:/root# io -4 0xfe580124 0x0root@host:/root# io -4 0xfe58011c 0x1
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568 System Optimization
1. Abnormal Returns on Sending

The default sending queue for the system CAN: txqueuelen:10, for wired network ports this value is 1000. A smaller value results in stronger real-time performance.

During large data sending, the write function often returns abnormally, mainly because of insufficient memory in the system sending queue. The following command can be used to increase the sending queue:

root@host:/root# ip link set txqueuelen 500 dev can1
2. Data Loss on Receiving

Insufficient receiving queue is reflected in incomplete data obtained from read, mostly because the data skb has already been provided to the receiver queue, but the application is unable to retrieve it in time, leading to final filling of all available memory size, and data is updated to the wrong location in the queue.

The following command can be used to adjust the receiving queue size:
root@host:/root# echo 1000000 > /proc/sys/net/core/rmem_maxroot@host:/root# echo 1000000 > /proc/sys/net/core/rmem_default
The Coral-EVa is an evaluation board launched by Zhiyuan Electronics for the Coral3568, which is equally powerful and features rich interfaces. The Coral-EVa evaluation board uses adapter power supply, making it more convenient for laboratory and R&D office use, with HDMI, DP, USB, CAN, RS485, RS232, TTL UART, 3.5mm four-wire headset interface, Micro SD card slot, SATA, M.2, LVDS LCD, MIPI_DSI, MIPI-CSI, RTC clock, buzzer and other functions all available.
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568 Technical Exchange Group
Long press to recognize the following QR code to join the “Industrial Control Board/Core Board Technical Exchange Group”, to communicate with like-minded friends, and have professional technicians to answer your questions. If you have any questions, you can consult Xiao Zhi’s WeChatzlgmcu-888.
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568

For more past articles, please click “ Read the original ”.

How to Filter CAN Frames and Optimize Hardware and Software for Coral3568

How to Filter CAN Frames and Optimize Hardware and Software for Coral3568

How to Filter CAN Frames and Optimize Hardware and Software for Coral3568
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568
How to Filter CAN Frames and Optimize Hardware and Software for Coral3568

Leave a Comment

×