38 #define MODE_READBIN 1 39 #define MODE_READINTEL 2 40 #define MODE_WRITEBIN 3 41 #define MODE_WRITEINTEL 4 42 #define MODE_WRITEALIAS 5 45 #define MAXSLENGTH 256 53 struct timeval tstart,tend,
tdif;
58 #define IHEXLENGTH 0x20 66 fp = fopen(fname,
"rb");
69 while (((c = fgetc(fp)) != EOF) && (cc <
MAXBUF))
81 int c, sc, retval = 1;
82 int ll, ladr, lt, sn, i, lval;
83 int hstart, hlength, sum;
85 fp = fopen(fname,
"r");
95 while (((c = fgetc(fp)) != EOF) && (c != 0x0A) && (sc < (
MAXSLENGTH -1)))
97 if ((c != EOF) && ((sc < 11) || (
sline[0] !=
':')))
101 printf(
"Invalid Intel Hex format.\n");
105 sn = sscanf(
sline ,
":%2x%4x%2x", &ll, &ladr, <);
106 if ((sn == 3) && ((ladr + ll) <=
MAXBUF) && (lt == 0))
108 sum = ll + (ladr >> 8) + (ladr & 0xff) + lt;
109 if(ladr < hstart) hstart = ladr;
110 for(i = 0; i < ll ; i++)
112 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
116 if(((ladr + ll) - hstart) > hlength)
117 hlength = (ladr + ll) - hstart;
118 sum = (0x100 - sum) & 0xff;
119 sn = sscanf(&
sline[9 + (i << 1)],
"%2x", &lval);
120 if (!sn || ((sum - lval) != 0))
124 printf(
"Invalid checksum.\n");
146 fp = fopen(fname,
"wb");
149 for (cc = 0 ; cc < length ; cc++)
150 fputc(
ebuf[cc], fp);
160 int cc = 0, ll, sum, i;
162 fp = fopen(fname,
"w");
169 sum = ll + (cc >> 8) + (cc & 0xff);
170 fprintf(fp,
":%2.2X%4.4X00", ll, cc);
171 for (i = 0; i < ll; i++)
173 fprintf(fp,
"%2.2X",
ebuf[cc + i]);
176 fprintf(fp,
"%2.2X\n", (0x100 - sum) & 0xff);
179 fprintf(fp,
":00000001FF\n");
204 estat =
etohs(estat);
208 for (i = start ; i < (start + length) ; i+=
ainc)
213 ebuf[i+2] = b8 >> 16;
214 ebuf[i+3] = b8 >> 24;
215 ebuf[i+4] = b8 >> 32;
216 ebuf[i+5] = b8 >> 40;
217 ebuf[i+6] = b8 >> 48;
218 ebuf[i+7] = b8 >> 56;
223 for (i = start ; i < (start + length) ; i+=
ainc)
228 ebuf[i+2] = b4 >> 16;
229 ebuf[i+3] = b4 >> 24;
256 for (i = start ; i < (start + length) ; i+=2)
280 if((
ec_slavecount >= slave) && (slave > 0) && (alias <= 0xffff))
298 int w, rc = 0, estart, esize;
304 printf(
"ec_init on %s succeeded.\n",ifname);
321 printf(
"Slave %d data\n", slave);
322 printf(
" PDI Control : %4.4X\n",*(wbuf + 0x00));
323 printf(
" PDI Config : %4.4X\n",*(wbuf + 0x01));
324 printf(
" Config Alias : %4.4X\n",*(wbuf + 0x04));
325 printf(
" Checksum : %4.4X\n",*(wbuf + 0x07));
326 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
327 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
328 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
329 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
330 printf(
" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
331 esize = (*(wbuf + 0x3E) + 1) * 128;
333 printf(
" Size : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
334 printf(
" Version : %4.4X\n",*(wbuf + 0x3F));
346 printf(
"\nTotal EEPROM read time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
357 printf(
"Slave %d\n", slave);
358 printf(
" Vendor ID : %8.8X\n",*(
uint32 *)(wbuf + 0x08));
359 printf(
" Product Code : %8.8X\n",*(
uint32 *)(wbuf + 0x0A));
360 printf(
" Revision Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0C));
361 printf(
" Serial Number : %8.8X\n",*(
uint32 *)(wbuf + 0x0E));
370 printf(
"\nTotal EEPROM write time :%ldms\n", (
tdif.tv_usec+(
tdif.tv_sec*1000000L)) / 1000);
373 printf(
"Error reading file, abort.\n");
378 printf(
"Alias %4.4X written successfully to slave %d\n");
380 printf(
"Alias not written\n");
384 printf(
"Slave number outside range.\n");
387 printf(
"No slaves found!\n");
388 printf(
"End, close socket\n");
393 printf(
"No socket connection on %s\nExcecute as root\n",ifname);
396 int main(
int argc,
char *argv[])
398 printf(
"SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
403 slave = atoi(argv[2]);
404 if ((strncmp(argv[3],
"-i",
sizeof(
"-i")) == 0))
mode =
MODE_INFO;
411 if ((strncmp(argv[3],
"-walias",
sizeof(
"-walias")) == 0))
414 alias = atoi(argv(4));
422 printf(
"Usage: eepromtool ifname slave OPTION fname|alias\n");
423 printf(
"ifname = eth0 for example\n");
424 printf(
"slave = slave number in EtherCAT order 1..n\n");
425 printf(
" -i display EEPROM information\n");
426 printf(
" -walias write slave alias\n");
427 printf(
" -r read EEPROM, output binary format\n");
428 printf(
" -ri read EEPROM, output Intel Hex format\n");
429 printf(
" -w write EEPROM, input binary format\n");
430 printf(
" -wi write EEPROM, input Intel Hex format\n");
433 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)