CAN

Introduction to CAN

CAN (Controller Area Network) bus is a serial communication network that effectively supports distributed control or real-time control. The CAN bus is a bus protocol widely adopted in automobiles, designed for microcontroller communication in the automotive environment. For more information, you can refer to theCAN application report

Hardware Connection

Wiring between CAN modules: CAN_H connects to CAN_H, CAN_L connects to CAN_L.

Communication Test

CAN Communication Test

You can test sending and receiving messages using the candump and cansend tools. Push the tools to the /system/bin/ directory for execution. The tools can be downloaded from  github .

#Turn off the can0 device at both sending and receiving ends
ip link set can0 down
#Set the bitrate at both sending and receiving ends               
ip link set can0 up type can bitrate 1000000 dbitrate 3000000 fd on
#Execute candump at the receiving end, blocking while waiting for messages                               
candump can0
#Execute cansend at the sending end to send a message          
cansend can0 123#1122334455667788         

More Commands

1、 ip link set canX down           //Turn off the CAN device;
2、 ip link set canX up             //Turn on the CAN device;
3、 ip -details link show canX             //Display detailed information about the CAN device;
4、 candump canX                    //Receive data sent from the CAN bus;
5、 ifconfig canX down                     //Turn off the CAN device for configuration;
6、 ip link set canX up type can bitrate 1000000 //Set the CAN baud rate
7、 conconfig canX bitrate + baud rate;
8、 canconfig canX start            //Start the CAN device;
9、 canconfig canX ctrlmode loopback on //Loopback test;
10、canconfig canX restart          // Restart the CAN device;
11、canconfig canX stop             //Stop the CAN device;
12、canecho canX                    //Check the CAN bus status;
13、cansend canX --identifier=ID+data      //Send data;
14、candump canX --filter=ID:mask   //Use a filter to receive data matching the ID

FAQS

A summary of several issues encountered during debugging and their solutions:

Messages are received long after being sent, or not received at all.

Check the CAN_H and CAN_L bus lines to see if the Dupont wires are loose or reversed.

CAN Clock Frequency Configuration

CAN

If the CAN bitrate is 1M, it is recommended to change the CAN clock to 300M for a more stable signal. For bitrates below 1M, a clock setting of 200M is sufficient.

Modifying CAN Clock Frequency

The method for modifying the CAN clock frequency is as follows:

diff --git a/kernel/arch/arm64/boot/dts/rockchip/rk3568-evb.dtsi b/kernel/arch/arm64/boot/dts/rockchip/rk3568-evb.dtsi
index b9df07be8..6b20e2eba 100644
--- a/kernel/arch/arm64/boot/dts/rockchip/rk3568-evb.dtsi
+++ b/kernel/arch/arm64/boot/dts/rockchip/rk3568-evb.dtsi
@@ -252,7 +252,7 @@ wireless_bluetooth: wireless-bluetooth {
 
 &can1 {
        assigned-clocks = <&cru CLK_CAN1>;
-       assigned-clock-rates = <150000000>;
+       assigned-clock-rates = <100000000>;
        pinctrl-names = "default";
        pinctrl-0 = <&can1m1_pins>;
        status = "disabled";
@@ -260,7 +260,7 @@ wireless_bluetooth: wireless-bluetooth {
 
 &can2 {
        assigned-clocks = <&cru CLK_CAN2>;
-       assigned-clock-rates = <150000000>;
+       assigned-clock-rates = <100000000>;
        pinctrl-names = "default";
        pinctrl-0 = <&can2m1_pins>;
        status = "disabled";

Note

  • At certain clock frequencies, the CAN bitrate may not achieve the exact rate. You can adjust the assigned-clock-rates to solve this. To check if the desired bitrate is obtained:

ip -d link show can1