1 |
786 |
skrzyp |
//==========================================================================
|
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 Free Software Foundation, 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
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Uwe Kindler
|
43 |
|
|
// Contributors: Uwe Kindler
|
44 |
|
|
// Date: 2007-06-26
|
45 |
|
|
// Description: CAN bus load test
|
46 |
|
|
//####DESCRIPTIONEND####
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
//===========================================================================
|
50 |
|
|
// INCLUDES
|
51 |
|
|
//===========================================================================
|
52 |
|
|
#include <pkgconf/system.h>
|
53 |
|
|
|
54 |
|
|
#include <cyg/infra/testcase.h> // test macros
|
55 |
|
|
#include <cyg/infra/cyg_ass.h> // assertion macros
|
56 |
|
|
#include <cyg/infra/diag.h>
|
57 |
|
|
|
58 |
|
|
// Package requirements
|
59 |
|
|
#if defined(CYGPKG_IO_CAN) && defined(CYGPKG_KERNEL)
|
60 |
|
|
|
61 |
|
|
#include <pkgconf/kernel.h>
|
62 |
|
|
#include <cyg/io/io.h>
|
63 |
|
|
#include <cyg/io/canio.h>
|
64 |
|
|
|
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 |
|
|
// We need two CAN channels
|
73 |
|
|
#if defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
// The same baud rates are required because we send from one channel to the other one
|
77 |
|
|
#if CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
// We need a large RX buffer
|
81 |
|
|
#ifdef CYGNUM_DEVS_CAN_LPC2XXX_CAN0_QUEUESIZE_RX_1024
|
82 |
|
|
|
83 |
|
|
#include "can_test_aux.inl" // include CAN test auxiliary functions
|
84 |
|
|
//===========================================================================
|
85 |
|
|
// DATA TYPES
|
86 |
|
|
//===========================================================================
|
87 |
|
|
typedef struct st_thread_data
|
88 |
|
|
{
|
89 |
|
|
cyg_thread obj;
|
90 |
|
|
long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
|
91 |
|
|
cyg_handle_t hdl;
|
92 |
|
|
} thread_data_t;
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
//===========================================================================
|
96 |
|
|
// LOCAL DATA
|
97 |
|
|
//===========================================================================
|
98 |
|
|
cyg_thread_entry_t can0_thread;
|
99 |
|
|
thread_data_t can0_thread_data;
|
100 |
|
|
cyg_io_handle_t hCAN_Tbl[2];
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
//===========================================================================
|
104 |
|
|
// Thread 0
|
105 |
|
|
//===========================================================================
|
106 |
|
|
void can0_thread(cyg_addrword_t data)
|
107 |
|
|
{
|
108 |
|
|
cyg_uint32 len;
|
109 |
|
|
cyg_can_message tx_msg;
|
110 |
|
|
cyg_can_event rx_event;
|
111 |
|
|
cyg_uint32 i;
|
112 |
|
|
cyg_uint32 rx_msg_cnt = 0;
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
//
|
116 |
|
|
// Prepeare message - we use a data length of 0 bytes here. Each received message
|
117 |
|
|
// causes an iterrupt. The shortest message is a 0 data byte message. This will generate
|
118 |
|
|
// the highest interrupt rate
|
119 |
|
|
//
|
120 |
|
|
CYG_CAN_MSG_SET_PARAM(tx_msg, 0, CYGNUM_CAN_ID_STD, 0, CYGNUM_CAN_FRAME_DATA);
|
121 |
|
|
|
122 |
|
|
//
|
123 |
|
|
// Now send 1024 CAN messages as fast as possible to stress the receiver of CAN
|
124 |
|
|
// channel 1
|
125 |
|
|
//
|
126 |
|
|
for (i = 0; i< 1024; ++i)
|
127 |
|
|
{
|
128 |
|
|
tx_msg.id = i;
|
129 |
|
|
len = sizeof(tx_msg);
|
130 |
|
|
if (ENOERR != cyg_io_write(hCAN_Tbl[1], &tx_msg, &len))
|
131 |
|
|
{
|
132 |
|
|
CYG_TEST_FAIL_FINISH("Error writing to channel 0");
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
//
|
137 |
|
|
// Now try to receive all 1024 CAN messages. If all messages are received
|
138 |
|
|
// and no overrun occured then the message processing is fast enought
|
139 |
|
|
//
|
140 |
|
|
while (1)
|
141 |
|
|
{
|
142 |
|
|
len = sizeof(rx_event);
|
143 |
|
|
//
|
144 |
|
|
// First receive CAN event from real CAN hardware
|
145 |
|
|
//
|
146 |
|
|
len = sizeof(rx_event);
|
147 |
|
|
if (ENOERR != cyg_io_read(hCAN_Tbl[0], &rx_event, &len))
|
148 |
|
|
{
|
149 |
|
|
CYG_TEST_FAIL_FINISH("Error reading from channel 1");
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
|
153 |
|
|
{
|
154 |
|
|
print_can_msg(&rx_event.msg, "RX chan 1:");
|
155 |
|
|
rx_msg_cnt++;
|
156 |
|
|
if (rx_msg_cnt == 1024)
|
157 |
|
|
{
|
158 |
|
|
CYG_TEST_PASS_FINISH("CAN load test OK");
|
159 |
|
|
}
|
160 |
|
|
} // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
|
161 |
|
|
else
|
162 |
|
|
{
|
163 |
|
|
print_can_flags(rx_event.flags, "");
|
164 |
|
|
if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
|
165 |
|
|
{
|
166 |
|
|
CYG_TEST_FAIL_FINISH("RX overrun for channel 1");
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
if (rx_event.flags & CYGNUM_CAN_EVENT_ERR_PASSIVE)
|
170 |
|
|
{
|
171 |
|
|
CYG_TEST_FAIL_FINISH("Channel 1 error passive event");
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
if (rx_event.flags & CYGNUM_CAN_EVENT_BUS_OFF)
|
175 |
|
|
{
|
176 |
|
|
CYG_TEST_FAIL_FINISH("Channel 1 bus off event");
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
} // while (1)
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
//===========================================================================
|
184 |
|
|
// Entry point
|
185 |
|
|
//===========================================================================
|
186 |
|
|
void cyg_start(void)
|
187 |
|
|
{
|
188 |
|
|
CYG_TEST_INIT();
|
189 |
|
|
|
190 |
|
|
//
|
191 |
|
|
// open CAN device driver channel 1
|
192 |
|
|
//
|
193 |
|
|
if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN0_NAME, &hCAN_Tbl[0]))
|
194 |
|
|
{
|
195 |
|
|
CYG_TEST_FAIL_FINISH("Error opening CAN channel 0");
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
|
199 |
|
|
//
|
200 |
|
|
// open CAN device driver channel 2
|
201 |
|
|
//
|
202 |
|
|
if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN1_NAME, &hCAN_Tbl[1]))
|
203 |
|
|
{
|
204 |
|
|
CYG_TEST_FAIL_FINISH("Error opening CAN channel 1");
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
//
|
208 |
|
|
// create the main thread
|
209 |
|
|
//
|
210 |
|
|
cyg_thread_create(5, can0_thread,
|
211 |
|
|
(cyg_addrword_t) 0,
|
212 |
|
|
"can_tx_thread",
|
213 |
|
|
(void *) can0_thread_data.stack,
|
214 |
|
|
1024 * sizeof(long),
|
215 |
|
|
&can0_thread_data.hdl,
|
216 |
|
|
&can0_thread_data.obj);
|
217 |
|
|
|
218 |
|
|
cyg_thread_resume(can0_thread_data.hdl);
|
219 |
|
|
|
220 |
|
|
cyg_scheduler_start();
|
221 |
|
|
}
|
222 |
|
|
#else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_QUEUESIZE_RX_1024
|
223 |
|
|
#define N_A_MSG "Channel 0 needs RX buffer size for 1024 events"
|
224 |
|
|
#endif
|
225 |
|
|
|
226 |
|
|
#else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
|
227 |
|
|
#define N_A_MSG "Baudrate of channel 0 and 1 need to be equal"
|
228 |
|
|
#endif
|
229 |
|
|
|
230 |
|
|
#else // defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
|
231 |
|
|
#define N_A_MSG "Needs support for CAN channel 1 and 2"
|
232 |
|
|
#endif
|
233 |
|
|
|
234 |
|
|
#else // CYGFUN_KERNEL_API_C
|
235 |
|
|
#define N_A_MSG "Needs kernel C API"
|
236 |
|
|
#endif
|
237 |
|
|
|
238 |
|
|
#else // CYGPKG_IO_CAN && CYGPKG_KERNEL
|
239 |
|
|
#define N_A_MSG "Needs Kernel"
|
240 |
|
|
#endif
|
241 |
|
|
|
242 |
|
|
#ifdef N_A_MSG
|
243 |
|
|
void
|
244 |
|
|
cyg_start( void )
|
245 |
|
|
{
|
246 |
|
|
CYG_TEST_INIT();
|
247 |
|
|
CYG_TEST_NA(N_A_MSG);
|
248 |
|
|
}
|
249 |
|
|
#endif // N_A_MSG
|
250 |
|
|
|
251 |
|
|
//---------------------------------------------------------------------------
|
252 |
|
|
// EOF can_busload.c
|