How to Configure CAN Bus in Linux on RK3568 Industrial Control Board


In the field of industrial control and automation, the CAN (Controller Area Network) bus is widely used for device communication due to its high reliability and real-time performance. This article will take the WTB-RK3568-S industrial control board from Weathink as an example to provide a detailed guide on configuring and using the CAN bus in a Linux environment. The RK3568 chip, developed by Rockchip, is a low-power, high-performance quad-core ARM SoC that integrates rich interfaces and multimedia capabilities, making it highly suitable for industrial control, IoT, and embedded device applications. Through this guide, developers can quickly get started with CAN bus configuration.


1. Hardware Basics and Driver Loading

Before starting the configuration, ensure that the CAN transceiver on your Weathink WTB-RK3568-S industrial control board is correctly connected. Most industrial control boards integrate a CAN controller, and its driver is usually included in the Linux kernel. Our task is to enable these drivers and configure the hardware resources properly.


2. Device Tree (DTS) Configuration

The Device Tree (DTS) is a file used by the Linux kernel to describe hardware resources, informing the kernel about the available hardware devices and how to interact with them. For the RK3568, the CAN controller must be enabled and configured in the device tree file.Typically, you can find your device tree file in the arch/arm64/boot/dts/rockchip/directory, such as rk3568-xxxx-board.dts. In this file, you need to locate or add the CAN controller node as shown below:

DTS:


// CAN1 Controller Configuration  

&can1 {  

    compatible = "rockchip,rk3568-can-2.0"; // Compatibility, matches the driver  

    assigned-clocks = <&cru clk_can1="">; // Specifies the clock source  

    assigned-clock-rates =<200000000>; // Sets the clock frequency to 200MHz  

    pinctrl-names = "default"; // Pin control name  

    pinctrl-0 = <&can1m1_pins>; // Specifies the pin configuration for CAN1  

    status = "okay"; // Enables this device node  

};  


// CAN2 Controller Configuration  

&can2 {  

    compatible = "rockchip,rk3568-can-2.0"; // Compatibility, matches the driver  

    assigned-clocks = <&cru clk_can2="">; // Specifies the clock source  

    assigned-clock-rates =<200000000>; // Sets the clock frequency to 200MHz  

    pinctrl-names = "default"; // Pin control name  

    pinctrl-0 = <&can2m0_pins>; // Specifies the pin configuration for CAN2  

    status = "okay"; // Enables this device node  

};

This configuration defines two CAN controllers: CAN1 and CAN2. status = "okay"ensures these controllers are enabled at system startup. The assigned-clock-ratessets the clock frequency, which is the basis for CAN baud rate calculation—here, it is set to 200MHz. pinctrl-0points to the specific GPIO pin configuration, ensuring the CAN_TX and CAN_RX pins function correctly. After making these changes, you need to recompile the device tree and update it on the industrial control board.


3. Kernel defconfig Configuration

In addition to the device tree, you must enable the corresponding CAN driver modules in the kernel configuration file. Locate your kernel defconfig file (usually rockchip_defconfig) and ensure the following configurations are set to y:

CONFIG_CAN=y              // Enables the CAN core support in the Linux kernel  

CONFIG_CAN_DEV=y          // Enables the CAN device driver framework  

CONFIG_CAN_ROCKCHIP=y     // Enables the Rockchip CAN controller driver  

CONFIG_CANFD_ROCKCHIP=y   // Enables Rockchip CAN-FD support (optional)

These configurations enable the CAN subsystem in the Linux kernel and load the dedicated CAN driver for Rockchip chips, allowing the system to recognize and operate the CAN controller. After modifying, recompile the kernel and update it on the industrial control board.


4. Configuration and Testing

After completing the device tree and kernel configurations and rebooting the system, the CAN devices should be correctly recognized as can0and can1. Next, you can use the ipand can-utilstools to configure and test the CAN bus.


4.1 Installing the can-utils Package

First, install the can-utilspackage:

sudo apt-get install can-utils

Configure the CAN interface using the ipcommand to set the baud rate and enable it. Here, both interfaces are set to a baud rate of 1Mbps.


# Configure the can0 interface with a baud rate of 1Mbps and enable it  

sudo ip link set can0 up type can bitrate 1000000  


# Configure the can1 interface with a baud rate of 1Mbps and enable it  

sudo ip link set can1 up type can bitrate 1000000

After executing these commands, you can check the interface status using ip link show can0or ip -details link show can1to confirm they are successfully enabled.


Next, perform a simple loopback test to verify whether the CAN bus is functioning correctly. If you have two physical CAN interfaces connected, you can test by sending from one interface and receiving on the other.


4.2 Test Steps:

In one terminal, start the candumpcommand to listen on the can0interface:

candump can0

The terminal will now wait to receive data from can0.

In another terminal, send a CAN message to the can1interface:

cansend can1 123#1122334455667788

This command sends a message with ID 123and data 1122334455667788.

In the first terminal, you should see can0receiving this message, proving successful communication between the two CAN interfaces. The output will resemble:

can0  123   [8]  11 22 33 44 55 66 77 88

For a reverse test, start listening on can1:

candump can1

In a new terminal, send a message to can0:

cansend can0 123#1122334455667799

In the candumpterminal, you will see can1receiving this message, confirming successful bidirectional CAN communication.


5.Summary

By following these steps, you can successfully configure and use the CAN bus on the Weathink WTB-RK3568-S industrial control board. These basic configurations and testing methods are essential for subsequent CAN application development.

Hangzhou Weathink Electronics Co., Ltd. is a professional Single Board Computers design company specializing in Rockchip solutions. We welcome you to contact us anytime. Our independently designed RK3568 SOM  provide stable and reliable industrial control solutions.


Tags: Array