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

📄 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 : Loopback program by using DHCP Client & TCP server mode for EVB8051
********************************************************************************
*/

/*
###############################################################################
Include Part
###############################################################################
*/
#include <at89x51.h>			// Definition file for 8051 SFR
//#include <stdio.h>			// to use printf
#include "serial.h" 			// serial related functions
#include "socket.h" 			// W3100A driver file
#include "lcd.h"			// lcd fucntion
#include "at24c02.h"			// EEPROM
#include "netconf.h"			// Network Configuration
#include "dhcp.h"			// DHCP related functions
#include "sockutil.h"                   // Useful function of W3100A

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

#define USER_SPECIFIC_MAC_VALUE		"\x00\x08\xDC\x00\x00\x00"	// You Must be Modified by suitable and unique value


/*
###############################################################################
Local  Variable Declaration Part
###############################################################################
*/
sfr  CKCON      =   0x8F;		// CKCON Reg. Define
u_char xdata * data_buf = 0x7000;	// Position of receive buffer

/*
###############################################################################
Function Prototype Declaration Part
###############################################################################
*/
void Init8051(); 		
void init_sock(u_char i);	
void InitNetConfig(void);
void DHCP_IP_Display(void);

/*
###############################################################################
Function Implementation Part
###############################################################################
*/
/*
********************************************************************************
*              Main function to run loopback function
*
* Description: After initialization of 8051 and W3100A, wait for IP address assignment from dhcp server.
	       After IP address is occupied, then wait for connection request from a peer.
*              And do loopback function which sends back received data.
*              When closing the channel, re-initialize the channel and wait for connection request.
* Arguments  : None.
* Returns    : None.
* Note       : 
********************************************************************************
*/
void main()
{
	SOCKET i;		//Variable to handle each socket
	int len;		// Variable to store received data size
	char k,j;

	Init8051();		// Initialize 8051
        InitLcd();		// LCD Init

	PutStringLn("\r\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
	PutString  (" DHCP Client & LoopBack Program.(TCP Server mode) - Created Date : ");PutLTOA(0x20021028); PutStringLn("");
	PutStringLn("                                                  - Created By   : WIZnet, Inc.");
	PutStringLn("                                                  - W3100A Driver: V2.2");
	PutStringLn("                                                  - DHCP API     : V1.5");
	PutStringLn("                                                  - Flatform     : 8051 EVB V3.0");
	PutStringLn("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\r\n");

	initW3100A();		// Initialize W3100A

	if(!Check_EEPROM())
		setMACAddr(USER_SPECIFIC_MAC_VALUE);		// If saved Mac address value in eeprom, set unique default value to MAC address.
	else
	{
		EEP_ReadBytes(MADDR,SRC_HA_PTR,6);		// Save Mac address value in eeprom to W3100A's MAC address register
		for(k = 0; k < 6; k++)
		      if(*(SRC_HA_PTR+k) != 0)	break;				
		for(j = 0; j < 6; j++)
		      if(*(SRC_HA_PTR+j) != 0)	break;					
		if(k == 6 || j == 6)	setMACAddr(USER_SPECIFIC_MAC_VALUE);
	}

	if(1 == DHCP_SetIP())	       				// Before you call DHCP_SetIP() function, W3100A's MAC Address Regiter value must be setted.
	{
		Config_Save('A');
		DHCP_IP_Display();
	}
	else
	{
		PutStringLn("<<<< DHCP FAILED .. USED DEFAULT IP >>>>");
		InitNetConfig();        // Setup network information		
	}

	
	for (i = 0; i < MAX_SOCK_NUM; i++) init_sock(i);				// Initialize channel for Loop Back Service and wait in server mode
	
	while(1)									// Loop Back Service
	{
		for( i = 0 ; i < MAX_SOCK_NUM ; i++ )					// Do loopback service to 4 channels of W3100A in order
		{
			switch(select(i,SEL_CONTROL))
			{
			case SOCK_ESTABLISHED:						// If client is connected 
				if ((len = select(i, SEL_RECV)) > 0) 			// Confirm if received the data
				{
					if (len > MAX_BUF_SIZE) len = MAX_BUF_SIZE;	// Handle as much as buffer size first when receiving data more than system buffer
					len = recv(i, data_buf, len);			// Loopback received data from client 
					send(i, data_buf, len);
				}
				break;
			case SOCK_CLOSE_WAIT:                           		// If the client request to close connection and wait for closing successfully
				PutString("CLOSE_WAIT : ");PutHTOA(i);PutStringLn("");					
				close(i);	       					// Close the appropriate channel connected to client
			case SOCK_CLOSED:                                               // If the socket connected to client already has been closed
				PutString("CLOSED : ");PutHTOA(i);PutStringLn("");	
				init_sock(i);						// To do loopback service again in closed channel, wait in initialization and server mode
				break;
			}
		}
	}		
}

/*
********************************************************************************
*              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);
}

/*
********************************************************************************
*              Re-initialization function for the disconnected channel.
*
* Description: Wait in server mode after re-initialization for the disconnected channel.
* Arguments  : None.
* Returns    : None.
* Note       : 
********************************************************************************
*/
void init_sock(u_char i) 
{
//	printf("socket : ");
	PutString("socket : ");	
	socket(i, SOCK_STREAM, 5000, 0);	// socket creation  
//	printf("%bd ok\r\n",i);
	PutHTOA(i);PutStringLn("");

//	printf("listen : ");
	PutString("listen : ");
	NBlisten(i);           			// Server Mode
//	printf("%bd ok..\r\n", i);
	PutHTOA(i);PutStringLn("");
}

/*
********************************************************************************
* 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(""); 
			setMACAddr(USER_SPECIFIC_MAC_VALUE);			// 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);
			Config_Save('A');
		}

		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
	sysinit(0x55,0x55);
	ClrScr();
	GotoXY(0,0);
	Puts("DHCP Client");
	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 DHCP IP address on LCD
* Arguments  : None.
* Returns    : None.
* Note       : 
********************************************************************************
*/
void DHCP_IP_Display(void)
{
       	u_char xdata ip[6];  	       		// Variable to setup network information
	u_char xdata ipstr[16];
	
	GetNetConfig();				// Display network information
	ClrScr();
	GotoXY(0,0);
	Puts("DHCP Client");
	GotoXY(1,1);
	GetIPAddress(ip);
	inet_ntoa(ip,ipstr);			// Output IP Address on LCD
	Puts(ipstr);
	T0 = 0;							// LED0 ON 
	T1 = 0;							// LED1 ON 
}

⌨️ 快捷键说明

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