1 #ifndef CYGONCE_CANIO_H
2 #define CYGONCE_CANIO_H
3 // ====================================================================
9 // ====================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
15 // eCos is free software; you can redistribute it and/or modify it under
16 // the terms of the GNU General Public License as published by the Free
17 // Software Foundation; either version 2 or (at your option) any later version.
19 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 // You should have received a copy of the GNU General Public License along
25 // with eCos; if not, write to the Free Software Foundation, Inc.,
26 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 // As a special exception, if other files instantiate templates or use macros
29 // or inline functions from this file, or you compile this file and link it
30 // with other works to produce a work based on this file, this file does not
31 // by itself cause the resulting work to be covered by the GNU General Public
32 // License. However the source code for this file must still be made available
33 // in accordance with section (3) of the GNU General Public License.
35 // This exception does not invalidate any other reasons why a work based on
36 // this file might be covered by the GNU General Public License.
38 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
39 // at http://sources.redhat.com/ecos/ecos-license/
40 // -------------------------------------------
41 //####ECOSGPLCOPYRIGHTEND####
42 // ====================================================================
43 //#####DESCRIPTIONBEGIN####
45 // Author(s): Uwe Kindler
46 // Contributors: gthomas
48 // Purpose: Special support for CAN I/O devices
51 //####DESCRIPTIONEND####
53 // ====================================================================
56 //===========================================================================
58 //===========================================================================
59 #include <pkgconf/system.h>
60 #include <pkgconf/io_can.h>
61 #include <pkgconf/hal.h>
62 #include <cyg/infra/cyg_type.h>
63 #include <cyg/io/config_keys.h>
71 //===========================================================================
73 //===========================================================================
76 // Supported baud rates
79 CYGNUM_CAN_KBAUD_10 = 1,
87 CYGNUM_CAN_KBAUD_1000,
88 CYGNUM_CAN_KBAUD_AUTO, // automatic detection of baudrate (if supported by hardware)
89 } cyg_can_baud_rate_t;
90 #define CYGNUM_CAN_KBAUD_MIN CYGNUM_CAN_KBAUD_10
91 #define CYGNUM_CAN_KBAUD_MAX CYGNUM_CAN_KBAUD_1000
94 // Note: two levels of macro are required to get proper expansion.
95 #define _CYG_CAN_BAUD_RATE(n) CYGNUM_CAN_KBAUD_##n
96 #define CYG_CAN_BAUD_RATE(n) _CYG_CAN_BAUD_RATE(n)
99 // Event types for received events. Not all event types are supported by each CAN
100 // hardware but normally these events should cover the most common CAN events
101 // that may occur. A combination of the event type values is allowed.
105 CYGNUM_CAN_EVENT_RX = 0x0001, // message received
106 CYGNUM_CAN_EVENT_TX = 0x0002, // mesage transmitted
107 CYGNUM_CAN_EVENT_WARNING_RX = 0x0004, // tx error counter (TEC) reached warning level (>96)
108 CYGNUM_CAN_EVENT_WARNING_TX = 0x0008, // rx error counter (REC) reached warning level (>96)
109 CYGNUM_CAN_EVENT_ERR_PASSIVE = 0x0010, // CAN "error passive" occured
110 CYGNUM_CAN_EVENT_BUS_OFF = 0x0020, // CAN "bus off" error occured
111 CYGNUM_CAN_EVENT_OVERRUN_RX = 0x0040, // overrun in RX queue or hardware occured
112 CYGNUM_CAN_EVENT_OVERRUN_TX = 0x0080, // overrun in TX queue occured
113 CYGNUM_CAN_EVENT_CAN_ERR = 0x0100, // a CAN bit or frame error occured
114 CYGNUM_CAN_EVENT_LEAVING_STANDBY = 0x0200, // CAN hardware leaves standby / power don mode or is waked up
115 CYGNUM_CAN_EVENT_ENTERING_STANDBY = 0x0400, // CAN hardware enters standby / power down mode
116 CYGNUM_CAN_EVENT_ARBITRATION_LOST = 0x0800, // arbitration lost
117 CYGNUM_CAN_EVENT_FILTER_ERR = 0x1000, // CAN message filter / acceptance filter error
118 CYGNUM_CAN_EVENT_PHY_FAULT = 0x2000, // General failure of physical layer detected (if supported by hardware)
119 CYGNUM_CAN_EVENT_PHY_H = 0x4000, // Fault on CAN-H detected (Low Speed CAN)
120 CYGNUM_CAN_EVENT_PHY_L = 0x8000, // Fault on CAN-L detected (Low Speed CAN)
121 } cyg_can_event_flags;
124 // State of CAN controller
126 typedef enum e_cyg_can_state
128 CYGNUM_CAN_STATE_ACTIVE, // CAN controller is active, no errors
129 CYGNUM_CAN_STATE_STOPPED, // CAN controller is in stopped mode
130 CYGNUM_CAN_STATE_STANDBY, // CAN controller is in Sleep mode
131 CYGNUM_CAN_STATE_BUS_WARN, // CAN controller is active, warning level is reached
132 CYGNUM_CAN_STATE_ERR_PASSIVE, // CAN controller went into error passive mode
133 CYGNUM_CAN_STATE_BUS_OFF, // CAN controller went into bus off mode
134 CYGNUM_CAN_STATE_PHY_FAULT, // General failure of physical layer detected (if supported by hardware)
135 CYGNUM_CAN_STATE_PHY_H, // Fault on CAN-H detected (Low Speed CAN)
136 CYGNUM_CAN_STATE_PHY_L, // Fault on CAN-L detected (Low Speed CAN)
137 CYGNUM_CAN_STATE_CONFIG, // CAN controller is in configuration state
141 // Identifiers for operating mode of the CAN controller.
143 typedef enum e_cyg_can_mode
145 CYGNUM_CAN_MODE_STOP, // set controller into stop mode
146 CYGNUM_CAN_MODE_START, // set controller into operational mode
147 CYGNUM_CAN_MODE_STANDBY,// set controller into standby / sleep mode
148 CYGNUM_CAN_MODE_CONFIG // set controller and driver into a state where it is safe to add/delete message buffers
152 // Type of CAN identifier.
154 typedef enum e_cyg_can_id_type
156 CYGNUM_CAN_ID_STD = 0x00, // standard ID 11 Bit
157 CYGNUM_CAN_ID_EXT = 0x01 // extended ID 29 Bit
163 typedef enum e_cyg_can_frame_type
165 CYGNUM_CAN_FRAME_DATA = 0x00, // CAN data frame
166 CYGNUM_CAN_FRAME_RTR = 0x01 // CAN remote transmission request
167 } cyg_can_frame_type;
170 // Message buffer configuration identifier - we do not use an enum here so that
171 // a specific device driver can add its own configuration identifier
173 typedef cyg_uint8 cyg_can_msgbuf_cfg_id;
174 #define CYGNUM_CAN_MSGBUF_RESET_ALL 0 // no message will be received, all remote buffers deleted
175 #define CYGNUM_CAN_MSGBUF_RX_FILTER_ALL 1 // cfg driver for reception of all can messges
176 #define CYGNUM_CAN_MSGBUF_RX_FILTER_ADD 2 // add single message filter
177 #define CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD 3 // add new remote response buffer
178 #define CYGNUM_CAN_MSGBUF_REMOTE_BUF_WRITE 4 // store data into existing remote buffer (remote buf handle required)
182 // CAN message data - this union is a container for the 8 data bytes of a can
183 // message and the union is alway part of a can message - no matter if this type
184 // is defined by generic CAN layer or by CAN hardware device driver
186 typedef union u_cyg_can_msg_data
188 cyg_uint8 bytes[8]; // byte access (array of 8 bytes)
189 cyg_uint16 words[4]; // word access (array of 4 words)
190 cyg_uint32 dwords[2]; // double word access (array of 2 dwords)
194 // CAN message type for transport or transmit of CAN messages
195 // The message data is a union. This enables byte, word and dword access and
196 // also ensures a 4 byte alignment of the message data
198 typedef struct st_cyg_can_message
200 cyg_uint32 id; // 11 Bit or 29 Bit CAN identifier - cyg_can_id_type
201 cyg_can_msg_data data; // CAN data (8 data bytes)
202 cyg_can_id_type ext; // CYGNUM_CAN_ID_STD = 11 Bit CAN id, CYGNUM_CAN_ID_EXT = 29 Bit CAN id
203 cyg_can_frame_type rtr; // CYGNUM_CAN_FRAME_DATA = data frame, CYGNUM_CAN_FRAME_RTR = remote transmission request
204 cyg_uint8 dlc; // data length code (number of bytes (0 - 8) containing valid data
208 // CAN event type for reception of CAN events from driver. CAN events may be
209 // a received CAN message or any other status information like tx msg or
212 typedef struct cyg_can_event_st
214 cyg_uint32 timestamp;
220 // CAN configuration - at the moment there is only one data member but we are
221 // prepared for future enhancements
223 typedef struct cyg_can_info_st {
224 cyg_can_baud_rate_t baud;
228 #define CYG_CAN_INFO_INIT(_baud) \
232 // buffer configuration - bufsize and count for tx are the number of messages
233 // and for rx the number of events
235 typedef struct cyg_can_buf_info_st
237 cyg_uint32 rx_bufsize;
239 cyg_uint32 tx_bufsize;
241 } cyg_can_buf_info_t;
244 // Message box configuration
246 typedef struct cyg_can_msgbox_info_st
248 cyg_uint16 count; // number of message buffers available for this device
249 cyg_uint16 free; // number of free message buffers
250 } cyg_can_msgbuf_info;
254 // Timeout configuration
256 typedef struct cyg_can_timeout_info_st
258 cyg_uint32 rx_timeout;
259 cyg_uint32 tx_timeout;
260 } cyg_can_timeout_info_t;
264 // this data type defines a handle to a message buffer or message box
265 // of the CAN hardware device
267 typedef cyg_int32 cyg_can_msgbuf_handle;
271 // structure for configuration of message buffers
273 typedef struct cyg_can_msgbox_cfg_st
275 cyg_can_msgbuf_cfg_id cfg_id; // configuration id - cfg. what to do with message buffer
276 cyg_can_msgbuf_handle handle; // handle to message buffer
277 cyg_can_message msg; // CAN message - for configuration of buffer
278 } cyg_can_msgbuf_cfg;
281 // this data type defines a CAN message filter. It consits
282 // of a handle to a message box or message buffer and a CAN message.
283 // For the filtering only the id and the ext field of the CAN message are
284 // important. The values of the other fields doesn't matter
286 typedef cyg_can_msgbuf_cfg cyg_can_filter;
289 // this data type defines a remote buffer. It consits
290 // of a handle to a message box or message buffer and the message data
291 // to send on reception of a remote request
293 typedef cyg_can_msgbuf_cfg cyg_can_remote_buf;
296 // Values for the handle field of the cyg_can_rtr_buf, cyg_can_filter and
297 // cyg_can_msgbuf_cfg data structure
299 #define CYGNUM_CAN_MSGBUF_NA -0x01 // no free message buffer available
303 // The Hardware Description Interface provides a method to gather information
304 // about the CAN hardware and the functionality of the driver. For
305 // this purpose the following structure is defined:
308 // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
309 // +-------+-------+-------+-------+--------+-------+-------+--------+
310 // | res | res | res |timest.|autobaud|FullCAN| Frametype |
312 typedef struct cyg_can_hdi_st
314 cyg_uint8 support_flags;
315 cyg_uint8 controller_type;
319 // Bit 0 and Bit 1 of the structure member support_flags describe the
320 // possibities of the CAN controller. The following values are defined:
322 #define CYGNUM_CAN_HDI_FRAMETYPE_STD 0x00 // standard frame (11-bit identifier), 2.0A
323 #define CYGNUM_CAN_HDI_FRAMETYPE_EXT_PASSIVE 0x01 // extended frame (29-bit identifier), 2.0B passive
324 #define CYGNUM_CAN_HDI_FRAMETYPE_EXT_ACTIVE 0x02 // extended frame (29-bit identifier), 2.0B active
325 #define CYGNUM_CAN_HDI_FULLCAN 0x04 // controller supports more than one receive and transmit buffer
326 #define CYGNUM_CAN_HDI_AUTBAUD 0x08 // driver supports automatic baudrate detection
327 #define CYGNUM_CAN_HDI_TIMESTAMP 0x10 // driver supports timestamps
331 // Callback configuration structure.
334 typedef void (*cyg_can_event_cb_t)(cyg_uint16, CYG_ADDRWORD);
336 // flag_mask should be set with a combination of CYGNUM_CAN_EVENT_* flags.
337 // If one of these events happens, the callback function will be called,
338 // with the actually event flags passed as a parameter.
340 typedef struct cyg_can_callback_cfg_st
342 cyg_can_event_cb_t callback_func; // callback function
343 cyg_uint16 flag_mask; // flags mask
344 CYG_ADDRWORD data; // data passed to callback
345 } cyg_can_callback_cfg;
348 //===========================================================================
349 // CAN MESSAGE ACCESS MACROS
351 // An application should not access a cyg_can_message directly instead it
352 // should use these macros for all manipulations to a CAN message.
353 //===========================================================================
355 //---------------------------------------------------------------------------
358 #define CYG_CAN_MSG_SET_FRAME_TYPE(_msg_, _type_) ((_msg_).rtr = (_type_))
359 #define CYG_CAN_MSG_GET_FRAME_TYPE(_msg_) ((_msg_).rtr)
360 #define CYG_CAN_MSG_SET_RTR(_msg_) ((_msg_).rtr = CYGNUM_CAN_FRAME_RTR)
361 #define CYG_CAN_MSG_IS_REMOTE(_msg_) ((_msg_).rtr == CYGNUM_CAN_FRAME_RTR)
364 //---------------------------------------------------------------------------
367 #define CYG_CAN_MSG_SET_ID_TYPE(_msg_, _type_) ((_msg_).ext = (_type_))
368 #define CYG_CAN_MSG_GET_ID_TYPE(_msg_) ((_msg_).ext)
369 #define CYG_CAN_MSG_SET_EXT(_msg_) ((_msg_).ext = CYGNUM_CAN_ID_EXT)
370 #define CYG_CAN_MSG_SET_STD(_msg_) ((_msg_).ext = CYGNUM_CAN_ID_STD)
371 #define CYG_CAN_MSG_IS_EXT(_msg_) ((_msg_).ext == CYGNUM_CAN_ID_EXT)
374 //---------------------------------------------------------------------------
375 // Identifier access macros
377 #define CYG_CAN_MSG_GET_ID(_msg_) ((_msg_).id)
378 #define CYG_CAN_MSG_SET_ID(_msg_, _id_) ((_msg_).id = (_id_))
379 #define CYG_CAN_MSG_SET_STD_ID(_msg_, _id_) \
381 CYG_CAN_MSG_SET_ID(_msg_, _id_); \
382 CYG_CAN_MSG_SET_STD(_msg_); \
385 #define CYG_CAN_MSG_SET_EXT_ID(_msg_, _id_) \
387 CYG_CAN_MSG_SET_ID(_msg_, _id_); \
388 CYG_CAN_MSG_SET_EXT(_msg_); \
392 //---------------------------------------------------------------------------
393 // DLC (data length code) access macros
395 #define CYG_CAN_MSG_GET_DATA_LEN(_msg_) ((_msg_).dlc)
396 #define CYG_CAN_MSG_SET_DATA_LEN(_msg_, _len_) ((_msg_).dlc = (_len_))
399 //---------------------------------------------------------------------------
400 // CAN message data access
401 // This macro returns a pointer to a cyg_can_msg_data union
403 #define CYG_CAN_MSG_DATA_PTR(_msg_) (&(_msg_).data)
404 #define CYG_CAN_MSG_GET_DATA(_msg_, _pos_) ((_msg_).data.bytes[_pos_])
405 #define CYG_CAN_MSG_SET_DATA(_msg_, _pos_, _val_) ((_msg_).data.bytes[_pos_] = (_val_))
408 //---------------------------------------------------------------------------
409 // Access multiple parameters
411 #define CYG_CAN_MSG_SET_PARAM(_msg_, _id_, _ext_, _dlc_, _rtr_) \
413 CYG_CAN_MSG_SET_ID(_msg_, _id_); \
414 CYG_CAN_MSG_SET_ID_TYPE(_msg_, _ext_); \
415 CYG_CAN_MSG_SET_DATA_LEN(_msg_, _dlc_); \
416 CYG_CAN_MSG_SET_FRAME_TYPE(_msg_, _rtr_); \
419 #define CYG_CAN_MSG_INIT(_clabel_, _id_, _ext_, _dlc_, _rtr_) \
420 cyg_can_message _clabel_ = \
434 //---------------------------------------------------------------------------
435 #endif // CYGONCE_CANIO_H