f0abe7fb8bd6aab35ddfabcd21743d11e9fe5d0f
[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                 putchar(c);
35 }
36
37 void
38 ao_application(void)
39 {
40         ao_boot_reboot(AO_BOOT_APPLICATION_BASE);
41 }
42
43 static uint32_t
44 ao_get_hex32(void)
45 {
46         int8_t  n;
47         uint32_t v = 0;
48
49         for (;;) {
50                 n = getchar();
51                 if (n != ' ')
52                         break;
53         }
54         for(;;) {
55                 if ('0' <= n && n <= '9')
56                         n = n - '0';
57                 else if ('a' <= n && n <= 'f')
58                         n = n - ('a' - 10);
59                 else if ('A' <= n && n <= 'F')
60                         n = n - ('A' - 10);
61                 else
62                         break;
63                 v = (v << 4) | n;
64                 n = getchar();
65         }
66         return v;
67 }
68
69 void
70 ao_block_erase(void)
71 {
72         uint32_t        addr = ao_get_hex32();
73         uint32_t        *p = (uint32_t *) addr;
74
75         ao_flash_erase_page(p);
76 }
77
78 void
79 ao_block_write(void)
80 {
81         uint32_t        addr = ao_get_hex32();
82         uint32_t        *p = (uint32_t *) addr;
83         union {
84                 uint8_t         data8[256];
85                 uint32_t        data32[64];
86         } u;
87         uint16_t        i;
88
89         if (addr < 0x08002000 || 0x08200000 <= addr) {
90                 ao_put_string("Invalid address\n");
91                 return;
92         }
93         for (i = 0; i < 256; i++)
94                 u.data8[i] = getchar();
95         ao_flash_page(p, u.data32);
96 }
97
98 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                 putchar(c);
109         }
110 }
111
112 static void
113 ao_show_version(void)
114 {
115         puts("altos-loader");
116         ao_put_string("manufacturer     "); puts(ao_manufacturer);
117         ao_put_string("product          "); puts(ao_product);
118         ao_put_string("software-version "); puts(ao_version);
119 }
120
121 static void
122 ao_flash_task(void) {
123         for (;;) {
124                 switch (getchar()) {
125                 case 'v': ao_show_version(); break;
126                 case 'a': ao_application(); break;
127                 case 'X': ao_block_erase(); break;
128                 case 'W': ao_block_write(); break;
129                 case 'R': ao_block_read(); break;
130                 }
131         }
132 }
133
134
135 int
136 main(void)
137 {
138         ao_clock_init();
139
140 //      ao_timer_init();
141 //      ao_dma_init();
142 //      ao_exti_init();
143         ao_usb_init();
144
145         ao_flash_task();
146         return 0;
147 }