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