comparison target-utils/loadagent/binflash.c @ 660:b34384991094

loadagent: implemented binary flash programming
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 04 Mar 2020 06:51:52 +0000
parents
children
comparison
equal deleted inserted replaced
659:761e8b0c65b0 660:b34384991094
1 /*
2 * Here we are going to implement our new binary protocol
3 * for flash programming.
4 */
5
6 #include "types.h"
7
8 void
9 binary_flash_prog_main(program_func)
10 int (*program_func)();
11 {
12 u8 buf[2048] __attribute__ ((aligned (2)));
13 u32 flash_offset;
14 unsigned nbytes, p;
15 int c;
16
17 for (;;) {
18 do
19 c = serial_in_poll();
20 while (c < 0);
21 if (c == 0x04)
22 return;
23 if (c != 0x01) {
24 serial_out(0x15); /* NAK */
25 printf("ERROR: invalid command opcode\n");
26 return;
27 }
28 /* receive header */
29 for (p = 0; p < 6; p++) {
30 c = serial_in_timeout(1000000); /* 0.6 s */
31 if (c < 0) {
32 intermediate_timeout: serial_out(0x15); /* NAK */
33 printf("ERROR: timeout receiving command\n");
34 return;
35 }
36 buf[p] = c;
37 }
38 flash_offset = ((u32) buf[0] << 24) |
39 ((u32) buf[1] << 16) |
40 ((u32) buf[2] << 8) |
41 (u32) buf[3];
42 if (flash_offset & 1) {
43 serial_out(0x15); /* NAK */
44 printf("ERROR: odd flash offset\n");
45 return;
46 }
47 nbytes = ((u32) buf[4] << 8) | (u32) buf[5];
48 if (nbytes & 1) {
49 serial_out(0x15); /* NAK */
50 printf("ERROR: odd byte count\n");
51 return;
52 }
53 if (nbytes > sizeof buf) {
54 serial_out(0x15); /* NAK */
55 printf("ERROR: byte count exceeds buffer\n");
56 return;
57 }
58 /* receive data */
59 for (p = 0; p < nbytes; p++) {
60 c = serial_in_timeout(1000000); /* 0.6 s */
61 if (c < 0)
62 goto intermediate_timeout;
63 buf[p] = c;
64 }
65 c = program_func(flash_offset, nbytes >> 1, buf);
66 if (c < 0)
67 return;
68 serial_out(0x06); /* ACK */
69 }
70 }