W5500 TCP transparent bridging

Hello,

I am writing a bare metal application for STM32F411 that uses W5500 to perform data exchange operations with the serial port.

One of these “modes” is a transparent mode where I open a listening TCP port and transfer what I receive to the serial. Consequently, everything that is sent from the serial must also be sent to the socket.

In general the flow is the following:

  1. open serial port and register rx callback
  2. set MAC, static/dinamic IP and stuff on W5500
  3. listen TCP port
  4. an external client connect to TCP port
  5. external client send a command (ex: “:12345”) length is not fixed
  6. W5500 get data and send it to serial port
  7. serial port get bytes and send it back to socket, answer may vary in length and no termination characters are presents
    back to 5. until connection is closed

May happen that socket send a command that has no answer from serial, so it’s possible to send a command again.

My code snippets are the following:

while(1)
      	  			  {

      	  				  if((sockStatus = getSn_SR(CLIENT_SOCKET)) == SOCK_ESTABLISHED)
      	  				  {
      	  					  uint8_t remoteIP[4];
      	  					  uint16_t remotePort;


      	  				    		getsockopt(CLIENT_SOCKET, SO_DESTIP, remoteIP);
      	  				    		getsockopt(CLIENT_SOCKET, SO_DESTPORT, (uint8_t*)&remotePort);

      	  				        	  				    		while(1)
      	      	  				    {

      	      	  							len = recv(CLIENT_SOCKET, (uint8_t *)buffer_rx, MAX_TCP_OUT_BYTE);

      	      	  							if (len < MAX_TCP_OUT_BYTE-1)
      	      	  							{
      	      	  								strncpy( out_string, (char *)buffer_rx, len);
      	      	  								out_string[len] = '\0';
      	      	  								
      	      	  								UART_Printf(&huart2,"%s", out_string);  
      	      	  							}
      	      	  				    }

      	  					  break;
      	  				  }
      	  				  else { 
      	  					  break;
      	  				  }
      	  			  }

Callback for serial

void HAL_UART_RxCpltCallback_mcu(UART_HandleTypeDef *huart2)
{

	HAL_UART_Receive_DMA(huart2, Rx_data_mcu, 1);
	switch (mode)
	{
		case TRANSPARENT:
		{
			  
			  if(sockStatus = getSn_SR(CLIENT_SOCKET) == SOCK_ESTABLISHED)
			  {
				  send(CLIENT_SOCKET, Rx_data_mcu[0], 1);

			  }
			  

			  break;
		}

From socket to serial it works, but from serial to socket not.
I am clearly missing something.
Can anyone give me some help?

Thank you

This is not enough. You must say what does not work (from user or process perspective) and/or how it does not work, as it is expected you have performed initial troubleshooting and research on the issue.

Ok, i try

After open the connection i try to send to W5500 the following string
immagine

The W5500 get correctly the data and send it to the UART

The execution continue looping to the previous blocking instruction, and i think the problem is here.

len = recv(CLIENT_SOCKET, (uint8_t *)buffer_rx, MAX_TCP_OUT_BYTE);

Now if i write data to the UART by opening a serial terminal, i have that the instruction

immagine

	  if(sockStatus = getSn_SR(CLIENT_SOCKET) == SOCK_ESTABLISHED)

assign to sockStatus values that can be “0”, “72” and “23”.

In case the instruction

send(CLIENT_SOCKET, Rx_data_mcu[0], 1);

is executed from the uart rx routine handler, the recv instructions becomes unlocked and return always a not zero length.

I hope I have provided clear information

If recv unblocks then either there’s data received or there is an error.
Before doing anything else please review W5100 datasheet chapter 5 to ensure you know what you are doing, and also see how you can properly do the things.