1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of Stesy, the MAGIC Steering System
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz 2001 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2008
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | ///////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // VmodIcan
|
---|
28 | //
|
---|
29 | // Class describing the interface to the Janz card in RawCan mode.
|
---|
30 | //
|
---|
31 | ///////////////////////////////////////////////////////////////////////
|
---|
32 | #include "vmodican.h"
|
---|
33 |
|
---|
34 | #include <fcntl.h> // O_RDONLY
|
---|
35 | #include <errno.h> // errno
|
---|
36 | #include <unistd.h> // read
|
---|
37 | #include <sys/time.h> // gettimeofday
|
---|
38 | #include <sys/ioctl.h> // ioctl
|
---|
39 |
|
---|
40 | #include "canopen.h"
|
---|
41 |
|
---|
42 | #include "MLog.h"
|
---|
43 |
|
---|
44 | ClassImp(VmodIcan);
|
---|
45 |
|
---|
46 | using namespace std;
|
---|
47 |
|
---|
48 | // --------------------------------------------------------------------------
|
---|
49 | //
|
---|
50 | // Embedded ioctl-function from C-lib
|
---|
51 | //
|
---|
52 | int VmodIcan::Ioctl(int msg, void *arg)
|
---|
53 | {
|
---|
54 | return fd<0 ? 1 : ioctl(fd, msg, arg) >= 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | // --------------------------------------------------------------------------
|
---|
58 | //
|
---|
59 | // Enables/Disables the termination.
|
---|
60 | //
|
---|
61 | void VmodIcan::SetTermination(int state) /* 0 = off, 1 = on */
|
---|
62 | {
|
---|
63 | /* -*-Func-*-
|
---|
64 | *
|
---|
65 | * SwitchCanTermination - Switch the CANbus termination resistor
|
---|
66 | *
|
---|
67 | * The VMOD-ICAN3 module allows the user to change the state of the CANbus
|
---|
68 | * termination via software. This is done with this service.
|
---|
69 | *
|
---|
70 | * SERVICE: HwConf
|
---|
71 | */
|
---|
72 |
|
---|
73 | Message msg;
|
---|
74 |
|
---|
75 | msg.cmd = M_HW_CONF;
|
---|
76 |
|
---|
77 | msg.len = 2;
|
---|
78 | msg.data[0] = 0x00;
|
---|
79 | msg.data[1] = (BYTE_t)state;
|
---|
80 |
|
---|
81 | while (!Send(&msg));
|
---|
82 |
|
---|
83 | gLog << "- CAN Bus Termination set to " << (state?"on":"off") << endl;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // --------------------------------------------------------------------------
|
---|
87 | //
|
---|
88 | // Receiver Thread. Listener. Listens for incomming messages and processes
|
---|
89 | // these messages through the standard interface:
|
---|
90 | // - therefor overload HandleCanMessge
|
---|
91 | //
|
---|
92 | Int_t VmodIcan::Thread()
|
---|
93 | {
|
---|
94 | if (fd<0)
|
---|
95 | return 0;
|
---|
96 |
|
---|
97 | gLog << "- Starting Receiver Loop." << endl;
|
---|
98 |
|
---|
99 | while (1)
|
---|
100 | {
|
---|
101 | //
|
---|
102 | // Sleeps until a message arrives
|
---|
103 | //
|
---|
104 | TThread::CancelPoint();
|
---|
105 |
|
---|
106 | unsigned char c;
|
---|
107 | const int n = read(fd, &c, 1);
|
---|
108 |
|
---|
109 | TThread::CancelPoint();
|
---|
110 |
|
---|
111 | if (n<0)
|
---|
112 | {
|
---|
113 | cerr << "Vmodican: read(" << dec << (int)fd << "," << (void*)&c;
|
---|
114 | cerr << ",1) returned rc=" << n << " c=" << (int)c << " '";
|
---|
115 | cerr << strerror(errno) << "' (errno = " << errno << ")" << endl;
|
---|
116 | continue;
|
---|
117 | }
|
---|
118 |
|
---|
119 | //
|
---|
120 | // if n==0 something strange happened. Stop receiver(?)
|
---|
121 | //
|
---|
122 | if (n == 0)
|
---|
123 | {
|
---|
124 | cerr << "Vmodican: Panic read '" << strerror(errno) << "' ";
|
---|
125 | cerr << "(errno=" << errno << ")" << endl;
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | //
|
---|
130 | // Check for what we received
|
---|
131 | //
|
---|
132 | switch (c)
|
---|
133 | {
|
---|
134 | //
|
---|
135 | // Fast message (not used/working)
|
---|
136 | //
|
---|
137 | /*
|
---|
138 | case FAST_QUEUE:
|
---|
139 | cout << "--> Fast Queue: " << flush;
|
---|
140 |
|
---|
141 | FastMessage fmsg;
|
---|
142 |
|
---|
143 | if (ReceiveFast(&fmsg) < 0)
|
---|
144 | return (void *)1;
|
---|
145 |
|
---|
146 | cout << "Fast msg ID " <<
|
---|
147 | (fmsg.data[0] << 3) + ((fmsg.data[1] >> 5) & 7) << ": " << flush;
|
---|
148 |
|
---|
149 | for(int i=0; i<16; i++)
|
---|
150 | cout << (int)*(((unsigned char *)(&fmsg))+i) << " " << flush;
|
---|
151 |
|
---|
152 | cout << endl;
|
---|
153 | continue;
|
---|
154 | */
|
---|
155 | //
|
---|
156 | // Plain Can Message to be processed
|
---|
157 | //
|
---|
158 | case PLAIN_QUEUE:
|
---|
159 |
|
---|
160 | Message msg;
|
---|
161 |
|
---|
162 | //
|
---|
163 | // Read the message from the card and process it
|
---|
164 | //
|
---|
165 | if (Receive(&msg) < 0)
|
---|
166 | return 1;
|
---|
167 |
|
---|
168 | HandleMessage(msg);
|
---|
169 | continue;
|
---|
170 | }
|
---|
171 |
|
---|
172 | cout << "Vmodican: read, Message c=" << (int)c << " unknown." << endl;
|
---|
173 | }
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // --------------------------------------------------------------------------
|
---|
178 | //
|
---|
179 | // This is can_recv from the Janz software.
|
---|
180 | //
|
---|
181 | // /* can_recv - receive a message from standard interface
|
---|
182 | // *
|
---|
183 | // * This function reads a whole message from the standard host interface of
|
---|
184 | // * a VMOD-ICAN.
|
---|
185 | // * The module is selected by the module number <fd>.
|
---|
186 | // *
|
---|
187 | // * The structure <pm> is filled with the received message.
|
---|
188 | // *
|
---|
189 | // * RETURNS:
|
---|
190 | // * The function returns the number of message received, or -1 when the
|
---|
191 | // * system call failed.
|
---|
192 | // * The return value therefore 0 determines, that no message was available to
|
---|
193 | // * be read: can_recv() does not block in such a case and therefore
|
---|
194 | // * can be used to poll a module for incoming messages.
|
---|
195 | // */
|
---|
196 | //
|
---|
197 | int VmodIcan::Receive(Message *pm) /* receive buffer */
|
---|
198 | {
|
---|
199 | struct dpm_rw_can_desc arg;
|
---|
200 |
|
---|
201 | arg.pm = pm;
|
---|
202 |
|
---|
203 | if (!Ioctl(DPM_READ_MBOX, &arg))
|
---|
204 | return -1;
|
---|
205 |
|
---|
206 | return arg.rval;
|
---|
207 | }
|
---|
208 |
|
---|
209 | // --------------------------------------------------------------------------
|
---|
210 | //
|
---|
211 | // This is can_recv_fast from the Janz software
|
---|
212 | //
|
---|
213 | // /* can_recv_fast - receive a message from layer2 interface
|
---|
214 | // *
|
---|
215 | // * This function reads a FastMessage from the layer2 fast message
|
---|
216 | // * interface of a VMOD-ICAN.
|
---|
217 | // * The module is selected by the file descriptor <fd>.
|
---|
218 | // *
|
---|
219 | // * The structure <pm> is filled with the received message.
|
---|
220 | // *
|
---|
221 | // * RETURNS:
|
---|
222 | // * The function returns -1 when the * system call failed.
|
---|
223 | // * The return value therefore 0 determines, that no message was available to
|
---|
224 | // * be read: can_recv_fast() does not block in such a case and therefore
|
---|
225 | // * can be used to poll a module for incoming messages.
|
---|
226 | // */
|
---|
227 | /*
|
---|
228 | int VmodIcan::ReceiveFast(FastMessage *pm)
|
---|
229 | {
|
---|
230 | struct dpm_write_fast_can_desc arg;
|
---|
231 |
|
---|
232 | arg.pm = pm;
|
---|
233 |
|
---|
234 | if (!Ioctl(DPM_READ_FAST_MBOX, &arg))
|
---|
235 | return -1;
|
---|
236 |
|
---|
237 | return arg.rval;
|
---|
238 | }
|
---|
239 | */
|
---|
240 |
|
---|
241 | // --------------------------------------------------------------------------
|
---|
242 | //
|
---|
243 | // This is IcWriteBtrBCAN from the Janz software
|
---|
244 | //
|
---|
245 | // /* IcWriteBtrBCAN - Set bit timing parameters
|
---|
246 | // *
|
---|
247 | // * Set bit timing parameters in CAN controller. May only be used if
|
---|
248 | // * CAN controller is in bus-off state. <btr> stores the bus-timing
|
---|
249 | // * parameters as required by the 82C200 controller. See the description
|
---|
250 | // * of the CBTRreq-service for possible values.
|
---|
251 | // *
|
---|
252 | // * BTR1 is stored in the upper byte of <btr> and BTR0 in the lower. Examples
|
---|
253 | // * are:
|
---|
254 | // * .CS
|
---|
255 | // * Baudrate btr Macro
|
---|
256 | // * 1Mbit 0x2300 BTR_1MB
|
---|
257 | // * 500kBit 0x1c00 BTR_500KB
|
---|
258 | // * 250kBit 0x1c01 BTR_250KB
|
---|
259 | // * 125kBit 0x1c03 BTR_125KB
|
---|
260 | // * 100kBit 0x34c7 BTR_100KB
|
---|
261 | // * 50kBit 0x34cf BTR_50KB
|
---|
262 | // * 20kBit 0x7fcf BTR_20KB
|
---|
263 | // * .CE
|
---|
264 | // *
|
---|
265 | // * SERVICE: CBTRreq
|
---|
266 | // *
|
---|
267 | // * NOTE:
|
---|
268 | // * Raw ICANOS version of the firmware only.
|
---|
269 | // */
|
---|
270 | //
|
---|
271 | void VmodIcan::SetBaudRate(int rate)
|
---|
272 | {
|
---|
273 | Message msg; /* buffer for module messages */
|
---|
274 |
|
---|
275 | int rateid;
|
---|
276 |
|
---|
277 | switch (rate)
|
---|
278 | {
|
---|
279 | case 1000:
|
---|
280 | rateid=BTR_1MB;
|
---|
281 | break;
|
---|
282 | case 500:
|
---|
283 | rateid=BTR_500KB;
|
---|
284 | break;
|
---|
285 | case 250:
|
---|
286 | rateid=BTR_250KB;
|
---|
287 | break;
|
---|
288 | case 125:
|
---|
289 | rateid=BTR_125KB;
|
---|
290 | break;
|
---|
291 | case 100:
|
---|
292 | rateid=BTR_100KB;
|
---|
293 | break;
|
---|
294 | case 50:
|
---|
295 | rateid=BTR_50KB;
|
---|
296 | break;
|
---|
297 | case 20:
|
---|
298 | rateid=BTR_20KB;
|
---|
299 | break;
|
---|
300 |
|
---|
301 | default:
|
---|
302 | cout << "Error: Wrong bit rate specified" << endl;
|
---|
303 | return;
|
---|
304 | }
|
---|
305 |
|
---|
306 | msg.cmd = M_BCAN_SET_BTR_req;
|
---|
307 |
|
---|
308 | msg.len = 4;
|
---|
309 |
|
---|
310 | msg.data[2] = word_to_lsb(rateid);
|
---|
311 | msg.data[3] = word_to_msb(rateid);
|
---|
312 |
|
---|
313 | while (!Send(&msg)); /* transmitt to module */
|
---|
314 |
|
---|
315 | gLog << "- Baudrate set to " << rate << "kbps" << endl;
|
---|
316 | }
|
---|
317 |
|
---|
318 | // --------------------------------------------------------------------------
|
---|
319 | //
|
---|
320 | // This is IcBusOnBCAN from the Janz software
|
---|
321 | //
|
---|
322 | // /* IcBusOnBCAN - switch CANbus controller to bus-on state
|
---|
323 | // *
|
---|
324 | // * Switch CAN controller bus-on. You will need to use this
|
---|
325 | // * function explicitly after you have connected yourself
|
---|
326 | // * to the module with can_open() (or ican_open() under DOS/WINDOWS).
|
---|
327 | // * This is because the module comes up in the bus-off state.
|
---|
328 | // *
|
---|
329 | // * SERVICE: CONreq
|
---|
330 | // *
|
---|
331 | // * NOTE:
|
---|
332 | // * Raw ICANOS version of the firmware only.
|
---|
333 | // */
|
---|
334 | //
|
---|
335 | void VmodIcan::EnableCanBusConnection()
|
---|
336 | {
|
---|
337 | Message msg; /* buffer for module messages */
|
---|
338 |
|
---|
339 | msg.cmd = M_BCAN_BUSON_req;
|
---|
340 | msg.len = 0;
|
---|
341 |
|
---|
342 | while (!Send(&msg));
|
---|
343 |
|
---|
344 | gLog << "- Controller connected to bus" << endl;
|
---|
345 | }
|
---|
346 |
|
---|
347 | // --------------------------------------------------------------------------
|
---|
348 | //
|
---|
349 | // This is ican2_init_fast_canfrom the Janz software
|
---|
350 | //
|
---|
351 | // /* ican2_init_fast_can - initialize fast can access for VMOD-ICAN
|
---|
352 | // *
|
---|
353 | // * By this function, the user may initialize and enable the fast
|
---|
354 | // * host interface (layer2 access) for a VMOD-ICAN module.
|
---|
355 | // *
|
---|
356 | // * The calling application can request <rbuffers> buffer elements in the queue
|
---|
357 | // * that sends data to the host and <wbuffers> buffer elements for the queue
|
---|
358 | // * that transports data to the module.
|
---|
359 | // *
|
---|
360 | // * NOTE:
|
---|
361 | // * Notice that the message filtering on the VMOD-ICAN has to be
|
---|
362 | // * set correctly, so that messages can be received through the fast
|
---|
363 | // * interface.
|
---|
364 | // *
|
---|
365 | // * CAVE AT:
|
---|
366 | // * The <rbuffers> and wbuffers> have no special limit, but the general
|
---|
367 | // * resources of the DPM must not be exceeded.
|
---|
368 | // * For the calculation you need to assume, that 16 buffers in one of the fast
|
---|
369 | // * interface queues take the same DPM space as 1 buffer in the standard
|
---|
370 | // * host interface.
|
---|
371 | // *
|
---|
372 | // * The user must use only one of the functions, either
|
---|
373 | // * ican2_init_fast_can or ican2_init_fast_can_prio
|
---|
374 | // *
|
---|
375 | // * RETURNS:
|
---|
376 | // * Zero if the operation performed successfully, or less than zero on error.
|
---|
377 | // */
|
---|
378 | //
|
---|
379 | int VmodIcan::EnableFastCan(int rbuffers, int wbuffers)
|
---|
380 | {
|
---|
381 | struct dpm_fast_can_desc hdp;
|
---|
382 |
|
---|
383 | hdp.tohost_len = rbuffers;
|
---|
384 | hdp.fromhost_len = wbuffers;
|
---|
385 |
|
---|
386 | if (!Ioctl(DPM_INIT_FAST_CAN, &hdp))
|
---|
387 | return -1;
|
---|
388 |
|
---|
389 | gLog << "- Fast Host Interface Enabled" << endl;
|
---|
390 |
|
---|
391 | return 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------------------
|
---|
395 | //
|
---|
396 | // This is IcWriteEwlBCAN from the Janz software
|
---|
397 | //
|
---|
398 | // /* IcWriteEwlBCAN - Set error warning limit
|
---|
399 | // *
|
---|
400 | // * Set error warning limit in CAN controller. If this limit is passed, the
|
---|
401 | // * user will get a CEVTind message stating an error interrupt. This type
|
---|
402 | // * of message will also occur if the both error counter again fall below
|
---|
403 | // * this limit.
|
---|
404 | // *
|
---|
405 | // * RESTRICTIONS:
|
---|
406 | // * Will only take effect if CAN controller is in bus-off state. Requires
|
---|
407 | // * an SJA1000 CANbus controller, and will be no-op for 82C200.
|
---|
408 | // *
|
---|
409 | // * SERVICE: CBCONFreq
|
---|
410 | // *
|
---|
411 | // * NOTE:
|
---|
412 | // * Raw ICANOS version of the firmware only.
|
---|
413 | // */
|
---|
414 | //
|
---|
415 | void VmodIcan::DisableCanBusConnection()
|
---|
416 | {
|
---|
417 | gLog << "- Disconnect VmodIcan module from Bus!" << endl;
|
---|
418 |
|
---|
419 | Message msg; /* buffer for module messages */
|
---|
420 |
|
---|
421 | msg.cmd = M_BCAN_BUSOFF_req;
|
---|
422 | msg.len = 0;
|
---|
423 |
|
---|
424 | while (!Send(&msg));
|
---|
425 |
|
---|
426 | gLog << "- VmodIcan disconnected." << endl;
|
---|
427 | }
|
---|
428 |
|
---|
429 | // --------------------------------------------------------------------------
|
---|
430 | //
|
---|
431 | // This is can_close from the Janz software
|
---|
432 | //
|
---|
433 | // /* can_close - close connection to a VMOD-ICAN module
|
---|
434 | // *
|
---|
435 | // * The function can be used to close a connection to a VMOD-ICAN
|
---|
436 | // * that has been established by a can_open() call.
|
---|
437 | // * The module has to be selected by the file descriptor <fd> which was
|
---|
438 | // * obtained when you did the can_open() call.
|
---|
439 | // *
|
---|
440 | // * When you call can_close, all the resources that were used by the driver
|
---|
441 | // * for communication are freed.
|
---|
442 | // *
|
---|
443 | // * The VMOD-ICAN module under question will be reseted, to make sure that
|
---|
444 | // * the communication with the host will stop. That means especially that
|
---|
445 | // * no further interrupt will occur and that the module will not longer be
|
---|
446 | // * active on the CANbus.
|
---|
447 | // *
|
---|
448 | // * RETURNS: N/A
|
---|
449 | // */
|
---|
450 | //
|
---|
451 | void VmodIcan::Close()
|
---|
452 | {
|
---|
453 | gLog << "- Closing device VmodIcan #" << (int)fd << endl;
|
---|
454 |
|
---|
455 | Message msg; /* disconnect message */
|
---|
456 |
|
---|
457 | msg.cmd = M_DISCONNECT;
|
---|
458 | msg.len = 0;
|
---|
459 |
|
---|
460 | while (!Send(&msg));
|
---|
461 |
|
---|
462 | close(fd);
|
---|
463 |
|
---|
464 | gLog << "- Device closed." << endl;
|
---|
465 | }
|
---|
466 |
|
---|
467 | // --------------------------------------------------------------------------
|
---|
468 | //
|
---|
469 | // Enable the fifo of the Janz card
|
---|
470 | // Allow VMOD to send messages through the fifo
|
---|
471 | //
|
---|
472 | int VmodIcan::EnableFifo()
|
---|
473 | {
|
---|
474 | Message msg; /* connect message */
|
---|
475 |
|
---|
476 | msg.cmd = M_CONNECT_INTR;
|
---|
477 | msg.len = 0;
|
---|
478 |
|
---|
479 | while (!Send(&msg));
|
---|
480 |
|
---|
481 | gLog << "- Fifo enabled" << endl;
|
---|
482 |
|
---|
483 | return TRUE;
|
---|
484 | }
|
---|
485 |
|
---|
486 | // --------------------------------------------------------------------------
|
---|
487 | //
|
---|
488 | // Reset the module
|
---|
489 | //
|
---|
490 | int VmodIcan::Reset()
|
---|
491 | {
|
---|
492 | const int rc = Ioctl(DPM_RESET, 0);
|
---|
493 |
|
---|
494 | gLog << "- Reset done." << endl;
|
---|
495 |
|
---|
496 | return rc;
|
---|
497 | }
|
---|
498 |
|
---|
499 | // --------------------------------------------------------------------------
|
---|
500 | //
|
---|
501 | // This is can_open from the Janz software
|
---|
502 | //
|
---|
503 | // /* can_open - open VMOD-ICAN device
|
---|
504 | // *
|
---|
505 | // * With this function call you open a VMOD-ICAN plugged
|
---|
506 | // * into a MODULbus carrier board for use. The module is
|
---|
507 | // * reseted and then initialized for communication to the host.
|
---|
508 | // *
|
---|
509 | // * A specific module is selected by it's device name (e.g. "/dev/dpm_01").
|
---|
510 | // */
|
---|
511 | //
|
---|
512 | int VmodIcan::Open(const char *devname) /* pathname of device */
|
---|
513 | {
|
---|
514 | fd = open(devname, O_RDONLY, 0);
|
---|
515 |
|
---|
516 | if (fd < 0)
|
---|
517 | {
|
---|
518 | gLog << "Error: Opening device '" << devname << "' (rc=" << fd << ")" << endl;
|
---|
519 | gLog << strerror(errno) << endl;
|
---|
520 | return FALSE;
|
---|
521 | }
|
---|
522 |
|
---|
523 | gLog << "- Device " << devname << " #" << fd << " open." << endl;
|
---|
524 |
|
---|
525 | return TRUE;
|
---|
526 | }
|
---|
527 |
|
---|
528 | // --------------------------------------------------------------------------
|
---|
529 | //
|
---|
530 | // This is ican2_select_hostif from the Janz software
|
---|
531 | //
|
---|
532 | // /* ican2_select_hostif - switch standard host interface to new style mode
|
---|
533 | // *
|
---|
534 | // * The routine ican2_select_hostif() can be used to switch a module from
|
---|
535 | // * the standard host interface to the new style mode. The module is selected
|
---|
536 | // * by the module number <fd>.
|
---|
537 | // *
|
---|
538 | // * The calling application can request <rbuffers> buffer for the communication
|
---|
539 | // * queue that sends data to the host and <wbuffers> buffer for the reverse
|
---|
540 | // * communication direction (normal priority queue). By this function the hi- and
|
---|
541 | // * low-prioritized message-queues which sends data to the module are initialized
|
---|
542 | // * to a length of 1.
|
---|
543 | // *
|
---|
544 | // * NOTE:
|
---|
545 | // * To notify the module of the new situation, the driver sends
|
---|
546 | // * a M_NEWHOSTIF message to the module. This is the last message to be
|
---|
547 | // * transfered through the old style host interface. Immediately after
|
---|
548 | // * sending this message, the library is switched to the new style mode.
|
---|
549 | // * Any messages that are sent by the module in this time gap, may be lost.
|
---|
550 | // * It is therefore not recommended to use this function when you wait
|
---|
551 | // * for messages from the module.
|
---|
552 | // *
|
---|
553 | // * The selection of the new host interface is not reversible. It will stay
|
---|
554 | // * until the next reset for the module occurs. This will probably occur
|
---|
555 | // * when you use can_close().
|
---|
556 | // *
|
---|
557 | // * HINTS:
|
---|
558 | // * When the new style mode is active, no more internal message buffering
|
---|
559 | // * on the module exists. That is whenever the module tries to send something
|
---|
560 | // * and cannot because the queue is full, this message will be dropped.
|
---|
561 | // * Thereby, when enabling the new style host interface you should create
|
---|
562 | // * enough buffers for the queue that sends to the host, to prevent the
|
---|
563 | // * loss of messages. If you loose messages, however you will be indicated
|
---|
564 | // * of that event by a MSGLOST messages (which will not be lost!).
|
---|
565 | // *
|
---|
566 | // * CAVE AT:
|
---|
567 | // * The parameters <rbuffers>, <wbuffers>, <wbuffers_hi> and <wbuffers_low>
|
---|
568 | // * must be greater than 0, less than 128, and the total sum must not
|
---|
569 | // * exceed 236. These parameters aren't checked by the driver!
|
---|
570 | // */
|
---|
571 | //
|
---|
572 | int VmodIcan::StdHost2NewStyle(int rbuffers, int wbuffers,
|
---|
573 | int wbuffers_hi, int wbuffers_low)
|
---|
574 | {
|
---|
575 | struct dpm_new_hostif_desc_prio hdp;
|
---|
576 |
|
---|
577 | hdp.tohost_len = rbuffers;
|
---|
578 | hdp.fromhost_len = wbuffers;
|
---|
579 | hdp.fromhost_hi_len = wbuffers_hi;
|
---|
580 | hdp.fromhost_low_len = wbuffers_low;
|
---|
581 |
|
---|
582 | Ioctl(DPM_INIT_NEW_HOSTIF_PRIO, &hdp);
|
---|
583 |
|
---|
584 | gLog << "- New style host interface enabled" << endl;
|
---|
585 |
|
---|
586 | return 0;
|
---|
587 | }
|
---|
588 |
|
---|
589 | // --------------------------------------------------------------------------
|
---|
590 | //
|
---|
591 | // This is can_send_hi from the Janz software
|
---|
592 | //
|
---|
593 | // /* can_send_hi - send message to standard host interface (high priority)
|
---|
594 | // *
|
---|
595 | // * This function performs the same action as can_send(), except it will
|
---|
596 | // * append message <pm> to the highest priority queue of the standard
|
---|
597 | // * host interface.
|
---|
598 | // *
|
---|
599 | // * NOTE:
|
---|
600 | // * Notice that the prioritized issue of the message take effect on the new style
|
---|
601 | // * mode of the standard host interface only.
|
---|
602 | // *
|
---|
603 | // * RETURNS:
|
---|
604 | // * The function returns the number of message send, or -1 when the system
|
---|
605 | // * call failed.
|
---|
606 | // * The return value 0 determines that no message could be send,
|
---|
607 | // * probably because there was no space in the targeted queue. can_send_hi()
|
---|
608 | // * does not block or retry in such a case, so you need to loop explicitly
|
---|
609 | // * until the message is send.
|
---|
610 | // */
|
---|
611 | //
|
---|
612 | /*
|
---|
613 | int VmodIcan::SendHi(Message *pm)
|
---|
614 | {
|
---|
615 | struct dpm_rw_can_desc arg;
|
---|
616 |
|
---|
617 | arg.pm = pm;
|
---|
618 |
|
---|
619 | if (!Ioctl(DPM_WRITE_MBOX_HI, &arg))
|
---|
620 | return FALSE;
|
---|
621 |
|
---|
622 | return arg.rval;
|
---|
623 | }
|
---|
624 | */
|
---|
625 |
|
---|
626 | // --------------------------------------------------------------------------
|
---|
627 | //
|
---|
628 | // This is can_send_low from the Janz software
|
---|
629 | //
|
---|
630 | // /* can_send_low - send message to standard host interface (low priority)
|
---|
631 | // *
|
---|
632 | // * This function performs the same action as can_send(), except it will
|
---|
633 | // * append message <pm> to the lowest priority queue of the standard
|
---|
634 | // * host interface.
|
---|
635 | // *
|
---|
636 | // * NOTE:
|
---|
637 | // * Notice that the prioritized issue of the message take effect on the new
|
---|
638 | // * style mode of the standard host interface only.
|
---|
639 | // *
|
---|
640 | // * RETURNS:
|
---|
641 | // * The function returns the number of message send, or -1 when the system
|
---|
642 | // * call failed.
|
---|
643 | // * The return value 0 determines that no message could be send,
|
---|
644 | // * probably because there was no space in the targeted queue. can_send_low()
|
---|
645 | // * does not block or retry in such a case, so you need to loop explicitly
|
---|
646 | // * until the message is send.
|
---|
647 | // *
|
---|
648 | // */
|
---|
649 | /*
|
---|
650 | int VmodIcan::SendLo(Message *pm)
|
---|
651 | {
|
---|
652 | struct dpm_rw_can_desc arg;
|
---|
653 |
|
---|
654 | arg.pm = pm;
|
---|
655 |
|
---|
656 | if (!Ioctl(DPM_WRITE_MBOX_LOW, &arg))
|
---|
657 | return FALSE;
|
---|
658 |
|
---|
659 | return arg.rval;
|
---|
660 | }
|
---|
661 | */
|
---|
662 |
|
---|
663 | // --------------------------------------------------------------------------
|
---|
664 | //
|
---|
665 | // This is can_send from the Janz software
|
---|
666 | //
|
---|
667 | // /* can_send - send message to standard host interface (mid priority)
|
---|
668 | // *
|
---|
669 | // * This function sends a complete message to the standard host interface
|
---|
670 | // * of a VMOD-ICAN.
|
---|
671 | // *
|
---|
672 | // * The message <pm> will be queued to the middle prioritized of the three
|
---|
673 | // * queues.
|
---|
674 | // *
|
---|
675 | // * RETURNS:
|
---|
676 | // * The function returns the number of message send, or -1 when the system
|
---|
677 | // * call failed.
|
---|
678 | // * The return value 0 determines that no message could be send,
|
---|
679 | // * probably because there was no space in the targeted queue. can_send()
|
---|
680 | // * does not block or retry in such a case, so you need to loop explicitly
|
---|
681 | // * until the message is send.
|
---|
682 | // */
|
---|
683 | //
|
---|
684 | int VmodIcan::Send(Message *pm) /* file descriptor, message to send */
|
---|
685 | {
|
---|
686 | struct dpm_rw_can_desc arg;
|
---|
687 |
|
---|
688 | arg.pm = pm;
|
---|
689 |
|
---|
690 | return Ioctl(DPM_WRITE_MBOX, &arg) ? arg.rval : 0;
|
---|
691 | }
|
---|
692 |
|
---|
693 | // --------------------------------------------------------------------------
|
---|
694 | //
|
---|
695 | // This is can_fast_send from the Janz software
|
---|
696 | //
|
---|
697 | // /* can_fast_send - send message to fast interface
|
---|
698 | // *
|
---|
699 | // * This function sends a message to the fast host interface (layer-2
|
---|
700 | // * interface) of a VMOD-ICAN. The module is selected by the module number
|
---|
701 | // * <fd>.
|
---|
702 | // * The message to be send will be given in the structure <pm>.
|
---|
703 | // *
|
---|
704 | // * The fast host interface needs to be established before can_fast_send()
|
---|
705 | // * can be used successfully.
|
---|
706 | // *
|
---|
707 | // * RETURNS:
|
---|
708 | // * The function returns 1 if can_fast_send() completed successful.
|
---|
709 | // * Otherwise the return value 0 determines that the message could not be send,
|
---|
710 | // * probably because there was no space in the DPM. The function
|
---|
711 | // * does not block or retry in such a case, so you need to loop explicitly
|
---|
712 | // * until the message is send.
|
---|
713 | // * The function returns -1 when the system-call itself failed.
|
---|
714 | // */
|
---|
715 | //
|
---|
716 | /*
|
---|
717 | int VmodIcan::Send(FastMessage *pm)
|
---|
718 | {
|
---|
719 | struct dpm_write_fast_can_desc arg;
|
---|
720 |
|
---|
721 | arg.pm = pm;
|
---|
722 |
|
---|
723 | if (!Ioctl(DPM_WRITE_FAST_CAN, &arg))
|
---|
724 | return FALSE;
|
---|
725 |
|
---|
726 | gLog << "done." << endl;
|
---|
727 |
|
---|
728 | return arg.rval;
|
---|
729 | }
|
---|
730 | */
|
---|
731 |
|
---|
732 | // --------------------------------------------------------------------------
|
---|
733 | //
|
---|
734 | // This is IcSetAfil from the Janz software
|
---|
735 | //
|
---|
736 | // /*
|
---|
737 | // * IcSetAfil - Set software acceptance filter mask
|
---|
738 | // *
|
---|
739 | // * Set software acceptance filtering.
|
---|
740 | // *
|
---|
741 | // * SERVICE: SetAfilMask
|
---|
742 | // */
|
---|
743 | //
|
---|
744 | void VmodIcan::DisableAllCobIds()
|
---|
745 | {
|
---|
746 | Message msg;
|
---|
747 |
|
---|
748 | msg.cmd = M_SET_AFIL;
|
---|
749 | msg.len = 5;
|
---|
750 |
|
---|
751 | msg.data[0] = word_to_lsb(0);
|
---|
752 | msg.data[1] = word_to_msb(0);
|
---|
753 | msg.data[2] = word_to_lsb(0x7ff); // 11 bit Cob-Ids
|
---|
754 | msg.data[3] = word_to_msb(0x7ff); // 11 bit Cob-Ids
|
---|
755 | msg.data[4] = 0;
|
---|
756 |
|
---|
757 | while (!Send(&msg));
|
---|
758 |
|
---|
759 | gLog << "- All CobIds disabled." << endl;
|
---|
760 | }
|
---|
761 |
|
---|
762 | // --------------------------------------------------------------------------
|
---|
763 | //
|
---|
764 | // This is IcSetAfil from the Janz software
|
---|
765 | //
|
---|
766 | // /*
|
---|
767 | // * IcSetAfil - Set software acceptance filter mask
|
---|
768 | // *
|
---|
769 | // * Set software acceptance filtering.
|
---|
770 | // *
|
---|
771 | // * SERVICE: SetAfilMask
|
---|
772 | // */
|
---|
773 | //
|
---|
774 | void VmodIcan::EnableCobId(WORD_t cobid, int flag)
|
---|
775 | {
|
---|
776 | Message msg;
|
---|
777 |
|
---|
778 | msg.cmd = M_SET_AFIL;
|
---|
779 | msg.len = 3;
|
---|
780 |
|
---|
781 | msg.data[0] = word_to_lsb(cobid);
|
---|
782 | msg.data[1] = word_to_msb(cobid);
|
---|
783 | msg.data[2] = (BYTE_t)(flag?3:0);
|
---|
784 |
|
---|
785 | while (!Send(&msg));
|
---|
786 | #ifdef EXPERT
|
---|
787 | gLog << "- CobId 0x" << hex << setfill('0') << setw(3) << cobid << " enabled." << endl;
|
---|
788 | #endif
|
---|
789 | }
|
---|
790 |
|
---|
791 | // --------------------------------------------------------------------------
|
---|
792 | //
|
---|
793 | // This is IcSendReqBCAN from the Janz software
|
---|
794 | //
|
---|
795 | // /*
|
---|
796 | // * IcSendReqBCAN - Send a CANbus message
|
---|
797 | // *
|
---|
798 | // * Issue request to send a CAN message. <Spec> controls whether to send with
|
---|
799 | // * or without spec/confirmation.
|
---|
800 | // * .CS
|
---|
801 | // * spec action
|
---|
802 | // * 0 send only
|
---|
803 | // * 1 send with confirmation to the host.
|
---|
804 | // * 2 send and echo message to the host.
|
---|
805 | // * 3 send and generate both echo and confirmation.
|
---|
806 | // * .CE
|
---|
807 | // *
|
---|
808 | // * SERVICE: CTXreq, CTXCreq, CTXEreq, CTXCEreq
|
---|
809 | // *
|
---|
810 | // * NOTE:
|
---|
811 | // * Raw ICANOS version of the firmware only.
|
---|
812 | // */
|
---|
813 | //
|
---|
814 | void VmodIcan::SendCanFrame(WORD_t cobid, BYTE_t m[8], BYTE_t rtr)
|
---|
815 | {
|
---|
816 | const WORD_t desc = MsgDescr(cobid, 8, rtr);
|
---|
817 |
|
---|
818 | Message msg;
|
---|
819 |
|
---|
820 | msg.cmd = M_BCAN_TX_req;
|
---|
821 |
|
---|
822 | msg.len = 12;
|
---|
823 | msg.data[0] = 0;
|
---|
824 | msg.data[1] = 0;
|
---|
825 | msg.data[2] = word_to_msb(desc);
|
---|
826 | msg.data[3] = word_to_lsb(desc);
|
---|
827 |
|
---|
828 | memcpy(&msg.data[4], m, 8);
|
---|
829 |
|
---|
830 | while (!Send(&msg));
|
---|
831 | }
|
---|
832 |
|
---|
833 | // --------------------------------------------------------------------------
|
---|
834 | //
|
---|
835 | // Constructor. Sets logging.
|
---|
836 | // Set the receiving thread to priority -10 and detached.
|
---|
837 | //
|
---|
838 | // Open the device.
|
---|
839 | // reset the device
|
---|
840 | // Enable the fifo buffers
|
---|
841 | // Set the baud rate to the given rate
|
---|
842 | // Disable passthrough of all cobids (all canbus messages)
|
---|
843 | // and switch the can bus communication on
|
---|
844 | //
|
---|
845 | VmodIcan::VmodIcan(CanOpen *rx, const char *dev, const int baud) : Interface(rx)
|
---|
846 | {
|
---|
847 | //
|
---|
848 | // Set priority of receiving thread and detach the receiving thread
|
---|
849 | //
|
---|
850 | // Detach();
|
---|
851 |
|
---|
852 | if (!Open(dev))
|
---|
853 | {
|
---|
854 | cout << "Cannot open device '" << dev << "'... exit." << endl;
|
---|
855 | cout << strerror(errno) << endl;
|
---|
856 | return;
|
---|
857 | }
|
---|
858 |
|
---|
859 | Reset();
|
---|
860 | EnableFifo(); // connect to host (in interrupt mode)
|
---|
861 | SetBaudRate(baud); // set baud rate
|
---|
862 | DisableAllCobIds();
|
---|
863 | EnableCanBusConnection(); // connect to bus
|
---|
864 | /*
|
---|
865 | StdHost2NewStyle(50, 50, 50, 50); // set host style
|
---|
866 | EnableFastCan(50, 50);
|
---|
867 | SetTermination(0);
|
---|
868 | */
|
---|
869 |
|
---|
870 | gLog << "- VmodIcan initialized." << endl;
|
---|
871 |
|
---|
872 | }
|
---|
873 |
|
---|
874 | // --------------------------------------------------------------------------
|
---|
875 | //
|
---|
876 | // Destructor. Stopt the receiver, disables the bus connection and
|
---|
877 | // close the device
|
---|
878 | //
|
---|
879 | VmodIcan::~VmodIcan()
|
---|
880 | {
|
---|
881 | gLog << "- Stopping VmodIcan module." << endl;
|
---|
882 | CancelThread();
|
---|
883 | DisableCanBusConnection();
|
---|
884 | Close();
|
---|
885 | gLog << "- VmodIcan stopped." << endl;
|
---|
886 | }
|
---|