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;
24 static __xdata uint8_t lex_echo;
28 static __xdata char cmd_line[CMD_LEN];
29 static __xdata uint8_t cmd_len;
30 static __xdata uint8_t cmd_i;
50 /* backspace/delete */
51 if (c == '\010' || c == '\177') {
54 put_string("\010 \010");
62 while (cmd_len != 0) {
64 put_string("\010 \010");
80 if (cmd_len >= CMD_LEN - 2) {
85 cmd_line[cmd_len++] = c;
89 cmd_line[cmd_len++] = '\n';
90 cmd_line[cmd_len++] = '\0';
99 ao_cmd_lex_c = cmd_line[cmd_i++];
108 putchar(v + ('a' - 10));
112 ao_cmd_put16(uint16_t v)
115 for (i = 3; i >= 0; i--)
116 putnibble((v >> (i << 2)) & 0xf);
120 ao_cmd_put8(uint8_t v)
122 putnibble((v >> 4) & 0xf);
129 while (ao_cmd_lex_c == ' ' || ao_cmd_lex_c == '\t')
136 __xdata uint8_t r = ao_cmd_lex_error;
141 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
142 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - '0');
143 else if ('a' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'f')
144 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - 'a' + 10);
145 else if ('A' <= ao_cmd_lex_c && ao_cmd_lex_c <= 'F')
146 ao_cmd_lex_i = (ao_cmd_lex_i << 4) | (ao_cmd_lex_c - 'A' + 10);
152 if (r != ao_cmd_success)
159 __xdata uint8_t r = ao_cmd_lex_error;
164 if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9')
165 ao_cmd_lex_u32 = (ao_cmd_lex_u32 * 10) + (ao_cmd_lex_c - '0');
171 if (r != ao_cmd_success)
173 ao_cmd_lex_i = (uint16_t) ao_cmd_lex_u32;
177 ao_match_word(__code char *word)
180 if (ao_cmd_lex_c != *word) {
181 ao_cmd_status = ao_cmd_syntax_error;
193 while (ao_cmd_lex_c != '\n')
201 lex_echo = ao_cmd_lex_i != 0;
208 if (!ao_match_word("eboot"))
210 WDCTL = WDCTL_EN | WDCTL_MODE_WATCHDOG | WDCTL_INT_64;
211 ao_sleep(AO_SEC_TO_TICKS(2));
212 ao_panic(AO_PANIC_REBOOT);
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);
224 static const char help_txt[] = "All numbers are in hex";
228 static __code struct ao_cmds *__xdata (ao_cmds[NUM_CMDS]);
229 static __xdata uint8_t ao_ncmds;
234 __xdata uint8_t cmds;
236 __code struct ao_cmds * __xdata cs;
238 for (cmds = 0; cmds < ao_ncmds; cmds++) {
240 for (cmd = 0; cs[cmd].cmd != '\0'; cmd++)
248 switch(ao_cmd_status) {
249 case ao_cmd_lex_error:
250 case ao_cmd_syntax_error:
251 puts("Syntax error");
258 ao_cmd_register(__code struct ao_cmds *cmds)
260 if (ao_ncmds >= NUM_CMDS)
261 ao_panic(AO_PANIC_CMD);
262 ao_cmds[ao_ncmds++] = cmds;
266 ao_cmd(void *parameters)
269 __xdata uint8_t cmd, cmds;
270 __code struct ao_cmds * __xdata cs;
271 void (*__xdata func)(void);
281 if (c == '\r' || c == '\n')
283 func = (void (*)(void)) NULL;
284 for (cmds = 0; cmds < ao_ncmds; cmds++) {
286 for (cmd = 0; cs[cmd].cmd != '\0'; cmd++)
287 if (cs[cmd].cmd == c) {
297 ao_cmd_status = ao_cmd_syntax_error;
302 __xdata struct ao_task ao_cmd_task;
304 __code struct ao_cmds ao_base_cmds[] = {
305 { '?', help, "? Print this message" },
306 { 'T', ao_task_info, "T Show task states" },
307 { 'E', echo, "E <0 off, 1 on> Set command echo mode" },
308 { 'r', ao_reboot, "r eboot Reboot" },
309 { 'v', version, "v Show version" },
316 ao_cmd_register(&ao_base_cmds[0]);
317 ao_add_task(&ao_cmd_task, ao_cmd, "cmd");