aliastool.c
Go to the documentation of this file.
1 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <unistd.h>
22 #include <time.h>
23 
24 #include "ethercattype.h"
25 #include "nicdrv.h"
26 #include "ethercatbase.h"
27 #include "ethercatmain.h"
28 #include "ethercatcoe.h"
29 //#include "ethercatfoe.h"
30 //#include "ethercatconfig.h"
31 //#include "ethercatprint.h"
32 
33 #define MAXBUF 32768
34 #define STDBUF 2048
35 #define MINBUF 128
36 
37 #define MODE_NONE 0
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
43 #define MODE_INFO 6
44 
45 #define MAXSLENGTH 256
46 
50 int os;
51 int slave;
52 int alias;
53 struct timeval tstart,tend, tdif;
54 int wkc;
55 int mode;
57 
58 #define IHEXLENGTH 0x20
59 
60 int input_bin(char *fname, int *length)
61 {
62  FILE *fp;
63 
64  int cc = 0, c;
65 
66  fp = fopen(fname, "rb");
67  if(fp == NULL)
68  return 0;
69  while (((c = fgetc(fp)) != EOF) && (cc < MAXBUF))
70  ebuf[cc++] = (uint8)c;
71  *length = cc;
72  fclose(fp);
73 
74  return 1;
75 }
76 
77 int input_intelhex(char *fname, int *start, int *length)
78 {
79  FILE *fp;
80 
81  int c, sc, retval = 1;
82  int ll, ladr, lt, sn, i, lval;
83  int hstart, hlength, sum;
84 
85  fp = fopen(fname, "r");
86  if(fp == NULL)
87  return 0;
88  hstart = MAXBUF;
89  hlength = 0;
90  sum = 0;
91  do
92  {
93  memset(sline, 0x00, MAXSLENGTH);
94  sc = 0;
95  while (((c = fgetc(fp)) != EOF) && (c != 0x0A) && (sc < (MAXSLENGTH -1)))
96  sline[sc++] = (uint8)c;
97  if ((c != EOF) && ((sc < 11) || (sline[0] != ':')))
98  {
99  c = EOF;
100  retval = 0;
101  printf("Invalid Intel Hex format.\n");
102  }
103  if (c != EOF)
104  {
105  sn = sscanf(sline , ":%2x%4x%2x", &ll, &ladr, &lt);
106  if ((sn == 3) && ((ladr + ll) <= MAXBUF) && (lt == 0))
107  {
108  sum = ll + (ladr >> 8) + (ladr & 0xff) + lt;
109  if(ladr < hstart) hstart = ladr;
110  for(i = 0; i < ll ; i++)
111  {
112  sn = sscanf(&sline[9 + (i << 1)], "%2x", &lval);
113  ebuf[ladr + i] = (uint8)lval;
114  sum += (uint8)lval;
115  }
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))
121  {
122  c = EOF;
123  retval = 0;
124  printf("Invalid checksum.\n");
125  }
126  }
127  }
128  }
129  while (c != EOF);
130  if (retval)
131  {
132  *length = hlength;
133  *start = hstart;
134  }
135  fclose(fp);
136 
137  return retval;
138 }
139 
140 int output_bin(char *fname, int length)
141 {
142  FILE *fp;
143 
144  int cc;
145 
146  fp = fopen(fname, "wb");
147  if(fp == NULL)
148  return 0;
149  for (cc = 0 ; cc < length ; cc++)
150  fputc( ebuf[cc], fp);
151  fclose(fp);
152 
153  return 1;
154 }
155 
156 int output_intelhex(char *fname, int length)
157 {
158  FILE *fp;
159 
160  int cc = 0, ll, sum, i;
161 
162  fp = fopen(fname, "w");
163  if(fp == NULL)
164  return 0;
165  while (cc < length)
166  {
167  ll = length - cc;
168  if (ll > IHEXLENGTH) ll = IHEXLENGTH;
169  sum = ll + (cc >> 8) + (cc & 0xff);
170  fprintf(fp, ":%2.2X%4.4X00", ll, cc);
171  for (i = 0; i < ll; i++)
172  {
173  fprintf(fp, "%2.2X", ebuf[cc + i]);
174  sum += ebuf[cc + i];
175  }
176  fprintf(fp, "%2.2X\n", (0x100 - sum) & 0xff);
177  cc += ll;
178  }
179  fprintf(fp, ":00000001FF\n");
180  fclose(fp);
181 
182  return 1;
183 }
184 
185 int eeprom_read(int slave, int start, int length)
186 {
187  int i, wkc, ainc = 4;
188  uint16 estat, aiadr;
189  uint32 b4;
190  uint64 b8;
191  uint8 eepctl;
192 
193  if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= MAXBUF))
194  {
195  aiadr = 1 - slave;
196  eepctl = 2;
197  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET); /* force Eeprom from PDI */
198  eepctl = 0;
199  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET); /* set Eeprom to master */
200 
201  estat = 0x0000;
202  aiadr = 1 - slave;
203  wkc=ec_APRD(aiadr, ECT_REG_EEPSTAT, sizeof(estat), &estat, EC_TIMEOUTRET); /* read eeprom status */
204  estat = etohs(estat);
205  if (estat & EC_ESTAT_R64)
206  {
207  ainc = 8;
208  for (i = start ; i < (start + length) ; i+=ainc)
209  {
210  b8 = ec_readeepromAP(aiadr, i >> 1 , EC_TIMEOUTEEP);
211  ebuf[i] = b8;
212  ebuf[i+1] = b8 >> 8;
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;
219  }
220  }
221  else
222  {
223  for (i = start ; i < (start + length) ; i+=ainc)
224  {
225  b4 = ec_readeepromAP(aiadr, i >> 1 , EC_TIMEOUTEEP);
226  ebuf[i] = b4;
227  ebuf[i+1] = b4 >> 8;
228  ebuf[i+2] = b4 >> 16;
229  ebuf[i+3] = b4 >> 24;
230  }
231  }
232 
233  return 1;
234  }
235 
236  return 0;
237 }
238 
239 int eeprom_write(int slave, int start, int length)
240 {
241  int i, wkc, dc = 0;
242  uint16 aiadr, *wbuf;
243  uint8 eepctl;
244  int ret;
245 
246  if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= MAXBUF))
247  {
248  aiadr = 1 - slave;
249  eepctl = 2;
250  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET); /* force Eeprom from PDI */
251  eepctl = 0;
252  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET); /* set Eeprom to master */
253 
254  aiadr = 1 - slave;
255  wbuf = (uint16 *)&ebuf[0];
256  for (i = start ; i < (start + length) ; i+=2)
257  {
258  ret = ec_writeeepromAP(aiadr, i >> 1 , *(wbuf + (i >> 1)), EC_TIMEOUTEEP);
259  if (++dc >= 100)
260  {
261  dc = 0;
262  printf(".");
263  fflush(stdout);
264  }
265  }
266 
267  return 1;
268  }
269 
270  return 0;
271 }
272 
274 {
275  int i, wkc, dc = 0;
276  uint16 aiadr, *wbuf;
277  uint8 eepctl;
278  int ret;
279 
280  if((ec_slavecount >= slave) && (slave > 0) && (alias <= 0xffff))
281  {
282  aiadr = 1 - slave;
283  eepctl = 2;
284  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET); /* force Eeprom from PDI */
285  eepctl = 0;
286  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , EC_TIMEOUTRET); /* set Eeprom to master */
287 
288  ret = ec_writeeepromAP(aiadr, 0x04 , alias, EC_TIMEOUTEEP);
289 
290  return ret;
291  }
292 
293  return 0;
294 }
295 
296 void eepromtool(char *ifname, int slave, int mode, char *fname)
297 {
298  int w, rc = 0, estart, esize;
299  uint16 *wbuf;
300 
301  /* initialise SOEM, bind socket to ifname */
302  if (ec_init(ifname))
303  {
304  printf("ec_init on %s succeeded.\n",ifname);
305 
306  w = 0x0000;
307  wkc = ec_BRD(0x0000, ECT_REG_TYPE, sizeof(w), &w, EC_TIMEOUTSAFE); /* detect number of slaves */
308  if (wkc > 0)
309  {
310  ec_slavecount = wkc;
311 
312  printf("%d slaves found.\n",ec_slavecount);
313  if((ec_slavecount >= slave) && (slave > 0))
314  {
315  if ((mode == MODE_INFO) || (mode == MODE_READBIN) || (mode == MODE_READINTEL))
316  {
317  rc = gettimeofday(&tstart, NULL);
318  eeprom_read(slave, 0x0000, MINBUF); // read first 128 bytes
319 
320  wbuf = (uint16 *)&ebuf[0];
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;
332  if (esize > MAXBUF) esize = MAXBUF;
333  printf(" Size : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
334  printf(" Version : %4.4X\n",*(wbuf + 0x3F));
335  }
336  if ((mode == MODE_READBIN) || (mode == MODE_READINTEL))
337  {
338  if (esize > MINBUF)
339  eeprom_read(slave, MINBUF, esize - MINBUF); // read reminder
340 
341  rc = gettimeofday(&tend, NULL);
342  timersub(&tend, &tstart, &tdif);
343  if (mode == MODE_READINTEL) output_intelhex(fname, esize);
344  if (mode == MODE_READBIN) output_bin(fname, esize);
345 
346  printf("\nTotal EEPROM read time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
347  }
348  if ((mode == MODE_WRITEBIN) || (mode == MODE_WRITEINTEL))
349  {
350  estart = 0;
351  if (mode == MODE_WRITEINTEL) rc = input_intelhex(fname, &estart, &esize);
352  if (mode == MODE_WRITEBIN) rc = input_bin(fname, &esize);
353 
354  if (rc > 0)
355  {
356  wbuf = (uint16 *)&ebuf[0];
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));
362 
363  printf("Busy");
364  fflush(stdout);
365  rc = gettimeofday(&tstart, NULL);
366  eeprom_write(slave, estart, esize);
367  rc = gettimeofday(&tend, NULL);
368  timersub(&tend, &tstart, &tdif);
369 
370  printf("\nTotal EEPROM write time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
371  }
372  else
373  printf("Error reading file, abort.\n");
374  }
375  if (mode == MODE_WRITEALIAS)
376  {
377  if(eeprom_writealias(slave, alias))
378  printf("Alias %4.4X written successfully to slave %d\n");
379  else
380  printf("Alias not written\n");
381  }
382  }
383  else
384  printf("Slave number outside range.\n");
385  }
386  else
387  printf("No slaves found!\n");
388  printf("End, close socket\n");
389  /* stop SOEM, close socket */
390  ec_close();
391  }
392  else
393  printf("No socket connection on %s\nExcecute as root\n",ifname);
394 }
395 
396 int main(int argc, char *argv[])
397 {
398  printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
399 
400  mode = MODE_NONE;
401  if (argc > 3)
402  {
403  slave = atoi(argv[2]);
404  if ((strncmp(argv[3], "-i", sizeof("-i")) == 0)) mode = MODE_INFO;
405  if (argc > 4)
406  {
407  if ((strncmp(argv[3], "-r", sizeof("-r")) == 0)) mode = MODE_READBIN;
408  if ((strncmp(argv[3], "-ri", sizeof("-ri")) == 0)) mode = MODE_READINTEL;
409  if ((strncmp(argv[3], "-w", sizeof("-w")) == 0)) mode = MODE_WRITEBIN;
410  if ((strncmp(argv[3], "-wi", sizeof("-wi")) == 0)) mode = MODE_WRITEINTEL;
411  if ((strncmp(argv[3], "-walias", sizeof("-walias")) == 0))
412  {
414  alias = atoi(argv(4));
415  }
416  }
417  /* start tool */
418  eepromtool(argv[1],slave,mode,argv[4]);
419  }
420  else
421  {
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");
431  }
432 
433  printf("End program\n");
434 
435  return (0);
436 }
int eeprom_read(int slave, int start, int length)
Definition: aliastool.c:185
int ec_writeeepromAP(uint16 aiadr, uint16 eeproma, uint16 data, int timeout)
int ec_APWR(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
Definition: ethercatbase.c:608
int ec_APRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
Definition: ethercatbase.c:569
#define MODE_READBIN
Definition: aliastool.c:38
#define timersub(a, b, result)
Definition: rtk/osal.c:41
#define MAXSLENGTH
Definition: aliastool.c:45
#define MAXBUF
Definition: aliastool.c:33
#define EC_TIMEOUTSAFE
Definition: ethercattype.h:94
uint8_t uint8
Definition: osal.h:33
#define MODE_WRITEINTEL
Definition: aliastool.c:41
void eepromtool(char *ifname, int slave, int mode, char *fname)
Definition: aliastool.c:296
uint16_t uint16
Definition: osal.h:34
#define MODE_INFO
Definition: aliastool.c:43
General typedefs and defines for EtherCAT.
#define etohs(A)
Definition: ethercattype.h:550
Headerfile for ethercatcoe.c.
int output_intelhex(char *fname, int length)
Definition: aliastool.c:156
struct timeval tstart tend tdif
Definition: aliastool.c:53
int alias
Definition: aliastool.c:52
int eeprom_write(int slave, int start, int length)
Definition: aliastool.c:239
int input_intelhex(char *fname, int *start, int *length)
Definition: aliastool.c:77
uint8 ob
Definition: aliastool.c:48
int ec_BRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
Definition: ethercatbase.c:564
char sline[MAXSLENGTH]
Definition: aliastool.c:56
int ec_init(char *ifname)
int os
Definition: aliastool.c:50
int slave
Definition: aliastool.c:51
#define MODE_WRITEALIAS
Definition: aliastool.c:42
#define EC_TIMEOUTEEP
Definition: ethercattype.h:96
uint64_t uint64
Definition: osal.h:37
#define EC_TIMEOUTRET
Definition: ethercattype.h:90
Headerfile for ethercatbase.c.
int wkc
Definition: aliastool.c:54
uint64 ec_readeepromAP(uint16 aiadr, uint16 eeproma, int timeout)
Headerfile for ethercatmain.c.
#define MINBUF
Definition: aliastool.c:35
int main(int argc, char *argv[])
Definition: aliastool.c:396
uint32_t uint32
Definition: osal.h:35
int gettimeofday(struct timeval *tp, void *tzp)
Definition: rtk/osal.c:65
uint8 ebuf[MAXBUF]
Definition: aliastool.c:47
#define MODE_NONE
Definition: aliastool.c:37
void ec_close(void)
int input_bin(char *fname, int *length)
Definition: aliastool.c:60
#define MODE_WRITEBIN
Definition: aliastool.c:40
int ainc
int ec_slavecount
Definition: ethercatmain.c:106
uint16 ow
Definition: aliastool.c:49
int output_bin(char *fname, int length)
Definition: aliastool.c:140
int mode
Definition: aliastool.c:55
int eeprom_writealias(int slave, int alias)
Definition: aliastool.c:273
#define EC_ESTAT_R64
Definition: ethercattype.h:293
#define IHEXLENGTH
Definition: aliastool.c:58
#define MODE_READINTEL
Definition: aliastool.c:39


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Sat Jun 8 2019 18:02:16