⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kslib.c

📁 S3C4510B的例程
💻 C
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************/
/*                                                                       */
/* FILE NAME                                      VERSION                */
/*                                                                       */
/*      kslib.c                    		 KS32C50100   : version 1.0 */
/*                                                                       */
/* COMPONENT                                                             */
/*                                                                       */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*                                                                       */
/* AUTHOR                                                                */
/*                                                                       */
/*                                                                       */
/* DATA STRUCTURES                                                       */
/*                                                                       */
/*                                                                       */
/* FUNCTIONS                                                             */
/*                                                                       */
/*      Diagnostic code for command line interface.                      */
/*                                                                       */
/* DEPENDENCIES                                                          */
/*                                                                       */
/*                                                                       */
/* HISTORY                                                               */
/*                                                                       */
/*************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "std.h"
#include "snds.h"
#include "memory.h"
#include "flash.h"
#include "down.h"
#include "pollio.h"
#include "uart.h"
#include "isr.h"
#include "lcd.h"
#include "dma.h"
#include "iic.h"
#include "timer.h"
#include "system.h"
#include "mac.h"
#include "kslib.h"
#include "dhry.h"
#include "sysconf.h"
#include "hdlc100.h"

DIAGCMD gDiagCmdTable[MAX_NUMOF_COMMAND] = {
"help",		HelpCmdLi,	4, "Help of command line interface. \0",
"ls",		ListOfCmd,	2, "List of command line interface COMMAND.\0",
"mc",		MCopyCli,	2, "Memory copy source to destinination\0",
"mf",		MFillCli,	2, "Memory pattern fill to Target Address\0",
"md",		MDumpCli,	2, "Memory dump\0",
"search",	MSearchCli,	6, "Memory search\0",
"compare",	MCompCli,	7, "Memory Compare\0",
"memtest",	MemTestCli,	7, "Memory read/write test\0",
"dmatest",	DmaTestCli,	7, "DMA test for long/short/byte\0",
"dcopy",	DCopyCli,	5, "DMA copy source to destinination\0",
"cache",	CacheCmdLi,	5, "Cache ON/OFF \0",
"mac",		MacCmdLi,	3, "MAC Test Function, Use [mac help] command\0",
"hdlc",		HdlcCmdLi,	3, "HDLC Test Function, Use [hdlc help] command\0",
"iic",		IicCmdLi,	3, "I2C read/write test command\0",
"march",	MarchTestCmdLi,	5, "Internal SRAM 10N March test\0",
"all",		AllTestCmdLi,	3, "All SNDS board function test\0",
"down",		DownPgmCmdLi,	4, "Download User Program\0",
"fuse",		FusePgmCmdLi,	4, "Fusing EEPROM\0",
"go",		GoPgmCmdLi,	2, "Start download user program\0",
"start",	StartCmdLi,	5, "Start User Program (Area : 0x1000000) \0",
"boot",		BootCmdLi,	4, "Boot pSOS at ROM Bank 1 user EEPROM\0",
"reset",	ResetCmdLi,	5, "Reset SNDS, Jump to Reset handler\0",
"exit",		ExitCmdLi,	4, "Exit command line interface to MENU mode\0",
"history",	HistoryCmdLi,	6, "Show commands executed recently\0"
} ;


extern TIME tm0,tm1 ;
void ClrAllErrRpt(void);
AllErrorReport AllErrorRpt={0.0,0,0,0,0,0} ;
U32 AllTestResult = 0 ;


void EtcTest(void) 
{
	DiagCmdLi() ;

}

/* 
 * Function : DiagCmdLi(void)
 * Description : Command Line Interface Diag
 */
