1 |
786 |
skrzyp |
//==========================================================================
|
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 Free Software Foundation, 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
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Uwe Kindler
|
43 |
|
|
// Contributors: Uwe Kindler
|
44 |
|
|
// Date: 2007-01-08
|
45 |
|
|
// Description: Simple write test of CAN driver
|
46 |
|
|
//####DESCRIPTIONEND####
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
//===========================================================================
|
50 |
|
|
// INCLUDES
|
51 |
|
|
//===========================================================================
|
52 |
|
|
#include <pkgconf/system.h>
|
53 |
|
|
|
54 |
|
|
#include <cyg/infra/testcase.h> // test macros
|
55 |
|
|
#include <cyg/infra/cyg_ass.h> // assertion macros
|
56 |
|
|
#include <cyg/infra/diag.h>
|
57 |
|
|
#include <cyg/hal/hal_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 |
|
|
|
73 |
|
|
//===========================================================================
|
74 |
|
|
// DATA TYPES
|
75 |
|
|
//===========================================================================
|
76 |
|
|
typedef struct st_thread_data
|
77 |
|
|
{
|
78 |
|
|
cyg_thread obj;
|
79 |
|
|
long stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
|
80 |
|
|
cyg_handle_t hdl;
|
81 |
|
|
} thread_data_t;
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
//===========================================================================
|
85 |
|
|
// LOCAL DATA
|
86 |
|
|
//===========================================================================
|
87 |
|
|
cyg_thread_entry_t can0_thread;
|
88 |
|
|
thread_data_t can0_thread_data;
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
//===========================================================================
|
92 |
|
|
// LOCAL FUNCTIONS
|
93 |
|
|
//===========================================================================
|
94 |
|
|
#include "can_test_aux.inl" // include CAN test auxiliary functions
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
//===========================================================================
|
98 |
|
|
// WRITER THREAD
|
99 |
|
|
//===========================================================================
|
100 |
|
|
void can0_thread(cyg_addrword_t data)
|
101 |
|
|
{
|
102 |
|
|
cyg_io_handle_t hCAN0;
|
103 |
|
|
cyg_uint32 i;
|
104 |
|
|
cyg_uint32 len;
|
105 |
|
|
CYG_CAN_MSG_INIT(tx_msg, 0x001, CYGNUM_CAN_ID_STD, 1, CYGNUM_CAN_FRAME_DATA);
|
106 |
|
|
|
107 |
|
|
//
|
108 |
|
|
// Open device and check return value
|
109 |
|
|
//
|
110 |
|
|
if (ENOERR != cyg_io_lookup("/dev/can0", &hCAN0))
|
111 |
|
|
{
|
112 |
|
|
CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
#ifdef CYGOPT_IO_CAN_STD_CAN_ID
|
116 |
|
|
//
|
117 |
|
|
// Now send 1000 messages with standard identifier.
|
118 |
|
|
// Each message contains the message number in its identifier
|
119 |
|
|
// and in the first data byte
|
120 |
|
|
//
|
121 |
|
|
for (i = 0; i < 1000; ++i)
|
122 |
|
|
{
|
123 |
|
|
CYG_CAN_MSG_SET_STD_ID(tx_msg, i);
|
124 |
|
|
CYG_CAN_MSG_SET_DATA(tx_msg, 0, i);
|
125 |
|
|
len = sizeof(tx_msg);
|
126 |
|
|
|
127 |
|
|
//
|
128 |
|
|
// Each message with an odd identifier should be a remote request message
|
129 |
|
|
//
|
130 |
|
|
if (i % 2)
|
131 |
|
|
{
|
132 |
|
|
CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_RTR);
|
133 |
|
|
}
|
134 |
|
|
else
|
135 |
|
|
{
|
136 |
|
|
CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_DATA);
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
//
|
140 |
|
|
// Sending CAN messages is blocking so the thread waits until there is
|
141 |
|
|
// space in the tx queue
|
142 |
|
|
//
|
143 |
|
|
if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
|
144 |
|
|
{
|
145 |
|
|
CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
#endif
|
149 |
|
|
|
150 |
|
|
#ifdef CYGOPT_IO_CAN_EXT_CAN_ID
|
151 |
|
|
//
|
152 |
|
|
// Now send 1000 messages with extended identifier.
|
153 |
|
|
// Each message contains the message number in its identifier
|
154 |
|
|
// and in the first data byte
|
155 |
|
|
//
|
156 |
|
|
for (i = 0; i < 1000; ++i)
|
157 |
|
|
{
|
158 |
|
|
CYG_CAN_MSG_SET_EXT_ID(tx_msg, i + 0x800);
|
159 |
|
|
CYG_CAN_MSG_SET_DATA(tx_msg, 0, i);
|
160 |
|
|
CYG_CAN_MSG_SET_DATA_LEN(tx_msg, 8);
|
161 |
|
|
len = sizeof(tx_msg);
|
162 |
|
|
|
163 |
|
|
//
|
164 |
|
|
// Each message with an odd identifier should be a remote request message
|
165 |
|
|
//
|
166 |
|
|
if (i % 2)
|
167 |
|
|
{
|
168 |
|
|
CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_RTR);
|
169 |
|
|
}
|
170 |
|
|
else
|
171 |
|
|
{
|
172 |
|
|
CYG_CAN_MSG_SET_FRAME_TYPE(tx_msg, CYGNUM_CAN_FRAME_DATA);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
//
|
176 |
|
|
// Sending CAN messages is blocking so the thread waits until there is
|
177 |
|
|
// space in the tx queue
|
178 |
|
|
//
|
179 |
|
|
if (ENOERR != cyg_io_write(hCAN0, &tx_msg, &len))
|
180 |
|
|
{
|
181 |
|
|
CYG_TEST_FAIL_FINISH("Error writing to /dev/can0");
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
#endif
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
CYG_TEST_PASS_FINISH("CAN TX test OK");
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
void
|
192 |
|
|
cyg_start(void)
|
193 |
|
|
{
|
194 |
|
|
CYG_TEST_INIT();
|
195 |
|
|
|
196 |
|
|
//
|
197 |
|
|
// create the threads that access the CAN device driver
|
198 |
|
|
//
|
199 |
|
|
cyg_thread_create(4, can0_thread,
|
200 |
|
|
(cyg_addrword_t) 0,
|
201 |
|
|
"can0_thread",
|
202 |
|
|
(void *) can0_thread_data.stack,
|
203 |
|
|
1024 * sizeof(long),
|
204 |
|
|
&can0_thread_data.hdl,
|
205 |
|
|
&can0_thread_data.obj);
|
206 |
|
|
|
207 |
|
|
cyg_thread_resume(can0_thread_data.hdl);
|
208 |
|
|
|
209 |
|
|
cyg_scheduler_start();
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
#else // CYGFUN_KERNEL_API_C
|
213 |
|
|
#define N_A_MSG "Needs kernel C API"
|
214 |
|
|
#endif
|
215 |
|
|
|
216 |
|
|
#else // CYGPKG_IO_CAN && CYGPKG_KERNEL
|
217 |
|
|
#define N_A_MSG "Needs IO/CAN and Kernel"
|
218 |
|
|
#endif
|
219 |
|
|
|
220 |
|
|
#ifdef N_A_MSG
|
221 |
|
|
void
|
222 |
|
|
cyg_start( void )
|
223 |
|
|
{
|
224 |
|
|
CYG_TEST_INIT();
|
225 |
|
|
CYG_TEST_NA( N_A_MSG);
|
226 |
|
|
}
|
227 |
|
|
#endif // N_A_MSG
|
228 |
|
|
|
229 |
|
|
// EOF can_tx.c
|