📄 shell.c
字号:
#include <stdarg.h>
#include "../inc/def.h"
#include "../inc/config.h"
#include "../inc/board.h"
#include "../inc/utils.h"
#include "../inc/shell.h"
//add by chang
#include "../inc/iop.h"
#include "../inc/iic.h"
#include "../inc/D12def.h"
void SetParams(void);
static unsigned int BaudSet[] = {4800, 9600, 19200, 38400, 57600, 115200, 0};
unsigned int NetPortNumber=1;
static EnvParams Env;
int Net_Test_Flag=0;
extern U32 NandFlashSize;
struct Partition *pNandPart = Env.NandPartition;
typedef int (*cmdproc)(int argc, char *argv[]);
typedef struct {
char *cmd;
char *hlp;
cmdproc proc;
}CMD_STRUC;
CMD_STRUC CMD_INNER[] = {
{"help", "show this list", Help},
{"?", "= help", Help},
#ifdef RTC_TIMER_SUPPORT
{"date", "show or set current date", GetDate},
{"time", "show or set current time", GetTime},
{"setweek", "set weekday", SetWeek},
#endif
{"clock", "show system running clock", ShowSysClock},
#ifdef SET_SYSCLK_SUPPORT
{"setmclk", "set system running clock", ChgSysClk},
#endif
#ifdef SERIAL_PORTS_SWITCH
{"chguart", "change uart(0/1)", ChgUartPort},
#endif
#ifdef ETHERNET_SUPPORT
{"chgnet", "change net port(1/2)",ChgNetPort},
#endif
{"setbaud", "set baud rate", ChgBaudRate},
#ifdef TFTP_DOWNLOAD_SUPPORT
{"ipcfg", "show or set current IP address", SetIpAddr},
{"netload", "download file by net", LoadFromNet},
{"netrun", "download file by net and run", NetLoadRun},
{"g", "download file by net and run", NetLoadRun},
#endif
{"comload", "download file by uart", LoadFromUart},
{"comrun", "download file by uart and run", UartLoadRun},
#ifdef XMODEM_DOWNLOAD_SUPPORT
{"rx", "download file by xmodem", XmodemReceive},
{"rxrun", "download file by xmodem and run", XmodemReceiveRun},
#endif
#ifdef NOR_FLASH_SUPPORT
{"prog", "program flash", ProgFlash},
{"ap", "download file and program it to flash", AutoLoadAndProgFlash},
{"backup", "move bios to the top of flash", BackupBios},
{"copy", "copy flash from src to dst address", CopyFlash},
{"boot", "boot from flash", BootLoader},
#endif
{"run", "run program", RunProgram},
{"move", "move data from addr1 to addr2", MoveMem},
{"mrun", "move data form prog_s_addr to prog_r_addr and run", MoveRun},
{"md", "show memory data", MemoryDisplay},
{"memd", "show 8/16/32bits memory", MemoryShow},
{"mems", "set 8/16/32bits memory", MemorySet},
#ifdef SAVE_ENV_SUPPORT
{"senv", "save enviroment value to flash", SaveEnv},
#endif
{"machine", "set machine number", SetMachine},
{"setpa", "set program save(run) address", SetProgAddr},
{"setbp", "set program boot parameters", SetBootParams},
{"bootkey", "set key to autoboot", SetBootKey},
#ifdef NAND_FLASH_SUPPORT
{"nfpart", "set nand flash partitions", NandPart},
{"nferase", "erase nand flash partition", NandErase},
{"nfprog", "program nand flash", NandProg},
{"nfload", "load program from nand flash", NandLoad},
#endif
{"defset", "default setting ", Default_Setting },
#ifdef TEST_SUPPORT
{"test","test 4510 all",Test},
#endif
{NULL, NULL, NULL}
};
U32 download_addr;
U32 download_len;
#ifdef RTC_TIMER_SUPPORT
TIME_STRUC SysTime;
char *WeekDay[7] = {"MON", "TUES", "WED", "THURS","FRI", "SAT", "SUN"};
#endif
/*
void cache_flush(void)
{
unsigned int *pLRU, i;
pLRU = (unsigned int *)0x10004000;
for(i=0; i<(128<<2); i+=4)
pLRU[i] = 0;
}*/
void GetBoardKey(void)
{
/* unsigned int key;
void (*fp)(void) =(void (*)(void))BIOS_LOAD;
if((rPDATG&0xf0) != 0xf0)
{
key = rPDATG&0xf0;
Delay(500); //延时若干个100us,消除抖动
if(key == (rPDATG&0xf0))
{
while((rPDATG&0xf0) != 0xf0);
printf("boot from flash...\n");
(*fp)();
}
}*/
LedSet(~GetKeyStatus());
}
/*******************************************************************************************************/
void ShellIn(void)
{
int i, j, key, led;
int x;
int h_i, h_j, h_jj;
char t_command[MAX_CMD_LEN];
char * command;
char H_command[MAX_CMD_HISTORY][MAX_CMD_LEN];
printf("Machine Number is %d\n", Env.machine);
printf("IP address : %u.%u.%u.%u\n", (Env.nic_cfg.ip>>24)&0xff, (Env.nic_cfg.ip>>16)&0xff, (Env.nic_cfg.ip>>8)&0xff, Env.nic_cfg.ip&0xff);
// printf("IP MASK : %u.%u.%u.%u\n", (Env.nic_cfg.mask>>24)&0xff, (Env.nic_cfg.mask>>16)&0xff, (Env.nic_cfg.mask>>8)&0xff, Env.nic_cfg.mask&0xff);
// printf("IP GATE : %u.%u.%u.%u\n", (Env.nic_cfg.gate>>24)&0xff, (Env.nic_cfg.gate>>16)&0xff, (Env.nic_cfg.gate>>8)&0xff, Env.nic_cfg.gate&0xff);
// NetPortNumber=Env.netportnumber;
printf("NetPortNumber =%i \n", NetPortNumber);
switch(NetPortNumber)
{
case 1 : printf("RTL8201 is used \n");
break;
case 2 : printf("RTL8019 is used \n");
break;
default : printf(" no network card is used \n");
break;
}
printf("Serial baud : %d\n", Env.baud);
printf("Program save in %s flash\n", (Env.saved_in&1)?"nand":"nor");
printf("Program save address 0x%x\n", Env.prog_s_addr);
if(Env.saved_in&2)
printf("Initrd save address 0x%x\n", Env.initrd_addr);
printf("Program run address 0x%x\n", Env.prog_r_addr);
printf("Program boot params : %s\n", Env.boot_params);
// for(i=0; i<8; i++)
// printf("%d, %x ,%x\n", i, Env.NandPartition[i].offset, Env.NandPartition[i].size);
ShowSysClock(0, NULL);
#ifdef RTC_TIMER_SUPPORT
GetDate(0, NULL);
GetTime(0, NULL);
#endif
if((Env.boot_key>>16)==(('k'<<8)|'b')) {
U8 key_stat, cnt, chk_loops, chk_key;
chk_key = (Env.boot_key>>12)&3;
printf("Set boot key is key%d, check state %s to boot\n", chk_key+1, (Env.boot_key&0x100)?"high":"low");
chk_loops = 5;
while(chk_loops--) {
key_stat = (GetKeyStatus()>>chk_key)&1;
for(cnt=30; cnt; cnt--) {
Delay(1);
if(key_stat!=((GetKeyStatus()>>chk_key)&1))
break;
}
if(!cnt) {
if(key_stat==((Env.boot_key>>8)&1)) {
if(Env.boot_key&1)
BootLoader(0, NULL);
else
MoveRun(0, NULL);
}
chk_loops = 0;
}
}
}
printf("\\>");
i = 0;
j = 0;
h_i = 0;
h_j = 0;
h_jj = 0;
for(x=0;x<MAX_CMD_HISTORY;x++)
H_command[x][0] = '\0';
command = H_command[0];
led = 0;
for(;;)
{
//GetBoardKey();
if(WaitEventWithTimeout(kbhit, 1, 400))
{
led ^= 1;
LedSet(led);
}
else
{
key = getkey();
if(key == ENTER_KEY)
{
int tmp;
putch('\n');
memcpy(t_command, command, i+1);
if(i)
{
memcpy(H_command[h_i], command, i+1);
h_i++;
h_i %= MAX_CMD_HISTORY;
h_j = h_i;
h_jj = 0;
command = H_command[h_i];
command[0] = '\0';
}
tmp = ParseCmd(t_command, i);
//if(tmp<0)
// printf("Bad command\n");
printf("\\>");
i = 0;
j = 0;
}
else if(key == BACK_KEY && j>0)
{
if(i==j)
{
i--;
j--;
printf("\b \b");
}
else
{
i--;
j--;
for(x=j;x<i;x++)
command[x]=command[x+1];
command[x] = ' ';
printf("\b \b");
for(x=j;x<i+1;x++)
putch(command[x]);
for(x=0;x<i+1-j;x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
}
command[i] = '\0';
}
else if(key == 0x1b)
{
key = getch();
if(key != 0x5b) continue;
key = getch();
if(key == RIGHT_KEY)
{
if(j==i) continue;
putch(0x1b);
putch(0x5b);
putch(RIGHT_KEY);
j++;
}
else if(key == LEFT_KEY)
{
if(j==0) continue;
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
j--;
}
else if(key == HOME_KEY)
{
for(x=0;x<j;x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
j = 0;
}
else if(key == END_KEY)
{
for(x=j;x<i;x++)
{
putch(0x1b);
putch(0x5b);
putch(RIGHT_KEY);
}
j = i;
}
else if(key == UP_KEY)
{
int cm_len;
int tmp_hj;
if(h_jj == MAX_CMD_HISTORY - 1)
{
putch(BEEP_KEY);
continue;
}
tmp_hj = h_j;
if(h_j==0) h_j = MAX_CMD_HISTORY - 1;
else h_j--;
cm_len = strlen(H_command[h_j]);
if(cm_len == 0)
{
putch(BEEP_KEY);
h_j = tmp_hj;
continue;
}
command = H_command[h_j];
for(x=0;x<j;x++)
{ //回到行首
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
for(x=0;x<cm_len;x++)
putch(command[x]);
for(;x<i;x++)
putch(' ');
for(;x>cm_len;x--)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
i = j = cm_len;
h_jj++;
}
else if(key == DOWN_KEY)
{
int cm_len;
if(h_jj == 0)
{
putch(BEEP_KEY);
continue;
}
h_j ++;
h_j %= MAX_CMD_HISTORY;
cm_len = strlen(H_command[h_j]);
command = H_command[h_j];
for(x=0;x<j;x++)
{ //回到行首
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
for(x=0;x<cm_len;x++)
putch(command[x]);
for(;x<i;x++)
putch(' ');
for(;x>cm_len;x--)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
i = j = cm_len;
h_jj--;
}
}
else if(key>=0x20 && key<=0x7e && i<(MAX_CMD_LEN-1))
{
if(i==j)
{
command[i++] = key;
putch(key);
j++;
}
else
{
for(x=i;x>j;x--)
command[x] = command[x-1];
command[j] = key;
i++;
for(x=j;x<i;x++)
putch(command[x]);
j++;
for(x=0;x<i-j;x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
}
command[i] = '\0';
}
}
}
}
/*******************************************************************************************************/
int ParseCmd(char *cmdline, int cmd_len)
{
int argc, num_commands;
char *argv[MAX_ARGS];
ParseArgs(cmdline, &argc, argv);
/* only whitespace */
if(argc == 0)
return 0;
num_commands = GetCmdMatche(argv[0]);
if(num_commands<0) {
printf("No '%s' command, please type 'help' or '?' 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;
}
/*******************************************************************************************************/
void ParseArgs(char *cmdline, int *argc, char **argv)
{
#define STATE_WHITESPACE 0
#define STATE_WORD 1
char *c;
int state = STATE_WHITESPACE;
int i;
*argc = 0;
if(strlen(cmdline) == 0)
return;
/* convert all tabs into single spaces */
c = cmdline;
while(*c != '\0')
{
if(*c == '\t')
*c = ' ';
c++;
}
c = cmdline;
i = 0;
/* now find all words on the command line */
while(*c != '\0')
{
if(state == STATE_WHITESPACE)
{
if(*c != ' ')
{
argv[i] = c; //将argv[i]指向c
i++;
state = STATE_WORD;
}
}
else
{ /* state == STATE_WORD */
if(*c == ' ')
{
*c = '\0';
state = STATE_WHITESPACE;
}
}
c++;
}
*argc = i;
#undef STATE_WHITESPACE
#undef STATE_WORD
}
/*******************************************************************************************************/
int GetCmdMatche(char *cmdline)
{
int i;
for(i=0; CMD_INNER[i].cmd!=NULL; i++)
{
if(strlen(cmdline)!=strlen(CMD_INNER[i].cmd))
continue;
if(strncmp(CMD_INNER[i].cmd, cmdline, strlen(CMD_INNER[i].cmd))==0)
return i;
}
return -1;
}
/*******************************************************************************************************/
int Help(int argc, char *argv[])
{
int i;
for(i=0; CMD_INNER[i].cmd!=NULL; i++)
{
if(CMD_INNER[i].hlp!=NULL)
{
printf("%-8s ------ %s\n", CMD_INNER[i].cmd, CMD_INNER[i].hlp);
}
}
return 0;
}
/*******************************************************************************************************/
int ShowSysClock(int argc, char *argv[])
{
int i, j;
SysClock clk[4];
i = GetSysClock(clk);
if(i>3) {
puts("Get System Clock Fail\n");
return -1;
}
for(j=0; j<i; j++)
printf("%s clock is %dHz\n", clk[j].name, clk[j].freq);
return 0;
}
/*******************************************************************************************************/
#ifdef RTC_TIMER_SUPPORT
int GetTime(int argc, char *argv[])
{
int i, error = 0;
char *str;
unsigned int hour, min, sec;
RtcGetTime(&SysTime);
printf("Current time is %2x:%2x:%2x\n", SysTime.hour, SysTime.min, SysTime.sec);
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) {
printf("hour error!\n");
return -1;
}
str += i+1;
i = 0;
if(str[++i]!=':')
if(str[++i]!=':')
error = 1;
str[i] = 0;
min = strtobcd(str);
if(min>0x59||error) {
printf("minute error!\n");
return -1;
}
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) {
printf("second error!\n");
return -1;
}
SysTime.hour = hour;
SysTime.min = min;
SysTime.sec = sec;
RtcSetTime(&SysTime);
printf("Set ");
GetTime(0, NULL);
}
return 0;
}
/*******************************************************************************************************/
int GetDate(int argc, char *argv[])
{
int i, error = 0;
char *str;
unsigned int year, month, day;
RtcGetTime(&SysTime);
printf("Current date is %x-%x-%x [%s]\n", SysTime.year, SysTime.month, SysTime.day, WeekDay[(SysTime.weekday-1U)%7U]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -