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