Introduction
MCP2515
is an SPI-to-CAN module.
It is recommended to perform this operation after completing and mastering the entire process of the Taishan-RK3566 mobile project, as it will be easier to handle.
Target Platform: LCSC Taishan-RK3566-Linux Dev Board
Connection
The SPI3 pins of the RK3566 (LCSC Taishan-RK3566-Linux Dev Board) are connected to the SPI interface of the MCP2515. Additionally, the MCP2515 requires an interrupt pin. The relevant pins used are as follows:
Where GPIO3_A6 is the interrupt pin.
Porting Process
1. Configure the Kernel
Navigate to the SDK/kernel directory and execute: make ARCH=arm64 menuconfig
to open the relevant configuration options, as shown in the following images: ↓
Enable CAN_ROCKCHIP and CAN_MCP251X options.
Save the configuration, and after generating the .config file, use the command: make ARCH=arm64 savedefconfig
. This will generate a defconfig file in the sdk/kernel directory.
In fact, the config file accessed by the compiler is located in sdk/kernel/arch/arm64/configs/ and is named rockchip_linux_defconfig.To avoid issues, you can rename the source file to rockchip_linux_defconfig.bak.
Next, in the sdk/kernel directory, run the following command:
mv defconfig arch/arm64/configs/rockchip_linux_defconfig
2. Modify the Driver File
The driver file for MCP2515 is cmp251x.c, located in the SDK/kernel/drivers/net/can/spi directory. Below is the complete content of the mcp251x.c file. Please replace the original content with this new one!!
【cmp251x.c】Download
📌 Download Center (Click to Jump)
📌 Go to Download Center
->Baidu Netdisk Resource Content
-> Chapter 03. Documentation Manuals
->[User-contributed] Document Attachments
->【User-contributed】SPI to CAN
Folder.
3. Configure the DTS
The device tree file is located in SDK/kernel/arch/arm64/boot/dts/rockchip, and is named tspi-rk3566-user-v10-linux.dts.
Find the SPI3 node. The original content of the SPI3 node is:
status = "okay";
max-freq = <48000000>;
dma-names = "tx","rx";
pinctrl-names = "default", "high_speed";
pinctrl-0 = <&spi3m1_cs0 &spi3m1_pins>;
pinctrl-1 = <&spi3m1_cs0 &spi3m1_pins_hs>;
spi_test@10 {
compatible ="rockchip,spi_test_bus1_cs0";
reg = <0>;
spi-max-frequency = <24000000>;
status = "okay";
};
2
3
4
5
6
7
8
9
10
11
12
Replace it with the following:
status = "okay";
max-freq = <48000000>;
//dma-names = "tx","rx";
pinctrl-names = "default", "high_speed";
pinctrl-0 = <&spi3m1_cs0 &spi3m1_pins>;
pinctrl-1 = <&spi3m1_cs0 &spi3m1_pins_hs>;
//pinctrl-0 is "default" pinctrl-1 is "high_speed"
mcp2515: can@00 {
status = "okay";
compatible ="microchip,mcp2515";
reg = <0x00>;
clocks = <&mcp251x_clk>; //mcp 2515 clock
interrupt-parent = <&gpio3>; //interrupt pin group3 ->gpio3_A6
interrupts = <RK_PA6 IRQ_TYPE_EDGE_FALLING>; //A0 = 0
spi-max-frequency = <10000000>;
pinctrl-names = "default";
pinctrl-0 = <&mcp2515_irq1_pins>;
poll_mode = <0>;
enable_dma = <1>;
};
mcp251x_clk:mcp251x-clk{
compatible="fixed-clock";
#clock-cells = <0>;
clock-frequency = <8000000>; //mcp2515 jingzhenpinlv
};
};
&pinctrl {
mcp2515 {
mcp2515_irq1_pins: mcp2515-irq1-pins {
rockchip,pins = <3 RK_PA6 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Based on the original SPI3 pins, the gpio3_pa6 is added as the interrupt pin.
4. Adding Configuration and Testing Tools in Buildroot
Open the rockchip_rk3566_defconfig file under SDK/buildroot/configs. Buildroot packages can be enabled for compilation here by adding the following fields:
BR2_PACKAGE_CAN_UTILS=y
BR2_PACKAGE_IPROUTE2=y
BR2_PACKAGE_LIBSOCKETCAN=y
These actions add the necessary tools: ip and canutils. The ip tool configures the CAN socket parameters, and canutils contains multiple tools for testing the CAN bus.
Next, go to the SDK directory and run the command: ./build.sh buildroot
This command is currently known to only work on the LCSC Taishan-RK3566-Linux Dev Board; other target boards may need adaptations. The purpose of this command is to compile Buildroot.
Common Issues (specific to the LCSC Taishan-RK3566-Linux Dev Board)
During compilation, the iproute tool may produce an error like first defined here multiple-definition. This is simple to resolve—find the duplicate symbols and rename them.
For example, if there's an error because the variable X is duplicated in a.o (from a.c) and b.o (from b.c), resolve this by renaming X to X1 in b.c. If you get another duplicate warning with X1, rename it to X2 in the other file.
5. Tool Replacement
Friendly Reminder: Please back up the current firmware on the target device before replacing tools, as system startup may be affected after replacement.
In Step 4, you compiled the iproute tool. Once compiled, an executable file named ip will be generated in the following path:
SDK/buildroot/output/rockchip_rk3566/build/iproute2-4.14.1/ip
Use the adb push command to temporarily place this file in the /etc folder on the target device.
In Windows PowerShell, the command is adb push ip /etc
, where ip is the file to be uploaded, and /etc is the destination path.
Once uploaded, you’ll find the ip executable file in the /etc folder on the target device. Since files pushed by adb do not have execute permissions, add execute permissions to ip in /etc with the command chmod +x ip
.
After adding permissions, run ./ip -V
to check the version of the ip executable file.
Replace the original ip executable file on the target machine with /etc/ip. Please operate cautiously and verify the correctness of the command. /etc/ip is the executable file generated from the iproute compilation above. On the target machine:
cd /etc
to navigate to the etc folder.cp ip /sbin/ip -f
to complete the replacement.
After replacing, return to the root directory of the target device to test the availability of the ip command.
This output indicates that the ip command is now functioning.
6. Configuring and Starting the CAN Device
To check if the CAN bus driver is loaded, use the ifconfig -a
command while the hardware is connected to inspect the device status.
Using ifconfig
, you can view the status of the successfully started device.
As seen, can0 has successfully loaded but is not yet started. At this point, configure the CAN bus bitrate by using the command ip link set can0 type can bitrate 250000
, where can0 corresponds to the name shown after running ifconfig -a, and 250000 is the bitrate, with an actual speed of 250K.
There’s also a loopback mode, which can be enabled with ip link set can0 type can bitrate 250000 loopback on
, followed by the usual setup.
After setting the bitrate, start can0 using the command ifconfig can0 up
to activate can0
.
Now, running ifconfig
again will show that can0 is active.
7. Testing CAN Transmission and Reception
This is for CAN testing only, as RK’s official support for CANFD is incomplete.
The previously compiled canutils package includes two tools, candump and cansend: cansend sends data, and candump receives data.
To Test CAN Reception, Connect the CAN Test Tool and Enter the Command on the Target Board
Use candump can0 &
to view the received data in the serial console (not tested in adb shell).
The CAN test tool sends data.
The target board receives the data as shown below:
Here, can0 is the MCP2515 device name on the target board, 000 is the sender’s address corresponding to the Frame ID in the image above, [] indicates the length, and 01234567 is the received data.
To Test CAN Transmission, Connect the CAN Test Tool and Enter the Command on the Target Board
Start the cansend tool to send data with a frame ID of 18E9F4C9 and content 12 23 34 45 56 67 78 89, with a length of 8 bytes.
can0 represents MCP2515, 18E9F4C9 is the Frame ID, and the data follows after #, separated by dots ..
The CAN test tool receives the data as shown below: