]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/can/arm/lpc2xxx/v2_0/tests/can_multichan_tx.c
Initial revision
[karo-tx-redboot.git] / packages / devs / can / arm / lpc2xxx / v2_0 / tests / can_multichan_tx.c
1 //==========================================================================
2 //
3 //        can_multichan_tx.c
4 //
5 //        CAN TX test for multiple CAN channels
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-06-26
46 // Description:   CAN TX test for multiple CAN controller channels
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
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 can_thread;
89 thread_data_t      can0_thread_data;
90 cyg_io_handle_t    hCAN_Tbl[4];
91
92
93 //===========================================================================
94 //                             READER THREAD 
95 //===========================================================================
96 void can_thread(cyg_addrword_t data)
97 {
98     cyg_uint32              len;
99     cyg_can_message         tx_msg;
100     cyg_can_info_t          can_info;
101     cyg_can_baud_rate_t     baud;
102     cyg_uint8               i = 0;
103     cyg_uint8               j = 0;
104
105     //
106     // Check that all cannels have the same baudrate
107     //
108 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN0
109     len = sizeof(can_info);
110     if (ENOERR != cyg_io_get_config(hCAN_Tbl[0], CYG_IO_GET_CONFIG_CAN_INFO, &can_info, &len))
111     {
112         CYG_TEST_FAIL_FINISH("Error reading baudrate of CAN channel 0");    
113     } 
114     else
115     {
116         baud = can_info.baud;
117         ++i;
118     }
119 #endif
120     
121 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN1
122     len = sizeof(can_info);
123     if (ENOERR != cyg_io_get_config(hCAN_Tbl[1], CYG_IO_GET_CONFIG_CAN_INFO, &can_info, &len))
124     {
125         CYG_TEST_FAIL_FINISH("Error reading baudrate of CAN channel 1");    
126     }
127     else
128     {
129         if (i && (baud != can_info.baud))
130         {
131             CYG_TEST_FAIL_FINISH("Error - different baudrates for CAN channel 0 and 1");               
132         }
133         baud = can_info.baud;
134         ++i;
135     }
136 #endif
137     
138 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN2
139     len = sizeof(can_info);
140     if (ENOERR != cyg_io_get_config(hCAN_Tbl[2], CYG_IO_GET_CONFIG_CAN_INFO, &can_info, &len))
141     {
142         CYG_TEST_FAIL_FINISH("Error reading baudrate of CAN channel 2");    
143     } 
144     else
145     {
146         if (i && (baud != can_info.baud))
147         {
148             CYG_TEST_FAIL_FINISH("Error - different baudrates for CAN channel 1 and 2");               
149         }
150         baud = can_info.baud;
151         ++i;
152     }
153 #endif
154     
155 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN3
156     len = sizeof(can_info);
157     if (ENOERR != cyg_io_get_config(hCAN_Tbl[3], CYG_IO_GET_CONFIG_CAN_INFO, &can_info, &len))
158     {
159         CYG_TEST_FAIL_FINISH("Error reading baudrate of CAN channel 3");    
160     }
161     else
162     {
163         if (i && (baud != can_info.baud))
164         {
165             CYG_TEST_FAIL_FINISH("Error - different baudrates for CAN channel 2 and 3");               
166         }
167         baud = can_info.baud;
168         ++i;
169     }
170 #endif
171     
172     diag_printf("\n\nYou should no receive 4 CAN messages from each active CAN channel\n");
173     
174     //
175     // Now each CAN channel sends 10 CAN messages
176     //
177     for (i = 0; i < 4; ++i)
178     {
179         if (hCAN_Tbl[i])
180         {
181             CYG_CAN_MSG_SET_PARAM(tx_msg, i * 0x100, CYGNUM_CAN_ID_STD, 4, CYGNUM_CAN_FRAME_DATA);
182             tx_msg.data.dwords[0] = 0;
183             tx_msg.data.dwords[1] = 0;
184             char err_msg[64];
185             diag_snprintf(err_msg, sizeof(err_msg), "Error sending TX using CAN channel %d", i);
186             for (j = 0; j < 4; ++j)
187             {
188                 tx_msg.id = i * 0x100 + j; 
189                 tx_msg.data.bytes[0] = j;
190                 len = sizeof(tx_msg);
191                 if (ENOERR != cyg_io_write(hCAN_Tbl[i], &tx_msg, &len))
192                 {
193                     CYG_TEST_FAIL_FINISH(err_msg);     
194                 }
195             }
196         } //  if (hCAN_Tbl[i])
197     } // for (i = 0; i < 4; ++i)
198     
199     CYG_TEST_PASS_FINISH("LPC2xxx CAN multi channel TX test OK");
200 }
201
202
203
204 void
205 cyg_start(void)
206 {
207     CYG_TEST_INIT();
208
209 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN0
210     //
211     // open CAN device driver
212     //
213     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN0_NAME, &hCAN_Tbl[0])) 
214     {
215         CYG_TEST_FAIL_FINISH("Error opening CAN channel 0");
216     }
217 #else
218     hCAN_Tbl[0] = 0;  
219 #endif
220     
221 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN1
222     //
223     // open CAN device driver
224     //
225     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN1_NAME, &hCAN_Tbl[1])) 
226     {
227         CYG_TEST_FAIL_FINISH("Error opening CAN channel 1");
228     }
229 #else
230     hCAN_Tbl[1] = 0;  
231 #endif
232     
233 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN2
234     //
235     // open CAN device driver
236     //
237     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN2_NAME, &hCAN_Tbl[2])) 
238     {
239         CYG_TEST_FAIL_FINISH("Error opening CAN channel 2");
240     }
241 #else
242     hCAN_Tbl[2] = 0;  
243 #endif
244     
245 #ifdef CYGPKG_DEVS_CAN_LPC2XXX_CAN3
246     //
247     // open CAN device driver
248     //
249     if (ENOERR != cyg_io_lookup(CYGPKG_DEVS_CAN_LPC2XXX_CAN3_NAME, &hCAN_Tbl[3])) 
250     {
251         CYG_TEST_FAIL_FINISH("Error opening CAN channel 3");
252     }
253 #else
254     hCAN_Tbl[3] = 0;  
255 #endif
256     
257     //
258     // create the thread that accesses the CAN device driver
259     //
260     cyg_thread_create(4, can_thread, 
261                         (cyg_addrword_t) 0,
262                         "can0_thread", 
263                         (void *) can0_thread_data.stack, 
264                         1024 * sizeof(long),
265                         &can0_thread_data.hdl, 
266                         &can0_thread_data.obj);
267                         
268     cyg_thread_resume(can0_thread_data.hdl);
269     
270     cyg_scheduler_start();
271 }
272
273
274 #else // CYGFUN_KERNEL_API_C
275 #define N_A_MSG "Needs kernel C API"
276 #endif
277
278 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
279 #define N_A_MSG "Needs Kernel"
280 #endif
281
282 #ifdef N_A_MSG
283 void
284 cyg_start( void )
285 {
286     CYG_TEST_INIT();
287     CYG_TEST_NA(N_A_MSG);
288 }
289 #endif // N_A_MSG
290
291 //---------------------------------------------------------------------------
292 // EOF can_multichan_tx.c