X-Git-Url: https://git.kernelconcepts.de/?p=karo-tx-redboot.git;a=blobdiff_plain;f=packages%2Fio%2Fcan%2Fv2_0%2Fdoc%2Fcan_driver_doc.html;h=543737bacc493446a010b607b459bf3d373ac2ff;hp=cea62e241b5472fac33800d81e275e137c61c21b;hb=7a4ea0a4d67744fd3f6b5f207d857005fc707b46;hpb=f0c1bd5d9f8457be4a43912a28ca2df207a7f5a4 diff --git a/packages/io/can/v2_0/doc/can_driver_doc.html b/packages/io/can/v2_0/doc/can_driver_doc.html index cea62e24..543737ba 100644 --- a/packages/io/can/v2_0/doc/can_driver_doc.html +++ b/packages/io/can/v2_0/doc/can_driver_doc.html @@ -4,7 +4,6 @@ - IO/CAN Doc @@ -23,19 +22,29 @@     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.

+

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 . The driver maintains a buffer to hold the data. The size of the intermediate buffer is configurable within the interface module. The data is not modified at all while it is being buffered. On return, *len contains the amount of characters actually consumed - that means *len always contains sizeof(cyg_can_message).

+

It is possible to configure the write call to be blocking (default) or non-blocking. Non-blocking mode requires both the configuration option CYGOPT_IO_CAN_SUPPORT_NONBLOCKING to be enabled, and the specific device to be set to non-blocking mode for writes (see cyg_io_set_config()). In blocking mode, the call will not return until there is space in the buffer and the content of the CAN message has been consumed. In non-blocking mode, if there is no space in buffer for the CAN message, -EAGAIN is returned and the caller must try again.

+

It is possible to configure the write call to be non-blocking with timeout. None-blocking mode with timeout requires the configuration option CYGOPT_IO_CAN_SUPPORT_NONBLOCKING and CYGOPT_IO_CAN_SUPPORT_TIMEOUTS to be enabled, requires the eCos kernel package to be included and the specific device to be set to non-blocking mode for writes (see cyg_io_set_config()). In non-blocking mode with timeouts, if there is no space in buffer for the CAN message, the driver waits a certain amount of time (the timeout time) for space in the buffer. If there is still no space in buffer after expiration of the timeout time, -EINTR is returned and the caller must try again.

+

If a message was sucessfully sent, the function returns ENOERR.

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:

@@ -66,7 +75,11 @@     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.

+

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. No manipulation of the data is performed before being transferred. Again, this buffering is completely configurable. On return, *len contains sizeof(cyg_can_event)
+

+

It is possible to configure the read call to be blocking (default) or non-blocking. Non-blocking mode requires both the configuration option CYGOPT_IO_CAN_SUPPORT_NONBLOCKING to be enabled, and the specific device to be set to non-blocking mode for reads (see cyg_io_set_config()). In blocking mode, the call will not return until one single CAN event has been read. In non-blocking mode, if there is no CAN event in buffer, the call returns immediately with -EAGAIN and the caller must try again.

+

It is possible to configure the write call to be non-blocking with timeout. None-blocking mode with timeout requires the configuration option CYGOPT_IO_CAN_SUPPORT_NONBLOCKING and CYGOPT_IO_CAN_SUPPORT_TIMEOUTS to be enabled, requires the eCos kernel package to be included and the specific device to be set to non-blocking mode for reads (see cyg_io_set_config()). In non-blocking mode with timeouts, if there is no CAN event in receive buffer, the driver waits a certain amound of time (the timeout time) for a CAN event to arrive. If there is still no CAN event in buffer after expiration of the timeout time, -EINTR is returned and the caller must try again.

+

If a event was sucessfully received, the function returns ENOERR.

typedef struct cyg_can_event_st
{
    cyg_uint32      timestamp;
@@ -103,11 +116,13 @@   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
+   
CYGNUM_CAN_EVENT_PHY_FAULT        = 0x2000, // General failure of physical layer detected (if supported by hardware)
+   
CYGNUM_CAN_EVENT_PHY_H            = 0x4000, // Fault on CAN-H detected (Low Speed CAN)
+   
CYGNUM_CAN_EVENT_PHY_L            = 0x8000, // Fault on CAN-L detected (Low Speed CAN)
} 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.

