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

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