📄 main.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 : Date - 2002/10/28
* Description - All file revision
* - Version up W3100A driver (V2.0->V2.2)
* Description : Loopback program by using 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 "sockutil.h" // W3100A Useful funtion
/*
###############################################################################
Define Part
###############################################################################
*/
#define MAX_BUF_SIZE 2048 // Maximum receive buffer size
/*
###############################################################################
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);
/*
###############################################################################
Function Implementation Part
###############################################################################
*/
/*
********************************************************************************
* Main function to run loopback function
*
* Description: After initialization of 8051 and W3100A, 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
Init8051(); // Initialize 8051
InitLcd(); // LCD Init
// printf("LoopBack Program.(TCP Server mode)\r\n");
PutStringLn("\r\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
PutString (" LoopBack Program.(TCP Server mode) - Created Date : ");PutLTOA(0x20021105); PutStringLn("");
PutStringLn(" - Created By : WIZnet, Inc.");
PutStringLn(" - W3100A Driver: V2.2(INDIRECT)");
PutStringLn(" - Flatform : 8051 EVB V3.0");
PutStringLn("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\r\n");
initW3100A(); // Initialize W3100A
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("");
/* When the socket is half-close and have some data to be received, receive the data. */
if ((len = select(i, SEL_RECV)) > 0) // Check to receive data when SOCK_CLOSE_WAIT
{
if (len > MAX_BUF_SIZE) // When receiving larger data than system buffer,it's processed buffer sized data first.
len = MAX_BUF_SIZE;
len = recv(i, data_buf, len);
PutString("Size of Received data in half-close : 0x");PutITOA(len);PutStringLn("");
}
close(i); // Close the appropriate channel connected to client
break;
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("");
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);
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(" < TCP SERVER > ");
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 + -