altos: Debugging TBT issues -- check pin configuration after boot
[fw/altos] / src / ao_cmd.c
1 /*
2  * Copyright © 2009 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
20 __xdata uint16_t ao_cmd_lex_i;
21 __xdata uint32_t ao_cmd_lex_u32;
22 __xdata char    ao_cmd_lex_c;
23 __xdata enum ao_cmd_status ao_cmd_status;
24
25 #define CMD_LEN 32
26
27 static __xdata char     cmd_line[CMD_LEN];
28 static __xdata uint8_t  cmd_len;
29 static __xdata uint8_t  cmd_i;
30
31 static void
32 put_string(char *s)
33 {
34         __xdata char    c;
35         while (c = *s++)
36                 putchar(c);
37 }
38
39 static void
40 readline(void)
41 {
42         __xdata char c;
43         if (ao_echo())
44                 put_string("> ");
45         cmd_len = 0;
46         for (;;) {
47                 flush();
48                 c = getchar();
49                 /* backspace/delete */
50                 if (c == '\010' || c == '\177') {
51                         if (cmd_len != 0) {
52                                 if (ao_echo())
53                                         put_string("\010 \010");
54                                 --cmd_len;
55                         }
56                         continue;
57                 }
58
59                 /* ^U */
60                 if (c == '\025') {
61                         while (cmd_len != 0) {
62                                 if (ao_echo())
63                                         put_string("\010 \010");
64                                 --cmd_len;
65                         }
66                         continue;
67                 }
68
69                 /* map CR to NL */
70                 if (c == '\r')
71                         c = '\n';
72
73                 if (c == '\n') {
74                         if (ao_echo())
75                                 putchar('\n');
76                         break;
77                 }
78
79                 if (cmd_len >= CMD_LEN - 2) {
80                         if (ao_echo())
81                                 putchar('\007');
82                         continue;
83                 }
84                 cmd_line[cmd_len++] = c;
85                 if (ao_echo())
86                         putchar(c);
87         }
88         cmd_line[cmd_len++] = '\n';
89         cmd_line[cmd_len++] = '\0';
90         cmd_i = 0;
91 }
92
93 void
94 ao_cmd_lex(void)
95 {
96         ao_cmd_lex_c = '\n';
97         if (cmd_i < cmd_len)
98                 ao_cmd_lex_c = cmd_line[cmd_i++];
99 }
100
101 static void
102 putnibble(uint8_t v)
103 {
104         if (v < 10)
105                 putchar(v + '0');
106         else
107                 putchar(v + ('a' - 10));
108 }
109
110 void
111 ao_cmd_put16(uint16_t v)
112 {
113         int8_t i;
114         for (i = 3; i >= 0; i--)
115                 putnibble((v >> (i << 2)) & 0xf);
116 }
117
118 void
119 ao_cmd_put8(uint8_t v)
120 {
121         putnibble((v >> 4) & 0xf);
122         putnibble(v & 0xf);
123 }
124
125 void
126 ao_cmd_white(void)
127 {
128         while (ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t')
129                 ao_cmd_lex();
130 }
131
132 void
133 ao_cmd_hex(void)
134 {
135         __xdata uint8_t r = ao_cmd_lex_error;
136
137         ao_cmd_lex_i = 0;
138         ao_cmd_white();
139         for(;;) {
140                 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
141                         ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - '0');
142                 else if ('a' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'f')
143                         ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - 'a' + 10);
144                 else if ('A' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'F')
145                         ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - 'A' + 10);
146                 else
147                         break;
148                 r = ao_cmd_success;
149                 ao_cmd_lex();
150         }
151         if (r != ao_cmd_success)
152                 ao_cmd_status = r;
153 }
154
155 void
156 ao_cmd_decimal(void)
157 {
158         __xdata uint8_t r = ao_cmd_lex_error;
159
160         ao_cmd_lex_u32 = 0;
161         ao_cmd_white();
162         for(;;) {
163                 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
164                         ao_cmd_lex_u32 = (ao_cmd_lex_u32 * 10) + (ao_cmd_lex_c - '0');
165                 else
166                         break;
167                 r = ao_cmd_success;
168                 ao_cmd_lex();
169         }
170         if (r != ao_cmd_success)
171                 ao_cmd_status = r;
172         ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
173 }
174
175 uint8_t
176 ao_match_word(__code char *word)
177 {
178         while (*word) {
179                 if (ao_cmd_lex_c != *word) {
180                         ao_cmd_status = ao_cmd_syntax_error;
181                         return 0;
182                 }
183                 word++;
184                 ao_cmd_lex();
185         }
186         return 1;
187 }
188
189 static void
190 eol(void)
191 {
192         while (ao_cmd_lex_c != '\n')
193                 ao_cmd_lex();
194 }
195
196 static void
197 echo(void)
198 {
199         ao_cmd_hex();
200         if (ao_cmd_status == ao_cmd_success)
201                 ao_stdios[ao_cur_stdio].echo = ao_cmd_lex_i != 0;
202 }
203
204 static void
205 ao_reboot(void)
206 {
207         ao_cmd_white();
208         if (!ao_match_word("eboot"))
209                 return;
210         WDCTL = WDCTL_EN | WDCTL_MODE_WATCHDOG | WDCTL_INT_64;
211         ao_delay(AO_SEC_TO_TICKS(2));
212         ao_panic(AO_PANIC_REBOOT);
213 }
214
215 static void
216 version(void)
217 {
218         printf("manufacturer     %s\n", ao_manufacturer);
219         printf("product          %s\n", ao_product);
220         printf("serial-number    %u\n", ao_serial_number);
221         printf("software-version %s\n", ao_version);
222 }
223
224 static const char help_txt[] = "All numbers are in hex";
225
226 #define NUM_CMDS        11
227
228 static __code struct ao_cmds    *__xdata (ao_cmds[NUM_CMDS]);
229 static __xdata uint8_t          ao_ncmds;
230
231 static void
232 help(void)
233 {
234         __xdata uint8_t cmds;
235         __xdata uint8_t cmd;
236         __code struct ao_cmds * __xdata cs;
237         puts(help_txt);
238         for (cmds = 0; cmds < ao_ncmds; cmds++) {
239                 cs = ao_cmds[cmds];
240                 for (cmd = 0; cs[cmd].func; cmd++)
241                         printf("%-45s %s\n",
242                                 cs[cmd].help,
243                                 cs[cmd].help+1+strlen(cs[cmd].help));
244         }
245 }
246
247 static void
248 report(void)
249 {
250         switch(ao_cmd_status) {
251         case ao_cmd_lex_error:
252         case ao_cmd_syntax_error:
253                 puts("Syntax error");
254                 ao_cmd_status = 0;
255                 break;
256         }
257 }
258
259 void
260 ao_cmd_register(__code struct ao_cmds *cmds)
261 {
262         if (ao_ncmds >= NUM_CMDS)
263                 ao_panic(AO_PANIC_CMD);
264         ao_cmds[ao_ncmds++] = cmds;
265 }
266
267 void
268 ao_cmd(void)
269 {
270         __xdata char    c;
271         __xdata uint8_t cmd, cmds;
272         __code struct ao_cmds * __xdata cs;
273         void (*__xdata func)(void);
274
275         for (;;) {
276                 readline();
277                 ao_cmd_lex();
278                 ao_cmd_white();
279                 c = ao_cmd_lex_c;
280                 ao_cmd_lex();
281                 if (c == '\r' || c == '\n')
282                         continue;
283                 func = (void (*)(void)) NULL;
284                 for (cmds = 0; cmds < ao_ncmds; cmds++) {
285                         cs = ao_cmds[cmds];
286                         for (cmd = 0; cs[cmd].func; cmd++)
287                                 if (cs[cmd].help[0] == c) {
288                                         func = cs[cmd].func;
289                                         break;
290                                 }
291                         if (func)
292                                 break;
293                 }
294                 if (func)
295                         (*func)();
296                 else
297                         ao_cmd_status = ao_cmd_syntax_error;
298                 report();
299         }
300 }
301
302 __xdata struct ao_task ao_cmd_task;
303
304 __code struct ao_cmds   ao_base_cmds[] = {
305         { help,         "?\0Print this message" },
306         { ao_task_info, "T\0Show task states" },
307         { echo,         "E <0 off, 1 on>\0Set command echo mode" },
308         { ao_reboot,    "r eboot\0Reboot" },
309         { version,      "v\0Show version" },
310         { 0,    NULL },
311 };
312
313 void
314 ao_cmd_init(void)
315 {
316         ao_cmd_register(&ao_base_cmds[0]);
317         ao_add_task(&ao_cmd_task, ao_cmd, "cmd");
318 }