1 //==========================================================================
5 // CAN remote response buffer 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 load 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_RUNTIME_MBOX_CFG)
75 // Package option requirements
76 #if defined(CYGOPT_IO_CAN_REMOTE_BUF)
79 //===========================================================================
81 //===========================================================================
82 typedef struct st_thread_data
85 long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
90 //===========================================================================
92 //===========================================================================
93 cyg_thread_entry_t can0_thread;
94 thread_data_t can0_thread_data;
95 cyg_io_handle_t hCAN0;
98 //===========================================================================
100 //===========================================================================
101 #include "can_test_aux.inl" // include CAN test auxiliary functions
104 //===========================================================================
106 //===========================================================================
107 void can0_thread(cyg_addrword_t data)
110 cyg_can_event rx_event;
111 cyg_can_remote_buf rtr_buf;
112 cyg_can_filter rx_filter;
113 cyg_can_msgbuf_info msgbox_info;
114 cyg_can_msgbuf_cfg msgbox_cfg;
117 // We would like to setup 2 remote buffers - check if we have enough
118 // free message buffers
120 len = sizeof(msgbox_info);
121 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
123 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
127 diag_printf("\n\n\nMessage boxes available: %d free: %d\n",
128 msgbox_info.count, msgbox_info.free);
132 // We have not enougth free message buffers, so we clear all message buffers now
135 if (msgbox_info.free < 2)
137 msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
138 len = sizeof(msgbox_cfg);
139 if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF, &msgbox_cfg, &len))
141 CYG_TEST_FAIL_FINISH("Error clearing message buffers of /dev/can0");
145 // Now query number of free message boxes again. We need 3 free message boxes.
146 // 2 message boxes for setup of remote response buffers and 1 message box for
147 // setup of receive message box for CAN identifier 0x100
149 len = sizeof(msgbox_info);
150 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
152 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
156 diag_printf("Message boxes available: %d free: %d\n",
157 msgbox_info.count, msgbox_info.free);
160 if (msgbox_info.free < 3)
162 CYG_TEST_FAIL_FINISH("Not enough free message buffers available for /dev/can0");
166 rx_filter.cfg_id = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD;
167 CYG_CAN_MSG_SET_STD_ID(rx_filter.msg, 0x100);
169 len = sizeof(rx_filter);
170 if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rx_filter, &len))
172 CYG_TEST_FAIL_FINISH("Error adding rx filter for CAN ID 0x100 for /dev/can0");
174 } // if (msgbox_info.free < 3)
175 } // if (msgbox_info.free < 2)
176 #ifdef CYGOPT_IO_CAN_STD_CAN_ID
178 // Setup the first remote response buffer for resception of standard
181 rtr_buf.cfg_id = CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD;
182 CYG_CAN_MSG_SET_PARAM(rtr_buf.msg, 0x7FF, CYGNUM_CAN_ID_STD, 1, CYGNUM_CAN_FRAME_DATA);
183 CYG_CAN_MSG_SET_DATA(rtr_buf.msg, 0, 0xAB);
185 len = sizeof(rtr_buf);
186 if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rtr_buf, &len))
188 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
192 #ifdef CYGOPT_IO_CAN_EXT_CAN_ID
193 cyg_can_remote_buf rtr_buf2;
195 // setup the second remote response buffer for reception of extended
198 rtr_buf2.cfg_id = CYGNUM_CAN_MSGBUF_REMOTE_BUF_ADD;
199 CYG_CAN_MSG_SET_PARAM(rtr_buf2.msg, 0x800, CYGNUM_CAN_ID_EXT, 4, CYGNUM_CAN_FRAME_DATA);
200 CYG_CAN_MSG_SET_DATA(rtr_buf2.msg, 0, 0xCD);
202 len = sizeof(rtr_buf2);
203 if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rtr_buf2, &len))
205 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
208 if (rtr_buf.handle == CYGNUM_CAN_MSGBUF_NA)
210 CYG_TEST_FAIL_FINISH("No free message buffer available for /dev/can0");
214 diag_printf("\nTest of CAN remote response buffer configuration\n"
215 "If a CAN node sends a remote request with ID 0x7FF (std. ID)\n"
216 "or 0x800 (ext. ID) then the CAN driver should respond with\n"
218 diag_printf("!!! This test can be stopped by sending a data frame\n"
219 "with ID 0x100 !!!\n\n");
221 len = sizeof(msgbox_info);
222 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
224 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
228 diag_printf("Message boxes available: %d free: %d\n",
229 msgbox_info.count, msgbox_info.free);
234 len = sizeof(rx_event);
236 if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
238 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
241 if (0x100 == rx_event.msg.id)
243 CYG_TEST_PASS_FINISH("can_remote test OK");
247 print_can_flags(rx_event.flags, "");
249 if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
251 print_can_msg(&rx_event.msg, "");
264 // open CAN device driver
266 if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
268 CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
272 // create the thread that accesses the CAN device driver
274 cyg_thread_create(4, can0_thread,
277 (void *) can0_thread_data.stack,
279 &can0_thread_data.hdl,
280 &can0_thread_data.obj);
282 cyg_thread_resume(can0_thread_data.hdl);
284 cyg_scheduler_start();
287 #else // CYGOPT_IO_CAN_REMOTE_BUF
288 #define N_A_MSG "Needs support for CAN remote response buffers"
291 #else // CYGOPT_IO_CAN_RUNTIME_MBOX_CFG
292 #define N_A_MSG "Needs support for CAN message buffer runtime configuration"
295 #else // CYGFUN_KERNEL_API_C
296 #define N_A_MSG "Needs kernel C API"
299 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
300 #define N_A_MSG "Needs IO/CAN and Kernel"
308 CYG_TEST_NA( N_A_MSG);