Software:Can4linux

From HandWiki

can4linux is an Open Source CAN Linux-Kernel device driver. Development started in the mid-1990s for the Philips 82C200 CAN controller stand alone chip on an ISA Board AT-CAN-MINI. In 1995 the first version was created to use the CAN bus with Linux for laboratory automation as a project of the Linux Lab Project at FU Berlin.

The increasing spread of the CAN field bus technology for automation especially in embedded devices and the growth of Linux in this area leads to a growing importance of device drivers as base for higher layer CAN protocols like CANopen, J1939 and DeviceNet.

Besides the NXP SJA1000 as successor of the first supported CAN controller Philips 82C200 and the Intel 82527, the development for powerful microcontrollers with integrated CAN controllers that are capable to run the Linux Operating system have been intensified since 2005. As examples for this development serve Freescale's ColdFire processors [1] or ARM models by ATMEL[2] and Freescale[3] but also the stand-alone CAN controller MCP2515,[4] connected via SPI Bus. A list of supported CAN controllers is available on the can4linux project site.[5]

In the latest version there is a virtual CAN mode implemented which allows applications to communicate without CAN hardware but only by using the device driver software layer. This mode already includes support for the new CAN frame format called CAN FD [6][7] which allows data frame length up to 64 byte.

Usage

Application software opens a CAN device descriptor and gets back a file descriptor. Using this file descriptor standard operating system functions like read() and write() are used to exchange CAN frames with other CAN nodes on a CAN bus.

The following code is an example which puts a CAN frame on the bus and afterwards waits for a CAN frame transmitted by another CAN node. More examples are available on the can4linux project site.[8]

/* simple CAN application example 
 * used for the Wikipedia article on can4linux
 */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#include <can4linux.h>

int main(int argc,char **argv)
{
    int fd;
    canmsg_t frame;
    
    /* open(),
       bit rate comes from parameters at proc/sys/dev/Can */
    fd = open("/dev/can0", O_RDWR);

    frame.id      = 100;
    frame.length  = 2;
    frame.data[0] = 5;
    frame.data[1] = 0;
    write(fd, &frame, 1); /* ! count is number of frames, not byte */

    read(fd, &frame, 1);  /* ! count is number of frames, not byte */
    printf("received CAN Frame Id %ld, DLC=%d\n", frame.id, frame.length);
    return 0;
}

can4linux can be compiled in a way that allows more than one process to have access to the same CAN controller hardware for reading and writing frames on the CAN bus. Beside the real application other processes like data loggers or other diagnosis processes can access the bus.

References

External links