## CAN driver details

A raw CAN driver is is provided as a standard part of the eCos system. Use the include file `<cyg/io/canio.h>` for this driver. The CAN driver is capable of sending single CAN messages and receiving single CAN events to a CAN device. Controls are provided to configure the actual hardware, but there is no manipulation of the data by this driver. There may be many instances of this driver in a given system, one for each CAN channel. Each channel corresponds to a physical device and there will typically be a device module created for this purpose. The device modules themselves are configurable, allowing specification of the actual hardware details.

### User API

The CAN driver uses the standard eCos I/O API functions. All functions except `cyg_io_lookup()` require an I/O "handle".

All functions return a value of the type `Cyg_ErrNo`. If an error condition is detected, this value will be negative and the absolute value indicates the actual error, as specified in `cyg/error/codes.h`. The only other legal return value will be `ENOERR` , `-EINTR` and`-EAGAIN`. All other function arguments are pointers (references). This allows the drivers to pass information efficiently, both into and out of the driver. The most striking example of this is the len value passed to the read and write functions. This parameter contains the desired length of data on input to the function and the actual transferred length on return.

#### Lookup a CAN device

`Cyg_ErrNo cyg_io_lookup(
const char *name,
cyg_io_handle_t *handle )`

This function maps a device name onto an appropriate handle. If the named device is not in the system, then the error -`ENOENT` is returned. If the device is found, then the handle for the device is returned by way of the handle pointer *handle.

#### Send a CAN message

`Cyg_ErrNo cyg_io_write(
cyg_io_handle_t handle,
const void *buf,
cyg_uint32 *len )
`

This function sends one single CAN message (not a buffer of CAN messages) to a device. The size of data to send is contained in *len and the actual size sent will be returned in the same place. A pointer to a `cyg_can_message` is contained in *buf . If a message was sucessfully sent, the function returns `ENOERR`. If nonblocking calls are supported and the TX buffer is full then the function returns immediatelly with `-EAGAIN`. If the driver supports timeouts and nonblocking calls are enabled then this function may also return `-EINTR` after the timout expired and the TX buffer is still full. This is important for applications where i.e. a watchdog need to be toggled by each task. Because it may happen that no CAN message will arrive for a long time the receiving thread remains blocked if the driver does not support timeouts and the watchdog will never be toggled.

`typedef struct can_message
{
cyg_uint32 id;
cyg_uint8 data[8];
cyg_can_id_type ext;
cyg_can_frame_type rtr;
cyg_uint8 dlc;
} cyg_can_message;`

The type `cyg_can_message` provides a device independent type of CAN message. Before calling the write function this message should be setup properly. The id field contains the 11 Bit or 29 bit CAN message identifier depending on the value of the ext field. The data field contains the 8 data bytes of one CAN message. The ext field configures the type of CAN message identifier (`CYGNUM_CAN_ID_STD` = standard 11 Bit id, `CYGNUM_CAN_ID_EXT` = extended 29 Bit id). The rtr field contains the frame type. (`CYGNUM_CAN_FRAME_DATA` = data frame, `CYGNUM_CAN_FRAME_RTR` = remote transmission request). The dlc field (data length code) contains the number of valid data bytes (0 - 8) in the data field.

Example code for sending one single CAN message:

`cyg_can_message tx_msg;
cyg_uint32 len;
Cyg_ErrNo ret;
tx_msg.id = 0x100;
tx_msg.ext = CYGNUM_CAN_ID_EXT;
tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
tx_msg.dlc = 1;
tx_msg.data[0] = 0xF1;
len = sizeof(tx_msg);
ret = cyg_io_write(hDrvCAN, &tx_msg, &len);`

#### Read a CAN event

`Cyg_ErrNo cyg_io_read(
cyg_io_handle_t handle,
void *buf,
cyg_uint32 *len )
`

This function receives one single event from a device. The desired size of data to receive is contained in *len and the actual size obtained will be returned in the same place. A pointer to a`cyg_can_event` is contained in *buf. If a message was sucessfully sent, the function returns `ENOERR`. If nonblocking calls are supported the driver returns `-EAGAIN` if the RX buffer is empty. If the driver supports timeouts and nonblocking calls are enabled then this function may also return `-EINTR` if the timout value expired and the RX buffer is still empty.

