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

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