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: FlexCAN 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>
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;
93 cyg_io_handle_t hDrvFlexCAN;
96 //===========================================================================
98 //===========================================================================
99 #include "can_test_aux.inl" // include CAN test auxiliary functions
102 //===========================================================================
104 //===========================================================================
105 void can0_thread(cyg_addrword_t data)
108 cyg_can_event rx_event;
109 cyg_can_timeout_info_t timeouts;
111 #if defined(CYGOPT_IO_CAN_SUPPORT_TIMEOUTS)
113 // setup large timeout values because we do not need timeouts here
115 timeouts.rx_timeout = 100000;
116 timeouts.tx_timeout = 100000;
118 len = sizeof(timeouts);
119 if (ENOERR != cyg_io_set_config(hDrvFlexCAN, CYG_IO_SET_CONFIG_CAN_TIMEOUT ,&timeouts, &len))
121 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
123 #endif // defined(CYGOPT_IO_CAN_SUPPORT_TIMEOUTS)
126 // This thread simply receives all CAN events and prints the event flags and the
127 // CAN message if it was a TX or RX event. You can use this test in order to check
128 // when a RX overrun occurs
132 len = sizeof(rx_event);
134 if (ENOERR != cyg_io_read(hDrvFlexCAN, &rx_event, &len))
136 CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
140 print_can_flags(rx_event.flags, "");
142 if ((rx_event.flags & CYGNUM_CAN_EVENT_RX) || (rx_event.flags & CYGNUM_CAN_EVENT_TX))
144 print_can_msg(&rx_event.msg, "");
151 //===========================================================================
153 //===========================================================================
154 void can1_thread(cyg_addrword_t data)
158 cyg_can_message tx_msg =
160 0x000, // CAN identifier
161 {0x00, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}, // 8 data bytes
162 CYGNUM_CAN_ID_STD, // standard frame
163 CYGNUM_CAN_FRAME_DATA, // data frame
164 8, // data length code
168 // This thread simply sends CAN messages. It increments the ID for each new CAN messsage
169 // and sends a remote frame after seven data frames. In the first byte of each data frame
170 // the number (0 - 7) of the data frame is stored and the length of the data frame grows
171 // from 1 - 8 data bytes.
173 // The received pattern should look like this way:
175 // ----------------------------------------------
180 // 004 5 04 F1 F2 F3 F4
181 // 005 6 05 F1 F2 F3 F4 F5
182 // 006 7 06 F1 F2 F3 F4 F5 F6
183 // 007 8 Remote Request
192 tx_msg.dlc = (i % 8) + 1;
193 tx_msg.data[0] = (i % 8);
197 // we send a remote frame after 7 data frames
201 tx_msg.rtr = CYGNUM_CAN_FRAME_RTR;
205 tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
208 len = sizeof(tx_msg);
209 if (ENOERR != cyg_io_write(hDrvFlexCAN, &tx_msg, &len))
211 CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
215 print_can_msg(&tx_msg, "TX: ");
218 cyg_thread_delay(100);
228 cyg_can_info_t can_cfg;
233 // open flexcan device driver
235 if (ENOERR != cyg_io_lookup("/dev/can0", &hDrvFlexCAN))
237 CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
241 // setup CAN baudrate 250 KBaud
243 can_cfg.baud = CYGNUM_CAN_KBAUD_250;
244 len = sizeof(can_cfg);
245 if (ENOERR != cyg_io_set_config(hDrvFlexCAN, CYG_IO_SET_CONFIG_CAN_INFO ,&can_cfg, &len))
247 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
251 // create the two threads which access the CAN device driver
252 // a reader thread with a higher priority and a writer thread
253 // with a lower priority
255 cyg_thread_create(4, can0_thread,
258 (void *) can0_thread_data.stack,
260 &can0_thread_data.hdl,
261 &can0_thread_data.obj);
263 cyg_thread_create(5, can1_thread,
264 (cyg_addrword_t) can0_thread_data.hdl,
266 (void *) can1_thread_data.stack,
268 &can1_thread_data.hdl,
269 &can1_thread_data.obj);
271 cyg_thread_resume(can0_thread_data.hdl);
272 cyg_thread_resume(can1_thread_data.hdl);
274 cyg_scheduler_start();
277 #else // CYGFUN_KERNEL_API_C
278 #define N_A_MSG "Needs kernel C API"
281 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
282 #define N_A_MSG "Needs IO/CAN and Kernel"
290 CYG_TEST_NA( N_A_MSG);
294 // EOF flexcan_load.c