linux/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);
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);
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....");
115  printf("result %d.\n",j);
116  printf("Request init state for slave %d\n", 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_STATE_BOOT
@ EC_STATE_BOOT
Definition: ethercattype.h:164
slave
int slave
Definition: aliastool.c:44
ec_slave::mbx_ro
uint16 mbx_ro
Definition: ethercatmain.h:163
ec_FPWR
int ec_FPWR(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
Definition: ethercatbase.c:608
ec_smt
PACKED_END PACKED_BEGIN struct PACKED ec_sm ec_smt
ec_close
void ec_close(void)
Definition: ethercatmain.c:2048
data
uint32 data
Definition: linux/firm_update/firm_update.c:27
uint32
uint32_t uint32
Definition: osal.h:30
ethercat.h
Headerfile for all ethercat headers.
ec_init
int ec_init(const char *ifname)
Definition: ethercatmain.c:2029
ec_readeeprom
uint32 ec_readeeprom(uint16 slave, uint16 eeproma, int timeout)
Definition: ethercatmain.c:2220
ECT_SII_BOOTTXMBX
@ ECT_SII_BOOTTXMBX
Definition: ethercattype.h:305
uint8
uint8_t uint8
Definition: osal.h:28
j
int j
Definition: linux/firm_update/firm_update.c:31
ec_slave::mbx_wo
uint16 mbx_wo
Definition: ethercatmain.h:159
ec_slave::state
uint16 state
Definition: ethercatmain.h:109
ec_config_init
int ec_config_init(uint8 usetable)
Definition: ethercatconfig.c:1606
ob
uint8 ob
Definition: linux/firm_update/firm_update.c:25
EC_TIMEOUTRET
#define EC_TIMEOUTRET
Definition: ethercattype.h:64
main
int main(int argc, char *argv[])
Definition: linux/firm_update/firm_update.c:139
argslave
uint16 argslave
Definition: linux/firm_update/firm_update.c:32
filesize
int filesize
Definition: linux/firm_update/firm_update.c:30
uint16
uint16_t uint16
Definition: osal.h:29
ECT_REG_SM1
@ ECT_REG_SM1
Definition: ethercattype.h:424
EC_STATE_INIT
@ EC_STATE_INIT
Definition: ethercattype.h:160
filebuffer
char filebuffer[FWBUFSIZE]
Definition: linux/firm_update/firm_update.c:29
ec_FOEwrite
int ec_FOEwrite(uint16 slave, char *filename, uint32 password, int psize, void *p, int timeout)
Definition: ethercatfoe.c:366
ec_writestate
int ec_writestate(uint16 slave)
Definition: ethercatmain.c:2149
input_bin
int input_bin(char *fname, int *length)
Definition: linux/firm_update/firm_update.c:34
HI_WORD
#define HI_WORD(l)
Definition: ethercattype.h:520
ec_slave
Definition: ethercatmain.h:106
FALSE
#define FALSE
Definition: osal.h:22
EC_TIMEOUTEEP
#define EC_TIMEOUTEEP
Definition: ethercattype.h:70
ECT_REG_SM0
@ ECT_REG_SM0
Definition: ethercattype.h:423
EC_TIMEOUTSTATE
#define EC_TIMEOUTSTATE
Definition: ethercattype.h:76
ec_slavecount
int ec_slavecount
Definition: ethercatmain.c:69
LO_WORD
#define LO_WORD(l)
Definition: ethercattype.h:518
ow
uint16 ow
Definition: linux/firm_update/firm_update.c:26
boottest
void boottest(char *ifname, uint16 slave, char *filename)
Definition: linux/firm_update/firm_update.c:51
FWBUFSIZE
#define FWBUFSIZE
Definition: linux/firm_update/firm_update.c:23
ec_statecheck
uint16 ec_statecheck(uint16 slave, uint16 reqstate, int timeout)
Definition: ethercatmain.c:2162
ECT_SII_BOOTRXMBX
@ ECT_SII_BOOTRXMBX
Definition: ethercattype.h:304
filename
char filename[256]
Definition: linux/firm_update/firm_update.c:28
ec_slave::mbx_l
uint16 mbx_l
Definition: ethercatmain.h:157
ec_slave::mbx_rl
uint16 mbx_rl
Definition: ethercatmain.h:161
ec_slave::SM
ec_smt SM[EC_MAXSM]
Definition: ethercatmain.h:143


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Wed Mar 2 2022 01:01:49