?? shell.c
字號:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "../inc/def.h"
#include "../inc/44b.h"
#include "../inc/44blib.h"
#include "../inc/rtc.h"
#include "../inc/shell.h"
#include "../inc/flash.h"
#include "../net/armnet.h"
#include "../inc/option.h"
#include "../inc/nand.h"
#define __ROM_BASE 0x00000000
#define __ROM_SIZE 0x00200000
#define DRAM_BASE 0x0c000000
#define DRAM_SIZE 0x00800000
#define BOOT_PARAMS_ADDR (DRAM_BASE+0x100)
#define BIOS_BASE (__ROM_SIZE-0x10000)
#define BIOS_LOAD (__ROM_SIZE-2*SECTOR_SIZE-4)
#define ENV_ADD (SECTOR_SIZE-0x100)
#define WAIT_TIME 0xf00000
static unsigned int BaudSet [] = {4800, 9600, 19200, 38400, 57600, 115200, 0};
static unsigned char boot_params[256];
struct Partition{
U32 offset;
U32 size;
char *name;
};
extern struct Partition NandPartition[];
extern U32 NandFlashSize;
typedef struct {
unsigned char flag[8];
struct {
unsigned long ip;
unsigned long mask;
unsigned long gate;
}nic_cfg;
unsigned long baud;
unsigned long sys_clk;
unsigned long prog_s_addr;
unsigned long prog_r_addr;
struct Partition NandPartition[8];
unsigned char boot_params[256];
}EnvParams;
extern void NetSever(void);
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},
{"date", "show or set current date", GetDate},
{"time", "show or set current time", GetTime},
{"setweek", "set weekday", SetWeek},
{"clock", "show system running clock", SysClock},
{"setmclk", "set system running clock", ChgSysMclk},
{"setbaud", "set baud rate", ChgBaudRate},
{"ipcfg", "show or set IP address", SetIpAddr},
{"netload", "download file by net", LoadFromNet},
{"netrun", "load codes from NET port and run the codes", Net_Load_Run},
{"comload", "download file by uart", LoadFromUart},
{"comrun", "load codes from serial port and run the codes", Uart_Load_Run},
{"prog", "program flash", ProgFlash},
{"ap", "download file and program it to flash", APP_Auto_Prog_Flash},
{"copy", "copy flash from src to dst address", CopyFlash},
{"move", "move program from flash to sdram", Flash2Mem},
{"run", "run from sdram", RunProgram},
{"mrun", "move program stored in prog_s_addr to prog_r_addr and run", MoveRun},
{"boot", "boot from flash", BootLoader},
{"backup", "move bios to the top of flash", BackupBios},
{"md", "show memory data", MemoryDisplay},
{"memd", "show 8/16/32bits memory", MemoryShow},
{"mems", "set 8/16/32bits memory", MemorySet},
{"senv", "save enviroment value to flash", SaveEnv},
{"lenv", "load enviroment value from flash", LoadEnv},
{"setpa", "set program save(run) address", SetProgAddr},
{"setbp", "set program boot parameters", SetBootParams},
{"nfprog", "program nand flash", NandProg},
{"nfload", "load program from nand flash", NandLoad},
{"nferase", "erase nand flash partition", NandErase},
{"nfpart", "set nand flash partitions", NandPart},
{NULL, NULL, NULL}
};
extern TIME_STRUC SysTime;
char *WeekDay[7] = {"SUN","MON", "TUES", "WED", "THURS","FRI", "SAT"};
extern int MCLK;
extern NODE locnode;
extern unsigned int IP_ADDRESS;
extern unsigned int GATE_ADDRESS;
extern unsigned int MASK_ADDRESS;
extern unsigned int SERIAL_BAUD;
extern unsigned char MCLK_M;
extern unsigned char MCLK_P;
extern unsigned char MCLK_S;
extern unsigned long prog_s_addr;
extern unsigned long prog_r_addr;
extern unsigned int download_addr;
extern unsigned int download_begin;
extern unsigned int download_end;
extern unsigned int download_len;
int ip[4];
unsigned int strtoul(unsigned char *s)
{
unsigned long ret;
int 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;
}
void ultostr(unsigned char *s, unsigned int data)
{
int 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;
}
}
unsigned int strtobcd(char *s)
{
unsigned long ret;
int i;
ret = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9')
i = *s - '0';
else
return -1;
ret = (ret << 4) + i;
s++;
}
return ret;
}
unsigned int strtodec(char *str, int 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;
}
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);
Uart_Printf("boot from flash...\n");
(*fp)();
}
}
}
/*******************************************************************************************************/
void ShellIn(void)
{
int i, j, key;
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];
SysClock(0, NULL);
GetDate(0, NULL);
GetTime(0, NULL);
// LoadEnv(0, NULL);
Uart_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];
for(;;)
{
// GetBoardKey();
key = Uart_GetKey();
if(key)
{
if(key == ENTER_KEY)
{
int tmp;
Uart_SendByte('\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)
// Uart_Printf("Bad command\n");
Uart_Printf("\\>");
i = 0;
j = 0;
}
else if(key == BACK_KEY && j>0)
{
if(i==j)
{
i--;
j--;
Uart_Printf("\b \b");
}
else
{
i--;
j--;
for(x=j;x<i;x++)
command[x]=command[x+1];
command[x] = ' ';
Uart_Printf("\b \b");
for(x=j;x<i+1;x++)
Uart_SendByte(command[x]);
for(x=0;x<i+1-j;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
}
command[i] = '\0';
}
else if(key == 0x1b)
{
key = Uart_Getch();
if(key != 0x5b) continue;
key = Uart_Getch();
if(key == RIGHT_KEY)
{
if(j==i) continue;
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(RIGHT_KEY);
j++;
}
else if(key == LEFT_KEY)
{
if(j==0) continue;
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
j--;
}
else if(key == HOME_KEY)
{
for(x=0;x<j;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
j = 0;
}
else if(key == END_KEY)
{
for(x=j;x<i;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(RIGHT_KEY);
}
j = i;
}
else if(key == UP_KEY)
{
int cm_len;
int tmp_hj;
if(h_jj == MAX_CMD_HISTORY - 1)
{
Uart_SendByte(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)
{
Uart_SendByte(BEEP_KEY);
h_j = tmp_hj;
continue;
}
command = H_command[h_j];
for(x=0;x<j;x++)
{ //回到行首
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
for(x=0;x<cm_len;x++)
Uart_SendByte(command[x]);
for(;x<i;x++)
Uart_SendByte(' ');
for(;x>cm_len;x--)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
i = j = cm_len;
h_jj++;
}
else if(key == DOWN_KEY)
{
int cm_len;
if(h_jj == 0)
{
Uart_SendByte(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++)
{ //回到行首
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
for(x=0;x<cm_len;x++)
Uart_SendByte(command[x]);
for(;x<i;x++)
Uart_SendByte(' ');
for(;x>cm_len;x--)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(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;
Uart_SendByte(key);
j++;
}
else
{
for(x=i;x>j;x--)
command[x] = command[x-1];
command[j] = key;
i++;
for(x=j;x<i;x++)
Uart_SendByte(command[x]);
j++;
for(x=0;x<i-j;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(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) {
Uart_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)
{
Uart_Printf("%-8s ------ %s\n", CMD_INNER[i].cmd, CMD_INNER[i].hlp);
}
}
return 0;
}
/*******************************************************************************************************/
int GetParameter(char *str, int cnt)
{
int i, key;
i = 0;
while(1)
{
key = Uart_Getch();
if(key)
{
if(key == ENTER_KEY)
{
str[i] = 0;
return i;
}
else if(key == BACK_KEY && i>0)
{
i--;
Uart_Printf("\b \b");
}
else if(key == 0x1b)
{
//Uart_Getch();
//Uart_Getch();
return -1;
}
else if(key>=0x20 && key<=0x7e && i<cnt)
{
str[i++] = key;
Uart_SendByte(key);
}
}
}
}
/*******************************************************************************************************/
int SysClock(int argc, char *argv[])
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -