📄 main.c
字号:
/*
###############################################################################
Wiznet.
5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
Seoul, Korea
(c) Copyright 2002, Wiznet, Seoul, Korea
File Name : MAIN.C
Version : 1.1
Programmer(s) : Woo Youl Kim
Created : 2002/03/23
Modify History : Date - 2002/10/28
Description - W3100A Driver Version UP (V2.0->V2.1)
- Remove goto statements and label
- All file revision
Description : Example program by using UDP Channel
This program is for exchanging data between EVB8051 and AX1 program on PC by using UDP.
AX1 send data to UDP channel #3000 opened by EVB8051
EVB8051 do loopback received data from AX1 to UDP channel #3000 opened by AX1
Data are exchanged in this Ping-Pong way.
Notice : This program is not supported multi-destination.
###############################################################################
*/
/*
###############################################################################
Include Part
###############################################################################
*/
#include <reg51.h> // Definition of 8051 Registers
#include "serial.h" // Definition for 8051EVB Serial related
#include "socket.h" // Definition for i2chip W3100A related
#include "lcd.h" // LCD fucntion
#include "at24c02.h" // EEPROM
#include "netconf.h" // Network Configuration
#include "sockutil.h" // Useful function of W3100A
/*
###############################################################################
Function Prototype Declaration Part
###############################################################################
*/
void Init8051();
void InitNetConfig();
/*
###############################################################################
Local Variable Declaration Part
###############################################################################
*/
sfr CKCON = 0x8F; // CKCON Reg. Define
u_char xdata* pRecvBuf = 0x006000; // Data Buffer Pointer
/*
###############################################################################
Function Implementation Part
###############################################################################
*/
/*
Description : UDP Ping-Pong Test Program
Argument :
Return Value :
Note :
UDP is connectionless protocol unlike TCP and cannot use listen(), connect(), settimeout() functions.
UDP just send data to designated port number by using sento(), recvfrom().
*/
void main()
{
u_char xdata addr[4]; // Variable to be used in network configuration and recvfrom()
u_long xdata SpecifiedAddr;
u_char xdata addrstr[16];
u_int port; // Variable to be used to store Destination Port in recvfrom()
u_int RecvSize; // Variable to be used to store received data size
SOCKET s; // Variable to be used to designate channel of W3100A
char IsFirstRecv = 0; // Variable to be used to have been received data alreay. when it is set, Data is alreay received.
Init8051(); // 8051 MCU Initialize
InitLcd(); // LCD Initialize
PutStringLn("\r\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
PutString (" UDP Ping-Pong Test Apps.(One Peer) - Created Date : ");PutLTOA(0x20021028); PutStringLn("");
PutStringLn(" - Created By : WIZnet, Inc.");
PutStringLn(" - W3100A Driver: V2.2");
PutStringLn(" - Flatform : 8051 EVB V3.0");
PutStringLn("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\r\n");
initW3100A(); // Initialization of W3100A
InitNetConfig(); // Setup network information
s = 0; // Use channel #0 of W3100A
socket(s,SOCK_DGRAM,3000,0); // Open UDP channel
while(1) // Ping-Pong Service Routine
{
RecvSize = select(s,SEL_RECV); // When received data from port #3000, read the size and send it to peer's port #3000
if(RecvSize > 0)
{
RecvSize = recvfrom(s,pRecvBuf,RecvSize,addr,&port);
if(-1 == RecvSize) // Read data as size as buffer received
{
PutStringLn("\r\n<<<< Receive Fail >>>>");
break;
}
if(!IsFirstRecv)
{
SpecifiedAddr = *((u_long*)addr);
IsFirstRecv = 1;
}
inet_ntoa(addr,addrstr);
if(IsFirstRecv && SpecifiedAddr == *((u_long*)addr)) // it can receive data from another peer, but it can't send received data to the peer.
{
PutString(addrstr);PutString(" : Received Size = ");PutITOA(RecvSize);
RecvSize = sendto(s,pRecvBuf,RecvSize,addr,port);
if(RecvSize == -1) // Loopback received data to destination : Ping-Pong
{
PutStringLn("\r\n<<<< Send Fail >>>>");
break;
}
PutString(", Sended Size = ");PutITOA(RecvSize);PutStringLn("");
}
else // if the board received data from another peer
{
PutString("Unknown peer send 'PING' to the board : ");PutString(addrstr);PutStringLn("");
}
}
}
close(s);
PutStringLn("End Program");
while(1);
}
/*
Description : Initialize 8051, Serial and Interrupt.
Argument :
Return Value :
Note :
*/
void Init8051(void)
{
EA = 0; // Disable all Interrupt
CKCON = 0x01; // X2 Mode
IT0 = 0; // Level Trigger
EX0 = 1; // INT 0 Enable
EX1 = 0; // INT 1 Disable
EA = 1; // Enable to all Interrupt
InitSerial(); // Initialize serial port (Refer to serial.c)
wait_10ms(1);
}
/*
Description : Initialize Network Information to be used by W3100
Argument :
Return Value :
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(" < UDP 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
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -