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

📄 main.c

📁 W3100是WIZnet公司专门为以太网互联和嵌入式设备推出的硬件TCP/IP协议栈芯片
💻 C
字号:
/*
********************************************************************************
* Wiznet.
* 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
* Seoul, Korea
*
* (c) Copyright 2002, Wiznet, Seoul, Korea
*
* Filename : main.c
* Programmer(s): 
* Created : 2002/01/
* Modified :
* Description : Using ping function, let's understand IP_RAW Mode channel
********************************************************************************
*/

/*
###############################################################################
Include Part
###############################################################################
*/
#include <reg51.h>			// Definition file for 8051 SFR
#include "serial.h" 			// serial related functions
#include "socket.h" 			// W3100A driver file
#include "lcd.h"			// lcd fucntion
#include "at24c02.h"			// EEPROM
#include "sockutil.h"			// Useful function of W3100A
#include "netconf.h"			// Network Configuration
#include "util.h"			// Useful function
#include "ping.h"                       // Ping Fucntion

/*
###############################################################################
Define Part
###############################################################################
*/
#define	MAX_BUF_SIZE	2048		// Maximum receive buffer size


/*
###############################################################################
Local  Variable Declaration Part
###############################################################################
*/
sfr  CKCON      =   0x8F;		// CKCON Reg. Define

/*
###############################################################################
Function Prototype Declaration Part
###############################################################################
*/
void Init8051(); 		
void InitNetConfig(void);	
void PingUsage();

/*
###############################################################################
Function Implementation Part
###############################################################################
*/
/*
********************************************************************************
*              Main function to run ping function
*
* Description: After initialization of 8051 and W3100A, wait for 'ping' command from serial terminal.
*     	       And do ping function with ping arguments from command.
*     	       When it is finished, display the result of pinging
* Arguments  : None.
* Returns    : None.
* Note       : 
********************************************************************************
*/
void main()
{
	PINGLOG xdata PingLog;			// Variable for result of pinging
	char xdata command[81];                 // Variable for command from serial terminal
	char xdata Token[20];                   // String token
	char* NextTok;				// Pointer to next token string
	char xdata PeerIp[16];                  // Peer IP Address String
	char IsValidPingArgs;                   // If ping arguments are correct then IsValidPingArgs = 0, else -1
	char IsExistPeerIP;			// If Peer IP Address exist among ping's arguments then IsExistPeerIP = 0, else -1
	int pingcnt;				// Count of sending ping-request to a peer
	int pingsize;                           // Size of ping-request's data
	int pingtimeout;                        // wait time for ping-reply

	Init8051();		// Initialize 8051
        InitLcd();		// LCD Init
	PutStringLn("\r\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
	PutString  (" Ping Program 1.0 - Created Date : ");PutLTOA(0x20021028); PutStringLn("");
	PutStringLn("                  - Created By   : WIZnet, Inc.");
	PutStringLn("                  - W3100A Driver: V2.1");
	PutStringLn("                  - ping API     : V1.0");
	PutStringLn("                  - Flatform     : 8051 EVB V3.0");
	PutStringLn("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\r\n");

	initW3100A();		// Initialize W3100A
	InitNetConfig();        // Setup network information

	/* Wait for command, and call ping function. Then display the result of pinging */
	while(1)
	{
		/* Initialize local variables */
		pingcnt = pingsize = pingtimeout = 0;	
		IsValidPingArgs = 0;
		IsExistPeerIP = 0;
		PutString("\r\nWIZnet>");				// Display prompt 'WIZnet>'
		GetString(command);                  			// Wait for command from serial Terminal
		if(*command=='\0') continue;				// Is command empty?
		ReplaceAll(command,'\t',' ');				// replace all of white space character '\t' with ' '
		StrTrim(command);   			                // Trim white space character at end of command
		NextTok = StrTok(command,' ',Token);    		// Get first token from command
		MakeUpper(Token);					// Converts first token to an uppercase string.

		if(CmpStr(Token,"EXIT")==0)	break;			// If first token is equal to "EXIT" then end program
		else if( CmpStr(Token,"PING") !=0 )     		// If first token is not equal to "PING" then enter command again
		{
			PutStringLn("Unknown Command");
			continue;
		}
		if(NextTok == 0)                        		// If second token is empty then display usage of ping
		{
			PingUsage();
			continue;
		}
		while( NextTok != 0 )                   		// Else	parse arguments
		{
			NextTok = StrTok(NextTok,' ',Token);		// Get next token
			if( CmpStr(Token,"-t") == 0 )	pingcnt = -1;	// If next token is '-t' option 
			else if(CmpStr(Token,"-n")==0)                  // If next token is '-n' option
			{
				NextTok = StrTok(NextTok,' ',Token);	// get value of '-n' option
				if(ValidATOI(Token,10,&pingcnt)==-1)    // If value of '-n' option is valid
				{
					PutStringLn("Bad value for option -n, valid range is from 1 to 32767.");
					IsValidPingArgs = -1;
					break;
				}
			}
			else if(CmpStr(Token,"-l")==0)			// If next token is '-l' option
			{
				NextTok = StrTok(NextTok,' ',Token);    // Get value of '-l' option
				if(ValidATOI(Token,10,&pingsize)==-1)	// If value of '-l' option is valid
				{
					PutStringLn("Bad value for option -l, valid range is from 1 to 1452.");
					IsValidPingArgs = -1;
					break;
				}
			}
			else if(CmpStr(Token,"-w")==0)			// If next token is '-w' option
			{
				NextTok = StrTok(NextTok,' ',Token);	// Get value of '-w' option
				if(ValidATOI(Token,10,&pingtimeout)==-1)// If value of '-w' option is valid
				{
					PutStringLn("Bad value for option -w, valid range is from 1 to 32767.");
					IsValidPingArgs = -1;
					break;
				}
			}
			else if(CmpStr(Token,"-?")==0 || CmpStr(Token,"/?")==0)	// If next token is '-?' or '/?' then display usage of ping
			{
				IsValidPingArgs = -1;
				PingUsage();
				break;
			}
			else if(FindFirst(Token,'.')!=0)			// If '.' character exist at next token
			{
				if(VerifyIPAddress(Token)== -1)			// Get the peer IP address string
				{
					IsValidPingArgs = -1;
					PutStringLn("IP address must be specified correctly.");
					break;
				}
				IsExistPeerIP = 1;
				MemCopy(PeerIp,Token);
			}                                                       // If next token is bad option
			else if(FindFirst(Token,'-') !=0)
			{
				PutStringLn("Bad option");
				IsValidPingArgs = -1;
				PingUsage();
				break;
			}                                                        
			else							// Unknown option
			{
				PutStringLn("Unknown option");
				IsValidPingArgs = -1;
				PingUsage();
				break;
			}
		}
		if(IsValidPingArgs==0 && IsExistPeerIP)				// If command is valid
		{
			if(ping(pingcnt,pingsize,pingtimeout,PeerIp,&PingLog)==-1)	// call ping function
				PutStringLn("Fail to ping()");
			else
			{
				DisplayPingStatistics(PingLog);                    	// Display the result of pinging
			}
		}
	}
	PutStringLn("Program ended. Bye~");
	while(1);
}

/*
********************************************************************************
*              8051 Initialization Function
*
* Description: 
* Arguments  : None.
* Returns    : None.
* Note       : 
********************************************************************************
*/
void Init8051(void)
{ 
	EA = 0; 		// Disable all interrupts 
	CKCON = 0x01;		// X2 Mode
	IT0 = 0;		// interrupt level trigger
	EX0 = 1;		// INT 0 enable
	EX1 = 0; 		// INT 1 disable
	EA = 1;	       		// Enable all interrupts
	InitSerial();  		// Initialize serial port (Refer to serial.c)
	wait_10ms(1);
}

/*
********************************************************************************
* Description: Setup network ( Source IP, G/W, S/N, MAC Address ) information
* Arguments  : None.
* Returns    : None.
* Note       : 
********************************************************************************
*/
void InitNetConfig(void)
{
	int i, j;
	char c;
	u_char xdata ip[6];  	       		// Variable for setting up network information
	u_char xdata ipstr[16];
	un_l2cval tip;
	if(!Check_EEPROM())
	{
		ip[0] = 0x00; ip[1] = 0x08; ip[2] = 0xDC; ip[3] = 0x00; ip[4] = 0x00; ip[5] = 0x00;     // ETC.
		setMACAddr(ip);		// Setup MAC
		ip[0] = 192; ip[1] = 168; ip[2] = 0; ip[3] = 2;		// VPN Env.						
		setIP(ip);		// Setup source IP
		ip[3] = 1;		// VPN , Develope Env.
		setgateway(ip);		// Setup gateway address
		ip[0] = 255; ip[1] = 255; ip[2] = 255; ip[3] = 0;
		setsubmask(ip);		// Setup subnet mask
	}
	else
	{
		ClrScr();
		GotoXY(0,0);
		Puts(" < NET CONFIG > ");
		T0 = 0;
		T1 = 1;

		EEP_ReadBytes(GIPADDR,tip.cVal,4);         // If IP address wasn't set up in EEPROM, setup initial network configuration.
		if(tip.lVal == 0 || tip.lVal == (0-1))     // If Gateway Address is 00.00.00.00 or ff.ff.ff.ff
		{       			
			PutString("Setting Initial Network Configuration ...");PutStringLn("");PutStringLn(""); 
			ip[0] = 0x00; ip[1] = 0x08; ip[2] = 0xDC; ip[3] = 0x00; ip[4] = 0x00; ip[5] = 0x00;     // etc.
			setMACAddr(ip);		// Setup MAC
			ip[0] = 192; ip[1] = 168; ip[2] = 0; ip[3] = 2;		// VPN Env.						
			setIP(ip);		// Setup source IP
			ip[3] = 1;		// VPN , Develope Env.
			setgateway(ip);		// Setup gateway address
			ip[0] = 255; ip[1] = 255; ip[2] = 255; ip[3] = 0;
			setsubmask(ip);
			All_Config_Save();
		}

		PutString("Press 'C' Key To Update Network Configuration");
		GotoXY(0,1);
		for(i = 0; i < 16; i++)
		{
			for( j = 0 ; j < 50 ; j++)
			{
				if(IsPressedKey() == 1)
				{
					c = GetByte();PutStringLn("");
					if(c == 'C' || c == 'c')
					{
						Configure();
						c = '*';
						break;
					}
					else if(c== 0x1B)		// Escape Character
					{
						c = '*';
						break;
					}
				}
				wait_1ms(2);
			}
			if(c == '*') break;
			T0 = !T0;				// LED TOGGLE
			T1 = !T1;
			Putch(0xFF);
			PutByte('.');
		}
		PutStringLn("");
		Config_Load();
	}
	GetNetConfig();				// Display network information
	settimeout("\x03\xE8\x03");
	PutString("Timeout is Modified : ");
	PutStringLn("Value = 100ms , Count = 3, Timeout = 700ms");
	sysinit(0x55,0x55);
	ClrScr();
	GotoXY(0,0);
	Puts("<  PING Apps.  >");
	GotoXY(1,1);
	GetIPAddress(ip);
	inet_ntoa(ip,ipstr);			// Output IP Address on LCD
	Puts(ipstr);
	T0 = 0;							// LED0 ON 
	T1 = 0;							// LED1 ON 
}


/*
********************************************************************************
* Description: Display usage of ping
* Arguments  : None.
* Returns    : None.
* Note       : 
********************************************************************************
*/
void PingUsage()
{
	PutStringLn("\r\nUsage : ping [-t] [-n count] [-l size] [-w timeout] peer-ip-address");
	PutStringLn("\r\nOption : ");
	PutStringLn("    -t            Ping the specified host until stopped.");
	PutStringLn("                  To see statistics and continue - type Control-Break;");
	PutStringLn("                  To stop - type Control-C.");
	PutStringLn("    -n count      Number of echo requests to send.");
	PutStringLn("    -l size       Send buffer size.");
	PutStringLn("    -w timeout    Timeout in milliseconds to wait for each reply.\r\n");
}


⌨️ 快捷键说明

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