tool/CommandCrc.cpp
author Florian Pose <fp@igh-essen.com>
Fri, 03 Mar 2017 13:33:17 +0100
branchstable-1.5
changeset 2666 d9e14a0ddbd6
child 2667 0d7936701e60
permissions -rw-r--r--
New tool command 'ethercat crc'.
2666
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  Copyright (C) 2006-2017  Florian Pose, Ingenieurgemeinschaft IgH
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  This file is part of the IgH EtherCAT Master.
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *  modify it under the terms of the GNU General Public License version 2, as
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  published by the Free Software Foundation.
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  Public License for more details.
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  You should have received a copy of the GNU General Public License along
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  with the IgH EtherCAT Master; if not, write to the Free Software
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  ---
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  The license mentioned above concerns the source code only. Using the
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  EtherCAT technology and brand is only permitted in compliance with the
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  industrial property and similar rights of Beckhoff Automation GmbH.
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 ****************************************************************************/
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
#include <iostream>
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
#include <iomanip>
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
using namespace std;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include "CommandCrc.h"
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include "MasterDevice.h"
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
/*****************************************************************************/
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
CommandCrc::CommandCrc():
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
    Command("crc", "CRC error register diagnosis.")
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
{
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
}
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
/*****************************************************************************/
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
string CommandCrc::helpString(const string &binaryBaseName) const
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
{
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
    stringstream str;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
    str << binaryBaseName << " " << getName()
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
        << " [OPTIONS]" << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
        << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
        << getBriefDescription() << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
        << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
        << "CRC - CRC Error Counter                0x300, 0x302, 0x304, 0x306"
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
        << "PHY - Physical Interface Error Counter 0x301, 0x303, 0x305, 0x307"
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
        << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
        << "FWD - Forwarded RX Error Counter       0x308, 0x309, 0x30a, 0x30b"
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
        << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
        << "LNK - Lost Link Counter                0x310, 0x311, 0x312, 0x313"
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        << endl
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
        << endl;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    return str.str();
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
}
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
/****************************************************************************/
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
#define NUM_PORTS (4)
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
void CommandCrc::execute(const StringVector &args)
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
{
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
    MasterDevice m(getSingleMasterIndex());
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
    m.open(MasterDevice::Read);
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
    ec_ioctl_master_t master;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
    m.getMaster(&master);
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
    cout << "   |";
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    for (unsigned int port = 0; port < NUM_PORTS; port++) {
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
        cout << "Port " << port << "         |";
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
    }
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    cout << endl;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
    cout << "   |";
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
    for (unsigned int port = 0; port < NUM_PORTS; port++) {
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
        cout << "CRC PHY FWD LNK|";
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
    }
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
    cout << endl;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
    ec_ioctl_slave_reg_t io;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    io.emergency = 0;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
    io.address = 0x0300;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    io.size = 20;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    io.data = new uint8_t[20];
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    for (unsigned int i = 0; i < master.slave_count; i++) {
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
        io.slave_position = i;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
        try {
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
            m.readReg(&io);
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
        } catch (MasterDeviceException &e) {
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
            delete [] io.data;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
            throw e;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
        }
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
        cout << setw(3) << i << "|";
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
        for (int port = 0; port < 4; port++) {
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
            cout << setw(3) << (unsigned int) io.data[ 0 + port * 2]; // CRC
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
            cout << setw(4) << (unsigned int) io.data[ 1 + port * 2]; // PHY
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
            cout << setw(4) << (unsigned int) io.data[ 8 + port]; // FWD
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
            cout << setw(4) << (unsigned int) io.data[16 + port]; // LNK
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
            cout << "|";
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
        }
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
        ec_ioctl_slave_t slave;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
        m.getSlave(&slave, i);
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
        std::string slaveName(slave.name);
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
        slaveName = slaveName.substr(0, 11);
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
        cout << slaveName << endl;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
    }
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
    delete [] io.data;
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
}
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
d9e14a0ddbd6 New tool command 'ethercat crc'.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
/*****************************************************************************/