1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// can_filter.c
|
4 |
|
|
//
|
5 |
|
|
// CAN message filter 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-03-21
|
45 |
|
|
// Description: CAN hardware filter 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_STD_CAN_ID)
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
//===========================================================================
|
76 |
|
|
// DATA TYPES
|
77 |
|
|
//===========================================================================
|
78 |
|
|
typedef struct st_thread_data
|
79 |
|
|
{
|
80 |
|
|
cyg_thread obj;
|
81 |
|
|
long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
|
82 |
|
|
cyg_handle_t hdl;
|
83 |
|
|
} thread_data_t;
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
//===========================================================================
|
87 |
|
|
// LOCAL DATA
|
88 |
|
|
//===========================================================================
|
89 |
|
|
cyg_thread_entry_t can0_thread;
|
90 |
|
|
thread_data_t can0_thread_data;
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
cyg_io_handle_t hCAN0;
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
//===========================================================================
|
97 |
|
|
// LOCAL FUNCTIONS
|
98 |
|
|
//===========================================================================
|
99 |
|
|
#include "can_test_aux.inl" // include CAN test auxiliary functions
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
//===========================================================================
|
103 |
|
|
// Main thread
|
104 |
|
|
//===========================================================================
|
105 |
|
|
void can0_thread(cyg_addrword_t data)
|
106 |
|
|
{
|
107 |
|
|
cyg_uint32 len;
|
108 |
|
|
cyg_can_event rx_event;
|
109 |
|
|
cyg_uint16 i;
|
110 |
|
|
cyg_can_hdi hdi;
|
111 |
|
|
cyg_can_msgbuf_info msgbox_info;
|
112 |
|
|
cyg_can_msgbuf_cfg msgbox_cfg;
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
len = sizeof(hdi);
|
116 |
|
|
if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_HDI ,&hdi, &len))
|
117 |
|
|
{
|
118 |
|
|
CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
//
|
122 |
|
|
// Normally the CAN modul should support message filters. So the
|
123 |
|
|
// FULLCAN flag should be set - if it is not, we treat this as an error
|
124 |
|
|
//
|
125 |
|
|
if (!(hdi.support_flags & CYGNUM_CAN_HDI_FULLCAN))
|
126 |
|
|
{
|
127 |
|
|
CYG_TEST_FAIL_FINISH("/dev/can0 does not support message buffers");
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
//
|
132 |
|
|
// Now reset message buffer configuration - this is mandatory bevore starting
|
133 |
|
|
// message buffer runtime configuration
|
134 |
|
|
//
|
135 |
|
|
msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
|
136 |
|
|
len = sizeof(msgbox_cfg);
|
137 |
|
|
if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&msgbox_cfg, &len))
|
138 |
|
|
{
|
139 |
|
|
CYG_TEST_FAIL_FINISH("Error resetting message buffer configuration of /dev/can0");
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
//
|
143 |
|
|
// Now query number of available and free message boxes
|
144 |
|
|
//
|
145 |
|
|
len = sizeof(msgbox_info);
|
146 |
|
|
if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
|
147 |
|
|
{
|
148 |
|
|
CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
//
|
152 |
|
|
// if there are no free message boxes available then this is a failure
|
153 |
|
|
//
|
154 |
|
|
if (!msgbox_info.free)
|
155 |
|
|
{
|
156 |
|
|
CYG_TEST_FAIL_FINISH("No free message boxes available for /dev/can0");
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
//
|
160 |
|
|
// We setup as many standard CAN message filters as there are free
|
161 |
|
|
// message buffers available.
|
162 |
|
|
//
|
163 |
|
|
for (i = 0; i < msgbox_info.free; ++i)
|
164 |
|
|
{
|
165 |
|
|
cyg_can_filter rx_filter;
|
166 |
|
|
|
167 |
|
|
rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD;
|
168 |
|
|
rx_filter.msg.id = i;
|
169 |
|
|
rx_filter.msg.ext = CYGNUM_CAN_ID_STD;
|
170 |
|
|
|
171 |
|
|
len = sizeof(rx_filter);
|
172 |
|
|
if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rx_filter, &len))
|
173 |
|
|
{
|
174 |
|
|
CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
|
175 |
|
|
}
|
176 |
|
|
else if (CYGNUM_CAN_MSGBUF_NA == rx_filter.handle)
|
177 |
|
|
{
|
178 |
|
|
CYG_TEST_FAIL_FINISH("Error setting up message filter for /dev/can0");
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
diag_printf("\n\nNow try to send CAN messages. The device should only\n"
|
184 |
|
|
"receive messages identifiers in the range of 0x00 to 0x%X.\n"
|
185 |
|
|
"As soon as a standard message with ID 0x000 arrives, all\n"
|
186 |
|
|
"message filters will be cleared\n\n", (msgbox_info.free - 1));
|
187 |
|
|
|
188 |
|
|
//
|
189 |
|
|
// Now receive messages until a message arrives with largest ID of all
|
190 |
|
|
// available message filters
|
191 |
|
|
//
|
192 |
|
|
rx_event.msg.id = 1;
|
193 |
|
|
while(rx_event.msg.id != 0)
|
194 |
|
|
{
|
195 |
|
|
len = sizeof(rx_event);
|
196 |
|
|
|
197 |
|
|
if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
|
198 |
|
|
{
|
199 |
|
|
CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
|
200 |
|
|
}
|
201 |
|
|
else if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
|
202 |
|
|
{
|
203 |
|
|
print_can_msg(&rx_event.msg, "");
|
204 |
|
|
} // if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
|
205 |
|
|
else
|
206 |
|
|
{
|
207 |
|
|
print_can_flags(rx_event.flags, "");
|
208 |
|
|
rx_event.msg.id = 1;
|
209 |
|
|
}
|
210 |
|
|
} // while(1)
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
//
|
214 |
|
|
// Now enable reception of all available CAN messages
|
215 |
|
|
//
|
216 |
|
|
cyg_can_filter rx_filter;
|
217 |
|
|
rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ALL;
|
218 |
|
|
len = sizeof(rx_filter);
|
219 |
|
|
if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF , &rx_filter, &len))
|
220 |
|
|
{
|
221 |
|
|
CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
|
225 |
|
|
diag_printf("\n\nAll message filters have been cleared an now the device\n"
|
226 |
|
|
"will receive any available CAN message identifiers.\n"
|
227 |
|
|
"Send a CAN message with ID 0x100 to stop this test.\n\n");
|
228 |
|
|
|
229 |
|
|
//
|
230 |
|
|
// Now receive messages until a message arrives with largest ID of all
|
231 |
|
|
// available message filters
|
232 |
|
|
//
|
233 |
|
|
rx_event.msg.id = 1;
|
234 |
|
|
while(rx_event.msg.id != 0x100)
|
235 |
|
|
{
|
236 |
|
|
len = sizeof(rx_event);
|
237 |
|
|
|
238 |
|
|
if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
|
239 |
|
|
{
|
240 |
|
|
CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
|
241 |
|
|
}
|
242 |
|
|
else if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
|
243 |
|
|
{
|
244 |
|
|
print_can_msg(&rx_event.msg, "");
|
245 |
|
|
} // if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
|
246 |
|
|
} // while(1)
|
247 |
|
|
|
248 |
|
|
CYG_TEST_PASS_FINISH("can_filter test OK");
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
void
|
253 |
|
|
cyg_start(void)
|
254 |
|
|
{
|
255 |
|
|
CYG_TEST_INIT();
|
256 |
|
|
|
257 |
|
|
//
|
258 |
|
|
// open CAN device driver
|
259 |
|
|
//
|
260 |
|
|
if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
|
261 |
|
|
{
|
262 |
|
|
CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
//
|
267 |
|
|
// create the two threads which access the CAN device driver
|
268 |
|
|
// a reader thread with a higher priority and a writer thread
|
269 |
|
|
// with a lower priority
|
270 |
|
|
//
|
271 |
|
|
cyg_thread_create(4, can0_thread,
|
272 |
|
|
(cyg_addrword_t) 0,
|
273 |
|
|
"can0_thread",
|
274 |
|
|
(void *) can0_thread_data.stack,
|
275 |
|
|
1024 * sizeof(long),
|
276 |
|
|
&can0_thread_data.hdl,
|
277 |
|
|
&can0_thread_data.obj);
|
278 |
|
|
|
279 |
|
|
cyg_thread_resume(can0_thread_data.hdl);
|
280 |
|
|
|
281 |
|
|
cyg_scheduler_start();
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
#else // CYGOPT_IO_CAN_STD_CAN_ID
|
285 |
|
|
#define N_A_MSG "Needs support for standard CAN identifiers"
|
286 |
|
|
#endif
|
287 |
|
|
|
288 |
|
|
#else // CYGFUN_KERNEL_API_C
|
289 |
|
|
#define N_A_MSG "Needs kernel C API"
|
290 |
|
|
#endif
|
291 |
|
|
|
292 |
|
|
#else // CYGPKG_IO_CAN && CYGPKG_KERNEL
|
293 |
|
|
#define N_A_MSG "Needs IO/CAN and Kernel"
|
294 |
|
|
#endif
|
295 |
|
|
|
296 |
|
|
#ifdef N_A_MSG
|
297 |
|
|
void
|
298 |
|
|
cyg_start( void )
|
299 |
|
|
{
|
300 |
|
|
CYG_TEST_INIT();
|
301 |
|
|
CYG_TEST_NA( N_A_MSG);
|
302 |
|
|
}
|
303 |
|
|
#endif // N_A_MSG
|
304 |
|
|
|
305 |
|
|
//---------------------------------------------------------------------------
|
306 |
|
|
// EOF can_filter.c
|