W5500 TCP功能-服务器功能

#W5500 TCP功能
By setting some register and memory operation, W5500 provides internet connectivity. This chapter describes how it can be operated.
通过设置一些寄存器和存储器操作,W5500提供英特网连接,这章描述如何操作它

##基本设置
对于W5500操作,选择并使用下面显示的适当寄存器。
1,Mode Register (MR) 模式寄存器
2,Interrupt Mask Register (IMR), 中断屏蔽寄存器
3,Retry Time-value Register (RTR), 重试定时值寄存器
4,Retry Count Register (RCR) 重试次数寄存器
对于这些寄存器的更详细的说明参考“寄存器说明”一节

##设置网络信息
Basic network information setting for communication: It must be set the basic network information.,为实现通讯,必须设置基本网络信息

###1,SHAR(Source Hardware Address Register) 原始硬件地址寄存器
It is prescribed that the source hardware addresses, which is set by SHAR, use unique hardware addresses (Ethernet MAC address) in the Ethernet MAC layer. The IEEE manages the MAC address allocation. The manufacturer which produces the network device allocates the MAC address to product.
规定由shar设置的源硬件地址在以太网MAC层中使用唯一的硬件地址(以太网MAC地址)。IEEE管理MAC地址分配。生产网络设备的制造商将MAC地址分配给产品。
Details on MAC address allocation refer to the website as below.
关于MAC地址分配的详情参考下面的web页
http://www.ieee.org/
http://standards.ieee.org/regauth/oui/index.shtml
###2,GAR(Gateway Address Register) 网关地址寄存器
###3,SUBR(Subnet Mask Register) 子网掩码寄存器
###4,SIPR(Source IP Address Register) 源IP地址寄存器

##Set socket memory information 设置套接字存储信息
此阶段设置套接字Tx/Rx内存信息。每个套接字的基址和掩码地址都是固定的,并保存在这个阶段。

##数据通讯
After the initialization process, W5500 can transmit and receive the data with others by ‘open’ the SOCKET of TCP, UDP, IPRAW, and MACRAW mode. The W5500 supports the independently and simultaneously usable 8 SOCKETS. In this section, the communication method for each mode will be introduced.
初始化过程完成后,W5500可以通过“打开”TCP、UDP、IPraw和Macraw模式的套接字,与其他方一起发送和接收数据。W5500支持独立且同时可用的8个套接字。本节将介绍每种模式的通信方法。
###1,TCP模式
The TCP is a connection-oriented protocol. The TCP make the connection SOCKET by using its own IP address, port number and destination IP address, port number. Then transmits and receives the data by using this SOCKET. Methods of making the connection to SOCKET are “TCP SERVER” and “TCP CLIENT”. It is divided by transmitting the connect-request (SYN packet). The “TCP SERVER” listens to the connect-request from the “TCP CLIENT”, and makes connection SOCKET by accepting the transmitted connect-request (Passive-open). The “TCP CLIENT” transmits the connect-request first to “TCP SERVER” to make the connection (Active-open).
TCP是一种面向连接的协议,TCP通过使用它自己的IP地址,端口号和目的IP地址端口号建立连接套接字,接着通过套接字发送和接收数据。连接套接字的方法有“TCP服务器”和“TCP客户端”。它被发送连接请求(SYN包)划分。“TCP服务器”监听来自“TCP客户端”的连接请求,并通过接受发送的连接请求(被动打开)来建立连接套接字。“TCP客户端”首先将连接请求发送到“TCP服务器”以进行连接(活动打开)。
服务器和客户端的连接区分

###2,TCP服务器
服务器建立连接流程图

SOCKET Initialization 套接字初始化

