URL
https://opencores.org/ocsvn/or1k/or1k/trunk
Subversion Repositories or1k
[/] [or1k/] [branches/] [oc/] [orpmon/] [common/] [common.c] - Rev 1771
Go to most recent revision | Compare with Previous | Blame | View Log
#include "common.h" #include "uart.h" #include "screen.h" #include "support.h" #define MAX_COMMANDS 100 bd_t bd; int num_commands = 0; struct command_struct { const char *name; const char *params; const char *help; int (*func)(int argc, char *argv[]); } command[MAX_COMMANDS]; unsigned long strtoul(char *s) { int base = 10; char *ptmp; unsigned long val = 0; unsigned long digit = 1; ptmp = s; while (*ptmp != '\0') if (*ptmp++ == 'x') base = 16; while (ptmp-- != s) if ((*ptmp >= '0') && (*ptmp <= '9')) { val += (*ptmp - '0') * digit; digit *= base; } else if ((*ptmp >= 'a') && (*ptmp <= 'f')) { val += (*ptmp - 'a' + 10) * digit; digit *= base; } return val; } void putc (const char c) { debug ("getc %i, %i = %c\n", bd.bi_console_type, c, c); switch (bd.bi_console_type) { case CT_NONE: break; case CT_UART: uart_putc (c); break; case CT_CRT: screen_putc (c); break; case CT_SIM: __printf ("%c", c); break; } } int getc () { debug ("getc %i\n", bd.bi_console_type); switch (bd.bi_console_type) { case CT_NONE: case CT_CRT: case CT_SIM: return -1; case CT_UART: return uart_getc (); } return -1; } int testc () { debug ("testc %i\n", bd.bi_console_type); switch (bd.bi_console_type) { case CT_NONE: case CT_CRT: case CT_SIM: return -1; case CT_UART: return uart_testc (); } return -1; } int ctrlc () { if (testc ()) { switch (getc ()) { case 0x03: /* ^C - Control C */ return 1; default: break; } } return 0; } void change_console_type (enum bi_console_type_t con_type) { debug ("Console change %i -> %i\n", bd.bi_console_type, con_type); /* Close previous */ switch (bd.bi_console_type) { case CT_NONE: case CT_UART: case CT_CRT: case CT_SIM: break; } bd.bi_console_type = con_type; /* Initialize new */ switch (bd.bi_console_type) { case CT_NONE: break; case CT_UART: uart_init (); break; case CT_CRT: screen_init (); break; case CT_SIM: break; } } void register_command_func (const char *name, const char *params, const char *help, int (*func)(int argc, char *argv[])) { debug ("register_command '%s'\n", name); if (num_commands < MAX_COMMANDS) { command[num_commands].name = name; command[num_commands].params = params; command[num_commands].help = help; command[num_commands].func = func; num_commands++; } else printf ("Command '%s' ignored; MAX_COMMANDS limit reached\n", name); } /* Process command and arguments by executing specific function. */ void mon_command(void) { char c = '\0'; char str[1000]; char *pstr = str; char *command_str; char *argv[20]; int argc = 0; int end = 0; /* Show prompt */ #ifdef XESS printf ("\norp-xsv> "); #else printf ("\nbender> "); #endif /* Get characters from UART */ c = getc(); while (c != '\r' && c != '\f' && c != '\n') { if (c == '\b') pstr--; else *pstr++ = c; putc(c); c = getc(); } *pstr = '\0'; printf ("\n"); /* Skip leading blanks */ pstr = str; while (*pstr == ' ' && *pstr != '\0') pstr++; /* Get command from the string */ command_str = pstr; while (*pstr != '\0' && *pstr != ' ') pstr++; if (*pstr == '\0') end = 1; *pstr = '\0'; while (!end) { /* Go to next argument */ while (*pstr == ' ' && *pstr != '\0') pstr++; if (*pstr) argv[argc++] = pstr; else end = 1; } { int i, found = 0; for (i = 0; i < num_commands; i++) if (strcmp (command_str, command[i].name) == 0) { switch (command[i].func (argc, &argv[0])) { case -1: printf ("Missing/wrong parameters, usage: %s %s\n", command[i].name, command[i].params); break; } found = 1; break; } if (!found) printf ("Unknown command. Type 'help' for help.\n"); } } #if HELP_ENABLED /* Displays help screen */ int help_cmd (int argc, char *argv[]) { int i; for (i = 0; i < num_commands; i++) printf ("%-10s %-20s - %s\n", command[i].name, command[i].params, command[i].help); return 0; } #endif /* HELP_ENABLED */ void module_cpu_init (void); void module_memory_init (void); void module_eth_init (void); //void module_dhry_init (void); void module_camera_init (void); void module_tftp_init (void); void tick_init(void); /* List of all initializations */ void mon_init (void) { module_cpu_init (); module_memory_init (); module_eth_init (); // module_dhry_init (); module_camera_init (); module_tftp_init (); // tick_init(); } /* Main shell loop */ int main(int argc, char **argv) { /* Initialize controller */ change_console_type (CT_UART); mon_init (); if (HELP_ENABLED) register_command ("help", "", "shows this help", help_cmd); #ifdef XESS printf ("\nORP-XSV Monitor (type 'help' for help)\n"); #else printf ("\nBender Monitor (type 'help' for help)\n"); #endif while(1) mon_command(); }
Go to most recent revision | Compare with Previous | Blame | View Log