AnsweredAssumed Answered

S32DS for PA 2017.R1: failed to malloc() if called from FreeRTOS task context

Question asked by wesley xie on Dec 30, 2017
Latest reply on Jan 20, 2018 by wesley xie

Hi, NXP guys

Kirk HumphriesMandar JoshiThe specified item was not found.Dhaval ShahRHinnenJames TrudeauMartin Wennerstromvcentea1 vcentea1Marc PaquetteRuth Hendrix

 

in S32DS for PA 2017.R1 version, with EAR SDK 0.8.1.

if I create a FreeRTOS task by xTaskCreate()/xTaskCreateStatic(),, and call malloc() to allocate some memory in the task context, the malloc() return NULL.

/* example code */

 

// create task

xTaskCreate(TaskFunc, "Task", 1024, NULL, 10, NULL);

 

// task function

void TaskFunc(void *pvParameters)

{

   void *p = malloc(16);

   if (p == NULL)

   {

      // malloc failed

   } 

 

   for( ;; )

   {

      vTaskDelay(1000);

   }

}

 

after some debugging, I firgured out that this is caused by the sbrk() implementation in S32DS/e200_ewl2/EWL_C/src/stdlib/alloc.c.

 

sbrk compare the heap pointer with the stack pointer, but in FreeRTOS task, the stack is always located before heap.

I tried below methods:

1. use xTaskCreate to create task, FreeRTOS internally using pvPortMalloc() to allocate the stack.

    pvPortMalloc() allocate the stack memory in ucHeap[] (in bss).

2. use malloc() to allocate task stack and pass into xTaskCreateStatic()

    the stack pointer (in heap) is before the "heap_end" when I called malloc() in task context.

3. define stack on bss RAM region, and pass into xTaskCreateStatic()

    bss is located before heap, that is the usual way we link program.

 

so sbrk failed at this compare and return -1.

 

I know FreeRTOS provide another alloc function pvPortMalloc(), but I have to use malloc() in some cases.

how can I fix it ?

 

 

Thanks , and best regards.

 

/WX

Outcomes