Changeset 8831 for trunk/MagicSoft/Cosy/candrv
- Timestamp:
- 01/24/08 11:54:29 (17 years ago)
- Location:
- trunk/MagicSoft/Cosy/candrv
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/candrv/vmodican.cc
r4105 r8831 16 16 ! 17 17 ! 18 ! Author(s): Thomas Bretz <mailto:tbretz@uni-sw.gwdg.de>, 200118 ! Author(s): Thomas Bretz 2001 <mailto:tbretz@astro.uni-wuerzburg.de> 19 19 ! 20 ! Copyright: MAGIC Software Development, 2000-200 120 ! Copyright: MAGIC Software Development, 2000-2008 21 21 ! 22 22 ! … … 31 31 /////////////////////////////////////////////////////////////////////// 32 32 #include "vmodican.h" 33 34 #include <iostream.h> // cout35 #include <iomanip.h> // setw, setfill36 33 37 34 #include <fcntl.h> // O_RDONLY … … 41 38 #include <sys/ioctl.h> // ioctl 42 39 40 #include "canopen.h" 41 42 #include "MLog.h" 43 43 44 ClassImp(VmodIcan); 44 45 45 // -------------------------------------------------------------------------- 46 // 47 // Prints a CAN Message. 48 // 49 void 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 } 46 using namespace std; 59 47 60 48 // -------------------------------------------------------------------------- … … 64 52 int VmodIcan::Ioctl(int msg, void *arg) 65 53 { 66 return fd<0 ? 1 : ioctl(fd, msg, (int)arg) >= 0;54 return fd<0 ? 1 : ioctl(fd, msg, arg) >= 0; 67 55 } 68 56 … … 93 81 while (!Send(&msg)); 94 82 95 lout<< "- CAN Bus Termination set to " << (state?"on":"off") << endl;83 gLog << "- CAN Bus Termination set to " << (state?"on":"off") << endl; 96 84 } 97 85 … … 102 90 // - therefor overload HandleCanMessge 103 91 // 104 void *VmodIcan::Thread()92 Int_t VmodIcan::Thread() 105 93 { 106 94 if (fd<0) 107 return NULL;108 109 lout<< "- Starting Receiver Loop." << endl;95 return 0; 96 97 gLog << "- Starting Receiver Loop." << endl; 110 98 111 99 while (1) … … 114 102 // Sleeps until a message arrives 115 103 // 104 TThread::CancelPoint(); 105 116 106 unsigned char c; 117 107 const int n = read(fd, &c, 1); 108 109 TThread::CancelPoint(); 110 118 111 if (n<0) 119 112 { … … 125 118 126 119 // 127 // read the time for the message as soon as possible128 //129 timeval_t tv;130 gettimeofday(&tv, NULL);131 132 //133 120 // if n==0 something strange happened. Stop receiver(?) 134 121 // … … 137 124 cerr << "Vmodican: Panic read '" << strerror(errno) << "' "; 138 125 cerr << "(errno=" << errno << ")" << endl; 139 return (void *)1;126 return 1; 140 127 } 141 128 … … 148 135 // Fast message (not used/working) 149 136 // 137 /* 150 138 case FAST_QUEUE: 151 139 cout << "--> Fast Queue: " << flush; … … 164 152 cout << endl; 165 153 continue; 166 154 */ 167 155 // 168 156 // Plain Can Message to be processed … … 176 164 // 177 165 if (Receive(&msg) < 0) 178 return (void *)1;179 180 HandleMessage( &msg, &tv);166 return 1; 167 168 HandleMessage(msg); 181 169 continue; 182 170 } … … 184 172 cout << "Vmodican: read, Message c=" << (int)c << " unknown." << endl; 185 173 } 186 return NULL; 187 } 188 189 // -------------------------------------------------------------------------- 190 // 191 // Does a basic message processing and hadles the so called command (cmd) 192 // stamp of the message. This is some kind of message type which is send 193 // by the can interface 194 // 195 void VmodIcan::HandleMessage(Message *msg, timeval_t *tv) 196 { 197 // 198 // Decode message 199 // 200 const WORD_t desc = msg->data[2]<<8 | msg->data[3]; 201 const BYTE_t rtr = (desc>>4)&1; 202 const BYTE_t len = desc&0xf; 203 const WORD_t cobid = desc>>5; 204 205 switch (msg->cmd) // FROM mican.h 206 { 207 case M_MSG_LOST: 208 cout << "VmodIcan reports: " << dec << (int)msg->data[0] << " msg(s) lost!" << endl; 209 return; 210 211 case M_BCAN_TX_con: /* confirm (+/-) transmission */ 212 cout << "VmodIcan reports: CTXcon=0x35" << endl; 213 cout << "This normally means, that the transmission of the following CAN frame failed:" << hex << endl; 214 cout << "Descr: 0x" << cobid << dec; 215 cout << " Rtr: " << (rtr?"Yes":"No"); 216 cout << " Len: " << (int)len << endl; 217 return; 218 219 case M_BCAN_EVENT_ind: 220 cout << "VmodIcan reports: CEVTind=0x37: " << hex; 221 switch (msg->data[0]) // error indicator 222 { 223 case 0x01: 224 cout << "Error interrup occured" << endl; 225 cout << "This means noisy network normally. Please check the bus termination." << endl; 226 switch (msg->data[1]) // msg type (board depending) 227 { 228 case 2: // SJA1000 229 cout << dec; 230 cout << "ModeReg=" << (int)msg->data[2] << ", "; 231 cout << "StatReg=" << (int)msg->data[3] << ", "; 232 cout << "RxErrCnt=" << (int)msg->data[4] << ", "; 233 cout << "TxErrCnt=" << (int)msg->data[5] << endl; 234 } 235 //FIXME? TerminateApp(); 236 return; 237 case 0x02: 238 cout << "Overrun interrup occured" << endl; 239 return; 240 case 0x04: 241 cout << "Interrupts lost" << endl; 242 return; 243 case 0x08: 244 cout << "Send queue full" << endl; 245 return; 246 case 0x10: 247 cout << "CANbus bus-error" << endl; 248 return; 249 } 250 return; 251 case M_BCAN_RX_ind: 252 // 253 // Message is a message from the Can bus 254 // 255 HandleCanMessage(cobid, &msg->data[4], tv); 256 return; 257 } 258 259 // 260 // Nothing of the above happened 261 // 262 cout << hex; 263 cout << "Cmd=0x" << (int)msg->cmd << ": "; 264 cout << "Descr: 0x" << cobid << dec; 265 cout << " Rtr: " << (rtr?"Yes":"No"); 266 cout << " Len: " << (int)len << endl; 267 268 cout << "As Raw Data:" << hex << setfill('0'); 269 for (int i=0; i<msg->len; i++) 270 cout << " " << setw(2) << (int)(msg->data[i]) << flush; 271 cout << endl; 174 return 0; 272 175 } 273 176 … … 294 197 int VmodIcan::Receive(Message *pm) /* receive buffer */ 295 198 { 296 297 199 struct dpm_rw_can_desc arg; 298 200 … … 323 225 // * can be used to poll a module for incoming messages. 324 226 // */ 325 / /227 /* 326 228 int VmodIcan::ReceiveFast(FastMessage *pm) 327 229 { … … 335 237 return arg.rval; 336 238 } 239 */ 337 240 338 241 // -------------------------------------------------------------------------- … … 410 313 while (!Send(&msg)); /* transmitt to module */ 411 314 412 lout<< "- Baudrate set to " << rate << "kbps" << endl;315 gLog << "- Baudrate set to " << rate << "kbps" << endl; 413 316 } 414 317 … … 439 342 while (!Send(&msg)); 440 343 441 lout<< "- Controller connected to bus" << endl;344 gLog << "- Controller connected to bus" << endl; 442 345 } 443 346 … … 484 387 return -1; 485 388 486 lout<< "- Fast Host Interface Enabled" << endl;389 gLog << "- Fast Host Interface Enabled" << endl; 487 390 488 391 return 0; … … 512 415 void VmodIcan::DisableCanBusConnection() 513 416 { 514 lout<< "- Disconnect VmodIcan module from Bus!" << endl;417 gLog << "- Disconnect VmodIcan module from Bus!" << endl; 515 418 516 419 Message msg; /* buffer for module messages */ … … 521 424 while (!Send(&msg)); 522 425 523 lout<< "- VmodIcan disconnected." << endl;426 gLog << "- VmodIcan disconnected." << endl; 524 427 } 525 428 … … 548 451 void VmodIcan::Close() 549 452 { 550 lout<< "- Closing device VmodIcan #" << (int)fd << endl;453 gLog << "- Closing device VmodIcan #" << (int)fd << endl; 551 454 552 455 Message msg; /* disconnect message */ … … 559 462 close(fd); 560 463 561 lout<< "- Device closed." << endl;464 gLog << "- Device closed." << endl; 562 465 } 563 466 … … 576 479 while (!Send(&msg)); 577 480 578 lout<< "- Fifo enabled" << endl;481 gLog << "- Fifo enabled" << endl; 579 482 580 483 return TRUE; … … 589 492 const int rc = Ioctl(DPM_RESET, 0); 590 493 591 lout<< "- Reset done." << endl;494 gLog << "- Reset done." << endl; 592 495 593 496 return rc; … … 613 516 if (fd < 0) 614 517 { 615 lout<< "Error: Opening device '" << devname << "' (rc=" << fd << ")" << endl;616 lout<< strerror(errno) << endl;518 gLog << "Error: Opening device '" << devname << "' (rc=" << fd << ")" << endl; 519 gLog << strerror(errno) << endl; 617 520 return FALSE; 618 521 } 619 522 620 lout<< "- Device " << devname << " #" << fd << " open." << endl;523 gLog << "- Device " << devname << " #" << fd << " open." << endl; 621 524 622 525 return TRUE; … … 679 582 Ioctl(DPM_INIT_NEW_HOSTIF_PRIO, &hdp); 680 583 681 lout<< "- New style host interface enabled" << endl;584 gLog << "- New style host interface enabled" << endl; 682 585 683 586 return 0; … … 707 610 // */ 708 611 // 612 /* 709 613 int VmodIcan::SendHi(Message *pm) 710 614 { … … 718 622 return arg.rval; 719 623 } 624 */ 720 625 721 626 // -------------------------------------------------------------------------- … … 742 647 // * 743 648 // */ 649 /* 744 650 int VmodIcan::SendLo(Message *pm) 745 651 { … … 753 659 return arg.rval; 754 660 } 661 */ 755 662 756 663 // -------------------------------------------------------------------------- … … 807 714 // */ 808 715 // 809 int VmodIcan::Send(FastMessage *pm) /* file descriptor, message to send */ 716 /* 717 int VmodIcan::Send(FastMessage *pm) 810 718 { 811 719 struct dpm_write_fast_can_desc arg; … … 816 724 return FALSE; 817 725 818 lout<< "done." << endl;726 gLog << "done." << endl; 819 727 820 728 return arg.rval; 821 729 } 730 */ 822 731 823 732 // -------------------------------------------------------------------------- … … 848 757 while (!Send(&msg)); 849 758 850 lout<< "- All CobIds disabled." << endl;759 gLog << "- All CobIds disabled." << endl; 851 760 } 852 761 … … 876 785 while (!Send(&msg)); 877 786 #ifdef EXPERT 878 lout<< "- CobId 0x" << hex << setfill('0') << setw(3) << cobid << " enabled." << endl;787 gLog << "- CobId 0x" << hex << setfill('0') << setw(3) << cobid << " enabled." << endl; 879 788 #endif 880 789 } … … 934 843 // and switch the can bus communication on 935 844 // 936 VmodIcan::VmodIcan( const char *dev, const int baud, MLog &out) : Log(out), MThread(false, 1)//: CanDriver(dev, baud)845 VmodIcan::VmodIcan(CanOpen *rx, const char *dev, const int baud) : Interface(rx) 937 846 { 938 847 // 939 848 // Set priority of receiving thread and detach the receiving thread 940 849 // 941 Detach();850 // Detach(); 942 851 943 852 if (!Open(dev)) … … 959 868 */ 960 869 961 lout<< "- VmodIcan initialized." << endl;870 gLog << "- VmodIcan initialized." << endl; 962 871 963 872 } … … 970 879 VmodIcan::~VmodIcan() 971 880 { 972 lout<< "- Stopping VmodIcan module." << endl;973 Stop();881 gLog << "- Stopping VmodIcan module." << endl; 882 CancelThread(); 974 883 DisableCanBusConnection(); 975 884 Close(); 976 lout << "- VmodIcan stopped." << endl; 977 } 978 979 // -------------------------------------------------------------------------- 980 // 981 // Sends a can frame with the given cobid and the given eight bytes 982 // through the can network 983 // 984 void VmodIcan::SendCanFrame(WORD_t cobid, 985 BYTE_t m0, BYTE_t m1, BYTE_t m2, BYTE_t m3, 986 BYTE_t m4, BYTE_t m5, BYTE_t m6, BYTE_t m7) 987 { 988 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 }; 989 SendCanFrame(cobid, msg); 990 } 885 gLog << "- VmodIcan stopped." << endl; 886 } -
trunk/MagicSoft/Cosy/candrv/vmodican.h
r4105 r8831 1 1 #ifndef COSY_VmodIcan 2 2 #define COSY_VmodIcan 3 4 #include <pthread.h>5 6 #include <unistd.h> // gettimeofday7 8 #include "log.h"9 3 10 4 #ifdef __CINT__ … … 12 6 typedef UShort_t WORD_t; 13 7 typedef Byte_t BYTE_t; 8 struct Message; 9 struct FastMessage; 14 10 #else 15 11 #include "gendef.h" 12 #include "dpm.h" 16 13 #endif 17 14 18 #ifndef COSY_MThread15 #ifndef MARS_MThread 19 16 #include "MThread.h" 20 17 #endif 21 18 22 class Message; 23 class FastMessage; 19 #ifndef COSY_Interface 20 #include "interface.h" 21 #endif 24 22 25 23 typedef struct timeval timeval_t; 26 24 27 class VmodIcan : public Log, protected MThread 25 class CanOpen; 26 27 class VmodIcan : public Interface, public MyThreadX 28 28 { 29 friend class VmodIcanRX;30 31 29 private: 32 30 int fd; // file descriptor for can module 33 31 32 // Specific tp the VmodIcan module 34 33 int Ioctl(int msg, void *arg); 35 36 void *Thread();37 38 void HandleMessage(Message *msg, timeval_t *tv);39 virtual void HandleCanMessage(WORD_t cobid, BYTE_t *data, timeval_t *tv) {}40 41 int Receive(Message *pm);42 int ReceiveFast(FastMessage *pm);43 34 44 35 void SetBaudRate(int rate); … … 62 53 int Open(const char *devname); /* pathname of device */ 63 54 64 WORD_t MsgDescr(const WORD_t cobid, const BYTE_t dlc, const BYTE_t rtr=0)65 {66 return (cobid<<5) | ((rtr&0x1)<<4) | (dlc&0xf);67 }55 // Receiver 56 int Receive(Message *pm); 57 //int ReceiveFast(FastMessage *pm); 58 Int_t Thread(); 68 59 69 virtual void TerminateApp() { exit(-1); } 60 void Start() { RunThread(); } 61 void Stop() { CancelThread(); } 70 62 71 63 public: 72 VmodIcan( const char *dev, const int baud, MLog &out=gLog);64 VmodIcan(CanOpen *rx, const char *dev, const int baud); 73 65 virtual ~VmodIcan(); 74 66 67 // Sending interface inherited from Interface 75 68 void EnableCobId(WORD_t cobid, int flag=TRUE); 76 77 69 void SendCanFrame(WORD_t cobid, BYTE_t m[8], BYTE_t rtr=0); 78 void SendCanFrame(WORD_t cobid,79 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,80 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0);81 70 82 71 ClassDef(VmodIcan, 0) // hardware interface to the vmodican can module (Janz)
Note:
See TracChangeset
for help on using the changeset viewer.