Skip to main content

CAN Debugging Guide

Preface​

This chapter mainly introduces the S100 SocketCAN configuration and error debugging methods.

MCU-Side Initialization Configuration​

The STBY pin of the CAN transceiver is high by default, which keeps the transceiver in STANDBY mode. The MCU side must pull the STBY pin low to exit STANDBY mode; otherwise, the CAN transceiver cannot work properly. The MCU side needs to execute the following code:

/* Set the clock source of CAN0~9 according to the actual situation, consistent with the device tree (80M); call this after McuFunc_ModuleInit/Mcu_InitClock */
writel((__IO uint32 *)(0x2370007c), 0x8001);
writel((__IO uint32 *)(0x23700080), 0x8001);
writel((__IO uint32 *)(0x23700084), 0x8001);
writel((__IO uint32 *)(0x23700088), 0x8001);
writel((__IO uint32 *)(0x2370008c), 0x8001);
writel((__IO uint32 *)(0x23700090), 0x8001);
writel((__IO uint32 *)(0x23700094), 0x8001);
writel((__IO uint32 *)(0x23700098), 0x8001);
writel((__IO uint32 *)(0x2370009c), 0x8001);
writel((__IO uint32 *)(0x237000a0), 0x8001);

/* Pull STBY low to enable the CAN transceiver */
Port_Lld_SetPinModeWithoutCheck(6, PORT_PINMUX_GPIO, S100_PORT_MCU);
Port_Lld_SetGpioDirWithoutCheck(6, PORT_PIN_DIR_OUT, S100_PORT_MCU);
Dio_WriteChannel(6, STD_LOW);

Device Tree Code​

dts files related to CAN configuration in the S100:

|-- source/hobot-drivers/kernel-dts/drobot-s100-soc.dtsi   # canfd0~canfd5 device nodes

Device Tree Configuration Description​

The S100 A-Core side has a total of 6 FlexCAN nodes:

canfd1: canfd@0x23720000 {
status = "okay";
compatible = "hobot,s100-flexcan";
reg = <0x0 0x23720000 0x0 0x4000>;
interrupts = <GIC_SPI MCUSYS_CANFD5_IPI_MB_INTR IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI MCUSYS_CANFD5_IPI_ERR_INTR IRQ_TYPE_LEVEL_HIGH>;
clock-frequency = <80000000>;
fsl,clk-source = <0>;
};

The mapping between each node and the MCU-side CAN-FD is as follows:

A-Core Device NodeMCU-Side CAN ControllerRegister AddressDefault Status
canfd0MCU canfd30x235b0000disabled
canfd1MCU canfd50x23720000disabled
canfd2MCU canfd60x23730000disabled
canfd3MCU canfd70x23740000disabled
canfd4MCU canfd80x23750000disabled
canfd5MCU canfd90x23760000disabled

Configuration item descriptions:

  • compatible = "hobot,s100-flexcan": vendor and model.
  • reg: register address range occupied by the device.
  • interrupts: two interrupts, the Mailbox TX/RX interrupt (_IPI_MB_INTR) and the error interrupt (_IPI_ERR_INTR).
  • clock-frequency: CAN module clock, 80 MHz; the baud rate calculation is based on this value.
  • fsl,clk-source: clock source selection.

Environment Setup and Testing​

This section uses the default-enabled can1 (canfd1, MCU-side canfd5) as an example to introduce the basic functions of SocketCAN.

Test Environment Preparation​

The board must have iproute2 (the ip command) and can-utils (candump/cansend).

1. Load the FlexCAN Driver​

If the FlexCAN driver is compiled as a kernel module, load it first:

modprobe flexcan

After loading, use ip link to confirm that the can0~can5 devices are present:

ip link show

If the driver is built in to the kernel image, this step is not required; the CAN devices are visible directly after the system boots.

2. Configure the Bitrate and Enable the Interface​

Classic CAN

# Bring the interface down first (parameters can only be changed in the DOWN state)
ip link set can1 down
# Configure the bitrate and sample point
ip link set can1 type can bitrate 500000 sample-point 0.8
# Enable the interface
ip link set can1 up

CAN-FD

# Bring the interface down first (parameters can only be changed in the DOWN state)
ip link set can1 down
# Configure CAN FD: arbitration phase 1 Mbps, sample point 80%; data phase 5 Mbps, sample point 75%
ip link set can1 type can bitrate 1000000 sample-point 0.8 dbitrate 5000000 dsample-point 0.75 fd on
# Enable the interface
ip link set can1 up
ParameterDescriptionExample
bitrate NArbitration-phase bitrate / classic CAN bitratebitrate 500000
dbitrate NCAN FD data-phase bitratedbitrate 5000000
restart-ms NAutomatically recover N ms after bus-offrestart-ms 3000
berr-reporting on/offReport bus error framesberr-reporting on
loopback on/offInternal loopbackloopback on
listen-only on/offListen-only modelisten-only on
one-shot on/offDo not automatically retransmit after a failed transmissionone-shot on
fd on/offCAN FD mode supportfd on
tip

When debugging error states, it is recommended to enable berr-reporting on; otherwise, the driver will not report bus error frames such as BIT0/CRC, and candump will not show error details.

Basic Transmit/Receive Test​

Internal Loopback Test​

ip link set can1 down
ip link set can1 type can bitrate 500000 loopback on
ip link set can1 up

# Terminal A: capture frames
candump can1 &
# Terminal B: send a standard frame, ID=0x123, 8 data bytes
cansend can1 123#1122334455667788

In loopback mode, transmitted frames are received by the interface itself, and candump should print can1 123 [8] 11 22 33 44 55 66 77 88.

Two-Node Closed-Loop Test​

Connect the CAN_H/CAN_L of two CAN channels (for example, can1 and can2), and connect a 120Ω termination resistor at each end (on the S100 MCU daughter board, connect it via a jumper cap). Send from one end and receive at the other:

# Node A (can1)
cansend can1 123#AABBCCDD
# Node B (can2)
candump can2

SocketCAN Debugging​

Viewing Interface Status and Error Counters​

# View detailed CAN parameters
ip -s -d link show can1

Example output:

18: can1: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10
link/can promiscuity 0 minmtu 0 maxmtu 0
can <BERR-REPORTING> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 3000
bitrate 500000 sample-point 0.875
tq 25 prop-seg 37 phase-seg1 32 phase-seg2 10 sjw 1
flexcan: tseg1 2..96 tseg2 2..32 sjw 1..16 brp 1..1024 brp-inc 1
flexcan: dtseg1 2..39 dtseg2 2..8 dsjw 1..4 dbrp 1..1024 dbrp-inc 1
clock 80000000
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 0 0 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 parentbus platform parentdev 23740000.canfd
RX: bytes packets errors dropped missed mcast
4 1 0 0 0 0
TX: bytes packets errors dropped carrier collsns
4 1 0 0 0 0

Error State Machine and bus-off​

