Code Confidencebuild

Chapter 35. Writing a CAN hardware device driver

A CAN driver is nothing more than a named entity that supports the basic I/O functions - read, write, get config, and set config. The device driver uses and manages interrupts from the device. While the interface is generic and device driver independent, the actual driver implementation is completely up to the device driver designer.

That said, the reason for using a device driver is to provide access to a CAN device from application code in as general purpose a fashion as reasonable. Most driver writers are also concerned with making this access as simple as possible while being as efficient as possible.

Like other device drivers the CAN device driver is concerned with the movement of information - the CAN messages. In order to make the most efficient use of system resources, interrupts are used. This will allow other application processing to take place while the data transfers are under way, with interrupts used to indicate when various events have occurred. For example, a CAN device typically generates an interrupt after a CAN message has been sent or a CAN message has been received by a CAN hardware message buffer. It makes sense to allow further application processing while the data is being sent since this can take quite a long time. The interrupt can be used to allow the driver to send a CAN message as soon as the current one is complete, without any active participation by the application code.

The main building blocks for CAN device drivers are found in the include files <cyg/io/devtab.h> and <cyg/io/can.h>

Like many other device drivers in eCos, CAN device drivers are described by a device table entry, using the cyg_devtab_entry_t type. The entry should be created using the DEVTAB_ENTRY() macro.

How to Write a CAN Hardware Interface Driver

The standard CAN driver supplied with eCos is structured as a hardware independent portion and a hardware dependent interface module. To add support for a new CAN device, the user should be able to use the existing hardware independent portion and just add their own interface driver which handles the details of the actual device. The user should have no need to change the hardware independent portion.

The interfaces used by the CAN driver and CAN implementation modules are contained in the file <cyg/io/can.h>.

DevTab Entry

The interface module contains the devtab entry (or entries if a single module supports more than one interface). This entry should have the form:




The "C" label for this devtab entry


The "C" string for the device. E.g. /dev/can0.


The table of I/O functions. This set is defined in the hardware independent CAN driver and should be used.


The hardware module initialization function.


The device lookup function. This function typically sets up the CAN device for actual use, turning on interrupts, configuring the message buffers, etc.


This table (defined below) contains the interface between the interface module and the CAN driver proper.

Example devtab entry for Motorola FlexCAN device driver:

             0,                     // Does not depend on a lower level interface
             flexcan_lookup,        // CAN driver may need initializing

CAN Channel Structure

Each CAN device must have a “CAN channel”. This is a set of data which describes all operations on the device. It also contains buffers, etc. The CAN channel is created by the macro:

CAN_CHANNEL_USING_INTERRUPTS(l, funs, dev_priv, baud,
                             out_buf, out_buflen,
                             in_buf,  in_buflen)



The "C" label for this structure.


The set of interface functions (see below).


A placeholder for any device specific data for this channel.


The initial baud rate value (cyg_can_baud_rate_t).


Pointer to the output buffer


The length of the output buffer.


pointer to the input buffer.


The length of the input buffer.

Example CAN channel implementation for Motorola FlexCAN device driver:

    flexcan_can0_txbuf, CYGNUM_DEVS_CAN_MCF52xx_FLEXCAN0_QUEUESIZE_TX,
    flexcan_can0_rxbuf, CYGNUM_DEVS_CAN_MCF52xx_FLEXCAN0_QUEUESIZE_RX

The interface from the hardware independent driver into the hardware interface module is contained in the funs table. This is defined by the macro:

CAN Lowlevel Functions Structure

CAN_LOWLEVEL_FUNS(l, putmsg, getevent, get_config, set_config, start_xmit, stop_xmit)



The "C" label for this structure.


bool (*putmsg)(can_channel *priv, cyg_can_message *pmsg, void *pdata)

This function sends one CAN message to the interface. It should return true if the message is actually consumed. It should return false if there is no space in the interface


bool (*getevent)(can_channel *priv, cyg_can_event *pevent, void *pdata)

This function fetches one event from the interface.


Cyg_ErrNo (*get_config)(can_channel *priv, cyg_uint32 key, const void *xbuf, cyg_uint32 *len)

This function is used to query the configuration of a CAN channel.


Cyg_ErrNo (*set_config)(can_channel *priv, cyg_uint32 key, const void *xbuf, cyg_uint32 *len)

This function is used to change configuration of a CAN channel.


void (*start_xmit)(can_channel *priv)

Enable the transmit channel and turn on transmit interrupts.


void (*stop_xmit)(can_channel *priv)

Disable the transmit channel and turn transmit interrupts off.

Example implementation of low level function structure for Motorola FlexCAN device driver:



The device interface module can execute functions in the hardware independent driver via chan->callbacks. These functions are available:

void (*can_init)(can_channel *chan)

This function is used to initialize the CAN channel.

cyg_bool (*xmt_msg)(can_channel *chan, void *pdata)

This function would be called from an interrupt handler after a transmit interrupt indicating that additional messages may be sent. The upper driver will call the putmsg function as appropriate to send more data to the device. xmt_msg() returns true if a message has been provided to the low level driver, false otherwise.

cyg_bool (*rcv_event)(can_channel *chan, void *pdata)

This function is used to tell the driver that a message has arrived at the interface or that an event has occurred. This function is typically called from the interrupt handler. rcv_event() returns true if an event has been provided by the low level driver, false otherwise.