1 //==========================================================================
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 RX/TX test for 2 CAN channels
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>
67 // Package option requirements
68 #if defined(CYGFUN_KERNEL_API_C)
70 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
71 #include <cyg/kernel/kapi.h>
73 // We need two CAN channels
74 #if defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
77 // The same baud rates are required because we send from one channel to the other one
78 #if CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
81 // We need the loop can driver
82 #if defined(CYGPKG_DEVS_CAN_LOOP)
83 #include <pkgconf/devs_can_loop.h>
85 #include "can_test_aux.inl" // include CAN test auxiliary functions
86 //===========================================================================
88 //===========================================================================
89 typedef struct st_thread_data
92 long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
97 //===========================================================================
99 //===========================================================================
100 cyg_thread_entry_t can_tx_thread;
101 thread_data_t can0_thread_data;
102 cyg_thread_entry_t can_rx_thread;
103 thread_data_t can1_thread_data;
104 cyg_io_handle_t hCAN_Tbl[2];
105 cyg_io_handle_t hLoopCAN_Tbl[2];
108 //===========================================================================
110 //===========================================================================
111 void can_rx_thread(cyg_addrword_t data)
114 cyg_can_event rx_event;
115 cyg_can_event loop_rx_event;
116 cyg_uint32 msg_cnt = 0;
119 while (msg_cnt < 0xF0)
123 // First receive CAN event from real CAN hardware
125 len = sizeof(rx_event);
126 if (ENOERR != cyg_io_read(hCAN_Tbl[1], &rx_event, &len))
128 CYG_TEST_FAIL_FINISH("Error reading from channel 1");
131 if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
133 print_can_msg(&rx_event.msg, "RX chan 1:");
134 } // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
137 print_can_flags(rx_event.flags, "");
141 // Now receive CAN event from loop CAN driver
143 len = sizeof(loop_rx_event);
144 if (ENOERR != cyg_io_read(hLoopCAN_Tbl[1], &loop_rx_event, &len))
146 CYG_TEST_FAIL_FINISH("Error reading from loop channel 1");
149 if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
151 print_can_msg(&rx_event.msg, "RX loop 1:");
152 } // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
155 print_can_flags(rx_event.flags, "");
159 // Chaeck message ID and DLC of HW CAN message and CAN message from loop driver
160 // booth should be the same
162 if (rx_event.msg.id != loop_rx_event.msg.id)
164 CYG_TEST_FAIL_FINISH("Received message IDs of hw CAN channel and loop CAN channel are not equal");
167 if (rx_event.msg.dlc != loop_rx_event.msg.dlc)
169 CYG_TEST_FAIL_FINISH("Received DLCs of hw CAN msg and loop CAN msg are not equal");
173 // Now check each data byte of the receive message
175 for (i = 0; i < rx_event.msg.dlc; ++i)
177 if (rx_event.msg.data.bytes[i] != loop_rx_event.msg.data.bytes[i])
179 CYG_TEST_FAIL_FINISH("Data of hw CAN msg and loop CAN msg are not equal");
182 if (rx_event.msg.data.bytes[i] != (i + msg_cnt))
184 CYG_TEST_FAIL_FINISH("CAN message contains unexpected data");
191 CYG_TEST_PASS_FINISH("CAN rx/tx test OK");
195 //===========================================================================
197 //===========================================================================
198 void can_tx_thread(cyg_addrword_t data)
201 cyg_can_message tx_msg;
202 cyg_uint32 msg_cnt = 0;
206 CYG_CAN_MSG_SET_PARAM(tx_msg, 0, CYGNUM_CAN_ID_STD, 0, CYGNUM_CAN_FRAME_DATA);
209 // Prepare CAN message with a known CAN state
211 for (i = 0; i < 8; ++i)
213 tx_msg.data.bytes[i] = i;
216 while (msg_cnt < 0xF0)
219 len = sizeof(tx_msg);
220 if (ENOERR != cyg_io_write(hCAN_Tbl[0], &tx_msg, &len))
222 CYG_TEST_FAIL_FINISH("Error writing to channel 0");
227 len = sizeof(tx_msg);
228 if (ENOERR != cyg_io_write(hLoopCAN_Tbl[0], &tx_msg, &len))
230 CYG_TEST_FAIL_FINISH("Error writing to channel 1");
234 // Increment data in each single data byte
236 for (i = 0; i < 8; ++i)
238 tx_msg.data.bytes[i] += 1;
242 tx_msg.dlc = (tx_msg.dlc + 1) % 9;
243 } // while (msg_cnt < 0x100)
247 //===========================================================================
249 //===========================================================================
255 // open CAN device driver channel 1
257 if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN0_NAME, &hCAN_Tbl[0]))
259 CYG_TEST_FAIL_FINISH("Error opening CAN channel 0");
264 // open CAN device driver channel 2
266 if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN1_NAME, &hCAN_Tbl[1]))
268 CYG_TEST_FAIL_FINISH("Error opening CAN channel 1");
272 // open Loop CAN device driver channel 1
274 if (ENOERR != cyg_io_lookup(CYGDAT_DEVS_CAN_LOOP_CAN0_NAME, &hLoopCAN_Tbl[0]))
276 CYG_TEST_FAIL_FINISH("Error opening loop CAN channel 0");
281 // open Loop CAN device driver channel 2
283 if (ENOERR != cyg_io_lookup(CYGDAT_DEVS_CAN_LOOP_CAN1_NAME, &hLoopCAN_Tbl[1]))
285 CYG_TEST_FAIL_FINISH("Error opening loop CAN channel 1");
293 // create the first thread that access the CAN device driver
295 cyg_thread_create(5, can_tx_thread,
298 (void *) can0_thread_data.stack,
300 &can0_thread_data.hdl,
301 &can0_thread_data.obj);
304 // create the second thread that access the CAN device driver
306 cyg_thread_create(4, can_rx_thread,
309 (void *) can1_thread_data.stack,
311 &can1_thread_data.hdl,
312 &can1_thread_data.obj);
314 cyg_thread_resume(can0_thread_data.hdl);
315 cyg_thread_resume(can1_thread_data.hdl);
317 cyg_scheduler_start();
319 #else // defined(CYGPKG_DEVS_CAN_LOOP)
320 #define N_A_MSG "Needs support for loop CAN device driver"
323 #else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
324 #define N_A_MSG "Baudrate of channel 0 and 1 need to be equal"
327 #else // defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
328 #define N_A_MSG "Needs support for CAN channel 1 and 2"
331 #else // CYGFUN_KERNEL_API_C
332 #define N_A_MSG "Needs kernel C API"
335 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
336 #define N_A_MSG "Needs Kernel"
344 CYG_TEST_NA(N_A_MSG);
348 //---------------------------------------------------------------------------