The CAN controller maintains an error state machine based on TEC/REC; different counter values correspond to different error levels:

StateTEC / RECMeaningReported As
ERROR-ACTIVE0 ~ 95Normal transmit/receive, active error flag—
ERROR-WARNING96 ~ 127Error warningCAN_ERR_CRTL + TX/RX_WARNING
ERROR-PASSIVE128 ~ 255Passive error, transmit/receive restrictedCAN_ERR_CRTL + TX/RX_PASSIVE
BUS-OFF≥ 256Bus off, transmit/receive stoppedCAN_ERR_BUSOFF

Understanding Error Frame Fields​

After enabling berr-reporting on, error frames can be seen with candump -e can1. An error frame is a struct can_frame whose can_id carries CAN_ERR_FLAG (0x20000000), with len=8:

FieldMeaningCommon Values
can_id bitsError categoryCAN_ERR_BUSOFF=0x40, CAN_ERR_CRTL=0x04, CAN_ERR_PROT=0x08, CAN_ERR_ACK=0x20, CAN_ERR_BUSERROR=0x80, CAN_ERR_RESTARTED=0x100
data[1]CRTL statusCAN_ERR_CRTL_RX_WARNING=0x04, TX_WARNING=0x08, RX_PASSIVE=0x10, TX_PASSIVE=0x20
data[2]PROT error typeCAN_ERR_PROT_BIT0=0x08, BIT1=0x10, BIT(single/CRC)=0x01, FORM=0x02, STUFF=0x04
data[3]PROT error locationCAN_ERR_PROT_LOC_ACK=0x19, CAN_ERR_PROT_LOC_CRC_SEQ=0x08, etc.

For example, when a bus-off occurs, a frame with can_id=0x20000040 is usually received first (reporting entry into the BUS-OFF state), immediately followed by a frame with can_id=0x20000088 (reporting the specific bus error, where data[2]=0x08 indicates BIT0). See the table above for the correspondence between error categories and field meanings.

bus-off Recovery​

Recovery falls into two categories: automatic recovery (recovery occurs automatically when the timer set by restart-ms expires) and manual soft reset. Both ultimately bring the controller back to the normal transmit/receive state (ERROR-ACTIVE) through a soft reset.

Automatic Recovery (restart-ms > 0)​

ip link set can1 down
ip link set can1 type can restart-ms 3000 # Automatically recover 3 seconds after bus-off
ip link set can1 up

After bus-off, the driver automatically performs a soft reset when the time set by restart-ms expires, and sends a CAN_ERR_RESTARTED frame to notify the application layer.

Manual Soft Reset​

In addition to automatic recovery, a soft reset can be triggered manually in two ways:

Method 1: the restart command

ip link set can1 type can restart

This command triggers a soft reset. After recovery, the interface carrier is turned on again, the state returns to ERROR-ACTIVE, and a CAN_ERR_RESTARTED frame is sent to the application layer. It succeeds only when restart_ms == 0 and state == BUS_OFF; otherwise, it returns:

ErrorCauseSolution
Invalid argument (-EINVAL)restart_ms != 0 (manual restart is not allowed while automatic recovery is enabled)Run down, set restart-ms 0, then run up
Device or resource busy (-EBUSY)state != BUS_OFF (bus-off has not actually occurred)Confirm that bus-off has actually occurred; an ACK error only reaches error-passive, which does not count as bus-off

Method 2: down + up

ip link set can1 down
ip link set can1 up

Running down followed by up reinitializes the entire interface (re-requesting interrupts and transmit/receive resources and performing a soft reset), returning the state to ERROR-ACTIVE.

caution

If the MCU-side CAN transceiver is not enabled and causes bus-off, both automatic recovery and manual soft reset will "immediately enter bus-off again after a successful recovery", appearing as a repeated bus-off -> restart -> bus-off loop. In this case, check the MCU-side physical layer.

Error Frame Monitoring: User-Space Example​

The following is an example error frame monitoring program based on a RAW socket, which can decode BUS OFF / ACK / CRC and other information (requires berr-reporting on):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/can/error.h>
#include <linux/can/netlink.h>

static volatile int running = 1;
static void sig_handler(int s) { running = 0; }

/* Decode the error frame */
static void parse_err_frame(const struct can_frame *cf, struct timeval *tv) {
printf("[%ld.%06ld] ", tv->tv_sec, tv->tv_usec);
canid_t id = cf->can_id;

/* Error category */
if (id & CAN_ERR_BUSOFF) printf("[BUS-OFF] ");
if (id & CAN_ERR_ACK) printf("[ACK_ERR] ");
if (id & CAN_ERR_RESTARTED) printf("[RESTARTED] ");

/* Controller status */
if (id & CAN_ERR_CRTL) {
printf("[CRTL] ");
if (cf->data[1] & CAN_ERR_CRTL_TX_WARNING) printf("TX_WRN ");
if (cf->data[1] & CAN_ERR_CRTL_RX_WARNING) printf("RX_WRN ");
if (cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) printf("TX_PASSIVE ");
if (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE) printf("RX_PASSIVE ");
}

/* Protocol error type */
if (id & CAN_ERR_PROT) {
printf("[PROT] ");
if (cf->data[2] & CAN_ERR_PROT_BIT0) printf("BIT0_ERR ");
if (cf->data[2] & CAN_ERR_PROT_BIT1) printf("BIT1_ERR ");
if (cf->data[2] & CAN_ERR_PROT_FORM) printf("FORM_ERR ");
if (cf->data[2] & CAN_ERR_PROT_STUFF) printf("STUFF_ERR ");
if (cf->data[2] & CAN_ERR_PROT_BIT) printf("CRC_ERR ");
}

printf("\n");
}

int main(int argc, char **argv) {
/* Interface name, can be specified via a command-line argument, defaults to can1 */
const char *ifname = argc > 1 ? argv[1] : "can1";

struct sigaction sa = { .sa_handler = sig_handler };
sigaction(SIGINT, &sa, NULL);

/* Create a CAN_RAW socket */
int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);

/* Get the index of the specified CAN interface */
struct ifreq ifr = {0};
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
ioctl(s, SIOCGIFINDEX, &ifr);

/* Clear the normal data frame filter: length 0, so no normal CAN data frames are received */
struct can_filter empty[0];
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, empty, 0);

/* Enable error message frame filtering: receive all types of errors */
can_err_mask_t err_mask = CAN_ERR_MASK;
setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask));

/* Enable timestamps */
int enable = 1;
setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &enable, sizeof(enable));

/* Bind the socket to the specified CAN interface */
struct sockaddr_can addr = {
.can_family = AF_CAN,
.can_ifindex = ifr.ifr_ifindex
};
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
close(s);
return 1;
}

printf("Listening for CAN error frames on %s ... (Ctrl+C to exit)\n", ifname);

struct can_frame cf;
struct msghdr msg = {0};
struct iovec iov;
unsigned char ctrl[1024];

