1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// can_multichan_rx.c
|
4 |
|
|
//
|
5 |
|
|
// CAN RX test for multiple CAN channels
|
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 test for multiple CAN controller 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 |
|
|
// Package option requirements
|
73 |
|
|
#if defined(CYGOPT_IO_CAN_SUPPORT_NONBLOCKING)
|
74 |
|
|
|
75 |
|
|
#include "can_test_aux.inl" // include CAN test auxiliary functions
|
76 |
|
|
//===========================================================================
|
77 |
|
|
// DATA TYPES
|
78 |
|
|
//===========================================================================
|
79 |
|
|
typedef struct st_thread_data
|
80 |
|
|
{
|
81 |
|
|
cyg_thread obj;
|
82 |
|
|
long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
|
83 |
|
|
cyg_handle_t hdl;
|
84 |
|
|
} thread_data_t;
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
//===========================================================================
|
88 |
|
|
// LOCAL DATA
|
89 |
|
|
//===========================================================================
|
90 |
|
|
cyg_thread_entry_t can_thread;
|
91 |
|
|
thread_data_t can0_thread_data;
|
92 |
|
|
cyg_io_handle_t hCAN_Tbl[4];
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
//===========================================================================
|
96 |
|
|
// Setup acceptance filter
|
97 |
|
|
//===========================================================================
|
98 |
|
|
void can_setup_channel(cyg_io_handle_t hCAN, unsigned char Channel)
|
99 |
|
|
{
|
100 |
|
|
cyg_uint32 len;
|
101 |
|
|
cyg_can_msgbuf_cfg msgbox_cfg;
|
102 |
|
|
cyg_uint8 i;
|
103 |
|
|
cyg_uint32 blocking;
|
104 |
|
|
|
105 |
|
|
//
|
106 |
|
|
// First we reset message buffer configuration - this is mandatory bevore starting
|
107 |
|
|
// message buffer runtime configuration. This call clears/frees all message buffers
|
108 |
|
|
// The CAN controller cannot receive any further CAN message after this call
|
109 |
|
|
//
|
110 |
|
|
msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
|
111 |
|
|
len = sizeof(msgbox_cfg);
|
112 |
|
|
if (ENOERR != cyg_io_set_config(hCAN, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&msgbox_cfg, &len))
|
113 |
|
|
{
|
114 |
|
|
CYG_TEST_FAIL_FINISH("Error resetting message buffer configuration of /dev/can0");
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
//
|
118 |
|
|
// Now setup 10 message filters for this channel
|
119 |
|
|
//
|
120 |
|
|
for (i = 0; i < 10; ++i)
|
121 |
|
|
{
|
122 |
|
|
cyg_can_filter rx_filter;
|
123 |
|
|
|
124 |
|
|
rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD;
|
125 |
|
|
rx_filter.msg.id = Channel * 0x100 + i;
|
126 |
|
|
rx_filter.msg.ext = CYGNUM_CAN_ID_STD;
|
127 |
|
|
|
128 |
|
|
len = sizeof(rx_filter);
|
129 |
|
|
if (ENOERR != cyg_io_set_config(hCAN, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rx_filter, &len))
|
130 |
|
|
{
|
131 |
|
|
CYG_TEST_FAIL_FINISH("Error writing config");
|
132 |
|
|
}
|
133 |
|
|
else if (CYGNUM_CAN_MSGBUF_NA == rx_filter.handle)
|
134 |
|
|
{
|
135 |
|
|
CYG_TEST_FAIL_FINISH("Error setting up message filter");
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
//
|
140 |
|
|
// Now set driver into nonblocking mode because the receiver thread will
|
141 |
|
|
// receive the messages from all channels
|
142 |
|
|
//
|
143 |
|
|
blocking = 0;
|
144 |
|
|
len = sizeof(blocking);
|
145 |
|
|
if (ENOERR != cyg_io_set_config(hCAN, CYG_IO_SET_CONFIG_READ_BLOCKING ,&blocking, &len))
|
146 |
|
|
{
|
147 |
|
|
CYG_TEST_FAIL_FINISH("Error setting channel into nonblocking mode");
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
//
|
151 |
|
|
// If timeouts are supported we need to setup a timeout value of 0 because
|
152 |
|
|
// the driver should return immediatelly if no message is available
|
153 |
|
|
//
|
154 |
|
|
#ifdef CYGOPT_IO_CAN_SUPPORT_TIMEOUTS
|
155 |
|
|
cyg_can_timeout_info_t timeouts;
|
156 |
|
|
|
157 |
|
|
timeouts.rx_timeout = 0;
|
158 |
|
|
timeouts.tx_timeout = 0;
|
159 |
|
|
len = sizeof(timeouts);
|
160 |
|
|
if (ENOERR != cyg_io_set_config(hCAN, CYG_IO_SET_CONFIG_CAN_TIMEOUT ,&timeouts, &len))
|
161 |
|
|
{
|
162 |
|
|
CYG_TEST_FAIL_FINISH("Error setting timeout for channel");
|
163 |
|
|
}
|
164 |
|
|
#endif
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
//===========================================================================
|
169 |
|
|
// READER THREAD
|
170 |
|
|
//===========================================================================
|
171 |
|
|
void can_thread(cyg_addrword_t data)
|
172 |
|
|
{
|
173 |
|
|
cyg_uint32 len;
|
174 |
|
|
cyg_uint8 i = 0;
|
175 |
|
|
|
176 |
|
|
//
|
177 |
|
|
// Check that all cannels have the same baudrate
|
178 |
|
|
//
|
179 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN0
|
180 |
|
|
can_setup_channel(hCAN_Tbl[0], 0);
|
181 |
|
|
#endif
|
182 |
|
|
|
183 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN1
|
184 |
|
|
can_setup_channel(hCAN_Tbl[1], 1);
|
185 |
|
|
#endif
|
186 |
|
|
|
187 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN2
|
188 |
|
|
can_setup_channel(hCAN_Tbl[2], 2);
|
189 |
|
|
#endif
|
190 |
|
|
|
191 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN3
|
192 |
|
|
can_setup_channel(hCAN_Tbl[3], 3);
|
193 |
|
|
#endif
|
194 |
|
|
|
195 |
|
|
diag_printf("\n\nThis test uses all available CAN channels for reception\n"
|
196 |
|
|
"of CAN standard messages. The following messages will be received:\n\n");
|
197 |
|
|
|
198 |
|
|
for (i = 0; i < 4; ++i)
|
199 |
|
|
{
|
200 |
|
|
if (hCAN_Tbl[i])
|
201 |
|
|
{
|
202 |
|
|
diag_printf("CAN channel %d: msg: 0x%03x - 0x%03x\n", i, i * 0x100, i * 0x100 + 9);
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
diag_printf("\n\nYou can stop this test by sending a message with ID 0xX09\n");
|
207 |
|
|
|
208 |
|
|
while (1)
|
209 |
|
|
{
|
210 |
|
|
for (i = 0; i < 4; ++i)
|
211 |
|
|
{
|
212 |
|
|
if (hCAN_Tbl[i])
|
213 |
|
|
{
|
214 |
|
|
Cyg_ErrNo ret;
|
215 |
|
|
cyg_can_event rx_event;
|
216 |
|
|
|
217 |
|
|
len = sizeof(rx_event);
|
218 |
|
|
ret = cyg_io_read(hCAN_Tbl[i], &rx_event, &len);
|
219 |
|
|
if ((ret == -EAGAIN) || (ret == -EINTR))
|
220 |
|
|
{
|
221 |
|
|
continue;
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
if (ENOERR != ret)
|
225 |
|
|
{
|
226 |
|
|
CYG_TEST_FAIL_FINISH("Error reading from channel");
|
227 |
|
|
}
|
228 |
|
|
else
|
229 |
|
|
{
|
230 |
|
|
diag_printf("Channel %d events: ", i);
|
231 |
|
|
print_can_flags(rx_event.flags, "");
|
232 |
|
|
if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
|
233 |
|
|
{
|
234 |
|
|
print_can_msg(&rx_event.msg, "");
|
235 |
|
|
if ((rx_event.msg.id & 9) == 9)
|
236 |
|
|
{
|
237 |
|
|
CYG_TEST_PASS_FINISH("LPC2xxx CAN multi channel RX test OK");
|
238 |
|
|
}
|
239 |
|
|
} // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
|
240 |
|
|
}
|
241 |
|
|
} // if (hCAN_Tbl[i])
|
242 |
|
|
} // for (i = 0; i < 4; ++i)
|
243 |
|
|
} // while (1)
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
void
|
249 |
|
|
cyg_start(void)
|
250 |
|
|
{
|
251 |
|
|
CYG_TEST_INIT();
|
252 |
|
|
|
253 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN0
|
254 |
|
|
//
|
255 |
|
|
// open CAN device driver
|
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 |
|
|
#else
|
262 |
|
|
hCAN_Tbl[0] = 0;
|
263 |
|
|
#endif
|
264 |
|
|
|
265 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN1
|
266 |
|
|
//
|
267 |
|
|
// open CAN device driver
|
268 |
|
|
//
|
269 |
|
|
if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN1_NAME, &hCAN_Tbl[1]))
|
270 |
|
|
{
|
271 |
|
|
CYG_TEST_FAIL_FINISH("Error opening CAN channel 1");
|
272 |
|
|
}
|
273 |
|
|
#else
|
274 |
|
|
hCAN_Tbl[1] = 0;
|
275 |
|
|
#endif
|
276 |
|
|
|
277 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN2
|
278 |
|
|
//
|
279 |
|
|
// open CAN device driver
|
280 |
|
|
//
|
281 |
|
|
if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN2_NAME, &hCAN_Tbl[2]))
|
282 |
|
|
{
|
283 |
|
|
CYG_TEST_FAIL_FINISH("Error opening CAN channel 2");
|
284 |
|
|
}
|
285 |
|
|
#else
|
286 |
|
|
hCAN_Tbl[2] = 0;
|
287 |
|
|
#endif
|
288 |
|
|
|
289 |
|
|
#ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN3
|
290 |
|
|
//
|
291 |
|
|
// open CAN device driver
|
292 |
|
|
//
|
293 |
|
|
if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN3_NAME, &hCAN_Tbl[3]))
|
294 |
|
|
{
|
295 |
|
|
CYG_TEST_FAIL_FINISH("Error opening CAN channel 3");
|
296 |
|
|
}
|
297 |
|
|
#else
|
298 |
|
|
hCAN_Tbl[3] = 0;
|
299 |
|
|
#endif
|
300 |
|
|
|
301 |
|
|
//
|
302 |
|
|
// create the thread that accesses the CAN device driver
|
303 |
|
|
//
|
304 |
|
|
cyg_thread_create(4, can_thread,
|
305 |
|
|
(cyg_addrword_t) 0,
|
306 |
|
|
"can0_thread",
|
307 |
|
|
(void *) can0_thread_data.stack,
|
308 |
|
|
1024 * sizeof(long),
|
309 |
|
|
&can0_thread_data.hdl,
|
310 |
|
|
&can0_thread_data.obj);
|
311 |
|
|
|
312 |
|
|
cyg_thread_resume(can0_thread_data.hdl);
|
313 |
|
|
|
314 |
|
|
cyg_scheduler_start();
|
315 |
|
|
}
|
316 |
|
|
#else // CYGOPT_IO_CAN_SUPPORT_NONBLOCKING
|
317 |
|
|
#define N_A_MSG "Needs support for nonblocking calls"
|
318 |
|
|
#endif
|
319 |
|
|
|
320 |
|
|
#else // CYGFUN_KERNEL_API_C
|
321 |
|
|
#define N_A_MSG "Needs kernel C API"
|
322 |
|
|
#endif
|
323 |
|
|
|
324 |
|
|
#else // CYGPKG_IO_CAN && CYGPKG_KERNEL
|
325 |
|
|
#define N_A_MSG "Needs Kernel"
|
326 |
|
|
#endif
|
327 |
|
|
|
328 |
|
|
#ifdef N_A_MSG
|
329 |
|
|
void
|
330 |
|
|
cyg_start( void )
|
331 |
|
|
{
|
332 |
|
|
CYG_TEST_INIT();
|
333 |
|
|
CYG_TEST_NA(N_A_MSG);
|
334 |
|
|
}
|
335 |
|
|
#endif // N_A_MSG
|
336 |
|
|
|
337 |
|
|
//---------------------------------------------------------------------------
|
338 |
|
|
// EOF can_multichan_rx.c
|