29 #define MODE_READBIN 1
30 #define MODE_READINTEL 2
31 #define MODE_WRITEBIN 3
32 #define MODE_WRITEINTEL 4
33 #define MODE_WRITEALIAS 5
36 #define MAXSLENGTH 256
49 #define IHEXLENGTH 0x20
55 for(
j = 0;
j <= 7 ;
j++ )
58 *crc = (*crc << 1) ^ 0x07;
70 for( i = 0 ; i <= 13 ; i++ )
83 fp = fopen(fname,
"rb");
86 while (((c = fgetc(fp)) != EOF) && (cc <
MAXBUF))
98 int c, sc, retval = 1;
99 int ll, ladr, lt, sn, i, lval;
100 int hstart, hlength, sum;
102 fp = fopen(fname,
"r");
112 while (((c = fgetc(fp)) != EOF) && (c != 0x0A) && (sc < (
MAXSLENGTH -1)))
114 if ((c != EOF) && ((sc < 11) || (
sline[0] !=
':')))
118 printf(
"Invalid Intel Hex format.\n");
122 sn = sscanf(
sline ,
":%2x%4x%2x", &ll, &ladr, <);
123 if ((sn == 3) && ((ladr + ll) <=
MAXBUF) && (lt == 0))
125 sum = ll + (ladr >> 8) + (ladr & 0xff) + lt;
126 if(ladr < hstart) hstart = ladr;
127 for(i = 0; i < ll ; i++)
129 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
133 if(((ladr + ll) - hstart) > hlength)
134 hlength = (ladr + ll) - hstart;
135 sum = (0x100 - sum) & 0xff;
136 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
137 if (!sn || ((sum - lval) != 0))
141 printf(
"Invalid checksum.\n");
163 fp = fopen(fname,
"wb");
166 for (cc = 0 ; cc < length ; cc++)
167 fputc(
ebuf[cc], fp);
177 int cc = 0, ll, sum, i;
179 fp = fopen(fname,
"w");
186 sum = ll + (cc >> 8) + (cc & 0xff);
187 fprintf(fp,
":%2.2X%4.4X00", ll, cc);
188 for (i = 0; i < ll; i++)
190 fprintf(fp,
"%2.2X",
ebuf[cc + i]);
193 fprintf(fp,
"%2.2X\n", (0x100 - sum) & 0xff);
196 fprintf(fp,
":00000001FF\n");
221 estat =
etohs(estat);
225 for (i = start ; i < (start + length) ; i+=
ainc)
229 ebuf[i+1] = (b8 >> 8) & 0xFF;
230 ebuf[i+2] = (b8 >> 16) & 0xFF;
231 ebuf[i+3] = (b8 >> 24) & 0xFF;
232 ebuf[i+4] = (b8 >> 32) & 0xFF;
233 ebuf[i+5] = (b8 >> 40) & 0xFF;
234 ebuf[i+6] = (b8 >> 48) & 0xFF;
235 ebuf[i+7] = (b8 >> 56) & 0xFF;
240 for (i = start ; i < (start + length) ; i+=
ainc)
244 ebuf[i+1] = (b4 >> 8) & 0xFF;
245 ebuf[i+2] = (b4 >> 16) & 0xFF;
246 ebuf[i+3] = (b4 >> 24) & 0xFF;
272 for (i = start ; i < (start + length) ; i+=2)
315 int w, rc = 0, estart, esize;
321 printf(
"ec_init on %s succeeded.\n",ifname);
338 printf(
"Slave %d data\n",
slave);
339 printf(
" PDI Control : %4.4X\n",*(wbuf + 0x00));
340 printf(
" PDI Config : %4.4X\n",*(wbuf + 0x01));
341 printf(
" Config Alias : %4.4X\n",*(wbuf + 0x04));
342 printf(
" Checksum : %4.4X\n",*(wbuf + 0x07));
343 printf(
" calculated : %4.4X\n",
SIIcrc(&
ebuf[0]));
344 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
345 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
346 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
347 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
348 printf(
" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
349 esize = (*(wbuf + 0x3E) + 1) * 128;
351 printf(
" Size : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
352 printf(
" Version : %4.4X\n",*(wbuf + 0x3F));
364 printf(
"\nTotal EEPROM read time :%ldms\n", (
tdif.
usec+(
tdif.
sec*1000000L)) / 1000);
375 printf(
"Slave %d\n",
slave);
376 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
377 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
378 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
379 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
388 printf(
"\nTotal EEPROM write time :%ldms\n", (
tdif.
usec+(
tdif.
sec*1000000L)) / 1000);
391 printf(
"Error reading file, abort.\n");
398 *(wbuf + 0x04) =
alias;
401 printf(
"Alias %4.4X written successfully to slave %d\n",
alias,
slave);
405 printf(
"Alias not written\n");
410 printf(
"Could not read slave EEPROM");
416 printf(
"Slave number outside range.\n");
421 printf(
"No slaves found!\n");
423 printf(
"End, close socket\n");
429 printf(
"No socket connection on %s\nExcecute as root\n",ifname);
433 int main(
int argc,
char *argv[])
435 printf(
"SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
440 slave = atoi(argv[2]);
441 if ((strncmp(argv[3],
"-i",
sizeof(
"-i")) == 0))
mode =
MODE_INFO;
448 if ((strncmp(argv[3],
"-walias",
sizeof(
"-walias")) == 0))
451 alias = atoi(argv[4]);
459 printf(
"Usage: eepromtool ifname slave OPTION fname|alias\n");
460 printf(
"ifname = eth0 for example\n");
461 printf(
"slave = slave number in EtherCAT order 1..n\n");
462 printf(
" -i display EEPROM information\n");
463 printf(
" -walias write slave alias\n");
464 printf(
" -r read EEPROM, output binary format\n");
465 printf(
" -ri read EEPROM, output Intel Hex format\n");
466 printf(
" -w write EEPROM, input binary format\n");
467 printf(
" -wi write EEPROM, input Intel Hex format\n");
470 printf(
"End program\n");