iov.iov_base = &cf;
iov.iov_len = sizeof(cf);

msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ctrl;
msg.msg_controllen = sizeof(ctrl);

while (running) {
msg.msg_controllen = sizeof(ctrl);
ssize_t n = recvmsg(s, &msg, 0);

if (n == sizeof(cf) && (cf.can_id & CAN_ERR_FLAG)) {
struct timeval tv = {0, 0};
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);

while (cmsg) {
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SO_TIMESTAMP) {
tv = *(struct timeval *)CMSG_DATA(cmsg);
break;
}
cmsg = CMSG_NXTHDR(&msg, cmsg);
}

parse_err_frame(&cf, &tv);
}
}
close(s);
return 0;
}

Usage:

# Enable error reporting and set automatic recovery to 3s
ip link set can1 type can berr-reporting on restart-ms 3000
# Compile and run
gcc can_err_monitor.c -o can_err_monitor
./can_err_monitor can1

Example output

Listening for CAN error frames on can1 ... (Ctrl+C to exit)
[1786678280.362306] [CRTL] TX_WRN
[1786678280.362316] [PROT] BIT0_ERR
[1786678280.362322] [CRTL] TX_PASSIVE # Passive error
[1786678280.362323] [PROT] BIT0_ERR
[1786678280.362338] [PROT] BIT0_ERR
[1786678280.362363] [PROT] BIT0_ERR
[1786678280.362390] [PROT] BIT0_ERR
[1786678280.362415] [PROT] BIT0_ERR
[1786678280.362441] [PROT] BIT0_ERR
[1786678280.362469] [PROT] BIT0_ERR
[1786678280.362494] [PROT] BIT0_ERR
[1786678280.362518] [PROT] BIT0_ERR
[1786678280.362542] [PROT] BIT0_ERR
[1786678280.362569] [PROT] BIT0_ERR
[1786678280.362597] [PROT] BIT0_ERR
[1786678280.362623] [PROT] BIT0_ERR
[1786678280.362650] [PROT] BIT0_ERR
[1786678280.362673] [PROT] BIT0_ERR
[1786678280.362713] [BUS-OFF] # A [BIT0_ERR] frame follows the [BUS-OFF] frame; the hardware timestamps of the two frames are almost identical (Δt=2us) because they come from the same interrupt: the driver reports the state change first, then the bus error
[1786678280.362715] [PROT] BIT0_ERR
[1786678283.389491] [RESTARTED]

Error Frame Monitoring: Kernel Logs​

  1. First raise the flexcan log level (the driver must be loaded first):
echo 'file flexcan-core.c +p' > /sys/kernel/debug/dynamic_debug/control
  1. View the kernel logs (dmesg -w):
[ 3877.853136] IPv6: ADDRCONF(NETDEV_CHANGE): can1: link becomes ready
[ 4238.143737] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143767] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143789] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143814] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143841] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143868] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143892] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143917] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143944] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143969] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.143995] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.144022] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.144048] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.144073] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.144097] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4238.144125] flexcan 23740000.canfd can1: bus-off, scheduling restart in 3000 ms
[ 4238.144135] flexcan 23740000.canfd can1: BIT0_ERR irq
[ 4241.368721] IPv6: ADDRCONF(NETDEV_CHANGE): can1: link becomes ready
[ 4241.368722] flexcan 23740000.canfd can1: writing ctrl=0x0000c050
[ 4241.368728] flexcan 23740000.canfd can1: writing cbt=0x802093e9
[ 4241.368731] flexcan 23740000.canfd can1: writing fdctrl=0x06c00000
[ 4241.368734] flexcan 23740000.canfd can1: flexcan_set_bittiming_cbt: mcr=0x5980000f ctrl=0x0000c050 ctrl2=0x80633800 fdctrl=0x06c00000 cbt=0x80000000 fdcbt=0x00000000
[ 4241.368739] flexcan 23740000.canfd can1: flexcan_chip_start: writing mcr=0x59a3023f
[ 4241.368742] flexcan 23740000.canfd can1: flexcan_chip_start: writing ctrl=0x00000050
[ 4241.368745] flexcan 23740000.canfd can1: flexcan_chip_start: writing fdctrl=0x06c00000
[ 4241.368795] flexcan 23740000.canfd can1: flexcan_chip_start: reading mcr=0x40a3023f ctrl=0x00000050

The S600 Acore/Main domain has 4 CAN channels. When these 4 CAN channels cannot meet the business requirements, Acore can also directly control the MCU-domain CAN controllers, taking over up to 6 additional channels, for a maximum of 10 CAN channels in total. This feature requires configuration changes on both the Acore and MCU sides. For details, see Section 3.

This document is a quick-start guide and consists of three parts:

  1. Standard SocketCAN usage on Acore: modify the device tree + load modules + pull down the CAN_STB pin;
  2. Bypass protocol stack (Fastpath) usage: module loading and user-space mmap/poll transmission and reception;
  3. Acore takes over MCU-domain CAN: MCU-side configuration changes for expanding the CAN channel count.

Driver Code​

drivers/net/can/flexcan/flexcan-core.c      # FlexCAN driver core (TX/RX, interrupts, bit timing, Mailbox management)
drivers/net/can/flexcan/flexcan-ethtool.c # ethtool statistics and ring parameter interfaces
drivers/net/can/flexcan/flexcan.h # driver header file and register definitions

Kernel Configuration Location​

Configuration file path: hobot-drivers/configs/drobot_s600_defconfig

CONFIG_CAN=m                    # CAN protocol family framework
CONFIG_CAN_RAW=m # CAN raw socket protocol
CONFIG_CAN_FLEXCAN=m # FlexCAN hardware driver
CONFIG_CAN_FLEXCAN_FASTPATH=y # bypass protocol stack (Fastpath), compiled by default

Kernel DTS Node Configuration​

The S600 CAN nodes are divided into two groups:

GroupNodeBase Address RangeClock FrequencypinctrlDefault Status
Acore/Main domaincanfd0~canfd30x34860000~0x3486C000200MHzYesdisabled
MCU domaincanfd4~canfd90x23870000~0x238e000080MHzNodisabled

The device tree source file is located at hobot-drivers/kernel-dts/drobot-s600-soc.dtsi.

Acore/Main domain CAN node (using canfd0 as an example):

canfd0: canfd@0x34860000 {
status = "disabled";
compatible = "hobot,s600-flexcan";
reg = <0x0 0x34860000 0x0 0x3400>;
interrupts = <GIC_SPI HSISYS_CANFD0_IPI_MB_INTR IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI HSISYS_CANFD0_IPI_ERR_INTR IRQ_TYPE_LEVEL_HIGH>;
clock-frequency = <200000000>;
fsl,clk-source = <0>;
pinctrl-names = "default";
pinctrl-0 = <&hsi_peri_canfd0_txd &hsi_peri_canfd0_rxd>;
};
/* canfd1/canfd2/canfd3 follow the same pattern, with base addresses 0x34864000 / 0x34868000 / 0x3486C000 */

