1 //==========================================================================
5 // CAN message filter test
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
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 version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): Uwe Kindler
44 // Contributors: Uwe Kindler
46 // Description: CAN hardware filter test
47 //####DESCRIPTIONEND####
50 //===========================================================================
52 //===========================================================================
53 #include <pkgconf/system.h>
55 #include <cyg/infra/testcase.h> // test macros
56 #include <cyg/infra/cyg_ass.h> // assertion macros
57 #include <cyg/infra/diag.h>
59 // Package requirements
60 #if defined(CYGPKG_IO_CAN) && defined(CYGPKG_KERNEL)
62 #include <pkgconf/kernel.h>
63 #include <cyg/io/io.h>
64 #include <cyg/io/canio.h>
66 // Package option requirements
67 #if defined(CYGFUN_KERNEL_API_C)
69 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
70 #include <cyg/kernel/kapi.h>
72 // Package option requirements
73 #if defined (CYGOPT_IO_CAN_STD_CAN_ID)
76 //===========================================================================
78 //===========================================================================
79 typedef struct st_thread_data
82 long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
87 //===========================================================================
89 //===========================================================================
90 cyg_thread_entry_t can0_thread;
91 thread_data_t can0_thread_data;
94 cyg_io_handle_t hCAN0;
97 //===========================================================================
99 //===========================================================================
100 #include "can_test_aux.inl" // include CAN test auxiliary functions
103 //===========================================================================
105 //===========================================================================
106 void can0_thread(cyg_addrword_t data)
109 cyg_can_event rx_event;
112 cyg_can_msgbuf_info msgbox_info;
113 cyg_can_msgbuf_cfg msgbox_cfg;
117 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_HDI ,&hdi, &len))
119 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
123 // Normally the CAN modul should support message filters. So the
124 // FULLCAN flag should be set - if it is not, we treat this as an error
126 if (!(hdi.support_flags & CYGNUM_CAN_HDI_FULLCAN))
128 CYG_TEST_FAIL_FINISH("/dev/can0 does not support message buffers");
133 // Now reset message buffer configuration - this is mandatory bevore starting
134 // message buffer runtime configuration
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))
140 CYG_TEST_FAIL_FINISH("Error resetting message buffer configuration of /dev/can0");
144 // Now query number of available and free message boxes
146 len = sizeof(msgbox_info);
147 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
149 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
153 // if there are no free message boxes available then this is a failure
155 if (!msgbox_info.free)
157 CYG_TEST_FAIL_FINISH("No free message boxes available for /dev/can0");
161 // We setup as many standard CAN message filters as there are free
162 // message buffers available.
164 for (i = 0; i < msgbox_info.free; ++i)
166 cyg_can_filter rx_filter;
168 rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD;
169 rx_filter.msg.id = i;
170 rx_filter.msg.ext = CYGNUM_CAN_ID_STD;
172 len = sizeof(rx_filter);
173 if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rx_filter, &len))
175 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
177 else if (CYGNUM_CAN_MSGBUF_NA == rx_filter.handle)
179 CYG_TEST_FAIL_FINISH("Error setting up message filter for /dev/can0");
184 diag_printf("\n\nNow try to send CAN messages. The device should only\n"
185 "receive messages identifiers in the range of 0x00 to 0x%X.\n"
186 "As soon as a standard message with ID 0x000 arrives, all\n"
187 "message filters will be cleared\n\n", (msgbox_info.free - 1));
190 // Now receive messages until a message arrives with largest ID of all
191 // available message filters
194 while(rx_event.msg.id != 0)
196 len = sizeof(rx_event);
198 if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
200 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
202 else if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
204 print_can_msg(&rx_event.msg, "");
205 } // if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
208 print_can_flags(rx_event.flags, "");
215 // Now enable reception of all available CAN messages
217 cyg_can_filter rx_filter;
218 rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ALL;
219 len = sizeof(rx_filter);
220 if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF , &rx_filter, &len))
222 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
226 diag_printf("\n\nAll message filters have been cleared an now the device\n"
227 "will receive any available CAN message identifiers.\n"
228 "Send a CAN message with ID 0x100 to stop this test.\n\n");
231 // Now receive messages until a message arrives with largest ID of all
232 // available message filters
235 while(rx_event.msg.id != 0x100)
237 len = sizeof(rx_event);
239 if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
241 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
243 else if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
245 print_can_msg(&rx_event.msg, "");
246 } // if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
249 CYG_TEST_PASS_FINISH("can_filter test OK");
259 // open CAN device driver
261 if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
263 CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
268 // create the two threads which access the CAN device driver
269 // a reader thread with a higher priority and a writer thread
270 // with a lower priority
272 cyg_thread_create(4, can0_thread,
275 (void *) can0_thread_data.stack,
277 &can0_thread_data.hdl,
278 &can0_thread_data.obj);
280 cyg_thread_resume(can0_thread_data.hdl);
282 cyg_scheduler_start();
285 #else // CYGOPT_IO_CAN_STD_CAN_ID
286 #define N_A_MSG "Needs support for standard CAN identifiers"
289 #else // CYGFUN_KERNEL_API_C
290 #define N_A_MSG "Needs kernel C API"
293 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
294 #define N_A_MSG "Needs IO/CAN and Kernel"
302 CYG_TEST_NA( N_A_MSG);
306 //---------------------------------------------------------------------------