author | lbessard |
Thu, 18 Oct 2007 10:23:45 +0200 | |
changeset 310 | 1975e546178b |
parent 267 | 96c688ebcde7 |
child 365 | 9b76e0881beb |
permissions | -rw-r--r-- |
255 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
CanFestival Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
CanFestival Win32 port Copyright (C) 2007 Leonid Tochinski, ChattenAssociates, Inc. |
|
6 |
||
7 |
See COPYING file for copyrights details. |
|
8 |
||
9 |
This library is free software; you can redistribute it and/or |
|
10 |
modify it under the terms of the GNU Lesser General Public |
|
11 |
License as published by the Free Software Foundation; either |
|
12 |
version 2.1 of the License, or (at your option) any later version. |
|
13 |
||
14 |
This library is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 |
Lesser General Public License for more details. |
|
18 |
||
19 |
You should have received a copy of the GNU Lesser General Public |
|
20 |
License along with this library; if not, write to the Free Software |
|
21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
// pragma based message |
|
24 |
// http://www.codeproject.com/macro/location_pragma.asp |
|
25 |
#define __STR2__(x) #x |
|
26 |
#define __STR1__(x) __STR2__(x) |
|
27 |
#define __LOC2__ __FILE__ "("__STR1__(__LINE__)") : " |
|
28 |
||
29 |
||
30 |
#pragma message("*********************************************************************************") |
|
31 |
#pragma message(" NOTE: IXXAT Win32 drivers and API should be installed in order to build this project!") |
|
32 |
#pragma message(__LOC2__ "See IXXAT.Cpp header for details.") |
|
33 |
#pragma message("*********************************************************************************") |
|
34 |
||
35 |
||
252 | 36 |
// IXXAT adapter driver for CanFestival-3 Win32 port |
37 |
// |
|
38 |
// Notes |
|
39 |
//-------------------------------------------- |
|
40 |
// For building of this project you will need |
|
41 |
// the following IXXAT API files |
|
42 |
// Vci2.h |
|
43 |
// Vci11un6.lib |
|
44 |
// |
|
45 |
// IXXAT Win32 drivers and API can be downloaded from |
|
46 |
// http://www.ixxat.com/download_vci_en,7547,5873.html |
|
47 |
// |
|
48 |
// Copy Vci2.h & Vci11un6.lib files to can_ixxat_win32 folder of add path to them in Project settings. |
|
49 |
||
255 | 50 |
|
252 | 51 |
#include <stdio.h> |
52 |
extern "C" { |
|
53 |
#include "applicfg.h" |
|
54 |
#include "can_driver.h" |
|
55 |
#include "def.h" |
|
56 |
} |
|
57 |
#include "VCI2.h" |
|
58 |
#include "async_access_que.h" |
|
59 |
||
60 |
#pragma warning(disable:4996) |
|
61 |
||
62 |
#define CAN_NUM 0 |
|
63 |
||
64 |
class IXXAT |
|
65 |
{ |
|
66 |
public: |
|
67 |
class error |
|
68 |
{ |
|
69 |
}; |
|
70 |
IXXAT(s_BOARD *board); |
|
71 |
~IXXAT(); |
|
72 |
bool send(const Message *m); |
|
73 |
bool receive(Message *m); |
|
74 |
private: |
|
75 |
bool open(const char* board_name, int board_number, const char* baud_rate); |
|
76 |
bool close(); |
|
77 |
void receive_queuedata(UINT16 que_hdl, UINT16 count, VCI_CAN_OBJ* p_obj); |
|
78 |
// VCI2 handler |
|
79 |
static void VCI_CALLBACKATTR message_handler(char *msg_str); |
|
80 |
static void VCI_CALLBACKATTR receive_queuedata_handler(UINT16 que_hdl, UINT16 count, VCI_CAN_OBJ* p_obj); |
|
81 |
static void VCI_CALLBACKATTR exception_handler(VCI_FUNC_NUM func_num, INT32 err_code, UINT16 ext_err, char* err_str); |
|
82 |
||
83 |
private: |
|
84 |
UINT16 m_BoardHdl; |
|
85 |
UINT16 m_TxQueHdl; |
|
86 |
UINT16 m_RxQueHdl; |
|
87 |
async_access_que<VCI_CAN_OBJ> m_RX_Que; |
|
88 |
static IXXAT* m_callbackPtr; |
|
89 |
}; |
|
90 |
||
91 |
IXXAT *IXXAT::m_callbackPtr = NULL; |
|
92 |
||
93 |
IXXAT::IXXAT(s_BOARD *board) : m_BoardHdl(0xFFFF), |
|
94 |
m_TxQueHdl(0xFFFF), |
|
95 |
m_RxQueHdl(0xFFFF) |
|
96 |
||
97 |
{ |
|
98 |
char busname[100]; |
|
99 |
::strcpy(busname,board->busname); |
|
100 |
char board_name[100]; |
|
101 |
long board_number = 0; |
|
102 |
char *ptr = ::strrchr(busname,':'); |
|
103 |
if (ptr != 0) |
|
104 |
{ |
|
105 |
*ptr = 0; |
|
106 |
::strcpy(board_name,busname); |
|
107 |
if (++ptr - busname < (int)::strlen(board->busname)) |
|
108 |
board_number = ::atoi(ptr); |
|
109 |
} |
|
110 |
if (!open(board_name,board_number,board->baudrate)) |
|
111 |
{ |
|
112 |
close(); |
|
113 |
throw error(); |
|
114 |
} |
|
115 |
m_callbackPtr = this; |
|
116 |
} |
|
117 |
||
118 |
IXXAT::~IXXAT() |
|
119 |
{ |
|
120 |
close(); |
|
121 |
m_callbackPtr = 0; |
|
122 |
} |
|
123 |
||
124 |
bool IXXAT::send(const Message *m) |
|
125 |
{ |
|
126 |
if (m_BoardHdl == 0xFFFF) |
|
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
255
diff
changeset
|
127 |
return true; // true -> NOT OK |
252 | 128 |
long res = VCI_ERR; |
129 |
if (m->rtr == NOT_A_REQUEST) |
|
130 |
res = VCI_TransmitObj(m_BoardHdl, m_TxQueHdl, m->cob_id.w, m->len, const_cast<unsigned char*>(m->data)); |
|
131 |
else |
|
132 |
res = VCI_RequestObj(m_BoardHdl, m_TxQueHdl, m->cob_id.w, m->len); |
|
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
255
diff
changeset
|
133 |
return (res == false); // false -> OK |
252 | 134 |
} |
135 |
||
136 |
||
137 |
bool IXXAT::receive(Message *m) |
|
138 |
{ |
|
139 |
if (m_BoardHdl == 0xFFFF) |
|
140 |
return false; |
|
141 |
VCI_CAN_OBJ obj; |
|
142 |
if (m_RX_Que.extract_top(obj)) |
|
143 |
{ |
|
144 |
m->cob_id.w = obj.id; |
|
145 |
m->len = obj.len; |
|
146 |
m->rtr = (obj.rtr == VCI_RX_BUF) ? NOT_A_REQUEST : REQUEST; |
|
147 |
if (m->rtr == NOT_A_REQUEST) |
|
148 |
::memcpy(m->data, obj.a_data, m->len); |
|
149 |
return true; |
|
150 |
} |
|
151 |
return false; |
|
152 |
} |
|
153 |
||
154 |
bool IXXAT::open(const char* board_name, int board_number, const char* baud_rate) |
|
155 |
{ |
|
156 |
// check, if baudrate is supported |
|
157 |
struct IXXAT_baud_rate_param |
|
158 |
{ |
|
159 |
UINT8 bt0; |
|
160 |
UINT8 bt1; |
|
161 |
}; |
|
162 |
struct IXXAT_look_up_table |
|
163 |
{ |
|
164 |
char baud_rate[20]; |
|
165 |
IXXAT_baud_rate_param bt; |
|
166 |
}; |
|
167 |
static const IXXAT_look_up_table br_lut[] = { |
|
168 |
{"10K",{VCI_10KB}}, |
|
169 |
{"20K",{VCI_20KB}}, |
|
170 |
{"50K",{VCI_50KB}}, |
|
171 |
{"100K",{VCI_100KB}}, |
|
172 |
{"125K",{VCI_125KB}}, |
|
173 |
{"250K",{VCI_250KB}}, |
|
174 |
{"500K",{VCI_500KB}}, |
|
175 |
{"800K",{VCI_800KB}}, |
|
176 |
{"1M",{VCI_1000KB}} |
|
177 |
}; |
|
178 |
static const long br_lut_size = sizeof (br_lut)/sizeof(IXXAT_look_up_table); |
|
179 |
int index; |
|
180 |
for (index = 0; index < br_lut_size; ++index) |
|
181 |
{ |
|
182 |
if (::strcmp(br_lut[index].baud_rate,baud_rate)==0) |
|
183 |
break; |
|
184 |
} |
|
185 |
if (index == br_lut_size) |
|
186 |
return false; |
|
187 |
// close existing board |
|
188 |
close(); |
|
189 |
// init IXXAT board |
|
190 |
unsigned long board_type = VCI_GetBrdTypeByName(const_cast<char*>(board_name)); |
|
191 |
long res = VCI2_PrepareBoard( board_type, // board type |
|
192 |
board_number, // unique board index |
|
193 |
NULL, // pointer to buffer for additional info |
|
194 |
0, // length of additional info buffer |
|
195 |
message_handler, // pointer to msg-callbackhandler |
|
196 |
receive_queuedata_handler, // pointer to receive-callbackhandler |
|
197 |
exception_handler); // pointer to exception-callbackhandler |
|
198 |
if (res < 0) |
|
199 |
return false; |
|
200 |
m_BoardHdl = (UINT16)res; |
|
201 |
||
202 |
VCI_ResetBoard(m_BoardHdl); |
|
203 |
||
204 |
// init CAN parameters |
|
205 |
||
206 |
// initialize CAN-Controller |
|
207 |
res = VCI_InitCan(m_BoardHdl, CAN_NUM, br_lut[index].bt.bt0,br_lut[index].bt.bt1, VCI_11B); |
|
208 |
||
209 |
// definition of Acceptance-Mask (define to receive all IDs) |
|
210 |
res = VCI_SetAccMask(m_BoardHdl, CAN_NUM, 0x0UL, 0x0UL); |
|
211 |
||
212 |
// definition of Transmit Queue |
|
213 |
res = VCI_ConfigQueue(m_BoardHdl, CAN_NUM, VCI_TX_QUE, 100 , 0, 0, 0, &m_TxQueHdl); |
|
214 |
||
215 |
// definition of Receive Queue (interrupt mode) |
|
216 |
res = VCI_ConfigQueue(m_BoardHdl, CAN_NUM, VCI_RX_QUE, 50, 1, 0, 100, &m_RxQueHdl); |
|
217 |
||
218 |
// assign the all IDs to the Receive Queue |
|
219 |
res = VCI_AssignRxQueObj(m_BoardHdl, m_RxQueHdl ,VCI_ACCEPT, 0, 0) ; |
|
220 |
||
221 |
// And now start the CAN |
|
222 |
res = VCI_StartCan(m_BoardHdl, CAN_NUM); |
|
223 |
||
224 |
return true; |
|
225 |
} |
|
226 |
||
227 |
bool IXXAT::close() |
|
228 |
{ |
|
229 |
if (m_BoardHdl == 0xFFFF) |
|
230 |
return true; |
|
231 |
VCI_ResetBoard(m_BoardHdl); |
|
232 |
VCI_CancelBoard(m_BoardHdl); |
|
233 |
m_BoardHdl = |
|
234 |
m_TxQueHdl = |
|
235 |
m_RxQueHdl = 0xFFFF; |
|
236 |
return true; |
|
237 |
} |
|
238 |
||
239 |
void IXXAT::receive_queuedata(UINT16 que_hdl, UINT16 count, VCI_CAN_OBJ *p_obj) |
|
240 |
{ |
|
241 |
for (int i = 0; i < count; ++i) |
|
242 |
m_RX_Que.append(p_obj[i]); // can packet |
|
243 |
} |
|
244 |
||
245 |
void VCI_CALLBACKATTR IXXAT::receive_queuedata_handler(UINT16 que_hdl, UINT16 count, VCI_CAN_OBJ *p_obj) |
|
246 |
{ |
|
247 |
if (m_callbackPtr != NULL) |
|
248 |
m_callbackPtr->receive_queuedata(que_hdl, count, p_obj); |
|
249 |
} |
|
250 |
||
251 |
void VCI_CALLBACKATTR IXXAT::message_handler(char *msg_str) |
|
252 |
{ |
|
253 |
char buf[200]; |
|
254 |
::sprintf(buf,"IXXAT Message: [%s]\n", msg_str); |
|
255 |
::OutputDebugString(buf); |
|
256 |
} |
|
257 |
||
258 |
void VCI_CALLBACKATTR IXXAT::exception_handler(VCI_FUNC_NUM func_num, INT32 err_code, UINT16 ext_err, char* err_str) |
|
259 |
{ |
|
260 |
static const char* Num2Function[] = |
|
261 |
{ |
|
262 |
"VCI_Init", |
|
263 |
"VCI_Searchboard", |
|
264 |
"VCI_Prepareboard", |
|
265 |
"VCI_Cancel_board", |
|
266 |
"VCI_Testboard", |
|
267 |
"VCI_ReadBoardInfo", |
|
268 |
"VCI_ReadBoardStatus", |
|
269 |
"VCI_Resetboard", |
|
270 |
"VCI_ReadCANInfo", |
|
271 |
"VCI_ReadCANStatus", |
|
272 |
"VCI_InitCAN", |
|
273 |
"VCI_SetAccMask", |
|
274 |
"VCI_ResetCAN", |
|
275 |
"VCI_StartCAN", |
|
276 |
"VCI_ResetTimeStamps", |
|
277 |
"VCI_ConfigQueue", |
|
278 |
"VCI_AssignRxQueObj", |
|
279 |
"VCI_ConfigBuffer", |
|
280 |
"VCI_ReconfigBuffer", |
|
281 |
"VCI_ConfigTimer", |
|
282 |
"VCI_ReadQueStatus", |
|
283 |
"VCI_ReadQueObj", |
|
284 |
"VCI_ReadBufStatus", |
|
285 |
"VCI_ReadBufData", |
|
286 |
"VCI_TransmitObj", |
|
287 |
"VCI_RequestObj", |
|
288 |
"VCI_UpdateBufObj", |
|
289 |
"VCI_CciReqData" |
|
290 |
}; |
|
291 |
char buf[200]; |
|
292 |
::sprintf(buf, "IXXAT Exception: %s (%i / %u) [%s]\n", Num2Function[func_num], err_code, ext_err, err_str); |
|
293 |
::OutputDebugString(buf); |
|
294 |
} |
|
295 |
||
296 |
//------------------------------------------------------------------------ |
|
297 |
extern "C" |
|
298 |
UNS8 canReceive_driver(CAN_HANDLE inst, Message *m) |
|
299 |
{ |
|
300 |
return (UNS8)reinterpret_cast<IXXAT*>(inst)->receive(m); |
|
301 |
} |
|
302 |
||
303 |
extern "C" |
|
304 |
UNS8 canSend_driver(CAN_HANDLE inst, Message *m) |
|
305 |
{ |
|
306 |
return (UNS8)reinterpret_cast<IXXAT*>(inst)->send(m); |
|
307 |
} |
|
308 |
||
309 |
extern "C" |
|
310 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
|
311 |
{ |
|
312 |
try |
|
313 |
{ |
|
314 |
return new IXXAT(board); |
|
315 |
} |
|
316 |
catch (IXXAT::error&) |
|
317 |
{ |
|
318 |
return 0; |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
extern "C" |
|
323 |
int canClose_driver(CAN_HANDLE inst) |
|
324 |
{ |
|
325 |
delete reinterpret_cast<IXXAT*>(inst); |
|
326 |
return 1; |
|
327 |
} |