]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/io/can/v2_0/tests/can_load.c
Initial revision
[karo-tx-redboot.git] / packages / io / can / v2_0 / tests / can_load.c
1 //==========================================================================
2 //
3 //        can_load.c
4 //
5 //        CAN load test
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-14
46 // Description:  CAN load test
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 // Package option requirements
73 #if defined(CYGOPT_IO_CAN_STD_CAN_ID) && defined(CYGOPT_IO_CAN_EXT_CAN_ID)
74
75
76 //===========================================================================
77 //                               DATA TYPES
78 //===========================================================================
79 typedef struct st_thread_data
80 {
81     cyg_thread   obj;
82     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
83     cyg_handle_t hdl;
84 } thread_data_t;
85
86
87 //===========================================================================
88 //                              LOCAL DATA
89 //===========================================================================
90 cyg_thread_entry_t can0_thread;
91 thread_data_t      can0_thread_data;
92
93 cyg_thread_entry_t can1_thread;
94 thread_data_t      can1_thread_data;
95
96 cyg_io_handle_t    hCAN0;
97
98
99 //===========================================================================
100 //                          LOCAL FUNCTIONS
101 //===========================================================================
102 #include "can_test_aux.inl" // include CAN test auxiliary functions
103
104
105 //===========================================================================
106 // Main thread
107 //===========================================================================
108 void can0_thread(cyg_addrword_t data)
109 {
110     cyg_uint32             len;
111     cyg_can_event          rx_event;
112     cyg_can_timeout_info_t timeouts;
113
114 #if defined(CYGOPT_IO_CAN_SUPPORT_TIMEOUTS)   
115     //
116     // setup large timeout values because we do not need timeouts here
117     //
118     timeouts.rx_timeout = 100000;
119     timeouts.tx_timeout = 100000;
120     
121     len = sizeof(timeouts);
122     if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_TIMEOUT ,&timeouts, &len))
123     {
124         CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
125     }
126 #endif // defined(CYGOPT_IO_CAN_SUPPORT_TIMEOUTS)  
127     
128     //
129     // This thread simply receives all CAN events and prints the event flags and the
130     // CAN message if it was a TX or RX event. You can use this test in order to check
131     // when a RX overrun occurs
132     //
133     while (1)
134     {
135         len = sizeof(rx_event); 
136             
137         if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
138         {
139             CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
140         }
141         else
142         {
143             print_can_flags(rx_event.flags, "");
144             
145             if ((rx_event.flags & CYGNUM_CAN_EVENT_RX) || (rx_event.flags & CYGNUM_CAN_EVENT_TX))
146             {
147                 print_can_msg(&rx_event.msg, "");
148             }
149         }    
150     }             
151 }
152
153
154 //===========================================================================
155 //                            WRITER THREAD
156 //===========================================================================
157 void can1_thread(cyg_addrword_t data)
158 {
159     cyg_uint16      i = 0;
160     cyg_uint32      len;
161     cyg_can_message tx_msg =
162     {
163         0x000,                                               // CAN identifier
164         data :
165         {
166             {0x00, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 }   // 8 data bytes
167         },
168         CYGNUM_CAN_ID_STD,                                   // standard frame
169         CYGNUM_CAN_FRAME_DATA,                               // data frame
170         8,                                                   // data length code
171     };
172     
173     //
174     // This thread simply sends CAN messages. It increments the ID for each new CAN messsage
175     // and sends a remote frame after seven data frames. In the first byte of each data frame
176     // the number (0 - 7) of the data frame is stored and the length of the data frame grows
177     // from 1 - 8 data bytes.
178     //
179     // The received pattern should look like this way:
180     // ID    Length    Data
181     // ----------------------------------------------
182     // 000   1         00
183     // 001   2         01 F1
184     // 002   3         02 F1 F2
185     // 003   4         03 F1 F2 F3
186     // 004   5         04 F1 F2 F3 F4
187     // 005   6         05 F1 F2 F3 F4 F5
188     // 006   7         06 F1 F2 F3 F4 F5 F6
189     // 007   8         Remote Request
190     // 008   1         00
191     // 009   2         01 F1
192     // 00A   3         02 F1 F2
193     // ...
194     //
195     while (1)
196     {
197         tx_msg.id            = i;
198         tx_msg.dlc           = (i % 8) + 1;
199         tx_msg.data.bytes[0] = (i % 8);
200         i = (i + 1) % 0x7FF;
201         
202         //
203         // the 6th frame is a remote frame
204         //
205         if ((i % 8) == 6)
206         {
207             tx_msg.rtr =  CYGNUM_CAN_FRAME_RTR;
208             tx_msg.ext =  CYGNUM_CAN_ID_STD;
209         }
210         //
211         // the 7th frame is a extended frame
212         //
213         else if ((i % 8) == 7)
214         {
215             tx_msg.ext =  CYGNUM_CAN_ID_EXT;
216             tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
217         }
218         //
219         // the 8th frame is a extended remote frame
220         //
221         else if (!(i % 8))
222         {
223             tx_msg.ext =  CYGNUM_CAN_ID_EXT;
224             tx_msg.rtr = CYGNUM_CAN_FRAME_RTR;
225         }
226         //
227         // all other frames are standard data frames
228         //
229         else
230         {
231             tx_msg.ext =  CYGNUM_CAN_ID_STD;
232             tx_msg.rtr =  CYGNUM_CAN_FRAME_DATA;
233         }
234         
235         len = sizeof(tx_msg);
236         if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
237         {
238             CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
239         }
240         
241         cyg_thread_delay(50);          
242     } // while (1)
243 }
244
245
246
247 void
248 cyg_start(void)
249 {
250     CYG_TEST_INIT();
251     
252     //
253     // open CAN device driver
254     //
255     if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0)) 
256     {
257         CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
258     }
259     
260     //
261     // create the two threads which access the CAN device driver
262     // a reader thread with a higher priority and a writer thread
263     // with a lower priority
264     //
265     cyg_thread_create(4, can0_thread, 
266                         (cyg_addrword_t) 0,
267                                 "can0_thread", 
268                                 (void *) can0_thread_data.stack, 
269                                 1024 * sizeof(long),
270                                 &can0_thread_data.hdl, 
271                                 &can0_thread_data.obj);
272                                 
273     cyg_thread_create(5, can1_thread, 
274                         (cyg_addrword_t) can0_thread_data.hdl,
275                                 "can1_thread", 
276                                 (void *) can1_thread_data.stack, 
277                                 1024 * sizeof(long),
278                                 &can1_thread_data.hdl, 
279                                 &can1_thread_data.obj);
280                                 
281     cyg_thread_resume(can0_thread_data.hdl);
282     cyg_thread_resume(can1_thread_data.hdl);
283     
284     cyg_scheduler_start();
285 }
286
287 #else // defined(CYGOPT_IO_CAN_STD_CAN_ID) && defined(CYGOPT_IO_CAN_EXT_CAN_ID)
288 #define N_A_MSG "Needs support for standard and extended CAN identifiers"
289 #endif
290
291 #else // CYGFUN_KERNEL_API_C
292 #define N_A_MSG "Needs kernel C API"
293 #endif
294
295 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
296 #define N_A_MSG "Needs IO/CAN and Kernel"
297 #endif
298
299 #ifdef N_A_MSG
300 void
301 cyg_start( void )
302 {
303     CYG_TEST_INIT();
304     CYG_TEST_NA( N_A_MSG);
305 }
306 #endif // N_A_MSG
307
308 // EOF can_load.c