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
116 {0x00, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 }// 8 data bytes
118 CYGNUM_CAN_ID_STD, // standard frame
119 CYGNUM_CAN_FRAME_DATA, // data frame
120 2, // data length code
123 if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
125 CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
128 len = sizeof(tx_buf_info);
129 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&tx_buf_info, &len))
131 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
135 // Before we can write the CAN messages, we need to know the buffer size of the
136 // receiver. The receiver will tell us this buffer size with one single CAN
139 len = sizeof(rx_event);
141 if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
143 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
147 // we expect a RX event here - we treat any other flag as an error
149 if (!(rx_event.flags & CYGNUM_CAN_EVENT_RX) || (rx_event.flags & !CYGNUM_CAN_EVENT_RX))
151 CYG_TEST_FAIL_FINISH("Unexpected RX event for /dev/can0");
154 rx_bufsize = *((cyg_uint32 *)rx_event.msg.data.bytes);
157 // now we send exactly one CAN message more than there is space in the receive buffer
158 // this should cause an RX ovverun in receive buffer
160 diag_printf("/dev/can0: Sending %d CAN messages\n", rx_bufsize);
161 for (i = 0; i <= rx_bufsize; ++i)
164 // we store the message number as CAN id and in first data byte so
165 // a receiver can check this later
167 CYG_CAN_MSG_SET_STD_ID(tx_msg, 0x000 + i);
168 CYG_CAN_MSG_SET_DATA(tx_msg, 0, i);
169 len = sizeof(tx_msg);
171 if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
173 CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
177 print_can_msg(&tx_msg, "");
179 } // for (i = 0; i <= rx_bufsize; ++i)
181 cyg_thread_suspend(cyg_thread_self());
185 //===========================================================================
187 //===========================================================================
188 void can1_thread(cyg_addrword_t data)
190 cyg_io_handle_t hCAN1;
193 cyg_can_buf_info_t rx_buf_info;
194 cyg_can_event rx_event;
195 cyg_can_message tx_msg;
197 if (ENOERR != cyg_io_lookup("/dev/can1", &hCAN1))
199 CYG_TEST_FAIL_FINISH("Error opening /dev/can1");
202 len = sizeof(rx_buf_info);
203 if (ENOERR != cyg_io_get_config(hCAN1, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&rx_buf_info, &len))
205 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can1");
209 // first we send the size of our receive buffer to the writer
210 // we setup tx message now
213 tx_msg.ext = CYGNUM_CAN_ID_STD;
214 tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
215 tx_msg.dlc = sizeof(rx_buf_info.rx_bufsize);
218 // we store size of rx buffer in CAN message. We do not need to care about
219 // endianess here because this is a loopback driver test and we will receive
222 *((cyg_uint32 *)tx_msg.data.bytes) = rx_buf_info.rx_bufsize;
223 len = sizeof(tx_msg);
226 // as soon as we send a CAN message, thread 0 will resume because it is waiting
229 diag_printf("/dev/can1: Sending size of RX buffer %d\n", rx_buf_info.rx_bufsize);
230 if (ENOERR != cyg_io_write(hCAN1, &tx_msg, &len))
232 CYG_TEST_FAIL_FINISH("Error writing to /dev/can1");
234 cyg_thread_delay(10); // let thread 0 run
237 // now we check if we received CAN messages - if receive buffer is not full
238 // the we have an error here because we expect a full receive buffer
240 len = sizeof(rx_buf_info);
241 if (ENOERR != cyg_io_get_config(hCAN1, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&rx_buf_info, &len))
243 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can1");
246 if (rx_buf_info.rx_bufsize != rx_buf_info.rx_count)
248 CYG_TEST_FAIL_FINISH("RX buffer of /dev/can1 does not contain number of expected messages");
252 // now we wait for messages from /dev/can0
254 diag_printf("/dev/can1: Receiving %d CAN messages\n", rx_buf_info.rx_count);
255 for (i = 0; i < rx_buf_info.rx_count; ++i)
257 len = sizeof(rx_event);
258 if (ENOERR != cyg_io_read(hCAN1, &rx_event, &len))
260 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
264 if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
266 print_can_msg(&rx_event.msg, "");
267 if (rx_event.msg.data.bytes[0] != (i + 1))
269 CYG_TEST_FAIL_FINISH("Received /dev/can1 RX event contains invalid data");
274 CYG_TEST_FAIL_FINISH("Unexpected CAN event for /dev/can1");
278 // now check if any other flag is set
280 if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
282 diag_printf("RX queue overrun successfully indicated for /dev/can1\n");
285 // if TX events are supported then we have already a TX event in receive queue because
286 // we sent a message and the RX queue overrun will occur one message earlier
288 #if defined(CYGOPT_IO_CAN_TX_EVENT_SUPPORT)
289 if (i < (rx_buf_info.rx_bufsize - 2))
291 if (i < (rx_buf_info.rx_bufsize - 1))
294 CYG_TEST_FAIL_FINISH("RX queue overrun occured too early for /dev/can1");
298 CYG_TEST_PASS_FINISH("can_overrun2 test OK");
300 } // if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
314 // create the two threads which access the CAN device driver
316 cyg_thread_create(4, can0_thread,
319 (void *) can0_thread_data.stack,
321 &can0_thread_data.hdl,
322 &can0_thread_data.obj);
324 cyg_thread_create(5, can1_thread,
325 (cyg_addrword_t) can0_thread_data.hdl,
327 (void *) can1_thread_data.stack,
329 &can1_thread_data.hdl,
330 &can1_thread_data.obj);
332 cyg_thread_resume(can0_thread_data.hdl);
333 cyg_thread_resume(can1_thread_data.hdl);
335 cyg_scheduler_start();
338 #else // CYGFUN_KERNEL_API_C
339 #define N_A_MSG "Needs kernel C API"
342 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
343 #define N_A_MSG "Needs IO/CAN and Kernel"
351 CYG_TEST_NA( N_A_MSG);