Add CLI support for configuring cache levels

This commit is contained in:
Tung
2026-03-26 10:29:18 +01:00
committed by Florian Pose
parent 8568942eb6
commit e32d9b1813
7 changed files with 170 additions and 6 deletions
-6
View File
@@ -2401,13 +2401,7 @@ static ATTRIBUTES int ec_ioctl_sii_caching(
)
{
int ret = 0;
if (unlikely(!ctx->requested)) {
ret = -EPERM;
goto out_return;
}
ret = ecrt_master_sii_caching(master, (unsigned long) arg);
out_return:
return ret;
}
+115
View File
@@ -0,0 +1,115 @@
/*****************************************************************************
*
*
* 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
*
****************************************************************************/
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#include "CommandCache.h"
#include "MasterDevice.h"
/****************************************************************************/
CommandCache::CommandCache():
Command("sii_caching", "Configure SII_CACHING settings.")
{
}
/****************************************************************************/
string CommandCache::helpString(const string &binaryBaseName) const
{
stringstream str;
str << binaryBaseName << " " << getName() << " <FIELDS>" << endl
<< endl
<< getBriefDescription() << endl
<< endl
<< "This command configures cache level to speed up scanning process" << endl
<< endl
<< "Caching fields are specified as a decimal number that is a" << endl
<< "combination of the following flags:" << endl
<< " 0 - Disable SII caching" << endl
<< " 1 - Use vendor ID" << endl
<< " 2 - Use product code" << endl
<< " 4 - Use revision number" << endl
<< " 8 - Use serial number" << endl
<< " 16 - Use alias address" << endl
<< endl
<< "Example: 'ethercat cache 31' enables caching based on all" << endl
<< " five fields (1+2+4+8+16=31)." << endl
<< "Example: 'ethercat cache 0' disables SII caching." << endl
<< endl
<< "Arguments:" << endl
<< " FIELDS Decimal number representing the caching fields." << endl
<< endl;
return str.str();
}
/****************************************************************************/
void CommandCache::execute(const StringVector &args)
{
MasterIndexList masterIndices;
uint32_t fields;
stringstream err;
if (args.size() != 1)
{
err << "'" << getName() << "' requires exactly one argument!";
throwInvalidUsageException(err);
}
// Use strtoul instead of stoul for older C++ standards
char *endptr;
fields = strtoul(args[0].c_str(), &endptr, 10);
// Basic error checking to ensure the string was actually a number
if (*endptr != '\0' || args[0].empty()) {
err << "Invalid caching fields value '" << args[0] << "'";
throwInvalidUsageException(err);
}
if (fields > 31) {
err << "Caching fields value must be between 0 and 31.";
throwInvalidUsageException(err);
}
masterIndices = getMasterIndices();
MasterIndexList::const_iterator mi;
for (mi = masterIndices.begin(); mi != masterIndices.end(); mi++) {
MasterDevice m(*mi);
m.open(MasterDevice::ReadWrite);
try {
m.setSiiCaching(fields);
cout << "Master " << dec << *mi << ": SII caching set to 0x"
<< hex << setfill('0') << setw(2) << fields << dec << endl;
} catch (const MasterDeviceException &e) {
err << "Master " << *mi << ": " << e.what();
throwCommandException(err);
}
}
}
/****************************************************************************/
+39
View File
@@ -0,0 +1,39 @@
/*****************************************************************************
*
* 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
*
****************************************************************************/
#ifndef __COMMANDCACHE_H__
#define __COMMANDCACHE_H__
#include "Command.h"
/****************************************************************************/
class CommandCache:
public Command
{
public:
CommandCache();
string helpString(const string &) const;
void execute(const StringVector &);
};
/****************************************************************************/
#endif
+2
View File
@@ -30,6 +30,7 @@ bin_PROGRAMS = ethercat
ethercat_SOURCES = \
Command.cpp \
CommandAlias.cpp \
CommandCache.cpp \
CommandCrc.cpp \
CommandCStruct.cpp \
CommandConfig.cpp \
@@ -73,6 +74,7 @@ endif
noinst_HEADERS = \
Command.h \
CommandAlias.h \
CommandCache.h \
CommandCrc.h \
CommandCStruct.h \
CommandConfig.h \
+11
View File
@@ -500,6 +500,17 @@ void MasterDevice::rescan()
/****************************************************************************/
void MasterDevice::setSiiCaching(unsigned int fields)
{
if (ioctl(fd, EC_IOCTL_SII_CACHING, fields) < 0) {
stringstream err;
err << "Failed to set SII caching: " << strerror(errno);
throw MasterDeviceException(err);
}
}
/****************************************************************************/
void MasterDevice::sdoDownload(ec_ioctl_slave_sdo_download_t *data)
{
if (ioctl(fd, EC_IOCTL_SLAVE_SDO_DOWNLOAD, data) < 0) {
+1
View File
@@ -147,6 +147,7 @@ class MasterDevice
void writeReg(ec_ioctl_slave_reg_t *);
void setDebug(unsigned int);
void rescan();
void setSiiCaching(unsigned int);
void sdoDownload(ec_ioctl_slave_sdo_download_t *);
void sdoUpload(ec_ioctl_slave_sdo_upload_t *);
void requestState(uint16_t, uint8_t);
+2
View File
@@ -28,6 +28,7 @@
using namespace std;
#include "CommandAlias.h"
#include "CommandCache.h"
#include "CommandConfig.h"
#include "CommandCrc.h"
#include "CommandCStruct.h"
@@ -272,6 +273,7 @@ int main(int argc, char **argv)
binaryBaseName = basename(argv[0]);
commandList.push_back(new CommandAlias());
commandList.push_back(new CommandCache());
commandList.push_back(new CommandConfig());
commandList.push_back(new CommandCrc());
commandList.push_back(new CommandCStruct());