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