SOCKET initialization is required for TCP data communication. The initialization is opening the SOCKET. The SOCKET opening process selects one SOCKET from 8 SOCKETS of the W5500, and sets the protocol mode (Sn_MR) and Sn_PORT0 which is source port number (Listen port number in “TCP SERVER”) in the selected SOCKET, and then executes OPEN command. After the OPEN command, if the status of Sn_SR is changed to SOCK_INIT, the SOCKET initialization process is completed. The SOCKET initialization process is identically applied in “TCP SERVER” and “TCP CLIENT.”The Initialization process of Socket n in TCP mode is shown below.
TCP数据通信需要套接字初始化。初始化打开套接字。套接字打开过程从W5500的8个套接字中选择一个,并在所选套接字中设置协议模式(sn_mr)和sn_port0,即源端口号(侦听“tcp server”中的端口号),然后执行open命令。打开命令后,如果sn_sr的状态更改为sock_init,则套接字初始化过程完成。socket初始化过程在“tcp server”和“tcp client”中应用相同,在tcp模式下,socket n的初始化过程如下。

{
START:
Sn_MR = 0x01; // sets TCP mode
Sn_PORT0 = source_port; // sets source port number
Sn_CR = OPEN; // sets OPEN command
/* wait until Sn_SR is changed to SOCK_INIT */
if (Sn_SR != SOCK_INIT) Sn_CR = CLOSE; goto START;
}

侦听

通过侦听命令运行作为服务器
{
/* listen SOCKET /
Sn_CR = LISTEN;
/
wait until Sn_SR is changed to SOCK_LISTEN */
if (Sn_SR != SOCK_LISTEN) Sn_CR = CLOSE; goto START;
}

ESTABLISHMENT 建立

When the status of Sn_SR is SOCK_LISTEN, if it receives a SYN packet, the status of Sn_SR is changed to SOCK_SYNRECV and transmits the SYN/ACK packet. After that, the Socket n makes a connection. After it makes the connection of Socket n , it enables the data communication. There are two methods to confirm the connection of Socket n .
当sn_sr的状态为sock_listen时,如果它接收到一个syn包,sn_sr的状态变为sock_synrecv并发送syn/ack包。之后,套接字n进行连接。当它连接了套接字n后,就可以进行数据通信。有两种方法可以确认套接字N的连接。
First method : 第一个方法
{
if (Sn_IR(CON) == ‘1’)
/* When an interrupt occurs and the mask bit of Sn_IMR is ‘1’, the interrupt bit of Sn_IR
becomes ‘1’ /
Sn_IR(CON) = ‘1’;
/In order to clear the Sn_IR bit, the host should write the bit as ‘1’. When all the bits of
Sn_IR is cleared (‘0’), IR(n) is automatically cleared.
/
goto ESTABLISHED stage;
/
In this case, if the interrupt of Socket n is activated, interrupt occurs. Refer to IR, IMR
Sn_IMR and Sn_IR. */
}
Second method :
{
if (Sn_SR == SOCK_ESTABLISHED) goto ESTABLISHED stage;
}

ESTABLISHMENT : Check received data 建立:检查接收的数据

Confirm the reception of the TCP data. 确认接收到的TCP数据
First method :
{
if (Sn_IR(RECV) == ‘1’) Sn_IR(RECV) = ‘1’; goto Receiving Process stage;
/* In this case, if the interrupt of Socket n is activated, interrupt occurs. Refer to IR, IMR
Sn_IMR and Sn_IR. */
}
Second Method :
{
if (Sn_RX_RSR0 != 0x0000) goto Receiving Process stage;
}
The First method: set the Sn_IR(RECV) to ‘1’ whenever you receive a DATA packet. If the host receives the next DATA packet without setting the Sn_IR(RECV) as ‘1’ in the prior DATA packet, it cannot recognize the Sn_IR(RECV) of the next DATA packet. This is due to the prior Sn_IR(RECV) and next Sn_IR(RECV) being overlapped. So this method is not recommended if the host cannot perfectly process the DATA packets of each Sn_IR(RECV).
第一个方法:无论何时收到数据包设置Sn_IR(RECV)为1。如果主机没有在前一个数据包中设置Sn_IR(RECV)为1,又收到下一个数据包,则无法识别下一个数据包的Sn_IR(RECV)。前一个Sn_IR(RECV)和下一个Sn_IR(RECV)期间重叠了。因此不建议使用这种方法,如果主机不能完美的处理每一个Sn_IR(RECV)数据包

