Skip to content
Canlab edited this page Jun 28, 2020 · 19 revisions

Introduction

Implementation of SocketCAN driver under Linux for the newest adapter USB2CAN Triple. With this driver you can bring up three available CAN ports as individual network interfaces and treat them as network interfaces as well. This makes it easy to use, on user level with currently available software with SocketCAN support(see 3rd-side software),as well as on developemnt level using socket API... (see Development). Whole project is still under devolopment, so please be patient.

Our new adapter also supports CAN FD protocol on port 3, but there is not much existing tools to work with CAN FD messages. This feature can be tested with can-utils like cansend, candump or cangen.

This driver doesn't support all features of our adapter, it's still work in progress. This means that some counters and feature like "Trigger/Marker" cannot be used, yet. HW counters report from adapter is ignored in kernel, which has his own counters.

Getting started

Using Windows is quite easy, just download PP2CAN software from our website and run it... With Linux what you have to do is

  1. Get copy of this repo git clone https://github.com/canlab-cz/triple
  2. Compile using make

This driver uses CDC-ACM driver for communication with our device, so please make sure that correct driver has been loaded...

Usage

If you compiled provided source code and connected adapter to PC, you can start receive and transmit messages to CAN bus using either console applications like cansend, cansniff, candump, etc., or GUI-based apps like CANDevStudio, Wireshark (RX only), Cangaroo...

Basic way to setup an interface using triple adapter is to set parameters of channels like bittiming, listen-only , ISO/nonISO mode, etc... So to setup adapter you need to run userspace deamon that will setup network interfaces and will bind them to our device. First load compiled module to system

sudo insmod usb2cansocketcan.ko
sudo cp usb2cansocketcan.ko /lib/modules/$(uname -r)/kernel/drivers/net/can
sudo depmod -a
sudo modprobe usb2cansocketcan``

Now you can use tripled utility to set up parameters of CAN bus, use -h param to see possible settings of communication channel.

sudo ./tripled_64 ttyACM0 -s<CAN 2.0x>:<CAN 2.0x speed>:<CANFD speed> -ncan0:can1:canfd2

This take a few seconds and status LED diodes will blink when commands for setting speed on each port is executed, after this step adapter is ready to use. Last step is to bring up network interfaces from linux side.

sudo sudo ip link set can0 up qlen 1000
sudo sudo ip link set can1 up qlen 1000
sudo sudo ip link set canfd2 up qlen 1000 ?? /parametr qlen --> test

Script description

For easier setup at boot or at runtime you can use prepared scripts.

start.sh

Basic script allows you to setup can interface on port 0 using triple HW at predefined speed of 250k. It does so by copying and loading module into kernel and starting userspace deamon with specified speed and then setting interface up using ip tool.

end.sh

To kill deamon and unload module from kernel run sh ./start.shor type following

boot.sh

Boot scripts are provided for SystemVInit and for SystemD managers. This feature is not tested thoroughly as well as most of this driver :) .

rm_boot.sh

These scripts allows you remove auto boot configs.

Development

For windows you can buy X2CAN API for development, in case of interest write to [email protected].

In Linux SocketCAN allows you to use traditional sockets to communicate over CAN bus. So it's same task as for example writing client/server for TCP/UDP communication.

Examples: You can examine existing can-utils tools, but to simply recieve/send messages what you have to do is :

//open socket
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) 
{
    perror("socket");
    return 1;
}
//bind socket
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
	perror("bind");
	return 1;
}

//call write to send message, similar for read instruction. 
if (write(s, &frame, required_mtu) != required_mtu) {
	perror("write");
	return 1;
}

3rd-side software

For testing, I've used tui and gui mostly open-source apps, few examples in screenshots from development. More details will be provided in form of better startup/shutdown scripts, also automatic startup scripts and examples for mentioned software...

CAN FD example

CANDevStudio

Technical specification

USB2CAN TRIPLE adapter is a CAN-USB converter, which contains 3x CAN interface. One of these ports (port 3) is compatible with a new version of CAN named CAN FD. The other two ports (ports 1 and 2) are of the high speed type (ISO 11898-2:2016). Supported speed of the frame’s data portion is up to 8Mb. Both ISO and nonISO modes are supported.

This converter is equipped with a DIP switch to ease the activation/deactivation of the 120 ohms terminating resistance.

Interface use plug-able terminal block with a span of 3.5mm. Included with the order is also a set of screwable terminal blocks. Terminal blcoks with push-in mechanism can be also used. In the picture below both types of terminal blocks are plugged in.

Terminal block example

Apart from the three CAN buses, the converter also contains a digital input separated by an optocoupler with a trigger/marker function. Activation / deactivation of this input is saved to data log in PP2CAN SW, alternatively it can be processed using X2CAN API. The firmware allows to activate timestamp function (timestamp of received messages) with resolution of 1 microsecond.

Pinout