]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/io/can/v2_0/tests/can_tx.c
Initial revision
[karo-tx-redboot.git] / packages / io / can / v2_0 / tests / can_tx.c
1 //==========================================================================
2 //
3 //        can_tx.c
4 //
5 //        Simple CAN TX 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-01-08
46 // Description:   Simple write test of CAN 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 #include <cyg/hal/hal_diag.h>
59
60 // Package requirements
61 #if defined(CYGPKG_IO_CAN) && defined(CYGPKG_KERNEL)
62
63 #include <pkgconf/kernel.h>
64 #include <cyg/io/io.h>
65 #include <cyg/io/canio.h>
66
67 // Package option requirements
68 #if defined(CYGFUN_KERNEL_API_C)
69
70 #include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
71 #include <cyg/kernel/kapi.h>
72
73
74 //===========================================================================
75 //                               DATA TYPES
76 //===========================================================================
77 typedef struct st_thread_data
78 {
79     cyg_thread   obj;
80     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
81     cyg_handle_t hdl;
82 } thread_data_t;
83
84
85 //===========================================================================
86 //                              LOCAL DATA
87 //===========================================================================
88 cyg_thread_entry_t can0_thread;
89 thread_data_t      can0_thread_data;
90
91
92 //===========================================================================
93 //                          LOCAL FUNCTIONS
94 //===========================================================================
95 #include "can_test_aux.inl" // include CAN test auxiliary functions
96
97
98 //===========================================================================
99 //                             WRITER THREAD 
100 //===========================================================================
101 void can0_thread(cyg_addrword_t data)
102 {
103     cyg_io_handle_t hCAN0;
104     cyg_uint32      i;
105     cyg_uint32      len;
106     CYG_CAN_MSG_INIT(tx_msg, 0x001, CYGNUM_CAN_ID_STD, 1, CYGNUM_CAN_FRAME_DATA);
107     
108     //
109     // Open device and check return value
110     //
111     if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0)) 
112     {
113         CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
114     }
115
116 #ifdef CYGOPT_IO_CAN_STD_CAN_ID    
117     //
118     // Now send 1000 messages with standard identifier.
119     // Each message contains the message number in its identifier 
120     // and in the first data byte
121     //
122     for (i = 0; i < 1000; ++i)
123     {
124         CYG_CAN_MSG_SET_STD_ID(tx_msg, i);
125         CYG_CAN_MSG_SET_DATA(tx_msg, 0, i);
126         len = sizeof(tx_msg);
127         
128         //
129         // Each message with an odd identifier should be a remote request message
130         //
131         if (i % 2)
132         {
133             CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_RTR); 
134         }
135         else
136         {
137             CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_DATA); 
138         }
139         
140         //
141         // Sending CAN messages is blocking so the thread waits until there is
142         // space in the tx queue
143         //
144         if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
145         {
146             CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
147         }    
148     }
149 #endif
150
151 #ifdef CYGOPT_IO_CAN_EXT_CAN_ID    
152     //
153     // Now send 1000 messages with extended identifier.
154     // Each message contains the message number in its identifier 
155     // and in the first data byte
156     //
157     for (i = 0; i < 1000; ++i)
158     {
159         CYG_CAN_MSG_SET_EXT_ID(tx_msg, i + 0x800);
160         CYG_CAN_MSG_SET_DATA(tx_msg, 0, i);
161         CYG_CAN_MSG_SET_DATA_LEN(tx_msg, 8);
162         len = sizeof(tx_msg);
163         
164         //
165         // Each message with an odd identifier should be a remote request message
166         //
167         if (i % 2)
168         {
169             CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_RTR); 
170         }
171         else
172         {
173             CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_DATA); 
174         }
175         
176         //
177         // Sending CAN messages is blocking so the thread waits until there is
178         // space in the tx queue
179         //
180         if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
181         {
182             CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
183         }    
184     }
185 #endif
186
187     
188     CYG_TEST_PASS_FINISH("CAN TX test OK");
189 }
190
191
192 void
193 cyg_start(void)
194 {
195     CYG_TEST_INIT();
196     
197     //
198     // create the threads that access the CAN device driver
199     //
200     cyg_thread_create(4, can0_thread, 
201                         (cyg_addrword_t) 0,
202                                 "can0_thread", 
203                                 (void *) can0_thread_data.stack, 
204                                 1024 * sizeof(long),
205                                 &can0_thread_data.hdl, 
206                                 &can0_thread_data.obj);
207                                 
208     cyg_thread_resume(can0_thread_data.hdl);
209     
210     cyg_scheduler_start();
211 }
212
213 #else // CYGFUN_KERNEL_API_C
214 #define N_A_MSG "Needs kernel C API"
215 #endif
216
217 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
218 #define N_A_MSG "Needs IO/CAN and Kernel"
219 #endif
220
221 #ifdef N_A_MSG
222 void
223 cyg_start( void )
224 {
225     CYG_TEST_INIT();
226     CYG_TEST_NA( N_A_MSG);
227 }
228 #endif // N_A_MSG
229
230 // EOF can_tx.c