首页 W5200_TCP_AN_v1.0_en

W5200_TCP_AN_v1.0_en

举报
开通vip

W5200_TCP_AN_v1.0_en ©Copyright 2011 WIZnet Co., Inc. All rights reserved. TCP Application note for W5200 Version 1.0 © 2011 WIZnet Co., Inc. All Rights Reserved. For more information, visit our website at http://www.wiznet.co.kr © Copyright 2011 WIZnet Co., Inc. All right...

W5200_TCP_AN_v1.0_en
©Copyright 2011 WIZnet Co., Inc. All rights reserved. TCP Application note for W5200 Version 1.0 © 2011 WIZnet Co., Inc. All Rights Reserved. For more information, visit our website at http://www.wiznet.co.kr © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 2 TCP Application note for W5200 Table of Contents 1 Introduction ...............................................................................................3 2 TCP/IP SOCKET ...........................................................................................3 2.1 OPEN............................................................................................5 2.2 LISTEN..........................................................................................6 2.3 CONNECT ......................................................................................6 2.4 SEND ............................................................................................7 2.5 RECEIVE ...................................................................................... 10 2.6 DISCONNECT................................................................................. 10 2.7 CLOSE ........................................................................................ 11 3 TCP Loopback Program ................................................................................ 12 3.1 Server Mode ................................................................................. 12 3.2 Client Mode ................................................................................. 15 4 TCP Loopback Program Demonstration ............................................................. 18 4.1 Setting for Running TCP Loopback ...................................................... 18 4.1.1 Physical Connection ................................................................ 19 4.1.2 Network Configuration ............................................................ 20 4.1.3 Compile .............................................................................. 21 4.1.4 Download ............................................................................ 22 4.1.5 Setting for Serial Terminal ........................................................ 25 4.2 AX1............................................................................................ 26 4.2.1 AX1 Setting for TCP Server........................................................ 26 4.2.2 AX1 Setting for TCP Client ........................................................ 27 4.3 Demonstration .............................................................................. 29 4.3.1 TCP Server Demonstration ........................................................ 29 4.3.2 TCP Client Demonstration ........................................................ 30 Document History Information ............................................................................ 31 © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 3 TCP Application note for W5200 1 Introduction The TCP (Transmission Control Protocol) controls data communication between networks. As one of the main protocols that forms the internet, more details are written in RFC 793 of IETF (Internet Engineering Task Force). TCP is a protocol that runs above IP; TCP guarantees the data transmission and allows receiving data in the order it was sent. Since W7100 supports TCP protocol in the Transport Layer, user can use TCP/IP protocol without any composition. 2 TCP/IP SOCKET User can use all eight SOCKETs provided by W5200 as TCP protocol. If the user wishes to use W5200 as TCP Protocol, user must first create the SOCKET that is going to be used. When creating SOCKET, SOCKET number, protocol to be used, port number to be used, and the flag to be set is required. This document is going to explain about TCP protocol; the protocol that is going to be used should set the Sn_MR (SOCKET n Mode Register in W5200.h) to Sn_MR_TCP (0x01). The SOCKET number means the existing eight SOCKETs and user can randomly number each one from 0 to 7. The port number that is going to be used can be assigned by user for the TCP protocol. There will be no problem if the above requirements for creating a SOCKET were assigned by using the SOCKET() function from WIZnet. Since the TCP protocol of W5200 supports both sever mode and client mode, user can select one and use for its application. The difference between server mode and client mode are shown below. Fig. 2.1 TCP Server & TCP Client © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 4 TCP Application note for W5200 As shown in Figure 2.1, all actions are the same except for one; whether the status is the LISTEN() or CONNECT() after opening the SOCKET. When TCP protocol runs in server mode, the SERVER will wait for CLIENT’s connection request in LISTEN status. However, when TCP protocol runs in client mode, the CLIENT will try to connect to server in CONNECT status. Once the connection is successful, the status of SOCKET will change to ESTABLISHED(SOCK_ESTABLISHED, 0x17) status. SOCKETs that connect after this point can stay connected and exchange data until the SOCKET is closed. The SOCKET lifecycle in server mode is consisted of OPEN, LISTEN, SEND, RECEIVE, DISCONNECT, and CLOSE. The SOCKET lifecycle in client mode is consisted of OPEN, CONNECT, SEND, RECEIVE, DISCONNECT, and CLOSE. Starting from section 2.1, the operations from each SOCKET Lifecycle will be explained in detail with example source code (Pseudo code). © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 5 TCP Application note for W5200 2.1 OPEN The first step of creating a SOCKET for both SERVER and CLIENT mode is OPEN. To create SOCKETn (the n th SOCKET), use the SOCKET() function to set the SOCKET number, protocol, port number, and flag. Since the protocol is TCP, set the protocol to Sn_MR_TCP(0x01). Setting of port number differs depending on whether it is server mode or client mode. When server mode is being used, server can set the source port number that the client is using. But when client mode is being used, it is best to choose a random port number and increase one number at a time until the SOCKET is connected since there can be a destination port number already in use. The flag of the TCP protocol is for ‘No Delayed Ack flag’ and etc. In general, it is set to 0. More details on protocol types, flag types, and etc. are explained in ‘Sn_MR value’ of ‘W5200.h,’ that is provided by WIZnet. After all settings are completed, check Sn_SR(n) register to see whether the status of SOCKETn is changed to SOCK_INIT(0x13). User can use getSn_SR(SOCKETn) function when checking Sn_SR(n) register. If the status of SOCKETn is SOCK_INIT(0x13), SOCKET is created properly. If it is not created, user should recreate the SOCKET. /* Method 1 : Server mode */ /* sets Protocol Number */ s = 0; // set SOCKET 0 /* OPEN SOCKET 0 */ socket(s, Sn_MR_TCP, port, mode); while(getSn_SR(s) != SOCK_INIT); /* Method 2 : Client mode */ /* sets Protocol Number */ s = 0; // set SOCKET 0 /* sets port number */ any_port = 1000; /* OPEN SOCKET 0 */ socket(s, Sn_MR_TCP, any_port++, mode); while(getSn_SR(s) != SOCK_INIT); Example 2.1 Open Socket © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 6 TCP Application note for W5200 2.2 LISTEN The LISTEN step is only used during SERVER mode. After creating SOCKETn, change the status of SOCKET to LISTEN so that the CLIENT can connect. In order to change the status of SOCKET from SOCK_INIT(0x13) to LISTEN status, user can directly set the Sn_CR_LISTEN(0x02) to the Sn_CR(n) register or can use the LISTEN(n) function from ‘SOCKET.c.’ After the status changes to LISTEN the status of SOCKET will change to SOCK_LISTEN(0x14). Then, SOCKET will wait until there is a request to connect from CLIENT. Once CLIENT is connected, the status of SOCKET will change again to SOCK_ESTABLISHED(0x17). Then finally data transmission is possible with the CLIENT. s = 0; // set SOCKET 0 listen(s); Example 2.2 set LISTEN state 2.3 CONNECT The CONNECT stage is used during CLIENT mode to connect to the SERVER. The SOCKET number that is going to be used, destination IP, and destination port number are required to CONNECT. Set these requirements by using CONNECT() function; and once the connection is successful, the status of SOCKET will change to SOCK_ESTABLISHED(0x17). s = 0; // set SOCKET 0 Serverip[4] = {192, 168, 11, 3}; // set Server(destination) IP Serverport = 3000; // set Server(destination) port connect(s, Serverip, Serverport); Example 2.3 set CONNECT state © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 7 TCP Application note for W5200 2.4 SEND In the case of TCP protocol, the connection between the peer is already established before sending data; therefore, not much information is required when sending data. Set the SOCKET number, address of the data that is going to be sent, data size, the use of resend (ON, OFF) when Windowfull occurs. The address of the data that is going to be sent is usually set by selecting the area, putting the data in, and setting the area with pointer. In order to confirm the data transmission after the SEND command, the data length to be sent and the data length that is actually sent must be identical. The data length that is actually sent can be calculated by checking the Sn_TX_RD register value before and after the SEND command. The SEND process will complete if the data length to be sent and the data length actually sent matches identically. However, if not, Windowfull will occur in the peer’s buffer and this means that parts of the data are not successfully sent. If Windowfull occurs in the send() process of the example code, the data is sent in unit of 1 byte and as much as the number of WINDOWFULL_MAX_RETRY_NYM untill the peer’s buffer is ready to send the remaining data. After the data is sent, wait for a period of time (WINDOWFULL_WAIT_TIME), and check whether the send process was successful by comparing the data length to be sent and the data length actually sent. The send process will be completed if the data lengths match. Be aware that a loss of data can occur if data copy is operated during th process. Close the socket since there can be problems of the network if the number of retry exceeds the WINDOWFULL_MAX_RETRY_NUM. Modify WINDOWFULL_WAIT_TIME, WINDOWFULL_MAX_RETRY_NUM, and Socket close according to user’s network when dealing with Windowfull operation. If needed, WINDOWFULL_WAIT_TIME can be set longer or user can close Socket right away when Windowfull occurs. Below are the steps for Send Process. 1. Start SEND Command by using Data Length. 2. Calculate the Data Length that is actually sent. If the total Data Length is 10 and the Data Length that is sent is 7, the remaining Data Length (=Sn_TX_RD_after_SEND-Sn_TX_RD_befor_SEND) is 3. 3. Repeat the Send Command until the sum of the Data Length that is actually sent equals the Data Length to be sent. Note: Do not operate Data Copy until the value of Return equals the value of Data Length to be sent. © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 8 TCP Application note for W5200 Start Send_data_len != Sent_data_len Next Process Time out Delay time Calculate free buffer size Data copy Sn_CR_SEND Wait to process the command Send() Calculate free buffer size Data copy Sn_CR_SEND Wait to process the command Send() Init. Windowfull MAX Retry count Init. Windowfull Delay time Retry count++ < Windowfull occurrence > Yes ※ : Do not execute ※ ※ Retry count = 0 No Retry count <= MAX Retry countYes No Send_data_len != Sent_data_len Yes No Send() return Actual Sent_data_length Data length scheduled to send : Send_data_length Fig. 2.2 SEND() : Windowfull handling process © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 9 TCP Application note for W5200 /* Send data to connected peer. */ // TX_RX_MAX_BUF_SIZE must be smaller than the maximum size of the TX buffer s = 0; // set SOCKET 0 * data_buf; // set position of data buffer RSR_len = getSn_RX_RSR(s); // set length for send sent_data_len = send(s, data_buf, RSR_len, WINDOWFULL_FLAG_OFF); // send data and save the length /* only assert when windowfull */ if(sent_data_len != received_len) { Init_windowfull_retry_cnt(s); // windowfull_retry_cnt[s] set 0 While(sent_data_len != received_len) { tmp_retry_cnt = incr_windowfull_retry_cnt(s); // default WINDOWFULL_MAX_RETRY_NUM = 3 if(tmp_retry_cnt <= WINDOWFULL_MAX_RETRY_NUM) { // try to send the remaining data sent_data_len += sent(s, data_buf, received_len, WINDOWFULL_FLAG_ON); Delay_ms(WINDOWFULL_WAIT_TIME); // default delay = 1 sec } else { // if the 'Windowfull' occers and then send retry 3 times over, socket close close(s); while(1); } } } Example 2.4 SEND DATA © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 10 TCP Application note for W5200 2.5 RECEIVE RECEIVE is similar in usage method to SEND, but it checks the Sn_RX_RSR(n). The RECEIVE step is to move the data, which came into the RX buffer, to the user’s data area. Therefore, user must check whether the value of Sn_RX_RSR(n) is larger than 0 before the RECEIVE step. If the value of Sn_RX_RSR(n) is larger than 0, it means that the data is in the RX buffer. User must use getSn_RX_RSR(n) function to check whether the data is received or not before the RECEIVE step. /* Check received data */ // len indicates the received data size in the RX buffer. // len must be smaller than the maximum size of the RX buffer s = 0; // set SOCKET 0 * data_buf; // set position of data buffer if ( (RSR_len = getSn_RX_RSR(s) ) > 0) /* Received data */ // RSR_len is a length included the DATA packet. received_len = recv(s, data_buf, RSR_len); Example 2.5 RECEIVE DATA 2.6 DISCONNECT There are two ways of closing a created SOCKET, and one is DISCONNECT(n). The DISCONNECT(n) is not used to just directly close the SOCKET; rather, it is used to send a disconnect-request (FIN packet) to a peer and wait for a disconnect-reply (FIN/ACK packet) to change the status of SOCKET to SOCK_CLOSED(0x00), and ultimately close the SOCKET. When a disconnect request comes in, W5200 generates a FIN/ACK packet to allow the peer to close SOCKET. If there is no answer from the peer after disconnect-request(FIN packet) is generated, TCP timeout occurs and after that, the status of SOCKET changes to SOCKET_CLOSED(0x00). When the user wants to DISCONNECT, use DISCONNECT(n) function and choose the SOCKET number that will generate the disconnect request. s = 0; // set SOCKET 0 disconnect(s); Example 2.6 SET DISCONNECT © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 11 TCP Application note for W5200 2.7 CLOSE Unlike DISCONNECT, CLOSE directly changes the SOCKET to SOCK_CLOSED(0x00). User can use the CLOSE(n) function and choose the SOCKET number; it closes the SOCKET regardless of the peer. If a RST packet comes from a peer, SOCKET will unconditionally change to SOCK_CLOSED(0x00). Once the SOCKET changes to SOCK_CLOSED(0x00), that SOCKET is not usable unless it is opened again. s = 0; // set SOCKET 0 close(s); Example 2.7 SET CLOSE © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 12 TCP Application note for W5200 3 TCP Loopback Program 3.1 Server Mode TCP Loopback can check the performance of TCP protocol by using TCP protocol to send back the data that came in from a peer. This section will explain the example of Loopback in SERVER mode. The example codes are as followed. void loopback_tcps(SOCKET s, uint16 port) { uint16 RSR_len; uint16 received_len; uint8 * data_buf = TX_BUF; uint16 sent_data_len = 0; uint8 tmp_retry_cnt = 0; switch (getSn_SR(s)) { case SOCK_ESTABLISHED: /* if connection is established */ if(ch_status[s]==1) { ch_status[s] = 2; } if ((RSR_len = getSn_RX_RSR(s)) > 0) /* check Rx data */ { /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */ if (RSR_len > TX_RX_MAX_BUF_SIZE) RSR_len = TX_RX_MAX_BUF_SIZE; received_len = recv(s, data_buf, RSR_len); /* read the received data */ /* sent the received data */ sent_data_len = send(s, data_buf, received_len, (bool)WINDOWFULL_FLAG_OFF); if(sent_data_len != received_len) /* only assert when windowfull */ { init_windowfull_retry_cnt(s); while(sent_data_len != received_len) { tmp_retry_cnt = incr_windowfull_retry_cnt(s); © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 13 TCP Application note for W5200 if(tmp_retry_cnt <= WINDOWFULL_MAX_RETRY_NUM) { sent_data_len += send(s, data_buf, received_len, (bool)WINDOWFULL_FLAG_ON); Delay_ms(WINDOWFULL_WAIT_TIME); } else { close(s); while(1); } } } } break; case SOCK_CLOSE_WAIT: /* If the client request to close */ printf("\r\n%d : CLOSE_WAIT", s); if ((RSR_len = getSn_RX_RSR(s)) > 0) /* check Rx data */ { /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */ if (RSR_len > TX_RX_MAX_BUF_SIZE) RSR_len = TX_RX_MAX_BUF_SIZE; received_len = recv(s, data_buf, RSR_len); /* read the received data */ } disconnect(s); ch_status[s] = 0; break; case SOCK_CLOSED: /* if a socket is closed */ if(!ch_status[s]) { printf("\r\n%d : Loop-Back TCP Server Started. port : %d", s, port); ch_status[s] = 1; } if(socket(s,Sn_MR_TCP,port,0x00) == 0) /* reinitialize the socket */ { printf("\r\n%d : Fail to create socket.",s); ch_status[s] = 0; © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 14 TCP Application note for W5200 } break; case SOCK_INIT: /* if a socket is initiated */ listen(s); break; default: break; } } Example 3.1 SET LOOPBACK SERVER As shown in the example above, all functions from the SOCKET Lifecycle are used except the connect() function. At first use the getSn_SR(s) function to check the status of SOCKET. After W5200 is reset, all SOCKETs are in SOCK_CLOSED(0x00) status. Therefore, use the close(s) function to completely close the SOCKET, and use the socket(s, Sn_MR_TCP, port, 0x00) function to newly create SOCKET. If created properly, status will be SOCK_INIT, and then use the listen(s) function to change the SOCKET into LISTEN status. Once connected with a peer, SOCKET will change to SOCK_ESTABLISHED and wait for data. All received data are in the RX buffer. Use the recv(s, data_buf, len) function to save the length of received data in data_buf. Then use the send(s, data_buf, len) function to send back the data to the client. The client can compare the data before/after exchange and check whether the communication is working properly. If the received data length (received_len) and sent data length (sent_data_len) does not match each other, there is a possibility of the peer’s Windowfull and Send Retry must be done as explained in section 2.4 to resend all remaining data. The default WINDOWFULL_WAIT_TIME of the implemented Loopback example code is set to 1000ms, and WINDOWFULL_MAX_RETRY_NUM is set to 3; the Socket will close if the WINDOWFULL_MAX_ RETRY_NUM exceeds 3 before the entire send process is not completed due to windowfull. © Copyright 2011 WIZnet Co., Inc. All rights reserved. Ver. 1.0 15 TCP Application note for W5200 3.2 Client Mode This section will explain the example of Loopback in CLIENT mode. The example codes are as followed. #define tick_second 1 void loopback_tcpc(SOCKET s, uint16 port) { uint16 RSR_len; uint16 received_len; uint8 * data_buf = TX_BUF; uint16 sent_data_len = 0; uint8 tmp_retry_cnt = 0; switch (getSn_SR(s)) { case SOCK_ESTABLISHED: /* if connection is established */ if(ch_status[s]==1) { printf("\r\n%d : Connected",s); ch_status[s] = 2; } if ((RSR_len = getSn_RX_RSR(s)) > 0) /* check Rx data */ { /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */ if (RSR_len > TX_RX_MAX_BUF_SIZE) RSR_len = TX_RX_MAX_BUF_SIZE; received_len = recv(s, data_buf, RSR_len); /* read the received data */ /* sent the received data
本文档为【W5200_TCP_AN_v1.0_en】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_558719
暂无简介~
格式:pdf
大小:1MB
软件:PDF阅读器
页数:0
分类:互联网
上传时间:2013-12-02
浏览量:23