]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/can/loop/v2_0/src/loop_can.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / devs / can / loop / v2_0 / src / loop_can.c
1 //==========================================================================
2 //
3 //      loop_can.c
4 //
5 //      Loopback CAN device driver
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):   Uwe Kindler
44 // Contributors: Uwe Kindler
45 // Date:        2005-07-10
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
51 //              channels.
52 //
53 //####DESCRIPTIONEND####
54 //
55 //==========================================================================
56
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>
61
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>
67
68 #ifdef CYGPKG_DEVS_CAN_LOOP
69
70 //-------------------------------------------------------------------------
71
72 extern void diag_printf(const char *fmt, ...);
73
74 //-------------------------------------------------------------------------
75 // Forward definitions
76
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,
81                                    const char *name);
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);
89
90 static void alarm_handler(cyg_handle_t alarm, cyg_addrword_t data);
91
92
93 //-------------------------------------------------------------------------
94 // Alarm object for feeding data back into CAN channels
95
96 static cyg_alarm alarm_obj;
97
98 static cyg_handle_t alarm_handle;
99
100 //-------------------------------------------------------------------------
101 // Transfer FIFOs
102
103 #define FIFO_SIZE 33
104
105 struct fifo
106 {
107     cyg_bool                tx_enable;
108     volatile int            head;
109     volatile int            tail;
110     volatile int            num;
111     volatile cyg_can_event  buf[FIFO_SIZE+1];
112 };
113
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
116
117 //-------------------------------------------------------------------------
118
119 #define BUFSIZE 128
120
121 //-------------------------------------------------------------------------
122 // Info for each serial device controlled
123
124 typedef struct loop_can_info {
125     struct fifo         *write_fifo;
126     struct fifo         *read_fifo;
127 } loop_can_info;
128
129 //-------------------------------------------------------------------------
130 // Callback functions exported by this driver   
131 CAN_LOWLEVEL_FUNS(loop_can_lowlevel_funs,
132                   loop_can_putmsg,
133                   loop_can_getevent,
134                   loop_can_get_config,
135                   loop_can_set_config,
136                   loop_can_start_xmit,
137                   loop_can_stop_xmit
138      );
139
140 //-------------------------------------------------------------------------
141 // Hardware info for each serial line
142
143 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN0
144 static loop_can_info loop_can_info0 = {
145     &fifo0,
146     &fifo1
147 };
148
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
152
153 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN1
154 static loop_can_info loop_can_info1 = {
155     &fifo1,
156     &fifo0
157 };
158
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
162
163
164
165 //-------------------------------------------------------------------------
166 // Channel descriptions:
167 //
168 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN0
169 CAN_CHANNEL_USING_INTERRUPTS(loop_can0_chan,
170                              loop_can_lowlevel_funs,
171                              loop_can_info0,
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
175     );
176 #endif // CYGPKG_DEVS_CAN_LOOP_CAN1
177     
178 #ifdef CYGPKG_DEVS_CAN_LOOP_CAN1
179 CAN_CHANNEL_USING_INTERRUPTS(loop_can1_chan,
180                              loop_can_lowlevel_funs,
181                              loop_can_info1,
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
185     );
186 #endif // CYGPKG_DEVS_CAN_LOOP_CAN1
187  
188  
189   
190 //-------------------------------------------------------------------------
191 // And finally, the device table entries:
192 //
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
197              &cyg_io_can_devio, 
198              loop_can_init, 
199              loop_can_lookup,        // CAN driver may need initializing
200              &loop_can0_chan
201     );
202 #endif // CYGPKG_DEVS_CAN_LOOP_CAN0
203
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
208              &cyg_io_can_devio, 
209              loop_can_init, 
210              loop_can_lookup,        // CAN driver may need initializing
211              &loop_can1_chan
212     );
213 #endif // CYGPKG_DEVS_CAN_LOOP_CAN1
214
215 //-------------------------------------------------------------------------
216
217 static bool
218 loop_can_config_channel(can_channel *chan, cyg_can_info_t *new_config, bool init)
219 {
220     if (new_config != &chan->config) {
221         chan->config = *new_config;
222     }
223     return true;
224 }
225
226 //-------------------------------------------------------------------------
227 // Function to initialize the device.  Called at bootstrap time.
228
229 bool loop_can_init(struct cyg_devtab_entry *tab)
230 {
231     can_channel *chan = (can_channel *)tab->priv;
232
233     (chan->callbacks->can_init)(chan);
234     
235     // Set up alarm for feeding data back into channels
236
237     cyg_alarm_create( cyg_real_time_clock(),
238                       alarm_handler,
239                       0,
240                       &alarm_handle,
241                       &alarm_obj);
242
243     cyg_alarm_initialize( alarm_handle, 1, 1 );
244     
245     loop_can_config_channel(chan, &chan->config, true);
246     
247     return true;
248 }
249
250 //-------------------------------------------------------------------------
251 // This routine is called when the device is "looked" up (i.e. attached)
252
253 static Cyg_ErrNo 
254 loop_can_lookup(struct cyg_devtab_entry **tab, 
255                 struct cyg_devtab_entry *sub_tab,
256                 const char *name)
257 {
258     can_channel *chan = (can_channel *)(*tab)->priv;
259     (chan->callbacks->can_init)(chan);
260     return ENOERR;
261 }
262
263 //-------------------------------------------------------------------------
264 // Return 'true' if message is sent to device
265
266 bool
267 loop_can_putmsg(can_channel *chan, cyg_can_message *pmsg, void *pdata)
268 {
269     loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
270
271     struct fifo *fwr = loop_chan->write_fifo;
272 #ifdef CYGOPT_IO_CAN_TX_EVENT_SUPPORT 
273     struct fifo *frd = loop_chan->read_fifo;
274 #endif
275
276     if( fwr->num == FIFO_SIZE )
277     {
278         return false;
279     }
280
281     fwr->buf[fwr->tail].msg  = *pmsg;
282     fwr->buf[fwr->tail].flags = CYGNUM_CAN_EVENT_RX;
283     fwr->num++;
284     fwr->tail = (fwr->tail + 1) % FIFO_SIZE;
285     
286 #ifdef CYGOPT_IO_CAN_TX_EVENT_SUPPORT 
287     //
288     // if TX events are supported we insert a TX event into read fifo
289     //
290     if( frd->num < FIFO_SIZE )
291     {
292         frd->buf[frd->tail].msg  = *pmsg;
293         frd->buf[frd->tail].flags = CYGNUM_CAN_EVENT_TX;
294         frd->num++;
295         frd->tail = (frd->tail + 1) % FIFO_SIZE;
296     }   
297 #endif
298     
299     return true;
300 }
301
302 //-------------------------------------------------------------------------
303
304 bool loop_can_getevent(can_channel *chan, cyg_can_event *pevent, void *pdata)
305 {
306     loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
307
308     struct fifo *frd = loop_chan->read_fifo;
309     
310     while( frd->num == 0 )
311     {
312         continue;
313     }
314
315     *pevent = frd->buf[frd->head];
316     
317     
318 #ifdef CYGOPT_IO_CAN_SUPPORT_TIMESTAMP 
319     //
320     // if timestamps are supported then we store a actual timestamp into
321     // CAN event
322     // 
323     pevent->timestamp = cyg_current_time();
324 #endif // CYGOPT_IO_CAN_SUPPORT_TIMESTAMP
325
326     frd->num--;
327     frd->head = (frd->head + 1) % FIFO_SIZE;
328     
329     return true;
330 }
331
332 //-------------------------------------------------------------------------
333
334 static Cyg_ErrNo
335 loop_can_set_config(can_channel *chan, cyg_uint32 key,
336                     const void *xbuf, cyg_uint32 *len)
337 {
338     return ENOERR;
339 }
340
341 //-------------------------------------------------------------------------
342 // Query device configuration
343
344 static Cyg_ErrNo 
345 loop_can_get_config(can_channel *chan, cyg_uint32 key, 
346                     const void*  buf,  cyg_uint32* len)
347 {
348         return ENOERR;
349 }
350
351
352 //-------------------------------------------------------------------------
353 // Enable the transmitter on the device
354
355 static void
356 loop_can_start_xmit(can_channel *chan)
357 {  
358     loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
359
360     loop_chan->write_fifo->tx_enable = true;
361     
362     (chan->callbacks->xmt_msg)(chan, 0);  
363 }
364
365 //-------------------------------------------------------------------------
366 // Disable the transmitter on the device
367
368 static void 
369 loop_can_stop_xmit(can_channel *chan)
370
371     loop_can_info *loop_chan = (loop_can_info *)chan->dev_priv;
372
373     loop_chan->write_fifo->tx_enable = false;  
374 }
375
376 //-------------------------------------------------------------------------
377
378 static void alarm_handler(cyg_handle_t alarm, cyg_addrword_t data)
379 {
380     can_channel *chan0 = &loop_can0_chan;
381     can_channel *chan1 = &loop_can1_chan;
382
383
384     while(fifo0.num )
385     {
386         (chan1->callbacks->rcv_event)(chan1, 0);
387         
388         if(fifo0.tx_enable )
389         {
390             (chan0->callbacks->xmt_msg)(chan0, 0); 
391         }       
392     }
393
394     while(fifo1.num )
395     {
396         (chan0->callbacks->rcv_event)(chan0, 0);
397         
398         if(fifo1.tx_enable )
399         {
400             (chan1->callbacks->xmt_msg)(chan1, 0); 
401         }        
402     } // while( fifo1.num )
403 }
404     
405
406 #endif // CYGPKG_IO_CAN_LOOP
407
408 //-------------------------------------------------------------------------
409 // EOF loop_can.c