First, If in Android, root your system and install Termux to get a Linux shell.
3.1. Introduction
Controller area network (can) is a kind of serial communication network which can effectively support distributed control or real-time control. Can bus is a bus protocol widely used in automobile, which is designed as the communication of microcontroller in automobile environment.
Notices:The CAN FD bit filling function of RK3568 is different from the CAN FD standard protocol, and CAN FD is not recommended.
3.2. Hardware Connection
Connection between two CAN devices, only need CAN_H to CAN_H, CAN_L to CAN_L.
3.3. DTS Configuration
- Common
arch/arm64/boot/dts/rockchip/rk3568-firefly-port.dtsi
1234567891011121314151617&can1 {status = "disabled";compatible = "rockchip,can-1.0";assigned-clocks = <&cru CLK_CAN1>;assigned-clock-rates = <150000000>;pinctrl-names = "default";pinctrl-0 = <&can1m1_pins>;};&can2 {status = "disabled";compatible = "rockchip,can-1.0";assigned-clocks = <&cru CLK_CAN2>;assigned-clock-rates = <150000000>;pinctrl-names = "default";pinctrl-0 = <&can2m0_pins>;};
- Board
arch/arm64/boot/dts/rockchip/rk3568-firefly-aioj.dtsi
1234567&can1 {status = "okay";};&can2 {status = "okay";};
the nodes on the hardware interface corresponding to the software are:
1 2 |
CAN2(H2、L2):can0 CAN1(H1、L1):can1 |
3.4. Communication
3.4.1. CAN communication test
Use the “candump” and “cansend” tools directly to send and receive messages, push tool into /system/bin/ . Tools “candump/cansend” download from Officail link or github.
1 2 3 4 5 6 7 8 9 10 11 12 |
#Close the can0 device at the transceiver ip link set can0 down #Set the bit rate to 250Kbps at the transceiver ip link set can0 type can bitrate 250000 #Show can0 details ip -details link show can0 #Open the can0 device at the transceiver ip link set can0 up #Perform candump on the receiving end, blocking waiting for messages candump can0 #Execute cansend at the sending end to send the message cansend can0 123#1122334455667788 |
3.5. More Command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
1、 ip link set canX down //turn off CAN device 2、 ip link set canX up //turn on CAN device 3、 ip -details link show canX //show CAN device details 4、 candump canX //Receive data from CAN bus 5、 ifconfig canX down //shutdown CAn device 6、 ip link set canX up type can bitrate 250000 //Set CAN Baudrate 7、 conconfig canX bitrate + (Baudrate) 8、 canconfig canX start //start CAN device 9、 canconfig canX ctrlmode loopback on //loopback test 10、canconfig canX restart //restart CAN device 11、canconfig canX stop //stop CAN device 12、canecho canX //check CAN device status查看can设备总线状态; 13、cansend canX --identifier=ID+data //send data 14、candump canX --filter=ID:mask //Use the filter to receive ID matching data |
3.6. FAQS
Summarize several problems and solutions encountered during debugging.
3.6.1. Check if the CAN_H and CAN_L lines of the bus are loose or connected in reverse.
The receiving end only successfully received the message once, and then no longer received the message.
3.6.2. Configuration about clock rate
3.6.2.1. CAN
If the bitrate of CAN is 1M, it is recommended to modify the CAN clock rate to 300M to make the signal more stable. If the bitrate is lower than 1M, the clock rate can be set to 200M.
3.6.3. Change clock rate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
diff --git a/kernel/arch/arm64/boot/dts/rockchip/rk3568-firefly-port.dtsi b/kernel/arch/arm64/boot/dts/rockchip/rk3568-firefly-port.dtsi index 274ebb1..5f2ca7b <span class="m">100644</span> --- a/kernel/arch/arm64/boot/dts/rockchip/rk3568-firefly-port.dtsi +++ b/kernel/arch/arm64/boot/dts/rockchip/rk3568-firefly-port.dtsi @@ -673,7 +673,7 @@ <span class="nv">status</span> <span class="o">=</span> <span class="s2">"disabled"</span><span class="p">;</span> <span class="nv">compatible</span> <span class="o">=</span> <span class="s2">"rockchip,can-1.0"</span><span class="p">;</span> assigned-clocks <span class="o">=</span> <<span class="p">&</span>cru CLK_CAN1><span class="p">;</span> - assigned-clock-rates <span class="o">=</span> <<span class="m">150000000</span>><span class="p">;</span> + assigned-clock-rates <span class="o">=</span> <<span class="m">100000000</span>><span class="p">;</span> pinctrl-names <span class="o">=</span> <span class="s2">"default"</span><span class="p">;</span> pinctrl-0 <span class="o">=</span> <<span class="p">&</span>can1m1_pins><span class="p">;</span> <span class="o">}</span><span class="p">;</span> @@ -682,7 +682,7 @@ <span class="nv">status</span> <span class="o">=</span> <span class="s2">"disabled"</span><span class="p">;</span> <span class="nv">compatible</span> <span class="o">=</span> <span class="s2">"rockchip,can-1.0"</span><span class="p">;</span> assigned-clocks <span class="o">=</span> <<span class="p">&</span>cru CLK_CAN2><span class="p">;</span> - assigned-clock-rates <span class="o">=</span> <<span class="m">150000000</span>><span class="p">;</span> + assigned-clock-rates <span class="o">=</span> <<span class="m">100000000</span>><span class="p">;</span> pinctrl-names <span class="o">=</span> <span class="s2">"default"</span><span class="p">;</span> pinctrl-0 <span class="o">=</span> <<span class="p">&</span>can2m0_pins><span class="p">;</span> <span class="o">}</span><span class="p">;</span> |
Note:
- under some clock rate frequencies, the bitrate of CAN cannot obtain accurate rate. We can adjust the
assigned clock rates
to solve it - Check bitrate
1<span class="n">ip</span> <span class="o">-</span><span class="n">d</span> <span class="n">link</span> <span class="n">show</span> <span class="n">can1</span>