cancel
Showing results for 
Search instead for 
Did you mean: 

HardFault error using lwIP TCP stack and freeRTOS!

Mujtaba
Senior

Dear friends,

I am trying to create a TCP server using lwIP and netconn APIs for STM32F407VGt6 MCU and recieve data from a PC software program which acts as a TCP client.

The MCU recieves all the data correctly from PC software in multiple packets and after finishing data transfer, a HardFault error occurs.

The TOTAL_HEAP_SIZE of freeRTOS is 15360 bytes and the TCP thread stack size is 256 words.

I am using CubeMX version 4.25 and HAL firmware version 1.21.0.

The code I wrote is as follows:

/* TCP_Server_Task function */

void TCP_Server_Task(void const * argument)

{

 /* init code for LWIP */

 MX_LWIP_Init();

 /* USER CODE BEGIN 5 */

 struct netconn *conn, *newconn;

 err_t err, accept_err;

 struct netbuf *buf;

 void *data;

 u16_t len;

 uint8_t j;

 uint8_t *tcp_rx_buf;

 uint8_t length;

 LWIP_UNUSED_ARG(argument);

 W25Qxx_Init();

 W25Qxx_SectorErase(W25QXX_BLOCK_1_ADDR);

 W25Qxx_SectorErase(W25QXX_BLOCK_2_ADDR);

 write_addr = W25QXX_BLOCK_2_ADDR;

 /* Create a new connection identifier. */

 conn = netconn_new(NETCONN_TCP);

 if (conn!=NULL)

 { 

   /* Bind connection to well known port number 502. */

   err = netconn_bind(conn, IP_ADDR_ANY, 36000);

   if (err == ERR_OK)

   {

     /* Tell connection to go into listening mode. */

     netconn_listen(conn);

     while (1)

     {

       /* Grab new connection. */

        accept_err = netconn_accept(conn, &newconn);

       /* Process the new connection. */

       if (accept_err == ERR_OK)

       {

         while (netconn_recv(newconn, &buf) == ERR_OK)

         {

           do

           {

             netbuf_data(buf, &data, &len);

                     tcp_rx_buf = (uint8_t *)data;

                     length = len-1;

                     if(tcp_rx_buf[0] == FC_CONFIG_PROJ)

                     {

                      W25Qxx_Write(W25QXX_BLOCK_1_ADDR,&tcp_rx_buf[1],length);   

                     }

                     else if(tcp_rx_buf[0] == FC_GROUP_WRITE)

                     {

                        for(j=byte_cnt ; j<len ;j++)

                        {

                           rcv_data[j] = tcp_rx_buf[j];

                        }   

                       W25Qxx_Write(write_addr,&tcp_rx_buf[1],length);

                        W25Qxx_Read(write_addr,rcv_buff,length);

                        write_addr += length;

                        byte_cnt += length;

                     }

                     else if(tcp_rx_buf[0] == FC_PROGRAM_DONE)

                     {

                        osSignalSet(W25Qxx_ThreadHandle,1);

                        length = 0;

                        byte_cnt = 0;

                        write_addr = W25QXX_BLOCK_2_ADDR;

                     }

             netconn_write(newconn, data, len, NETCONN_COPY);

           }

           while (netbuf_next(buf) >= 0);

           netbuf_delete(buf);

         }

         /* Close connection and discard connection identifier. */

         netconn_close(newconn);

         netconn_delete(newconn);

       }

     }

   }

   else

   {

     netconn_delete(conn);

   }

 }

 /* USER CODE END 5 */

}

Any helps would be appreciated.

Mutaba

4 REPLIES 4
AvaTar
Lead

What do the SCB registers say about the hardfault reason ?

Have you tried to narrow down the exact location, perhaps with instrumenting your code ?

Mujtaba
Senior

Dear AvaTar,

thanks for your quick reply.

Yes I have checked it. HardFault is caused by MemMange and when I checked the CFSR register, the instruction access violation (IACCVIOL) flag was set.

The problem is I cannot find where exactly this happen and why.

Unfortunately the board I am using does not have a trace pin, so I cannot use trace feature.

Could you give me any ideas how to solve this problem?

AvaTar
Lead

JTAG/SWD based tracing, or an extra UART. In many cases, a single GPIO pin for toggling works.

Or you try the debugger.

> ...and after finishing data transfer, a HardFault error occurs.

I would start with the specific code parts involved.

I'm not much in Cube/RTOS code, but it sounds like a "stable" problem. Meaning consistent error at a single location.

Perhaps trying to access released/invalidated memory ?

Perhaps instrument malloc/free to make sure you don't have a memory leak, try to free memory twice, etc. Use a Hard Fault​ Handler that outputs diagnostic information.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..