📄 shell.c
字号:
#include "def.h"
#include "config.h"
#include "board.h"
#include "utils.h"
#include "option.h"
#include "shell.h"
void SetParams( void );
static unsigned int BaudSet[] =
{
4800, 9600, 19200, 38400, 57600, 115200, 0
};
static EnvParams Env;
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
{"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},
// {"n", "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 USB_DOWNLOAD_SUPPORT
{"usbload", "download file by usb", UsbDownload},
{"usbrun", "download file by usb and run", UsbDownloadRun},
#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
#ifdef USER_TEST
/*{"key", "4*2 Keystoke Test", Keystoke_Test },
{"ram", "RAM Self Test", SRAM_Test },
{"adc", "ADC Test", ADC_Test },
{"pwm", "PWM5 Beep Test", PWM5_Beep_Test },
{"t", "Temp Test, Nothing", Temp_Test },
{"d12", "PDIUSBD12 Test", PDIUSBD12_Test },
//{"can", "CAN Bus Test", Can_Test },
{"cf", "CF Card Test", CF_TureIDE_Test },
*/
{"cfinfor", "Get CF Card information", Get_TRUE_IDE_Infor },
#endif
//{"defset", "default setting for ucLinux", Default_Setting },
{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 GetBoardKey( void )
{
LedSet( ~GetKeyStatus() );
}
/*******************************************************************************************************/
void ShellIn( void )
{
U16 i, j, key ;
U16 led;
U16 x;
U16 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);
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
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 = 0x55 ;
//Can_Test( 0, NULL ) ; //Temporary Test
for ( ; ; )
{
if ( WaitEventWithTimeout( kbhit , 1 , 220 ) )
{
led = ( ~led );
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[] )
{
printf( "Feq of crystal is %dHz\n" , Fosc ) ; //晶振频率,10MHz~25MHz,应当与实际一至
printf( "Feq of system is %dHz\n" , Fcclk ) ; //系统频率,必须为Fosc的整数倍(1~32),且<=60MHZ
printf( "Feq of CCO is %dHz\n" , Fcco ) ; //CCO频率,必须为Fcclk的2、4、8、16倍,范围为156MHz~320MHz
printf( "Feq of VPB is %dHz\n" , Fpclk ) ; //VPB时钟频率,只能为(Fcclk / 4)的1 ~ 4倍
return 1 ;
}
/*******************************************************************************************************/
#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 %2d:%2d:%2d\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 = strtodec( str , strlen( str ) );
if ( hour > 24 || error )
{
printf( "hour error!\n" );
return -1;
}
str += i + 1;
i = 0;
if ( str[++i] != ':' )
if ( str[++i] != ':' )
error = 1;
str[i] = 0;
min = strtodec( str , strlen( str ) );
if ( min > 59 || 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 = strtodec( str , strlen( str ) );
if ( sec > 59 || 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 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -