1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// can_remote.c
|
4 |
|
|
//
|
5 |
|
|
// CAN remote response buffer 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: 2005-08-14
|
45 |
|
|
// Description: CAN 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 |
|
|
// 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 |
|
|
// Package option requirements
|
72 |
|
|
#if defined(CYGOPT_IO_CAN_RUNTIME_MBOX_CFG)
|
73 |
|
|
|
74 |
|
|
// Package option requirements
|
75 |
|
|
#if defined(CYGOPT_IO_CAN_REMOTE_BUF)
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
//===========================================================================
|
79 |
|
|
// DATA TYPES
|
80 |
|
|
//===========================================================================
|
81 |
|
|
typedef struct st_thread_data
|
82 |
|
|
{
|
83 |
|
|
cyg_thread obj;
|
84 |
|
|
long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
|
85 |
|
|
cyg_handle_t hdl;
|
86 |
|
|
} thread_data_t;
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
//===========================================================================
|
90 |
|
|
// LOCAL DATA
|
91 |
|
|
//===========================================================================
|
92 |
|
|
cyg_thread_entry_t can0_thread;
|
93 |
|
|
thread_data_t can0_thread_data;
|
94 |
|
|
cyg_io_handle_t hCAN0;
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
//===========================================================================
|
98 |
|
|
// LOCAL FUNCTIONS
|
99 |
|
|
//===========================================================================
|
100 |
|
|
#include "can_test_aux.inl" // include CAN test auxiliary functions
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
//===========================================================================
|
104 |
|
|
// Main thread
|
105 |
|
|
//===========================================================================
|
106 |
|
|
void can0_thread(cyg_addrword_t data)
|
107 |
|
|
{
|
108 |
|
|
cyg_uint32 len;
|
109 |
|
|
cyg_can_event rx_event;
|
110 |
|
|
cyg_can_remote_buf rtr_buf;
|
111 |
|
|
cyg_can_filter rx_filter;
|
112 |
|
|
cyg_can_msgbuf_info msgbox_info;
|
113 |
|
|
cyg_can_msgbuf_cfg msgbox_cfg;
|
114 |
|
|
|
115 |
|
|
//
|
116 |
|
|
// We would like to setup 2 remote buffers - check if we have enough
|
117 |
|
|
// free message buffers
|
118 |
|
|
//
|
119 |
|
|
len = sizeof(msgbox_info);
|
120 |
|
|
if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
|
121 |
|
|
{
|
122 |
|
|
CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
|
123 |
|
|
}
|
124 |
|
|
else
|
125 |
|
|
{
|
126 |
|
|
diag_printf("\n\n\nMessage boxes available: %d free: %d\n",
|
127 |
|
|
msgbox_info.count, msgbox_info.free);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
//
|
131 |
|
|
// We have not enougth free message buffers, so we clear all message buffers now
|
132 |
|
|
// and try again
|
133 |
|
|
//
|
134 |
|
|
if (msgbox_info.free < 2)
|
135 |
|
|
{
|
136 |
|
|
msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
|
137 |
|
|
len = sizeof(msgbox_cfg);
|
138 |
|
|
if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF, &msgbox_cfg, &len))
|
139 |
|
|
{
|
140 |
|
|
CYG_TEST_FAIL_FINISH("Error clearing message buffers of /dev/can0");
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
//
|
144 |
|
|
// Now query number of free message boxes again. We need 3 free message boxes.
|
145 |
|
|
// 2 message boxes for setup of remote response buffers and 1 message box for
|
146 |
|
|
// setup of receive message box for CAN identifier 0x100
|
147 |
|
|
//
|
148 |
|
|
len = sizeof(msgbox_info);
|
149 |
|
|
if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
|
150 |
|
|
{
|
151 |
|
|
CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
|
152 |
|
|
}
|
153 |
|
|
else
|
154 |
|
|
{
|
155 |
|
|
diag_printf("Message boxes available: %d free: %d\n",
|
156 |
|
|
msgbox_info.count, msgbox_info.free);
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
if (msgbox_info.free < 3)
|
160 |
|
|
{
|
161 |
|
|
CYG_TEST_FAIL_FINISH("Not enough free message buffers available for /dev/can0");
|
162 |
|
|
}
|
163 |
|
|
else
|
164 |
|
|
{
|
165 |
|
|
rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD;
|
166 |
|
|
CYG_CAN_MSG_SET_STD_ID(rx_filter.msg, 0x100);
|
167 |
|
|
|
168 |
|
|
len = sizeof(rx_filter);
|
169 |
|
|
if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rx_filter, &len))
|
170 |
|
|
{
|
171 |
|
|
CYG_TEST_FAIL_FINISH("Error adding rx filter for CAN ID 0x100 for /dev/can0");
|
172 |
|
|
}
|
173 |
|
|
} // if (msgbox_info.free < 3)
|
174 |
|
|
} // if (msgbox_info.free < 2)
|
175 |
|
|
#ifdef CYGOPT_IO_CAN_STD_CAN_ID
|
176 |
|
|
//
|
177 |
|
|
// Setup the first remote response buffer for resception of standard
|
178 |
|
|
// remote frames
|
179 |
|
|
//
|
180 |
|
|
rtr_buf.cfg_id = CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD;
|
181 |
|
|
CYG_CAN_MSG_SET_PARAM(rtr_buf.msg, 0x7FF, CYGNUM_CAN_ID_STD, 1, CYGNUM_CAN_FRAME_DATA);
|
182 |
|
|
CYG_CAN_MSG_SET_DATA(rtr_buf.msg, 0, 0xAB);
|
183 |
|
|
|
184 |
|
|
len = sizeof(rtr_buf);
|
185 |
|
|
if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rtr_buf, &len))
|
186 |
|
|
{
|
187 |
|
|
CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
|
188 |
|
|
}
|
189 |
|
|
#endif
|
190 |
|
|
|
191 |
|
|
#ifdef CYGOPT_IO_CAN_EXT_CAN_ID
|
192 |
|
|
cyg_can_remote_buf rtr_buf2;
|
193 |
|
|
//
|
194 |
|
|
// setup the second remote response buffer for reception of extended
|
195 |
|
|
// remote frames
|
196 |
|
|
//
|
197 |
|
|
rtr_buf2.cfg_id = CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD;
|
198 |
|
|
CYG_CAN_MSG_SET_PARAM(rtr_buf2.msg, 0x800, CYGNUM_CAN_ID_EXT, 4, CYGNUM_CAN_FRAME_DATA);
|
199 |
|
|
CYG_CAN_MSG_SET_DATA(rtr_buf2.msg, 0, 0xCD);
|
200 |
|
|
|
201 |
|
|
len = sizeof(rtr_buf2);
|
202 |
|
|
if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rtr_buf2, &len))
|
203 |
|
|
{
|
204 |
|
|
CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
if (rtr_buf.handle == CYGNUM_CAN_MSGBUF_NA)
|
208 |
|
|
{
|
209 |
|
|
CYG_TEST_FAIL_FINISH("No free message buffer available for /dev/can0");
|
210 |
|
|
}
|
211 |
|
|
#endif
|
212 |
|
|
|
213 |
|
|
diag_printf("\nTest of CAN remote response buffer configuration\n"
|
214 |
|
|
"If a CAN node sends a remote request with ID 0x7FF (std. ID)\n"
|
215 |
|
|
"or 0x800 (ext. ID) then the CAN driver should respond with\n"
|
216 |
|
|
"data frames.\n\n");
|
217 |
|
|
diag_printf("!!! This test can be stopped by sending a data frame\n"
|
218 |
|
|
"with ID 0x100 !!!\n\n");
|
219 |
|
|
|
220 |
|
|
len = sizeof(msgbox_info);
|
221 |
|
|
if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
|
222 |
|
|
{
|
223 |
|
|
CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
|
224 |
|
|
}
|
225 |
|
|
else
|
226 |
|
|
{
|
227 |
|
|
diag_printf("Message boxes available: %d free: %d\n",
|
228 |
|
|
msgbox_info.count, msgbox_info.free);
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
while (1)
|
232 |
|
|
{
|
233 |
|
|
len = sizeof(rx_event);
|
234 |
|
|
|
235 |
|
|
if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
|
236 |
|
|
{
|
237 |
|
|
CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
if (0x100 == rx_event.msg.id)
|
241 |
|
|
{
|
242 |
|
|
CYG_TEST_PASS_FINISH("can_remote test OK");
|
243 |
|
|
}
|
244 |
|
|
else
|
245 |
|
|
{
|
246 |
|
|
print_can_flags(rx_event.flags, "");
|
247 |
|
|
|
248 |
|
|
if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
|
249 |
|
|
{
|
250 |
|
|
print_can_msg(&rx_event.msg, "");
|
251 |
|
|
}
|
252 |
|
|
}
|
253 |
|
|
}
|
254 |
|
|
}
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
void
|
258 |
|
|
cyg_start(void)
|
259 |
|
|
{
|
260 |
|
|
CYG_TEST_INIT();
|
261 |
|
|
|
262 |
|
|
//
|
263 |
|
|
// open CAN device driver
|
264 |
|
|
//
|
265 |
|
|
if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
|
266 |
|
|
{
|
267 |
|
|
CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
//
|
271 |
|
|
// create the thread that accesses the CAN device driver
|
272 |
|
|
//
|
273 |
|
|
cyg_thread_create(4, can0_thread,
|
274 |
|
|
(cyg_addrword_t) 0,
|
275 |
|
|
"can0_thread",
|
276 |
|
|
(void *) can0_thread_data.stack,
|
277 |
|
|
1024 * sizeof(long),
|
278 |
|
|
&can0_thread_data.hdl,
|
279 |
|
|
&can0_thread_data.obj);
|
280 |
|
|
|
281 |
|
|
cyg_thread_resume(can0_thread_data.hdl);
|
282 |
|
|
|
283 |
|
|
cyg_scheduler_start();
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
#else // CYGOPT_IO_CAN_REMOTE_BUF
|
287 |
|
|
#define N_A_MSG "Needs support for CAN remote response buffers"
|
288 |
|
|
#endif
|
289 |
|
|
|
290 |
|
|
#else // CYGOPT_IO_CAN_RUNTIME_MBOX_CFG
|
291 |
|
|
#define N_A_MSG "Needs support for CAN message buffer runtime configuration"
|
292 |
|
|
#endif
|
293 |
|
|
|
294 |
|
|
#else // CYGFUN_KERNEL_API_C
|
295 |
|
|
#define N_A_MSG "Needs kernel C API"
|
296 |
|
|
#endif
|
297 |
|
|
|
298 |
|
|
#else // CYGPKG_IO_CAN && CYGPKG_KERNEL
|
299 |
|
|
#define N_A_MSG "Needs IO/CAN and Kernel"
|
300 |
|
|
#endif
|
301 |
|
|
|
302 |
|
|
#ifdef N_A_MSG
|
303 |
|
|
void
|
304 |
|
|
cyg_start( void )
|
305 |
|
|
{
|
306 |
|
|
CYG_TEST_INIT();
|
307 |
|
|
CYG_TEST_NA( N_A_MSG);
|
308 |
|
|
}
|
309 |
|
|
#endif // N_A_MSG
|
310 |
|
|
|
311 |
|
|
// EOF can_remote.c
|