shell.c

来自「通过串口实现的CLI界面。在超级终端中实现界面的显示。支持命令记录和带多参数。功」· C语言 代码 · 共 1,427 行 · 第 1/3 页

C
1,427
字号
/*
**************************************************************************************************
*
*
*
*
*
*
*
*
*************************************************************************************************
*/

#include "../inc/flash.h"
#include "../inc/shell.h"
#include "../inc/serial.h"
#include "../inc/brd.h"
#include "../inc/s4510b.h"
#include "../inc/modem.h"
#include "../inc/iic.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#define __dbgShell
INT32S downloadLen;
INT32S downloadAdrs;
extern tTIME CurrentTime;

const INT32U BaudTable[7] = {2400,	4800,	9600,	19200,	38400,	57600,	115200 };

CMD_STRUC CMD_INNER[] =
				{ 	{"help", 	"show help !", Help},
					{"ls",   	"= help !", Help},
					{"date", 	"date 2008-05-13 !", GetDate},
					{"time", 	"time 16:24:22 !", GetTime},
					{"setweek", "setweek * (0 to 6) !", SetWeek},
					{"clock",   "show system running clock !", SysClock},
					{"bdset", 	"set baud rate !", ChgBaudRate},
					{"bdshow",	"show baud rate !",baudShow},
//					{"ipcfg", "show or set IP address", SetIpAddr},
					{"sback",  "send code back via xmodem !", tProgBack},
					{"sendx",  "load codes via xmodem(UART) !", XmodemClient_t},
					{"burn",   "burn ram code to flash !", progToFlash},
//					{"prog",   "program flash", ProgFlash},
//					{"appprog", "load APP file to ram and AutoProgram flash", APP_Auto_Prog_Flash},
//					{"copy",  "copy flash from src to dst address", CopyFlash},
//					{"move",  "move program from flash to sdram", Flash2Mem},
//					{"boot",  "boot from flash", BootLoader},
					{"efeb",   "Erase all the flash except BIOS section !", earseAllExceptBios},
//					{"progb",  "load BIOS file to ram and AutoProgram flash", Bios_Auto_Prog_Flash},
//					{"md",	"show memory data", MemoryDisplay},
					{"runapp", "run application code !", runApp},
#ifdef __dbgShell
					{"reset", "software reset !", resetRun},	 
#endif
					{NULL, NULL, NULL}
				};


INT8S *WeekDay[7] = {"SUN","MON", "TUE", "WED", "THU","FRI", "SAT"}; 

 
  
  void ultostr(INT8U *s, INT32U data)
  {
	  INT32S i;
	  
	  s[8] = 0;
	  for(i=7; i>=0; i--, data >>=4)
	  {
		  if((data&0xf)<=9)
			  s[i] = (data&0xf)+'0';
		  else
			  s[i] = (data&0xf)+'a'-0x0a;
	  }   
  }
  
  
  INT32U strtobcd(INT8S *s)
  {
	  unsigned long ret;
	  INT32S i;
  
	  ret = 0;
  
	  while (*s != '\0') {
		  if (*s >= '0' && *s <= '9')
			  i = *s - '0'; 	  
		  else
			  return -1;
		  ret = (ret << 4) + i;
		  s++;
	  }
  
	  return ret;
  }
  
  INT32U strtodec(INT8S *str, INT32S cnt)
  {
	  unsigned long i, data = 0;  
	  
	  for(i=0; i<cnt; i++)
	  {
		  data *= 10;
		  if(str[i]<'0'||str[i]>'9')
			  return -1;
		  data += str[i]-'0';		  
	  }   
	  return data;
  }

INT32S strtoul(INT8U *s)
{
	INT32S ret;
	INT32S i;

	ret = 0;

	while (*s != '\0') {
		if (*s >= '0' && *s <= '9')
			i = *s - '0';
		else if (*s >= 'a' && *s <= 'f')
			i = *s - 'a' + 0xa;
		else if (*s >= 'A' && *s <= 'F')
			i = *s - 'A' + 0xa;
		else
			return -1;
		ret = (ret << 4) + i;
		s++;
	}

	return ret;
}

INT32S ParseCmd(INT8S *cmdline, INT32S cmd_len)
{
	INT32S argc, num_commands;
	INT8S *argv[MAX_ARGS];

	ParseArgs(cmdline, &argc, argv);

	/* only whitespace */
	if(argc == 0) 
		return 0;
	
	num_commands = GetCmdMatche(argv[0]);
	if(num_commands<0){
		urtPrintf("->Oh! Sorry..!!\n");
		urtPrintf("->No '%s' command, please type 'help' or 'ls' for a command list\n", argv[0]);
		return -1;
		}
	if(CMD_INNER[num_commands].proc!=NULL)	
		CMD_INNER[num_commands].proc(argc, argv);
				
	return 0;			
}