+

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 oldest 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. The CYGNUM_CAN_EVENT_OVERRUN_RX flag will be set also if a overrun occures in hardware message buffers of the CAN device.

Example code for receiving one single CAN event:

cyg_can_event rx_event;
cyg_uint32    len;
@@ -163,8 +178,11 @@

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_BUFFER_INFO
CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO
+ CYG_IO_GET_CONFIG_CAN_TIMEOUT
+ CYG_IO_GET_CONFIG_CAN_HDI
+ CYG_IO_GET_CONFIG_CAN_STATE

Change configuration of a CAN device

Cyg_ErrNo cyg_io_set_config(
@@ -181,9 +199,14 @@

CYG_IO_SET_CONFIG_READ_BLOCKING
CYG_IO_SET_CONFIG_WRITE_BLOCKING
CYG_IO_SET_CONFIG_CAN_INFO
+ CYG_IO_SET_CONFIG_CAN_OUTPUT_DRAIN
+ CYG_IO_SET_CONFIG_CAN_OUTPUT_FLUSH
+ CYG_IO_SET_CONFIG_CAN_INPUT_FLUSH
+ - CYG_IO_SET_CONFIG_CAN_TIMEOUT
- CYG_IO_SET_CONFIG_CAN_RTR_BUF
+ CYG_IO_SET_CONFIG_CAN_TIMEOUT
CYG_IO_SET_CONFIG_CAN_MSGBUF
+
CYG_IO_SET_CONFIG_CAN_MODE
+

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.

@@ -222,7 +245,83 @@ } 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.

+

Reading hardware description information

+

typedef struct cyg_can_hdi_st
+ {
+     cyg_uint8 support_flags;
+     cyg_uint8 controller_type;
+ } cyg_can_hdi;

+

CYG_IO_GET_CONFIG_CAN_HDI - This function retrieves information about the used hardware. The Hardware Description Interface provides a method to gather information about the CAN hardware and the functionality of the driver. For this purpose the structure cyg_can_hdi is defined. The field support_flags contains information about the capabilities of the used CAN hardware. The following flags are available:

+ + + + + + + + + + + + + + + + + + + + +
76543210
resresrestimest.SW-FiltFullCANFrametype
+

Frametype:
+
Bit 0 and Bit 1 of the structure describe the possibilities of the CAN controller. The following values are defined:

+

CYGNUM_CAN_HDI_FRAMETYPE_STD          // receives only standard frame
+ CYGNUM_CAN_HDI_FRAMETYPE_EXT_PASSIVE  // can recieve but not send extended frames
+ CYGNUM_CAN_HDI_FRAMETYPE_EXT_ACTIVE   // can send and receive extended frames
+

+

FullCAN:
+
If the Bit 2 is set to one, the CAN controller supports more than one message buffer.

+

CYGNUM_CAN_HDI_FULLCAN                // supports more than one message buffer
+

+

SW-Filter:
+
If Bit3 is set to one then the CAN driver supports some kind of software message filtering.

+

CYGNUM_CAN_HDI_FILT_SW                // software message filtering supported
+

+

Timestamp:
+
If Bit 4 is set to one then the CAN hardware supports timestamps for CAN messages

+

CYGNUM_CAN_HDI_TIMESTAMP              // CAN hardware supports timestamps
+

+

Reading message buffer configuration

+

typedef struct cyg_can_msgbox_info_st
+
{
+     c
yg_uint8 count; // number of message buffers available for this device
+     
cyg_uint8 free;  // number of free message buffers
+
} cyg_can_msgbuf_info;

+

CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO - If the CAN hardware supports more than one message buffer for reception of CAN message (flag CYGNUM_CAN_HDI_FULLCAN is set after reading hardware description interface with CYG_IO_GET_CONFIG_CAN_HDI) then this function reads the number of message buffers the CAN hardware supports and the number of free message buffers. The field count contains the number of message buffers supported by device and the field free contains the number of free message buffers. The free message buffers are available for setting up remote buffers (CYG_IO_SET_CONFIG_CAN_REMOTE_BUF) and message filters (CYG_IO_SET_CONFIG_CAN_FILTER_MSG).

+

Reading state of CAN hardware

+

typedef enum
+ {
  CYGNUM_CAN_STATE_ACTIVE,      // CAN controller is active, no errors
+   
CYGNUM_CAN_STATE_STOPPED,     // CAN controller is in stopped mode
+   
CYGNUM_CAN_STATE_STANDBY,     // CAN controller is in Sleep mode
+   
CYGNUM_CAN_STATE_BUS_WARN,    // CAN controller is active, warning level is reached
+   
CYGNUM_CAN_STATE_ERR_PASSIVE, // CAN controller went into error passive mode
+   
CYGNUM_CAN_STATE_BUS_OFF,     // CAN controller went into bus off mode
+   
CYGNUM_CAN_STATE_PHY_FAULT,   // General failure of physical layer detected (if supported by hardware)
+   
CYGNUM_CAN_STATE_PHY_H,       // Fault on CAN-H detected (Low Speed CAN)
+   
CYGNUM_CAN_STATE_PHY_L,       // Fault on CAN-L detected (Low Speed CAN)
+
} cyg_can_state;

+

CYG_IO_GET_CONFIG_CAN_STATE - This function retrieves the present state of the CAN controller. Possible values are defined in the cyg_can_state enumeration.

+

Drain output buffers

+

CYG_IO_SET_CONFIG_CAN_OUTPUT_DRAIN - This function waits for any buffered output to complete. This function only completes when there is no more data remaining to be sent to the device.

+

Flush output buffers

+

CYG_IO_SET_CONFIG_CAN_OUTPUT_FLUSH - This function discards any buffered output for the device.

+

Flush input buffers

+

CYG_IO_SET_CONFIG_CAN_INPUT_FLUSH - This function discards any buffered input for the device.

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
@@ -235,62 +334,162 @@
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. +

Message buffer configuration

+

typedef struct cyg_can_msgbox_cfg_st
+
{
+     
cyg_can_msgbuf_cfg_id cfg_id; // configuration id - cfg. what to do with message buffer
+     
cyg_can_msgbuf_handle handle; // handle to message buffer
+     
cyg_can_message msg;          // CAN message - for configuration of buffer
+
} cyg_can_msgbuf_cfg;

+

Full CAN controllers often support more the one message buffer. These message buffers are often configurable for transmission or reception of certain CAN messages or as a remote buffer. If a CAN hardware supports more than one message buffer then it is possible to configure the CAN hardware to receive only CAN messages with certain identifiers or to configure hardware support for remote buffers. If message filtering is done by hardware, the number of received CAN messages decreases and so also the time for processing received CAN messages and the memory required for buffering received messages decreases. This saves valuable memory and processing time. The eCos CAN driver supports a generic way of adding message filters or remote buffers. By default the CAN driver is configured for reception of any kind of CAN standard and extended frames. Configuration of message buffers is done by calling cyg_io_set_config() with the config key:

+

CYG_IO_SET_CONFIG_CAN_MSGBUF
+

+

and exchanging cyg_can_msgbuf_cfg data structures. The cfg_id field contains the configuration ID that tells the driver what to do with a message buffer, the handle field contains a reference to a certain message buffer and the msg field is necessary for configuration of message buffer parameters. The following configuration identifiers are supported:

+

CYGNUM_CAN_MSGBUF_RESET_ALL        // clears alle message buffers, no message will be received, all remote buffers deleted
+
CYGNUM_CAN_MSGBUF_RX_FILTER_ALL    // cfg driver for reception of all can messges
+
CYGNUM_CAN_MSGBUF_RX_FILTER_ADD    // add single message filter
+
CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD   // add new remote response buffer
+
CYGNUM_CAN_MSGBUF_REMOTE_BUF_WRITE // stores data into existing remote buffer (remote buf handle required)