`typedef struct cyg_can_event_st
{
cyg_uint32 timestamp;
cyg_can_message msg;
cyg_uint16 flags;
} cyg_can_event;
`

A `cyg_can_event` provides a generic device independent type for handling CAN events that may occur. If the driver supports timestamps then the timestamp field my contain a timestamp value for an event that occured. The msg field contains a CAN message if an RX or TX event occured. The flags field contains 16 bits that indicate which kind of events occured. The following events are supported and after receiving an event the flag field should be checked against these values:

`typedef enum
{
CYGNUM_CAN_EVENT_RX = 0x0001, // message received
CYGNUM_CAN_EVENT_TX = 0x0002, // mesage transmitted
CYGNUM_CAN_EVENT_WARNING_RX = 0x0004, // tx error counter (TEC) reached warning level (>96)
CYGNUM_CAN_EVENT_WARNING_TX = 0x0008, // rx error counter (REC) reached warning level (>96)
CYGNUM_CAN_EVENT_ERR_PASSIVE = 0x0010, // CAN "error passive" occured
CYGNUM_CAN_EVENT_BUS_OFF = 0x0020, // CAN "bus off" error occured
CYGNUM_CAN_EVENT_OVERRUN_RX = 0x0040, // overrun in RX queue or hardware occured
CYGNUM_CAN_EVENT_OVERRUN_TX = 0x0080, // overrun in TX queue occured
CYGNUM_CAN_EVENT_CAN_ERR = 0x0100, // a CAN bit or frame error occured
CYGNUM_CAN_EVENT_LEAVING_STANDBY = 0x0200, // CAN hardware leaves standby / power don mode or is waked up
CYGNUM_CAN_EVENT_ENTERING_STANDBY = 0x0400, // CAN hardware enters standby / power down mode
CYGNUM_CAN_EVENT_ARBITRATION_LOST = 0x0800, // arbitration lost
CYGNUM_CAN_EVENT_DEVICE_CHANGED = 0x1000, // device changed event
} cyg_can_event_flags;`

Often the flags field will contain only one single set flag. But it is possible that a number of flags is set and so the flag field should always be checked by a receiver. I.e. if the `CYGNUM_CAN_EVENT_RX` is set then also the `CYGNUM_CAN_EVENT_OVERRUN_RX` may be set if the received message caused an RX overrun.

The internal receive buffers of the CAN device driver a circular buffers. That means that even if the buffers are completely filled new messages will be received. In this case the newest message will always overwrite the oldes message in receive buffer. If this happens the `CYGNUM_CAN_EVENT_OVERRUN_RX` flag will be set for this new message that caused overwriting of the old one.

Example code for receiving one single CAN event:

`cyg_can_event rx_event;
cyg_uint32 len;
Cyg_ErrNo ret;
len = sizeof(rx_event);
ret = cyg_io_read(hDrvCAN, &rx_event, &len);
if (ENOERR == ret)
{
if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
{
// handle RX event
}
if (rx_event.flags & ~CYGNUM_CAN_EVENT_RX)
{
// handle other events
}
}
else if (-EINTR == ret)
{
// handle timeout
}
`

#### Read configuration of a CAN device

`Cyg_ErrNo cyg_io_get_config(
cyg_io_handle_t handle,
cyg_uint32 key,
void *buf,
cyg_uint32 *len )
`

This function is used to obtain run-time configuration about a device. The type of information retrieved is specified by the key. The data will be returned in the given buffer. The value of *len should contain the amount of data requested, which must be at least as large as the size appropriate to the selected key. The actual size of data retrieved is placed in *len. The appropriate key values are all listed in the file`<cyg/io/config_keys.h>`.

The following config keys are currently supported:

`CYG_IO_GET_CONFIG_READ_BLOCKING
CYG_IO_GET_CONFIG_WRITE_BLOCKING
CYG_IO_GET_CONFIG_CAN_INFO
CYG_IO_GET_CONFIG_CAN_BUFFER_INFO
CYG_IO_GET_CONFIG_CAN_TIMEOUT
`

#### Change configuration of a CAN device

`Cyg_ErrNo cyg_io_set_config(
cyg_io_handle_t handle,
cyg_uint32 key,
const void *buf,
cyg_uint32 *len)
`

