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/io_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_IO_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 void loop_can_start_xmit(can_channel *chan);
86 static void loop_can_stop_xmit(can_channel *chan);
88 static void alarm_handler(cyg_handle_t alarm, cyg_addrword_t data);
91 //-------------------------------------------------------------------------
92 // Alarm object for feeding data back into CAN channels
94 static cyg_alarm alarm_obj;
96 static cyg_handle_t alarm_handle;
98 //-------------------------------------------------------------------------
109 volatile cyg_can_event buf[FIFO_SIZE+1];
112 static struct fifo fifo0 = { false, 0, 0, 0 }; // from CAN0 to CAN1
113 static struct fifo fifo1 = { false, 0, 0, 0 }; // from CAN1 to CAN0
115 //-------------------------------------------------------------------------
119 //-------------------------------------------------------------------------
120 // Info for each serial device controlled
122 typedef struct loop_can_info {
123 struct fifo *write_fifo;
124 struct fifo *read_fifo;
127 //-------------------------------------------------------------------------
128 // Callback functions exported by this driver
129 CAN_LOWLEVEL_FUNS(loop_can_lowlevel_funs,
137 //-------------------------------------------------------------------------
138 // Hardware info for each serial line
140 #ifdef CYGPKG_IO_CAN_LOOP_CAN0
141 static loop_can_info loop_can_info0 = {
146 static cyg_can_message loop_can_txbuf0[CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_TX];
147 static cyg_can_event loop_can_rxbuf0[CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_RX];
148 #endif // CYGPKG_IO_SERIAL_LOOP_SERIAL0
150 #ifdef CYGPKG_IO_CAN_LOOP_CAN1
151 static loop_can_info loop_can_info1 = {
156 static cyg_can_message loop_can_txbuf1[CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_TX];
157 static cyg_can_event loop_can_rxbuf1[CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_RX];
158 #endif // CYGPKG_IO_SERIAL_LOOP_SERIAL1
162 //-------------------------------------------------------------------------
163 // Channel descriptions:
165 #ifdef CYGPKG_IO_CAN_LOOP_CAN0
166 CAN_CHANNEL_USING_INTERRUPTS(loop_can0_chan,
167 loop_can_lowlevel_funs,
169 CYG_CAN_BAUD_RATE(CYGNUM_DEVS_CAN_LOOP_CAN0_KBAUD),
170 loop_can_txbuf0, CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_TX,
171 loop_can_rxbuf0, CYGNUM_DEVS_CAN_LOOP_CAN0_QUEUESIZE_RX
173 #endif // CYGPKG_IO_CAN_LOOP_CAN1
175 #ifdef CYGPKG_IO_CAN_LOOP_CAN1
176 CAN_CHANNEL_USING_INTERRUPTS(loop_can1_chan,
177 loop_can_lowlevel_funs,
179 CYG_CAN_BAUD_RATE(CYGNUM_DEVS_CAN_LOOP_CAN1_KBAUD),
180 loop_can_txbuf1, CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_TX,
181 loop_can_rxbuf1, CYGNUM_DEVS_CAN_LOOP_CAN1_QUEUESIZE_RX
183 #endif // CYGPKG_IO_CAN_LOOP_CAN1
187 //-------------------------------------------------------------------------
188 // And finally, the device table entries:
190 #ifdef CYGPKG_IO_CAN_LOOP_CAN0
191 DEVTAB_ENTRY(loop_can_io0,
192 CYGDAT_DEVS_CAN_LOOP_CAN0_NAME,
193 0, // Does not depend on a lower level interface
196 loop_can_lookup, // CAN driver may need initializing
199 #endif // CYGPKG_IO_CAN_LOOP_CAN0
201 #ifdef CYGPKG_IO_CAN_LOOP_CAN1
202 DEVTAB_ENTRY(loop_can_io1,
203 CYGDAT_DEVS_CAN_LOOP_CAN1_NAME,
204 0, // Does not depend on a lower level interface
207 loop_can_lookup, // CAN driver may need initializing
210 #endif // CYGPKG_IO_CAN_LOOP_CAN1
212 //-------------------------------------------------------------------------
215 loop_can_config_channel(can_channel *chan, cyg_can_info_t *new_config, bool init)
217 if (new_config != &chan->config) {
218 chan->config = *new_config;
223 //-------------------------------------------------------------------------
224 // Function to initialize the device. Called at bootstrap time.
226 bool loop_can_init(struct cyg_devtab_entry *tab)
228 can_channel *chan = (can_channel *)tab->priv;
230 (chan->callbacks->can_init)(chan);
232 // Set up alarm for feeding data back into channels
234 cyg_alarm_create( cyg_real_time_clock(),
240 cyg_alarm_initialize( alarm_handle, 1, 1 );
242 loop_can_config_channel(chan, &chan->config, true);
247 //-------------------------------------------------------------------------
248 // This routine is called when the device is "looked" up (i.e. attached)
251 loop_can_lookup(struct cyg_devtab_entry **tab,
252 struct cyg_devtab_entry *sub_tab,
255 can_channel *chan = (can_channel *)(*tab)->priv;
256 (chan->callbacks->can_init)(chan);
260 //-------------------------------------------------------------------------
261 // Return 'true' if message is sent to device
264 loop_can_putmsg(can_channel *chan, cyg_can_message *pmsg, void *pdata)
266 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
268 struct fifo *fwr = loop_chan->write_fifo;
269 struct fifo *frd = loop_chan->read_fifo;
271 if( fwr->num == FIFO_SIZE )
276 fwr->buf[fwr->tail].msg = *pmsg;
277 fwr->buf[fwr->tail].flags = CYGNUM_CAN_EVENT_RX;
279 fwr->tail = (fwr->tail + 1) % FIFO_SIZE;
281 #ifdef CYGOPT_IO_CAN_TX_EVENT_SUPPORT
283 // if TX events are supported we insert a TX event into read fifo
285 if( frd->num < FIFO_SIZE )
287 frd->buf[frd->tail].msg = *pmsg;
288 frd->buf[frd->tail].flags = CYGNUM_CAN_EVENT_TX;
290 frd->tail = (frd->tail + 1) % FIFO_SIZE;
297 //-------------------------------------------------------------------------
299 bool loop_can_getevent(can_channel *chan, cyg_can_event *pevent, void *pdata)
301 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
303 struct fifo *frd = loop_chan->read_fifo;
305 while( frd->num == 0 )
310 *pevent = frd->buf[frd->head];
313 #ifdef CYGOPT_IO_CAN_SUPPORT_TIMESTAMP
315 // if timestamps are supported then we store a actual timestamp into
318 pevent->timestamp = cyg_current_time();
319 #endif // CYGOPT_IO_CAN_SUPPORT_TIMESTAMP
322 frd->head = (frd->head + 1) % FIFO_SIZE;
327 //-------------------------------------------------------------------------
330 loop_can_set_config(can_channel *chan, cyg_uint32 key,
331 const void *xbuf, cyg_uint32 *len)
337 //-------------------------------------------------------------------------
338 // Enable the transmitter on the device
341 loop_can_start_xmit(can_channel *chan)
343 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
345 loop_chan->write_fifo->tx_enable = true;
347 (chan->callbacks->xmt_msg)(chan, 0);
350 //-------------------------------------------------------------------------
351 // Disable the transmitter on the device
354 loop_can_stop_xmit(can_channel *chan)
356 loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
358 loop_chan->write_fifo->tx_enable = false;
361 //-------------------------------------------------------------------------
363 static void alarm_handler(cyg_handle_t alarm, cyg_addrword_t data)
365 can_channel *chan0 = &loop_can0_chan;
366 can_channel *chan1 = &loop_can1_chan;
371 (chan1->callbacks->rcv_event)(chan1, 0);
375 (chan0->callbacks->xmt_msg)(chan0, 0);
381 (chan0->callbacks->rcv_event)(chan0, 0);
385 (chan1->callbacks->xmt_msg)(chan1, 0);
387 } // while( fifo1.num )
391 #endif // CYGPKG_IO_CAN_LOOP
393 //-------------------------------------------------------------------------