MCU domain CAN node (using canfd4 as an example, disabled by default, change to okay when Acore takes over):

canfd4: canfd@0x23870000 {
status = "disabled"; /* change to okay when Acore takes over */
compatible = "hobot,s600-flexcan";
reg = <0x0 0x23870000 0x0 0x4000>;
interrupts = <GIC_SPI MCUSYS_CANFD1_IPI_MB_INTR IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI MCUSYS_CANFD1_IPI_ERR_INTR IRQ_TYPE_LEVEL_HIGH>;
clock-frequency = <80000000>;
fsl,clk-source = <0>;
};
/* canfd5~canfd9 follow the same pattern, with base addresses 0x23880000/0x238b0000/0x238c0000/0x238d0000/0x238e0000 */
Pin Multiplexing and Conflicts

The TX/RX pins of the 4 Acore/Main domain CAN channels are all multiplexed with other peripherals. Before use, you must correctly configure pinctrl in the device tree and disable the conflicting peripherals:

CAN ControllerTXD PinRXD PinConflicting Peripheral
canfd0 (CAN0)hsi_spi0_misohsi_spi0_sclkSPI0
canfd1 (CAN1)hsi_spi0_csn0hsi_spi0_mosiSPI0
canfd2 (CAN2)hsi_uart3_txdhsi_uart3_rxdUART3
canfd3 (CAN3)hsi_uart4_txdhsi_uart4_rxdUART4
  • CAN0 + CAN1 share the same group of 4 pins with SPI0, so the two cannot be used simultaneously. Enabling CAN0/CAN1 requires disabling SPI0;
  • CAN2 conflicts with UART3 and CAN3 conflicts with UART4. Enabling the corresponding CAN requires disabling the corresponding UART.

Using CAN​

1. Standard SocketCAN Usage on Acore​

Using SocketCAN requires three steps: modify the device tree to enable the CAN nodes and disable conflicting peripherals, load three kernel modules, and pull down the CAN_STB pin to enable the transceiver.

1.1 Modify the Device Tree​

By default, the status of the 4 CAN nodes canfd0~canfd3 is disabled, while the conflicting spi0 may be okay. Before using SocketCAN, verify two things:

① Confirm the CAN nodes are enabled

Make sure the status of canfd0~canfd3 is okay (see the DTS node configuration above).

② Disable the conflicting SPI0

CAN0/CAN1 share the same group of 4 pins with SPI0. Before using CAN0 or CAN1, you must disable SPI0:

spi0: spi@34900000 {
...
status = "disabled"; /* okay → disabled */
...
};
Disable on Demand
  • As long as CAN0 or CAN1 is used, SPI0 must be disabled;
  • If only CAN2 is used, UART3 must be disabled; if only CAN3 is used, UART4 must be disabled.

After making the changes, recompile the device tree and flash it.

1.2 Load the CAN Modules (Three Modules)​

After system startup, load the following three kernel modules:

modprobe can        # CAN protocol family framework
modprobe can-raw # CAN raw socket protocol
modprobe flexcan # FlexCAN hardware driver
Module Dependencies

can-raw depends on can, and flexcan depends on can (including the can-dev device interface). Loading in the above order automatically handles the dependencies. If the drivers are built-in, manual loading is unnecessary — only the protocol modules need to be loaded.

1.3 Pull Down the CAN_STB Pin (Enable the Transceiver)​

The CAN transceiver standby control pin MAIN_CAN_STB is located on line 7 of the I2C GPIO expander chip tpt29555a@0x27, corresponding to gpio369 in the Linux sysfs. It must be pulled low for normal data transmission and reception:

echo out > /sys/class/gpio/gpio369/direction
echo 0 > /sys/class/gpio/gpio369/value
Persistence

You can add the above commands to a startup script (such as /etc/rc.local or a systemd service) to ensure the pin is pulled low automatically on every boot.

1.4 Configure the CAN Interface and Communicate​

After loading the modules and pulling down STB, configure the baud rate and bring up the interface (using CAN-FD as an example, with a nominal rate of 1 Mbps and a data rate of 5 Mbps):

# can0
ip link set can0 down
ip link set can0 type can fd on loopback off bitrate 1000000 sample-point 0.75 dbitrate 5000000 dsample-point 0.75
ip link set can0 up

# can1 / can2 / can3 follow the same pattern
ip link set can1 down
ip link set can1 type can fd on loopback off bitrate 1000000 sample-point 0.75 dbitrate 5000000 dsample-point 0.75
ip link set can1 up

Classic CAN (non-FD) configuration example (500 kbps):

ip link set can0 down
ip link set can0 type can bitrate 500000 sample-point 0.875
ip link set can0 up

Transmit/receive test:

# listen on the receiving end
candump can0

# send a standard frame
cansend can0 123#DEADBEEF

# send a CAN-FD frame (## indicates FD; the 0 is followed by the BRS flag)
cansend can0 123##0112233445566778899AABBCCDDEE

# send a remote frame
cansend can0 123#R

Operating modes (optional):

# loopback mode (self-test when no external node is connected)
ip link set can0 type can bitrate 500000 loopback on
# listen-only mode (only listens, does not send or acknowledge)
ip link set can0 type can bitrate 500000 listen-only on

1.5 Quick Verification Tools​

The official stress test tool can_stress and the interop verification tool can_pair_test are provided:

Source path: hobot-io-samples/debian/app/Can/socketcan/
├── can_stress.c # C stress/latency test tool
├── can_stress.py # Python version
├── can_pair_test.c # paired interop verification (can0<->can1, can2<->can3)
├── Makefile
└── README.md

Compilation and usage:

cd hobot-io-samples/debian/app/Can/socketcan
make

# single-channel loopback test (CAN-FD, 64 bytes, 1 ms period, 60 seconds)
sudo ./can_stress -i can0 -f 1 -l 64 -t 0x100 -r 0x100 -p 1 -w 1000 -L -D 60

# dual-channel concurrent interconnect test
sudo ./can_stress -i can0 -f 1 -l 64 -t 0x100 -r 0x101 -p 1 -w 1000 -L \
-i can1 -f 1 -l 64 -t 0x101 -r 0x100 -p 1 -w 1000 -L -D 60

# paired interop verification (hardware connection can0<->can1, can2<->can3)
sudo ./can_pair_test

1.6 Acquiring CAN Hardware Timestamps (RX)​

FlexCAN supports hardware timestamps for received frames: when a message reaches the controller, the hardware latches the SoC RTC time into the HR_TIME_STAMPn register. The driver reads this value back when receiving the frame, converts it to the CLOCK_REALTIME timeline, and finally SocketCAN reports it to user space through the SCM_TIMESTAMPING control message.

