CAN Testing

Rebuild Rootfs

Reconfiguration

When configuring CAN communication rate and loopback mode operations later, the ip command needs to be used. However, when using the ip command compiled by default with PetaLinux, the following error message appears:

According to the instructions in the reference material, the iproute2 component needs to be correctly configured in the rootfs to obtain full-featured ip command support:

Reference material:

65243 - PetaLinux 2014.4 - The "ip" Command Fails to Properly Locate CAN Devices

Compilation and Packaging

After completing the previous step 'petalinux-config -c rootfs', use the following commands to recompile and package:

Update Filesystem to TF Card

Clear the contents of the previous second partition of the TF card, and extract the ./image/rootfs.tar.gz file to the second partition of the TF card using the following command:

Insert the TF card into the board, select the SD card boot mode with the jumper cap, connect the serial port to the board, and power it on.

Add Code to Vitis Application Project

Right-click on src to create a new folder:

image-20250710140513635

After creating the relevant .c and .h files, it should look like the following:

image-20250714102016171

Compilation and Debugging

Add the folder containing .h files to the path:

image-20250714102318467

Testing via Commands

Open the CAN channel and set the baud rate using commands. The relevant commands are as follows:

When changing the working state of CAN, must first turn off can0, otherwise it will report "Device or resource busy"

image-20250714111708984

image-20250714111809960

Testing via Vitis Project

The Linux system manages CAN devices as network devices and provides the SocketCAN interface, making CAN bus communication similar to Ethernet, with more universal and flexible application development interfaces.

Initialization

CAN initialization and configuration are completed by using the system() system call to execute the corresponding commands mentioned earlier.

Most data structures and functions in SocketCAN are defined in linux/can.h, and the creation of CAN bus sockets is done using standard network sockets.

Data Transmission

Each time the CAN bus receives data, it is in units of can_frame. This structure is defined as follows:

can_id is the frame identifier. If sending a standard frame, use the lower 11 bits of can_id; if it's an extended frame, use bits 0~28. The lower 29th, 30th, and 31st bits of can_id are frame identifier bits used to define the frame type, as shown below:

Data transmission uses the write function. To send a data frame with identifier 0x123 and data of one byte 0xAB, the method is as follows:

If sending a remote frame, then frame.can_id = CAN_RTR_FLAG | 0x123

Data Reception

Data reception uses the read function, implemented as follows:

Creating a Receive Thread

When testing CAN communication programs under Linux, it is usually necessary to create a thread responsible for reception. Using threads can fully utilize the task scheduling and resource reuse provided by the Linux kernel.