W5500EVB as TCP Client

This sample code demonstrates how W5500EVB works as a TCP client or multiple TCP clients. As a TCP client, W5500EVB request to connect to TCP Server within the same subnet using ethernet. After doing handshakes, TCP clients are allowed to communicate with the TCP server. W5500 allows users to establish 8 sockets at a time. The following sample code demonstrates W5500EVB serves as both one socket client and multiple TCP clients.


Before burning the bin file into your EVB:

      1. Connect W5500 EVB to the router/NAT that you are using, or any other device that allows you to connect to the internet
      2. Manually change the Network configuration:
          • Go to the definition of function “set_default()” (Should be inside device.c), make sure the subnet mask, local IP address, gateway and DNS are set according to the internet setting you are using with your router/NAT
      3. Rebuild the sample code and burn it into W5500EVB
      4. Change your PC’s IP address to a static IP if you are not using router


Code Explanation

The sample code are basically divided into four states: Initialized, Established, To Be Closed, Closed. 

int main(void)
{


After boot or reboot W5500EVB, all necessary configurations are initialized.

  uint8 server_ip[4]={192,168,1,151};
  uint16 server_port=5000;
  uint16 local_port=6000;
  uint16 len=0;
  Systick_Init(72);	
  NVIC_Configuration();
  GPIO_Configuration();			
  WIZ_SPI_Init();
  USART1_Init(); 		
  at24c16_init();
  Reset_W5500();
  set_default();
  set_network();

  setkeepalive(0);
	
  printf("TCP Server IP: %d.%d.%d.%d \r\n",server_ip[0],server_ip[1],server_ip[2],server_ip[3]);
  printf("TCP Server Port: %d \r\n",server_port);	
  printf("W5500 Init Complete!\r\n");
  printf("Start TCP Client Test!\r\n"); 
                                                           
                                             
/******************


These are all the necessary
 initialization


********************/






/******************


These are messages shown in 
serial window for debugging
 

********************/

Socket Initialized

After initialization, W5500EVB goes into the main while loop, and open a socket:

while(1){
  switch(getSn_SR(0))
  {      
    case SOCK_INIT:
      connect(0, server_ip,server_port);
    break;
/***************
Switch Statement for different Socket State


Socket Initialized State
waiting for user to connect to server
****************/

Socket Established

Once you connect W5500EVB to a server, W5500 will change the state to Socket Established state, meaning that the client(our W5500EVB) and server can now communicate with each other. The step of communication are as below:

    case SOCK_ESTABLISHED:     
      if(getSn_IR(0) & Sn_IR_CON)
      {
        setSn_IR(0, Sn_IR_CON);
      }

      len=getSn_RX_RSR(0);
      if(len>0)
      {
        recv(0,buffer,len);
        printf("%s\r\n",buffer);
        send(0,buffer,len);
      }

      if(USART_RX_STA & 0x8000)
      {
        len=USART_RX_STA & 0x3fff;
        send(0,USART_RX_BUF,len);
        USART_RX_STA=0;
        memset(USART_RX_BUF,0,len+1);
      }
    break;
/*************

When W5500 receives message from server,
1. Get the length of data receivedGet and store the received data from
2. W5500’s buffer to MCU’s bufferPrint the data out to our serial
3. windowSend the data back to server as echo/feedback  

****************/





/***************

When we send data to server,
1. Keep looping the process until data from terminal are all received by MCU
2. Identify the length of data packet and send the data from the MCU’s buffer, through W5500, to server
3. Clear the flag and data buffer for next communication

****************/

Socket to be closed

It is a transition process of a socket from opened to closed

    case SOCK_CLOSE_WAIT:
      close(0);
    break;

Socket Closed

Socket was closed and open again for next connection

    case SOCK_CLOSED:
      socket(0,Sn_MR_TCP,local_port,Sn_MR_ND);
    break;
    }
  }
}
/*
Socket was closed and open again for
 next connection
*/



Remarks

For multiple sockets, the main loop is structured in the same way as one socket. The only difference is that pulling method is used to function all sockets, therefore delay for sending message is significantly noticeable. The sample code are only for demonstration, users are free to use any other method to do open multiple sockets(interrupt for example). For the sample code of multiple sockets, only two sockets are allowed by default. Users are free to change it if necessary.