WIZnet Developer Forum

could not able to open socket

Hi,

I am using stm32f105vct MCU and w5500 to establish ethernet communication using UDP protocol.
I am able to communicate b/w MCU and W5500 ic using SPI but when i tried to initialise socket for udp protocol , socket is always closed.I tried with all the sockets 0 to 8 but same problem.
I am getting PHYCFGR register value as 0xBB.
I have pasted my program below. Please help me to fine out the probelm .

Program:

uint8_t gate_way={0,0,0,0},sub_net={255,255,255,0},my_ip={192,168,0,6},dest_ip={192,168,0,2};
uint8_t udp_packets={10,20,30,40,50,60,70,80,90,100},read_data=0;
uint16_t prt=5000,dprt=5000;

int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* Configure the system clock */
SystemClock_Config();

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI2_Init();
MX_UART4_Init();

//UDP PROTOCOL*//
//Initialization

setSn_CR(0, Sn_CR_CLOSE);
setSn_CR(1, Sn_CR_CLOSE);
setSn_CR(2, Sn_CR_CLOSE);
setSn_CR(3, Sn_CR_CLOSE);
setSn_CR(4, Sn_CR_CLOSE);
setSn_CR(5, Sn_CR_CLOSE);
setSn_CR(6, Sn_CR_CLOSE);
setSn_CR(7, Sn_CR_CLOSE);

setGAR(gate_way);
setSUBR(sub_net);
setSIPR(my_ip);

setMR(MR_RST);
HAL_Delay(1000);

START:
// OPEN Socket4 Mode UDP MYPORT 5000
setMR(Sn_MR_UDP);
setSn_PORT(4, prt);
setSn_CR(4, Sn_CR_OPEN);

read_data= getSn_SR(4);

if(read_data !=SOCK_UDP)
{
setSn_CR(4, Sn_CR_CLOSE);
goto START;
}
//SET DESTIP 192.168.0.2 DESTPORT 7760
setSn_DIPR(4, dest_ip);
setSn_DPORT(4, dprt);

while (1)
{
wiz_send_data(4,udp_packets,sizeof(udp_packets));
setSn_CR(4, Sn_CR_SEND);
HAL_Delay(1000);
}

}

Thanks in Advance

Try configuring your IP address, gateway & subnet AFTER resetting the chip rather than before. Sending the reset command (setMR(MR_RST)) resets all registers to default values, undoing your configuration.

Dears!

I think you can not follow the guide line of command & reset processing.
A command shoud be checked to clear for completing the command process.

Anyway, Your code has many unuseful block. I modified as following.

// ***********UDP PROTOCOL*********** *//
//Initialization

//setSn_CR(0, Sn_CR_CLOSE);
//setSn_CR(1, Sn_CR_CLOSE);
//setSn_CR(2, Sn_CR_CLOSE);
//setSn_CR(3, Sn_CR_CLOSE);
//setSn_CR(4, Sn_CR_CLOSE);
//setSn_CR(5, Sn_CR_CLOSE);
//setSn_CR(6, Sn_CR_CLOSE);
//setSn_CR(7, Sn_CR_CLOSE);

setMR(MR_RST);   // Move it up.
HAL_Delay(1000);

setGAR(gate_way);
setSUBR(sub_net);
setSIPR(my_ip);

//setMR(MR_RST);
//HAL_Delay(1000);

START:
// OPEN Socket4 Mode UDP MYPORT 5000
setMR(Sn_MR_UDP);
setSn_PORT(4, prt);
setSn_CR(4, Sn_CR_OPEN);
while(getSn_CR(4) !=0); // should be wait for command clearing

while( (read_data= getSn_SR(4)) != SOCK_UDP);
//SET DESTIP 192.168.0.2 DESTPORT 7760
setSn_DIPR(4, dest_ip);
setSn_DPORT(4, dprt);

while (1)
{
     wiz_send_data(4,udp_packets,sizeof(udp_packets));
     setSn_CR(4, Sn_CR_SEND);
     while(getSn_CR(4) !=0);
     //HAL_Delay(1000);
     while((getSn_IR(4) & Sn_IR_SENDOK) != Sn_IR_SENDOK)
     { 
           if(getSn_IR(4) & Sn_IR_TIMEOUT) 
          {
               printf ("Send fail");
               break;
           }
     }
}
}

}

Most of all, I recommend you should be used with ioLibrary because you own driver code is not proved.
ioLibrary can be donwloaded at https://github.com/Wiznet/ioLibrary_Driver

Thank you.

Thank You for your reply,

