annotate target-utils/loadagent/binflash.c @ 746:f19c347d0a80

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