FRDM K64F + FreeRTOS + LwIP - Hard fault

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FRDM K64F + FreeRTOS + LwIP - Hard fault

1,319 Views
wailyt
Contributor I

Hello all,

 

Am looking for advice on an issue I am currently facing. Details:

 

Running FreeRTOS + LwIP on FRDMK64F, toolchain is KDS v3.0 and KSDK v1.2.0. Project was created using Processor Expert.

I'm observing that malloc() called from the LwIP code always seems to return NULL. Origin of this observation is because I am running into a hard fault when one of the LwIP sntp functions calls gmtime(). Based on the disassembly listing it seems a malloc() was called in gmtime() and I am suspecting a memory allocation issue.

 

Not quite certain what I may have got wrong in my setup. Am continuing to look into this but any ideas would be useful and much appreciated. Thanks!

 

Heap and stack setup details as follows for review:

 

FreeRTOS config:

/* Heap Memory */

#define configFRTOS_MEMORY_SCHEME                2

#define configTOTAL_HEAP_SIZE                    98304

#define configUSE_HEAP_SECTION_NAME              1

#define configHEAP_SECTION_NAME_STRING           ".m_data_2"

 

 

Linker:

HEAP_SIZE  = DEFINED(__heap_size__)  ? __heap_size__  : 0xA000;

STACK_SIZE = DEFINED(__stack_size__) ? __stack_size__ : 0x4000;

/* Specify the memory areas */

MEMORY {

  m_interrupts          (RX)  : ORIGIN = 0x00000000, LENGTH = 0x00000400

  m_flash_config        (RX)  : ORIGIN = 0x00000400, LENGTH = 0x00000010

  m_text                (RX)  : ORIGIN = 0x00000410, LENGTH = 0x000FFBF0

  m_data                (RW)  : ORIGIN = 0x1FFF0000, LENGTH = 0x00010000

  m_data_2              (RW)  : ORIGIN = 0x20000000, LENGTH = 0x00030000

}

Labels (1)
0 Kudos
1 Reply

396 Views
FreeRTOS_org
Contributor IV

FreeRTOS provdes 5 different heap memory allocation schemes, although heap_2 is somewhat redundant now.  Which one are you using?  If you are using heap_3 then FreeRTOS will also use malloc and free (although they will be called in a thread safe way), and configTOTAL_HEAP_SIZE will not be used.  If you are using any other (heap_1, heap_2, heap_4 or heap_5) then FreeRTOS will not call malloc and free and configTOTAL_HEAP_SIZE will be used.  Its simplest to either have all memory allocation use malloc and free, or all memory allocation use the FreeRTOS memory allocators.

If lwIP is calling malloc and free directly then it is unlikely to be thread safe (but I don't know for sure, maybe they have something in there to make it ok).  You can direct the malloc and free to the FreeRTOS versions on the liker command line, or by providing malloc and free implementations in your code that then calls the FreeRTOS versions.  You can then set the heap allocated by the linker to near zero, and simplify the memory configuration, and probably save RAM.

Once using the FreeRTOS versions you can define configASSERT(), which can help track some allocation problems, but not all, and define a malloc failed hook to get an immediate notification of allocation failures.