I have corrected my code as per your suggestion but still i am facing the same problem.
I would like to inform you that I am using w5500.h and w5500.c from standard IO library prescribed by wiznet.io,(https://github.com/Wiznet/ioLibrary_Driver).

Sorry. My recommended code missed code to clear Sn_IR_SENDOK & Sn_IR_TIMEOUT.

while (1)
{
     wiz_send_data(4,udp_packets,sizeof(udp_packets));
     setSn_CR(4, Sn_CR_SEND);
     while(getSn_CR(4) !=0);
     //HAL_Delay(1000);
     while((getSn_IR(4) & Sn_IR_SENDOK) != Sn_IR_SENDOK)
     { 
           if(getSn_IR(4) & Sn_IR_TIMEOUT) 
          {
               printf ("Send fail");
               setSn_IR(4, Sn_IR_TIMEOUT);
               return SOCKERR_TIMOUT;
           }
     }
}
setSn_IR(4, Sn_IR_SENDOK);

This code maybe bad code. Why do you use socket.c & socket.h?

For more details, you can refer to sendto() function at socket.c.

Thank you.

@midnightcow,

Thanks for the response.

I have used sendto() func as you said but still it is not working. please find my code below.

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"
#include "wizchip_conf.h"
#include "socket.h"
#include <string.h>

#define GREETING_MSG 		 "Well done guys! Welcome to the IoT world. Bye!\r\n"


SPI_HandleTypeDef 	hspi2;
UART_HandleTypeDef  huart2;
char msg[60];
uint8_t dest_ip[]={192,168,0,6};
uint16_t dest_port=5000;
void SystemClock_Config(void);

void cs_sel() {
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); //CS LOW
}

void cs_desel() {
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); //CS HIGH
}

uint8_t spi_rb(void) {
	uint8_t rbuf;
	HAL_SPI_Receive(&hspi2, &rbuf, 1,1);
	return rbuf;
}

void spi_wb(uint8_t b) {
	HAL_SPI_Transmit(&hspi2, &b, 1, 1);
}
int main(void)
{
  
	uint8_t retVal, sockStatus;
	int16_t rcvLen;
	uint8_t rcvBuf[20], bufSize[] = {2, 2, 2, 2};

 
  HAL_Init();
  SystemClock_Config();

  
  MX_GPIO_Init();
  MX_SPI2_Init();
  MX_USART2_UART_Init();
 

   reg_wizchip_cs_cbfunc(cs_sel, cs_desel);
   reg_wizchip_spi_cbfunc(spi_rb, spi_wb);

   wizchip_init(bufSize, bufSize);
   wiz_NetInfo netInfo = { .mac 	= {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef},	// Mac address
                           .ip 	= {192, 168, 0, 2},					// IP address
                           .sn 	= {255, 255, 255, 0},					// Subnet mask
                           .gw 	= {192, 168, 2, 1}};					// Gateway address
   wizchip_setnetinfo(&netInfo);
   wizchip_getnetinfo(&netInfo);
  

 reconnect:
  
   if((retVal = socket(1, Sn_MR_UDP, 5000, 0)) ==1) 
   {
 	  while(1) 
	  {
 			  
 			  if(sockStatus = getSn_SR(1) == SOCK_UDP) 
			  {
			  /* Let's send a welcome message and closing socket */
 				  if(retVal = sendto(1, GREETING_MSG, strlen(GREETING_MSG),dest_ip,dest_port) == (int16_t)strlen(GREETING_MSG))
 					  PRINT_STR(SENT_MESSAGE_MSG);
 				  else {
 					  snprintf(msg, WRONG_RETVAL_MSG, retVal);
 					  PRINT_STR(msg);
 				  }
 				  break;
 			  }
 			  else { 
 				  snprintf(msg, WRONG_STATUS_MSG, sockStatus);
 				  PRINT_STR(msg);
 				  break;
 			  }
 			
 	}

   } else { 
 	  snprintf(msg, WRONG_RETVAL_MSG, retVal);
 	  PRINT_STR(msg);
   }

   /* We close the socket and start a connection again */
   
   close(1);
   goto reconnect;
 
  
  
}

Sorry . Your code hasn’t problem.

Give me more information.
thank you.

@midnightcow
Thanks for the reply.
I am using Nucleo STM32F103RB development board and w5100 Ethernet shield .
STMcubeMX & Atollic true studio for coding ,also I am using wireshark tool to monitor the data.
Hardware configuration in Nucleo board.
1.Peripheral: SPI2
2.Pins : PB13,PB14,PB15 & PB12
3.Baud_rate: 18MBits/sec.

The code which i have got from following link is modified for my board and it is working for TCP, but the same code is modified for UDP it is not working which i already shared with you.
Please find the link below.

Dear Yogesh!

When you have worked well with TCP socket, Your code has not a problem to access chip registers & memory.

And your shared UDP code hasn’t problem.

Can I check your runinig log message?

Is there the wireshark catured any packet such as ARP packets?

And In this situation, Can you try to ping request to W5100?

Anyway, Regardless of this problem I found your gateway ip address is wrong.
Gateway IP has to have subnetmask value.

Thank you.

I have shared data packets captured from wireshark tool, also i changed Gateway IP value but still not working.

Thank you,

Hi Yogesh!

The captured file is not help for me. The file doesn’t have any information to solve problem.

I checked again your udp code and tcp code.
I think your code maybe have some logical problem.

The operator such as (), = and ==, maybe ambiguous ordered.
So I will suggest to you explict order by using ().

For example,
if(sockStatus = getSn_SR(1) == SOCK_UDP) ==> if((sockStatus = getSn_SR(1)) == SOCK_UDP)

Thank you.

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