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

📄 shell.c

📁 这是ARM的启动装载代码
💻 C
📖 第 1 页 / 共 3 页
字号:
{
	int i;	
	
	for(i=0; CMD_INNER[i].cmd!=NULL; i++)
	{
		if(CMD_INNER[i].hlp!=NULL)
		{
			Uart_Printf(CMD_INNER[i].cmd);
			Uart_Printf("	------	");
			Uart_Printf(CMD_INNER[i].hlp);
			Uart_SendByte('\n');
		}
	}
	
	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();
			}
			else if(key>=0x20 && key<=0x7e && i<cnt)
			{
				str[i++] = key;
				Uart_SendByte(key);
			}				
		}
	}
	
}


/*******************************************************************************************************/
int SysClock(int argc, char *argv[])
{
	Uart_Printf("System is running @ %dHz\n", MCLK);
	return 0;
}

int GetDate(int argc, char *argv[])
{
	int i, error = 0;
	char tmp[8];
	char *str;
	unsigned int year, month, day;
	
	RtcGetTime(&SysTime);
	Uart_Printf("Current date is %d-%x-%x %s\n", SysTime.year, SysTime.month, SysTime.day, WeekDay[SysTime.weekday]);

	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)
	{			
		Uart_Printf("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)
	{				
		Uart_Printf("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)
	{		
		Uart_Printf("day error!\n");
		return 0;
	}	
	
	SysTime.year  = year-2000;
	SysTime.month = month;
	SysTime.day   = day;		
	RtcSetDay(&SysTime);	
	
	Uart_Printf("Set date %d-%2.2x-%2.2x %s\n", year, month, day, WeekDay[SysTime.weekday]);
	
	return 1;
}


/*******************************************************************************************************/
int SetWeek(int argc, char *argv[])
{
	int i;
	int error = 0;	
	
	if(argc<2)
		error = 1;	
	
	i = strtodec(argv[1], 1);
	if((i-1)>5)
		error = 1;
	
	if(!error)
	{
		SysTime.weekday = i;
		RtcSetWeek(&SysTime);
		Uart_Printf("Set to %s\n", WeekDay[i]);
	}
	else
	{
		Uart_Printf("Please enter weekday\n");
		Uart_Printf("0 : Sunday\n");
		Uart_Printf("1 : Monday\n");
		Uart_Printf("2 : Tuesday\n");
		Uart_Printf("3 : Wednesday\n");
		Uart_Printf("4 : Thursday\n");
		Uart_Printf("5 : Friday\n");
		Uart_Printf("6 : Satday\n");		
	}
	
	return 0;	
}


/*******************************************************************************************************/
int GetTime(int argc, char *argv[])
{
	int i, error = 0;
	char *str;
	unsigned int hour, min, sec;
	
	RtcGetTime(&SysTime);
	Uart_Printf("Current time is %2.2x:%2.2x:%2.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)
		{
			Uart_Printf("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)
		{
			Uart_Printf("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)
		{
			Uart_Printf("second error!\n");
			return error;
		}
		
		SysTime.hour = hour;
		SysTime.min  = min;
		SysTime.sec = sec;		
		RtcSetTime(&SysTime);
		
		Uart_Printf("Set time %x:%x:%x\n", hour, min, sec);								
	}	
	
	return error;
}


/*******************************************************************************************************/
int ChgSysMclk(int argc, char *argv[])
{
	int i, mdiv, pdiv, sdiv;
	char tmp[4];
	
	Uart_Printf("This function is for adjust system running clock!!!\n");
	Uart_Printf("Current clock is %dHz\n", MCLK);
	Uart_Printf("Please enter the PLL parameter to use, mdiv[0-255], pdiv[0-63], sdiv[0-3]\n");
	
	Uart_Printf("mdiv: ");	
	if((mdiv=strtodec(tmp, GetParameter(tmp, 3)))<0)		
	{
		Uart_Printf("\nget mdiv Error!\n");
		return 0;
	}					
	
	Uart_Printf("\npdiv: ");
	if((pdiv=strtodec(tmp, GetParameter(tmp, 3)))<0)		
	{
		Uart_Printf("\nget pdiv Error!\n");
		return 0;
	}	
	
	Uart_Printf("\nsdiv: ");
	
	if((sdiv=strtodec(tmp, GetParameter(tmp, 3)))<0)		
	{
		Uart_Printf("\nget sdiv Error!\n");
		return 0;
	}		
	
	Uart_Printf("\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);
	Uart_Printf("Current clock is %dHz\n", MCLK);	
	MCLK_M = mdiv; MCLK_P = pdiv; MCLK_S = sdiv;
	
	return 0;
}


/*******************************************************************************************************/
int ChgBaudRate(int argc, char *argv[])
{
	int i;
	unsigned int BaudSet [6] = {4800, 9600, 19200, 38400, 57600, 115200};
	
	for(i=0; i<6; i++)
		Uart_Printf("%d : %d\n", i, BaudSet[i]);
		
	Uart_Printf("Please enter choice : ");
	
	while(1)
	{
		int ch = Uart_Getch();
		
		if(ch<='6'||ch>='0')
		{
			SERIAL_BAUD = BaudSet[ch-'0'];
			Uart_Printf("Baud rate set to : %d\nPress any key to continue\n", SERIAL_BAUD);
			i = 0x4000;
			while(i--);		//wait transmit end
			Uart_Init(0,SERIAL_BAUD);
			Uart_Getch();
			break;
		}
			
	}	

	return 0;
}


/*******************************************************************************************************/
int SetIpAddr(int argc, char *argv[])
{
	int i, j, err = 0;
	char *str;
	char ch = '.';
	
	if(argc<2)
	{
		Uart_Printf("IP address : %u.%u.%u.%u\n", (IP_ADDRESS>>24)&0xff, (IP_ADDRESS>>16)&0xff, (IP_ADDRESS>>8)&0xff, IP_ADDRESS&0xff);
		return 0;
	}
	
	str = argv[1];
	
	for(j=0; j<4; j++)
	{
		if(j==3)
			ch = 0;
		i = 0;
		if(str[++i]!=ch)
			if(str[++i]!=ch)
				if(str[++i]!=ch)
					err = 1;
		str[i] = 0;
		ip[j] = strtodec(str, i);
		if(ip[j]<0||ip[j]>254||err)
		{
			Uart_Printf("IP address error\n");
			return -1;
		}			
		str += i+1;
	}
	
	Uart_Printf("Set IP address : %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);					
	IP_ADDRESS = (ip[0]<<24)|(ip[1]<<16)|(ip[2]<<8)|ip[3];
	locnode.ip = IP_ADDRESS;
	ArpInit();
	return 0;	
}


int LoadFile2Mem(int argc, char *argv[])
{
	unsigned char key;

	if(argc<2)
		download_addr = DFT_DOWNLOAD_ADDR;
	else
		download_addr = strtoul((unsigned char*)argv[1]);

	ResetNic();
	Uart_Printf("Now begin address for download, use address 0x%x\n", download_addr);
	Uart_Printf("Load image file from host\n");
	Uart_Printf("Type tftp -i %d.%d.%d.%d put filename at the host PC\n", (IP_ADDRESS>>24)&0xff, (IP_ADDRESS>>16)&0xff, (IP_ADDRESS>>8)&0xff, IP_ADDRESS&0xff);
	Uart_Printf("Press ESC key to exit\n");	

	download_begin = 1;
	download_end = 0;
	
	while (1) 
	{
		NetSever();
		if ((key=Uart_GetKey())==ESC_KEY)
		{
			Uart_SendByte('\n');
			download_begin = 0;
			return 0;
		}
		if(download_end)
		{
			download_begin = 0;
			download_end = 0;
			return 0;
		}
	}
}
//*****************************************************************************

/******************************************************************************
【功能说明】将目标代码通过网口下载到SDRAM的默认下载地址,然后运行下载的目标代码
******************************************************************************/
int Net_Load_Run(int argc, char *argv[])
{
	unsigned char key;
	void (*fp)(void) = (void (*)(void))DFT_DOWNLOAD_ADDR;

	download_addr = DFT_DOWNLOAD_ADDR;

	ResetNic();
	Uart_Printf("Now begin address for download, use address 0x%x\n", download_addr);
	Uart_Printf("Load image file from host\n");
	Uart_Printf("Type tftp -i %d.%d.%d.%d put filename at the host PC\n", (IP_ADDRESS>>24)&0xff, (IP_ADDRESS>>16)&0xff, (IP_ADDRESS>>8)&0xff, IP_ADDRESS&0xff);
	Uart_Printf("Press ESC key to exit\n\n");	

	download_begin = 1;
	download_end = 0;
	
	while (1) 
	{
		NetSever();
		if ((key=Uart_GetKey())==ESC_KEY)
		{
			Uart_SendByte('\n');
			download_begin = 0;
			return 0;
		}
		if(download_end)
		{
			download_begin = 0;
			download_end = 0;
			goto NEXT_RUN_CODES ;
		}
	}

NEXT_RUN_CODES:
	Uart_Printf( "Now run the received codes from address 0x%x!\n\n", DFT_DOWNLOAD_ADDR );
	(*fp)();
	
	return 0;
}


int LoadFromUart(int argc, char *argv[])
{
	int start, time_out;
	int load_addr = DFT_DOWNLOAD_ADDR;
		
	if(argc>1)
	{
		start = strtoul((unsigned char*)argv[1]);
		if(load_addr>0)
			load_addr = start;						
	}
	start = load_addr;
	
	time_out = WAIT_TIME;	
	do{
		if(rUTRSTAT0&0x1)
		{
			*((unsigned char *)load_addr++) = Uart_GetKey();
			time_out = 90000;
		}
	}while(--time_out);
	
	Uart_Printf("Received 0x%x bytes from serial port.\n", load_addr-start);
	
	return 0;
}
//*****************************************************************************

/******************************************************************************
【功能说明】将目标代码通过串口下载到SDRAM,然后运行下载的目标代码
******************************************************************************/
int Uart_Load_Run(int argc, char *argv[])
{
	int start, time_out;
	int load_addr = DFT_DOWNLOAD_ADDR ;
	void (*fp)(void) = (void (*)(void))DFT_DOWNLOAD_ADDR;

	Uart_Printf( "\n\nReceive codes from UART and place to addrress: 0x%x!\n", DFT_DOWNLOAD_ADDR );
	Uart_Printf( "Please send object codes [*.bin] from Uart :\n" );
	
	time_out = WAIT_TIME;	
	do{
		if(rUTRSTAT0&0x1)
		{
			*((unsigned char *)load_addr++) = Uart_GetKey();
			time_out = 90000;
		}
	}while(--time_out);
	
	Uart_Printf( "\n\nReceived 0x%x Bytes codes from UART!\n", load_addr-start );
	Uart_Printf( "Now run the received codes from address 0x%x!\n\n", DFT_DOWNLOAD_ADDR );
	(*fp)();
	
	return 0;
}
//*****************************************************************************

int RunProgram(int argc, char *argv[])
{
	unsigned char key;

⌨️ 快捷键说明

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