1 //==========================================================================
5 // Loopback CAN device driver
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): Uwe Kindler
44 // Contributors: Uwe Kindler
46 // Purpose: Loopback CAN device driver
47 // Description: This device driver implements a pair of CAN lines that are
48 // connected back-to-back. Data output to one will appear as
49 // input on the other. This process in part driven by an alarm
50 // object which provides a degree of separation between the two
53 //####DESCRIPTIONEND####
55 //==========================================================================
57 #include <pkgconf/hal.h>
58 #include <pkgconf/io_can.h>
59 #include <pkgconf/devs_can_loop.h>
60 #include <cyg/hal/hal_io.h>
62 #include <cyg/io/io.h>
63 #include <cyg/io/devtab.h>
64 #include <cyg/io/can.h>
65 #include <cyg/hal/hal_intr.h>
66 #include <cyg/kernel/kapi.h>
68 #ifdef CYGPKG_DEVS_CAN_LOOP
70 //-------------------------------------------------------------------------
72 extern void diag_printf(const char *fmt, ...);
74 //-------------------------------------------------------------------------
75 // Forward definitions
77 static bool loop_can_init(struct cyg_devtab_entry *devtab_entry);
78 static bool loop_can_putmsg(can_channel *priv, cyg_can_message *pmsg, void *pdata);
79 static Cyg_ErrNo loop_can_lookup(struct cyg_devtab_entry **tab,
80 struct cyg_devtab_entry *sub_tab,
82 static bool loop_can_getevent(can_channel *priv, cyg_can_event *pevent, void *pdata);
83 static Cyg_ErrNo loop_can_set_config(can_channel *chan, cyg_uint32 key,
84 const void *xbuf, cyg_uint32 *len);
85 static Cyg_ErrNo loop_can_get_config(can_channel *chan, cyg_uint32 key,
86 const void* buf, cyg_uint32* len);
87 static void loop_can_start_xmit(can_channel *chan);
88 static void loop_can_stop_xmit(can_channel *chan);
90 static void alarm_handler(cyg_handle_t alarm, cyg_addrword_t data);
93 //-------------------------------------------------------------------------
94 // Alarm object for feeding data back into CAN channels
96 static cyg_alarm alarm_obj;
98 static cyg_handle_t alarm_handle;
100 //-------------------------------------------------------------------------
111 volatile cyg_can_event buf[FIFO_SIZE+1];
114 static struct fifo fifo0 = { false, 0, 0, 0 }; // from CAN0 to CAN1
115 static struct fifo fifo1 = { false, 0, 0, 0 }; // from CAN1 to CAN0
117 //-------------------------------------------------------------------------
121 //-------------------------------------------------------------------------
122 // Info for each serial device controlled
124 typedef struct loop_can_info {
125 struct fifo *write_fifo;
126 struct fifo *read_fifo;
129 //-------------------------------------------------------------------------
130 // Callback functions exported by this driver
131 CAN_LOWLEVEL_FUNS(loop_can_lowlevel_funs,
140 //-------------------------------------------------------------------------
141 // Hardware info for each serial line
143 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN0
144 static loop_can_info loop_can_info0 = {
149 static cyg_can_message loop_can_txbuf0[CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_TX];
150 static cyg_can_event loop_can_rxbuf0[CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_RX];
151 #endif // CYGPKG_IO_SERIAL_LOOP_SERIAL0
153 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN1
154 static loop_can_info loop_can_info1 = {
159 static cyg_can_message loop_can_txbuf1[CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_TX];
160 static cyg_can_event loop_can_rxbuf1[CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_RX];
161 #endif // CYGPKG_IO_SERIAL_LOOP_SERIAL1
165 //-------------------------------------------------------------------------
166 // Channel descriptions:
168 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN0
169 CAN_CHANNEL_USING_INTERRUPTS(loop_can0_chan,
170 loop_can_lowlevel_funs,
172 CYG_CAN_BAUD_RATE(CYGNUM_DEVS_CAN_LOOP_CAN0_KBAUD),
173 loop_can_txbuf0, CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_TX,
174 loop_can_rxbuf0, CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_RX
176 #endif // CYGPKG_DEVS_CAN_LOOP_CAN1
178 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN1
179 CAN_CHANNEL_USING_INTERRUPTS(loop_can1_chan,
180 loop_can_lowlevel_funs,
182 CYG_CAN_BAUD_RATE(CYGNUM_DEVS_CAN_LOOP_CAN1_KBAUD),
183 loop_can_txbuf1, CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_TX,
184 loop_can_rxbuf1, CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_RX
186 #endif // CYGPKG_DEVS_CAN_LOOP_CAN1
190 //-------------------------------------------------------------------------
191 // And finally, the device table entries:
193 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN0
194 DEVTAB_ENTRY(loop_can_io0,
195 CYGDAT_DEVS_CAN_LOOP_CAN0_NAME,
196 0, // Does not depend on a lower level interface
199 loop_can_lookup, // CAN driver may need initializing
202 #endif // CYGPKG_DEVS_CAN_LOOP_CAN0
204 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN1
205 DEVTAB_ENTRY(loop_can_io1,
206 CYGDAT_DEVS_CAN_LOOP_CAN1_NAME,
207 0, // Does not depend on a lower level interface
210 loop_can_lookup, // CAN driver may need initializing
213 #endif // CYGPKG_DEVS_CAN_LOOP_CAN1
215 //-------------------------------------------------------------------------
218 loop_can_config_channel(can_channel *chan, cyg_can_info_t *new_config, bool init)
220 if (new_config != &chan->config) {
221 chan->config = *new_config;
226 //-------------------------------------------------------------------------
227 // Function to initialize the device. Called at bootstrap time.
229 bool loop_can_init(struct cyg_devtab_entry *tab)
231 can_channel *chan = (can_channel *)tab->priv;
233 (chan->callbacks->can_init)(chan);
235 // Set up alarm for feeding data back into channels
237 cyg_alarm_create( cyg_real_time_clock(),
243 cyg_alarm_initialize( alarm_handle, 1, 1 );
245 loop_can_config_channel(chan, &chan->config, true);
250 //-------------------------------------------------------------------------
251 // This routine is called when the device is "looked" up (i.e. attached)
254 loop_can_lookup(struct cyg_devtab_entry **tab,
255 struct cyg_devtab_entry *sub_tab,
258 can_channel *chan = (can_channel *)(*tab)->priv;
259 (chan->callbacks->can_init)(chan);
263 //-------------------------------------------------------------------------
264 // Return 'true' if message is sent to device
267 loop_can_putmsg(can_channel *chan, cyg_can_message *pmsg, void *pdata)
269 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
271 struct fifo *fwr = loop_chan->write_fifo;
272 #ifdef CYGOPT_IO_CAN_TX_EVENT_SUPPORT
273 struct fifo *frd = loop_chan->read_fifo;
276 if( fwr->num == FIFO_SIZE )
281 fwr->buf[fwr->tail].msg = *pmsg;
282 fwr->buf[fwr->tail].flags = CYGNUM_CAN_EVENT_RX;
284 fwr->tail = (fwr->tail + 1) % FIFO_SIZE;
286 #ifdef CYGOPT_IO_CAN_TX_EVENT_SUPPORT
288 // if TX events are supported we insert a TX event into read fifo
290 if( frd->num < FIFO_SIZE )
292 frd->buf[frd->tail].msg = *pmsg;
293 frd->buf[frd->tail].flags = CYGNUM_CAN_EVENT_TX;
295 frd->tail = (frd->tail + 1) % FIFO_SIZE;
302 //-------------------------------------------------------------------------
304 bool loop_can_getevent(can_channel *chan, cyg_can_event *pevent, void *pdata)
306 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
308 struct fifo *frd = loop_chan->read_fifo;
310 while( frd->num == 0 )
315 *pevent = frd->buf[frd->head];
318 #ifdef CYGOPT_IO_CAN_SUPPORT_TIMESTAMP
320 // if timestamps are supported then we store a actual timestamp into
323 pevent->timestamp = cyg_current_time();
324 #endif // CYGOPT_IO_CAN_SUPPORT_TIMESTAMP
327 frd->head = (frd->head + 1) % FIFO_SIZE;
332 //-------------------------------------------------------------------------
335 loop_can_set_config(can_channel *chan, cyg_uint32 key,
336 const void *xbuf, cyg_uint32 *len)
341 //-------------------------------------------------------------------------
342 // Query device configuration
345 loop_can_get_config(can_channel *chan, cyg_uint32 key,
346 const void* buf, cyg_uint32* len)
352 //-------------------------------------------------------------------------
353 // Enable the transmitter on the device
356 loop_can_start_xmit(can_channel *chan)
358 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
360 loop_chan->write_fifo->tx_enable = true;
362 (chan->callbacks->xmt_msg)(chan, 0);
365 //-------------------------------------------------------------------------
366 // Disable the transmitter on the device
369 loop_can_stop_xmit(can_channel *chan)
371 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
373 loop_chan->write_fifo->tx_enable = false;
376 //-------------------------------------------------------------------------
378 static void alarm_handler(cyg_handle_t alarm, cyg_addrword_t data)
380 can_channel *chan0 = &loop_can0_chan;
381 can_channel *chan1 = &loop_can1_chan;
386 (chan1->callbacks->rcv_event)(chan1, 0);
390 (chan0->callbacks->xmt_msg)(chan0, 0);
396 (chan0->callbacks->rcv_event)(chan0, 0);
400 (chan1->callbacks->xmt_msg)(chan1, 0);
402 } // while( fifo1.num )
406 #endif // CYGPKG_IO_CAN_LOOP
408 //-------------------------------------------------------------------------