fp@1142: /***************************************************************************** fp@1142: * fp@1363: * $Id$ fp@1363: * fp@1363: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1363: * fp@1363: * This file is part of the IgH EtherCAT Master. fp@1363: * fp@1363: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1363: * modify it under the terms of the GNU General Public License version 2, as fp@1363: * published by the Free Software Foundation. fp@1363: * fp@1363: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1363: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1363: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1363: * Public License for more details. fp@1363: * fp@1363: * You should have received a copy of the GNU General Public License along fp@1363: * with the IgH EtherCAT Master; if not, write to the Free Software fp@1363: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1363: * fp@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. fp@1142: * fp@1142: ****************************************************************************/ fp@1142: fp@1142: #include fp@1142: #include fp@2436: #include fp@1142: using namespace std; fp@1142: fp@1142: #include "CommandPdos.h" fp@1826: #include "MasterDevice.h" fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1142: CommandPdos::CommandPdos(): fp@1327: Command("pdos", "List Sync managers, PDO assignment and mapping.") fp@1142: { fp@1142: } fp@1142: fp@1142: /*****************************************************************************/ fp@1142: fp@1968: string CommandPdos::helpString(const string &binaryBaseName) const fp@1142: { fp@1142: stringstream str; fp@1142: fp@1968: str << binaryBaseName << " " << getName() << " [OPTIONS]" << endl fp@1142: << endl fp@1804: << getBriefDescription() << endl fp@1804: << endl fp@2436: << "For the default skin (see --skin option) the information" << endl fp@2436: << "is displayed in three layers, which are" << endl fp@1804: << "indented accordingly:" << endl fp@1804: << endl fp@1804: << "1) Sync managers - Contains the sync manager information" << endl fp@1804: << " from the SII: Index, physical start address, default" << endl fp@1804: << " size, control register and enable word. Example:" << endl fp@1804: << endl fp@1804: << " SM3: PhysAddr 0x1100, DefaultSize 0, ControlRegister 0x20, " fp@1804: << "Enable 1" << endl fp@1804: << endl fp@1804: << "2) Assigned PDOs - PDO direction, hexadecimal index and" << endl fp@1804: << " the PDO name, if avaliable. Note that a 'Tx' and 'Rx'" << endl fp@1144: << " are seen from the slave's point of view. Example:" << endl fp@1804: << endl fp@1804: << " TxPDO 0x1a00 \"Channel1\"" << endl fp@1804: << endl fp@1804: << "3) Mapped PDO entries - PDO entry index and subindex (both" << endl fp@1804: << " hexadecimal), the length in bit and the description, if" << endl fp@1804: << " available. Example:" << endl fp@1804: << endl fp@1804: << " PDO entry 0x3101:01, 8 bit, \"Status\"" << endl fp@1804: << endl fp@1804: << "Note, that the displayed PDO assignment and PDO mapping" << endl fp@1804: << "information can either originate from the SII or from the" << endl fp@1804: << "CoE communication area." << endl fp@1804: << endl fp@2436: << "The \"etherlab\" skin outputs a template configuration" << endl fp@2436: << "for EtherLab's generic EtherCAT slave block." << endl fp@2436: << endl fp@1804: << "Command-specific options:" << endl fp@1157: << " --alias -a " << endl fp@1157: << " --position -p Slave selection. See the help of" << endl fp@1157: << " the 'slaves' command." << endl fp@2436: << " --skin -s Choose output skin. Possible values are" fp@2436: << endl fp@2436: << " \"default\" and \"etherlab\"." << endl fp@1804: << endl fp@1804: << numericInfo(); fp@1142: fp@1804: return str.str(); fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1826: void CommandPdos::execute(const StringVector &args) fp@1142: { fp@1869: MasterIndexList masterIndices; fp@1151: SlaveList slaves; fp@1151: SlaveList::const_iterator si; fp@1826: bool showHeader, multiMaster; fp@2421: fp@1373: if (args.size()) { fp@1373: stringstream err; fp@1373: err << "'" << getName() << "' takes no arguments!"; fp@1373: throwInvalidUsageException(err); fp@1373: } fp@1373: fp@2436: if (getSkin().empty() || getSkin() == "default") { fp@2436: masterIndices = getMasterIndices(); fp@2436: multiMaster = masterIndices.size() > 1; fp@2436: MasterIndexList::const_iterator mi; fp@2436: for (mi = masterIndices.begin(); fp@2436: mi != masterIndices.end(); mi++) { fp@2436: MasterDevice m(*mi); fp@2436: m.open(MasterDevice::Read); fp@2436: slaves = selectedSlaves(m); fp@2436: showHeader = multiMaster || slaves.size() > 1; fp@2436: fp@2436: for (si = slaves.begin(); si != slaves.end(); si++) { fp@2436: listSlavePdos(m, *si, showHeader); fp@2436: } fp@2436: } fp@2436: } fp@2436: else if (getSkin() == "etherlab") { fp@2436: masterIndices = getMasterIndices(); fp@2436: MasterIndexList::const_iterator mi; fp@2436: for (mi = masterIndices.begin(); fp@2436: mi != masterIndices.end(); mi++) { fp@2436: MasterDevice m(*mi); fp@2436: m.open(MasterDevice::Read); fp@2436: slaves = selectedSlaves(m); fp@2436: fp@2436: for (si = slaves.begin(); si != slaves.end(); si++) { fp@2436: etherlabConfig(m, *si); fp@2436: } fp@2436: } fp@2436: } fp@2436: else { fp@2436: stringstream err; fp@2436: err << "Invalid skin '" << getSkin() << "'!"; fp@2436: throwInvalidUsageException(err); fp@1142: } fp@1142: } fp@1142: fp@1142: /****************************************************************************/ fp@1142: fp@1142: void CommandPdos::listSlavePdos( fp@1804: MasterDevice &m, fp@1151: const ec_ioctl_slave_t &slave, fp@1804: bool showHeader fp@1804: ) fp@1142: { fp@1142: ec_ioctl_slave_sync_t sync; fp@1142: ec_ioctl_slave_sync_pdo_t pdo; fp@1142: ec_ioctl_slave_sync_pdo_entry_t entry; fp@1142: unsigned int i, j, k; fp@2421: fp@1827: if (showHeader && slave.sync_count) fp@1826: cout << "=== Master " << m.getIndex() fp@1826: << ", Slave " << slave.position << " ===" << endl; fp@1142: fp@1142: for (i = 0; i < slave.sync_count; i++) { fp@1151: m.getSync(&sync, slave.position, i); fp@1142: fp@1142: cout << "SM" << i << ":" fp@1142: << " PhysAddr 0x" fp@1142: << hex << setfill('0') fp@1142: << setw(4) << sync.physical_start_address fp@1142: << ", DefaultSize " fp@1142: << dec << setfill(' ') << setw(4) << sync.default_size fp@1142: << ", ControlRegister 0x" fp@1142: << hex << setfill('0') << setw(2) fp@1142: << (unsigned int) sync.control_register fp@1142: << ", Enable " << dec << (unsigned int) sync.enable fp@1142: << endl; fp@1142: fp@1142: for (j = 0; j < sync.pdo_count; j++) { fp@1151: m.getPdo(&pdo, slave.position, i, j); fp@1142: fp@1142: cout << " " << (sync.control_register & 0x04 ? "R" : "T") fp@1327: << "xPDO 0x" fp@1142: << hex << setfill('0') fp@1142: << setw(4) << pdo.index fp@1142: << " \"" << pdo.name << "\"" << endl; fp@1142: fp@1142: if (getVerbosity() == Quiet) fp@1142: continue; fp@1142: fp@1142: for (k = 0; k < pdo.entry_count; k++) { fp@1151: m.getPdoEntry(&entry, slave.position, i, j, k); fp@1142: fp@1327: cout << " PDO entry 0x" fp@1142: << hex << setfill('0') fp@1142: << setw(4) << entry.index fp@1142: << ":" << setw(2) << (unsigned int) entry.subindex fp@1187: << ", " << dec << setfill(' ') fp@1187: << setw(2) << (unsigned int) entry.bit_length fp@1142: << " bit, \"" << entry.name << "\"" << endl; fp@1142: } fp@1142: } fp@1142: } fp@1142: } fp@1142: fp@2436: /****************************************************************************/ fp@2436: fp@2436: void CommandPdos::etherlabConfig( fp@2436: MasterDevice &m, fp@2436: const ec_ioctl_slave_t &slave fp@2436: ) fp@2436: { fp@2436: ec_ioctl_slave_sync_t sync; fp@2436: ec_ioctl_slave_sync_pdo_t pdo; fp@2436: ec_ioctl_slave_sync_pdo_entry_t entry; fp@2436: unsigned int i, j, k; fp@2436: fp@2436: cout << "%" << endl fp@2436: << "% Master " << m.getIndex() << ", Slave " << slave.position; fp@2436: if (strlen(slave.order)) { fp@2436: cout << ", \"" << slave.order << "\""; fp@2436: } fp@2436: cout << endl fp@2436: << "%" << endl fp@2436: << "function rv = slave" << slave.position << "()" << endl << endl; fp@2436: fp@2436: cout << "% Slave configuration" << endl << endl; fp@2436: fp@2436: cout << "rv.SlaveConfig.vendor = " << slave.vendor_id << ";" << endl fp@2436: << "rv.SlaveConfig.product = hex2dec('" fp@2436: << hex << setfill('0') << setw(8) << slave.product_code << "');" fp@2436: << endl fp@2436: << "rv.SlaveConfig.description = '" << slave.order << "';" << endl fp@2436: << "rv.SlaveConfig.sm = { ..." << endl; fp@2436: fp@2436: /* slave configuration */ fp@2436: for (i = 0; i < slave.sync_count; i++) { fp@2436: m.getSync(&sync, slave.position, i); fp@2436: fp@2436: cout << " {" << dec << i << ", " fp@2436: << (sync.control_register & 0x04 ? 0 : 1) << ", {" << endl; fp@2436: fp@2436: for (j = 0; j < sync.pdo_count; j++) { fp@2436: m.getPdo(&pdo, slave.position, i, j); fp@2436: fp@2436: cout << " {hex2dec('" << fp@2436: hex << setfill('0') << setw(4) << pdo.index << "'), [" fp@2436: << endl; fp@2436: fp@2436: for (k = 0; k < pdo.entry_count; k++) { fp@2436: m.getPdoEntry(&entry, slave.position, i, j, k); fp@2436: fp@2436: cout << " hex2dec('" fp@2436: << hex << setfill('0') << setw(4) << entry.index fp@2436: << "'), hex2dec('" fp@2436: << setw(2) << (unsigned int) entry.subindex fp@2436: << "'), " << dec << setfill(' ') << setw(3) fp@2436: << (unsigned int) entry.bit_length fp@2436: << "; ..." << endl; fp@2436: } fp@2436: fp@2436: cout << " ]}, ..." << endl; fp@2436: } fp@2436: cout << " }}, ..." << endl; fp@2436: } fp@2436: fp@2436: cout << " };" << endl << endl; fp@2436: fp@2436: cout << "% Port configuration" << endl << endl; fp@2436: fp@2436: unsigned int input = 1, output = 1; fp@2436: for (i = 0; i < slave.sync_count; i++) { fp@2436: m.getSync(&sync, slave.position, i); fp@2436: fp@2436: for (j = 0; j < sync.pdo_count; j++) { fp@2436: m.getPdo(&pdo, slave.position, i, j); fp@2436: fp@2436: for (k = 0; k < pdo.entry_count; k++) { fp@2436: m.getPdoEntry(&entry, slave.position, i, j, k); fp@2436: fp@2436: if (!entry.index) { fp@2436: continue; fp@2436: } fp@2436: fp@2436: string dir; fp@2436: unsigned int port; fp@2436: fp@2436: if (sync.control_register & 0x04) { fp@2436: dir = "input"; fp@2436: port = input++; fp@2436: } fp@2436: else { fp@2436: dir = "output"; fp@2436: port = output++; fp@2436: } fp@2436: fp@2436: stringstream var; fp@2436: var << "rv.PortConfig." << dir << "(" << dec << port fp@2436: << ")"; fp@2436: fp@2436: cout << var.str() << ".pdo = [" fp@2436: << i << ", " << j << ", " << k << ", 0];" << endl; fp@2436: cout << var.str() << ".pdo_data_type = " fp@2436: << 1000 + (unsigned int) entry.bit_length fp@2436: << ";" << endl << endl; fp@2436: } fp@2436: } fp@2436: } fp@2436: fp@2436: cout << "end" << endl; fp@2436: } fp@2436: fp@1142: /*****************************************************************************/