-2

I'm currently working on freertos with stm32f4. After creating the project with cubemx with below configration.

Its seem RTOS has around 25k bytes for me to allocate stacks to threads. But somehow when i create thread with stack size 1000. It has only 20888 bytes left for total heap of RTOS. If i allocate 2000. It got 16888 left. It seems like it always consumes 4 times of stack size allocation. Really really confused about what is happening.

osThreadDef(Task_Embedded, Task_VATEmbedded, osPriorityNormal, 0, 1000);


 VATEmbeddedTaskHandle = osThreadCreate(osThread(Task_Embedded), NULL);



osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument)
{
 TaskHandle_t handle;

  if (xTaskCreate((TaskFunction_t)thread_def->pthread,(const portCHAR *)thread_def->name,
              thread_def->stacksize, argument, makeFreeRtosPriority(thread_def->tpriority),
              &handle) != pdPASS)  {
    return NULL;
  }

  return handle;
}
glts
  • 21,808
  • 12
  • 73
  • 94
Pawan
  • 111
  • 1
  • 4
  • 15

2 Answers2

0

Looking at CMSIS man

Configuration of Thread count and Stack Space

osThreadDef defines a thread function.

The parameter stacksz specifies thereby the stack requirements of this thread function. CMSIS-RTOS RTX defines two methods for defining the stack requirements:

when stacksz is 0, a fixed-size memory pool is used to for the thread stack. In this case OS_STKSIZE specifies the stack size for the thread function. when stacksz is not 0, the thread stack is allocated from a user space. The size of this user space is specified with OS_PRIVSTKSIZE.

(Emphasis mine.)

Where OS-PRIVSTKSIZE tells

Is the combined stack requirement (in words) of all threads that are defined with with osThreadDef stacksz != 0 (excluding main).

(Emphasis mine.)

Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61
  • i still dont get it. i thought the stack is allocated by the total heap of rtos. and the heap size seems to decrease as i allocate it to thread. – Pawan Aug 04 '16 at 08:48
  • 1
    The man says **in words**, that I guess (i'm pretty sure) is 32 bits on your platform. – LPs Aug 04 '16 at 08:52
  • i thought "stacksz" is the stack size that i give to this thread. – Pawan Aug 04 '16 at 08:52
  • Yes, it is but **in words** not **in bytes** – LPs Aug 04 '16 at 08:53
  • just curious about it. In the thread, local variable(in the function which called by thread) is storing in the thread stack. what if i use malloc instead? does it also storing in the stack? it seems like it still cause a stack overflow if memory allocation is over stack size. – Pawan Aug 04 '16 at 09:10
  • You should post a new question for this. BTW short answer: Local scoped variable (the pointer) is allocated into stack, `malloc`ated byte are placed into heap. – LPs Aug 04 '16 at 09:14
  • what heap do you refer to? is it the heap of the platform, or the heap that allocated to rtos(in my case, that 25k)? sorry man. too much questions. – Pawan Aug 04 '16 at 09:18
  • 1
    IIRC it depends on memory used by RTOS: heap_1, heap_2 and so on. Better to post a new question to have specific details about it from experts. – LPs Aug 04 '16 at 09:21
0

The FreeRTOS API is online, and the description of the usStackDepth parameter to the xTaskCreate() function clearly states the stack is defined in words, not bytes. FreeRTOS runs on 8, 16 32 and 64-bit processors, so the word size is dependent on the architecture - in your case it is 4, which matches your observation. http://www.freertos.org/a00125.html

Richard
  • 3,081
  • 11
  • 9