]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/io/can/v2_0/include/can.h
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / io / can / v2_0 / include / can.h
1 #ifndef CYGONCE_CAN_H
2 #define CYGONCE_CAN_H
3 // ====================================================================
4 //
5 //      can.h
6 //
7 //      Device I/O 
8 //
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.
14 //
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.
18 //
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
22 // for more details.
23 //
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.
27 //
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.
34 //
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.
37 //
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####
44 //
45 // Author(s):    Uwe Kindler
46 // Contributors: gthomas
47 // Date:         2005-12-05
48 // Purpose:      Internal interfaces for CAN I/O drivers
49 // Description:
50 //
51 //####DESCRIPTIONEND####
52 //
53 // ====================================================================
54
55
56 //==========================================================================
57 //                                INCLUDES
58 //==========================================================================
59 #include <pkgconf/system.h>
60 #include <pkgconf/io_can.h>
61
62 #include <cyg/infra/cyg_type.h>
63 #include <cyg/io/io.h>
64 #include <cyg/io/canio.h>
65 #include <cyg/io/devtab.h>
66 #include <cyg/hal/drv_api.h>
67
68
69 // define standard and extended id masks
70 #define CYG_CAN_STD_ID_MASK 0x7FF
71 #define CYG_CAN_EXT_ID_MASK 0x1FFFFFFF
72
73 //
74 // include device header
75 // a device can define its own types of CAN messages and CAN events. It does this
76 // in the device header CYGDAT_IO_CAN_DEVICE_INL. Because this header is included
77 // here, the device header file can use all definitions in the files included
78 // in the include section above
79 //
80 #ifdef CYGDAT_IO_CAN_DEVICE_INL 
81 #include CYGDAT_IO_CAN_DEVICE_INL   // include device header
82 #endif
83
84 //
85 // If the device did not define its own type of CAN message and CAN event, then
86 // we define standard types here and use the types cyg_can_message and cyg_can_event
87 // from CAN I/O layer
88 //
89 #ifndef CYG_CAN_MSG_T
90 #define CYG_CAN_MSG_T cyg_can_message
91 #define CYG_CAN_EVENT_T cyg_can_event
92 #define CYG_CAN_WRITE_MSG(_devmsg_ptr_, _iomsg_ptr_) (*(_devmsg_ptr_) = *(_iomsg_ptr_))
93 #define CYG_CAN_READ_EVENT(_ioevent_ptr_, _devevent_ptr_) (*(_ioevent_ptr_) = *(_devevent_ptr_))
94 #endif
95
96
97 //===========================================================================
98 //                            FORWARD DECLARATIONS
99 //===========================================================================
100 typedef struct can_channel       can_channel;
101 typedef struct can_lowlevel_funs can_lowlevel_funs;
102
103
104 //===========================================================================
105 //                                DATA TYPES
106 //===========================================================================
107
108 //---------------------------------------------------------------------------
109 // Pointers into uper-level driver which ISRs and DSRs need
110 //
111 typedef struct 
112 {
113     void (*can_init)(can_channel *chan);              // Initialize the can channel
114     void (*xmt_msg)(can_channel *chan, void *pdata);  // transmit one single message
115     void (*rcv_event)(can_channel *chan, void *pdata);// indicate can event occurance
116 } can_callbacks_t;
117
118
119 #define CAN_CALLBACKS(_l,_init,_xmt_msg,_rcv_event)  \
120 can_callbacks_t _l = {                               \
121     _init,                                           \
122     _xmt_msg,                                        \
123     _rcv_event                                       \
124 };
125
126 extern can_callbacks_t cyg_io_can_callbacks;
127
128
129 //---------------------------------------------------------------------------
130 // Data buffer for receive and transmit FIFOs
131 //
132 typedef struct can_cbuf_st
133 {
134     void                    *pdata;      // points to data buffer
135     volatile int             put;
136     volatile int             get;
137     int                      len;
138     volatile int             data_cnt;   // count of events or messages currently in buffer
139     cyg_drv_cond_t           wait;
140     cyg_drv_mutex_t          lock;
141     bool                     waiting;
142 #if defined(CYGOPT_IO_CAN_SUPPORT_NONBLOCKING)
143     bool                     blocking;   // true - timeouts will bes used, false - call blocks until data arrives
144 #endif    
145     volatile bool            abort;      // Set by an outsider to kill processing
146     volatile cyg_int32       pending;    // This many bytes waiting to be sent
147 #if defined(CYGOPT_IO_CAN_SUPPORT_TIMEOUTS)
148     cyg_uint32               timeout;    // timeout value for reading data from buffer
149 #endif
150 } can_cbuf_t;
151
152
153 #ifdef CYGOPT_IO_CAN_SUPPORT_TIMEOUTS
154 #define _TX_TIMEOUT CYGNUM_IO_CAN_DEFAULT_TIMEOUT_WRITE
155 #define _RX_TIMEOUT CYGNUM_IO_CAN_DEFAULT_TIMEOUT_READ
156 #define CBUF_INIT(_data, _len, _timeout)   \
157    {_data, 0, 0, _len, timeout : _timeout}
158 #else
159 #define _TX_TIMEOUT 0
160 #define _RX_TIMEOUT 0
161 #define CBUF_INIT(_data, _len, _timeout)   \
162    {_data, 0, 0, _len, }
163 #endif // #ifdef CYGOPT_IO_CAN_SUPPORT_TIMEOUTS
164  
165    
166 //---------------------------------------------------------------------------
167 // Private data which describes this channel + initialisation macro
168 //
169 struct can_channel 
170 {
171     can_lowlevel_funs   *funs;
172     can_callbacks_t     *callbacks;
173     void                *dev_priv;     // Whatever is needed by actual device routines
174     cyg_can_info_t       config;       // Current configuration
175     bool                 init;         // true if driver is already initialized
176     can_cbuf_t           out_cbuf;     // buffer for transmit can messages
177     can_cbuf_t           in_cbuf;      // buffer with received can events
178 #ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
179     cyg_can_callback_cfg callback_cfg; // Callback configuration
180 #endif
181 };
182
183 #ifdef CYGOPT_IO_CAN_SUPPORT_CALLBACK
184 #define CYG_CAN_CALLBACK_INIT  , {(cyg_can_event_cb_t) 0, 0, 0}
185 #else
186 #define CYG_CAN_CALLBACK_INIT
187 #endif
188
189
190 #define CAN_CHANNEL_USING_INTERRUPTS(_l,                                \
191                        _funs,                                           \
192                        _dev_priv,                                       \
193                        _baud,                                           \
194                        _out_buf, _out_buflen,                           \
195                        _in_buf, _in_buflen)                             \
196 can_channel _l = {                                                      \
197     &_funs,                                                             \
198     &cyg_io_can_callbacks,                                              \
199     &(_dev_priv),                                                       \
200     CYG_CAN_INFO_INIT(_baud),                                           \
201     false,                                                              \
202     CBUF_INIT(_out_buf, _out_buflen, _TX_TIMEOUT),                      \
203     CBUF_INIT(_in_buf, _in_buflen, _RX_TIMEOUT)                         \
204     CYG_CAN_CALLBACK_INIT                                               \
205 };
206
207
208
209 //---------------------------------------------------------------------------
210 // Low level interface functions - these functions are required by the generic 
211 // CAN driver in order to access the low level hardware
212 //
213 struct can_lowlevel_funs 
214 {
215     bool (*putmsg)(can_channel *priv, CYG_CAN_MSG_T *pmsg, void *pdata);       // send one can message - return true if consumed
216     bool (*getevent)(can_channel *priv, CYG_CAN_EVENT_T *pevent, void *pdata); // fetch one CAN event from device
217     Cyg_ErrNo (*get_config)(can_channel    *priv,                              // query hardware configuration (baud rate, etc)
218                             cyg_uint32      key, 
219                             const void     *xbuf,
220                             cyg_uint32     *len);
221     Cyg_ErrNo (*set_config)(can_channel    *priv,                              // Change hardware configuration (baud rate, etc)
222                             cyg_uint32      key, 
223                             const void     *xbuf,
224                             cyg_uint32     *len);
225     void (*start_xmit)(can_channel *priv);                                    // Enable the transmit channel and turn on transmit interrupts
226     void (*stop_xmit)(can_channel *priv);                                     // Disable the transmit channel and turn transmit interrupts off
227 };
228
229 #define CAN_LOWLEVEL_FUNS(_l,_putmsg,_getevent,_get_config,_set_config,_start_xmit,_stop_xmit)  \
230 can_lowlevel_funs _l = {                                                                        \
231   _putmsg,                                                                                      \
232   _getevent,                                                                                    \
233   _get_config,                                                                                  \
234   _set_config,                                                                                  \
235   _start_xmit,                                                                                  \
236   _stop_xmit                                                                                    \
237 };
238
239 extern cyg_devio_table_t cyg_io_can_devio;
240 //-------------------------------------------------------------------------
241 #endif // CYGONCE_SERIAL_H