39 #define MODE_READBIN 1 40 #define MODE_READINTEL 2 41 #define MODE_WRITEBIN 3 42 #define MODE_WRITEINTEL 4 43 #define MODE_WRITEALIAS 5 46 #define MAXSLENGTH 256 54 struct timeval tstart,tend,
tdif;
59 #define IHEXLENGTH 0x20 65 for(j = 0; j <= 7 ; j++ )
68 *crc = (*crc << 1) ^ 0x07;
80 for( i = 0 ; i <= 13 ; i++ )
93 fp = fopen(fname,
"rb");
96 while (((c = fgetc(fp)) != EOF) && (cc <
MAXBUF))
108 int c, sc, retval = 1;
109 int ll, ladr, lt, sn, i, lval;
110 int hstart, hlength, sum;
112 fp = fopen(fname,
"r");
122 while (((c = fgetc(fp)) != EOF) && (c != 0x0A) && (sc < (
MAXSLENGTH -1)))
124 if ((c != EOF) && ((sc < 11) || (
sline[0] !=
':')))
128 printf(
"Invalid Intel Hex format.\n");
132 sn = sscanf(
sline ,
":%2x%4x%2x", &ll, &ladr, <);
133 if ((sn == 3) && ((ladr + ll) <=
MAXBUF) && (lt == 0))
135 sum = ll + (ladr >> 8) + (ladr & 0xff) + lt;
136 if(ladr < hstart) hstart = ladr;
137 for(i = 0; i < ll ; i++)
139 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
143 if(((ladr + ll) - hstart) > hlength)
144 hlength = (ladr + ll) - hstart;
145 sum = (0x100 - sum) & 0xff;
146 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
147 if (!sn || ((sum - lval) != 0))
151 printf(
"Invalid checksum.\n");
173 fp = fopen(fname,
"wb");
176 for (cc = 0 ; cc < length ; cc++)
177 fputc(
ebuf[cc], fp);
187 int cc = 0, ll, sum, i;
189 fp = fopen(fname,
"w");
196 sum = ll + (cc >> 8) + (cc & 0xff);
197 fprintf(fp,
":%2.2X%4.4X00", ll, cc);
198 for (i = 0; i < ll; i++)
200 fprintf(fp,
"%2.2X",
ebuf[cc + i]);
203 fprintf(fp,
"%2.2X\n", (0x100 - sum) & 0xff);
206 fprintf(fp,
":00000001FF\n");
231 estat =
etohs(estat);
235 for (i = start ; i < (start + length) ; i+=
ainc)
240 ebuf[i+2] = b8 >> 16;
241 ebuf[i+3] = b8 >> 24;
242 ebuf[i+4] = b8 >> 32;
243 ebuf[i+5] = b8 >> 40;
244 ebuf[i+6] = b8 >> 48;
245 ebuf[i+7] = b8 >> 56;
250 for (i = start ; i < (start + length) ; i+=
ainc)
255 ebuf[i+2] = b4 >> 16;
256 ebuf[i+3] = b4 >> 24;
283 for (i = start ; i < (start + length) ; i+=2)
307 if((
ec_slavecount >= slave) && (slave > 0) && (alias <= 0xffff))
327 int w, rc = 0, estart, esize;
333 printf(
"ec_init on %s succeeded.\n",ifname);
350 printf(
"Slave %d data\n", slave);
351 printf(
" PDI Control : %4.4X\n",*(wbuf + 0x00));
352 printf(
" PDI Config : %4.4X\n",*(wbuf + 0x01));
353 printf(
" Config Alias : %4.4X\n",*(wbuf + 0x04));
354 printf(
" Checksum : %4.4X\n",*(wbuf + 0x07));
355 printf(
" calculated : %4.4X\n",
SIIcrc(&
ebuf[0]));
356 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
357 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
358 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
359 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
360 printf(
" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
361 esize = (*(wbuf + 0x3E) + 1) * 128;
363 printf(
" Size : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
364 printf(
" Version : %4.4X\n",*(wbuf + 0x3F));
376 printf(
"\nTotal EEPROM read time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
387 printf(
"Slave %d\n", slave);
388 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
389 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
390 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
391 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
400 printf(
"\nTotal EEPROM write time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
403 printf(
"Error reading file, abort.\n");
410 *(wbuf + 0x04) =
alias;
413 printf(
"Alias %4.4X written successfully to slave %d\n",
alias, slave);
417 printf(
"Alias not written\n");
422 printf(
"Could not read slave EEPROM");
428 printf(
"Slave number outside range.\n");
433 printf(
"No slaves found!\n");
435 printf(
"End, close socket\n");
441 printf(
"No socket connection on %s\nExcecute as root\n",ifname);
445 int main(
int argc,
char *argv[])
447 printf(
"SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
452 slave = atoi(argv[2]);
453 if ((strncmp(argv[3],
"-i",
sizeof(
"-i")) == 0))
mode =
MODE_INFO;
460 if ((strncmp(argv[3],
"-walias",
sizeof(
"-walias")) == 0))
463 alias = atoi(argv[4]);
471 printf(
"Usage: eepromtool ifname slave OPTION fname|alias\n");
472 printf(
"ifname = eth0 for example\n");
473 printf(
"slave = slave number in EtherCAT order 1..n\n");
474 printf(
" -i display EEPROM information\n");
475 printf(
" -walias write slave alias\n");
476 printf(
" -r read EEPROM, output binary format\n");
477 printf(
" -ri read EEPROM, output Intel Hex format\n");
478 printf(
" -w write EEPROM, input binary format\n");
479 printf(
" -wi write EEPROM, input Intel Hex format\n");
482 printf(
"End program\n");
int ec_writeeepromAP(uint16 aiadr, uint16 eeproma, uint16 data, int timeout)
int ec_APWR(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
int ec_APRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
#define timersub(a, b, result)
General typedefs and defines for EtherCAT.
Headerfile for ethercatcoe.c.
int ec_BRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
int ec_init(char *ifname)
Headerfile for ethercatbase.c.
uint64 ec_readeepromAP(uint16 aiadr, uint16 eeproma, int timeout)
Headerfile for ethercatmain.c.
int gettimeofday(struct timeval *tp, void *tzp)