31 #define MODE_READBIN 1 32 #define MODE_READINTEL 2 33 #define MODE_WRITEBIN 3 34 #define MODE_WRITEINTEL 4 35 #define MODE_WRITEALIAS 5 38 #define MAXSLENGTH 256 51 #define IHEXLENGTH 0x20 59 fp = fopen(fname,
"rb");
62 while (((c = fgetc(fp)) != EOF) && (cc <
MAXBUF))
74 int c, sc, retval = 1;
75 int ll, ladr, lt, sn, i, lval;
76 int hstart, hlength, sum;
78 fp = fopen(fname,
"r");
88 while (((c = fgetc(fp)) != EOF) && (c != 0x0A) && (sc < (
MAXSLENGTH -1)))
90 if ((c != EOF) && ((sc < 11) || (
sline[0] !=
':')))
94 printf(
"Invalid Intel Hex format.\n");
98 sn = sscanf(
sline ,
":%2x%4x%2x", &ll, &ladr, <);
99 if ((sn == 3) && ((ladr + ll) <=
MAXBUF) && (lt == 0))
101 sum = ll + (ladr >> 8) + (ladr & 0xff) + lt;
102 if(ladr < hstart) hstart = ladr;
103 for(i = 0; i < ll ; i++)
105 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
109 if(((ladr + ll) - hstart) > hlength)
110 hlength = (ladr + ll) - hstart;
111 sum = (0x100 - sum) & 0xff;
112 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
113 if (!sn || ((sum - lval) != 0))
117 printf(
"Invalid checksum.\n");
139 fp = fopen(fname,
"wb");
142 for (cc = 0 ; cc < length ; cc++)
143 fputc(
ebuf[cc], fp);
153 int cc = 0, ll, sum, i;
155 fp = fopen(fname,
"w");
162 sum = ll + (cc >> 8) + (cc & 0xff);
163 fprintf(fp,
":%2.2X%4.4X00", ll, cc);
164 for (i = 0; i < ll; i++)
166 fprintf(fp,
"%2.2X",
ebuf[cc + i]);
169 fprintf(fp,
"%2.2X\n", (0x100 - sum) & 0xff);
172 fprintf(fp,
":00000001FF\n");
197 estat =
etohs(estat);
201 for (i = start ; i < (start + length) ; i+=
ainc)
206 ebuf[i+2] = b8 >> 16;
207 ebuf[i+3] = b8 >> 24;
208 ebuf[i+4] = b8 >> 32;
209 ebuf[i+5] = b8 >> 40;
210 ebuf[i+6] = b8 >> 48;
211 ebuf[i+7] = b8 >> 56;
216 for (i = start ; i < (start + length) ; i+=
ainc)
221 ebuf[i+2] = b4 >> 16;
222 ebuf[i+3] = b4 >> 24;
249 for (i = start ; i < (start + length) ; i+=2)
273 if((
ec_slavecount >= slave) && (slave > 0) && (alias <= 0xffff))
291 int w, rc = 0, estart, esize;
297 printf(
"ec_init on %s succeeded.\n",ifname);
314 printf(
"Slave %d data\n", slave);
315 printf(
" PDI Control : %4.4X\n",*(wbuf + 0x00));
316 printf(
" PDI Config : %4.4X\n",*(wbuf + 0x01));
317 printf(
" Config Alias : %4.4X\n",*(wbuf + 0x04));
318 printf(
" Checksum : %4.4X\n",*(wbuf + 0x07));
319 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
320 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
321 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
322 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
323 printf(
" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
324 esize = (*(wbuf + 0x3E) + 1) * 128;
326 printf(
" Size : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
327 printf(
" Version : %4.4X\n",*(wbuf + 0x3F));
339 printf(
"\nTotal EEPROM read time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
350 printf(
"Slave %d\n", slave);
351 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
352 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
353 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
354 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
363 printf(
"\nTotal EEPROM write time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
366 printf(
"Error reading file, abort.\n");
371 printf(
"Alias %4.4X written successfully to slave %d\n");
373 printf(
"Alias not written\n");
377 printf(
"Slave number outside range.\n");
380 printf(
"No slaves found!\n");
381 printf(
"End, close socket\n");
386 printf(
"No socket connection on %s\nExcecute as root\n",ifname);
389 int main(
int argc,
char *argv[])
391 printf(
"SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
396 slave = atoi(argv[2]);
397 if ((strncmp(argv[3],
"-i",
sizeof(
"-i")) == 0))
mode =
MODE_INFO;
404 if ((strncmp(argv[3],
"-walias",
sizeof(
"-walias")) == 0))
407 alias = atoi(argv[4]);
415 printf(
"Usage: eepromtool ifname slave OPTION fname|alias\n");
416 printf(
"ifname = eth0 for example\n");
417 printf(
"slave = slave number in EtherCAT order 1..n\n");
418 printf(
" -i display EEPROM information\n");
419 printf(
" -walias write slave alias\n");
420 printf(
" -r read EEPROM, output binary format\n");
421 printf(
" -ri read EEPROM, output Intel Hex format\n");
422 printf(
" -w write EEPROM, input binary format\n");
423 printf(
" -wi write EEPROM, input Intel Hex format\n");
426 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)
Headerfile for all ethercat headers.
int ec_init(const char *ifname)
int ec_BRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
uint64 ec_readeepromAP(uint16 aiadr, uint16 eeproma, int timeout)
int gettimeofday(struct timeval *tp, void *tzp)