win32/firm_update/firm_update.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/time.h>
19 #include <unistd.h>
20 
21 #include "ethercat.h"
22 
23 #define FWBUFSIZE (8 * 1024 * 1024)
24 
28 char filename[256];
29 char filebuffer[FWBUFSIZE]; // 8MB buffer
31 int j;
33 
34 int input_bin(char *fname, int *length)
35 {
36  FILE *fp;
37 
38  int cc = 0, c;
39 
40  fp = fopen(fname, "rb");
41  if(fp == NULL)
42  return 0;
43  while (((c = fgetc(fp)) != EOF) && (cc < FWBUFSIZE))
44  filebuffer[cc++] = (uint8)c;
45  *length = cc;
46  fclose(fp);
47  return 1;
48 }
49 
50 
51 void boottest(char *ifname, uint16 slave, char *filename)
52 {
53  printf("Starting firmware update example\n");
54 
55  /* initialise SOEM, bind socket to ifname */
56  if (ec_init(ifname))
57  {
58  printf("ec_init on %s succeeded.\n",ifname);
59  /* find and auto-config slaves */
60 
61 
62  if ( ec_config_init(FALSE) > 0 )
63  {
64  printf("%d slaves found and configured.\n",ec_slavecount);
65 
66  printf("Request init state for slave %d\n", slave);
68  ec_writestate(slave);
69 
70  /* wait for slave to reach INIT state */
72  printf("Slave %d state to INIT.\n", slave);
73 
74  /* read BOOT mailbox data, master -> slave */
76  ec_slave[slave].SM[0].StartAddr = (uint16)LO_WORD(data);
77  ec_slave[slave].SM[0].SMlength = (uint16)HI_WORD(data);
78  /* store boot write mailbox address */
80  /* store boot write mailbox size */
82 
83  /* read BOOT mailbox data, slave -> master */
85  ec_slave[slave].SM[1].StartAddr = (uint16)LO_WORD(data);
86  ec_slave[slave].SM[1].SMlength = (uint16)HI_WORD(data);
87  /* store boot read mailbox address */
89  /* store boot read mailbox size */
91 
92  printf(" SM0 A:%4.4x L:%4d F:%8.8x\n", ec_slave[slave].SM[0].StartAddr, ec_slave[slave].SM[0].SMlength,
93  (int)ec_slave[slave].SM[0].SMflags);
94  printf(" SM1 A:%4.4x L:%4d F:%8.8x\n", ec_slave[slave].SM[1].StartAddr, ec_slave[slave].SM[1].SMlength,
95  (int)ec_slave[slave].SM[1].SMflags);
96  /* program SM0 mailbox in for slave */
97  ec_FPWR (ec_slave[slave].configadr, ECT_REG_SM0, sizeof(ec_smt), &ec_slave[slave].SM[0], EC_TIMEOUTRET);
98  /* program SM1 mailbox out for slave */
99  ec_FPWR (ec_slave[slave].configadr, ECT_REG_SM1, sizeof(ec_smt), &ec_slave[slave].SM[1], EC_TIMEOUTRET);
100 
101  printf("Request BOOT state for slave %d\n", slave);
103  ec_writestate(slave);
104 
105  /* wait for slave to reach BOOT state */
107  {
108  printf("Slave %d state to BOOT.\n", slave);
109 
110  if (input_bin(filename, &filesize))
111  {
112  printf("File read OK, %d bytes.\n",filesize);
113  printf("FoE write....");
114  j = ec_FOEwrite(slave, filename, 0, filesize , &filebuffer, EC_TIMEOUTSTATE);
115  printf("result %d.\n",j);
116  printf("Request init state for slave %d\n", slave);
118  ec_writestate(slave);
119  }
120  else
121  printf("File not read OK.\n");
122  }
123 
124  }
125  else
126  {
127  printf("No slaves found!\n");
128  }
129  printf("End firmware update example, close socket\n");
130  /* stop SOEM, close socket */
131  ec_close();
132  }
133  else
134  {
135  printf("No socket connection on %s\nExcecute as root\n",ifname);
136  }
137 }
138 
139 int main(int argc, char *argv[])
140 {
141  printf("SOEM (Simple Open EtherCAT Master)\nFirmware update example\n");
142 
143  if (argc > 3)
144  {
145  argslave = atoi(argv[2]);
146  boottest(argv[1], argslave, argv[3]);
147  }
148  else
149  {
150  printf("Usage: firm_update ifname1 slave fname\n");
151  printf("ifname = eth0 for example\n");
152  printf("slave = slave number in EtherCAT order 1..n\n");
153  printf("fname = binary file to store in slave\n");
154  printf("CAUTION! Using the wrong file can result in a bricked slave!\n");
155  }
156 
157  printf("End program\n");
158  return (0);
159 }
ec_smt SM[EC_MAXSM]
Definition: ethercatmain.h:143
uint16 mbx_ro
Definition: ethercatmain.h:163
int ec_FOEwrite(uint16 slave, char *filename, uint32 password, int psize, void *p, int timeout)
Definition: ethercatfoe.c:366
#define EC_TIMEOUTSTATE
Definition: ethercattype.h:76
uint8_t uint8
Definition: osal.h:28
uint16 mbx_rl
Definition: ethercatmain.h:161
Headerfile for all ethercat headers.
uint16 state
Definition: ethercatmain.h:109
int ec_init(const char *ifname)
uint16_t uint16
Definition: osal.h:29
int main(int argc, char *argv[])
uint32 ec_readeeprom(uint16 slave, uint16 eeproma, int timeout)
char filename[256]
char filebuffer[FWBUFSIZE]
PACKED_END PACKED_BEGIN struct PACKED ec_sm ec_smt
int ec_slavecount
Definition: ethercatmain.c:69
int slave
Definition: aliastool.c:44
#define FALSE
Definition: osal.h:22
#define EC_TIMEOUTEEP
Definition: ethercattype.h:70
#define HI_WORD(l)
Definition: ethercattype.h:520
#define EC_TIMEOUTRET
Definition: ethercattype.h:64
#define LO_WORD(l)
Definition: ethercattype.h:518
int ec_writestate(uint16 slave)
int ec_FPWR(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
Definition: ethercatbase.c:608
uint16 mbx_wo
Definition: ethercatmain.h:159
uint32_t uint32
Definition: osal.h:30
uint16 mbx_l
Definition: ethercatmain.h:157
int input_bin(char *fname, int *length)
void ec_close(void)
uint16 ec_statecheck(uint16 slave, uint16 reqstate, int timeout)
#define FWBUFSIZE
void boottest(char *ifname, uint16 slave, char *filename)
int ec_config_init(uint8 usetable)


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Sat Jun 27 2020 03:48:21