Added 'ethercat cstruct' command to output PDO information in C language.
authorFlorian Pose <fp@igh-essen.com>
Wed, 29 Jul 2009 12:24:55 +0000
changeset 1514 85ac1c91045d
parent 1513 60ca68d853b8
child 1515 e722e0ac5d28
Added 'ethercat cstruct' command to output PDO information in C language.
NEWS
documentation/Makefile
documentation/ethercat_doc.tex
tool/CommandCStruct.cpp
tool/CommandCStruct.h
tool/Makefile.am
tool/main.cpp
--- a/NEWS	Wed Jul 29 09:19:53 2009 +0000
+++ b/NEWS	Wed Jul 29 12:24:55 2009 +0000
@@ -59,6 +59,7 @@
 * Module symbol versions file for ec_master.ko is installed to
   prefix/modules/ec_master.symvers.
 * Added 'ethercat eoe' command to display Ethernet over EtherCAT statistics.
+* Added 'ethercat cstruct' command to output PDO information in C language.
 * Significantly improved EoE bandwidth by running EoE processing in a kthread.
 
 Changes in 1.4.0:
--- a/documentation/Makefile	Wed Jul 29 09:19:53 2009 +0000
+++ b/documentation/Makefile	Wed Jul 29 12:24:55 2009 +0000
@@ -13,6 +13,7 @@
 COMMANDS := \
 	alias \
 	config \
+	cstruct \
 	data \
 	debug \
 	domains \
--- a/documentation/ethercat_doc.tex	Wed Jul 29 09:19:53 2009 +0000
+++ b/documentation/ethercat_doc.tex	Wed Jul 29 12:24:55 2009 +0000
@@ -2088,6 +2088,13 @@
 
 %------------------------------------------------------------------------------
 
+\subsection{Output PDO information in C Language}
+\label{sec:ethercat-cstruct}
+
+\lstinputlisting[basicstyle=\ttfamily\footnotesize]{external/ethercat_cstruct}
+
+%------------------------------------------------------------------------------
+
 \subsection{Displaying Process Data}
 
 \lstinputlisting[basicstyle=\ttfamily\footnotesize]{external/ethercat_data}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/CommandCStruct.cpp	Wed Jul 29 12:24:55 2009 +0000
