81ae86df2523acdd9b9bd7c44ea311b041d8d506
[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 <<<<<<< HEAD
21 #include <ao_boot.h>
22 #include <ao_flash_stm.h>
23
24 void
25 ao_panic(uint8_t reason)
26 {
27         for (;;);
28 }
29
30 void
31 ao_application(void)
32 {
33         ao_boot_reboot(AO_BOOT_APPLICATION_BASE);
34 }
35
36 static uint32_t
37 ao_cmd_hex32(void)
38 {
39         __pdata uint8_t r = ao_cmd_lex_error;
40         int8_t  n;
41         uint32_t v = 0;
42
43         ao_cmd_white();
44         for(;;) {
45                 n = ao_cmd_hexchar(ao_cmd_lex_c);
46                 if (n < 0)
47                         break;
48                 v = (v << 4) | n;
49                 r = ao_cmd_success;
50                 ao_cmd_lex();
51         }
52         if (r != ao_cmd_success)
53                 ao_cmd_status = r;
54         return v;
55 }
56
57 void
58 ao_block_erase(void)
59 {
60         uint32_t        addr = ao_cmd_hex32();
61         uint32_t        *p = (uint32_t *) addr;
62
63         ao_flash_erase_page(p);
64 }
65
66 void
67 ao_block_write(void)
68 {
69         uint32_t        addr = ao_cmd_hex32();
70         uint32_t        *p = (uint32_t *) addr;
71         union {
72                 uint8_t         data8[256];
73                 uint32_t        data32[64];
74         } u;
75         uint16_t        i;
76
77         if (addr < 0x08002000 || 0x08200000 <= addr) {
78                 puts("Invalid address");
79                 return;
80         }
81         for (i = 0; i < 256; i++)
82                 u.data8[i] = i;
83         ao_flash_page(p, u.data32);
84 }
85
86 static void
87 puthex(uint8_t c)
88 {
89         c &= 0xf;
90         if (c < 10)
91                 c += '0';
92         else
93                 c += 'a' - 10;
94         putchar (c);
95 }
96
97 void
98 ao_block_read(void)
99 {
100         uint32_t        addr = ao_cmd_hex32();
101         uint8_t         *p = (uint8_t *) addr;
102         uint16_t        i;
103         uint8_t         c;
104
105         for (i = 0; i < 256; i++) {
106                 c = *p++;
107                 puthex(c);
108                 puthex(c>>4);
109                 if ((i & 0xf) == 0xf)
110                         putchar('\n');
111         }
112 }
113
114 __code struct ao_cmds ao_flash_cmds[] = {
115         { ao_application, "a\0Switch to application" },
116         { ao_block_erase, "e <addr>\0Erase block." },
117         { ao_block_write, "W <addr>\0Write block. 256 binary bytes follow newline" },
118         { ao_block_read, "R <addr>\0Read block. Returns 256 bytes" },
119         { 0, NULL },
120 };
121 =======
122
123 void
124 ao_panic(uint8_t c)
125 {
126 }
127
128 void
129 ao_test(void)
130 {
131         char    c;
132
133         for (;;) {
134                 c = ao_usb_getchar();
135                 ao_usb_putchar(c);
136                 ao_usb_flush();
137         }
138 }
139
140 struct ao_task ao_test_task;
141 >>>>>>> 5187bb4... Add STM self-flashing loader
142
143 int
144 main(void)
145 {
146         ao_clock_init();
147 <<<<<<< HEAD
148
149         ao_task_init();
150
151 =======
152 >>>>>>> 5187bb4... Add STM self-flashing loader
153         ao_timer_init();
154 //      ao_dma_init();
155         ao_cmd_init();
156 //      ao_exti_init();
157         ao_usb_init();
158
159 <<<<<<< HEAD
160         ao_cmd_register(&ao_flash_cmds[0]);
161 =======
162 >>>>>>> 5187bb4... Add STM self-flashing loader
163         ao_start_scheduler();
164         return 0;
165 }