+

Example code for resetting all message buffers:

+

cyg_can_msgbuf_cfg msgbox_cfg;

+

msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
+
len = sizeof(msgbox_cfg);
+
if (ENOERR != cyg_io_set_config(hDrvFlexCAN, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&msgbox_cfg, &len))
+
{
+     
// handle configuration error
+
}

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 transmission of the data frame in response to a 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 not provide hardware support for sending data frames in response to a remote frame, then this need to be implemented in software by the hardware device driver.

+

It is always possible to add remote response buffers. It does not matter if the driver is configured for reception of all CAN messages or if message filtering is used. As long as there are free message buffers available, it is possible to add remote response buffers.

+

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_remote_buf data structures with the driver via the cyg_io_set_config() function using the config key CYG_IO_SET_CONFIG_CAN_MSGBUF. Once the buffer is initialized, the CAN data can be changed at any time by the application.

+

typedef struct cyg_can_msgbuf_cfg_st
+
{
    cyg_can_msgbuf_cfg_id cfg_id; // configuration id - cfg. what to do with message buffer
+     
cyg_can_msgbuf_handle handle; // handle to message buffer
+     
cyg_can_message msg;          // CAN message - for configuration of buffer
+ } cyg_can_remote_buf;

-

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().

+

The CAN frame that should be transmitted in response to a remote frame is stored in the msg field of the cyg_can_remote_buf data structure. If there is no buffer initialized for this data, the value of the handle field need to be set to CYGNUM_CAN_MSGBUF_INIT. After the call to cyg_io_set_config() the handle field contains a valid value ( >= 0) or the value CYGNUM_CAN_MSGBUF_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(). Before adding remote buffers the device should be stopped and after configuration it should be set into operational mode again

Example code for setting up a remote response buffer:

-

cyg_can_rtr_buf_t rtr_buf;
+

cyg_can_remote_buf rtr_buf;

- // prepare the remote response buffer
-
rtr_buf.handle  = CYGNUM_CAN_RTR_BUF_INIT;
+ // prepare the remote response buffer
rtr_buf.cfg_id  = CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD;
+
rtr_buf.handle  = CYGNUM_CAN_MSGBUF_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_MSGBUF,
-                                 CYG_IO_SET_CONFIG_CAN_RTR_BUF ,
                                &rtr_buf, &len))
{
+     // handle configuration error
}

-
if (rtr_buf.handle == CYGNUM_CAN_RTR_BUF_NA)
+
if (rtr_buf.handle == CYGNUM_CAN_MSGBUF_NA)
{
+     // no free message buffer available - handle this problem here
}
-
// change CAN data for a buffer that is already initialized
+
+
+ + // change CAN data for a buffer that is already initialized
+
rtr_buf.cfg_id = CYGNUM_CAN_MSGBUF_REMOTE_BUF_WRITE;
+ + rtr_buf.msg.data[0] = 0x11;

+ + len = sizeof(rtr_buf);
if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
-                                 CYG_IO_SET_CONFIG_CAN_RTR_BUF ,
+                                 CYG_IO_SET_CONFIG_CAN_MSGBUF,
+ +                                 &rtr_buf, &len))
{
+ +     // handle configuration error
}

+

Message filter configuration

+

If message filtering is done by hardware the number of received CAN messages decreases and so also the time for processing received CAN messages and the memory required for buffering received messages decreases. This saves valuable memory and processing time. The eCos CAN driver supports a generic way of adding message filters. By default the CAN driver is configured for reception of any kind of CAN standard and extended frames. As soon as a message filter is added, the CAN driver will only receive the CAN frames with the identifier of the CAN filter. By adding a number of message filters it is possible for the CAN hardware to receive an number of different CAN messages.

+

Adding message filters is only possible if driver is not configured for reception of all available CAN messages. If driver is configured for recption of all CAN messages then message buffers neet to be reset before adding single message filters.

+

In order to add a message filter, a message buffer need to be initialized. This is achieved by by exchanging cyg_can_filter data structures with the driver via the cyg_io_set_config() function using the config key CYG_IO_SET_CONFIG_CAN_MSGBUF. Once the buffer is initialized, the CAN hardware can recive messages with the identifier of the filter.

