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


ethercat_soem
Author(s): Arthur Ketels, M.J.G. van de Molengraft
autogenerated on Wed Aug 26 2015 11:32:40