W5500EVB as TCP Server

This is a sample code for using W5500 EVB as a TCP server. As a TCP server, it stands by until being connected by a TCP client. As TCP states, handshakes would be done before connection actually established between client and server(for more details please refer to Transmission Control Protocol). W5500 allows users to establish 8 sockets at a time. The following sample code demonstrates W5500EVB serves as both one socket server and multiple servers.


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 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. Turn off the firewall if necessary
      5. 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.

uint16 local_port=5000;
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 Port: %d\r\n",local_port);
printf("W5500 Init Complete!\r\n"); printf("Start TCP Server Test!\r\n");
                                                           
                                             
/******************


These are all the necessary
 initialization


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






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


These are messages shown in 
serial window for debugging
 

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

Socket Initialized

The first state after booting/rebooting W5500EVB, socket is opened and waiting for client to connect(listen()).

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


Socket Initialized State waiting for client to connect(listen())
****************/

Socket Established

Once the handshake is done, W5500EVB will change the state from Initial state to Socket Established state, meaning that the client and server(our W5500EVB) 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 data from client, 
1. Get the length of data received 
2. Get and store the received data from W5500’s buffer to MCU’s buffer. 
3. Print the data out to our serial window 
4. Send the data back to client as echo/feedback 

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




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

When we send data to client, 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 client 
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).