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>
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;
96 //===========================================================================
98 //===========================================================================
99 #include "can_test_aux.inl" // include CAN test auxiliary functions
102 //===========================================================================
104 //===========================================================================
105 void can0_thread(cyg_addrword_t data)
107 cyg_io_handle_t hCAN0;
110 cyg_can_buf_info_t tx_buf_info;
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 8, // 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");
134 if (tx_buf_info.tx_bufsize < 10)
136 CYG_TEST_FAIL_FINISH("TX quesize of /dev/can0 too small for 10 CAN messages");
142 // now we simply send 10 CAN messages and then suspend the thread
144 CYG_TEST_INFO("Thread0: Writing 10 CAN messages");
145 for (i = 0; i < 10; ++i)
148 // we store the message number as CAN id and in first data byte so
149 // a receiver can check this later
151 CYG_CAN_MSG_SET_STD_ID(tx_msg, 0x000 + i);
152 CYG_CAN_MSG_SET_DATA(tx_msg, 0, i);
153 len = sizeof(tx_msg);
155 if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
157 CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
161 print_can_msg(&tx_msg, "");
163 } // for (i = 0; i < 10; ++i)
166 // Give reader thread 200 ticks time for readung all messages. The reader thread
167 // signals the semaphore if it received all transmitted messages
169 if (!cyg_semaphore_timed_wait( &sem_wait, cyg_current_time( ) + 200 ))
171 CYG_TEST_FAIL_FINISH("Waiting for reader thread timed out.");
175 CYG_TEST_PASS_FINISH("can_rdwr test OK");
181 //===========================================================================
183 //===========================================================================
184 void can1_thread(cyg_addrword_t data)
186 cyg_io_handle_t hCAN1;
189 cyg_can_buf_info_t rx_buf_info;
190 cyg_can_event rx_event;
192 if (ENOERR != cyg_io_lookup("/dev/can1", &hCAN1))
194 CYG_TEST_FAIL_FINISH("Error opening /dev/can1");
197 len = sizeof(rx_buf_info);
198 if (ENOERR != cyg_io_get_config(hCAN1, CYG_IO_GET_CONFIG_CAN_BUFFER_INFO ,&rx_buf_info, &len))
200 CYG_TEST_FAIL_FINISH("Error reading config of /dev/can1");
203 if (rx_buf_info.rx_bufsize < 10)
205 CYG_TEST_FAIL_FINISH("RX quesize of /dev/can1 too small for 10 CAN events");
211 // now we try to read the 10 CAN messages that was sent by writer
214 CYG_TEST_INFO("Thread1: Reading 10 CAN messages");
215 for (i = 0; i < 10; ++i)
217 len = sizeof(rx_event);
219 if (ENOERR != cyg_io_read(hCAN1, &rx_event, &len))
221 CYG_TEST_FAIL_FINISH("Error reading from /dev/can1");
225 // we expect only RX events in this test case - so all other events
228 if (!(rx_event.flags & CYGNUM_CAN_EVENT_RX))
230 CYG_TEST_FAIL_FINISH("Received wrong CAN event type");
234 // The writer thread stored the message number in CAN id and first
235 // data byte so we can check now if we received valid data
237 if ((rx_event.msg.id != i) || (rx_event.msg.data.bytes[0] != i))
239 CYG_TEST_FAIL_FINISH("Received CAN message contains unexpected data");
243 print_can_msg(&rx_event.msg, "");
245 } //for (i = 0; i < 10; ++i)
248 // signal successfull reception of all messages
250 cyg_semaphore_post(&sem_wait);
262 // Initialize the wait semaphore to 0
264 cyg_semaphore_init( &sem_wait, 0 );
267 // create the two threads which access the CAN device driver
269 cyg_thread_create(4, can0_thread,
272 (void *) can0_thread_data.stack,
274 &can0_thread_data.hdl,
275 &can0_thread_data.obj);
277 cyg_thread_create(5, can1_thread,
278 (cyg_addrword_t) can0_thread_data.hdl,
280 (void *) can1_thread_data.stack,
282 &can1_thread_data.hdl,
283 &can1_thread_data.obj);
285 cyg_thread_resume(can0_thread_data.hdl);
286 cyg_thread_resume(can1_thread_data.hdl);
288 cyg_scheduler_start();
291 #else // CYGFUN_KERNEL_API_C
292 #define N_A_MSG "Needs kernel C API"
295 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
296 #define N_A_MSG "Needs IO/CAN and Kernel"
304 CYG_TEST_NA( N_A_MSG);