This function is used to manipulate or change the run-time configuration of a device. The type of information is specified by the key. The data will be obtained from the given buffer. The value of *len should contain the amount of data provided, which must match the size appropriate to the selected key. The appropriate key values are all listed in the file `<cyg/io/config_keys.h>`.

The following config keys are currently supported:

`CYG_IO_SET_CONFIG_READ_BLOCKING
CYG_IO_SET_CONFIG_WRITE_BLOCKING
CYG_IO_SET_CONFIG_CAN_INFO
CYG_IO_SET_CONFIG_CAN_TIMEOUT
CYG_IO_SET_CONFIG_CAN_RTR_BUF
`

### Runtime Configuration

Runtime configuration is achieved by exchanging data structures with the driver via the `cyg_io_set_config()` and `cyg_io_get_config()` functions.

#### Device configuration

`typedef struct cyg_can_info_st {
cyg_can_baud_rate_t baud;
} cyg_can_info_t;`

Device configuration is achieved by by exchanging `cyg_can_info_t` data structures with the driver via the `cyg_io_set_config()` and `cyg_io_get_config()` functions using the config keys `CYG_IO_GET_CONFIG_CAN_INFO` and`CYG_IO_SET_CONFIG_CAN_INFO`. The field baud contains a baud rate selection. This must be one of the follwing values:

`CYGNUM_CAN_KBAUD_10
CYGNUM_CAN_KBAUD_20
CYGNUM_CAN_KBAUD_50
CYGNUM_CAN_KBAUD_100
CYGNUM_CAN_KBAUD_125
CYGNUM_CAN_KBAUD_250
CYGNUM_CAN_KBAUD_500
CYGNUM_CAN_KBAUD_800
CYGNUM_CAN_KBAUD_1000
`

#### Timeout configuration

`typedef struct cyg_can_timeout_info_st
{
cyg_uint32 rx_timeout;
cyg_uint32 tx_timeout;
} cyg_can_timeout_info_t;
`

Timeout configuration is achieved by by exchanging `cyg_can_timeout_info_t` data structures with the driver via the `cyg_io_set_config()` and `cyg_io_get_config()` functions using the config keys `CYG_IO_SET_CONFIG_CAN_TIMEOUT` and`CYG_IO_SET_CONFIG_CAN_TIMEOUT`. The field rx_timeout contains the timeout value for `cyg_io_read` calls and tx_timeout contains the timeout value for `cyg_io_write` calls. This call is only available if the configuration options `CYGOPT_IO_CAN_SUPPORT_NONBLOCKING` and `CYGOPT_IO_CAN_SUPPORT_TIMEOUTS` are enabled.

#### Reading buffer configuration

`typedef struct cyg_can_buf_info_st
{
cyg_int32 rx_bufsize;
cyg_int32 rx_count;
cyg_int32 tx_bufsize;
cyg_int32 tx_count;
} cyg_can_buf_info_t;
`

`CYG_IO_GET_CONFIG_CAN_BUFFER_INFO -` This function retrieves the current state of the software buffers in the serial drivers. For the transmit buffer it returns the the total number of `cyg_can_message` objects in buffer and the current number of `cyg_can_message` objects occupied in the buffer. For the recieve buffer it returns the total number of `cyg_can_event` objects in receive buffer and the current number of `cyg_can_event` objects occupied in the buffer. It does not take into account any buffering such as FIFOs or holding registers that the CAN hardware device itself may have.

#### Configuring blocking/nonblocking calls

By default all calls to `cyg_io_read()` and`cyg_io_write()` are blocking calls. The config keys
`CYG_IO_GET_CONFIG_READ_BLOCKING
CYG_IO_GET_CONFIG_WRITE_BLOCKING
` and
`CYG_IO_GET_CONFIG_READ_BLOCKING
CYG_IO_GET_CONFIG_WRITE_BLOCKING
` enable switching between blocking and nonblocking calls separatly for read and write calls. If blocking calls are configured then the read/write functions return only if a message was stored into TX buffer or a event was received from RX buffer. If nonblocking calls are enabled and there is no space in TX buffer or RX buffer is empty then the function returns immediatelly with `-EAGAIN`. If nonblocking calls are enabled and additionally timeouts are supported by driver, then the read/write functions wait until timeout value is expired and then return witn `-EINTR`. If the read/write operation succeeds during the timed wait then the functions return succesfully with`ENOERR`.

