00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <sys/time.h>
00019 #include <unistd.h>
00020 #include <time.h>
00021
00022 #include "ethercattype.h"
00023 #include "nicdrv.h"
00024 #include "ethercatbase.h"
00025 #include "ethercatmain.h"
00026 #include "ethercatcoe.h"
00027
00028
00029
00030
00031 #define MAXBUF 32768
00032 #define STDBUF 2048
00033 #define MINBUF 128
00034
00035 #define MODE_NONE 0
00036 #define MODE_READBIN 1
00037 #define MODE_READINTEL 2
00038 #define MODE_WRITEBIN 3
00039 #define MODE_WRITEINTEL 4
00040
00041 #define MAXSLENGTH 256
00042
00043 uint8 ebuf[MAXBUF];
00044 uint8 ob;
00045 uint16 ow;
00046 int os;
00047 int slave;
00048 struct timeval tstart,tend, tdif;
00049 int wkc;
00050 int mode;
00051 char sline[MAXSLENGTH];
00052
00053 #define IHEXLENGTH 0x20
00054
00055 int input_bin(char *fname, int *length)
00056 {
00057 FILE *fp;
00058
00059 int cc = 0, c;
00060
00061 fp = fopen(fname, "rb");
00062 if(fp == NULL)
00063 return 0;
00064 while (((c = fgetc(fp)) != EOF) && (cc < MAXBUF))
00065 ebuf[cc++] = (uint8)c;
00066 *length = cc;
00067 fclose(fp);
00068 return 1;
00069 }
00070
00071 int input_intelhex(char *fname, int *start, int *length)
00072 {
00073 FILE *fp;
00074
00075 int c, sc, retval = 1;
00076 int ll, ladr, lt, sn, i, lval;
00077 int hstart, hlength, sum;
00078
00079 fp = fopen(fname, "r");
00080 if(fp == NULL)
00081 return 0;
00082 hstart = MAXBUF;
00083 hlength = 0;
00084 sum = 0;
00085 do
00086 {
00087 memset(sline, 0x00, MAXSLENGTH);
00088 sc = 0;
00089 while (((c = fgetc(fp)) != EOF) && (c != 0x0A) && (sc < (MAXSLENGTH -1)))
00090 sline[sc++] = (uint8)c;
00091 if ((c != EOF) && ((sc < 11) || (sline[0] != ':')))
00092 {
00093 c = EOF;
00094 retval = 0;
00095 printf("Invalid Intel Hex format.\n");
00096 }
00097 if (c != EOF)
00098 {
00099 sn = sscanf(sline , ":%2x%4x%2x", &ll, &ladr, <);
00100 if ((sn == 3) && ((ladr + ll) <= MAXBUF) && (lt == 0))
00101 {
00102 sum = ll + (ladr >> 8) + (ladr & 0xff) + lt;
00103 if(ladr < hstart) hstart = ladr;
00104 for(i = 0; i < ll ; i++)
00105 {
00106 sn = sscanf(&sline[9 + (i << 1)], "%2x", &lval);
00107 ebuf[ladr + i] = (uint8)lval;
00108 sum += (uint8)lval;
00109 }
00110 if(((ladr + ll) - hstart) > hlength)
00111 hlength = (ladr + ll) - hstart;
00112 sum = (0x100 - sum) & 0xff;
00113 sn = sscanf(&sline[9 + (i << 1)], "%2x", &lval);
00114 if (!sn || ((sum - lval) != 0))
00115 {
00116 c = EOF;
00117 retval = 0;
00118 printf("Invalid checksum.\n");
00119 }
00120 }
00121 }
00122 }
00123 while (c != EOF);
00124 if (retval)
00125 {
00126 *length = hlength;
00127 *start = hstart;
00128 }
00129 fclose(fp);
00130 return retval;
00131 }
00132
00133 int output_bin(char *fname, int length)
00134 {
00135 FILE *fp;
00136
00137 int cc;
00138
00139 fp = fopen(fname, "wb");
00140 if(fp == NULL)
00141 return 0;
00142 for (cc = 0 ; cc < length ; cc++)
00143 fputc( ebuf[cc], fp);
00144 fclose(fp);
00145 return 1;
00146 }
00147
00148 int output_intelhex(char *fname, int length)
00149 {
00150 FILE *fp;
00151
00152 int cc = 0, ll, sum, i;
00153
00154 fp = fopen(fname, "w");
00155 if(fp == NULL)
00156 return 0;
00157 while (cc < length)
00158 {
00159 ll = length - cc;
00160 if (ll > IHEXLENGTH) ll = IHEXLENGTH;
00161 sum = ll + (cc >> 8) + (cc & 0xff);
00162 fprintf(fp, ":%2.2X%4.4X00", ll, cc);
00163 for (i = 0; i < ll; i++)
00164 {
00165 fprintf(fp, "%2.2X", ebuf[cc + i]);
00166 sum += ebuf[cc + i];
00167 }
00168 fprintf(fp, "%2.2X\n", (0x100 - sum) & 0xff);
00169 cc += ll;
00170 }
00171 fprintf(fp, ":00000001FF\n");
00172 fclose(fp);
00173 return 1;
00174 }
00175
00176 int eeprom_read(int slave, int start, int length)
00177 {
00178 int i, wkc, ainc = 4;
00179 uint16 estat, aiadr;
00180 uint32 b4;
00181 uint64 b8;
00182 uint8 eepctl;
00183
00184 if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= MAXBUF))
00185 {
00186 aiadr = 1 - slave;
00187 eepctl = 2;
00188 wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET);
00189 eepctl = 0;
00190 wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET);
00191
00192 estat = 0x0000;
00193 aiadr = 1 - slave;
00194 wkc=ec_APRD(aiadr, ECT_REG_EEPSTAT, sizeof(estat), &estat, EC_TIMEOUTRET);
00195 estat = etohs(estat);
00196 if (estat & EC_ESTAT_R64)
00197 {
00198 ainc = 8;
00199 for (i = start ; i < (start + length) ; i+=ainc)
00200 {
00201 b8 = ec_readeepromAP(aiadr, i >> 1 , EC_TIMEOUTEEP);
00202 ebuf[i] = b8;
00203 ebuf[i+1] = b8 >> 8;
00204 ebuf[i+2] = b8 >> 16;
00205 ebuf[i+3] = b8 >> 24;
00206 ebuf[i+4] = b8 >> 32;
00207 ebuf[i+5] = b8 >> 40;
00208 ebuf[i+6] = b8 >> 48;
00209 ebuf[i+7] = b8 >> 56;
00210 }
00211 }
00212 else
00213 {
00214 for (i = start ; i < (start + length) ; i+=ainc)
00215 {
00216 b4 = ec_readeepromAP(aiadr, i >> 1 , EC_TIMEOUTEEP);
00217 ebuf[i] = b4;
00218 ebuf[i+1] = b4 >> 8;
00219 ebuf[i+2] = b4 >> 16;
00220 ebuf[i+3] = b4 >> 24;
00221 }
00222 }
00223 return 1;
00224 }
00225 return 0;
00226 }
00227
00228 int eeprom_write(int slave, int start, int length)
00229 {
00230 int i, wkc, dc = 0;
00231 uint16 aiadr, *wbuf;
00232 uint8 eepctl;
00233 int ret;
00234
00235 if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= MAXBUF))
00236 {
00237 aiadr = 1 - slave;
00238 eepctl = 2;
00239 wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET);
00240 eepctl = 0;
00241 wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET);
00242
00243 aiadr = 1 - slave;
00244 wbuf = (uint16 *)&ebuf[0];
00245 for (i = start ; i < (start + length) ; i+=2)
00246 {
00247 ret = ec_writeeepromAP(aiadr, i >> 1 , *(wbuf + (i >> 1)), EC_TIMEOUTEEP);
00248 if (++dc >= 100)
00249 {
00250 dc = 0;
00251 printf(".");
00252 fflush(stdout);
00253 }
00254 }
00255 return 1;
00256 }
00257 return 0;
00258 }
00259
00260 void eepromtool(char *ifname, int slave, int mode, char *fname)
00261 {
00262 int w, rc = 0, estart, esize;
00263 uint16 *wbuf;
00264
00265
00266 if (ec_init(ifname))
00267 {
00268 printf("ec_init on %s succeeded.\n",ifname);
00269
00270 w = 0x0000;
00271 wkc = ec_BRD(0x0000, ECT_REG_TYPE, sizeof(w), &w, EC_TIMEOUTSAFE);
00272 if (wkc > 0)
00273 {
00274 ec_slavecount = wkc;
00275
00276 printf("%d slaves found.\n",ec_slavecount);
00277 if((ec_slavecount >= slave) && (slave > 0))
00278 {
00279 if ((mode == MODE_READBIN) || (mode == MODE_READINTEL))
00280 {
00281 rc = gettimeofday(&tstart, NULL);
00282 eeprom_read(slave, 0x0000, MINBUF);
00283
00284 wbuf = (uint16 *)&ebuf[0];
00285 printf("Slave %d data\n", slave);
00286 printf(" PDI Control : %4.4X\n",*(wbuf + 0x00));
00287 printf(" PDI Config : %4.4X\n",*(wbuf + 0x01));
00288 printf(" Config Alias : %4.4X\n",*(wbuf + 0x04));
00289 printf(" Checksum : %4.4X\n",*(wbuf + 0x07));
00290 printf(" Vendor ID : %8.8X\n",*(uint32 *)(wbuf + 0x08));
00291 printf(" Product Code : %8.8X\n",*(uint32 *)(wbuf + 0x0A));
00292 printf(" Revision Number : %8.8X\n",*(uint32 *)(wbuf + 0x0C));
00293 printf(" Serial Number : %8.8X\n",*(uint32 *)(wbuf + 0x0E));
00294 printf(" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
00295 esize = (*(wbuf + 0x3E) + 1) * 128;
00296 if (esize > MAXBUF) esize = MAXBUF;
00297 printf(" Size : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
00298 printf(" Version : %4.4X\n",*(wbuf + 0x3F));
00299
00300 if (esize > MINBUF)
00301 eeprom_read(slave, MINBUF, esize - MINBUF);
00302
00303 rc = gettimeofday(&tend, NULL);
00304 timersub(&tend, &tstart, &tdif);
00305 if (mode == MODE_READINTEL) output_intelhex(fname, esize);
00306 if (mode == MODE_READBIN) output_bin(fname, esize);
00307
00308 printf("\nTotal EEPROM read time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
00309 }
00310 if ((mode == MODE_WRITEBIN) || (mode == MODE_WRITEINTEL))
00311 {
00312 estart = 0;
00313 if (mode == MODE_WRITEINTEL) rc = input_intelhex(fname, &estart, &esize);
00314 if (mode == MODE_WRITEBIN) rc = input_bin(fname, &esize);
00315
00316 if (rc > 0)
00317 {
00318 wbuf = (uint16 *)&ebuf[0];
00319 printf("Slave %d\n", slave);
00320 printf(" Vendor ID : %8.8X\n",*(uint32 *)(wbuf + 0x08));
00321 printf(" Product Code : %8.8X\n",*(uint32 *)(wbuf + 0x0A));
00322 printf(" Revision Number : %8.8X\n",*(uint32 *)(wbuf + 0x0C));
00323 printf(" Serial Number : %8.8X\n",*(uint32 *)(wbuf + 0x0E));
00324
00325 printf("Busy");
00326 fflush(stdout);
00327 rc = gettimeofday(&tstart, NULL);
00328 eeprom_write(slave, estart, esize);
00329 rc = gettimeofday(&tend, NULL);
00330 timersub(&tend, &tstart, &tdif);
00331
00332 printf("\nTotal EEPROM write time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
00333 }
00334 else
00335 printf("Error reading file, abort.\n");
00336 }
00337 }
00338 else
00339 printf("Slave number outside range.\n");
00340 }
00341 else
00342 printf("No slaves found!\n");
00343 printf("End, close socket\n");
00344
00345 ec_close();
00346 }
00347 else
00348 printf("No socket connection on %s\nExcecute as root\n",ifname);
00349 }
00350
00351 int main(int argc, char *argv[])
00352 {
00353 printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
00354
00355 if (argc > 4)
00356 {
00357 slave = atoi(argv[2]);
00358 mode = MODE_NONE;
00359 if ((strncmp(argv[3], "-r", sizeof("-r")) == 0)) mode = MODE_READBIN;
00360 if ((strncmp(argv[3], "-ri", sizeof("-ri")) == 0)) mode = MODE_READINTEL;
00361 if ((strncmp(argv[3], "-w", sizeof("-w")) == 0)) mode = MODE_WRITEBIN;
00362 if ((strncmp(argv[3], "-wi", sizeof("-wi")) == 0)) mode = MODE_WRITEINTEL;
00363
00364
00365 eepromtool(argv[1],slave,mode,argv[4]);
00366 }
00367 else
00368 {
00369 printf("Usage: eepromtool ifname slave OPTION fname\n");
00370 printf("ifname = eth0 for example\n");
00371 printf("slave = slave number in EtherCAT order 1..n\n");
00372 printf(" -r read EEPROM, output binary format\n");
00373 printf(" -ri read EEPROM, output Intel Hex format\n");
00374 printf(" -w write EEPROM, input binary format\n");
00375 printf(" -wi write EEPROM, input Intel Hex format\n");
00376 }
00377
00378 printf("End program\n");
00379 return (0);
00380 }