source: trunk/MagicSoft/Cosy/candrv/vmodican.cc@ 7551

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