void DiagCmdLi(void) 
{
	U32 error;
	U32 comlen;
	U32 CmdOffset = 0 ;
	//char commandline[MAX_LENGTHOF_COMMANDLINE]; // Up to 256 character 
	CMDPOOL commandpool[MAX_COMMAND_LIST],*pCmd=commandpool;

	int i ;

	InitSnds() ;   /* SNDS100 Board initialized */
	InitCommandPool(commandpool) ;

	Print("\n >>> ------------------------------------------- <<<") ;
	Print("\r >>> SNDS Command Line Interface for Diagonistic <<<") ;
        Print("\r >>> SNDS100 B'd Rev 1.0 for NetMCU,Diag Ver 1.0 <<<");
        Print("\r >>> Samsung Electronic Co. NETWORK DIV.         <<<");
        Print("\r >>>  >>>>>>>  Updated date is  1999.05.04       <<<");
	Print("\r >>> ------------------------------------------- <<<\n") ;

	while(CMDLI_TRUE)
	{
		ClearLcdDisplay() ;
		lPrint("CMD LINE I/F ... ") ;
		Print("SNDS(%d)> ",CmdOffset); // diag prompt 


		// Clear Command Line
		for(i=0;i<MAX_LENGTHOF_COMMANDLINE;i++) pCmd->commandline[i] = '\0';

		while(ReadCommand(pCmd,&CmdOffset));
		// if command length is over 256 bytes .
		comlen = pCmd->comlen & ~IS_USED;
		if(comlen > MAX_LENGTHOF_COMMANDLINE) 
		{
			ReportError(ERROR_TOO_LONG_COMMAND);
			continue;
		}
		error = RunCommand(pCmd);
		//if(error) ReportError(error);
		CmdOffset++ ;
		pCmd=pCmd->nextcmd;
	}
}	

void InitCommandPool(CMDPOOL *commandpool) 
{
	CMDPOOL *pCmd,*pStart,*pEnd,*prevCmd;
	int i,j;

	pCmd=commandpool;
	pStart=commandpool;
	pEnd=commandpool+MAX_COMMAND_LIST;
	for(i=0;i<MAX_COMMAND_LIST;i++)
	{
		// Clear Command Line
		for(j=0;j<MAX_LENGTHOF_COMMANDLINE;j++) pCmd->commandline[j] = '\0';
		pCmd->prevcmd = (i==0) ? pEnd : prevCmd ;
		prevCmd = pCmd;
		pCmd->nextcmd = (i==MAX_COMMAND_LIST-1) ? pStart : pCmd+1 ;
		pCmd++;
	}
}

U32 HistoryCmdLi(U8 argc, char (*argv)[MAX_LENGTHOF_ARGS],CMDPOOL *pCmd)
{
	U32 cnt=1,lcnt=10;
	//CMDPOOL *pCurCmd;

	while(lcnt--) {
		if(!(pCmd->prevcmd->comlen&IS_USED)) break; 
		pCmd=pCmd->prevcmd;
		Print("\r [%d]  %s\b",cnt++,pCmd->commandline);
	}
	/*
	lcnt = cnt ;
	for(cnt=1;cnt<=lcnt;cnt++) {
		Print("\r %d  -> %s\b",cnt,pCmd->commandline);
		pCmd=pCmd->nextcmd;
	}
	*/
	Print("\r");
	//size = hexstrtohex(argv[2], sizeofstr(argv[2]));
	return NO_ERROR;
}

CMDPOOL *ReadCommand(CMDPOOL *pCmd,U32 *CmdOffset)
{
	char ch;

	static U32 cur_pos=0;
	static U32 flag=0;
	static CMDPOOL *pCurCmd;

	if(!flag) pCurCmd=pCmd;

	switch(ch=get_ch(cur_pos)) {

		case '-' : 
		case '+' : 
			if(ch=='-') pCmd=pCmd->prevcmd;
			else pCmd=pCmd->nextcmd;
			return SearchCommand(pCmd,CmdOffset,&flag) ;

		case CR :  
		case BSP :
		case ' ' : 
		default : 
			return UpdateCommand(pCurCmd,pCmd,&cur_pos,&flag,CmdOffset,ch);
				  
	}
}

CMDPOOL *UpdateCommand(CMDPOOL *pCurCmd,CMDPOOL *pCmd,U32 *cur_pos,U32 *flag,U32 *CmdOffset,char ch)
{

	switch(ch) {
		case CR : 
			if(!*flag) PutPrompt(pCmd,CmdOffset,3);
			else {
				GetSelectedCommand(pCurCmd,pCmd,cur_pos);
				AnalyzeCommand(pCurCmd);
				Print("\r");
				pCurCmd->comlen |= IS_USED ;
				*flag=*cur_pos=0;
				return NULL;
			}
			break;

		case BSP :
			GetSelectedCommand(pCurCmd,pCmd,cur_pos);
			if(*cur_pos>=0) pCurCmd->commandline[(*cur_pos)--]='\0';
			break;

		case ' ':
			if(!*flag) {
				pCurCmd = ReadCommand(pCmd,CmdOffset);
				break;
			}

		default : 
			GetSelectedCommand(pCurCmd,pCmd,cur_pos);
			pCurCmd->commandline[(*cur_pos)++]=ch;
			pCurCmd->comlen++ ;	
			*flag = 1;
	}
	return pCmd;
}

