]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/io/can/v2_0/tests/can_filter.c
Initial revision
[karo-tx-redboot.git] / packages / io / can / v2_0 / tests / can_filter.c
1 //==========================================================================
2 //
3 //        can_filter.c
4 //
5 //        CAN message filter test
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:          2007-03-21
46 // Description:   CAN hardware filter test
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_STD_CAN_ID)
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 cyg_io_handle_t    hCAN0;
95
96
97 //===========================================================================
98 //                          LOCAL FUNCTIONS
99 //===========================================================================
100 #include "can_test_aux.inl" // include CAN test auxiliary functions
101
102
103 //===========================================================================
104 // Main thread
105 //===========================================================================
106 void can0_thread(cyg_addrword_t data)
107 {
108     cyg_uint32             len;
109     cyg_can_event          rx_event;
110     cyg_uint16             i;
111     cyg_can_hdi            hdi;
112     cyg_can_msgbuf_info    msgbox_info;
113     cyg_can_msgbuf_cfg     msgbox_cfg;
114
115     
116     len = sizeof(hdi);
117     if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_HDI ,&hdi, &len))
118     {
119         CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
120     } 
121     
122     //
123     // Normally the CAN modul should support message filters. So the
124     // FULLCAN flag should be set - if it is not, we treat this as an error
125     //
126     if (!(hdi.support_flags & CYGNUM_CAN_HDI_FULLCAN))
127     {
128         CYG_TEST_FAIL_FINISH("/dev/can0 does not support message buffers");
129     }
130     
131     
132     //
133     // Now reset message buffer configuration - this is mandatory bevore starting
134     // message buffer runtime configuration
135     //
136     msgbox_cfg.cfg_id = CYGNUM_CAN_MSGBUF_RESET_ALL;
137     len = sizeof(msgbox_cfg);
138     if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&msgbox_cfg, &len))
139     {
140         CYG_TEST_FAIL_FINISH("Error resetting message buffer configuration of /dev/can0");
141     } 
142     
143     //
144     // Now query number of available and free message boxes
145     //
146     len = sizeof(msgbox_info);
147     if (ENOERR != cyg_io_get_config(hCAN0, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
148     {
149         CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
150     } 
151     
152     //
153     // if there are no free message boxes available then this is a failure
154     //
155     if (!msgbox_info.free)
156     {
157         CYG_TEST_FAIL_FINISH("No free message boxes available for /dev/can0");
158     }
159     
160     //
161     // We setup as many standard CAN message filters as there are free
162     // message buffers available.
163     //
164     for (i = 0; i < msgbox_info.free; ++i)
165     {
166         cyg_can_filter rx_filter;
167         
168         rx_filter.cfg_id  = CYGNUM_CAN_MSGBUF_RX_FILTER_ADD;
169         rx_filter.msg.id  = i;
170         rx_filter.msg.ext = CYGNUM_CAN_ID_STD;
171   
172         len = sizeof(rx_filter); 
173         if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF ,&rx_filter, &len))
174         {
175             CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
176         }
177         else if (CYGNUM_CAN_MSGBUF_NA == rx_filter.handle)
178         {
179             CYG_TEST_FAIL_FINISH("Error setting up message filter for /dev/can0");
180         }
181     }
182     
183     
184     diag_printf("\n\nNow try to send CAN messages. The device should only\n"
185                 "receive messages identifiers in the range of 0x00 to 0x%X.\n"
186                 "As soon as a standard message with ID 0x000 arrives, all\n"
187                 "message filters will be cleared\n\n", (msgbox_info.free - 1));
188     
189     //
190     // Now receive messages until a message arrives with largest ID of all
191     // available message filters
192     //
193     rx_event.msg.id = 1;
194     while(rx_event.msg.id != 0)
195     {
196         len = sizeof(rx_event); 
197             
198         if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
199         {
200             CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
201         }      
202         else if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
203         {
204             print_can_msg(&rx_event.msg, "");    
205         } // if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
206         else
207         {
208                 print_can_flags(rx_event.flags, "");
209                 rx_event.msg.id = 1;
210         }
211     } // while(1)
212     
213     
214     //
215     // Now enable reception of all available CAN messages
216     //
217     cyg_can_filter rx_filter;
218     rx_filter.cfg_id  = CYGNUM_CAN_MSGBUF_RX_FILTER_ALL;
219     len = sizeof(rx_filter);
220     if (ENOERR != cyg_io_set_config(hCAN0, CYG_IO_SET_CONFIG_CAN_MSGBUF , &rx_filter, &len))
221     {
222         CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
223     }
224     
225     
226     diag_printf("\n\nAll message filters have been cleared an now the device\n"
227                 "will receive any available CAN message identifiers.\n"
228                 "Send a CAN message with ID 0x100 to stop this test.\n\n");
229     
230     //
231     // Now receive messages until a message arrives with largest ID of all
232     // available message filters
233     //
234     rx_event.msg.id = 1;
235     while(rx_event.msg.id != 0x100)
236     {
237         len = sizeof(rx_event); 
238             
239         if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
240         {
241             CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
242         }      
243         else if (rx_event.flags & CYGNUM_CAN_EVENT_RX)
244         {
245             print_can_msg(&rx_event.msg, "");    
246         } // if (ENOERR != cyg_io_read(hCAN0, &rx_event, &len))
247     } // while(1)
248     
249     CYG_TEST_PASS_FINISH("can_filter test OK");
250 }
251
252
253 void
254 cyg_start(void)
255 {
256     CYG_TEST_INIT();
257     
258     //
259     // open CAN device driver
260     //
261     if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0)) 
262     {
263         CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
264     }
265     
266    
267     //
268     // create the two threads which access the CAN device driver
269     // a reader thread with a higher priority and a writer thread
270     // with a lower priority
271     //
272     cyg_thread_create(4, can0_thread, 
273                         (cyg_addrword_t) 0,
274                                 "can0_thread", 
275                                 (void *) can0_thread_data.stack, 
276                                 1024 * sizeof(long),
277                                 &can0_thread_data.hdl, 
278                                 &can0_thread_data.obj);
279                                 
280     cyg_thread_resume(can0_thread_data.hdl);
281     
282     cyg_scheduler_start();
283 }
284
285 #else // CYGOPT_IO_CAN_STD_CAN_ID
286 #define N_A_MSG "Needs support for standard CAN identifiers"
287 #endif
288
289 #else // CYGFUN_KERNEL_API_C
290 #define N_A_MSG "Needs kernel C API"
291 #endif
292
293 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
294 #define N_A_MSG "Needs IO/CAN and Kernel"
295 #endif
296
297 #ifdef N_A_MSG
298 void
299 cyg_start( void )
300 {
301     CYG_TEST_INIT();
302     CYG_TEST_NA( N_A_MSG);
303 }
304 #endif // N_A_MSG
305
306 //---------------------------------------------------------------------------
307 // EOF can_filter.c