?? socket.c
字號:
#include<C8051F020.H>
#include"socket.h"
//******************************W3100驅(qū)動程序API******************************
//******************************局部變量******************************
u_char data 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
void Int0(void) interrupt 0
{
u_char data status;
EX0 = 0; // INT0 DISABLE
status = INT_REG;
while (status) {
if (status & 0x01) { // channel 0 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[0] = INT_STATUS(0);
// if (I_STATUS[0] & SESTABLISHED) ISR_ESTABLISHED(0);
// if (I_STATUS[0] & SCLOSED) ISR_CLOSED(0);
INT_REG = 0x01;
}
if (status & 0x02) { // channel 1 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[1] = INT_STATUS(1);
// if (I_STATUS[1] & SESTABLISHED) ISR_ESTABLISHED(1);
// if (I_STATUS[1] & SCLOSED) ISR_CLOSED(1);
INT_REG = 0x02;
}
if (status & 0x04) { // channel 2 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[2] = INT_STATUS(2);
// if (I_STATUS[2] & SESTABLISHED) ISR_ESTABLISHED(2);
// if (I_STATUS[2] & SCLOSED) ISR_CLOSED(2);
INT_REG = 0x04;
}
if (status & 0x08) { // channel 3 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
I_STATUS[3] = INT_STATUS(3);
// if (I_STATUS[3] & SESTABLISHED) ISR_ESTABLISHED(3);
// if (I_STATUS[3] & SCLOSED) ISR_CLOSED(3);
INT_REG = 0x08;
}
if (status & 0x10) { // channel 0 receive interrupt
// ISR_RX(0);
INT_REG = 0x10;
}
if (status & 0x20) { // channel 1 receive interrupt
// ISR_RX(1);
INT_REG = 0x20;
}
if (status & 0x40) { // channel 2 receive interrupt
// ISR_RX(2);
INT_REG = 0x40;
}
if (status & 0x80) { // channel 3 receive interrupt
// ISR_RX(3);
INT_REG = 0x80;
}
status = INT_REG;
}
INT_REG = 0xFF;
EX0 = 1;
}
/*
void ISR_ESTABLISHED(SOCKET s)
{
;
}
*/
/*
void ISR_CLOSED(SOCKET s)
{
;
}
*/
/*
void ISR_RX(SOCKET s)
{
u_int xdata * port;
u_char xdata * w3100;
recvfrom(s,testdata,14,w3100,port);
// if(*testdata=='t')//'h','i','s',' ','i','s',' ','a','t','e','s','t'
// LED1 = !LED1;
display_eng(12,48,testdata,14);
}
*/
/*
void ISR_RX(SOCKET s)
{
;
}
*/
void initW3100A(void)
{
#ifdef DEBUG
// printf("initW3100A()\r\n");
PutStringLn("initW3100A()");
#endif
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)
COMMAND(0) = CSW_RESET; // Software RESET
}
void sysinit(u_char sbufsize, u_char rbufsize)
{
char i;
int ssum,rsum;
ssum = 0;
rsum = 0;
TX_DMEM_SIZE = sbufsize; // Set Tx memory size for each channel
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;
COMMAND(0) = CSYS_INIT;
while(!(I_STATUS[0] & SSYS_INIT_OK));
}
void setsubmask(u_char * addr)
{
u_char i;
for (i = 0; i < 4; i++) {
*(SUBNET_MASK_PTR + i) = addr[i];
}
}
void setgateway(u_char * addr)
{
u_char i;
for (i = 0; i < 4; i++) {
*(GATEWAY_PTR + i) = addr[i];
}
}
void setIP(u_char * addr)
{
u_char i;
for (i = 0; i < 4; i++) {
*(SRC_IP_PTR + i) = addr[i];
}
}
void setMACAddr(u_char * addr)
{
u_char i;
for (i = 0; i < 6; i++) {
*(SRC_HA_PTR + i) = addr[i];
}
}
#ifdef __IP_RAW__
void setIPprotocol(SOCKET s, u_char ipprotocol)
{
IP_PROTOCOL(s) = ipprotocol;
}
#endif
#ifdef __OPT__
void settimeout(u_char * val)
{
u_char i;
for (i = 0; i < 3; i++) {
*(TIMEOUT_PTR + i) = val[i];
}
}
void setINTMask(u_char mask)
{
INTMASK = mask;
}
void setTOS(SOCKET s, u_char tos)
{
TOS(s) = tos;
}
#endif
char socket(SOCKET s, u_char protocol, u_int port, u_char flag)
{
u_char k;
OPT_PROTOCOL(s) = protocol | flag; // Designate socket protocol and option
if (port != 0) // setup designated port number
{
k = (u_char)((port & 0xff00) >> 8);
*(SRC_PORT_PTR(s)) = k;
k = (u_char)(port & 0x00ff);
*(SRC_PORT_PTR(s) + 1) = k;
}
else // Designate random port number which is managed by local when you didn't designate source port
{
Local_Port++;
*SRC_PORT_PTR(s) = (u_char)((Local_Port & 0xff00) >> 8);
*(SRC_PORT_PTR(s) + 1) = (u_char)(Local_Port & 0x00ff);
}
I_STATUS[s] = 0;
COMMAND(s) = CSOCK_INIT; // SOCK_INIT
while (I_STATUS[s] == 0); // Waiting Interrupt to CSOCK_INIT
if (!(I_STATUS[s] & SSOCK_INIT_OK)) return (-1); // Error
initseqnum(s); // Use initial seq# with random number
return (s);
}
#ifdef __TCP_CLIENT__
char connect(SOCKET s, u_char * addr, u_int port)
{
if (port != 0)
{ // designate destination port
*DST_PORT_PTR(s) = (u_char)((port & 0xff00) >> 8);
*(DST_PORT_PTR(s) + 1) = (u_char)(port & 0x00ff);
}
else return (-1);
*DST_IP_PTR(s) = addr[0]; // designate destination IP address
*(DST_IP_PTR(s) + 1) = addr[1];
*(DST_IP_PTR(s) + 2) = addr[2];
*(DST_IP_PTR(s) + 3) = addr[3];
I_STATUS[s] = 0;
COMMAND(s) = CCONNECT; // CONNECT
while (I_STATUS[s] == 0) // Wait until connection is established successfully
if (select(s, SEL_CONTROL) == SOCK_CLOSED) return -1; // When failed, appropriate channel will be closed and return an error
if (!(I_STATUS[s] & SESTABLISHED)) return (-1); // Error
return (1);
}
#endif
#ifdef __TCP_SERVER__
/*
char listen(SOCKET s, u_char * addr, u_int * port)
{
u_int i;
I_STATUS[s] = 0;
COMMAND(s) = CLISTEN; // LISTEN
while (I_STATUS[s] == 0) // Wait until connection is established
if (select(s, SEL_CONTROL) == SOCK_CLOSED) return -1; // When failed to connect, the designated channel will be closed and return an error.
if (I_STATUS[s] & SESTABLISHED) // Receive IP address and port number of the peer connected
{
i = *DST_PORT_PTR(s);
*port = (u_int)((i & 0xff00) >> 8);
i = *(DST_PORT_PTR(s) + 1);
i = (u_int)(i & 0x00ff);
*port += (i << 8);
addr[0] = *DST_IP_PTR(s);
addr[1] = *(DST_IP_PTR(s) + 1);
addr[2] = *(DST_IP_PTR(s) + 2);
addr[3] = *(DST_IP_PTR(s) + 3);
}
else return (-1); // Error
return (1);
}
*/
void NBlisten(SOCKET s)
{
I_STATUS[s] = 0;
COMMAND(s) = CLISTEN; // LISTEN
}
#endif
void initseqnum(SOCKET s)
{
int i;
i = s;
SEQ_NUM.lVal++; // Designate initial seq#
// If you have random number generation function, assign random number instead of SEQ_NUM.lVal++.
*TX_WR_PTR(s) = SEQ_NUM.cVal[0];
*(TX_WR_PTR(s) + 1) = SEQ_NUM.cVal[1];
*(TX_WR_PTR(s) + 2) = SEQ_NUM.cVal[2];
*(TX_WR_PTR(s) + 3) = SEQ_NUM.cVal[3];
wait_1us(2); // Wait until TX_WR_PRT has been written safely. ( Must have delay(1.6us) if next action is to write 4byte-pointer register )
*TX_RD_PTR(s) = SEQ_NUM.cVal[0];
*(TX_RD_PTR(s) + 1) = SEQ_NUM.cVal[1];
*(TX_RD_PTR(s) + 2) = SEQ_NUM.cVal[2];
*(TX_RD_PTR(s) + 3) = SEQ_NUM.cVal[3];
wait_1us(2); // Wait until TX_RD_PRT has been written safely.
*TX_ACK_PTR(s) = SEQ_NUM.cVal[0];
*(TX_ACK_PTR(s) + 1) = SEQ_NUM.cVal[1];
*(TX_ACK_PTR(s) + 2) = SEQ_NUM.cVal[2];
*(TX_ACK_PTR(s) + 3) = SEQ_NUM.cVal[3];
}
#ifdef __TCP__
int send(SOCKET s, const u_char xdata * buf, u_int len)
{
int ptr, size;
if (len <= 0) return (0);
else
{
ptr = 0;
size = send_in(s, buf + ptr, len);
if (size == -1) return -1; // Error
len = len - size;
ptr += size;
} while ( len > 0);
}
return ptr;
}
int send_in(SOCKET s, const u_char xdata * buf, u_int len)
{
u_char k;
int size;
un_l2cval wr_ptr, ack_ptr;
u_char * send_ptr;
S_START:
EX0 = 0;
k = *SHADOW_TXWR_PTR(s); // Must read the shadow register for reading 4byte pointer registers
wait_1us(2); // wait for reading 4byte pointer registers safely
wr_ptr.cVal[0] = *TX_WR_PTR(s);
wr_ptr.cVal[1] = *(TX_WR_PTR(s) + 1);
wr_ptr.cVal[2] = *(TX_WR_PTR(s) + 2);
wr_ptr.cVal[3] = *(TX_WR_PTR(s) + 3);
k = *SHADOW_TXACK_PTR(s); // Must read the shadow register for reading 4byte pointer registers
wait_1us(2); // wait for reading 4byte pointer registers safely
ack_ptr.cVal[0] = *TX_ACK_PTR(s);
ack_ptr.cVal[1] = *(TX_ACK_PTR(s) + 1);
ack_ptr.cVal[2] = *(TX_ACK_PTR(s) + 2);
ack_ptr.cVal[3] = *(TX_ACK_PTR(s) + 3);
EX0 = 1;
// Calculate send free buffer size
if (wr_ptr.lVal >= ack_ptr.lVal) size = SSIZE[s] - (wr_ptr.lVal - ack_ptr.lVal);
else size = SSIZE[s] - (0 - ack_ptr.lVal + wr_ptr.lVal);
if (size > SSIZE[s]) // Recalulate after some delay because of error in pointer caluation
{
if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1; // Error
wait_1ms(1);
#ifdef DEBUG
// printf("1 ");
// printf("%.8lx ", wr_ptr.lVal);
// printf("%.8lx\r\n", ack_ptr.lVal);
PutString("send_in() at S_START : ");PutHTOA(s); PutString(" : "); PutLTOA(wr_ptr.lVal) ; PutString(" : ");PutLTOA(ack_ptr.lVal) ;PutStringLn("");
#endif
goto S_START;
}
if (size == 0) // Wait when previous sending has not finished yet and there's no free buffer
{
if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1; // Error
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -