]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/can/loop/v2_0/tests/can_callback.c
Initial revision
[karo-tx-redboot.git] / packages / devs / can / loop / v2_0 / tests / can_callback.c
1 //==========================================================================
2 //
3 //        can_callback.c
4 //
5 //        CAN driver test of callback on event
6 //
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.
12 //
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.
16 //
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
20 // for more details.
21 //
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.
25 //
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.
32 //
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.
35 //
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####
42 //
43 // Author(s):     Uwe Kindler, Alexey Shusharin
44 // Contributors:  Uwe Kindler, Alexey Shusharin
45 // Date:          2007-08-23
46 // Description:   CAN driver test of callback on event
47 //####DESCRIPTIONEND####
48
49
50 //===========================================================================
51 //                                INCLUDES
52 //===========================================================================
53 #include <pkgconf/system.h>
54
55 #include <cyg/infra/testcase.h>         // test macros
56 #include <cyg/infra/cyg_ass.h>          // assertion macros
57 #include <cyg/infra/diag.h>
58
59 // Package requirements
60 #if defined(CYGPKG_IO_CAN) && defined(CYGPKG_KERNEL)
61
62 #include <pkgconf/kernel.h>
63 #include <cyg/io/io.h>
64 #include <cyg/io/canio.h>
65
66 // Package option requirements
67 #if defined(CYGFUN_KERNEL_API_C)
68
69 #include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
70 #include <cyg/kernel/kapi.h>
71
72 // Package option requirements
73 #if defined(CYGOPT_IO_CAN_SUPPORT_CALLBACK)
74
75 //===========================================================================
76 //                               DATA TYPES
77 //===========================================================================
78 typedef struct st_thread_data
79 {
80     cyg_thread   obj;
81     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
82     cyg_handle_t hdl;
83 } thread_data_t;
84
85
86 //===========================================================================
87 //                              LOCAL DATA
88 //===========================================================================
89 cyg_thread_entry_t can_thread;
90 thread_data_t      can_thread_data;
91
92 cyg_mutex_t can_lock;
93 cyg_cond_t can_wait;
94
95 cyg_io_handle_t    hCAN0;
96 cyg_io_handle_t    hCAN1;
97
98 //===========================================================================
99 //                          LOCAL FUNCTIONS
100 //===========================================================================
101 #include "can_test_aux.inl" // include CAN test auxiliary functions
102
103
104 //===========================================================================
105 //                           CALLBACK FUNCTION
106 //===========================================================================
107
108 static void callback_func(cyg_uint16 flags, CYG_ADDRWORD data)
109 {
110     if (data == ((CYG_ADDRWORD) hCAN0) && (flags & CYGNUM_CAN_EVENT_RX))
111     {
112         // Wake up thread
113         cyg_cond_signal(&can_wait);
114     }
115 }
116
117 //===========================================================================
118 //                             READER THREAD 
119 //===========================================================================
120 void can_thread(cyg_addrword_t data)
121 {
122     cyg_uint32              len;
123     cyg_can_callback_cfg    callback_cfg;
124     cyg_can_message         tx_msg;
125     cyg_bool_t              wait_res;
126     
127     //
128     // open CAN0 device driver
129     //
130     if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0)) 
131     {
132         CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
133     }
134     
135     //
136     // open CAN1 device driver
137     //
138     if (ENOERR != cyg_io_lookup("/dev/can1", &hCAN1)) 
139     {
140         CYG_TEST_FAIL_FINISH("Error opening /dev/can1");
141     }
142     
143     //
144     // configure CAN0 callback
145     //
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;
150     
151     if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_CALLBACK,
152             &callback_cfg, &len))
153     {
154         CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
155     }
156     
157     //
158     // transmit message from CAN1 to CAN0
159     //
160     tx_msg.id  = 0x001;
161     tx_msg.ext = CYGNUM_CAN_ID_STD;
162     tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
163     tx_msg.dlc = 0;
164     len = sizeof(tx_msg); 
165     
166     if (ENOERR != cyg_io_write(hCAN1, &tx_msg, &len))
167     {
168         CYG_TEST_FAIL_FINISH("Error writing message to /dev/can1");
169     }
170     
171     //
172     // Wait CAN0 callback
173     //
174     cyg_mutex_lock(&can_lock);
175     
176     wait_res = cyg_cond_timed_wait(&can_wait, 100);
177     
178     cyg_mutex_unlock(&can_lock);
179     
180     //
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
184     //
185     if(wait_res)
186     {
187         CYG_TEST_PASS_FINISH("can_callback test OK");
188     }
189     else
190     {
191         CYG_TEST_FAIL_FINISH("can_callback test FAILED");
192     }
193 }
194
195
196 void
197 cyg_start(void)
198 {
199     CYG_TEST_INIT();
200     
201     cyg_mutex_init(&can_lock);
202     cyg_cond_init(&can_wait, &can_lock);
203     
204     //
205     // create the main thread
206     //
207     cyg_thread_create(4, can_thread, 
208                         (cyg_addrword_t) 0,
209                         "can_thread", 
210                         (void *) can_thread_data.stack, 
211                         1024 * sizeof(long),
212                         &can_thread_data.hdl, 
213                         &can_thread_data.obj);
214                         
215     cyg_thread_resume(can_thread_data.hdl);
216     
217     cyg_scheduler_start();
218 }
219
220 #else // #if defined(CYGOPT_IO_CAN_SUPPORT_CALLBACK)
221 #define N_A_MSG "Needs callback support"
222 #endif
223
224 #else // CYGFUN_KERNEL_API_C
225 #define N_A_MSG "Needs kernel C API"
226 #endif
227
228 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
229 #define N_A_MSG "Needs IO/CAN and Kernel"
230 #endif
231
232 #ifdef N_A_MSG
233 void
234 cyg_start( void )
235 {
236     CYG_TEST_INIT();
237     CYG_TEST_NA( N_A_MSG);
238 }
239 #endif // N_A_MSG
240
241 // EOF can_callback.c