ItemDescription
Supported directionRX (receive) only; TX hardware timestamps are not supported
Time baseSoC RTC (32768 Hz tick); after conversion it falls on the CLOCK_REALTIME timeline
With FastpathAfter the bypass protocol stack is enabled, frames do not enter the protocol stack, so SocketCAN cannot obtain hardware timestamps
Prerequisites
  • The driver must have hardware timestamp capability (FLEXCAN_QUIRK_HW_TIMESTAMP);
  • The CAN interface is up, and the modules have been loaded and CAN_STB pulled low as described in Sections 1.2 and 1.3;
  • SIOCSHWTSTAMP requires root (or CAP_NET_ADMIN).

Three-step call flow

StepInterfacePurpose
1ioctl(SIOCSHWTSTAMP) with rx_filter = HWTSTAMP_FILTER_ALLTurn on the RX hardware timestamp switch for the entire network interface
2setsockopt(SO_TIMESTAMPING, SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_RAW_HARDWARE)Make the kernel deliver hardware timestamps to this socket
3recvmsg() parses the SCM_TIMESTAMPING cmsgThe hardware timestamp is in ts[2], in nanoseconds
Why read ts[2]

The SCM_TIMESTAMPING data is always three timespec values, and FlexCAN fills only the last one:

ts[0]  SOF_TIMESTAMPING_SOFTWARE      software timestamp
ts[1] SOF_TIMESTAMPING_SYS_HARDWARE converted hardware timestamp (not filled by flexcan)
ts[2] SOF_TIMESTAMPING_RAW_HARDWARE raw hardware timestamp <-- use this one

Key call stack:

[Enable] application ioctl(SIOCSHWTSTAMP)
sock_ioctl -> dev_ioctl -> dev_ifsioc -> dev_eth_ioctl
-> flexcan_eth_ioctl -> flexcan_set_hwtstamp_config # record rx_filter, set hwts.rx_enabled

[Receive] the CAN controller receives a message
flexcan_irq -> flexcan_mailbox_read -> flexcan_hwts_read_mb # read HR_TIME_STAMPn
-> skb_hwtstamps(skb)->hwtstamp = flexcan_hwts_to_ktime() # convert RTC time to CLOCK_REALTIME
-> netif_receive_skb -> can_receive -> raw_rcv -> socket receive queue

[Read] application recvmsg()
raw_recvmsg -> sock_recv_cmsgs -> __sock_recv_timestamp -> put_cmsg(SCM_TIMESTAMPING)

① Check interface capabilities

ethtool -T can0

If SOF_TIMESTAMPING_RX_HARDWARE / SOF_TIMESTAMPING_RAW_HARDWARE are valid, while SOF_TIMESTAMPING_TX_HARDWARE is empty and phc_index = -1, only RX hardware timestamps are supported.

② Enable/disable RX hardware timestamps

User space issues it through SIOCSHWTSTAMP (already wrapped in the sample program):

struct hwtstamp_config cfg = {
.flags = 0,
.tx_type = HWTSTAMP_TX_OFF, /* flexcan has no TX hardware timestamp */
.rx_filter = HWTSTAMP_FILTER_ALL, /* timestamp all RX frames in hardware */
};
struct ifreq ifr = { 0 };
int fd = socket(AF_INET, SOCK_DGRAM, 0);

strncpy(ifr.ifr_name, "can0", IFNAMSIZ - 1);
ifr.ifr_data = &cfg;
ioctl(fd, SIOCSHWTSTAMP, &ifr); /* requires CAP_NET_ADMIN;
* use SIOCGHWTSTAMP to read back the current configuration */

To disable it, set rx_filter to HWTSTAMP_FILTER_NONE and issue the call again.

Device-level switch

This switch applies to the entire network interface. As long as one process enables it, other sockets can also obtain hardware timestamps; ip link set canX down resets this state, and it must be issued again after bringing the interface back up.

③ Read hardware timestamps

unsigned int flags = SOF_TIMESTAMPING_RX_HARDWARE |
SOF_TIMESTAMPING_RAW_HARDWARE;
struct timespec ts[3]; /* [0]=SW [1]=reserved [2]=HW */
char ctrl[CMSG_SPACE(sizeof(ts))];

setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof(flags));

for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_TIMESTAMPING)
continue;
memcpy(ts, CMSG_DATA(cmsg), sizeof(ts));
printf("hw = %lld.%09ld\n",
(long long)ts[2].tv_sec, (long)ts[2].tv_nsec);
}

④ Compile and run the example

The complete code is in hobot-io-samples/debian/app/Can/socketcan/can_hwtstamp_rx.c (minimal example) and can_hwtstamp_test.c (diagnostic version: capability query, software timestamp comparison, latency statistics).

cd hobot-io-samples/debian/app/Can/socketcan
make can_hwtstamp_rx can_hwtstamp_test

Single-board self-test (hardware loopback, no second node required):

ip link set can0 down
ip link set can0 type can bitrate 500000 loopback on
ip link set can0 up

sudo ./can_hwtstamp_rx can0 &
cansend can0 123#1122334455667788
SIOCSHWTSTAMP ok: tx_type=0 rx_filter=1
id=0x123 len=8 no hw timestamp (tx echo frame or not enabled)
id=0x123 len=8 hw_ts=1789563324.123456789

The first line is the TX echo frame looped back from the local transmission (no hardware timestamp), and only the second line is the frame received through the hardware loopback; when testing with can0 <-> can1 on two nodes, no echo frame appears. Diagnostic version usage:

# Receive side: print capabilities, wait for 10 frames with hardware timestamps (-o filters echo frames)
sudo ./can_hwtstamp_test -T -s -n 10 -o can0
# Transmit side
cangen can1 -g 10 -I 123 -L 8

⑤ Common issues

SymptomCause / Handling
SIOCSHWTSTAMP: Operation not supportedThe driver has no hardware timestamp capability, or the interface is not flexcan
ts[2] is always 0SIOCSHWTSTAMP was not issued; the frame is a local TX echo; the interface went down and the call was not reissued
No timestamp after enabling FastpathBypass-mode frames do not enter the protocol stack; disable fastpath first
A fixed offset between the timestamp and dateHardware timestamps are converted from SoC RTC ticks; the RTC and system clock must stay synchronized

1.7 Errors and Statistics​

# network TX/RX statistics
ip -s link show can0

# ethtool ring parameters and statistics
ethtool -g can0
ethtool -S can0

# CAN controller status and error counters
ip -details link show can0

2. Bypass Protocol Stack (Fastpath) Usage​

The bypass protocol stack exchanges CAN frames directly with user space in interrupt context through a shared-memory ring buffer (mmap), bypassing the SocketCAN/NAPI protocol stack. It offers lower latency and jitter, and is suitable for scenarios with real-time requirements. Compilation is controlled by the kernel configuration CONFIG_CAN_FLEXCAN_FASTPATH (default y), and it is enabled at runtime via module parameters.

2.1 Enable Fastpath (Module Loading)​

Fastpath is disabled by default and is enabled via flexcan module parameters:

