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