2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
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;
27 static __xdata char cmd_line[CMD_LEN];
28 static __xdata uint8_t cmd_len;
29 static __xdata uint8_t cmd_i;
49 /* backspace/delete */
50 if (c == '\010' || c == '\177') {
53 put_string("\010 \010");
61 while (cmd_len != 0) {
63 put_string("\010 \010");
79 if (cmd_len >= CMD_LEN - 2) {
84 cmd_line[cmd_len++] = c;
88 cmd_line[cmd_len++] = '\n';
89 cmd_line[cmd_len++] = '\0';
98 ao_cmd_lex_c = cmd_line[cmd_i++];
107 putchar(v + ('a' - 10));
111 ao_cmd_put16(uint16_t v)
114 for (i = 3; i >= 0; i--)
115 putnibble((v >> (i << 2)) & 0xf);
119 ao_cmd_put8(uint8_t v)
121 putnibble((v >> 4) & 0xf);
128 while (ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t')
135 __xdata uint8_t r = ao_cmd_lex_error;
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);
151 if (r != ao_cmd_success)
158 __xdata uint8_t r = ao_cmd_lex_error;
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');
170 if (r != ao_cmd_success)
172 ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
176 ao_match_word(__code char *word)
179 if (ao_cmd_lex_c != *word) {
180 ao_cmd_status = ao_cmd_syntax_error;
193 while (ao_cmd_lex_c != '\n')
202 if (ao_cmd_status == ao_cmd_success)
203 ao_stdios[ao_cur_stdio].echo = ao_cmd_lex_i != 0;
210 if (!ao_match_word("eboot"))
214 WDCTL = WDCTL_EN | WDCTL_MODE_WATCHDOG | WDCTL_INT_64;
216 ao_delay(AO_SEC_TO_TICKS(2));
217 ao_panic(AO_PANIC_REBOOT);
223 printf("manufacturer %s\n", ao_manufacturer);
224 printf("product %s\n", ao_product);
225 // printf("serial-number %u\n", ao_romconfig.serial_number);
226 printf("software-version %s\n", ao_version);
229 static const char help_txt[] = "All numbers are in hex";
233 static __code struct ao_cmds *__xdata (ao_cmds[NUM_CMDS]);
234 static __xdata uint8_t ao_ncmds;
239 __xdata uint8_t cmds;
241 __code struct ao_cmds * __xdata cs;
243 for (cmds = 0; cmds < ao_ncmds; cmds++) {
245 for (cmd = 0; cs[cmd].func; cmd++)
248 cs[cmd].help+1+strlen(cs[cmd].help));
255 switch(ao_cmd_status) {
256 case ao_cmd_lex_error:
257 case ao_cmd_syntax_error:
258 puts("Syntax error");
266 ao_cmd_register(__code struct ao_cmds *cmds)
268 if (ao_ncmds >= NUM_CMDS)
269 ao_panic(AO_PANIC_CMD);
270 ao_cmds[ao_ncmds++] = cmds;
277 __xdata uint8_t cmd, cmds;
278 __code struct ao_cmds * __xdata cs;
279 void (*__xdata func)(void);
287 if (c == '\r' || c == '\n')
289 func = (void (*)(void)) NULL;
290 for (cmds = 0; cmds < ao_ncmds; cmds++) {
292 for (cmd = 0; cs[cmd].func; cmd++)
293 if (cs[cmd].help[0] == c) {
303 ao_cmd_status = ao_cmd_syntax_error;
308 __xdata struct ao_task ao_cmd_task;
310 __code struct ao_cmds ao_base_cmds[] = {
311 { help, "?\0Print this message" },
312 { ao_task_info, "T\0Show task states" },
313 { echo, "E <0 off, 1 on>\0Set command echo mode" },
314 { ao_reboot, "r eboot\0Reboot" },
315 { version, "v\0Show version" },
322 ao_cmd_register(&ao_base_cmds[0]);
323 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");