#### Remote frame response buffer configuration

The remote frame is a message frame which is transmitted to request a data frame. Some CAN hardware generates receive interrupts when a remote transmission request arrives. Other CAN hardware, i.e. the FlexCAN module, does not generate any receive interrupt. These CAN hardware chips, i.e. the FlexCAN module, can be configured to transmit a data frame automatically in response to a remote frame. In oder to support any kind of CAN hardware the eCos CAN driver provides a generic handling of remote transmission requests.

The transmission of the data frame in response of the remote frame is completely handled by the CAN driver. If the hardware driver, like the driver for the FlexCAN modul, supports harware message buffers, then the response frame is automatically transmitted if a remote transmission request with a matching ID arrives. If a CAN hardware does provide hardware support for sending the data frames in response to a remote frame, then this need to be implemented in software by the hardware device driver.

In order to respond to a remote frame, a remote frame reponse buffer need to be initialized before a data frame CAN be sent in response to a remote frame. This is achieved by by exchanging `cyg_can_rtr_buf_t` data structures with the driver via the `cyg_io_set_config()` function using the config key `CYG_IO_SET_CONFIG_CAN_RTR_BUF`. Once the buffer is initialized, the CAN data can be changed at any time by the application.

`typedef struct cyg_can_rtr_buf_st
` `{
cyg_int8 handle;
cyg_can_message msg;
` `} cyg_can_rtr_buf_t;
`

The CAN frame that should be transmitted in response to a remote frame is stored in the msg field of the `cyg_can_rtr_buf_t` data structure. If there is no buffer initialized for this data, the value of the handle field need to be set to `CYGNUM_CAN_RTR_BUF_INIT`. After the call to `cyg_io_set_config()` the handle field contains a valid value ( >= 0) or the value `CYGNUM_CAN_RTR_BUF_NA` ( < 0) if no free buffer is available. With the valid handle value the CAN data can be changed later by calling `cyg_io_set_config().`

Example code for setting up a remote response buffer:

`cyg_can_rtr_buf_t rtr_buf;
// prepare the remote response buffer
` `rtr_buf.handle = CYGNUM_CAN_RTR_BUF_INIT;
` `rtr_buf.msg.id = 0x7FF;
` `rtr_buf.msg.ext = CYGNUM_CAN_ID_STD;
` `rtr_buf.msg.rtr = CYGNUM_CAN_FRAME_DATA;
` `rtr_buf.msg.dlc = 1;
` `rtr_buf.msg.data[0] = 0xAB;
len = sizeof(rtr_buf);
` `if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
CYG_IO_SET_CONFIG_CAN_RTR_BUF ,
&rtr_buf, &len))
` `{
// handle configuration error
` `}
` `
` `if (rtr_buf.handle == CYGNUM_CAN_RTR_BUF_NA)
` `{
// no free message buffer available - handle this problem here
` `}
// change CAN data for a buffer that is already initialized
rtr_buf.msg.data[0] = 0x11;
len = sizeof(rtr_buf);
` `if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
CYG_IO_SET_CONFIG_CAN_RTR_BUF ,
&rtr_buf, &len))
` `{
// handle configuration error
` `}
`

## FlexCAN device driver

The FlexCAN module is a communication controller implementing the controller area network (CAN) protocol, an asynchronous communications protocol used in automotive and industrial control systems. It is a high speed (1 Mbit/sec), short distance, priority based protocol which can communicate using a variety of mediums (for example, fiber optic cable or an unshielded twisted pair of wires). The FlexCAN supports both the standard and extended identifier (ID) message formats specified in the CAN protocol specification, revision 2.0, part B.

It supports up to 16 flexible flexible message buffers of 0–8 bytes data length, each configurable as Rx or Tx, all supporting standard and extended messages.

The FlexCAN device driver currently supports two message buffers for sending and receiving CAN messages - message buffer 14 for receiving CAN messages and message buffer 15 for transmitting CAN messages. The receive mask of message buffer 14 is configured in a way that it is possible to receive any kind of CAN message. Message buffers 0 - 13 can be used for setting up remote frame response buffers.