1 //==========================================================================
5 // Test CAN device drivers
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>
72 // tx event support required
73 #if defined(CYGOPT_IO_CAN_TX_EVENT_SUPPORT)
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 //===========================================================================
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_can_buf_info_t buf_info;
109 cyg_can_event rx_event;
110 cyg_can_message tx_msg =
112 0x000, // CAN identifier
113 {0x00, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}, // 8 data bytes
114 CYGNUM_CAN_ID_STD, // standard frame
115 CYGNUM_CAN_FRAME_DATA, // data frame
116 0, // data length code
119 if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
121 CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
124 len = sizeof(buf_info);
125 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&buf_info, &len))
127 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
130 if (buf_info.rx_count > 0)
132 CYG_TEST_FAIL_FINISH("Empty RX buffer expected for /dev/can0");
136 // now we send exactly one CAN message more than it is space in left in buffer
137 // Because each TX message will cause a TX event in receive queue we should
138 // get a RX queue overrun
140 diag_printf("Sending %d CAN messages to /dev/can0\n", buf_info.rx_bufsize + 1);
141 for (i = 0; i <= buf_info.rx_bufsize; ++i)
143 tx_msg.id = 0x000 + i;
145 len = sizeof(tx_msg);
147 if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
149 CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
153 print_can_msg(&tx_msg, "");
158 // now check if receive queue is completely filled - that means number of rx events should
159 // be equal to RX queue size
161 if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&buf_info, &len))
163 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
166 if (buf_info.rx_bufsize != buf_info.rx_count)
168 CYG_TEST_FAIL_FINISH("Number of events in /dev/can0 RX queue differs from queue size");
172 // Now read all events from receive queue - if everything is o.k. than the oldest
173 // event should be overwritten by the latest sent message and the last event in
174 // queue should indicate a RX overrun
176 diag_printf("\nReceiving %d CAN messages from /dev/can0\n", buf_info.rx_bufsize);
177 for (i = 0; i < buf_info.rx_bufsize; ++i)
179 len = sizeof(rx_event);
181 if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
183 CYG_TEST_FAIL_FINISH("Error reading from /dev/can1");
188 // if we received a valid TX event then we can print the message
190 if (rx_event.flags & CYGNUM_CAN_EVENT_TX)
192 print_can_msg(&rx_event.msg, "");
193 if (rx_event.msg.data[0] != (i + 1))
195 CYG_TEST_FAIL_FINISH("Received /dev/can0 TX event contains invalid data");
200 CYG_TEST_FAIL_FINISH("Unexpected CAN event for /dev/can0");
204 // now check if any other flag is set
206 if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
208 diag_printf("RX queue overrun successfully indicated for /dev/can0\n");
209 if (i < (buf_info.rx_bufsize - 1))
211 CYG_TEST_FAIL_FINISH("RX queue overrun occured too early /dev/can0");
215 CYG_TEST_PASS_FINISH("can_overrun1 test OK");
217 } // if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
219 } // for (i = 0; i < buf_info.rx_bufsize; ++i)
221 CYG_TEST_FAIL_FINISH("RX overrun expected but not received for /dev/can0");
232 // create the two threads which access the CAN device driver
234 cyg_thread_create(4, can0_thread,
237 (void *) can0_thread_data.stack,
239 &can0_thread_data.hdl,
240 &can0_thread_data.obj);
242 cyg_thread_resume(can0_thread_data.hdl);
244 cyg_scheduler_start();
247 #else // CYGOPT_IO_CAN_TX_EVENT_SUPPORT
248 #define N_A_MSG "TX event support required for IO/CAN"
251 #else // CYGFUN_KERNEL_API_C
252 #define N_A_MSG "Needs kernel C API"
255 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
256 #define N_A_MSG "Needs IO/CAN and Kernel"
264 CYG_TEST_NA( N_A_MSG);