加入 写 SN

This commit is contained in:
Chinky 2024-01-14 18:31:52 +08:00
parent be172749b2
commit 25fd9f81fb
2 changed files with 113 additions and 38 deletions

View File

@ -5,6 +5,7 @@
"struct_timeval.h": "c",
"features.h": "c",
"random": "c",
"logn.h": "c"
"logn.h": "c",
"timesize.h": "c"
}
}

View File

@ -31,7 +31,8 @@
#define MODE_WRITEBIN 3
#define MODE_WRITEINTEL 4
#define MODE_WRITEALIAS 5
#define MODE_INFO 6
#define MODE_WRITESN 6
#define MODE_INFO 7
#define MAXSLENGTH 256
@ -40,7 +41,8 @@ uint8 ob;
uint16 ow;
int os;
int slave;
int alias;
uint16 alias;
uint32 serial_no;
ec_timet tstart, tend, tdif;
int wkc;
int mode;
@ -292,13 +294,13 @@ int eeprom_write(int slave, int start, int length)
return 0;
}
int eeprom_writealias(int slave, int alias, uint16 crc)
int eeprom_writealias(int slave, uint16 alias, uint16 crc)
{
uint16 aiadr;
uint8 eepctl;
int ret;
if ((ec_slavecount >= slave) && (slave > 0) && (alias <= 0xffff))
if ((ec_slavecount >= slave) && (slave > 0))
{
aiadr = 1 - slave;
eepctl = 2;
@ -318,7 +320,34 @@ int eeprom_writealias(int slave, int alias, uint16 crc)
return 0;
}
void eepromtool(char *ifname, int slave, int mode, char *fname)
int eeprom_writesn(int slave, uint32_t sn)
{
uint16 aiadr;
uint8 eepctl;
int ret;
if ((slave > ec_slavecount) || (slave <= 0))
{
return 0;
}
aiadr = 1 - slave;
eepctl = 2;
ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl,
EC_TIMEOUTRET); /* force Eeprom from PDI */
eepctl = 0;
ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl,
EC_TIMEOUTRET); /* set Eeprom to master */
ret = ec_writeeepromAP(aiadr, 0x0E, (uint16)(sn & 0xffff), EC_TIMEOUTEEP);
if (ret)
{
ret = ec_writeeepromAP(aiadr, 0x0F, (uint16)(sn >> 16), EC_TIMEOUTEEP);
}
return ret;
}
int eepromtool(char *ifname, int slave, int mode, char *fname)
{
int w, rc = 0, estart, esize;
uint16 *wbuf;
@ -345,21 +374,25 @@ void eepromtool(char *ifname, int slave, int mode, char *fname)
wbuf = (uint16 *)&ebuf[0];
printf("Slave %d data\n", slave);
printf(" PDI Control : %4.4X\n", *(wbuf + 0x00));
printf(" PDI Config : %4.4X\n", *(wbuf + 0x01));
printf(" Config Alias : %4.4X\n", *(wbuf + 0x04));
printf(" Checksum : %4.4X\n", *(wbuf + 0x07));
printf(" calculated : %4.4X\n", SIIcrc(&ebuf[0]));
printf(" Vendor ID : %8.8X\n", *(uint32 *)(wbuf + 0x08));
printf(" Product Code : %8.8X\n", *(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : %8.8X\n", *(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : %8.8X\n", *(uint32 *)(wbuf + 0x0E));
printf(" Mailbox Protocol : %4.4X\n", *(wbuf + 0x1C));
printf(" PDI Control : 0x%4.4X(%u)\n", *(wbuf + 0x00), *(wbuf + 0x00));
printf(" PDI Config : 0x%4.4X(%u)\n", *(wbuf + 0x01), *(wbuf + 0x01));
printf(" Config Alias : 0x%4.4X(%u)\n", *(wbuf + 0x04), *(wbuf + 0x04));
printf(" Checksum : 0x%4.4X\n", *(wbuf + 0x07));
printf(" calculated : 0x%4.4X\n", SIIcrc(&ebuf[0]));
printf(" Vendor ID : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x08),
*(uint32 *)(wbuf + 0x08));
printf(" Product Code : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0A),
*(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0C),
*(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0E),
*(uint32 *)(wbuf + 0x0E));
printf(" Mailbox Protocol : 0x%4.4X\n", *(wbuf + 0x1C));
esize = (*(wbuf + 0x3E) + 1) * 128;
if (esize > MAXBUF)
esize = MAXBUF;
printf(" Size : %4.4X = %d bytes\n", *(wbuf + 0x3E), esize);
printf(" Version : %4.4X\n", *(wbuf + 0x3F));
printf(" Size : 0x%4.4X = %d bytes\n", *(wbuf + 0x3E), esize);
printf(" Version : 0x%4.4X(%u)\n", *(wbuf + 0x3F), *(wbuf + 0x3F));
}
if ((mode == MODE_READBIN) || (mode == MODE_READINTEL))
{
@ -373,7 +406,7 @@ void eepromtool(char *ifname, int slave, int mode, char *fname)
if (mode == MODE_READBIN)
output_bin(fname, esize);
printf("\nTotal EEPROM read time :%ldms\n",
printf("\nTotal EEPROM read time: %ldms\n",
(tdif.usec + (tdif.sec * 1000000L)) / 1000);
}
if ((mode == MODE_WRITEBIN) || (mode == MODE_WRITEINTEL))
@ -388,10 +421,14 @@ void eepromtool(char *ifname, int slave, int mode, char *fname)
{
wbuf = (uint16 *)&ebuf[0];
printf("Slave %d\n", slave);
printf(" Vendor ID : %8.8X\n", *(uint32 *)(wbuf + 0x08));
printf(" Product Code : %8.8X\n", *(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : %8.8X\n", *(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : %8.8X\n", *(uint32 *)(wbuf + 0x0E));
printf(" Vendor ID : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x08),
*(uint32 *)(wbuf + 0x08));
printf(" Product Code : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0A),
*(uint32 *)(wbuf + 0x0A));
printf(" Revision Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0C),
*(uint32 *)(wbuf + 0x0C));
printf(" Serial Number : 0x%8.8X(%u)\n", *(uint32 *)(wbuf + 0x0E),
*(uint32 *)(wbuf + 0x0E));
printf("Busy");
fflush(stdout);
@ -412,9 +449,12 @@ void eepromtool(char *ifname, int slave, int mode, char *fname)
{
wbuf = (uint16 *)&ebuf[0];
*(wbuf + 0x04) = alias;
if (eeprom_writealias(slave, alias, SIIcrc(&ebuf[0])))
rc = eeprom_writealias(slave, alias, SIIcrc(&ebuf[0]));
if (rc)
{
printf("Alias %4.4X written successfully to slave %d\n", alias, slave);
printf("Alias %d (HEX: %4.4X) written successfully to slave %d\n",
alias, alias, slave);
}
else
{
@ -426,6 +466,19 @@ void eepromtool(char *ifname, int slave, int mode, char *fname)
printf("Could not read slave EEPROM");
}
}
if (mode == MODE_WRITESN)
{
rc = eeprom_writesn(slave, serial_no);
if (rc)
{
printf("SerialNo %u (HEX: %8.8X) written successfully to slave %d\n",
serial_no, serial_no, slave);
}
else
{
printf("SerialNo not written\n");
}
}
}
else
{
@ -444,10 +497,12 @@ void eepromtool(char *ifname, int slave, int mode, char *fname)
{
printf("No socket connection on %s\nExcecute as root\n", ifname);
}
return rc;
}
int main(int argc, char *argv[])
{
int rc = 1;
printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
mode = MODE_NONE;
@ -466,28 +521,47 @@ int main(int argc, char *argv[])
mode = MODE_WRITEBIN;
if ((strncmp(argv[3], "-wi", sizeof("-wi")) == 0))
mode = MODE_WRITEINTEL;
if ((strncmp(argv[3], "-walias", sizeof("-walias")) == 0))
if ((strncmp(argv[3], "-wa", sizeof("-wa")) == 0))
{
mode = MODE_WRITEALIAS;
alias = atoi(argv[4]);
alias = (uint16)strtoul(argv[4], NULL, 10);
}
if ((strncmp(argv[3], "-wah", sizeof("-wah")) == 0))
{
mode = MODE_WRITEALIAS;
alias = (uint16)strtoul(argv[4], NULL, 16);
}
if ((strncmp(argv[3], "-wsn", sizeof("-wsn")) == 0))
{
mode = MODE_WRITESN;
serial_no = strtoul(argv[4], NULL, 10);
}
if ((strncmp(argv[3], "-wsnh", sizeof("-wsnh")) == 0))
{
mode = MODE_WRITESN;
serial_no = strtoul(argv[4], NULL, 16);
}
}
/* start tool */
eepromtool(argv[1], slave, mode, argv[4]);
rc = !eepromtool(argv[1], slave, mode, argv[4]);
}
else
{
ec_adaptert *adapter = NULL;
printf("Usage: eepromtool ifname slave OPTION fname|alias\n");
printf("ifname = eth0 for example\n");
printf("slave = slave number in EtherCAT order 1..n\n");
printf(" -i display EEPROM information\n");
printf(" -walias write slave alias\n");
printf(" -r read EEPROM, output binary format\n");
printf(" -ri read EEPROM, output Intel Hex format\n");
printf(" -w write EEPROM, input binary format\n");
printf(" -wi write EEPROM, input Intel Hex format\n");
printf("Usage: eepromtool ifname slave OPTION fname|alias|serial_no\n");
printf("ifname = name of the network interface, e.g., eth0\n");
printf("slave = slave number in EtherCAT order (1..n)\n");
printf("Options:\n");
printf(" -i Display EEPROM information\n");
printf(" -wa Write slave alias in decimal format\n");
printf(" -wah Write slave alias in hexadecimal format\n");
printf(" -wsn Write serial number in decimal format\n");
printf(" -wsnh Write serial number in hexadecimal format\n");
printf(" -r Read EEPROM and output in binary format\n");
printf(" -ri Read EEPROM and output in Intel Hex format\n");
printf(" -w Write EEPROM using binary input format\n");
printf(" -wi Write EEPROM using Intel Hex input format\n");
printf("\nAvailable adapters:\n");
adapter = ec_find_adapters();
@ -501,5 +575,5 @@ int main(int argc, char *argv[])
printf("End program\n");
return (0);
return rc;
}