void GetSelectedCommand(CMDPOOL *pCurCmd,CMDPOOL *pCmd,U32 *cur_pos)
{
	if(pCurCmd!=pCmd) {
		strcpy(pCurCmd->commandline,pCmd->commandline);
		*cur_pos = pCmd->comlen;
		pCurCmd->comlen = pCmd->comlen ;	
		pCmd=pCurCmd;
	}
}

void AnalyzeCommand(CMDPOOL *pCurCmd)
{
	char replace[10];
	char digit;
	U32	match_flag;
	CMDPOOL *pCmd=pCurCmd->prevcmd;
	int cnt;

	if(*pCurCmd->commandline=='!') // | *pCmd->commmandline='^')
	{
		//cnt = atol(pCurCmd->commandline+1);
		digit = *(pCurCmd->commandline+1);
		digit = (digit=='!') ? '0' : digit ;
		if(digit<'0' | digit>'9'){
			if(!SearchMatchedCommand(pCurCmd))
				Print("\rCan't find command which has such pattern");
		}
		else {
			//cnt = cnt -'0';
			cnt = digit -'0';
			while(cnt--) pCmd=pCmd->prevcmd;
			strcpy(pCurCmd->commandline,pCmd->commandline);
		}
	}
	else if(*pCurCmd->commandline=='^'){
		if(pCmd->comlen&IS_USED) SplitPattern(pCurCmd);
		else Print("\rNo previous command to replace!!!");
	}

}


CMDPOOL *SearchCommand(CMDPOOL *pCmd,U32 *CmdOffset,U32 *flag)
{
	if(pCmd->comlen & IS_USED) {
		*flag = 1;
		pCmd=PutPrompt(pCmd,CmdOffset,0);
	}
	else {
		*flag = 0;
		pCmd=PutPrompt(pCmd,CmdOffset,1);
	}

	return pCmd;
}

U32 SearchMatchedCommand(CMDPOOL *pCurCmd)
{
	char pattern[10];
	CMDPOOL *pCmd=pCurCmd->prevcmd;

	strcpy(pattern,pCurCmd->commandline+1);
	while(pCmd!=pCurCmd) {
		if(!(pCmd->comlen&IS_USED)) break; 
		if(strncmp(pattern,pCmd->commandline,strlen(pattern))==0)
		{
			strcpy(pCurCmd->commandline,pCmd->commandline);
			return 1;
		} 
		else pCmd=pCmd->prevcmd;
	}
	return 0;
}

void SplitPattern(CMDPOOL *pCurCmd)
{
	char pattern[10];
	char replace[10];
	char *src=pattern;
	char *dst=replace;
	char *command=pCurCmd->commandline+1;
	U32 i=0;

	while(*command!='^')
		*src++=*command++;
	command++;
	while(*command!='\0')
		*dst++=*command++;

	*dst=*src='\0';

	command=pCurCmd->prevcmd->commandline;

	while(*command!='\0')
	{
		if(strncmp(pattern,command,strlen(pattern))==0)
		{
			*command='\0';
			break;
		}
		command++;i++;
	}
	command=pCurCmd->prevcmd->commandline;
	src=i+command+strlen(pattern);
	command=pCurCmd->commandline;
	strcpy(command,pCurCmd->prevcmd->commandline);
	strcat(command,replace);
	strcat(command,src);
	
	Print("\r%s",command);
}

CMDPOOL *PutPrompt(CMDPOOL *pCmd,U32 *CmdOffset,U32 mode)
{
	switch(mode) {
		case 1 :
			Print("\rNo more command!!!");
			break;
		case 2 :
			Print("\rNo next command!!!");
			break;
	}
	switch(mode) {
		case 1 :
		case 2 :
			Print("\b "); 
		case 3 :
			(*CmdOffset)++;
	}
	Print("\rSNDS(%d)> ",*CmdOffset); 
	switch(mode) {
		case 0 :
			Print("%s ",pCmd->commandline); 
			put_byte('\b');
		case 3 :
			pCmd=ReadCommand(pCmd,CmdOffset);
	}
	return pCmd;
}


/* 
 * Function : U32 RunCommand(char *commandline, U32 comlinelen)
 * Description : Command Running
 */
