34 #include <sys/types.h>
45 char* term =
"/dev/ttyUSB0";
51 int second_delay = 100;
57 struct termios options;
65 int main(
int argc,
char **argv)
74 while ((c = getopt(argc, argv,
"f:s:zt:vu:r:c:a:b:eh")) != -1) {
93 if (strcmp(optarg,
"115200")) {
95 }
else if (strcmp(optarg,
"57600")) {
97 }
else if (strcmp(optarg,
"19200")) {
99 }
else if (strcmp(optarg,
"9600")) {
102 printf(
"Unknown baud rate %s!\n", optarg);
113 first_delay = atoi(optarg);
116 second_delay = atoi(optarg);
135 printf(
"Primary file (RAM): %s\n", filename);
136 printf(
"Secondary file (Flash): %s\n", second);
137 printf(
"Zero secondary file: %s\n", zerolen == 1 ?
"Yes" :
"No");
138 printf(
"Port: %s\n", term);
139 printf(
"Baud rate: %i\n", baud);
140 printf(
"Flow control: %s\n", rts);
141 printf(
"Reset command: %s\n", command);
142 printf(
"Exit after load: %s\n", do_exit == 1 ?
"Yes" :
"No");
143 printf(
"Delay 1: %i\n", first_delay);
144 printf(
"Delay 2: %i\n", second_delay);
148 pfd = open(term, O_RDWR | O_NOCTTY | O_NDELAY);
150 printf(
"Cannot open serial port %s!\n", term);
153 fcntl(pfd, F_SETFL, FNDELAY);
154 tcgetattr(pfd, &options);
155 cfsetispeed(&options, baud);
156 options.c_cflag |= (CLOCAL | CREAD);
157 options.c_cflag &= ~PARENB;
158 options.c_cflag &= ~CSTOPB;
159 options.c_cflag &= ~CSIZE;
160 options.c_cflag |= CS8;
161 if (strcmp(rts,
"rts")) {
162 options.c_cflag &= ~CRTSCTS;
164 options.c_cflag |= CRTSCTS;
166 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
167 options.c_oflag &= ~OPOST;
168 tcsetattr(pfd, TCSANOW, &options);
171 printf(
"Reset the board to enter bootloader (waiting for CONNECT)...\n");
173 printf(
"Performing reset: %s\n", command);
181 r = write(pfd, (
const void*)
"\0", 1);
183 r = read(pfd, &buf[i],
sizeof(buf)-1-i);
186 printf(
"%s", &buf[i]); fflush(stdout);
187 if (strstr(&buf[i],
"CONNECT")) {
192 if (i >=
sizeof(buf)-1) {
196 printf(
"."); fflush(stdout);
202 printf(
"Please specify firmware file name (-f option)!\n");
205 if (stat(filename, &sbuf)) {
206 printf(
"Cannot open firmware file %s!\n", filename);
209 ffd = open(filename, O_RDONLY);
211 printf(
"Cannot open firmware file %s!\n", filename);
215 printf(
"Sending %s (%i bytes)...\n", filename, s);
216 r = write(pfd, (
const void*)&s, 4);
218 r = read(ffd, buf, 1);
222 c = write(pfd, (
const void*)buf, r);
225 printf(
"Written %i\r", i); fflush(stdout);
226 r = read(ffd, buf, 1);
231 if (second || zerolen) {
233 printf(
"Sending secondary file (waiting for ready)...\n");
237 r = read(pfd, &buf[i],
sizeof(buf)-1-i);
240 printf(
"%s", &buf[i]); fflush(stdout);
241 if (strstr(buf,
"ready")) {
246 if (i >=
sizeof(buf)-1) {
250 printf(
"."); fflush(stdout);
256 if (stat(second, &sbuf)) {
257 printf(
"Cannot open secondary file %s!\n", second);
260 sfd = open(second, O_RDONLY);
262 printf(
"Cannot open secondary file %s!\n", second);
266 printf(
"Sending %s (%i bytes)...\n", second, s);
267 r = write(pfd, (
const void*)&s, 4);
269 r = read(sfd, buf, 1);
272 usleep(second_delay);
273 c = write(pfd, (
const void*)buf, r);
276 printf(
"Written %i\r", i); fflush(stdout);
277 r = read(sfd, buf, 1);
280 }
else if (zerolen) {
282 printf(
"Sending %i...\n", s);
283 write(pfd, (
const void*)&s, 4);
289 printf(
"Sending %s\n", args);
290 r = write(pfd, (
const void*)args, strlen(args));
291 r = write(pfd, (
const void*)
",", 1);
297 r = read(pfd, buf,
sizeof(buf));
300 printf(
"%s", buf); fflush(stdout);
309 printf(
"Example usage: mc1322x-load -f foo.bin -t /dev/ttyS0 -b 9600\n");
310 printf(
" or : mc1322x-load -f flasher.bin -s flashme.bin 0x1e000,0x11223344,0x55667788\n");
311 printf(
" or : mc1322x-load -f flasher.bin -z 0x1e000,0x11223344,0x55667788\n");
312 printf(
" -f required: binary file to load\n");
313 printf(
" -s optional: secondary binary file to send\n");
314 printf(
" -z optional: send a zero length file as secondary\n");
315 printf(
" -t, terminal default: /dev/ttyUSB0\n");
316 printf(
" -u, baud rate default: 115200\n");
317 printf(
" -r [none|rts] flow control default: rts\n");
318 printf(
" -c command to run for autoreset: \n");
319 printf(
" e.g. -c 'bbmc -l redbee-econotag -i 0 reset'\n");
320 printf(
" -e exit instead of dropping to terminal display\n");
321 printf(
" -a first intercharacter delay, passed to usleep\n");
322 printf(
" -b second intercharacter delay, passed to usleep\n");
324 printf(
"Anything on the command line is sent after all of the files.\n\n");
#define NULL
The null pointer.
int main(void)
This is main...