Configure CAN Channels

CAN interfaces exist on an ECU as PCIe cards and/or USB devices. The Linux system denotes each CAN bus on an interface as a channel, and sets a unqiue channel identifier. Some multi-channel CAN interfaces also contain channel indexes to identify a specific CAN bus.

PolySync Core supports linuxcan and socketcan-enabled CAN interface hardware devices. Core enables the linuxcan drivers to communicate with CAN hardware by default.

1. Define the CAN driver used by Core nodes

PolySync uses a symbolic link named libpolysync_can.so which points to one of the two supported drivers─linuxcan or socketcan.

To determine which driver is currently being used by PolySync Core, check where the symbolic link points.

$ ls -l /usr/lib/libpolysync_can.so
lrwxrwxrwx 1 root root 27 Apr 20 02:00 /usr/lib/libpolysync_can.so -> libpolysync_can_XXXXX.so

Only one driver can be loaded by the kernel - i.e. for a given host, all nodes must use either linuxcan or all nodes use socketcan drivers to communicate with CAN interface hardware.

1.1 linuxcan

To ensure PolySync is configured to use linuxcan drivers, issue the command:

$ cd /usr/lib/
$ sudo ln -sf libpolysync_can_linuxcan.so libpolysync_can.so

1.2 socketcan

To ensure PolySync is configured to use socketcan drivers, issue the command:

$ cd /usr/lib
$ sudo ln -sf libpolysync_can_socketcan.so libpolysync_can.so

2. Locate linuxcan CAN channels

linuxcan drivers are compatible with Kvaser CAN interfaces.

2.1 Locating identifiers

linuxcan uses both the CAN channel hardware ID and the CAN channel circuit ID of the Kvaser CAN interfaces (USB, PCIe, etc.) to detect the hardware that is connected to the ECU.

The hardware identifier is unique to each physical hardware device, where each device has one or more channels. The circuit identifier is an index for multi-channel CAN hardware interfaces.

Consider the following example where there are three Kvaser Leaf Lights connected:

  • Two 2x CAN to 1x USB Leaflight
  • One 1x CAN to to 1x USB Leaflight
$ /usr/doc/canlib/examples/listChannels
Found 5 channels.
channel 0 = Kvaser Leaf Light v2, 73-30130-00685-0, (0)28373, 3.3.0.769
channel 1 = Kvaser Leaf Light v2, 73-30130-00685-0, (1)28373, 3.3.0.769
channel 2 = Kvaser Leaf Light v2, 73-30130-00685-0, (0)11375, 3.3.0.769
channel 3 = Kvaser Leaf Light v2, 73-30130-00685-0, (0)10049, 3.3.0.769
channel 4 = Kvaser Leaf Light v2, 73-30130-00685-0, (1)10049, 3.3.0.769
Example input CAN Channel Hardware Identifier CAN Channel Circuit Identifier System index
channel 0 = Kvaser Leaf Light v2, 73-30130-00685-0, (0)28373, 3.3.0.769 28373 0 0
channel 1 = Kvaser Leaf Light v2, 73-30130-00685-0, (1)28373, 3.3.0.769 28373 1 1
channel 2 = Kvaser Leaf Light v2, 73-30130-00685-0, (0)11375, 3.3.0.769 11375 0 2
channel 3 = Kvaser Leaf Light v2, 73-30130-00685-0, (0)10049, 3.3.0.769 10049 0 3
channel 4 = Kvaser Leaf Light v2, 73-30130-00685-0, (1)10049, 3.3.0.769 10049 1 4

For single-channel Kvaser CAN interfaces like the Leaflight, the circuit identifier will always be 0.

Enter the CAN channel hardware identifier and CAN channel circuit identifier in the IO Configuration section of the nodes SDF Configurator entry.

IO Configuration

2.2 Troubleshooting

2.2.1 Secure boot

The kernel modules are built, but the kernel will not load the module.

Secure boot must be disabled on Ubuntu 16.04 (or any kernel after 4.4.0-20) for the Kvaser Linuxcan kernel modules to be properly loaded. Secure boot is configured through the machines BIOS.

2.2.2 Cannot detect linuxcan channels

A USB Leaf Light is connected, but the listChannels example application does not detect any hardware.

This happens when the Linux system cannot properly load the module, or the linuxcan module wasn’t built against the most recent kernel headers.

Follow these steps repeatedly until the Kvaser device(s) can be detected by listChannels.

  $ cd /usr/local/polysync/deps
  $ sudo tar xf linuxcan-5.19.tar.gz
  $ cd linuxcan
  $ sudo make clean 
  $ sudo make
  $ sudo make install 
  • Unplug and replug the USB Leaf Light hardware
  • Manually modprobe the kernel module used to communicate with the hardware
  $ sudo modprobe leaf   # see the list of modules and associated hardware in the linuxcan/README 
  • Run the listChannels example application
  $ /usr/local/polysync/deps/linuxcan/canlib/examples/listChannels

If channels are not listed, return to step 2. Eventually the Linux system will detect the USB device and load the driver.

2.2.3 Unable to sniff CAN data

While trying to validate the data coming in over CAN, error frames are read using the Kvaser linuxcan canmonitor example.

This is likely caused by having the wrong baudrate set in the canmonitor example, versus what the CAN sensor is publishing. Nearly all automotive based sensors publish data to the CAN bus at 500K baud.

Update the canmonitor source code to open the CAN channel with 500K baud to read and validate the incoming data.

$ cd /usr/local/polysync/deps/linuxcan/canlib/examples/
$ vim canmonitor.c
  Line 93: BAUD_1M --> BAUD_500K

Rebuild the example application with the new baud-rate to read CAN data at the proper frequency.

3. Configure socketcan CAN channels

The Linux system does not know how to talk to CAN interfaces natively. Using system tools like ip and ifconfig you can configure and locate socketcan CAN channels.

3.1 Enumerate CAN channels

socketcan enumerates CAN interfaces as if it were an Ethernet socket. CAN interfaces are represented as can0, can1, pcan0, etc.

With the CAN interface connected to the ECU, use ip to link the physical hardware to a CAN device profile, and bring the interface up.

$ sudo ip link set can0 type can bitrate 500000
$ sudo ip link set can0 up

The CAN interface is now detectable by software applications like the PolySync Core dynamic driver, or the tool ifconfig.

3.2 Locating identifiers

Only the hardware identifier is needed when using socketcan drivers to talk with CAN interfaces.

Available devices can be queried from the system with ifconfig. The devices map to the following values for the PolySync parameter CAN channel hardware identifier.

socketcan Channel CAN channel hardware identifier
can0 1
can1 2
can2 3
canN N+1

Enter the CAN channel hardware identifier in the IO Configuration section of the nodes SDF Configurator entry.

IO Configuration