ESTABLISHMENT : Receiving process 建立:接收过程

In this process, it processes the TCP data which was received in the Internal RX memory. At the TCP mode, the W5500 cannot receive the data if the size of received data is larger than the RX memory free size of Socket n . If the prior stated condition is happened, the W5500 holds on to the connection (pauses), and waits until the RX memory’s free size is larger than the size of the received data.
在这个过程中,它处理接收到内部RX存储器中的TCP数据。在TCP模式下,如果接收数据的大小大于套接字n的RX内存可用大小,则W5500无法接收数据。如果发生上述情况,W5500将保持连接(暂停),并等待直到RX内存的可用大小大于接收数据的大小。
{
/* first, get the received size /
len = Sn_RX_RSR; // len is received size
/
select RX memory, refer to Datasheet 14 page /
cntl_byte = Socket_n_RX_Buffer
/
Get offset address /
src_ptr = Sn_RX_RD;
/
set memory copy len bytes of source_ptr to destination_address */
for (i=0; i<len; i++)
{
(dst_ptr+i) = W5500_READ(addr, cntl_byte, src_ptr+i);
}
/
increase Sn_RX_RD as length of len /
Sn_RX_RD += len;
/
set RECV command */
Sn_CR = RECV;
}

ESTABLISHMENT: Check send data / Send process
建立:检查发送数据/发送过程

The size of the transmit data cannot be larger than assigned internal TX memory of Socket n. If the size of transmit data is larger than configured MSS, it is divided by size of MSS and transmits. To transmit the next data, user must check the completion of prior SEND command. An error may occur if the SEND command executes before completion of prior SEND command. The larger the data size, the more time to complete the SEND command. So the user should properly divide the data to transmit.
To check the completion of the SEND command, it should be check that the send data length is equal with the actual sent data length. The actual sent data length is calculated by the difference of the Sn_TX_RD before and after performing the SEND command. If the actual sent data is less than the send data length, the SEND command is retried for sending the left data. The send process is therefore completed the SENDwhen the sum of the actual sent data is equal the send data length. A simple example of the send process is as below
传输数据的大小不能大于分配的套接字n的内部tx内存。如果传输数据的大小大于配置的mss,则划分为MSS的大小并传输。要传输下一个数据,用户必须检查先前发送命令的完成情况。如果发送命令在完成先前的发送命令之前执行,则可能发生错误。数据大小越大,完成发送命令的时间就越多。因此,用户应该正确划分要传输的数据。
要检查send命令的完成情况,应该检查send数据长度是否等于实际发送的数据长度。实际发送的数据长度由执行发送命令前后的sn_tx_rd的差计算得出。如果实际发送的数据小于发送数据的长度,则会重试发送命令以发送左侧数据。因此,当实际发送数据的总和等于发送数据的长度时,发送过程将完成发送。发送过程的一个简单示例如下

Ex) 发送数据长度=10
1,执行带有数据长度的SEND命令
2,计算实际要发送的数据长度
如果实际发送数据长度是7(=Sn_TX_RD_after_SEND-Sn_TX_RD_befor_SEND),
如果剩下的数据长度=3
3,重试SEND命令直到实际发生数据长度的和与待发送数据的长度相等。

{
/* first, get the free TX memory size */ 首先获得可用的TX内存大小
FREESIZE:
freesize = Sn_TX_FSR;
if (freesize<len) goto FREESIZE; // len is send size

/* select TX memory, refer to Datasheet 14 page /
cntl_byte = Socket_n_TX_Buffer
/
Get offset address /
dst_ptr = Sn_TX_RD;
/
set memory copy len bytes of source_ptr to destination_address */
for (i=0; i<len; i++)
{
W5500_WRITE(addr, cntl_byte, dst_ptr+i);
}

/* increase Sn_TX_WR as length of len /
Sn_TX_WR += send_size;
/
set SEND command /
Sn_CR = SEND;
/
return real packet size */
return len;
}

