]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/can/loop/v2_0/tests/can_overrun2.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / devs / can / loop / v2_0 / tests / can_overrun2.c
1 //==========================================================================
2 //
3 //        can_overrun2.c
4 //
5 //        Test CAN device RX overrun events
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-08-07
46 // Description:   Simple read/write test of CAN driver
47 //####DESCRIPTIONEND####
48
49
50 //===========================================================================
51 //                                INCLUDES
52 //===========================================================================
53 #include <pkgconf/system.h>
54
55 #include <cyg/infra/testcase.h>         // test macros
56 #include <cyg/infra/cyg_ass.h>          // assertion macros
57 #include <cyg/infra/diag.h>
58
59 // Package requirements
60 #if defined(CYGPKG_IO_CAN) && defined(CYGPKG_KERNEL)
61
62 #include <pkgconf/kernel.h>
63 #include <cyg/io/io.h>
64 #include <cyg/io/canio.h>
65
66 // Package option requirements
67 #if defined(CYGFUN_KERNEL_API_C)
68
69 #include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
70 #include <cyg/kernel/kapi.h>
71
72
73 //===========================================================================
74 //                               DATA TYPES
75 //===========================================================================
76 typedef struct st_thread_data
77 {
78     cyg_thread   obj;
79     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
80     cyg_handle_t hdl;
81 } thread_data_t;
82
83
84 //===========================================================================
85 //                              LOCAL DATA
86 //===========================================================================
87 cyg_thread_entry_t can0_thread;
88 thread_data_t      can0_thread_data;
89
90 cyg_thread_entry_t can1_thread;
91 thread_data_t      can1_thread_data;
92
93
94 //===========================================================================
95 //                          LOCAL FUNCTIONS
96 //===========================================================================
97 #include "can_test_aux.inl" // include CAN test auxiliary functions
98
99
100 //===========================================================================
101 //                             WRITER THREAD 
102 //===========================================================================
103 void can0_thread(cyg_addrword_t data)
104 {
105     cyg_io_handle_t    hCAN0;
106     cyg_uint8          i;
107     cyg_uint32         len;
108     cyg_uint32         rx_bufsize;
109     cyg_can_buf_info_t tx_buf_info;
110     cyg_can_event      rx_event;
111     cyg_can_message    tx_msg =
112     {
113         0x000,                                               // CAN identifier
114         data :
115         {
116             {0x00, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 }// 8 data bytes
117         },
118         CYGNUM_CAN_ID_STD,                                   // standard frame
119         CYGNUM_CAN_FRAME_DATA,                               // data frame
120         2,                                                   // data length code
121     };
122     
123     if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0)) 
124     {
125         CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
126     }
127     
128     len = sizeof(tx_buf_info);
129     if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&tx_buf_info, &len))
130     {
131         CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
132     }
133     
134     //
135     // Before we can write the CAN messages, we need to know the buffer size of the
136     // receiver. The receiver will tell us this buffer size with one single CAN
137     // message
138     //
139     len = sizeof(rx_event); 
140             
141     if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
142     {
143         CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
144     }
145     
146     //
147     // we expect a RX event here - we treat any other flag as an error
148     //
149     if (!(rx_event.flags & CYGNUM_CAN_EVENT_RX) || (rx_event.flags & !CYGNUM_CAN_EVENT_RX))
150     {
151         CYG_TEST_FAIL_FINISH("Unexpected RX event for /dev/can0");
152     }
153     
154     rx_bufsize = *((cyg_uint32 *)rx_event.msg.data.bytes);
155     
156     //
157     // now we send exactly one CAN message more than there is space in the receive buffer
158     // this should cause an RX ovverun in receive buffer
159     //
160     diag_printf("/dev/can0: Sending %d CAN messages\n", rx_bufsize); 
161     for (i = 0; i <= rx_bufsize; ++i)
162     {
163         //
164         // we store the message number as CAN id and in first data byte so
165         // a receiver can check this later
166         //
167         CYG_CAN_MSG_SET_STD_ID(tx_msg, 0x000 + i);
168         CYG_CAN_MSG_SET_DATA(tx_msg, 0, i);
169         len = sizeof(tx_msg); 
170             
171         if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
172         {
173             CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
174         }
175         else
176         {
177             print_can_msg(&tx_msg, "");
178         }
179     }  // for (i = 0; i <= rx_bufsize; ++i)
180     
181     cyg_thread_suspend(cyg_thread_self());                 
182 }
183
184
185 //===========================================================================
186 //                            READER THREAD
187 //===========================================================================
188 void can1_thread(cyg_addrword_t data)
189 {
190     cyg_io_handle_t    hCAN1;
191     cyg_uint8          i;
192     cyg_uint32         len;
193     cyg_can_buf_info_t rx_buf_info;
194     cyg_can_event      rx_event;
195     cyg_can_message    tx_msg;
196     
197     if (ENOERR != cyg_io_lookup("/dev/can1", &hCAN1)) 
198     {
199         CYG_TEST_FAIL_FINISH("Error opening /dev/can1");
200     }
201     
202     len = sizeof(rx_buf_info);
203     if (ENOERR != cyg_io_get_config(hCAN1, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&rx_buf_info, &len))
204     {
205         CYG_TEST_FAIL_FINISH("Error reading config of /dev/can1");
206     }
207     
208     //
209     // first we send the size of our receive buffer to the writer
210     // we setup tx message now
211     //
212     tx_msg.id  = 0x000;
213     tx_msg.ext = CYGNUM_CAN_ID_STD;
214     tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
215     tx_msg.dlc = sizeof(rx_buf_info.rx_bufsize);
216     
217     //
218     // we store size of rx buffer in CAN message. We do not need to care about
219     // endianess here because this is a loopback driver test and we will receive
220     // our own messages
221     //
222     *((cyg_uint32 *)tx_msg.data.bytes) = rx_buf_info.rx_bufsize;
223     len = sizeof(tx_msg); 
224     
225     //
226     // as soon as we send a CAN message, thread 0 will resume because it is waiting
227     // for a message
228     //
229     diag_printf("/dev/can1: Sending size of RX buffer %d\n", rx_buf_info.rx_bufsize);        
230     if (ENOERR != cyg_io_write(hCAN1, &tx_msg, &len))
231     {
232         CYG_TEST_FAIL_FINISH("Error writing to /dev/can1");
233     }
234     cyg_thread_delay(10); // let thread 0 run
235        
236     //
237     // now we check if we received CAN messages  - if receive buffer is not full
238     // the we have an error here because we expect a full receive buffer
239     //
240     len = sizeof(rx_buf_info);
241     if (ENOERR != cyg_io_get_config(hCAN1, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&rx_buf_info, &len))
242     {
243         CYG_TEST_FAIL_FINISH("Error reading config of /dev/can1");
244     }
245     
246     if (rx_buf_info.rx_bufsize != rx_buf_info.rx_count)
247     {
248         CYG_TEST_FAIL_FINISH("RX buffer of /dev/can1 does not contain number of expected messages");
249     }
250     
251     //
252     // now we wait for messages from /dev/can0
253     //
254     diag_printf("/dev/can1: Receiving %d CAN messages\n", rx_buf_info.rx_count);
255     for (i = 0; i < rx_buf_info.rx_count; ++i)
256     {
257         len = sizeof(rx_event);
258         if (ENOERR != cyg_io_read(hCAN1, &rx_event, &len))
259         {
260             CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
261         }
262         else
263         {
264             if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
265             {
266                 print_can_msg(&rx_event.msg, "");
267                 if (rx_event.msg.data.bytes[0] != (i + 1))
268                 {
269                     CYG_TEST_FAIL_FINISH("Received /dev/can1 RX event contains invalid data");
270                 }                    
271             }
272             else
273             {
274                 CYG_TEST_FAIL_FINISH("Unexpected CAN event for /dev/can1");
275             }
276             
277             //
278             // now check if any other flag is set
279             //
280             if (rx_event.flags &  CYGNUM_CAN_EVENT_OVERRUN_RX)
281             {
282                 diag_printf("RX queue overrun successfully indicated for /dev/can1\n");
283
284 //
285 // if TX events are supported then we have already a TX event in receive queue because
286 // we sent a message and the RX queue overrun will occur one message earlier
287 //
288 #if defined(CYGOPT_IO_CAN_TX_EVENT_SUPPORT)
289                 if (i < (rx_buf_info.rx_bufsize - 2))
290 #else
291                 if (i < (rx_buf_info.rx_bufsize - 1))
292 #endif
293                 {
294                     CYG_TEST_FAIL_FINISH("RX queue overrun occured too early for /dev/can1");
295                 }
296                 else
297                 {
298                     CYG_TEST_PASS_FINISH("can_overrun2 test OK"); 
299                 }
300             } // if (rx_event.flags &  CYGNUM_CAN_EVENT_OVERRUN_RX)  
301         }
302         
303     }
304 }
305
306
307
308 void
309 cyg_start(void)
310 {
311     CYG_TEST_INIT();
312     
313     //
314     // create the two threads which access the CAN device driver
315     //
316     cyg_thread_create(4, can0_thread, 
317                         (cyg_addrword_t) 0,
318                                 "can0_thread", 
319                                 (void *) can0_thread_data.stack, 
320                                 1024 * sizeof(long),
321                                 &can0_thread_data.hdl, 
322                                 &can0_thread_data.obj);
323                                 
324     cyg_thread_create(5, can1_thread, 
325                         (cyg_addrword_t) can0_thread_data.hdl,
326                                 "can1_thread", 
327                                 (void *) can1_thread_data.stack, 
328                                 1024 * sizeof(long),
329                                 &can1_thread_data.hdl, 
330                                 &can1_thread_data.obj);
331                                 
332     cyg_thread_resume(can0_thread_data.hdl);
333     cyg_thread_resume(can1_thread_data.hdl);
334     
335     cyg_scheduler_start();
336 }
337
338 #else // CYGFUN_KERNEL_API_C
339 #define N_A_MSG "Needs kernel C API"
340 #endif
341
342 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
343 #define N_A_MSG "Needs IO/CAN and Kernel"
344 #endif
345
346 #ifdef N_A_MSG
347 void
348 cyg_start( void )
349 {
350     CYG_TEST_INIT();
351     CYG_TEST_NA( N_A_MSG);
352 }
353 #endif // N_A_MSG
354
355 // EOF serial4.c