author | Christian Taedcke <Christian.Taedcke@ica-traffic.de> |
Fri, 19 Feb 2010 15:52:59 +0100 | |
changeset 653 | e2d0c5251fbd |
parent 647 | 8203ce2b9752 |
child 655 | ea1ddcc77acf |
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) |
|
365 | 130 |
res = VCI_TransmitObj(m_BoardHdl, m_TxQueHdl, m->cob_id, m->len, const_cast<unsigned char*>(m->data)); |
252 | 131 |
else |
365 | 132 |
res = VCI_RequestObj(m_BoardHdl, m_TxQueHdl, m->cob_id, m->len); |
647
8203ce2b9752
CHANGED: - canReceive and canSend return now 1 on error and 0 on success
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
646
diff
changeset
|
133 |
|
8203ce2b9752
CHANGED: - canReceive and canSend return now 1 on error and 0 on success
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
646
diff
changeset
|
134 |
return (res == VCI_OK); |
252 | 135 |
} |
136 |
||
137 |
||
138 |
bool IXXAT::receive(Message *m) |
|
139 |
{ |
|
140 |
if (m_BoardHdl == 0xFFFF) |
|
141 |
return false; |
|
142 |
VCI_CAN_OBJ obj; |
|
143 |
if (m_RX_Que.extract_top(obj)) |
|
144 |
{ |
|
646
4177b05ab835
CHANGED: - added explicit cast to remove compiler warning
Christian Taedcke
parents:
645
diff
changeset
|
145 |
m->cob_id = static_cast<UNS16>(obj.id); //valid for 11Bit ids |
252 | 146 |
m->len = obj.len; |
147 |
m->rtr = (obj.rtr == VCI_RX_BUF) ? NOT_A_REQUEST : REQUEST; |
|
148 |
if (m->rtr == NOT_A_REQUEST) |
|
149 |
::memcpy(m->data, obj.a_data, m->len); |
|
150 |
return true; |
|
151 |
} |
|
152 |
return false; |
|
153 |
} |
|
154 |
||
155 |
bool IXXAT::open(const char* board_name, int board_number, const char* baud_rate) |
|
156 |
{ |
|
157 |
// check, if baudrate is supported |
|
158 |
struct IXXAT_baud_rate_param |
|
159 |
{ |
|
160 |
UINT8 bt0; |
|
161 |
UINT8 bt1; |
|
162 |
}; |
|
163 |
struct IXXAT_look_up_table |
|
164 |
{ |
|
165 |
char baud_rate[20]; |
|
166 |
IXXAT_baud_rate_param bt; |
|
167 |
}; |
|
168 |
static const IXXAT_look_up_table br_lut[] = { |
|
169 |
{"10K",{VCI_10KB}}, |
|
170 |
{"20K",{VCI_20KB}}, |
|
171 |
{"50K",{VCI_50KB}}, |
|
172 |
{"100K",{VCI_100KB}}, |
|
173 |
{"125K",{VCI_125KB}}, |
|
174 |
{"250K",{VCI_250KB}}, |
|
175 |
{"500K",{VCI_500KB}}, |
|
176 |
{"800K",{VCI_800KB}}, |
|
177 |
{"1M",{VCI_1000KB}} |
|
178 |
}; |
|
179 |
static const long br_lut_size = sizeof (br_lut)/sizeof(IXXAT_look_up_table); |
|
180 |
int index; |
|
181 |
for (index = 0; index < br_lut_size; ++index) |
|
182 |
{ |
|
183 |
if (::strcmp(br_lut[index].baud_rate,baud_rate)==0) |
|
184 |
break; |
|
185 |
} |
|
186 |
if (index == br_lut_size) |
|
187 |
return false; |
|
188 |
// close existing board |
|
189 |
close(); |
|
190 |
// init IXXAT board |
|
191 |
unsigned long board_type = VCI_GetBrdTypeByName(const_cast<char*>(board_name)); |
|
192 |
long res = VCI2_PrepareBoard( board_type, // board type |
|
193 |
board_number, // unique board index |
|
194 |
NULL, // pointer to buffer for additional info |
|
195 |
0, // length of additional info buffer |
|
196 |
message_handler, // pointer to msg-callbackhandler |
|
197 |
receive_queuedata_handler, // pointer to receive-callbackhandler |
|
198 |
exception_handler); // pointer to exception-callbackhandler |
|
199 |
if (res < 0) |
|
200 |
return false; |
|
201 |
m_BoardHdl = (UINT16)res; |
|
202 |
||
203 |
VCI_ResetBoard(m_BoardHdl); |
|
204 |
||
205 |
// init CAN parameters |
|
206 |
||
207 |
// initialize CAN-Controller |
|
208 |
res = VCI_InitCan(m_BoardHdl, CAN_NUM, br_lut[index].bt.bt0,br_lut[index].bt.bt1, VCI_11B); |
|
209 |
||
210 |
// definition of Acceptance-Mask (define to receive all IDs) |
|
211 |
res = VCI_SetAccMask(m_BoardHdl, CAN_NUM, 0x0UL, 0x0UL); |
|
212 |
||
213 |
// definition of Transmit Queue |
|
214 |
res = VCI_ConfigQueue(m_BoardHdl, CAN_NUM, VCI_TX_QUE, 100 , 0, 0, 0, &m_TxQueHdl); |
|
215 |
||
216 |
// definition of Receive Queue (interrupt mode) |
|
217 |
res = VCI_ConfigQueue(m_BoardHdl, CAN_NUM, VCI_RX_QUE, 50, 1, 0, 100, &m_RxQueHdl); |
|
218 |
||
219 |
// assign the all IDs to the Receive Queue |
|
220 |
res = VCI_AssignRxQueObj(m_BoardHdl, m_RxQueHdl ,VCI_ACCEPT, 0, 0) ; |
|
221 |
||
222 |
// And now start the CAN |
|
223 |
res = VCI_StartCan(m_BoardHdl, CAN_NUM); |
|
224 |
||
225 |
return true; |
|
226 |
} |
|
227 |
||
228 |
bool IXXAT::close() |
|
229 |
{ |
|
230 |
if (m_BoardHdl == 0xFFFF) |
|
231 |
return true; |
|
232 |
VCI_ResetBoard(m_BoardHdl); |
|
233 |
VCI_CancelBoard(m_BoardHdl); |
|
234 |
m_BoardHdl = |
|
235 |
m_TxQueHdl = |
|
236 |
m_RxQueHdl = 0xFFFF; |
|
237 |
return true; |
|
238 |
} |
|
239 |
||
240 |
void IXXAT::receive_queuedata(UINT16 que_hdl, UINT16 count, VCI_CAN_OBJ *p_obj) |
|
241 |
{ |
|
242 |
for (int i = 0; i < count; ++i) |
|
243 |
m_RX_Que.append(p_obj[i]); // can packet |
|
244 |
} |
|
245 |
||
246 |
void VCI_CALLBACKATTR IXXAT::receive_queuedata_handler(UINT16 que_hdl, UINT16 count, VCI_CAN_OBJ *p_obj) |
|
247 |
{ |
|
248 |
if (m_callbackPtr != NULL) |
|
249 |
m_callbackPtr->receive_queuedata(que_hdl, count, p_obj); |
|
250 |
} |
|
251 |
||
252 |
void VCI_CALLBACKATTR IXXAT::message_handler(char *msg_str) |
|
253 |
{ |
|
254 |
char buf[200]; |
|
255 |
::sprintf(buf,"IXXAT Message: [%s]\n", msg_str); |
|
256 |
::OutputDebugString(buf); |
|
257 |
} |
|
258 |
||
259 |
void VCI_CALLBACKATTR IXXAT::exception_handler(VCI_FUNC_NUM func_num, INT32 err_code, UINT16 ext_err, char* err_str) |
|
260 |
{ |
|
261 |
static const char* Num2Function[] = |
|
262 |
{ |
|
263 |
"VCI_Init", |
|
264 |
"VCI_Searchboard", |
|
265 |
"VCI_Prepareboard", |
|
266 |
"VCI_Cancel_board", |
|
267 |
"VCI_Testboard", |
|
268 |
"VCI_ReadBoardInfo", |
|
269 |
"VCI_ReadBoardStatus", |
|
270 |
"VCI_Resetboard", |
|
271 |
"VCI_ReadCANInfo", |
|
272 |
"VCI_ReadCANStatus", |
|
273 |
"VCI_InitCAN", |
|
274 |
"VCI_SetAccMask", |
|
275 |
"VCI_ResetCAN", |
|
276 |
"VCI_StartCAN", |
|
277 |
"VCI_ResetTimeStamps", |
|
278 |
"VCI_ConfigQueue", |
|
279 |
"VCI_AssignRxQueObj", |
|
280 |
"VCI_ConfigBuffer", |
|
281 |
"VCI_ReconfigBuffer", |
|
282 |
"VCI_ConfigTimer", |
|
283 |
"VCI_ReadQueStatus", |
|
284 |
"VCI_ReadQueObj", |
|
285 |
"VCI_ReadBufStatus", |
|
286 |
"VCI_ReadBufData", |
|
287 |
"VCI_TransmitObj", |
|
288 |
"VCI_RequestObj", |
|
289 |
"VCI_UpdateBufObj", |
|
290 |
"VCI_CciReqData" |
|
291 |
}; |
|
292 |
char buf[200]; |
|
293 |
::sprintf(buf, "IXXAT Exception: %s (%i / %u) [%s]\n", Num2Function[func_num], err_code, ext_err, err_str); |
|
294 |
::OutputDebugString(buf); |
|
295 |
} |
|
296 |
||
297 |
//------------------------------------------------------------------------ |
|
298 |
extern "C" |
|
645
93ad82ee0426
FIXED: - added CALLBACK to can callback function prototypes
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
631
diff
changeset
|
299 |
UNS8 __stdcall canReceive_driver(CAN_HANDLE inst, Message *m) |
252 | 300 |
{ |
647
8203ce2b9752
CHANGED: - canReceive and canSend return now 1 on error and 0 on success
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
646
diff
changeset
|
301 |
return reinterpret_cast<IXXAT*>(inst)->receive(m) ? 0 : 1; |
252 | 302 |
} |
303 |
||
304 |
extern "C" |
|
645
93ad82ee0426
FIXED: - added CALLBACK to can callback function prototypes
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
631
diff
changeset
|
305 |
UNS8 __stdcall canSend_driver(CAN_HANDLE inst, Message const *m) |
252 | 306 |
{ |
647
8203ce2b9752
CHANGED: - canReceive and canSend return now 1 on error and 0 on success
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
646
diff
changeset
|
307 |
return reinterpret_cast<IXXAT*>(inst)->send(m) ? 0 : 1; |
252 | 308 |
} |
309 |
||
310 |
extern "C" |
|
645
93ad82ee0426
FIXED: - added CALLBACK to can callback function prototypes
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
631
diff
changeset
|
311 |
CAN_HANDLE __stdcall canOpen_driver(s_BOARD *board) |
252 | 312 |
{ |
313 |
try |
|
314 |
{ |
|
315 |
return new IXXAT(board); |
|
316 |
} |
|
317 |
catch (IXXAT::error&) |
|
318 |
{ |
|
319 |
return 0; |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
extern "C" |
|
645
93ad82ee0426
FIXED: - added CALLBACK to can callback function prototypes
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
631
diff
changeset
|
324 |
int __stdcall canClose_driver(CAN_HANDLE inst) |
252 | 325 |
{ |
326 |
delete reinterpret_cast<IXXAT*>(inst); |
|
327 |
return 1; |
|
328 |
} |
|
384 | 329 |
|
330 |
extern "C" |
|
645
93ad82ee0426
FIXED: - added CALLBACK to can callback function prototypes
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents:
631
diff
changeset
|
331 |
UNS8 __stdcall canChangeBaudRate_driver( CAN_HANDLE fd, char* baud) |
384 | 332 |
{ |
333 |
//printf("canChangeBaudRate not yet supported by this driver\n"); |
|
334 |
return 0; |
|
631 | 335 |
} |