Hi ! Thanks for reply.
Sure, all chip commands are sent as recommended, because we are using default SOCKET.C functions, provided by Wiznet.
Exactly, to run telnet server, at least two functions must be used:
socket() - to reserve resources for new socket in W5200
listen() - to activate new socket to make it listen for connections
/*
*
@file socket.c
@brief setting chip register for socket
* last update : July 30, 2013
*
* -----------------------------------------------------------------------------------
* History
* -----------------------------------------------------------------------------------
*
* - first release : Jan. 2008
* - modified code July 30, 2013 @wiznet : fix the code to add condition "retry" in send().
*
*/
/**
@brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5200 done it.
@return 1 for sucess else 0.
*/
uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag)
{
uint8 ret;
#ifdef __DEF_IINCHIP_DBG__
printf("socket()\r\n");
#endif
if ((protocol == Sn_MR_TCP) || (protocol == Sn_MR_UDP) || (protocol == Sn_MR_IPRAW) || (protocol == Sn_MR_MACRAW) || (protocol == Sn_MR_PPPOE))
{
close(s);
IINCHIP_WRITE(Sn_MR(s),protocol | flag);
if (port != 0) {
IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8));
IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff));
} else {
local_port++; // if don't set the source port, set local_port number.
if(local_port==23) local_port++;
IINCHIP_WRITE(Sn_PORT0(s),(uint8)((local_port & 0xff00) >> 8));
IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(local_port & 0x00ff));
}
IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN); // run sockinit Sn_CR
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s)) )
;
/* ------- */
ret = 1;
}
else
{
ret = 0;
}
#ifdef __DEF_IINCHIP_DBG__
printf("Sn_SR = %.2x , Protocol = %.2x\r\n", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
#endif
return ret;
}
/**
@brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
@return 1 for success else 0.
*/
uint8 listen(SOCKET s)
{
uint8 ret;
#ifdef __DEF_IINCHIP_DBG__
printf("listen()\r\n");
#endif
if (IINCHIP_READ(Sn_SR(s)) == SOCK_INIT)
{
IINCHIP_WRITE(Sn_CR(s),Sn_CR_LISTEN);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s)) )
;
/* ------- */
ret = 1;
}
else
{
ret = 0;
#ifdef __DEF_IINCHIP_DBG__
printf("Fail[invalid ip,port]\r\n");
#endif
}
return ret;
}
As we can see, all commands for W5200 are followed by waiting for clear register to make sure command was executed:
while( IINCHIP_READ(Sn_CR(s)) );
Send() and Recv() functions are also respect this rule:
Any write to command register is followed by waiting to command complete.
[code]
/**
@brief This function used to send the data in TCP mode
@return 1 for success else 0.
*/
uint16 send(SOCKET s, const uint8 * buf, uint16 len, bool retry)
{
uint8 status=0;
uint16 ret=0;
uint16 freesize=0;
uint16 txrd, txrd_before_send;
#ifdef DEF_IINCHIP_DBG
printf(“send()\r\n”);
#endif
if(retry) ;
else {
if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
else ret = len;
// if freebuf is available, start.
do
{
freesize = getSn_TX_FSR(s);
status = IINCHIP_READ(Sn_SR(s));
if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT))
{
ret = 0;
break;
}
#ifdef DEF_IINCHIP_DBG
printf(“socket %d freesize(%d) empty or error\r\n”, s, freesize);
#endif
} while (freesize < ret);
// copy data
send_data_processing(s, (uint8 *)buf, ret);
}
// if(ret != 0) // error code
// 2013-07-30 wiznet fix the code to add condition “retry”
if(retry || ret != 0)
{
txrd_before_send = IINCHIP_READ(Sn_TX_RD0(s));
txrd_before_send = (txrd_before_send << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s)) );
while ( (IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK )
{
if(IINCHIP_READ(Sn_IR(s)) == SOCK_CLOSED)
{
#ifdef DEF_IINCHIP_DBG
printf(“SOCK_CLOSED.\r\n”);
#endif
close(s);
return 0;
}
}
IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
txrd = IINCHIP_READ(Sn_TX_RD0(s));
txrd = (txrd << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
if(txrd > txrd_before_send) {
ret = txrd - txrd_before_send;
} else {
ret = (0xffff - txrd_before_send) + txrd + 1;
}
}
return ret;
}
/**
@brief This function is an application I/F function which is used to receive the data in TCP mode.
It continues to wait for data as much as the application wants to receive.
@return received data size for success else -1.
*/
uint16 recv(SOCKET s, uint8 * buf, uint16 len)
{
uint16 ret=0;
#ifdef DEF_IINCHIP_DBG
printf(“recv()\r\n”);
#endif
if ( len > 0 )
{
recv_data_processing(s, buf, len);
IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
/* wait to process the command... */
while( IINCHIP_READ(Sn_CR(s)));
/* ------- */
ret = len;
}
return ret;
}[/code]
Regards, Alex