📄 socket.c
字号:
/*
********************************************************************************
* Wiznet.
* 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
* Seoul, Korea
*
* (c) Copyright 2002, Wiznet, Seoul, Korea
*
* Filename : socket.c
* Programmer(s) : Wooyoul Kim
* Version : 2.2
* Created : 2002/04/10
* Modified : Date - 2002/11/04
* Description - Convert Driect W3100A driver into Indriect.
* Description : Driver API program of W3100A (Big Endian for Keil Compiler)
********************************************************************************
*/
/*
###############################################################################
File Include Section
###############################################################################
*/
#include <at89x51.h> // 8051 SFR definition file
//#include <stdio.h>
#include "serial.h" // serial related functions
#include "socket.h" // W3100A driver file
/*
###############################################################################
Define Part
###############################################################################
*/
//#define DEBUG // Add debugging code
/*
###############################################################################
Local Variable Declaration Section
###############################################################################
*/
u_char xdata I_STATUS[4]; // Store Interrupt Status according to channels
u_int xdata Local_Port; // Designate Local Port
un_l2cval xdata SEQ_NUM; // Set initial sequence number
u_long xdata SMASK[MAX_SOCK_NUM]; // Variable to store MASK of Tx in each channel, on setting dynamic memory size.
u_long xdata RMASK[MAX_SOCK_NUM]; // Variable to store MASK of Rx in each channel, on setting dynamic memory size.
int xdata SSIZE[MAX_SOCK_NUM]; // Maximun Tx memory size by each channel
int xdata RSIZE[MAX_SOCK_NUM]; // Maximun Rx memory size by each channel
u_char xdata* SBUFBASEADDRESS[MAX_SOCK_NUM]; // Maximun Tx memory base address by each channel
u_char xdata* RBUFBASEADDRESS[MAX_SOCK_NUM]; // Maximun Rx memory base address by each channel
/*
###############################################################################
Function Implementation Section
###############################################################################
*/
/*
********************************************************************************
* Interrupt handling function of the W3100A
*
* Description :
* Stores the status information that each function waits for in the global variable S_STATUS for transfer.
* S_STATUS stores the interrupt status value for each channel.
* Arguments : None
* Returns : None
* Note : Internal Function
********************************************************************************
*/
void Int0(void) interrupt 0
{
u_char status;
EX0 = 0; // INT0 DISABLE
status = IndirectReadByte(INT_REG);
while (status) {
if (status & 0x01) { // channel 0 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[0] = IndirectReadByte(INT_STATUS(0));
// if (I_STATUS[0] & SESTABLISHED) ISR_ESTABLISHED(0);
// if (I_STATUS[0] & SCLOSED) ISR_CLOSED(0);
IndirectWriteByte(INT_REG,0x01);
}
if (status & 0x02) { // channel 1 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[1] = IndirectReadByte(INT_STATUS(1));
// if (I_STATUS[1] & SESTABLISHED) ISR_ESTABLISHED(1);
// if (I_STATUS[1] & SCLOSED) ISR_CLOSED(1);
IndirectWriteByte(INT_REG,0x02);
}
if (status & 0x04) { // channel 2 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[2] = IndirectReadByte(INT_STATUS(2));
// if (I_STATUS[2] & SESTABLISHED) ISR_ESTABLISHED(2);
// if (I_STATUS[2] & SCLOSED) ISR_CLOSED(2);
IndirectWriteByte(INT_REG,0x04);
}
if (status & 0x08) { // channel 3 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[3] = IndirectReadByte(INT_STATUS(3));
// if (I_STATUS[3] & SESTABLISHED) ISR_ESTABLISHED(3);
// if (I_STATUS[3] & SCLOSED) ISR_CLOSED(3);
IndirectWriteByte(INT_REG,0x08);
}
if (status & 0x10) { // channel 0 receive interrupt
// ISR_RX(0);
IndirectWriteByte(INT_REG,0x10);
}
if (status & 0x20) { // channel 1 receive interrupt
// ISR_RX(1);
IndirectWriteByte(INT_REG,0x20);
}
if (status & 0x40) { // channel 2 receive interrupt
// ISR_RX(2);
IndirectWriteByte(INT_REG,0x40);
}
if (status & 0x80) { // channel 3 receive interrupt
// ISR_RX(3);
IndirectWriteByte(INT_REG,0x80);
}
status = IndirectReadByte(INT_REG);
}
IndirectWriteByte(INT_REG,0xFF);
EX0 = 1;
}
/*
********************************************************************************
* Established connection interrupt handling function.
*
* Description :
* Called upon connection establishment, and may be inserted in user code if needed by the programmer.
* Arguments : None
* Returns : None
* Note : Internal Function
********************************************************************************
*/
/*
void ISR_ESTABLISHED(SOCKET s)
{
// TO ADD YOUR CODE
}
*/
/*
********************************************************************************
* Closed connection interrupt handling function
*
* Description :
* Called upon connection closure, and may be inserted in user code if needed by the programmer.
* Arguments : None
* Returns : None
* Note : Internal Function
********************************************************************************
*/
/*
void ISR_CLOSED(SOCKET s)
{
// TO ADD YOUR CODE
}
*/
/*
********************************************************************************
* Received data interrupt handling function
*
* Description :
* Called upon receiving data, and may be inserted in user code if needed by the programmer.
* Arguments : None
* Returns : None
* Note : Internal Function
********************************************************************************
*/
/*
void ISR_RX(SOCKET s)
{
// TO ADD YOUR CODE
}
*/
/*
********************************************************************************
* W3100A initialization function
*
* Description :
* Function for S/W resetting of the W3100A.
* Sets the initial SEQ# to be used for TCP communication.
* Arguments : None
* Returns : None
* Note : API Function
********************************************************************************
*/
void initW3100A(void)
{
#ifdef DEBUG
// printf("initW3100A()\r\n");
PutStringLn("initW3100A()");
#endif
IDM_OR = 0x80;
Local_Port = 1000; // This default value will be set if you didn't designate it when you create a socket
// If you don't designate port number and create a socket continuously,
// the port number will be assigned with incremented by one to Local_Port
SEQ_NUM.lVal = 0x12233445; // Sets the initial SEQ# to be used for TCP communication. (It should be ramdom value)
IndirectWriteByte(COMMAND(0),CSW_RESET); // Software RESET
}
/*
********************************************************************************
* W3100A initialization function
*
* Description :
* Sets the Tx, Rx memory size by each channel, source MAC, source IP, gateway, and subnet mask
* to be used by the W3100A to the designated values.
* May be called when reflecting modified network information or Tx, Rx memory size on the W3100A
* Include Ping Request for ARP update (In case that a device embedding W3100A is directly connected to Router)
* Arguments : sbufsize - Tx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte)
* bit 1-0 : Tx memory size of channel #0
* bit 3-2 : Tx memory size of channel #1
* bit 5-4 : Tx memory size of channel #2
* bit 7-6 : Tx memory size of channel #3
* rbufsize - Rx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte)
* bit 1-0 : Rx memory size of channel #0
* bit 3-2 : Rx memory size of channel #1
* bit 5-4 : Rx memory size of channel #2
* bit 7-6 : Rx memory size of channel #3
* Returns : None
* Note : API Function
* Maximum memory size for Tx, Rx in W3100A is 8KBytes,
* In the range of 8KBytes, the memory size could be allocated dynamically by each channel
* Be attentive to sum of memory size shouldn't exceed 8Kbytes
* and to data transmission and receiption from non-allocated channel may cause some problems.
* If 8KBytes memory already is assigned to centain channel, other 3 channels couldn't be used, for there's no available memory.
* If two 4KBytes memory are assigned to two each channels, other 2 channels couldn't be used, for there's no available memory.
* (Example of memory assignment)
* sbufsize => 00000011, rbufsize => 00000011 : Assign 8KBytes for Tx and Rx to channel #0, Cannot use channel #1,#2,#3
* sbufsize => 00001010, rbufsize => 00001010 : Assign 4KBytes for Tx and Rx to each channel #0,#1 respectively. Cannot use channel #2,#3
* sbufsize => 01010101, rbufsize => 01010101 : Assign 2KBytes for Tx and Rx to each all channels respectively.
* sbufsize => 00010110, rbufsize => 01010101 : Assign 4KBytes for Tx, 2KBytes for Rx to channel #0
* 2KBytes for Tx, 2KBytes for Rx to channel #1
* 2KBytes for Tx, 2KBytes for Rx to channel #2
* 2KBytes is available exclusively for Rx in channel #3. There's no memory for Tx.
********************************************************************************
*/
void sysinit(u_char sbufsize, u_char rbufsize)
{
char i;
int ssum,rsum;
ssum = 0;
rsum = 0;
IndirectWriteByte(TX_DMEM_SIZE,sbufsize); // Set Tx memory size for each channel
IndirectWriteByte(RX_DMEM_SIZE,rbufsize); // Set Rx memory size for each channel
SBUFBASEADDRESS[0] = SEND_DATA_BUF; // Set Base Address of Tx memory for channel #0
RBUFBASEADDRESS[0] = RECV_DATA_BUF; // Set Base Address of Rx memory for channel #0
#ifdef DEBUG
PutStringLn("Channel : SEND MEM SIZE : RECV MEM SIZE");
#endif
for(i = 0 ; i < MAX_SOCK_NUM; i++) // Set maximum memory size for Tx and Rx, mask, base address of memory by each channel
{
SSIZE[i] = 0;
RSIZE[i] = 0;
if(ssum < 8192)
{
switch((sbufsize >> i*2) & 0x03) // Set maximum Tx memory size
{
case 0:
SSIZE[i] = 1024;
SMASK[i] = 0x000003FF;
break;
case 1:
SSIZE[i] = 2048;
SMASK[i] = 0x000007FF;
break;
case 2:
SSIZE[i] = 4096;
SMASK[i] = 0x00000FFF;
break;
case 3:
SSIZE[i] = 8192;
SMASK[i] = 0x00001FFF;
break;
}
}
if( rsum < 8192)
{
switch((rbufsize>> i*2) & 0x03) // Set maximum Rx memory size
{
case 0:
RSIZE[i] = 1024;
RMASK[i] = 0x000003FF;
break;
case 1:
RSIZE[i] = 2048;
RMASK[i] = 0x000007FF;
break;
case 2:
RSIZE[i] = 4096;
RMASK[i] = 0x00000FFF;
break;
case 3:
RSIZE[i] = 8192;
RMASK[i] = 0x00001FFF;
break;
}
}
ssum += SSIZE[i];
rsum += RSIZE[i];
if(i != 0) // Set base address of Tx and Rx memory for channel #1,#2,#3
{
SBUFBASEADDRESS[i] = SBUFBASEADDRESS[i-1] + SSIZE[i-1];
RBUFBASEADDRESS[i] = RBUFBASEADDRESS[i-1] + RSIZE[i-1];
}
#ifdef DEBUG
PutHTOA(i); PutString(" : 0x");PutITOA(SSIZE[i]); PutString(" : 0x");PutITOA(RSIZE[i]);PutStringLn("");
#endif
}
I_STATUS[0] = 0;
IndirectWriteByte(COMMAND(0),CSYS_INIT);
while(!(I_STATUS[0] & SSYS_INIT_OK));
}
/*
********************************************************************************
* Subnet mask setup function
*
* Description : Subnet mask setup function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -