1 //==========================================================================
5 // CAN driver test of callback on event
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, Alexey Shusharin
44 // Contributors: Uwe Kindler, Alexey Shusharin
46 // Description: CAN driver test of callback on event
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 // Package option requirements
73 #if defined(CYGOPT_IO_CAN_SUPPORT_CALLBACK)
75 //===========================================================================
77 //===========================================================================
78 typedef struct st_thread_data
81 long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
86 //===========================================================================
88 //===========================================================================
89 cyg_thread_entry_t can_thread;
90 thread_data_t can_thread_data;
95 cyg_io_handle_t hCAN0;
96 cyg_io_handle_t hCAN1;
98 //===========================================================================
100 //===========================================================================
101 #include "can_test_aux.inl" // include CAN test auxiliary functions
104 //===========================================================================
106 //===========================================================================
108 static void callback_func(cyg_uint16 flags, CYG_ADDRWORD data)
110 if (data == ((CYG_ADDRWORD) hCAN0) && (flags & CYGNUM_CAN_EVENT_RX))
113 cyg_cond_signal(&can_wait);
117 //===========================================================================
119 //===========================================================================
120 void can_thread(cyg_addrword_t data)
123 cyg_can_callback_cfg callback_cfg;
124 cyg_can_message tx_msg;
128 // open CAN0 device driver
130 if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
132 CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
136 // open CAN1 device driver
138 if (ENOERR != cyg_io_lookup("/dev/can1", &hCAN1))
140 CYG_TEST_FAIL_FINISH("Error opening /dev/can1");
144 // configure CAN0 callback
146 len = sizeof(callback_cfg);
147 callback_cfg.flag_mask = 0xFFFF;
148 callback_cfg.data = (CYG_ADDRWORD) hCAN0;
149 callback_cfg.callback_func = callback_func;
151 if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_CALLBACK,
152 &callback_cfg, &len))
154 CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
158 // transmit message from CAN1 to CAN0
161 tx_msg.ext = CYGNUM_CAN_ID_STD;
162 tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
164 len = sizeof(tx_msg);
166 if (ENOERR != cyg_io_write(hCAN1, &tx_msg, &len))
168 CYG_TEST_FAIL_FINISH("Error writing message to /dev/can1");
172 // Wait CAN0 callback
174 cyg_mutex_lock(&can_lock);
176 wait_res = cyg_cond_timed_wait(&can_wait, 100);
178 cyg_mutex_unlock(&can_lock);
181 // If result of wait is a signal operation, test is successed
182 // If timeout - test is failed, because callback_func() hasn't been
183 // called with correct parameters
187 CYG_TEST_PASS_FINISH("can_callback test OK");
191 CYG_TEST_FAIL_FINISH("can_callback test FAILED");
201 cyg_mutex_init(&can_lock);
202 cyg_cond_init(&can_wait, &can_lock);
205 // create the main thread
207 cyg_thread_create(4, can_thread,
210 (void *) can_thread_data.stack,
212 &can_thread_data.hdl,
213 &can_thread_data.obj);
215 cyg_thread_resume(can_thread_data.hdl);
217 cyg_scheduler_start();
220 #else // #if defined(CYGOPT_IO_CAN_SUPPORT_CALLBACK)
221 #define N_A_MSG "Needs callback support"
224 #else // CYGFUN_KERNEL_API_C
225 #define N_A_MSG "Needs kernel C API"
228 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
229 #define N_A_MSG "Needs IO/CAN and Kernel"
237 CYG_TEST_NA( N_A_MSG);
241 // EOF can_callback.c