altos: Stop copying cc1111 binaries to the altos/src dir
[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 }
28
29 void
30 ao_put_string(__code char *s)
31 {
32         char    c;
33         while ((c = *s++)) {
34                 if (c == '\n')
35                         ao_usb_putchar('\r');
36                 ao_usb_putchar(c);
37         }
38 }
39
40 static void
41 ao_application(void)
42 {
43         ao_boot_reboot(AO_BOOT_APPLICATION_BASE);
44 }
45
46 static uint32_t
47 ao_get_hex32(void)
48 {
49         int8_t  n;
50         uint32_t v = 0;
51
52         for (;;) {
53                 n = ao_usb_getchar();
54                 if (n != ' ')
55                         break;
56         }
57         for(;;) {
58                 if ('0' <= n && n <= '9')
59                         n = n - '0';
60                 else if ('a' <= n && n <= 'f')
61                         n = n - ('a' - 10);
62                 else if ('A' <= n && n <= 'F')
63                         n = n - ('A' - 10);
64                 else
65                         break;
66                 v = (v << 4) | n;
67                 n = ao_usb_getchar();
68         }
69         return v;
70 }
71
72 static void
73 ao_block_erase(void)
74 {
75         uint32_t        addr = ao_get_hex32();
76         void            *p = (void *) addr;
77
78         ao_flash_erase_page(p);
79 }
80
81 static void
82 ao_block_write(void)
83 {
84         uint32_t        addr = ao_get_hex32();
85         void            *p = (void *) addr;
86         uint8_t         data[256];
87         uint16_t        i;
88
89         if (addr < (uint32_t) AO_BOOT_APPLICATION_BASE) {
90                 ao_put_string("Invalid address\n");
91                 return;
92         }
93         for (i = 0; i < 256; i++)
94                 data[i] = ao_usb_getchar();
95         ao_flash_page(p, (void *) data);
96 }
97
98 static void
99 ao_block_read(void)
100 {
101         uint32_t        addr = ao_get_hex32();
102         uint8_t         *p = (uint8_t *) addr;
103         uint16_t        i;
104         uint8_t         c;
105
106         for (i = 0; i < 256; i++) {
107                 c = *p++;
108                 ao_usb_putchar(c);
109         }
110 }
111
112 static void
113 ao_show_version(void)
114 {
115         ao_put_string("altos-loader");
116         ao_put_string("\nmanufacturer     "); ao_put_string(ao_manufacturer);
117         ao_put_string("\nproduct          "); ao_put_string(ao_product);
118         ao_put_string("\nsoftware-version "); ao_put_string(ao_version);
119         ao_put_string("\n");
120 }
121
122 void
123 ao_flash_task(void) {
124         for (;;) {
125                 ao_usb_flush();
126                 switch (ao_usb_getchar()) {
127                 case 'v': ao_show_version(); break;
128                 case 'a': ao_application(); break;
129                 case 'X': ao_block_erase(); break;
130                 case 'W': ao_block_write(); break;
131                 case 'R': ao_block_read(); break;
132                 }
133         }
134 }