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 bus load test
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 a large RX buffer
82 #ifdef CYGNUM_DEVS_CAN_LPC2XXX_CAN0_QUEUESIZE_RX_1024
84 #include "can_test_aux.inl" // include CAN test auxiliary functions
85 //===========================================================================
87 //===========================================================================
88 typedef struct st_thread_data
91 long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
96 //===========================================================================
98 //===========================================================================
99 cyg_thread_entry_t can0_thread;
100 thread_data_t can0_thread_data;
101 cyg_io_handle_t hCAN_Tbl[2];
104 //===========================================================================
106 //===========================================================================
107 void can0_thread(cyg_addrword_t data)
110 cyg_can_message tx_msg;
111 cyg_can_event rx_event;
113 cyg_uint32 rx_msg_cnt = 0;
117 // Prepeare message - we use a data length of 0 bytes here. Each received message
118 // causes an iterrupt. The shortest message is a 0 data byte message. This will generate
119 // the highest interrupt rate
121 CYG_CAN_MSG_SET_PARAM(tx_msg, 0, CYGNUM_CAN_ID_STD, 0, CYGNUM_CAN_FRAME_DATA);
124 // Now send 1024 CAN messages as fast as possible to stress the receiver of CAN
127 for (i = 0; i< 1024; ++i)
130 len = sizeof(tx_msg);
131 if (ENOERR != cyg_io_write(hCAN_Tbl[1], &tx_msg, &len))
133 CYG_TEST_FAIL_FINISH("Error writing to channel 0");
138 // Now try to receive all 1024 CAN messages. If all messages are received
139 // and no overrun occured then the message processing is fast enought
143 len = sizeof(rx_event);
145 // First receive CAN event from real CAN hardware
147 len = sizeof(rx_event);
148 if (ENOERR != cyg_io_read(hCAN_Tbl[0], &rx_event, &len))
150 CYG_TEST_FAIL_FINISH("Error reading from channel 1");
153 if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
155 print_can_msg(&rx_event.msg, "RX chan 1:");
157 if (rx_msg_cnt == 1024)
159 CYG_TEST_PASS_FINISH("CAN load test OK");
161 } // if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
164 print_can_flags(rx_event.flags, "");
165 if (rx_event.flags & CYGNUM_CAN_EVENT_OVERRUN_RX)
167 CYG_TEST_FAIL_FINISH("RX overrun for channel 1");
170 if (rx_event.flags & CYGNUM_CAN_EVENT_ERR_PASSIVE)
172 CYG_TEST_FAIL_FINISH("Channel 1 error passive event");
175 if (rx_event.flags & CYGNUM_CAN_EVENT_BUS_OFF)
177 CYG_TEST_FAIL_FINISH("Channel 1 bus off event");
184 //===========================================================================
186 //===========================================================================
192 // open CAN device driver channel 1
194 if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN0_NAME, &hCAN_Tbl[0]))
196 CYG_TEST_FAIL_FINISH("Error opening CAN channel 0");
201 // open CAN device driver channel 2
203 if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN1_NAME, &hCAN_Tbl[1]))
205 CYG_TEST_FAIL_FINISH("Error opening CAN channel 1");
209 // create the main thread
211 cyg_thread_create(5, can0_thread,
214 (void *) can0_thread_data.stack,
216 &can0_thread_data.hdl,
217 &can0_thread_data.obj);
219 cyg_thread_resume(can0_thread_data.hdl);
221 cyg_scheduler_start();
223 #else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_QUEUESIZE_RX_1024
224 #define N_A_MSG "Channel 0 needs RX buffer size for 1024 events"
227 #else // CYGNUM_DEVS_CAN_LPC2XXX_CAN0_KBAUD == CYGNUM_DEVS_CAN_LPC2XXX_CAN1_KBAUD
228 #define N_A_MSG "Baudrate of channel 0 and 1 need to be equal"
231 #else // defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN0) && defined(CYGPKG_DEVS_CAN_LPC2XXX_CAN1)
232 #define N_A_MSG "Needs support for CAN channel 1 and 2"
235 #else // CYGFUN_KERNEL_API_C
236 #define N_A_MSG "Needs kernel C API"
239 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
240 #define N_A_MSG "Needs Kernel"
248 CYG_TEST_NA(N_A_MSG);
252 //---------------------------------------------------------------------------