Changeset 8831 for trunk/MagicSoft/Cosy


Ignore:
Timestamp:
01/24/08 11:54:29 (17 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Cosy/candrv
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Cosy/candrv/vmodican.cc

    r4105 r8831  
    1616!
    1717!
    18 !   Author(s): Thomas Bretz <mailto:tbretz@uni-sw.gwdg.de>, 2001
     18!   Author(s): Thomas Bretz 2001 <mailto:tbretz@astro.uni-wuerzburg.de>
    1919!
    20 !   Copyright: MAGIC Software Development, 2000-2001
     20!   Copyright: MAGIC Software Development, 2000-2008
    2121!
    2222!
     
    3131///////////////////////////////////////////////////////////////////////
    3232#include "vmodican.h"
    33 
    34 #include <iostream.h>      // cout
    35 #include <iomanip.h>       // setw, setfill
    3633
    3734#include <fcntl.h>         // O_RDONLY
     
    4138#include <sys/ioctl.h>     // ioctl
    4239
     40#include "canopen.h"
     41
     42#include "MLog.h"
     43
    4344ClassImp(VmodIcan);
    4445
    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 }
     46using namespace std;
    5947
    6048// --------------------------------------------------------------------------
     
    6452int VmodIcan::Ioctl(int msg, void *arg)
    6553{
    66     return fd<0 ? 1 : ioctl(fd, msg, (int)arg) >= 0;
     54    return fd<0 ? 1 : ioctl(fd, msg, arg) >= 0;
    6755}
    6856
     
    9381    while (!Send(&msg));
    9482
    95     lout << "- CAN Bus Termination set to " << (state?"on":"off") << endl;
     83    gLog << "- CAN Bus Termination set to " << (state?"on":"off") << endl;
    9684}
    9785
     
    10290//  - therefor overload HandleCanMessge
    10391//
    104 void *VmodIcan::Thread()
     92Int_t VmodIcan::Thread()
    10593{
    10694    if (fd<0)
    107         return NULL;
    108 
    109     lout << "- Starting Receiver Loop." << endl;
     95        return 0;
     96
     97    gLog << "- Starting Receiver Loop." << endl;
    11098
    11199    while (1)
     
    114102        // Sleeps until a message arrives
    115103        //
     104        TThread::CancelPoint();
     105
    116106        unsigned char c;
    117107        const int n = read(fd, &c, 1);
     108
     109        TThread::CancelPoint();
     110
    118111        if (n<0)
    119112        {
     
    125118
    126119        //
    127         // read the time for the message as soon as possible
    128         //
    129         timeval_t tv;
    130         gettimeofday(&tv, NULL);
    131 
    132         //
    133120        // if n==0 something strange happened. Stop receiver(?)
    134121        //
     
    137124            cerr << "Vmodican: Panic read '" << strerror(errno) << "' ";
    138125            cerr << "(errno=" << errno << ")" << endl;
    139             return (void *)1;
     126            return 1;
    140127        }
    141128
     
    148135            // Fast message (not used/working)
    149136            //
     137            /*
    150138        case FAST_QUEUE:
    151139            cout << "--> Fast Queue:  " << flush;
     
    164152            cout << endl;
    165153            continue;
    166 
     154            */
    167155        //
    168156        // Plain Can Message to be processed
     
    176164            //
    177165            if (Receive(&msg) < 0)
    178                 return (void *)1;
    179 
    180             HandleMessage(&msg, &tv);
     166                return 1;
     167
     168            HandleMessage(msg);
    181169            continue;
    182170        }
     
    184172        cout << "Vmodican: read, Message c=" << (int)c << " unknown." << endl;
    185173    }
    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;
    272175}
    273176
     
    294197int VmodIcan::Receive(Message *pm) /* receive buffer */
    295198{
    296 
    297199    struct dpm_rw_can_desc arg;
    298200
     
    323225//   * can be used to poll a module for incoming messages.
    324226//   */
    325 //
     227/*
    326228int VmodIcan::ReceiveFast(FastMessage *pm)
    327229{
     
    335237    return arg.rval;
    336238}
     239*/
    337240
    338241// --------------------------------------------------------------------------
     
    410313    while (!Send(&msg));      /* transmitt to module */
    411314
    412     lout << "- Baudrate set to " << rate << "kbps" << endl;
     315    gLog << "- Baudrate set to " << rate << "kbps" << endl;
    413316}
    414317
     
    439342    while (!Send(&msg));
    440343
    441     lout << "- Controller connected to bus" << endl;
     344    gLog << "- Controller connected to bus" << endl;
    442345}
    443346
     
    484387        return -1;
    485388
    486     lout << "- Fast Host Interface Enabled" << endl;
     389    gLog << "- Fast Host Interface Enabled" << endl;
    487390
    488391    return 0;
     
    512415void VmodIcan::DisableCanBusConnection()
    513416{
    514     lout << "- Disconnect VmodIcan module from Bus!" << endl;
     417    gLog << "- Disconnect VmodIcan module from Bus!" << endl;
    515418
    516419    Message msg;                  /* buffer for module messages */
     
    521424    while (!Send(&msg));
    522425
    523     lout << "- VmodIcan disconnected." << endl;
     426    gLog << "- VmodIcan disconnected." << endl;
    524427}
    525428
     
    548451void VmodIcan::Close()
    549452{
    550     lout << "- Closing device VmodIcan #" << (int)fd << endl;
     453    gLog << "- Closing device VmodIcan #" << (int)fd << endl;
    551454
    552455    Message msg; /* disconnect message */
     
    559462    close(fd);
    560463
    561     lout << "- Device closed." << endl;
     464    gLog << "- Device closed." << endl;
    562465}
    563466
     
    576479    while (!Send(&msg));
    577480
    578     lout << "- Fifo enabled" << endl;
     481    gLog << "- Fifo enabled" << endl;
    579482
    580483    return TRUE;
     
    589492    const int rc = Ioctl(DPM_RESET, 0);
    590493
    591     lout << "- Reset done." << endl;
     494    gLog << "- Reset done." << endl;
    592495
    593496    return rc;
     
    613516    if (fd < 0)
    614517    {
    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;
    617520        return FALSE;
    618521    }
    619522
    620     lout << "- Device " << devname << " #" << fd << " open." << endl;
     523    gLog << "- Device " << devname << " #" << fd << " open." << endl;
    621524
    622525    return TRUE;
     
    679582    Ioctl(DPM_INIT_NEW_HOSTIF_PRIO, &hdp);
    680583
    681     lout << "- New style host interface enabled" << endl;
     584    gLog << "- New style host interface enabled" << endl;
    682585
    683586    return 0;
     
    707610//   */
    708611//
     612/*
    709613int VmodIcan::SendHi(Message *pm)
    710614{
     
    718622    return arg.rval;
    719623}
     624*/
    720625
    721626// --------------------------------------------------------------------------
     
    742647//   *
    743648//   */
     649/*
    744650int VmodIcan::SendLo(Message *pm)
    745651{
     
    753659    return arg.rval;
    754660}
     661*/
    755662
    756663// --------------------------------------------------------------------------
     
    807714//   */
    808715//
    809 int VmodIcan::Send(FastMessage *pm) /* file descriptor, message to send */
     716/*
     717int VmodIcan::Send(FastMessage *pm)
    810718{
    811719    struct dpm_write_fast_can_desc arg;
     
    816724        return FALSE;
    817725
    818     lout << "done." << endl;
     726    gLog << "done." << endl;
    819727
    820728    return arg.rval;
    821729}
     730*/
    822731
    823732// --------------------------------------------------------------------------
     
    848757    while (!Send(&msg));
    849758
    850     lout << "- All CobIds disabled." << endl;
     759    gLog << "- All CobIds disabled." << endl;
    851760}
    852761
     
    876785    while (!Send(&msg));
    877786#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;
    879788#endif
    880789}
     
    934843//  and switch the can bus communication on
    935844//
    936 VmodIcan::VmodIcan(const char *dev, const int baud, MLog &out) : Log(out), MThread(false, 1)//: CanDriver(dev, baud)
     845VmodIcan::VmodIcan(CanOpen *rx, const char *dev, const int baud) : Interface(rx)
    937846{
    938847    //
    939848    // Set priority of receiving thread and detach the receiving thread
    940849    //
    941     Detach();
     850//    Detach();
    942851
    943852    if (!Open(dev))
     
    959868     */
    960869
    961     lout << "- VmodIcan initialized." << endl;
     870    gLog << "- VmodIcan initialized." << endl;
    962871
    963872}
     
    970879VmodIcan::~VmodIcan()
    971880{
    972     lout << "- Stopping VmodIcan module." << endl;
    973     Stop();
     881    gLog << "- Stopping VmodIcan module." << endl;
     882    CancelThread();
    974883    DisableCanBusConnection();
    975884    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  
    11#ifndef COSY_VmodIcan
    22#define COSY_VmodIcan
    3 
    4 #include <pthread.h>
    5 
    6 #include <unistd.h>          // gettimeofday
    7 
    8 #include "log.h"
    93
    104#ifdef __CINT__
     
    126typedef UShort_t WORD_t;
    137typedef Byte_t   BYTE_t;
     8struct Message;
     9struct FastMessage;
    1410#else
    1511#include "gendef.h"
     12#include "dpm.h"
    1613#endif
    1714
    18 #ifndef COSY_MThread
     15#ifndef MARS_MThread
    1916#include "MThread.h"
    2017#endif
    2118
    22 class Message;
    23 class FastMessage;
     19#ifndef COSY_Interface
     20#include "interface.h"
     21#endif
    2422
    2523typedef struct timeval timeval_t;
    2624
    27 class VmodIcan : public Log, protected MThread
     25class CanOpen;
     26
     27class VmodIcan : public Interface, public MyThreadX
    2828{
    29     friend class VmodIcanRX;
    30 
    3129private:
    3230    int fd; // file descriptor for can module
    3331
     32    // Specific tp the VmodIcan module
    3433    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);
    4334
    4435    void SetBaudRate(int rate);
     
    6253    int Open(const char *devname);          /* pathname of device */
    6354
    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();
    6859
    69     virtual void TerminateApp() { exit(-1); }
     60    void Start() { RunThread(); }
     61    void Stop()  { CancelThread(); }
    7062
    7163public:
    72     VmodIcan(const char *dev, const int baud, MLog &out=gLog);
     64    VmodIcan(CanOpen *rx, const char *dev, const int baud);
    7365    virtual ~VmodIcan();
    7466
     67    // Sending interface inherited from Interface
    7568    void EnableCobId(WORD_t cobid, int flag=TRUE);
    76 
    7769    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);
    8170
    8271    ClassDef(VmodIcan, 0) // hardware interface to the vmodican can module (Janz)
Note: See TracChangeset for help on using the changeset viewer.