ParameterTypeDefaultDescription
shm_rxbool0(off)enable the fast receive channel
shm_rx_entriesuint1024number of RX ring buffer entries
shm_txbool0(off)enable the fast transmit channel
shm_tx_entriesuint1024number of TX ring buffer entries
shm_tx_budgetuint256maximum number of entries sent per doorbell/interrupt
# enable bidirectional fast channels at load time
modprobe flexcan shm_rx=1 shm_rx_entries=2048 shm_tx=1 shm_tx_entries=2048

# or modify via sysfs at runtime (must be set before bringing the interface up)
echo 1 | sudo tee /sys/module/flexcan/parameters/shm_tx
echo 1 | sudo tee /sys/module/flexcan/parameters/shm_rx
When the Parameters Take Effect

The parameters are read when ip link set canX up is executed, at which point the shared memory and misc devices are initialized. After modification, the interface must be brought up again:

ip link set can0 down
echo 1 > /sys/module/flexcan/parameters/shm_rx
ip link set can0 up

After loading and bringing up the interface, misc device nodes are created (one pair per CAN channel):

ls -l /dev/flexcan_shm_rx_can0 /dev/flexcan_shm_tx_can0
  • Header files: hobot_can_hal.h + hobot_can_hal_fast.h
  • Link library: -lhbcanhal
  • Applicable languages: C / C++

2.3 Data Types​

The fast path reuses the ordinary interface's struct canframe (defined in hobot_can_hal.h):

struct canframe {
uint64_t time_stamp; /* hardware RX timestamp (µs), ignored for TX */
uint32_t canid; /* pure numeric CAN ID, without any flag bits */
uint8_t count; /* unused by the fast path, fixed to 0 */
uint8_t can_type; /* frame type, one of the CANFAST_TYPE_* values */
uint8_t can_channel; /* unused by the fast path, fixed to 0 */
uint8_t len; /* payload length in bytes (CAN: 0~8, CANFD: 0~64) */
uint8_t data[64]; /* payload data */
};

can_type encodes both the frame format (SFF/EFF) and the bus type (classic CAN / CANFD+BRS), taking one of the following constants:

ConstantValueMeaning
CANFAST_TYPE_CAN0classic CAN, standard frame (SFF, 11-bit ID, len ≤ 8)
CANFAST_TYPE_CAN_EFF1classic CAN, extended frame (EFF, 29-bit ID, len ≤ 8)
CANFAST_TYPE_CANFD2CAN FD + BRS, standard frame (SFF, len ≤ 64)
CANFAST_TYPE_CANFD_EFF3CAN FD + BRS, extended frame (EFF, len ≤ 64)

canid is always a pure number without any flag bits. On TX, the frame format is determined by can_type; on RX, canid is likewise the pure number with flags removed, so no additional masking is required.

2.4 Interface Overview​

FunctionPurpose
canFastTxOpen(ifname)open the TX channel, clear stale frames, return a handle
canFastTxClose(tx)close the TX channel and release resources
canFastSend(tx, frames[], n)send in bulk (up to n frames)
canFastPollTx(tx, ms)wait until the TX ring has space
canFastRxOpen(ifname)open the RX channel, discard stale frames, return a handle
canFastRxClose(rx)close the RX channel and release resources
canFastRecv(rx, frames[], n)receive in bulk (up to n frames)
canFastPollRx(rx, ms)wait until RX has data
canFastRxDropped(rx)query the kernel packet-drop count
canFastIsEff(can_type)check whether it is an extended frame (returns 0 or 1)
canFastIsFd(can_type)check whether it is a CANFD frame (returns 0 or 1)
Automatic Clearing of Stale Frames on Open

canFastTxOpen aligns the TX ring head to the current tail, discarding unsent frames left by the previous process; canFastRxOpen aligns the RX ring tail to the current head, discarding unconsumed frames left by the previous process. No manual reset is needed, so "old-frame resend" will not occur when running tests multiple times.

2.5 Code Examples​

2.5.1 Send: Classic CAN Standard Frame (Single Frame)​
#include <hobot_can_hal_fast.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
canfast_tx_t *tx = canFastTxOpen("can1");
if (!tx) return -1;

struct canframe frame;
memset(&frame, 0, sizeof(frame));
frame.canid = 0x123;
frame.can_type = CANFAST_TYPE_CAN; /* classic CAN, standard frame */
frame.len = 8;
frame.data[0] = 0xAA;
frame.data[1] = 0xBB;

int rc;
do {
rc = canFastSend(tx, &frame, 1);
if (rc == -EAGAIN) canFastPollTx(tx, 100);
} while (rc == -EAGAIN);
printf("send rc=%d\n", rc); /* rc==1 means 1 frame was sent successfully */

canFastTxClose(tx);
return 0;
}
2.5.2 Send: CANFD + BRS Extended Frame (Bulk)​
#include <hobot_can_hal_fast.h>
#include <string.h>

#define BATCH 8

int main(void)
{
canfast_tx_t *tx = canFastTxOpen("can1");
if (!tx) return -1;

struct canframe frames[BATCH];
for (int i = 0; i < BATCH; i++) {
memset(&frames[i], 0, sizeof(frames[i]));
frames[i].canid = 0x1FFFFFFF;
frames[i].can_type = CANFAST_TYPE_CANFD_EFF; /* CANFD + BRS, extended frame */
frames[i].len = 64;
memset(frames[i].data, (uint8_t)i, 64);
}

int remaining = BATCH;
int offset = 0;
while (remaining > 0) {
int rc = canFastSend(tx, &frames[offset], (uint32_t)remaining);
if (rc == -EAGAIN) { canFastPollTx(tx, 10); continue; }
if (rc < 0) { fprintf(stderr, "send error %d\n", rc); break; }
offset += rc;
remaining -= rc;
}

canFastTxClose(tx);
return 0;
}
2.5.3 Receive: Bulk Receive and Parse​
#include <hobot_can_hal_fast.h>
#include <stdio.h>

#define BATCH 32

int main(void)
{
canfast_rx_t *rx = canFastRxOpen("can1");
if (!rx) return -1;

struct canframe frames[BATCH];

while (1) {
if (canFastPollRx(rx, 1000) <= 0)
continue;

int count = canFastRecv(rx, frames, BATCH);
for (int i = 0; i < count; i++) {
struct canframe *f = &frames[i];
printf("id=0x%X (%s) len=%u fd=%d\n",
f->canid,
canFastIsEff(f->can_type) ? "EFF" : "SFF",
f->len,
canFastIsFd(f->can_type));
}

uint32_t dropped = canFastRxDropped(rx);
if (dropped)
printf("WARNING: kernel dropped %u frames\n", dropped);
}

canFastRxClose(rx);
return 0;
}
2.5.4 Bidirectional Communication (Separate TX and RX Threads)​
#include <hobot_can_hal_fast.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

#define BATCH 32

static volatile int g_stop = 0;
static void on_signal(int s) { (void)s; g_stop = 1; }

