altos: Remove stdio from stm-flash
[fw/altos] / src / stm-flash / ao_stm_flash.c
1 /*
2  * Copyright © 2011 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_stm.h>
22
23 void
24 ao_panic(uint8_t reason)
25 {
26         for (;;);
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 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 void
73 ao_block_erase(void)
74 {
75         uint32_t        addr = ao_get_hex32();
76         uint32_t        *p = (uint32_t *) addr;
77
78         ao_flash_erase_page(p);
79 }
80
81 void
82 ao_block_write(void)
83 {
84         uint32_t        addr = ao_get_hex32();
85         uint32_t        *p = (uint32_t *) addr;
86         union {
87                 uint8_t         data8[256];
88                 uint32_t        data32[64];
89         } u;
90         uint16_t        i;
91
92         if (addr < (uint32_t) AO_BOOT_APPLICATION_BASE) {
93                 ao_put_string("Invalid address\n");
94                 return;
95         }
96         for (i = 0; i < 256; i++)
97                 u.data8[i] = ao_usb_getchar();
98         ao_flash_page(p, u.data32);
99 }
100
101 void
102 ao_block_read(void)
103 {
104         uint32_t        addr = ao_get_hex32();
105         uint8_t         *p = (uint8_t *) addr;
106         uint16_t        i;
107         uint8_t         c;
108
109         for (i = 0; i < 256; i++) {
110                 c = *p++;
111                 ao_usb_putchar(c);
112         }
113 }
114
115 static void
116 ao_show_version(void)
117 {
118         ao_put_string("altos-loader");
119         ao_put_string("\nmanufacturer     "); ao_put_string(ao_manufacturer);
120         ao_put_string("\nproduct          "); ao_put_string(ao_product);
121         ao_put_string("\nsoftware-version "); ao_put_string(ao_version);
122         ao_put_string("\n");
123 }
124
125 static void
126 ao_flash_task(void) {
127         for (;;) {
128                 ao_usb_flush();
129                 switch (ao_usb_getchar()) {
130                 case 'v': ao_show_version(); break;
131                 case 'a': ao_application(); break;
132                 case 'X': ao_block_erase(); break;
133                 case 'W': ao_block_write(); break;
134                 case 'R': ao_block_read(); break;
135                 }
136         }
137 }
138
139
140 int
141 main(void)
142 {
143         ao_clock_init();
144
145 //      ao_timer_init();
146 //      ao_dma_init();
147 //      ao_exti_init();
148         ao_usb_init();
149
150         ao_flash_task();
151         return 0;
152 }