ao_flash_task: Add casts to reduce -Wconversion warnings
[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 void
33 ao_put_string(const char *s)
34 {
35         char    c;
36         while ((c = *s++)) {
37                 if (c == '\n')
38                         ao_usb_putchar('\r');
39                 ao_usb_putchar(c);
40         }
41 }
42
43 static void
44 ao_application(void)
45 {
46         ao_boot_reboot(AO_BOOT_APPLICATION_BASE);
47 }
48
49 static uint32_t
50 ao_get_hex32(void)
51 {
52         int     n;
53         uint32_t v = 0;
54
55         for (;;) {
56                 n = ao_usb_getchar();
57                 if (n != ' ')
58                         break;
59         }
60         for(;;) {
61                 if ('0' <= n && n <= '9')
62                         n = n - '0';
63                 else if ('a' <= n && n <= 'f')
64                         n = n - ('a' - 10);
65                 else if ('A' <= n && n <= 'F')
66                         n = n - ('A' - 10);
67                 else
68                         break;
69                 v = (v << 4) | (uint8_t) n;
70                 n = ao_usb_getchar();
71         }
72         return v;
73 }
74
75 static void
76 ao_block_erase(void)
77 {
78         uint32_t        addr = ao_get_hex32();
79         void            *p = (void *) addr;
80
81         ao_flash_erase_page(p);
82 }
83
84 static int
85 ao_block_valid_address(uint32_t addr)
86 {
87         if ((uint32_t) AO_BOOT_APPLICATION_BASE <= addr && addr <= (uint32_t) AO_BOOT_APPLICATION_BOUND - 256)
88                 return 1;
89         return 0;
90 }
91
92 static void
93 ao_block_write(void)
94 {
95         uint32_t        addr = ao_get_hex32();
96         void            *p = (void *) addr;
97         uint8_t         data[256];
98         uint16_t        i;
99
100         for (i = 0; i < 256; i++)
101                 data[i] = ao_usb_getchar();
102         if (!ao_block_valid_address(addr))
103                 return;
104         ao_flash_page(p, (void *) data);
105 }
106
107 static void
108 ao_block_read(void)
109 {
110         uint32_t        addr = ao_get_hex32();
111         uint8_t         *p = (uint8_t *) addr;
112         uint16_t        i;
113         uint8_t         c;
114
115         if (!ao_block_valid_address(addr)) {
116                 for (i = 0; i < 256; i++)
117                         ao_usb_putchar(0xff);
118                 return;
119         }
120         for (i = 0; i < 256; i++) {
121                 c = *p++;
122                 ao_usb_putchar(c);
123         }
124 }
125
126 static inline void
127 hexchar(uint8_t c)
128 {
129         if (c > 10)
130                 c += 'a' - ('9' + 1);
131         ao_usb_putchar(c + '0');
132 }
133
134 static void
135 ao_put_hex(uint32_t u)
136 {
137         int8_t i;
138         for (i = 28; i >= 0; i -= 4)
139                 hexchar((u >> i) & 0xf);
140 }
141
142 static void
143 ao_show_version(void)
144 {
145         ao_put_string("altos-loader");
146         ao_put_string("\nmanufacturer     "); ao_put_string(ao_manufacturer);
147         ao_put_string("\nproduct          "); ao_put_string(ao_product);
148         ao_put_string("\nflash-range      ");
149         ao_put_hex((uint32_t) AO_BOOT_APPLICATION_BASE);
150         ao_usb_putchar(' ');
151         ao_put_hex((uint32_t) AO_BOOT_APPLICATION_BOUND);
152         ao_put_string("\nsoftware-version "); ao_put_string(ao_version);
153         ao_put_string("\n");
154 }
155
156 void
157 ao_flash_task(void) {
158         for (;;) {
159                 ao_usb_flush();
160                 switch (ao_usb_getchar()) {
161                 case 'v': ao_show_version(); break;
162                 case 'a': ao_application(); break;
163                 case 'X': ao_block_erase(); break;
164                 case 'W': ao_block_write(); break;
165                 case 'R': ao_block_read(); break;
166                 }
167         }
168 }