WIZnet Developer Forum

W5500 Socket Creation successful but TCP Client Connect hanging Up STM

Good day,

I was hoping someone could possibly assist me.

we have about 200 w5500 running as Servers which all work very well.

however we have recently received a project that requires us to use the W5500 as a client and a server talking to one another vs just a server.

attempting to connect after creating a socket completely hangs up the unit.
if((ret = connect(SOCK_ID_TCP,__au8_DesIp,0x1E61)) == SOCK_OK);

the problem appears to be with (getSn_IR(sn), creating an infinite loop.

while(getSn_SR(sn) != SOCK_ESTABLISHED) // not here
{
if (getSn_IR(sn) & Sn_IR_TIMEOUT) // this is where the infinite loop is created.
{
setSn_IR(sn, Sn_IR_TIMEOUT);
return SOCKERR_TIMEOUT;
}
}

please see below procedure -

int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)

{
uint32_t taddr;

CHECK_SOCKNUM();
CHECK_SOCKMODE(Sn_MR_TCP);
CHECK_SOCKINIT();

taddr = ((uint32_t)addr[0] & 0x000000FF);
taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
taddr = (taddr << 8) + ((uint32_t)addr[0] & 0x000000FF);
if( taddr == 0xFFFFFFFF || taddr == 0)
{
    return SOCKERR_IPINVALID;
}

if(port == 0)
{
    return SOCKERR_PORTZERO;
}

setSn_DIPR(sn,addr);
setSn_DPORT(sn,port);
setSn_CR(sn,Sn_CR_CONNECT);

while(getSn_CR(sn));

if(sock_io_mode & (1<<sn))
{
    return SOCK_BUSY;
}

while(getSn_SR(sn) != SOCK_ESTABLISHED)
{
    if (getSn_IR(sn) & Sn_IR_TIMEOUT)
    {
        setSn_IR(sn, Sn_IR_TIMEOUT);
        return SOCKERR_TIMEOUT;
    }
}

return SOCK_OK;

}

please see below for full execution

#include "Wizchip_Conf.h"

#include “W5500.h”
#include “Socket.h”

sfr sbit Wizchip_Rst at GPIOC_ODR.B13;
sfr sbit Wizchip_CS at GPIOA_ODR.B4;

#define SOCK_ID_TCP 0
#define SOCK_ID_UDP 1
#define PORT_TCP_LOCAL 6000
#define PORT_TCP_REMOTE 6001
#define PORT_UDP 10001
#define DATA_BUF_SIZE 2048

char UartDebug;
char bufferDebug[2048];
int D,I;
unsigned short ReceivedDebug = 0,triggerTCP =0;

uint8_t gDATABUF[DATA_BUF_SIZE];

uint8_t __au8_DesIp[4] = {192, 168, 0, 40};

volatile wiz_NetInfo gWIZNETINFO =
{
{0x00, 0x14, 0xA3, 0x72, 0x17, 0x3f}, // Source Mac Address
{192, 168, 0, 177}, // Source IP Address
{255, 255, 255, 0}, // Subnet Mask
{192, 168, 0, 215}, // Gateway IP Address
{192, 168, 0, 215}, // DNS server IP Address
NETINFO_STATIC
};

volatile wiz_PhyConf phyConf =
{
PHY_CONFBY_HW, // PHY_CONFBY_SW
PHY_MODE_AUTONEGO, // PHY_MODE_AUTONEGO
PHY_SPEED_100, // PHY_SPEED_100
PHY_DUPLEX_FULL, // PHY_DUPLEX_HALF
};

volatile wiz_NetInfo pnetinfo;

// brief Call back function for WIZCHIP select.
void CB_ChipSelect(void)
{
Wizchip_CS = 0;
}

// brief Call back function for WIZCHIP deselect.
void CB_ChipDeselect(void)
{
Wizchip_CS = 1;
}

// brief Callback function to read byte usig SPI.
uint8_t CB_SpiRead(void)
{
return SPI_Read(0xAA);
}

// brief Callback function to write byte usig SPI.
void CB_SpiWrite(uint8_t wb)
{
SPI_Write(wb);
}

static void W5500_Init(void)
{
// Set Tx and Rx buffer size to 2KB
uint8_t buffsize[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };
uint8_t sn2;
int32_t ret;
uint16_t size = 0, sentsize = 0;
int BufferSize =0;
GPIO_Digital_Output(&GPIOC_ODR, _GPIO_PINMASK_13); // Set Rst pin to be output
GPIO_Digital_Output(&GPIOA_ODR, _GPIO_PINMASK_4); // Set CS pin to be output

CB_ChipDeselect();                                                          // Deselect module

// Reset module
Delay_ms(10);
Wizchip_Rst = 0;
Delay_ms(10);
Wizchip_Rst = 1;
Delay_ms(10);

SPI1_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER | _SPI_8_BIT |
                   _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                   _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                  &_GPIO_MODULE_SPI1_PA567);

// Wizchip initialize
wizchip_init(buffsize, buffsize, 0, 0, CB_ChipSelect, CB_ChipDeselect, 0, 0, CB_SpiRead, CB_SpiWrite);

// Wizchip netconf
ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);
ctlwizchip(CW_SET_PHYCONF, (void*) &phyConf);

}

void TCP_Client(void)
{
int32_t ret;
uint16_t size = 0, sentsize = 0;
int BufferSize =0;
UART3_Write_Text(“TCP Client Started !!!\n\r”);
if((ret = socket(SOCK_ID_TCP, Sn_MR_TCP, PORT_TCP_LOCAL, 0x00)) == SOCK_ID_TCP)
{
UART3_Write_Text(“created socket !!!\n\r”);
delay_ms(800);
if((ret = connect(SOCK_ID_TCP,__au8_DesIp,0x1E61)) == SOCK_OK){
UART3_Write_Text(“SOCKET connected\r\n”);
}
}
}

wireshark responds with

Frame 29080: 54 bytes on wire (432 bits), 54 bytes captured (432 bits) on interface 0
Ethernet II, Src: IntelCor_6f:28:31 (34:41:5d:6f:28:31), Dst: VitelecB_72:17:3f (00:14:a3:72:17:3f)
Internet Protocol Version 4, Src: 192.168.0.40, Dst: 192.168.0.177
Transmission Control Protocol, Src Port: 7777, Dst Port: 6000, Seq: 1, Ack: 1, Len: 0
Source Port: 7777
Destination Port: 6000
[Stream index: 1539]
[TCP Segment Len: 0]
Sequence number: 1 (relative sequence number)
[Next sequence number: 1 (relative sequence number)]
Acknowledgment number: 1 (relative ack number)
0101 … = Header Length: 20 bytes (5)
Flags: 0x014 (RST, ACK)
Window size value: 0
[Calculated window size: 0]
[Window size scaling factor: -2 (no window scaling used)]
Checksum: 0xb499 [unverified]
[Checksum Status: Unverified]
Urgent pointer: 0
[SEQ/ACK analysis]
[This is an ACK to the segment in frame: 29079]
[The RTT to ACK the segment was: 0.000026000 seconds]
[iRTT: 0.000026000 seconds]
[Timestamps]

I have a lot tried to change the ports to multiple ports.

please advise should you require any further info or if you would like me to upload libraries

client server
SYN->
<-SYN, ACK
ACK->
TCP connection is made through the above three processes.

Guessing the information you sent me,
The server does not send a SYN, ACK packet, but sends an RST packet.
Is it correct that you are opening a TCP server from ip 192.168.0.40 and port 7777?

Thanks, Bechy.

looks like that was the issue the port on the server side was closed.
thanks so much.

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.