static void *tx_task(void *arg)
{
(void)arg;
canfast_tx_t *tx = canFastTxOpen("can1");
if (!tx) return NULL;

struct canframe frames[8];
for (int i = 0; i < 8; i++) {
memset(&frames[i], 0, sizeof(frames[i]));
frames[i].canid = 0x100;
frames[i].can_type = CANFAST_TYPE_CANFD; /* CANFD + BRS, standard frame */
frames[i].len = 64;
}

while (!g_stop) {
int remaining = 8, offset = 0;
while (!g_stop && remaining > 0) {
int rc = canFastSend(tx, &frames[offset], (uint32_t)remaining);
if (rc == -EAGAIN) { canFastPollTx(tx, 10); continue; }
if (rc < 0) break;
offset += rc; remaining -= rc;
}
usleep(1000);
}

canFastTxClose(tx);
return NULL;
}

static void *rx_task(void *arg)
{
(void)arg;
canfast_rx_t *rx = canFastRxOpen("can1");
if (!rx) return NULL;

struct canframe frames[BATCH];
long total = 0;

while (!g_stop) {
if (canFastPollRx(rx, 1000) <= 0) continue;

int n = canFastRecv(rx, frames, BATCH);
total += n;
}

printf("RX total=%ld dropped=%u\n", total, canFastRxDropped(rx));
canFastRxClose(rx);
return NULL;
}

int main(void)
{
signal(SIGINT, on_signal);
signal(SIGTERM, on_signal);

pthread_t t_tx, t_rx;
pthread_create(&t_rx, NULL, rx_task, NULL);
pthread_create(&t_tx, NULL, tx_task, NULL);
pthread_join(t_tx, NULL);
pthread_join(t_rx, NULL);
return 0;
}

2.6 Error Handling and Notes​

Error handling:

int rc = canFastSend(tx, frames, count);
if (rc < 0) {
if (rc == -EAGAIN) {
/* TX ring is full; wait and retry */
canFastPollTx(tx, 10);
} else if (rc == -EINVAL) {
/* invalid parameter; check whether canFastTxOpen succeeded and whether frames/count are valid */
} else {
fprintf(stderr, "canFastSend error: %s\n", strerror(-rc));
}
} else if (rc < (int)count) {
/* the ring was full and only some frames were sent; call again with the remaining pointer and count */
}

Notes:

  • Thread safety: the same handle must not be called concurrently by multiple threads. Each thread should call canFastTxOpen / canFastRxOpen independently and hold its own handle.
  • TX ring full: when canFastSend returns -EAGAIN, the frame has not been sent and must be retried (unless the business allows dropping frames). It is recommended to call canFastPollTx(tx, ms) to wait for space and then retry.
  • RX packet loss: canFastRxDropped(rx) > 0 means the consumption speed is slower than the kernel write speed. You can increase BATCH, reduce processing time, or increase the kernel shm_rx_entries.
  • Resource release: before the program exits, canFastTxClose / canFastRxClose must be called to avoid mmap and fd leaks. It is recommended to set an exit flag in the SIGINT / SIGTERM signal handler and perform close after the main loop exits.

2.7 Bypass Channel vs. Standard Path Comparison​

DimensionStandard SocketCANBypass Fastpath
User interfacePF_CAN raw socket/dev/flexcan_shm_rx/tx_* + hobot_can_hal_fast library
TX/RX pathskb + NAPI + net_rx_actionmmap ring buffer + IRQ direct write
Latencyhigher (protocol stack overhead)low (bypasses the protocol stack, µs level)
Default stateavailable once the interface is upmodule parameter off, activated only when the misc device is opened
CAN-FDsupportedsupported
Message filteringSocketCAN filternone (filtered in user space)

3. Acore Takes Over MCU-domain CAN (Expanding CAN Channels)​

3.1 Background​

The S600 Acore/Main domain has only 4 CAN channels. When the business needs more CAN channels, Acore can directly control the MCU-domain CAN controllers, taking over up to 6 additional channels (canfd4~canfd9), expanding the CAN channels available to Acore to 10.

On the Acore side, the standard FlexCAN driver directly accesses the MCU-domain CAN register space (memory mapping), and the data TX/RX path is direct register access, without IPC forwarding. Interrupts are routed from the MCU domain to Acore via inter-core interrupts (IPI).

Since MCU-domain CAN controllers are initialized and managed by the MCU side by default, the configuration must be modified on the MCU side before takeover, releasing the CAN controller's interrupt, clock, and startup control from the MCU side. Otherwise, issues such as both cores simultaneously grabbing interrupts and the clock not being switched will occur. The inter-core passthrough solution for MCU-domain CAN (CAN2IPC / CANHAL) has dedicated documentation, which is not repeated here.

3.2 CAN Node Mapping​

The correspondence between the MCU-domain CAN nodes in the Acore device tree and the CAN controllers on the MCU side:

Acore Device NodeMCU-side CANRegister Base AddressDefault Status
canfd4CAN10x23870000disabled
canfd5CAN20x23880000disabled
canfd6CAN50x238b0000disabled
canfd7CAN60x238c0000disabled
canfd8CAN70x238d0000disabled
canfd9CAN80x238e0000disabled

The Acore-side device tree nodes and interrupt routing configuration have been merged into the mainline code. To use a particular CAN channel, simply change the status of the corresponding node from disabled to okay in the board-level DTS. The default disabled setting prevents anomalies for customers who use MCU-side CAN.

3.3 MCU-side Changes (Three Modification Points)​

For each CAN controller to be handed over to Acore, the MCU side requires 3 modifications, following this rule: hand over to Acore = make all three changes; keep on MCU = make none of the three changes.

No.Modification PointFileFunctionOperation
①Interrupt registrationIsr_Hal.cInterrupt_McuConfigs[] arraycomment out the interrupt configuration line for the corresponding CAN
②Clock registerCan_test.cCan_Initclock()change the clock register write value from 0x8000 to 0x8001
③Controller startupCan_test.cCanPro_Init()comment out the corresponding Can_SetControllerMode(..., CAN_CS_STARTED)

CAN controller parameter reference table:

CAN No.CANFDClock Register AddressController EnumInterrupt Channel / ISR
1CANFD10x235200CCCanController_1Os_IntChannel_Can1_DataIsr / Os_Isr_Can1_DataIsr
2CANFD20x235200D0CanController_2Os_IntChannel_Can2_DataIsr / Os_Isr_Can2_DataIsr
5CANFD50x235200DCCanController_5Os_IntChannel_Can5_DataIsr / Os_Isr_Can5_DataIsr
6CANFD60x235200E0CanController_6Os_IntChannel_Can6_DataIsr / Os_Isr_Can6_DataIsr
7CANFD70x235200E4CanController_7Os_IntChannel_Can7_DataIsr / Os_Isr_Can7_DataIsr
8CANFD80x235200E8CanController_8Os_IntChannel_Can8_DataIsr / Os_Isr_Can8_DataIsr
Modification Point ① — Interrupt Registration (Isr_Hal.c)​

