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