📄 main.c
字号:
/*
********************************************************************************
* Wiznet.
* 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
* Seoul, Korea
*
* (c) Copyright 2002, Wiznet, Seoul, Korea
*
* Filename : main.c
* Created : 2002/01/
* Modified : Date - 2002/10/28
* Description - All source file revision
* Version of W3100A driver up. (V2.0-->V2.2)
*
* Description : Loopback program using TCP client mode for EVB8051.
********************************************************************************
*/
/*
###############################################################################
Include Part
###############################################################################
*/
#include <reg51.h> // 8051 SFR definition file
//#include <stdio.h> // To use printf function
#include "serial.h" // Functions referring to serial
#include "socket.h" // W3100A driver file
#include "type.h" // Type definition file
#include "util.h" // File defined useful function
#include "lcd.h" // LCD fucntion
#include "at24c02.h" // EEPROM
#include "netconf.h" // Network Configuration
#include "sockutil.h"
/*
###############################################################################
Define Part
###############################################################################
*/
#define MAX_BUF_SIZE 2048 // Max size received_buffer
/*
###############################################################################
Local Variable Definition Part
###############################################################################
*/
sfr CKCON = 0x8F; // CKCON Reg. Define
u_char xdata * data_buf = 0x7000; // Location of received_buffer
UCHAR Inc = 0; // for Client Port Increment
/*
###############################################################################
Function Prototype Definition Part
###############################################################################
*/
void Init8051(); // Initialization of 8051 MCU
void InitNetConfig();
/*
###############################################################################
Function Implementation Part
###############################################################################
*/
/*
********************************************************************************
* main function to Loopback
*
* Description: After initialization of 8051MCU and W3100A and try to establish connection with peer.
* Loopback which resend data received is executed when it's connected.
* Arguments : None.
* Returns : None.
* Note :
********************************************************************************
*/
void main()
{
int len;
u_char state;
u_char ip[6];
SOCKET s;
Init8051();
PutStringLn("\r\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
PutString (" LoopBack Program.(TCP Client 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");
InitLcd();
initW3100A();
InitNetConfig();
START:
s = 0;
// printf("socket "); // socket initialization
PutString("socket "); // socket initialization
socket(s, SOCK_STREAM, 7000+Inc++, 0); // Client Port (7000 ~ 7255)
// printf("%bd", s); printf(" ok..\r\n");
PutHTOA(s);PutStringLn(" ok..\r\n");
PutString("Local Port : 0x");PutITOA(7000+Inc);PutStringLn("");
PutString("Enter Host IP Address(Dotted Decimal Format) : ");
if( GetDotNotationAddr(ip,10,4)== -1) // Read server's address to connect by serial
{
PutString("\r\nCanceled or Invalid Address : continue?(y/n)");
while(1)
{
state = GetByte();
if( ((state - 'Y') % 0x20) == 0)
{
PutByte(state);PutStringLn("");
goto START;
}
else if( ((state - 'N') % 0x20) == 0 )
{
PutByte(state);PutStringLn("");
goto PRG_END;
}
}
}
// printf("connect ");
PutString("connect ");
if (connect(s, ip, 3000) == -1) { // Establish connection with peer
state = select(0, SEL_CONTROL);
// printf("connect fail : state : %.2bx\r\n", state);
PutString("connect fail : state : ");PutHTOA(state);PutStringLn("");
wait_10ms(100);
goto START;
}
// printf("%bd ok...\r\n", s);
PutHTOA(s); PutStringLn(" ok...\r\n");
while (1)
{
state = select(s, SEL_CONTROL);
switch(state)
{
case SOCK_ESTABLISHED: // status established connection
if ((len = select(s, SEL_RECV)) > 0) // Check to receive data
{
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(s, data_buf, len);
send(s, data_buf, len);
}
break;
case SOCK_CLOSE_WAIT: // Close of connection
// printf("SOCK_CLOSE_WAIT : %bd\r\n", s);
PutString("SOCK_CLOSE_WAIT : ");PutHTOA(s);PutStringLn("");
/* When the socket is half-close and have some data to be received, receive the data. */
if ((len = select(s, 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(s, data_buf, len);
PutString("Size of Received data in half-close : 0x");PutITOA(len);PutStringLn("");
}
close(s);
break;
case SOCK_CLOSED: // Release of connection
// printf("closed : %bd\r\n", s);
PutString("closed : ");PutHTOA(s);PutStringLn("");
goto START;
break;
}
} // while
PRG_END:
PutStringLn("Program ended");
while(1);
}
/*
********************************************************************************
* Function to process 8051 Initialization
*
* Description:
* Arguments : None.
* Returns : None.
* Note :
********************************************************************************
*/
void Init8051(void)
{
EA = 0; // All Interrupt disable
CKCON = 0x01; // X2 Mode
IT0 = 0; // interrupt level trigger
EX0 = 1; // INT 0 enable
EX1 = 0; // INT 1 disable
EA = 1; // Enable Entire interrupt
InitSerial(); // Initiate Serial Port(Refer serial.c file)
wait_10ms(1);
}
/*
********************************************************************************
* Description: Set 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 to set Network information
u_char xdata ipstr[16];
if(!Check_EEPROM())
{
ip[0] = 0x00; ip[1] = 0x08; ip[2] = 0xDC; ip[3] = 0x00; ip[4] = 0x00; ip[5] = 0x00; // ETC.
setMACAddr(ip); // MAC setting
ip[0] = 192; ip[1] = 168; ip[2] = 0; ip[3] = 2; // VPN Env.
setIP(ip); // source IP setting
ip[3] = 1; // VPN , Develope Env.
setgateway(ip); // gateway address setting
ip[0] = 255; ip[1] = 255; ip[2] = 255; ip[3] = 0;
setsubmask(ip); // subnet mask setting
}
else
{
ClrScr();
GotoXY(0,0);
Puts(" < NET CONFIG > ");
T0 = 0;
T1 = 1;
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(); // Network information Display
sysinit(0x55,0x55);
ClrScr();
GotoXY(0,0);
Puts(" < TCP CLIENT > ");
GotoXY(1,1);
GetIPAddress(ip);
inet_ntoa(ip,ipstr); // Put string IP Address LCD
Puts(ipstr);
T0 = 0; // LED0 ON
T1 = 0; // LED1 ON
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -