tool/CommandCStruct.cpp
changeset 1514 85ac1c91045d
child 1524 e7bdab4d6189
equal deleted inserted replaced
1513:60ca68d853b8 1514:85ac1c91045d
       
     1 /*****************************************************************************
       
     2  *
       
     3  *  $Id: CommandXml.cpp 1778 2009-06-16 08:04:50Z fp $
       
     4  *
       
     5  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT Master.
       
     8  *
       
     9  *  The IgH EtherCAT Master is free software; you can redistribute it and/or
       
    10  *  modify it under the terms of the GNU General Public License version 2, as
       
    11  *  published by the Free Software Foundation.
       
    12  *
       
    13  *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
       
    14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
       
    16  *  Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU General Public License along
       
    19  *  with the IgH EtherCAT Master; if not, write to the Free Software
       
    20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    21  *
       
    22  *  ---
       
    23  *
       
    24  *  The license mentioned above concerns the source code only. Using the
       
    25  *  EtherCAT technology and brand is only permitted in compliance with the
       
    26  *  industrial property and similar rights of Beckhoff Automation GmbH.
       
    27  *
       
    28  ****************************************************************************/
       
    29 
       
    30 #include <iostream>
       
    31 #include <iomanip>
       
    32 #include <string.h>
       
    33 using namespace std;
       
    34 
       
    35 #include "CommandCStruct.h"
       
    36 
       
    37 /*****************************************************************************/
       
    38 
       
    39 CommandCStruct::CommandCStruct():
       
    40     Command("cstruct", "Generate slave PDO information in C language.")
       
    41 {
       
    42 }
       
    43 
       
    44 /*****************************************************************************/
       
    45 
       
    46 string CommandCStruct::helpString() const
       
    47 {
       
    48     stringstream str;
       
    49 
       
    50     str << getName() << " [OPTIONS]" << endl
       
    51         << endl
       
    52         << getBriefDescription() << endl
       
    53         << endl
       
    54         << "The output C code can be used directly with the" << endl
       
    55         << "ecrt_slave_config_pdos() function of the application" << endl
       
    56 		<< "interface." << endl
       
    57         << endl
       
    58         << "Command-specific options:" << endl
       
    59         << "  --alias    -a <alias>" << endl
       
    60         << "  --position -p <pos>    Slave selection. See the help of" << endl
       
    61         << "                         the 'slaves' command." << endl
       
    62         << endl
       
    63         << numericInfo();
       
    64 
       
    65     return str.str();
       
    66 }
       
    67 
       
    68 /****************************************************************************/
       
    69 
       
    70 void CommandCStruct::execute(MasterDevice &m, const StringVector &args)
       
    71 {
       
    72     SlaveList slaves;
       
    73     SlaveList::const_iterator si;
       
    74 
       
    75     if (args.size()) {
       
    76         stringstream err;
       
    77         err << "'" << getName() << "' takes no arguments!";
       
    78         throwInvalidUsageException(err);
       
    79     }
       
    80 
       
    81     m.open(MasterDevice::Read);
       
    82     slaves = selectedSlaves(m);
       
    83 
       
    84     for (si = slaves.begin(); si != slaves.end(); si++) {
       
    85         generateSlaveCStruct(m, *si);
       
    86     }
       
    87 }
       
    88 
       
    89 /****************************************************************************/
       
    90 
       
    91 void CommandCStruct::generateSlaveCStruct(
       
    92         MasterDevice &m,
       
    93         const ec_ioctl_slave_t &slave
       
    94         )
       
    95 {
       
    96     ec_ioctl_slave_sync_t sync;
       
    97     ec_ioctl_slave_sync_pdo_t pdo;
       
    98     ec_ioctl_slave_sync_pdo_entry_t entry;
       
    99     unsigned int i, j, k, pdo_pos = 0, entry_pos = 0;
       
   100     stringstream id, syncs, pdos, entries;
       
   101 
       
   102     if (!slave.sync_count)
       
   103         return;
       
   104 
       
   105     id << "slave_" << dec << slave.position << "_";
       
   106 
       
   107     for (i = 0; i < slave.sync_count; i++) {
       
   108         m.getSync(&sync, slave.position, i);
       
   109 
       
   110         syncs << "   {" << dec << sync.sync_index
       
   111             << ", " << (EC_READ_BIT(&sync.control_register, 2) ?
       
   112                     "EC_DIR_OUTPUT" : "EC_DIR_INPUT")
       
   113             << ", " << dec << (unsigned int) sync.pdo_count
       
   114             << ", ";
       
   115         if (sync.pdo_count) {
       
   116             syncs << id.str() << "pdos + " << dec << pdo_pos;
       
   117         } else {
       
   118             syncs << "NULL";
       
   119         }
       
   120         syncs << ", " << (EC_READ_BIT(&sync.control_register, 6) ?
       
   121                 "EC_WD_ENABLE" : "EC_WD_DISABLE")
       
   122             << "},";
       
   123         syncs << endl;
       
   124         pdo_pos += sync.pdo_count;
       
   125 
       
   126         for (j = 0; j < sync.pdo_count; j++) {
       
   127             m.getPdo(&pdo, slave.position, i, j);
       
   128 
       
   129             pdos << "   {0x" << hex << setfill('0')
       
   130                 << setw(4) << pdo.index
       
   131                 << ", " << dec << (unsigned int) pdo.entry_count
       
   132                 << ", ";
       
   133                 if (pdo.entry_count) {
       
   134                     pdos << id.str() << "pdo_entries + " << dec << entry_pos;
       
   135                 } else {
       
   136                     pdos << "NULL";
       
   137                 }
       
   138             pdos << "},";
       
   139             if (strlen((const char *) pdo.name)) {
       
   140                 pdos << " /* " << pdo.name << " */";
       
   141             }
       
   142             pdos << endl;
       
   143             entry_pos += pdo.entry_count;
       
   144 
       
   145             for (k = 0; k < pdo.entry_count; k++) {
       
   146                 m.getPdoEntry(&entry, slave.position, i, j, k);
       
   147 
       
   148                 entries << "   {0x" << hex << setfill('0')
       
   149                     << setw(4) << entry.index
       
   150                     << ", 0x" << setw(2) << (unsigned int) entry.subindex
       
   151                     << ", " << dec << (unsigned int) entry.bit_length
       
   152                     << "},";
       
   153                 if (strlen((const char *) entry.name)) {
       
   154                     entries << " /* " << entry.name << " */";
       
   155                 }
       
   156                 entries << endl;
       
   157             }
       
   158         }
       
   159     }
       
   160 
       
   161     cout
       
   162         << "/* Slave " << slave.position;
       
   163     if (strlen(slave.order)) {
       
   164         cout << ", \"" << slave.order << "\"";
       
   165     }
       
   166 
       
   167     cout << endl
       
   168         << " * Vendor ID:       0x" << hex << setfill('0')
       
   169         << setw(8) << slave.vendor_id << endl
       
   170         << " * Product code:    0x" << hex << setfill('0')
       
   171         << setw(8) << slave.product_code << endl
       
   172         << " * Revision number: 0x" << hex << setfill('0')
       
   173         << setw(8) << slave.revision_number << endl 
       
   174         << " */" << endl
       
   175         << endl;
       
   176 
       
   177     if (entry_pos) {
       
   178         cout << "ec_pdo_entry_info_t " << id.str()
       
   179             << "pdo_entries[] = {" << endl
       
   180             << entries.str()
       
   181             << "};" << endl
       
   182             << endl;
       
   183     }
       
   184 
       
   185     if (pdo_pos) {
       
   186         cout << "ec_pdo_info_t " << id.str() << "pdos[] = {" << endl
       
   187             << pdos.str()
       
   188             << "};" << endl
       
   189             << endl;
       
   190     }
       
   191 
       
   192     cout << "ec_sync_info_t " << id.str() << "syncs[] = {" << endl
       
   193         << syncs.str()
       
   194         << "};" << endl
       
   195         << endl;
       
   196 }
       
   197 
       
   198 /*****************************************************************************/