1 //==========================================================================
5 // Test CAN device RX overrun events
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: Simple read/write test of CAN driver
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>
73 //===========================================================================
75 //===========================================================================
76 typedef struct st_thread_data
79 long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
84 //===========================================================================
86 //===========================================================================
87 cyg_thread_entry_t can0_thread;
88 thread_data_t can0_thread_data;
90 cyg_thread_entry_t can1_thread;
91 thread_data_t can1_thread_data;
94 //===========================================================================
96 //===========================================================================
97 #include "can_test_aux.inl" // include CAN test auxiliary functions
100 //===========================================================================
102 //===========================================================================
103 void can0_thread(cyg_addrword_t data)
105 cyg_io_handle_t hCAN0;
108 cyg_uint32 rx_bufsize;
109 cyg_can_buf_info_t tx_buf_info;
110 cyg_can_event rx_event;
111 cyg_can_message tx_msg =
113 0x000, // CAN identifier
114 {0x00, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}, // 8 data bytes
115 CYGNUM_CAN_ID_STD, // standard frame
116 CYGNUM_CAN_FRAME_DATA, // data frame
117 2, // data length code
120 if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
122 CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
125 len = sizeof(tx_buf_info);
126 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&tx_buf_info, &len))
128 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
132 // Before we can write the CAN messages, we need to know the buffer size of the
133 // receiver. The receiver will tell us this buffer size with one single CAN
136 len = sizeof(rx_event);
138 if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
140 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
144 // we expect a RX event here - we treat any other flag as an error
146 if (!(rx_event.flags & CYGNUM_CAN_EVENT_RX) || (rx_event.flags & !CYGNUM_CAN_EVENT_RX))
148 CYG_TEST_FAIL_FINISH("Unexpected RX event for /dev/can0");
151 rx_bufsize = *((cyg_uint32 *)rx_event.msg.data);
154 // now we send exactly one CAN message more than there is space in the receive buffer
155 // this should cause an RX ovverun in receive buffer
157 diag_printf("/dev/can0: Sending %d CAN messages\n", rx_bufsize);
158 for (i = 0; i <= rx_bufsize; ++i)
161 // we store the message number as CAN id and in first data byte so
162 // a receiver can check this later
164 tx_msg.id = 0x000 + i;
166 len = sizeof(tx_msg);
168 if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
170 CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
174 print_can_msg(&tx_msg, "");
176 } // for (i = 0; i <= rx_bufsize; ++i)
178 cyg_thread_suspend(cyg_thread_self());
182 //===========================================================================
184 //===========================================================================
185 void can1_thread(cyg_addrword_t data)
187 cyg_io_handle_t hCAN1;
190 cyg_can_buf_info_t rx_buf_info;
191 cyg_can_event rx_event;
192 cyg_can_message tx_msg;
194 if (ENOERR != cyg_io_lookup("/dev/can1", &hCAN1))
196 CYG_TEST_FAIL_FINISH("Error opening /dev/can1");
199 len = sizeof(rx_buf_info);
200 if (ENOERR != cyg_io_get_config(hCAN1, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&rx_buf_info, &len))
202 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can1");
206 // first we send the size of our receive buffer to the writer
207 // we setup tx message now
210 tx_msg.ext = CYGNUM_CAN_ID_STD;
211 tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
212 tx_msg.dlc = sizeof(rx_buf_info.rx_bufsize);
215 // we store size of rx buffer in CAN message. We do not need to care about
216 // endianess here because this is a loopback driver test and we will receive
219 *((cyg_uint32 *)tx_msg.data) = rx_buf_info.rx_bufsize;
220 len = sizeof(tx_msg);
223 // as soon as we send a CAN message, thread 0 will resume because it is waiting
226 diag_printf("/dev/can1: Sending size of RX buffer %d\n", rx_buf_info.rx_bufsize);
227 if (ENOERR != cyg_io_write(hCAN1, &tx_msg, &len))
229 CYG_TEST_FAIL_FINISH("Error writing to /dev/can1");
231 cyg_thread_delay(10); // let thread 0 run
234 // now we check if we received CAN messages - if receive buffer is not full
235 // the we have an error here because we expect a full receive buffer
237 len = sizeof(rx_buf_info);
238 if (ENOERR != cyg_io_get_config(hCAN1, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&rx_buf_info, &len))
240 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can1");
243 if (rx_buf_info.rx_bufsize != rx_buf_info.rx_count)
245 CYG_TEST_FAIL_FINISH("RX buffer of /dev/can1 does not contain number of expected messages");
249 // now we wait for messages from /dev/can0
251 diag_printf("/dev/can1: Receiving %d CAN messages\n", rx_buf_info.rx_count);
252 for (i = 0; i < rx_buf_info.rx_count; ++i)
254 len = sizeof(rx_event);
255 if (ENOERR != cyg_io_read(hCAN1, &rx_event, &len))
257 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
261 if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
263 print_can_msg(&rx_event.msg, "");
264 if (rx_event.msg.data[0] != (i + 1))
266 CYG_TEST_FAIL_FINISH("Received /dev/can1 RX event contains invalid data");
271 CYG_TEST_FAIL_FINISH("Unexpected CAN event for /dev/can1");
275 // now check if any other flag is set
277 if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
279 diag_printf("RX queue overrun successfully indicated for /dev/can1\n");
282 // if TX events are supported then we have already a TX event in receive queue because
283 // we sent a message and the RX queue overrun will occur one message earlier
285 #if defined(CYGOPT_IO_CAN_TX_EVENT_SUPPORT)
286 if (i < (rx_buf_info.rx_bufsize - 2))
288 if (i < (rx_buf_info.rx_bufsize - 1))
291 CYG_TEST_FAIL_FINISH("RX queue overrun occured too early for /dev/can1");
295 CYG_TEST_PASS_FINISH("can_overrun2 test OK");
297 } // if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
311 // create the two threads which access the CAN device driver
313 cyg_thread_create(4, can0_thread,
316 (void *) can0_thread_data.stack,
318 &can0_thread_data.hdl,
319 &can0_thread_data.obj);
321 cyg_thread_create(5, can1_thread,
322 (cyg_addrword_t) can0_thread_data.hdl,
324 (void *) can1_thread_data.stack,
326 &can1_thread_data.hdl,
327 &can1_thread_data.obj);
329 cyg_thread_resume(can0_thread_data.hdl);
330 cyg_thread_resume(can1_thread_data.hdl);
332 cyg_scheduler_start();
335 #else // CYGFUN_KERNEL_API_C
336 #define N_A_MSG "Needs kernel C API"
339 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
340 #define N_A_MSG "Needs IO/CAN and Kernel"
348 CYG_TEST_NA( N_A_MSG);