cancel
Showing results for 
Search instead for 
Did you mean: 

TCP Client Raw API

MGogr.1
Associate III

Hello,

I have STM32F767ZI and STM32F429ZI Nucleo board, which I want to configure in TCP Client mode. I have attached .ioc file and tcpclient.c and ethernetif.c files. This files I have downloaded from internet. Now here is my problem when I try to run this code my board is not getting connected to server. For server I am using Hercules software. My board/s are getting from ping response, but not getting connected to server.

 

 

 

void tcp_echoclient_connect(void)
{
  ip_addr_t DestIPaddr;
  
  /* create new tcp pcb */
  echoclient_pcb = tcp_new();
  
  if (echoclient_pcb != NULL)
  {
    IP4_ADDR( &DestIPaddr, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
    
    /* connect to destination address/port */
    tcp_connect(echoclient_pcb,&DestIPaddr,DEST_PORT,tcp_echoclient_connected);
  }
}

/**
  * @brief Function called when TCP connection established
  *  tpcb: pointer on the connection contol block
  *  err: when connection correctly established err should be ERR_OK 
  * @retval err_t: returned error 
  */
static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
  struct echoclient *es = NULL;
  
  if (err == ERR_OK)   
  {
    /* allocate structure es to maintain tcp connection informations */
    es = (struct echoclient *)mem_malloc(sizeof(struct echoclient));
  
    if (es != NULL)
    {
      es->state = ES_CONNECTED;
      es->pcb = tpcb;
      
      sprintf((char*)data, "sending tcp client message %d", (int)message_count++);
        
      /* allocate pbuf */
      es->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char*)data) , PBUF_POOL);
         
      if (es->p_tx)
      {       
        /* copy data to pbuf */
        pbuf_take(es->p_tx, (char*)data, strlen((char*)data));
        
        /* pass newly allocated es structure as argument to tpcb */
        tcp_arg(tpcb, es);
  
        /* initialize LwIP tcp_recv callback function */ 
        tcp_recv(tpcb, tcp_echoclient_recv);
  
        /* initialize LwIP tcp_sent callback function */
        tcp_sent(tpcb, tcp_echoclient_sent);
  
        /* initialize LwIP tcp_poll callback function */
        tcp_poll(tpcb, tcp_echoclient_poll, 1);
    
        /* send data */
        tcp_echoclient_send(tpcb,es);
        
        return ERR_OK;
      }
    }
    else
    {
      /* close connection */
      tcp_echoclient_connection_close(tpcb, es);
      
      /* return memory allocation error */
      return ERR_MEM;  
    }
  }
  else
  {
    /* close connection */
    tcp_echoclient_connection_close(tpcb, es);
  }
  return err;
}

 

 

 

  My code never enters tcp_echoclient_connected(). I am stuck on this from last few days and very little information is available.

I am not using FreeRTOS.

Can you please tell me what am I doing wrong?

Any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

So I have solved this issue. I modified my .ioc file; Firstly I enabled FreeRTOS. I also enabled Raw API from Lwip configurations.

I also had to switch to Cube IDE from IAR (don't know for what reason, but the same code is not working in IAR Environment).

View solution in original post

3 REPLIES 3
MS.9
Associate III

The ethernetif.c code attached shows a MII interface not the RMII, which the Nucleo has. Its also showing the DP83848_PHY_ADDRESS, but the nucleo has a LAN8742. Try looking in the HAL example/CubeMX code for a better offering.

(The HAL examples have something that is nearly what you are trying to do - that will be a good start point into which to add your stuff)

Also double checked the memory configuration.

Take a look at

"How to create a project for STM32H7 with Ethernet and LwIP stack working" by

 @Adam BERLINGER

 

Cheers

Mark

Sorry, I have attached wrong file for ethernetif.c. I have updated post with right file.

So I have solved this issue. I modified my .ioc file; Firstly I enabled FreeRTOS. I also enabled Raw API from Lwip configurations.

I also had to switch to Cube IDE from IAR (don't know for what reason, but the same code is not working in IAR Environment).