9a12add659a00af10bc62b48b5042b62f834f73e
[fw/altos] / src / product / ao_flash_task.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include <ao_exti.h>
20 #include <ao_boot.h>
21 #include <ao_flash.h>
22 #include <ao_flash_task.h>
23
24 void
25 ao_panic(uint8_t reason)
26 {
27         (void) reason;
28 }
29
30 void
31 ao_put_string(__code char *s)
32 {
33         char    c;
34         while ((c = *s++)) {
35                 if (c == '\n')
36                         ao_usb_putchar('\r');
37                 ao_usb_putchar(c);
38         }
39 }
40
41 static void
42 ao_application(void)
43 {
44         ao_boot_reboot(AO_BOOT_APPLICATION_BASE);
45 }
46
47 static uint32_t
48 ao_get_hex32(void)
49 {
50         int8_t  n;
51         uint32_t v = 0;
52
53         for (;;) {
54                 n = ao_usb_getchar();
55                 if (n != ' ')
56                         break;
57         }
58         for(;;) {
59                 if ('0' <= n && n <= '9')
60                         n = n - '0';
61                 else if ('a' <= n && n <= 'f')
62                         n = n - ('a' - 10);
63                 else if ('A' <= n && n <= 'F')
64                         n = n - ('A' - 10);
65                 else
66                         break;
67                 v = (v << 4) | n;
68                 n = ao_usb_getchar();
69         }
70         return v;
71 }
72
73 static void
74 ao_block_erase(void)
75 {
76         uint32_t        addr = ao_get_hex32();
77         void            *p = (void *) addr;
78
79         ao_flash_erase_page(p);
80 }
81
82 static int
83 ao_block_valid_address(uint32_t addr)
84 {
85         if ((uint32_t) AO_BOOT_APPLICATION_BASE <= addr && addr <= (uint32_t) AO_BOOT_APPLICATION_BOUND - 256)
86                 return 1;
87         return 0;
88 }
89
90 static void
91 ao_block_write(void)
92 {
93         uint32_t        addr = ao_get_hex32();
94         void            *p = (void *) addr;
95         uint8_t         data[256];
96         uint16_t        i;
97
98         for (i = 0; i < 256; i++)
99                 data[i] = ao_usb_getchar();
100         if (!ao_block_valid_address(addr))
101                 return;
102         ao_flash_page(p, (void *) data);
103 }
104
105 static void
106 ao_block_read(void)
107 {
108         uint32_t        addr = ao_get_hex32();
109         uint8_t         *p = (uint8_t *) addr;
110         uint16_t        i;
111         uint8_t         c;
112
113         if (!ao_block_valid_address(addr)) {
114                 for (i = 0; i < 256; i++)
115                         ao_usb_putchar(0xff);
116                 return;
117         }
118         for (i = 0; i < 256; i++) {
119                 c = *p++;
120                 ao_usb_putchar(c);
121         }
122 }
123
124 static inline void
125 hexchar(uint8_t c)
126 {
127         if (c > 10)
128                 c += 'a' - ('9' + 1);
129         ao_usb_putchar(c + '0');
130 }
131
132 static void
133 ao_put_hex(uint32_t u)
134 {
135         int8_t i;
136         for (i = 28; i >= 0; i -= 4)
137                 hexchar((u >> i) & 0xf);
138 }
139
140 static void
141 ao_show_version(void)
142 {
143         ao_put_string("altos-loader");
144         ao_put_string("\nmanufacturer     "); ao_put_string(ao_manufacturer);
145         ao_put_string("\nproduct          "); ao_put_string(ao_product);
146         ao_put_string("\nflash-range      ");
147         ao_put_hex((uint32_t) AO_BOOT_APPLICATION_BASE);
148         ao_usb_putchar(' ');
149         ao_put_hex((uint32_t) AO_BOOT_APPLICATION_BOUND);
150         ao_put_string("\nsoftware-version "); ao_put_string(ao_version);
151         ao_put_string("\n");
152 }
153
154 void
155 ao_flash_task(void) {
156         for (;;) {
157                 ao_usb_flush();
158                 switch (ao_usb_getchar()) {
159                 case 'v': ao_show_version(); break;
160                 case 'a': ao_application(); break;
161                 case 'X': ao_block_erase(); break;
162                 case 'W': ao_block_write(); break;
163                 case 'R': ao_block_read(); break;
164                 }
165         }
166 }