/*******************************************************************************************************/
INT32S GetCmdMatche(INT8S *cmdline)
{
	INT32S i;	
	
	for(i=0; CMD_INNER[i].cmd != NULL; i++)
	{
		if(strncmp(CMD_INNER[i].cmd, cmdline, strlen(CMD_INNER[i].cmd)) == 0)
			return i;
	}
	
	return -1;
}
/*******************************************************************************************************/
INT32S Help(INT32S argc, INT8S *argv[])
{
	
	INT32S i;
	if(argc <2){
		urtPrintf("\n\n\n\n\n\n\n\n\n\n\n\n");
		for(i=0; CMD_INNER[i].cmd!=NULL; i++)
		{
			if(CMD_INNER[i].hlp!=NULL)
			{
				urtPrintf(CMD_INNER[i].cmd);
				urtPrintf("	--> ");
				urtPrintf(CMD_INNER[i].hlp);
				urtPutChar('\n');
			}
		}
	}
	else
	urtPrintf("\nBad command detect!!\n");
	
	return 0;
}





/*******************************************************************************************************/
INT32S SysClock(INT32S argc, INT8S *argv[])
{

	urtPrintf("\nSystem is running @ %dHz\n", fMCLK);
	
	return 0;
}

INT32S GetDate(INT32S argc, INT8S *argv[])
{


	INT32S i, error = 0;
	INT8S tmp[8];
	INT8S *str;
	INT32U year, month, day;
	
	DS1307TimeRead(&CurrentTime);
	urtPrintf("Current date is %d-%x-%x %s\n", CurrentTime.Time_Year + 2000, CurrentTime.Time_Month, CurrentTime.Time_Date, WeekDay[CurrentTime.Time_Day]);

	if(argc<2)
		return 0;	
		
	str = argv[1];	
	
	for(i=0; i<5; i++)	
		tmp[i] = str[i];
	if(tmp[4]!='-')
		error = 1;
	year = strtodec(str, 4);
	if(year<2000||error)
	{			
		urtPrintf("year error!\n");
		return 0;
	}		
				
	str += 5;
	i = 0;		
	if(str[++i]!='-')
		if(str[++i]!='-')
			error = 1;
	str[i] = 0;					
	month = strtobcd(str);
	if((month-1)>0x11||error)
	{				
		urtPrintf("month error!\n");
		return 0;
	}		
		
	str += i+1;		
	i = 0;
	if(str[++i]!=0)
		if(str[++i]!=0)
			error = 1;
	str[i] = 0;		
	day = strtobcd(str);
	if((day-1)>0x30||error)
	{		
		urtPrintf("day error!\n");
		return 0;
	}	
	
	CurrentTime.Time_Year= year-2000;
	CurrentTime.Time_Month = month;
	CurrentTime.Time_Date= day;		
	DS1307PreSet(&CurrentTime);
	
	urtPrintf("Set date %d-%2.2x-%2.2x %s\n", CurrentTime.Time_Year + 2000, CurrentTime.Time_Month, CurrentTime.Time_Date, WeekDay[CurrentTime.Time_Day]);
	
	return 1;

}


/*******************************************************************************************************/
INT32S SetWeek(INT32S argc, INT8S *argv[])
{

	INT32S i;
	INT32S error = 0;	
	
	if(argc<2)
		error = 1;	
	
	i = strtodec(argv[1], 1);
	if((i-1)>5)
		error = 1;
	
	if(!error)
	{
		CurrentTime.Time_Day = i;
		DS1307PreSet(&CurrentTime);
		urtPrintf("Set to %s\n", WeekDay[i]);
	}
	else
	{
		urtPrintf("Please enter weekday\n");
		urtPrintf("0 : Sunday\n");
		urtPrintf("1 : Monday\n");
		urtPrintf("2 : Tuesday\n");
		urtPrintf("3 : Wednesday\n");
		urtPrintf("4 : Thursday\n");
		urtPrintf("5 : Friday\n");
		urtPrintf("6 : Satday\n");		
	}
	return 0;	
}