@@ -0,0 +1,198 @@
+/*****************************************************************************
+ *
+ *  $Id: CommandXml.cpp 1778 2009-06-16 08:04:50Z fp $
+ *
+ *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License version 2, as
+ *  published by the Free Software Foundation.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+ *  Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  ---
+ *
+ *  The license mentioned above concerns the source code only. Using the
+ *  EtherCAT technology and brand is only permitted in compliance with the
+ *  industrial property and similar rights of Beckhoff Automation GmbH.
+ *
+ ****************************************************************************/
+
+#include <iostream>
+#include <iomanip>
+#include <string.h>
+using namespace std;
+
+#include "CommandCStruct.h"
+
+/*****************************************************************************/
+
+CommandCStruct::CommandCStruct():
+    Command("cstruct", "Generate slave PDO information in C language.")
+{
+}
+
+/*****************************************************************************/
+
+string CommandCStruct::helpString() const
+{
+    stringstream str;
+
+    str << getName() << " [OPTIONS]" << endl
+        << endl
+        << getBriefDescription() << endl
+        << endl
+        << "The output C code can be used directly with the" << endl
+        << "ecrt_slave_config_pdos() function of the application" << endl
+		<< "interface." << endl
+        << endl
+        << "Command-specific options:" << endl
+        << "  --alias    -a <alias>" << endl
+        << "  --position -p <pos>    Slave selection. See the help of" << endl
+        << "                         the 'slaves' command." << endl
+        << endl
+        << numericInfo();
+
+    return str.str();
+}
+
+/****************************************************************************/
+
+void CommandCStruct::execute(MasterDevice &m, const StringVector &args)
+{
+    SlaveList slaves;
+    SlaveList::const_iterator si;
+
+    if (args.size()) {
+        stringstream err;
+        err << "'" << getName() << "' takes no arguments!";
+        throwInvalidUsageException(err);
+    }
+
+    m.open(MasterDevice::Read);
+    slaves = selectedSlaves(m);
+
+    for (si = slaves.begin(); si != slaves.end(); si++) {
+        generateSlaveCStruct(m, *si);
+    }
+}
+
+/****************************************************************************/
+
+void CommandCStruct::generateSlaveCStruct(
+        MasterDevice &m,
+        const ec_ioctl_slave_t &slave
+        )
+{
+    ec_ioctl_slave_sync_t sync;
+    ec_ioctl_slave_sync_pdo_t pdo;
+    ec_ioctl_slave_sync_pdo_entry_t entry;
+    unsigned int i, j, k, pdo_pos = 0, entry_pos = 0;
+    stringstream id, syncs, pdos, entries;
+
+    if (!slave.sync_count)
+        return;
+
+    id << "slave_" << dec << slave.position << "_";
+
+    for (i = 0; i < slave.sync_count; i++) {
+        m.getSync(&sync, slave.position, i);
+
+        syncs << "   {" << dec << sync.sync_index
+            << ", " << (EC_READ_BIT(&sync.control_register, 2) ?
+                    "EC_DIR_OUTPUT" : "EC_DIR_INPUT")
+            << ", " << dec << (unsigned int) sync.pdo_count
+            << ", ";
+        if (sync.pdo_count) {
+            syncs << id.str() << "pdos + " << dec << pdo_pos;
+        } else {
+            syncs << "NULL";
+        }
+        syncs << ", " << (EC_READ_BIT(&sync.control_register, 6) ?
+                "EC_WD_ENABLE" : "EC_WD_DISABLE")
+            << "},";
+        syncs << endl;
+        pdo_pos += sync.pdo_count;
+
+        for (j = 0; j < sync.pdo_count; j++) {
+            m.getPdo(&pdo, slave.position, i, j);
+
+            pdos << "   {0x" << hex << setfill('0')
+                << setw(4) << pdo.index
+                << ", " << dec << (unsigned int) pdo.entry_count
+                << ", ";
+                if (pdo.entry_count) {
+                    pdos << id.str() << "pdo_entries + " << dec << entry_pos;
+                } else {
+                    pdos << "NULL";
+                }
+            pdos << "},";
+            if (strlen((const char *) pdo.name)) {
+                pdos << " /* " << pdo.name << " */";
+            }
+            pdos << endl;
+            entry_pos += pdo.entry_count;
+
+            for (k = 0; k < pdo.entry_count; k++) {
+                m.getPdoEntry(&entry, slave.position, i, j, k);
+
+                entries << "   {0x" << hex << setfill('0')
+                    << setw(4) << entry.index
+                    << ", 0x" << setw(2) << (unsigned int) entry.subindex
+                    << ", " << dec << (unsigned int) entry.bit_length
+                    << "},";
+                if (strlen((const char *) entry.name)) {
+                    entries << " /* " << entry.name << " */";
+                }
+                entries << endl;
+            }
+        }
+    }
+
+    cout
+        << "/* Slave " << slave.position;
+    if (strlen(slave.order)) {
+        cout << ", \"" << slave.order << "\"";
+    }
+
+    cout << endl
+        << " * Vendor ID:       0x" << hex << setfill('0')
+        << setw(8) << slave.vendor_id << endl
+        << " * Product code:    0x" << hex << setfill('0')
+        << setw(8) << slave.product_code << endl
+        << " * Revision number: 0x" << hex << setfill('0')
+        << setw(8) << slave.revision_number << endl 
+        << " */" << endl
+        << endl;
+
+    if (entry_pos) {
+        cout << "ec_pdo_entry_info_t " << id.str()
+            << "pdo_entries[] = {" << endl
+            << entries.str()
+            << "};" << endl
+            << endl;
+    }
+
+    if (pdo_pos) {
+        cout << "ec_pdo_info_t " << id.str() << "pdos[] = {" << endl
+            << pdos.str()
+            << "};" << endl
+            << endl;
+    }
+
+    cout << "ec_sync_info_t " << id.str() << "syncs[] = {" << endl
+        << syncs.str()
+        << "};" << endl
+        << endl;
+}
+
+/*****************************************************************************/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/CommandCStruct.h	Wed Jul 29 12:24:55 2009 +0000
@@ -0,0 +1,52 @@
+/*****************************************************************************
+ *
+ *  $Id: CommandXml.h 1778 2009-06-16 08:04:50Z fp $
+ *
+ *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License version 2, as
+ *  published by the Free Software Foundation.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+ *  Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  ---
+ *
+ *  The license mentioned above concerns the source code only. Using the
+ *  EtherCAT technology and brand is only permitted in compliance with the
+ *  industrial property and similar rights of Beckhoff Automation GmbH.
+ *
+ ****************************************************************************/
+
+#ifndef __COMMANDCSTRUCT_H__
+#define __COMMANDCSTRUCT_H__
+
+#include "Command.h"
+
+/****************************************************************************/
+
+class CommandCStruct:
+    public Command
+{
+    public:
+        CommandCStruct();
+
+        string helpString() const;
+        void execute(MasterDevice &, const StringVector &);
+
+    protected:
+        void generateSlaveCStruct(MasterDevice &, const ec_ioctl_slave_t &);
+};
+
+/****************************************************************************/
+
+#endif
--- a/tool/Makefile.am	Wed Jul 29 09:19:53 2009 +0000
+++ b/tool/Makefile.am	Wed Jul 29 12:24:55 2009 +0000
@@ -37,6 +37,7 @@
 	Command.cpp \
 	CommandAlias.cpp \
 	CommandConfig.cpp \
+	CommandCStruct.cpp \
 	CommandData.cpp \
 	CommandDebug.cpp \
 	CommandDomains.cpp \
@@ -68,6 +69,7 @@
 	Command.h \
 	CommandAlias.h \
 	CommandConfig.h \
+	CommandCStruct.h \
 	CommandData.h \
 	CommandDebug.h \
 	CommandDomains.h \
--- a/tool/main.cpp	Wed Jul 29 09:19:53 2009 +0000
+++ b/tool/main.cpp	Wed Jul 29 12:24:55 2009 +0000
@@ -37,6 +37,7 @@
 
 #include "CommandAlias.h"
 #include "CommandConfig.h"
+#include "CommandCStruct.h"
 #include "CommandData.h"
 #include "CommandDebug.h"
 #include "CommandDomains.h"
@@ -293,6 +294,7 @@
 
     commandList.push_back(new CommandAlias());
     commandList.push_back(new CommandConfig());
+    commandList.push_back(new CommandCStruct());
     commandList.push_back(new CommandData());
     commandList.push_back(new CommandDebug());
     commandList.push_back(new CommandDomains());