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

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