+

typedef struct cyg_can_msgbox_cfg_st
+
{
+     cyg_can_msgbuf_cfg_id cfg_id;
+     
cyg_can_msgbuf_handle handle;
+ +     
cyg_can_message       msg;
+
} cyg_can_filter;

+

After the call to cyg_io_set_config() the handle field contains a valid value ( >= 0) or the value CYGNUM_CAN_MSGBUF_NA ( < 0) if no free buffer is available. Before adding message filters the device should be stopped and after configuration it should be set into operational mode again

+

Example code for setting up a message filter:

+

cyg_can_msgbuf_cfg msgbox_cfg;
+ cyg_can_filter rx_filter;
+
+ // reset all message buffers

+ msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
+
len = sizeof(msgbox_cfg);
+
if (ENOERR != cyg_io_set_config(hDrvFlexCAN, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&msgbox_cfg, &len))
+
{
+     
// handle configuration error
+
}
+
+
// prepare the message filter
+ rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD
+
rx_filter.msg.id  = 0x800;
+ rx_filter.msg.ext = CYGNUM_CAN_ID_EXT;
+
+ len = sizeof(rx_filter);
+ if (ENOERR != cyg_io_set_config(hDrvFlexCAN,
+                                 CYG_IO_SET_CONFIG_CAN_MSGBUF,
+                                 &rx_filter, &len))
+
{
+     
// handle configuration error;
+
}
+
else if (CYGNUM_CAN_MSGBUF_NA == rx_filter.handle)
+
{
+     // no free message buffer available - handle this problem here
+ }

+

Receive all CAN messages

After startup of your device the CAN driver is configured for reception of all available CAN messages. If you change this configuration by adding single message filters then you can reset this default state with the configuration ID:

CYGNUM_CAN_MSGBUF_RX_FILTER_ALL

+

A call to this function will clear all message filters and remote buffers and prepares the CAN hardware for recption of any kind of CAN standard and extended frames. It is not neccesary to reset the message buffer configuration before this configuration is setup because this should be done by device driver.

+

Example code for setup of a receive all CAN frames configuration:

+

cyg_can_filter rx_filter;
+
+ // now setup a RX all configuration
+
rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ALL;
+
len = sizeof(rx_filter);
+
if (ENOERR != cyg_io_set_config(hDrvFlexCAN, CYG_IO_SET_CONFIG_CAN_MSGBUF , &rx_filter, &len))
+
{
+     
CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
+
}

+

Set mode of CAN hardware

+

typedef enum
+
{
+   CYGNUM_CAN_MODE_STOP,   // set controller into stop mode
+   
CYGNUM_CAN_MODE_START,  // set controller into operational mode
+   
CYGNUM_CAN_MODE_STANDBY // set controller into standby / sleep mode
+ } cyg_can_mode;

+

CYG_IO_SET_CONFIG_CAN_MODE - This function changes the operating mode of the CAN controller. Possible values for mode are defined in the cyg_can_mode enumeration. Befor the hardware configuration of the device is changed, that means if baudrate is changed or the message buffer and filter configuration is changed, the CAN hardware should be set into stop mode and if configuration is finished, then device should be set back into operational mode. Before the device is set into standby mode, the output buffers should be flushed or drained because transmission of a CAN message may wake up the CAN hardware. If a received message wakes up the CAN hardware from standby mode then a CYGNUM_CAN_EVENT_LEAVING_STANDBY event will be inserted into receive message buffer or the CYGNUM_CAN_EVENT_LEAVING_STANDBY flag will be set for the message that caused wake up of CAN hardware.

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.

+

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 message buffer 16 of the FlexCAN modul is reserved for transmission of CAN messages. Message buffers 1 - 15 are available for configuration of remote buffers and message filters. If the FlexCAN modul is configured for reception of all CAN frames, then the user can select the number of buffers used for reception of CAN frames. The interrupt priority of each message box is configurable.

\ No newline at end of file