lwIP memory question with FreeRTOS

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everyone:

 

I have SAME54 board with code derived from AtmelStart lwip + FreeRTOS.

 

I'm trying to send / receive UDP data in one task, and TCP data from another task.

It works fine much of the time, but I think lwIP memory gets screwy, and then I see problems, when the TCP and UDP tasks are calling the lwip memory functions at the same time.

 

So - basic question: a quick scan of my code shows that I'm using the following functions in the two tasks:

 - netbuf_new

 - netbuf_alloc

 - netbuf_data

 - netbuf_next

 - netbuf_delete

 

Is it OK to use these functions from multiple tasks?

 

Any suggestions on debugging this kind of issue in lwIP greatly appreciated.

 

Thanks!!

This topic has a solution.

pondzone

Last Edited: Thu. May 23, 2019 - 05:05 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As long as those functions are not using static buffers and/or utilizing Hardware functions.. Issues will occur if one say Task1 is setting some register values, writing to a static buffer, or writing a UDP packet into a TX register, but then while the packet is transmitting you get a context switch and Task2 begins settings things up and attempts to write a TCP packet.

 

The problems that come from this could be minor like the TCP packet being dropped, or catastrophic and potentially corrupt memory and cause hard faults. Super easy to work around this though since you are using FreeRTOS... Take the time to become familiar with Mutexes and implement one (or several?) to protect or guard your valuable resources (i.e. the Ethernet stack and registers). It will ensure that only one task can use a resource at any given time (And you have full control of saying when a task is DONE, and someone else is now allowed to use the resource).

 

Mutexes - https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html

Example usage at the bottom of this page -  https://www.freertos.org/a00122.html

 

murph

Debugging - Being a detective in a crime movie where you are also the murderer.

Last Edited: Thu. May 23, 2019 - 01:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the info! I think I have it working, using a FreeRTOS mutex as you suggest.

 

I'm using the netconn api. lwIP v1.4.0.

 

In my case the mutex is used from the two tasks that do the TCP and UDP, in addition to the task that calls ethernetif_mac_input().

It looks to me like all calls that access pbufs, plus calls to functions like netconn_recv, need to be protected by the mutex, so only one task is in there at a time.

 

I also found the stats.c file and the MEM_STATS_DISPLAY() and MEMP_STATS_DISPLAY() for example very helpful. Anyone else reading this and using lwIP - suggest checking that out.

 

pondzone

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Great! Glad to hear that works for you.. Be sure to mark as a solution for others!

murph

Debugging - Being a detective in a crime movie where you are also the murderer.