36 #define MODE_READBIN 1 37 #define MODE_READINTEL 2 38 #define MODE_WRITEBIN 3 39 #define MODE_WRITEINTEL 4 41 #define MAXSLENGTH 256 48 struct timeval tstart,tend,
tdif;
53 #define IHEXLENGTH 0x20 61 fp = fopen(fname,
"rb");
64 while (((c = fgetc(fp)) != EOF) && (cc <
MAXBUF))
76 int c, sc, retval = 1;
77 int ll, ladr, lt, sn, i, lval;
78 int hstart, hlength, sum;
80 fp = fopen(fname,
"r");
90 while (((c = fgetc(fp)) != EOF) && (c != 0x0A) && (sc < (
MAXSLENGTH -1)))
92 if ((c != EOF) && ((sc < 11) || (
sline[0] !=
':')))
96 printf(
"Invalid Intel Hex format.\n");
100 sn = sscanf(
sline ,
":%2x%4x%2x", &ll, &ladr, <);
101 if ((sn == 3) && ((ladr + ll) <=
MAXBUF) && (lt == 0))
103 sum = ll + (ladr >> 8) + (ladr & 0xff) + lt;
104 if(ladr < hstart) hstart = ladr;
105 for(i = 0; i < ll ; i++)
107 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
111 if(((ladr + ll) - hstart) > hlength)
112 hlength = (ladr + ll) - hstart;
113 sum = (0x100 - sum) & 0xff;
114 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
115 if (!sn || ((sum - lval) != 0))
119 printf(
"Invalid checksum.\n");
141 fp = fopen(fname,
"wb");
144 for (cc = 0 ; cc < length ; cc++)
145 fputc(
ebuf[cc], fp);
155 int cc = 0, ll, sum, i;
157 fp = fopen(fname,
"w");
164 sum = ll + (cc >> 8) + (cc & 0xff);
165 fprintf(fp,
":%2.2X%4.4X00", ll, cc);
166 for (i = 0; i < ll; i++)
168 fprintf(fp,
"%2.2X",
ebuf[cc + i]);
171 fprintf(fp,
"%2.2X\n", (0x100 - sum) & 0xff);
174 fprintf(fp,
":00000001FF\n");
199 estat =
etohs(estat);
203 for (i = start ; i < (start + length) ; i+=
ainc)
208 ebuf[i+2] = b8 >> 16;
209 ebuf[i+3] = b8 >> 24;
210 ebuf[i+4] = b8 >> 32;
211 ebuf[i+5] = b8 >> 40;
212 ebuf[i+6] = b8 >> 48;
213 ebuf[i+7] = b8 >> 56;
218 for (i = start ; i < (start + length) ; i+=
ainc)
223 ebuf[i+2] = b4 >> 16;
224 ebuf[i+3] = b4 >> 24;
251 for (i = start ; i < (start + length) ; i+=2)
270 int w, rc = 0, estart, esize;
276 printf(
"ec_init on %s succeeded.\n",ifname);
293 printf(
"Slave %d data\n", slave);
294 printf(
" PDI Control : %4.4X\n",*(wbuf + 0x00));
295 printf(
" PDI Config : %4.4X\n",*(wbuf + 0x01));
296 printf(
" Config Alias : %4.4X\n",*(wbuf + 0x04));
297 printf(
" Checksum : %4.4X\n",*(wbuf + 0x07));
298 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
299 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
300 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
301 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
302 printf(
" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
303 esize = (*(wbuf + 0x3E) + 1) * 128;
305 printf(
" Size : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
306 printf(
" Version : %4.4X\n",*(wbuf + 0x3F));
316 printf(
"\nTotal EEPROM read time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
327 printf(
"Slave %d\n", slave);
328 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
329 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
330 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
331 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
340 printf(
"\nTotal EEPROM write time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
343 printf(
"Error reading file, abort.\n");
347 printf(
"Slave number outside range.\n");
350 printf(
"No slaves found!\n");
351 printf(
"End, close socket\n");
356 printf(
"No socket connection on %s\nExcecute as root\n",ifname);
359 int main(
int argc,
char *argv[])
361 printf(
"SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
365 slave = atoi(argv[2]);
377 printf(
"Usage: eepromtool ifname slave OPTION fname\n");
378 printf(
"ifname = eth0 for example\n");
379 printf(
"slave = slave number in EtherCAT order 1..n\n");
380 printf(
" -r read EEPROM, output binary format\n");
381 printf(
" -ri read EEPROM, output Intel Hex format\n");
382 printf(
" -w write EEPROM, input binary format\n");
383 printf(
" -wi write EEPROM, input Intel Hex format\n");
386 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)