📄 socket.c
字号:
/*
********************************************************************************
* TERN, Inc.
* (c) Copyright 2005, http://www.tern.com
*
* MODIFIED BY RICHARD BARRY TO ADD SEMAPHORE FOR COMMUNICATION BETWEEN THE
* WIZnet ISR AND THE HTTP TASK.
*
* - Derived based on development version provided by Wiznet.
*
* Filename : socket.h
* Programmer(s):
* Created : 2002/06/20
* Modified :
* 2002/09/27 : - Renaming
* INT_STATUS --> INT_REG
* STATUS(i) --> INT_STATUS(i)
* C_STATUS(i) --> SOCK_STATUS(i)
* 2003/11/06 : Ported for use with TERN controller. Note all byte access is at even addresses
* 2005/10/8 : Modified constants for easier initialization.
*
* Description : Header file of W3100A for TERN embedded controller
********************************************************************************
*/
/*
###############################################################################
File Include Section
###############################################################################
*/
#include "i2chip_hw.h"
#include "socket.h"
#include "types.h"
#include <string.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <semphr.h>
#include <portasm.h>
/*
###############################################################################
Local Variable Declaration Section
###############################################################################
*/
u_char I_STATUS[4]; // Store Interrupt Status according to channels
u_int Local_Port; // Designate Local Port
union un_l2cval SEQ_NUM; // Set initial sequence number
u_long SMASK[MAX_SOCK_NUM]; // Variable to store MASK of Tx in each channel,
// on setting dynamic memory size.
u_long RMASK[MAX_SOCK_NUM]; // Variable to store MASK of Rx in each channel,
// on setting dynamic memory size.
int SSIZE[MAX_SOCK_NUM]; // Maximun Tx memory size by each channel
int RSIZE[MAX_SOCK_NUM]; // Maximun Rx memory size by each channel
u_int SBUFBASEADDRESS[MAX_SOCK_NUM]; // Maximun Tx memory base address by each channel
u_int 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 I_STATUS
* for transfer. I_STATUS stores the interrupt status value for each channel.
* Arguments : None
* Returns : None
* Note : Internal Function
********************************************************************************
*/
portBASE_TYPE prvProcessISR( void )
{
unsigned char status;
extern xSemaphoreHandle xTCPSemaphore;
portBASE_TYPE xSwitchRequired = pdFALSE;
#ifdef I2CHIP_WINDOW
u_int current_window = i2chip_get_window();
#endif
status = READ_VALUE(INT_REG);
if (status)
{
xSwitchRequired = pdTRUE;
// channel 0 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
if (status & 0x01)
{
I_STATUS[0] = READ_VALUE(INT_STATUS(0));
// if (I_STATUS[0] & SESTABLISHED)
// ISR_ESTABLISHED(0);
// if (I_STATUS[0] & SCLOSED)
// ISR_CLOSED(0);
WRITE_VALUE(INT_REG, 0x01);
}
// channel 1 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
if (status & 0x02)
{
I_STATUS[1] = READ_VALUE(INT_STATUS(1));
// if (I_STATUS[1] & SESTABLISHED)
// ISR_ESTABLISHED(1);
// if (I_STATUS[1] & SCLOSED)
// ISR_CLOSED(1);
WRITE_VALUE(INT_REG, 0x02);
}
// channel 2 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
if (status & 0x04)
{
I_STATUS[2] = READ_VALUE(INT_STATUS(2));
// if (I_STATUS[2] & SESTABLISHED)
// ISR_ESTABLISHED(2);
// if (I_STATUS[2] & SCLOSED)
// ISR_CLOSED(2);
WRITE_VALUE(INT_REG, 0x04);
}
// channel 3 interrupt(sysinit, sockinit, established, closed, timeout, send_ok, recv_ok)
if (status & 0x08)
{
I_STATUS[3] = READ_VALUE(INT_STATUS(3));
// if (I_STATUS[3] & SESTABLISHED) ISR_ESTABLISHED(3);
// if (I_STATUS[3] & SCLOSED) ISR_CLOSED(3);
WRITE_VALUE(INT_REG, 0x08);
}
// channel 0 receive interrupt
if (status & 0x10)
{
// ISR_RX(0);
WRITE_VALUE(INT_REG, 0x10);
}
// channel 1 receive interrupt
if (status & 0x20)
{
// ISR_RX(1);
WRITE_VALUE(INT_REG, 0x20);
}
// channel 2 receive interrupt
if (status & 0x40)
{
// ISR_RX(2);
WRITE_VALUE(INT_REG, 0x40);
}
// channel 3 receive interrupt
if (status & 0x80)
{
// ISR_RX(3);
WRITE_VALUE(INT_REG, 0x80);
}
status = READ_VALUE(INT_REG);
}
WRITE_VALUE(INT_REG, 0xFF);
#ifdef I2CHIP_WINDOW
i2chip_set_window(current_window);
#endif
if( xSwitchRequired == pdTRUE )
{
xSwitchRequired = xSemaphoreGiveFromISR( xTCPSemaphore, pdFALSE );
}
return xSwitchRequired;
}
void far interrupt in4_isr_i2chip(void)
{
if( prvProcessISR() == pdTRUE )
{
portEND_SWITCHING_ISR();
}
INT_EOI;
}
/*
****************************************************************************************************
* 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: Reset of W3100A S/W and Registeration of i386 interrupt
* Arguments : None.
* Returns : None.
* Note :
****************************************************************************************************
*/
void initW3100A(void)
{
// Install interrupt handler for i2Chip
INT_INIT(in4_isr_i2chip);
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 = 4294967293ul; // Sets the initial SEQ# to be used for TCP communication.
// (It should be ramdom value)
WRITE_VALUE(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
* s 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;
// Set Tx memory size for each channel
WRITE_VALUE(TX_DMEM_SIZE, sbufsize);
// Set Rx memory size for each channel
WRITE_VALUE(RX_DMEM_SIZE, rbufsize);
// Set Base Address of Tx memory for channel #0
SBUFBASEADDRESS[0] = 0;
// Set Base Address of Rx memory for channel #0
RBUFBASEADDRESS[0] = 0;
// Set maximum memory size for Tx and Rx, mask, base address of memory by each channel
for(i = 0 ; i < MAX_SOCK_NUM; i++)
{
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];
// Set base address of Tx and Rx memory for channel #1,#2,#3
if(i != 0)
{
SBUFBASEADDRESS[i] = ssum - SSIZE[i];
RBUFBASEADDRESS[i] = rsum - RSIZE[i];
}
}
WRITE_VALUE(COMMAND(0), CSYS_INIT);
while(!(I_STATUS[0] & SSYS_INIT_OK))
I2CHIP_POLL_ISR(in4_isr_i2chip);
#ifdef __PING__
{
u_char xdata pingbuf[8];
setIPprotocol(0, IPPROTO_ICMP);
socket(0, SOCK_IPL_RAW, 3000,0); // Create a socket for ARP update
pingbuf[0] = 8; // ICMP TYPE
pingbuf[1] = 0; // ICMP CODE
pingbuf[2] = 0xf7; // CHECKSUM (already calculated)
pingbuf[3] = 0xfd;
pingbuf[4] = 0; // ID
pingbuf[5] = 1;
pingbuf[6] = 0; // SEQ #
pingbuf[7] = 1;
pingbuf[8] = 0; // Data 1 Byte
sendto(0, pingbuf, 9, GATEWAY_PTR,3000); // Ping Request
close(0);
printf("Route MAC Update Success");
}
#endif
}
/*
****************************************************************************************************
* Function to set subnet mask
*
* Description:
* Arguments : addr--> Pointer that has the value to be set
* Returns : None.
* Note :
****************************************************************************************************
*/
void setsubmask(u_char * addr)
{
u_char i;
u_char far* sm_ptr = SUBNET_MASK_PTR; // We can only convert to 'regular'
// pointer if we're confident arithmetic
// won't take us out of current window.
for (i = 0; i < 4; i++)
{
WRITE_VALUE(sm_ptr + SA_OFFSET(i), addr[i]);
}
}
/*
****************************************************************************************************
* Function to set gateway IP
*
* Description:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -