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

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