ESTABLISHMENT : Check disconnect-request(FIN packet)
建立:检查断开连接请求(FIN包)
Check if the Disconnect-request(FIN packet) has been received. User can confirm the reception of FIN packet as below.
检查是否收到断开连接请求(FIN包)。用户按下面的方法确认FIN包的接收。
First method :
{
if (Sn_IR(DISCON) == ‘1’) Sn_IR(DISCON)=‘1’; goto CLOSED stage;
/* In this case, if the interrupt of Socket n is activated, interrupt occurs. Refer to IR, IMR
Sn_IMR and Sn_IR. */
}
Second method :
{
if (Sn_SR == SOCK_CLOSE_WAIT) goto CLOSED stage;
}

ESTABLISHMENT : Check disconnect / disconnecting process
建立:检查断开连接/断开连接过程
When the user does not need data communication with others, or receives a FIN packet, disconnect the connection SOCKET.
当用户没有数据需要处理时,或者接受到一个FIN包,断开套接字的连接。
{
/* set DISCON command */
Sn_CR = DISCON;
}

ESTABLISHMENT : Check closed
建立:检查关闭
Confirm that the Socket n is disconnected or closed by DISCON or close command.
通过DISCON或CLOSE命令确认套接字n是断开连接或关闭的
First method :
{
if (Sn_IR(DISCON) == ‘1’) goto CLOSED stage;
/* In this case, if the interrupt of Socket n is activated, interrupt occurs. Refer to IR, IMR
Sn_IMR and Sn_IR. */
}
Second method :
{
if (Sn_SR == SOCK_CLOSED) goto CLOSED stage;
}

ESTABLISHMENT: Timeout
建立:超时
The timeout can occur by Connect-request(SYN packet) or its response(SYN/ACK packet), the DATA packet or its response(DATA/ACK packet), the Disconnectrequest FIN packet) or its response(FIN/ACK packet) and transmission all TCP packet. If it cannot transmit the above packets within ‘timeout’ which is configured at RTR and RCR, the TCP final timeout(TCPTO) occurs and the state of Sn_SR is set to SOCK_CLOSED. Confirming method of the TCPTO is as below:
超时可以通过连接请求(syn packet)或其响应(syn/ack packet)、数据包或其响应(data/ack packet)、断开连接请求fin packet或其响应(fin/ack packet)发生,并传输所有TCP包。如果它不能在RTR和RCR配置的“超时”内传输上述数据包,则会发生TCP最终超时(TCPTO),sn_sr的状态设置为sock_closed。TCPTO的确认方法如下:
First method :
{
if (Sn_IR(TIMEOUT bit) == ‘1’) Sn_IR(TIMEOUT)=‘1’; goto CLOSED stage;
/* In this case, if the interrupt of Socket n is activated, interrupt occurs. Refer to IR, IMR
Sn_IMR and Sn_IR. */
}
Second method :
{
if (Sn_SR == SOCK_CLOSED) goto CLOSED stage;
}

SOCKET Close 套接字关闭

It can be used to close the Socket n , which disconnected by disconnect-process, or closed by TCPTO or closed by host’s need without disconnect-process.
它可用于关闭通过断开过程断开的套接字N,或通过TCPTO关闭的套接字N,或在没有断开过程的情况下根据主机需要关闭的套接字N。
{
/* clear the remained interrupts of Socket n /
Sn_IR = 0xFF;
IR(n) = ‘1’;
/
set CLOSE command */
Sn_CR = CLOSE;
}

发布了8 篇原创文章 · 获赞 1 · 访问量 3635

猜你喜欢

转载自blog.csdn.net/qq_15548761/article/details/89366237