U32 RunCommand(CMDPOOL *pCmd)
{
	U32 comindex;		
	U32 argc  ;
	char argv[MAX_NUMOF_ARGS][MAX_LENGTHOF_ARGS] = {{0,},};

	argc = ParseCommandLine(argv,pCmd->commandline); // return arguments number
	comindex = LookUpCommand(argv[0]); // argv[0] is command 

	if (gDiagCmdTable[comindex].cmd_action == NULL)
		return ERROR_BAD_COMMAND; 
		// return error code, command not found 

	// Run the command & return error code
	return (gDiagCmdTable[comindex].cmd_action)(argc,argv,pCmd); 
}


/*
 * Function :  LookUpCommand
 * Description : Search the command and return the index of the command.
 */
U32 LookUpCommand(char* cmd)
{
	U32 tableindex = 0;
	while( tableindex < MAX_NUMOF_COMMAND)
	{
		if ((gDiagCmdTable[tableindex].cmd_action != NULL)&&
			(!str_ncmp(cmd, gDiagCmdTable[tableindex].cmd_name , gDiagCmdTable[tableindex].cmd_length)))
			break;
		tableindex++;
	}
	return tableindex;
}

/*
 * 	Function :  ParseCommandLine
 *	Description : 
 */
U32 ParseCommandLine(char (*argv)[MAX_LENGTHOF_ARGS],char *commandline)
{
	U32 count = 1;
	U32 offset = 0;
	U32 i,j;
	char cmd;

	for(j = 0 ; j < MAX_NUMOF_ARGS ; j++)
	{
		for(i = 0 ; i < MAX_LENGTHOF_ARGS ; i++)
		{
			if((cmd = commandline[offset]) == SPACE)
			{
				offset++;
				count++;
				argv[j][i] = '\0';
				break;
			}


			switch(cmd)
			{
			case NL: argv[j][i] = '\0';
				 return count;
			case CR: argv[j][i] = '\0';
				 return count;
			default: argv[j][i] = cmd;
				 // Clear Used command line buffer
			 	 //commandline[offset] = NL ;
			 	 offset++;
				 break;
			}
		}
	}

	return count ;
}


/*
 * 	Function :  ReportError
 *	Description :
 */
void ReportError(U32 err)
{
	Print("Error Code :%x\n",err);
}

/*
 * 	Function :  str_ncmp
 *	Description : String Compare
 */
U32 str_ncmp(char* str1 , char* str2 , U32 n)
{
	U32 i = 0;
	while(i < n)
	{	
		if(str1[i] != str2[i]) return  1;
		i++;
	}
	return	0;
}

/*
 * 	Function :  strtoint
 *	Description : String Value to Interger Value
 */
U32 strtoint(char *str, U32 count)
{
	U32 temp;
	U32 total = 0 ;
	
	while(count)
	{
		temp = *str++ - '0'; 
		total += temp * power(10,count-1);
		count--;
	}
	return total; 
}

/*
 * 	Function :  hexstrtohex
 *	Description : String Value to HEX Value
 */
U32 hexstrtohex(char *str, U32 count)
{
	U32 temp;
	U32 total = 0 ;
		
	while(count)
	{
		count--;
		if(('0'<=(*str))&&((*str) <= '9'))
		{
			temp = *str - '0';
		    total += temp << (4*count);
		    temp = 0;
		}
		else if(('a'<=(*str))&&((*str) <= 'f'))
		{
			switch(*str)
			{
				case 'a': temp = 10; break;
				case 'b': temp = 11; break;
				case 'c': temp = 12; break;
				case 'd': temp = 13; break;
				case 'e': temp = 14; break;
				case 'f': temp = 15; break;
				default: break;
			}
			total +=temp << (4*count);
			temp = 0;
		}
		str++;
	}
	return total;
}

/*
 * 	Function :  sizeofstr
 *	Description : Size of String
 */
U32 sizeofstr(char *str)
{
	U32 size = 0;
	while((*str != '\0')&&(*str != NL)&&(*str != CR))
	{
		str++;
		size++;
	}
	return size;
}

/*
 * 	Function :  power
 *	Description : 
 */
U32 power(U32 base, U32 cha)
{
	U32 i ,temp;
	temp = 1;
	i=0;
	while(i < cha)
	{
		temp = base * temp;
		i++;
	}
	return temp;
}

/*
 * 	Function :  PrintHelpMessage
 *	Description :   
 */
void PrintHelpMessage(char *cmd)
{
	U8 index;
	index = LookUpCommand(cmd);
	put_string(gDiagCmdTable[index].cmd_help);
	Print("\r") ;

⌨️ 快捷键说明

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