/*******************************************************************************************************/
INT32S GetTime(INT32S argc, INT8S *argv[])
{

	INT32S i, error = 0;
	INT8S *str;
	INT32U hour, min, sec;
	
	DS1307TimeRead(&CurrentTime);
	urtPrintf("Current time is %2.2x:%2.2x:%2.2x\n", CurrentTime.Time_Hour, CurrentTime.Time_Minute, CurrentTime.Time_Second);
		

   			
	
	if(argc>1)
	{
		str = argv[1];
		i = 0;
		if(str[++i]!=':')
			if(str[++i]!=':')
				error = 1;
		str[i] = 0;		
		hour = strtobcd(str);
		if(hour>0x23||error)
		{
			urtPrintf("hour error!\n");
			return error;
		}
		
		str	+= i+1;
		i = 0;		
		if(str[++i]!=':')
			if(str[++i]!=':')
				error = 1;
		str[i] = 0;		
		min = strtobcd(str);		
		if(min>0x59||error)
		{
			urtPrintf("minute error!\n");
			return error;
		}
		
		str	+= i+1;
		i = 0;		
		if(str[++i]!=0)
			if(str[++i]!=0)
				error = 1;
		str[i] = 0;		
		sec = strtobcd(str);		
		if(sec>0x59||error)
		{
			urtPrintf("second error!\n");
			return error;
		}
		
		CurrentTime.Time_Hour= hour;
		CurrentTime.Time_Minute= min;
		CurrentTime.Time_Second= sec;		
		DS1307PreSet(&CurrentTime);
		
		urtPrintf("Set time %x:%x:%x\n", hour, min, sec);								
	}	
	
	return error;

 // hojin add //
}


/*******************************************************************************************************/
INT32S ChgSysMclk(INT32S argc, INT8S *argv[])
{
#if 0
	INT32S i, mdiv, pdiv, sdiv;
	INT8S tmp[4];
	
	urtPrintf("This function is for adjust system running clock!!!\n");
	urtPrintf("Current clock is %dHz\n", MCLK);
	urtPrintf("Please enter the PLL parameter to use, mdiv[0-255], pdiv[0-63], sdiv[0-3]\n");
	
	urtPrintf("mdiv: ");	
	if((mdiv=strtodec(tmp, GetParameter(tmp, 3)))<0)		
	{
		urtPrintf("\nget mdiv Error!\n");
		return 0;
	}					
	
	urtPrintf("\npdiv: ");
	if((pdiv=strtodec(tmp, GetParameter(tmp, 3)))<0)		
	{
		urtPrintf("\nget pdiv Error!\n");
		return 0;
	}	
	
	urtPrintf("\nsdiv: ");
	
	if((sdiv=strtodec(tmp, GetParameter(tmp, 3)))<0)		
	{
		urtPrintf("\nget sdiv Error!\n");
		return 0;
	}		
	
	urtPrintf("\nYou set System clock mdiv = %d, pdiv = %d, sdiv = %d\n", mdiv, pdiv, sdiv);
	
	i = 8000;
	while(i--);
		
	ChangePllValue(mdiv, pdiv, sdiv);
	
	i = 8000;
	while(i--);	
	Uart_Init(0,SERIAL_BAUD);
	urtPrintf("Current clock is %dHz\n", MCLK);	
	MCLK_M = mdiv; MCLK_P = pdiv; MCLK_S = sdiv;
#endif
	urtPrintf("->Change sysclock running ok!!\n");

	return 0;
}


/*******************************************************************************************************/
INT32S ChgBaudRate(INT32S argc, INT8S *argv[])
{

	INT32S i;
	INT8U tBaud;
	
	for(i=0; i<7; i++)
		urtPrintf("%d ->: %d\n", i, BaudTable[i]);
		
	urtPrintf("Please enter the number to choice : ");
	
	while(1)
	{
		UBYTE ch = urtGetChar();
		
		if((ch<='6' && ch>='0'))
		{
			tBaud= ch-'0';
			urtPrintf("Baud rate set to ->: %d [y/n]?\n", BaudTable[tBaud]);
			i = 0x4000;
			while(i--);		//wait transmit end
			break;
			urtGetChar();
		}
	}	
	while(1){
		UBYTE	CH = urtGetChar();
		if(CH == 'y' || CH == 'Y'){
			urtPrintf("\n\nBaud rate has been set to ->: %d\n", BaudTable[tBaud]);	
			
			changeBand(tBaud);
			break;
		}else{
			urtPrintf("BaudRate not be changed !!\n");	
			break;
			}
	}
	return 0;
}


INT32S baudShow(INT32S argc, INT8S *argv[])
{
	INT32U urt0baud,urt1baud;
	INT8U  i;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?