]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/devs/can/arm/lpc2xxx/v2_0/tests/can_busload.c
Initial revision
[karo-tx-redboot.git] / packages / devs / can / arm / lpc2xxx / v2_0 / tests / can_busload.c
1 //==========================================================================
2 //
3 //        can_busload.c
4 //
5 //        CAN bus 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:          2007-06-26
46 // Description:   CAN bus 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
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 a large RX buffer
82 #ifdef CYGNUM_DEVS_CAN_LPC2XXX_CAN0_QUEUESIZE_RX_1024
83
84 #include "can_test_aux.inl" // include CAN test auxiliary functions
85 //===========================================================================
86 //                               DATA TYPES
87 //===========================================================================
88 typedef struct st_thread_data
89 {
90     cyg_thread   obj;
91     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
92     cyg_handle_t hdl;
93 } thread_data_t;
94
95
96 //===========================================================================
97 //                              LOCAL DATA
98 //===========================================================================
99 cyg_thread_entry_t can0_thread;
100 thread_data_t      can0_thread_data;
101 cyg_io_handle_t    hCAN_Tbl[2];
102
103
104 //===========================================================================
105 // Thread 0
106 //===========================================================================
107 void can0_thread(cyg_addrword_t data)
108 {
109     cyg_uint32      len;
110     cyg_can_message tx_msg;
111     cyg_can_event   rx_event;
112     cyg_uint32      i;
113     cyg_uint32      rx_msg_cnt = 0;
114
115     
116     //
117     // Prepeare message - we use a data length of 0 bytes here. Each received message
118     // causes an iterrupt. The shortest message is a 0 data byte message. This will generate
119     // the highest interrupt rate
120     //
121     CYG_CAN_MSG_SET_PARAM(tx_msg, 0, CYGNUM_CAN_ID_STD, 0, CYGNUM_CAN_FRAME_DATA);
122     
123     //
124     // Now send 1024 CAN messages as fast as possible to stress the receiver of CAN
125     // channel 1
126     //
127     for (i = 0; i< 1024; ++i)
128     {
129         tx_msg.id = i; 
130         len = sizeof(tx_msg);
131         if (ENOERR != cyg_io_write(hCAN_Tbl[1], &tx_msg, &len))
132         {
133             CYG_TEST_FAIL_FINISH("Error writing to channel 0");    
134         }
135     }
136     
137     //
138     // Now try to receive all 1024 CAN messages. If all messages are received
139     // and no overrun occured then the message processing is fast enought
140     //
141     while (1)
142     {
143         len = sizeof(rx_event);  
144         //
145         // First receive CAN event from real CAN hardware
146         //
147         len = sizeof(rx_event);
148         if (ENOERR != cyg_io_read(hCAN_Tbl[0], &rx_event, &len))
149         {
150             CYG_TEST_FAIL_FINISH("Error reading from channel 1");   
151         }
152         
153         if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
154         {
155             print_can_msg(&rx_event.msg, "RX chan 1:");
156             rx_msg_cnt++;
157             if (rx_msg_cnt == 1024)
158             {
159                 CYG_TEST_PASS_FINISH("CAN load test OK");        
160             }
161         } // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
162         else
163         {
164             print_can_flags(rx_event.flags, "");  
165             if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
166             {
167                 CYG_TEST_FAIL_FINISH("RX overrun for channel 1");       
168             }
169             
170             if (rx_event.flags & CYGNUM_CAN_EVENT_ERR_PASSIVE)
171             {
172                 CYG_TEST_FAIL_FINISH("Channel 1 error passive event");       
173             }
174             
175             if (rx_event.flags & CYGNUM_CAN_EVENT_BUS_OFF)
176             {
177                 CYG_TEST_FAIL_FINISH("Channel 1 bus off event");       
178             }
179         }
180     } // while (1)
181 }
182
183
184 //===========================================================================
185 // Entry point
186 //===========================================================================
187 void cyg_start(void)
188 {
189     CYG_TEST_INIT();
190
191     //
192     // open CAN device driver channel 1
193     //
194     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN0_NAME, &hCAN_Tbl[0])) 
195     {
196         CYG_TEST_FAIL_FINISH("Error opening CAN channel 0");
197     }
198     
199
200     //
201     // open CAN device driver channel 2
202     //
203     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN1_NAME, &hCAN_Tbl[1])) 
204     {
205         CYG_TEST_FAIL_FINISH("Error opening CAN channel 1");
206     }
207    
208     //
209     // create the main thread
210     //
211     cyg_thread_create(5, can0_thread, 
212                         (cyg_addrword_t) 0,
213                         "can_tx_thread", 
214                         (void *) can0_thread_data.stack, 
215                         1024 * sizeof(long),
216                         &can0_thread_data.hdl, 
217                         &can0_thread_data.obj);
218                         
219     cyg_thread_resume(can0_thread_data.hdl);
220     
221     cyg_scheduler_start();
222 }
223 #else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_QUEUESIZE_RX_1024
224 #define N_A_MSG "Channel 0 needs RX buffer size for 1024 events"
225 #endif
226
227 #else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
228 #define N_A_MSG "Baudrate of channel 0 and 1 need to be equal"
229 #endif
230
231 #else // defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
232 #define N_A_MSG "Needs support for CAN channel 1 and 2"
233 #endif
234
235 #else // CYGFUN_KERNEL_API_C
236 #define N_A_MSG "Needs kernel C API"
237 #endif
238
239 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
240 #define N_A_MSG "Needs Kernel"
241 #endif
242
243 #ifdef N_A_MSG
244 void
245 cyg_start( void )
246 {
247     CYG_TEST_INIT();
248     CYG_TEST_NA(N_A_MSG);
249 }
250 #endif // N_A_MSG
251
252 //---------------------------------------------------------------------------
253 // EOF can_busload.c