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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

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

1,521 次查看
wesleyxie
Contributor III

Hi, NXP guys

khumphriMandarDeactivated userdhavalRHinnenjimtrudeauMartinvladcenteamarc.paquetteRChapman

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 ?

QQ截图20171231012021.png

Thanks , and best regards.

/WX

0 项奖励
3 回复数

1,112 次查看
jiri_kral
NXP Employee
NXP Employee

I find out similar question on Kinetis community - https://community.nxp.com/thread/358984 

Hope it helps. 

Jiri 

0 项奖励

1,112 次查看
wesleyxie
Contributor III

Hi, Jiri

thanks, implement a own sbrk(remove the stack pointer checking) to replace the one in EWL libc fixed my issue.

but does NXP have plan to fix it in Beta SDK version ? anyway it is a bug in EWL lib I think. 

/WX

0 项奖励

1,112 次查看
jiri_kral
NXP Employee
NXP Employee

Hi, 

you can enable user heap allocation by setting to 1 this config define in FreeRTOS.h file:

 

#ifndef configAPPLICATION_ALLOCATED_HEAP
#define configAPPLICATION_ALLOCATED_HEAP 0
#endif

Here are more details - FreeRTOS - The Free RTOS configuration constants and configuration options - FREE Open Source RTOS f... 

Anyway -  usage standard malloc inside FreeRTOS tasks is not good idea - you can find more info here - FreeRTOS - Memory management options for the FreeRTOS small footprint, professional grade, real time... 

Jiri

0 项奖励