]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/can/loop/v2_0/tests/can_timeout.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / devs / can / loop / v2_0 / tests / can_timeout.c
1 //==========================================================================
2 //
3 //        can_timeout.c
4 //
5 //        Test CAN device driver timeouts
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
44 // Contributors:  Uwe Kindler
45 // Date:          2005-08-07
46 // Description:   Timeout test of CAN device driver
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 // timeout support required
73 #if defined(CYGOPT_IO_CAN_SUPPORT_TIMEOUTS)
74
75
76 //===========================================================================
77 //                               DATA TYPES
78 //===========================================================================
79 typedef struct st_thread_data
80 {
81     cyg_thread   obj;
82     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
83     cyg_handle_t hdl;
84 } thread_data_t;
85
86
87 //===========================================================================
88 //                              LOCAL DATA
89 //===========================================================================
90 cyg_thread_entry_t can0_thread;
91 thread_data_t      can0_thread_data;
92
93
94 //===========================================================================
95 //                          LOCAL FUNCTIONS
96 //===========================================================================
97 #include "can_test_aux.inl" // include CAN test auxiliary functions
98
99
100 //===========================================================================
101 // Measure timeout time and compare it with RX timeout value
102 //===========================================================================
103 void check_timeout(cyg_io_handle_t hCAN, cyg_uint32 timeout)
104 {
105     cyg_can_event          rx_event;
106     cyg_can_timeout_info_t timeout_info;
107     cyg_uint32             len;
108     cyg_uint32             current_time_old;
109     cyg_uint32             current_time_new;
110     cyg_int32              diff_time;   
111     
112     
113
114     timeout_info.tx_timeout = timeout;
115     timeout_info.rx_timeout = timeout;
116     len = sizeof(timeout_info);
117     if (ENOERR != cyg_io_set_config(hCAN, CYG_IO_SET_CONFIG_CAN_TIMEOUT ,&timeout_info, &len))
118     {
119         CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
120     }
121     
122     diag_printf("read timeout:  %d\n", timeout);
123     
124     //
125     // if the return code is -EINTR then we know that a timout occured. Normally the
126     // receive queue should be empty so we can expect a timeout here
127     //
128     current_time_old = (cyg_uint32)cyg_current_time();
129     len = sizeof(rx_event);
130     if (-EINTR == cyg_io_read(hCAN, &rx_event, &len))
131     {
132         current_time_new = (cyg_uint32)cyg_current_time();    
133         diff_time = current_time_new -  current_time_old;
134         diag_printf("measured time: %d\n\n", diff_time);
135         diff_time = diff_time - timeout;
136         
137         //
138         // If the measured value is smaller then the timeout value or if
139         // the difference is > 2 the we treat this as an error
140         //
141         if ((diff_time < 0) || (diff_time > 2))
142         {
143             CYG_TEST_FAIL_FINISH("Measured timeout time differs from /dev/can0 RX timeout value.");  
144         }
145     }
146     else
147     {
148         CYG_TEST_FAIL_FINISH("Timeout expected for /dev/can0");  
149     }
150 }
151
152 //===========================================================================
153 //                             WRITER THREAD 
154 //===========================================================================
155 void can0_thread(cyg_addrword_t data)
156 {
157     cyg_io_handle_t        hCAN0;
158     cyg_uint32             len;
159     cyg_uint32             blocking;
160     cyg_can_timeout_info_t timeout_info;
161    
162     
163     if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0)) 
164     {
165         CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
166     }
167     
168     //
169     // the first thing we need to do is to switch to nonblocking calls because
170     // function calls with timeout require nonblocking calls
171     //
172     blocking = 0;
173     len = sizeof(blocking);
174     if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_READ_BLOCKING ,&blocking, &len))
175     {
176         CYG_TEST_FAIL_FINISH("Error changing config of /dev/can0");
177     }
178       
179     //
180     // first we read the timeout configuration
181     //
182     len = sizeof(timeout_info);
183     if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_TIMEOUT ,&timeout_info, &len))
184     {
185         CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
186     }
187     
188     //
189     // now we check the default timeout and a number of different timeout values
190     //
191     check_timeout(hCAN0, timeout_info.rx_timeout);
192     check_timeout(hCAN0, 10);
193     check_timeout(hCAN0, 20);
194     check_timeout(hCAN0, 50);
195     check_timeout(hCAN0, 100);
196     check_timeout(hCAN0, 200);
197     check_timeout(hCAN0, 500);
198
199         
200     CYG_TEST_PASS_FINISH("can_timeout test OK");                     
201 }
202
203
204
205
206 void
207 cyg_start(void)
208 {
209     CYG_TEST_INIT();
210     
211     //
212     // create the thread which access the CAN device driver
213     //
214     cyg_thread_create(4, can0_thread, 
215                         (cyg_addrword_t) 0,
216                                 "can0_thread", 
217                                 (void *) can0_thread_data.stack, 
218                                 1024 * sizeof(long),
219                                 &can0_thread_data.hdl, 
220                                 &can0_thread_data.obj);
221                                 
222     cyg_thread_resume(can0_thread_data.hdl);
223     
224     cyg_scheduler_start();
225 }
226
227 #else // CYGOPT_IO_CAN_SUPPORT_TIMEOUTS
228 #define N_A_MSG "Timeout support required for IO/CAN"
229 #endif
230
231 #else // CYGFUN_KERNEL_API_C
232 #define N_A_MSG "Needs kernel C API"
233 #endif
234
235 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
236 #define N_A_MSG "Needs IO/CAN and Kernel"
237 #endif
238
239 #ifdef N_A_MSG
240 void
241 cyg_start( void )
242 {
243     CYG_TEST_INIT();
244     CYG_TEST_NA( N_A_MSG);
245 }
246 #endif // N_A_MSG
247
248 // EOF can_timeout.c