/********************************************************************************************************************* *File : cmd_line.c *Description : Serial command line interprenter.The program accepts commands with argumentsfrom the internal UART, : and executes them.THE UART USE 19200bps with 11.059MHz crystal !!! *Version : 1.0 *Create by : yuanguiyou *Date : 2008/03/01 *Note : The original code was graciously shared by Jon Ward.and written for Keil compiler.now to TKStudio IDE ********************************************************************************************************************* *Compiler : SDCC(version >= 2.2.2) ********************************************************************************************************************* **MODIFY INFORMATION *Modify by : *Date : *Reason : **********************************************************************************************************************/ /* Includes from standard lib. */ #include /* printf_small */ #include /* toupper */ #include /* strncmp strncpy strchr */ #include <8051.h> /* Special Function Register*/ #include /* ser_putc ser_getc ser_init*/ /* Include header file */ #include "cmd_line.h" /********************************************************************************************************************** *Function : putchar *Description : This is a support function for the printf_small function *Param : c--the char will be send *Return : None *Note : Call lib function ser_putc 19200bps with 11.059MHz crystal **********************************************************************************************************************/ void putchar(char c) { ser_putc(c); } /********************************************************************************************************************** *Function : cmdid_search *Description : This function searches the cmd_tbl for a specific command *Param : cmstr -- string will be search *Return : returns the ID associated with that command or CID_LAST if theres no matching command. *Note : **********************************************************************************************************************/ static unsigned char cmdid_search (char *cmdstr) { struct cmd_st *ctp; for (ctp = cmd_tbl; ctp <= &cmd_tbl [CMD_TBL_LEN-1]; ctp++) { if (strncmp (ctp->st_cmdstr, cmdstr, cmdndx) == 0) return (ctp->id); } return (CID_LAST); } /********************************************************************************************************************** *Function : strupr *Description : change string to upper *Param : src -- string will be case upper *Return : returns string after upper *Note : No programmer's library would be complete without strupr! **********************************************************************************************************************/ char *strupr (char *src) { char *s; for (s = src; *s != '\0'; s++) *s = toupper (*s); return (src); } /********************************************************************************************************************** *Function : cmd_proc *Description : This function processes the cmd command. *Param : cmd -- command string get from UART *Return : None *Note : the function only printf the command name,you can change for you app **********************************************************************************************************************/ void cmd_proc (char *cmd) { xdata char cmdstr_buf [1 + MAX_CMD_LEN]; xdata char argstr_buf [1 + MAX_CMD_LEN]; char *argsep; unsigned char id; /* First, copy the command and convert it to all uppercase. */ strncpy (cmdstr_buf, cmd, sizeof (cmdstr_buf) - 1); cmdstr_buf [sizeof (cmdstr_buf) - 1] = '\0'; /* For safety we terminate the buffer. */ strupr (cmdstr_buf); /* Next, find the end of the first thing in the buffer. Since the command ends with a space,we'll look for that. * * NULL-Terminate the command and keep a pointer to the arguments. */ argsep = strchr (cmdstr_buf, ' '); if (argsep == NULL) { argstr_buf [0] = '\0'; } else { cmdndx = argsep - cmdstr_buf; strcpy (argstr_buf, argsep + 1); *argsep = '\0'; } /* Search for a command ID, then switch on it.you should invoke each function here. */ id = cmdid_search (cmdstr_buf); switch (id) { case CID_SET_CLK: printf_small("set clk"); break; case CID_READ_CLK: printf_small("read clk"); break; case CID_SCAN_CLK: printf_small("scan clk"); break; case CID_SET_ALM: printf_small("set alm"); break; case CID_READ_ALM: printf_small("read clk"); break; case CID_CLR_ALM: printf_small("clear clk"); break; case CID_HELP: printf_small("%s",helptext); break; case CID_LAST: break; default : printf_small("unknow command"); } } /********************************************************************************************************************** *Function : cmdb_init *Description : Initialize the command buffer and index. *Param : Noe *Return : None *Note : the function call first before use cmd_proc **********************************************************************************************************************/ void cmdb_init (void) { cmdndx = 0; cmdbuf [0] = '\0'; } /********************************************************************************************************************** *Function : cmdb_prompt *Description : Prompt for a command. *Param : None *Return : None *Note : **********************************************************************************************************************/ void cmdb_prompt (void) { printf_small("CMD>"); } /********************************************************************************************************************** *Function : cmdb_scan *Description : This functions gets characters from the serial uffer and appends them to the end of the command buffer. *Param : None *Return : If no character is available the function returns NULL. If a newline character is read, a pointer to * : the buffer is returned. *Note : Do NOT reinvoke this function before extracting the command from the buffer. **********************************************************************************************************************/ char * cmdb_scan (void) { int c; while (1) { if (!ser_charAvail) /* No character */ break; /* Exit NOW */ c = ser_getc(); if (c == '\r') /* Newline? */ { printf_small("\r\n"); /* Output it and */ return (cmdbuf); /* Return the buffer address */ } if ((c == '\b') && (cmdndx != 0)) /* Backspace? */ { printf_small("\b\b"); cmdbuf [--cmdndx] = '\0'; continue; } if (!isprint (c)) /* Printable character? */ { BEEPCHAR: printf_small("\x7"); continue; } if (cmdndx >= MAX_CMD_LEN) /* Past buffer length? */ goto BEEPCHAR; /*printf_small("%c",c);*/ /* Output char use this as a sort of echo */ cmdbuf [cmdndx++] = (unsigned char)c; /* Add to the buffer */ cmdbuf [cmdndx] = '\0'; /* NULL-Terminate the buffer */ } return (NULL); } /********************************************************************************************************************** *Function : main *Description : main test function *Param : None *Return : None *Note : **********************************************************************************************************************/ void main (void) { EA=1; /* Enable all interrupts */ while (1) { char *cmd; ser_init(); cmdb_init (); /* init a new command instance */ cmdb_prompt (); /* prompt for a command */ /* get command */ for (cmd = NULL; cmd == NULL; cmd = cmdb_scan ()) { /* do something useful here e.g. watchdog. */ } cmd_proc (cmd); /* process the command */ } }