]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/can/arm/lpc2xxx/v2_0/tests/can_rx_tx.c
Initial revision
[karo-tx-redboot.git] / packages / devs / can / arm / lpc2xxx / v2_0 / tests / can_rx_tx.c
1 //==========================================================================
2 //
3 //        can_rx_tx.c
4 //
5 //        CAN RX / TX 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:          2007-06-26
46 // Description:   CAN RX/TX test for 2 CAN channels
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
67 // Package option requirements
68 #if defined(CYGFUN_KERNEL_API_C)
69
70 #include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
71 #include <cyg/kernel/kapi.h>
72
73 // We need two CAN channels
74 #if defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
75
76
77 // The same baud rates are required because we send from one channel to the other one
78 #if CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
79
80
81 // We need the loop can driver
82 #if defined(CYGPKG_DEVS_CAN_LOOP)
83 #include <pkgconf/devs_can_loop.h>
84
85 #include "can_test_aux.inl" // include CAN test auxiliary functions
86 //===========================================================================
87 //                               DATA TYPES
88 //===========================================================================
89 typedef struct st_thread_data
90 {
91     cyg_thread   obj;
92     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
93     cyg_handle_t hdl;
94 } thread_data_t;
95
96
97 //===========================================================================
98 //                              LOCAL DATA
99 //===========================================================================
100 cyg_thread_entry_t can_tx_thread;
101 thread_data_t      can0_thread_data;
102 cyg_thread_entry_t can_rx_thread;
103 thread_data_t      can1_thread_data;
104 cyg_io_handle_t    hCAN_Tbl[2];
105 cyg_io_handle_t    hLoopCAN_Tbl[2];
106
107
108 //===========================================================================
109 // Thread 0
110 //===========================================================================
111 void can_rx_thread(cyg_addrword_t data)
112 {
113     cyg_uint32    len;
114     cyg_can_event rx_event;
115     cyg_can_event loop_rx_event;
116     cyg_uint32    msg_cnt = 0;
117     cyg_uint8     i;
118
119     while (msg_cnt < 0xF0)
120     {
121         
122         //
123         // First receive CAN event from real CAN hardware
124         //
125         len = sizeof(rx_event);
126         if (ENOERR != cyg_io_read(hCAN_Tbl[1], &rx_event, &len))
127         {
128             CYG_TEST_FAIL_FINISH("Error reading from channel 1");   
129         }
130         
131         if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
132         {
133             print_can_msg(&rx_event.msg, "RX chan 1:");
134         } // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
135         else
136         {
137             print_can_flags(rx_event.flags, "");    
138         }
139         
140         //
141         // Now receive CAN event from loop CAN driver
142         //
143         len = sizeof(loop_rx_event);
144         if (ENOERR != cyg_io_read(hLoopCAN_Tbl[1], &loop_rx_event, &len))
145         {
146             CYG_TEST_FAIL_FINISH("Error reading from loop channel 1");   
147         }
148         
149         if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
150         {
151             print_can_msg(&rx_event.msg, "RX loop 1:");
152         } // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
153         else
154         {
155             print_can_flags(rx_event.flags, "");    
156         }   
157         
158         //
159         // Chaeck message ID and DLC of HW CAN message and CAN message from loop driver
160         // booth should be the same
161         //
162         if (rx_event.msg.id != loop_rx_event.msg.id)
163         {
164             CYG_TEST_FAIL_FINISH("Received message IDs of hw CAN channel and loop CAN channel are not equal");      
165         }
166         
167         if (rx_event.msg.dlc != loop_rx_event.msg.dlc)
168         {
169             CYG_TEST_FAIL_FINISH("Received DLCs of hw CAN msg and loop CAN msg are not equal");      
170         }
171         
172         //
173         // Now check each data byte of the receive message
174         //
175         for (i = 0; i < rx_event.msg.dlc; ++i)
176         {
177             if (rx_event.msg.data.bytes[i] != loop_rx_event.msg.data.bytes[i])
178             {
179                 CYG_TEST_FAIL_FINISH("Data of hw CAN msg and loop CAN  msg are not equal");          
180             }
181             
182             if (rx_event.msg.data.bytes[i] != (i + msg_cnt))
183             {
184                 CYG_TEST_FAIL_FINISH("CAN message contains unexpected data");         
185             }
186         }
187         
188         msg_cnt++;
189     } // while (1)
190     
191     CYG_TEST_PASS_FINISH("CAN rx/tx test OK");         
192 }
193
194
195 //===========================================================================
196 // Thread 1
197 //===========================================================================
198 void can_tx_thread(cyg_addrword_t data)
199 {
200     cyg_uint32      len;
201     cyg_can_message tx_msg;
202     cyg_uint32      msg_cnt = 0;
203     cyg_uint8       i;
204
205     
206     CYG_CAN_MSG_SET_PARAM(tx_msg, 0, CYGNUM_CAN_ID_STD, 0, CYGNUM_CAN_FRAME_DATA);
207     
208     //
209     // Prepare CAN message with a known CAN state
210     //
211     for (i = 0; i < 8; ++i)
212     {
213         tx_msg.data.bytes[i] = i; 
214     }
215     
216     while (msg_cnt < 0xF0)
217     {
218         tx_msg.id = msg_cnt;
219         len = sizeof(tx_msg);
220         if (ENOERR != cyg_io_write(hCAN_Tbl[0], &tx_msg, &len))
221         {
222             CYG_TEST_FAIL_FINISH("Error writing to channel 0");    
223         }
224         
225         
226         tx_msg.id = msg_cnt;
227         len = sizeof(tx_msg);
228         if (ENOERR != cyg_io_write(hLoopCAN_Tbl[0], &tx_msg, &len))
229         {
230             CYG_TEST_FAIL_FINISH("Error writing to channel 1");    
231         }
232         
233         //
234         // Increment data in each single data byte
235         //
236         for (i = 0; i < 8; ++i)
237         {
238             tx_msg.data.bytes[i] += 1;    
239         }
240         
241         msg_cnt++;
242         tx_msg.dlc = (tx_msg.dlc + 1) % 9;
243     } // while (msg_cnt < 0x100)
244 }
245
246
247 //===========================================================================
248 // Entry point
249 //===========================================================================
250 void cyg_start(void)
251 {
252     CYG_TEST_INIT();
253
254     //
255     // open CAN device driver channel 1
256     //
257     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN0_NAME, &hCAN_Tbl[0])) 
258     {
259         CYG_TEST_FAIL_FINISH("Error opening CAN channel 0");
260     }
261     
262
263     //
264     // open CAN device driver channel 2
265     //
266     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN1_NAME, &hCAN_Tbl[1])) 
267     {
268         CYG_TEST_FAIL_FINISH("Error opening CAN channel 1");
269     }
270     
271     //
272     // open Loop CAN device driver channel 1
273     //
274     if (ENOERR != cyg_io_lookup(CYGDAT_DEVS_CAN_LOOP_CAN0_NAME, &hLoopCAN_Tbl[0])) 
275     {
276         CYG_TEST_FAIL_FINISH("Error opening loop CAN channel 0");
277     }
278     
279
280     //
281     // open Loop CAN device driver channel 2
282     //
283     if (ENOERR != cyg_io_lookup(CYGDAT_DEVS_CAN_LOOP_CAN1_NAME, &hLoopCAN_Tbl[1])) 
284     {
285         CYG_TEST_FAIL_FINISH("Error opening loop CAN channel 1");
286     }
287     
288     
289
290     
291    
292     //
293     // create the first thread that access the CAN device driver
294     //
295     cyg_thread_create(5, can_tx_thread, 
296                         (cyg_addrword_t) 0,
297                         "can_tx_thread", 
298                         (void *) can0_thread_data.stack, 
299                         1024 * sizeof(long),
300                         &can0_thread_data.hdl, 
301                         &can0_thread_data.obj);
302     
303     //
304     // create the second thread that access the CAN device driver
305     //
306     cyg_thread_create(4, can_rx_thread, 
307                          (cyg_addrword_t) 0,
308                          "can_rx_thread", 
309                          (void *) can1_thread_data.stack, 
310                          1024 * sizeof(long),
311                          &can1_thread_data.hdl, 
312                          &can1_thread_data.obj);
313                         
314     cyg_thread_resume(can0_thread_data.hdl);
315     cyg_thread_resume(can1_thread_data.hdl);
316     
317     cyg_scheduler_start();
318 }
319 #else // defined(CYGPKG_DEVS_CAN_LOOP)
320 #define N_A_MSG "Needs support for loop CAN device driver"
321 #endif
322
323 #else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
324 #define N_A_MSG "Baudrate of channel 0 and 1 need to be equal"
325 #endif
326
327 #else // defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
328 #define N_A_MSG "Needs support for CAN channel 1 and 2"
329 #endif
330
331 #else // CYGFUN_KERNEL_API_C
332 #define N_A_MSG "Needs kernel C API"
333 #endif
334
335 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
336 #define N_A_MSG "Needs Kernel"
337 #endif
338
339 #ifdef N_A_MSG
340 void
341 cyg_start( void )
342 {
343     CYG_TEST_INIT();
344     CYG_TEST_NA(N_A_MSG);
345 }
346 #endif // N_A_MSG
347
348 //---------------------------------------------------------------------------
349 // EOF can_rx_tx.c