xTaskCreateStatic() stack_buffer

Hi all,
i am totally new to FreeRTOS. Please excuse me if the query below sounds silly.

xTaskCreateStatic() function accepts as parameter a StackType_t stackbuffer[], which the newly created task is going to use as it’s stack. i have the following function -

createNewThread(Function_t, unsigned int stackSize, void * function_parameter)

Function_t - The function to run as the new task.
stackSize - The size of the stackbuffer to be sent t xTaskCreateStatic()
function_parameters - parameter required the Function_t

From this function i want to call xTaskCreateStatic() and provide it with a static array StackType_t stackbuffer[] of size unsigned int stackSize. But declaring static array of non static size is not possible. i cannot declare an array like this - static StackType_t stackbuffer[stackSize] inside the createNewThread().

I want to know that if i create a stackbuffer of maximum size required for the application and pass it to xTaskCreateStatic() and with it in the ulStackDepth parameter of xTaskCreateStatic() i pass the stackSize (received as parameter in createNewThread()`` ) that the newly created task should use as its stack, will it work properly or not.
I mean will the task be created in such a way that it will only use that amount of the stack which is specified by the parameter ulStackDepth?
If not then it would be great if any one can provide me with a suggestion to handle this situation.

Read the comprehensive FreeRTOS docs e.g. StackSize and you’ll know :wink: Beware the StackType_t !
Since you obviously want to use static task resources you have to provide them. But your createNewThread abstraction implies the opposite except you allocate required stack and task buffer dynamically in createNewThread and provide it to xTaskCreateStatic accordingly.

The stack buffer provided can be bigger than needed, you will just waste resources.

But, the whole intent of the create-static functions is to allow the preallocation at compile time of the resources, so making them run time variable doesn’t really make sense.

Note also, you will only be able to call this functions once, as you can’t use that static memory buffer for another task (unless the first one has deleted itself)

Hello Richard,

Thank you so much for replying. i got your point and it was very helpful.