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