File path: Target/Target_S600/Target-hobot-lite-freertos-mcu1/target/FreeRtosOsHal/Isr_Hal.c

In the Interrupt_McuConfigs[] array, each CAN controller corresponds to one interrupt configuration entry. For controllers handed over to Acore, comment out this line so that the MCU no longer registers and responds to the CAN data interrupt, avoiding dual-core contention:

// For the CAN handed over to Acore, comment out this line:
//{Os_IntChannel_Can1_DataIsr, Os_Isr_Can1_DataIsr, OS_IMASK_FOR_Can1_DataIsr/8, ENABLE},
Modification Point ② — Clock Register (Can_test.c / Can_Initclock())​

File path: samples/Can/Can_Pro_Sample/src/Can_test.c

Can_Initclock() writes the clock register value for each CANFD, controlling clock ownership:

  • 0x8000: clock used by the MCU domain (default, corresponding to 40MHz)
  • 0x8001: clock handed over to the Acore domain (bit0 set to 1 means ownership switched to Acore, corresponding to 80MHz)

For controllers handed over to Acore, change the corresponding writel value from 0x8000 to 0x8001:

writel((__IO uint32 *)(0x235200CC), 0x8001); // CANFD1
Modification Point ③ — Controller Startup (Can_test.c / CanPro_Init())​

File path: samples/Can/Can_Pro_Sample/src/Can_test.c

CanPro_Init() calls Can_SetControllerMode(CanController_X, CAN_CS_STARTED) for each controller to start it. For controllers handed over to Acore, comment out this call so that the MCU does not start the controller, and Acore initializes and starts it by itself:

//Can_SetControllerMode(CanController_1, CAN_CS_STARTED);
Quick Reference Table for Each CAN Controller Modification​
CAN to Hand Over to AcoreIsr_Hal.c Line to Comment OutCan_Initclock Register to ChangeCanPro_Init Line to Comment Out
CAN1Can1_DataIsrCANFD1 0x235200CC → 0x8001CanController_1
CAN2Can2_DataIsrCANFD2 0x235200D0 → 0x8001CanController_2
CAN5Can5_DataIsrCANFD5 0x235200DC → 0x8001CanController_5
CAN6Can6_DataIsrCANFD6 0x235200E0 → 0x8001CanController_6
CAN7Can7_DataIsrCANFD7 0x235200E4 → 0x8001CanController_7
CAN8Can8_DataIsrCANFD8 0x235200E8 → 0x8001CanController_8

3.4 Compilation and Upgrade Process​

After making the changes, upgrade MCU1 and then the kernel in sequence:

① Upgrade MCU1 Apply the MCU patch, compile MCU1, and upgrade MCU1. ② Upgrade the kernel After merging the Acore-side device tree changes, recompile the kernel and upgrade it.

3.5 System Startup Sequence​

Startup Sequence

The system must start MCU1 first and then load the CAN modules, otherwise anomalies will occur.

# 1. Start MCU1 first
cd /sys/class/remoteproc/remoteproc_mcu0
echo S600_MCU_DEBUG.elf > firmware
echo start > state

# 2. Load the CAN modules
modprobe can
modprobe can-raw
modprobe flexcan

# 3. Configure the CAN interfaces (Acore domain can0~can3 + MCU domain can4~can5, CAN-FD 1M/5M)
ip link set can0 down
ip link set can0 type can fd on loopback off bitrate 1000000 sample-point 0.75 dbitrate 5000000 dsample-point 0.75
ip link set can0 up

ip link set can4 down
ip link set can4 type can fd on loopback off bitrate 1000000 sample-point 0.75 dbitrate 5000000 dsample-point 0.75
ip link set can4 up

# ... configure can1~can3 and can5 the same way

# 4. Test verification
candump -ta can4
cansend can5 123#1122334455667788
Mutually Exclusive with the MCU Passthrough Solution

Once an MCU-domain CAN controller is taken over by Acore, the MCU-side CAN2IPC passthrough path becomes invalid for that controller.

Debugging​

Logs and Device Confirmation​

# driver probe log
dmesg | grep -i flexcan

# network device
ip link show can0

# bypass misc device (only after enabled and brought up)
ls -l /dev/flexcan_shm_rx_can0 /dev/flexcan_shm_tx_can0

Common Issue Troubleshooting​

  • CAN cannot receive data: ① confirm gpio369 is pulled low (cat /sys/class/gpio/gpio369/value should be 0); ② confirm canfd0~canfd3 are okay in the device tree and the conflicting SPI0/UART are disabled; ③ confirm the three modules are loaded (lsmod | grep -E "can|flexcan"); ④ confirm the interface is up and the baud rate matches the peer.
  • misc device not appearing: confirm CONFIG_CAN_FLEXCAN_FASTPATH=y, shm_rx=1/shm_tx=1 are set, and the interface is up.
  • Bypass RX packet loss: check canFastRxDropped(rx) and increase shm_rx_entries.
  • MCU-domain CAN startup reports interrupt conflict: confirm the board-level DTS has enabled the corresponding CAN node and the three MCU-side modifications are all done (interrupt commented out, clock changed to 0x8001, startup commented out).

FAQ​

Q1: Why can't CAN0/CAN1 be used?​

A: CAN0/CAN1 share the same group of 4 pins with SPI0. Disable SPI0 in the device tree (status = "disabled") and enable the CAN nodes.

Q2: Why can't I receive data?​

A: The most common cause is that the CAN_STB pin is not pulled low. The transceiver is in standby mode and cannot transmit or receive. Run echo out > /sys/class/gpio/gpio369/direction; echo 0 > /sys/class/gpio/gpio369/value.

Q3: Which three modules need to be loaded?​

A: can (CAN framework), can-raw (raw socket), flexcan (hardware driver).

Q4: Does CAN-FD require RX Mailbox mode?​

A: Yes. The FlexCAN hardware limitation means CAN-FD does not support RX-FIFO. The S600 devtype is fixed to use RX Mailbox + RTR reception, with no configuration needed. CAN-FD and three-sample mode (CAN_CTRLMODE_3_SAMPLES) cannot be used simultaneously.

Q5: Does the bypass protocol stack affect standard SocketCAN?​

A: No. When the module parameter is off by default, the standard path is unaffected. Only after user space opens the shm_rx device does the controller's receive interrupt switch to the fast path. shm_tx shares the TX Mailbox with the standard netdev and is serialized via tx_lock, so there is no conflict.

Q6: What is the difference between the fast path interface and the ordinary interface?​

A: The fast path exchanges frames directly with the FlexCAN driver through a kernel shared-memory ring buffer (mmap), bypassing the socket and protocol stack, with lower latency (µs level); the ordinary interface (canSendMsgFrame, etc.) goes through IPCF/socket and the kernel protocol stack, with higher latency. The fast path only requires the interface name and no configuration file, and the maximum frame length is 64 bytes (CANFD).