Follow+Star Public Account, don’t miss out on exciting content
WeChat Public Account | strongerHuang
Many students have such doubts: How much stack space should I allocate when creating a task? If one of my tasks has a larger code size, should I allocate more stack space?
Below, I will describe the relevant content regarding task code size and stack allocation.
RTOS Task Stack Allocation
Many RTOS tasks on the market require stack size to be allocated in advance, meaning that the stack size is determined when the task is created.
For example, in uCOS, to create a check task:
// Task priority#define TASK_CHECK_PRIO 6
// Task stack size#define TASK_CHECK_STK_SIZE 128
// StackOS_STK TaskCheckStk[TASK_CHECK_STK_SIZE];
// Create task - signal detectionOSTaskCreateExt((void (*)(void *)) AppTaskCheck, (void *) 0, (OS_STK *)&TaskCheckStk[TASK_CHECK_STK_SIZE-1], (INT8U ) TASK_CHECK_PRIO, (INT16U ) TASK_CHECK_PRIO, (OS_STK *)&TaskCheckStk[0], (INT32U ) TASK_CHECK_STK_SIZE, (void *) 0, (INT16U )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
// Task application implementationvoid AppTaskCheck(void *p_arg){ // Code···
(void)p_arg;
for(;;) { // Code··· }}
Creating a task in FreeRTOS is similar; the stack size is also allocated at creation:
#define TASK_CHECK_PRIO 6#define TASK_CHECK_STK_SIZE 128
BaseType_t xReturn;
xReturn = xTaskCreate(AppTaskCheck, "AppTaskCheck", TASK_CHECK_STK_SIZE, NULL, TASK_CHECK_PRIO, NULL);
In addition to the stack, things like message queues and mailboxes also require stack allocation in advance.
For example, to create a CLI message queue in FreeRTOS:
#define CLI_QUEUE_NUM 256 // Number of CLI receive queues#define CLI_PACKAGE_LEN 2 // CLI data packet length
QueueHandle_t xCLIRcvQueue = NULL;
/* Create queue */if(xCLIRcvQueue == NULL){ xCLIRcvQueue = xQueueCreate(CLI_QUEUE_NUM, CLI_PACKAGE_LEN);}
This is the stack allocation for creating tasks (or queues). As for how much to allocate, it depends on your actual situation, which I will describe in the next section.
The code size of a task is the code that you call within that task.
For example, in the code from the previous example:
// Task application implementationvoid AppTaskCheck(void *p_arg){ // Code···
(void)p_arg;
for(;;) { // Code··· }}
This may include thousands of lines of code or call hundreds of functions, each containing a lot of code.
Thus, the code size of this task can become quite large.
Is there a relationship between task code size and stack size?
Many people have a doubt:When a task is suspended, it needs to temporarily save the task in the stack. If the code size of this task is large, shouldn’t it require a larger stack space?
Answer:Not necessarily; the task code size and stack size do not have a direct relationship.
Many beginners may have a misconception: saving a task means saving all the task’s code (in the stack).
The stack primarily saves the task’s own variables (control blocks), as well as temporary variables and other key variable information, but does not need to save all the code.
How much stack should be allocated?
The task stack size mainly depends on the number of temporary variables in your task.
Note: Temporary variables include all temporary variables in nested functions within your code.
For processors with relatively large RAM resources, you can allocate a bit more stack resources.
However, many times, our RAM resources are relatively tight. At this time, you need to balance comprehensively.
For example, static local variables:
void AppTaskCheck(void *p_arg){ static uint8_t aaa; // Static local variable
(void)p_arg;
for(;;) { // Code··· }}
The variable aaa here will not occupy the stack space of that task, but it will occupy global variable (RAM) space.
Choosing between static local variables and temporary variables depends on your project’s specific situation, such as RAM resources, code execution efficiency, etc. (temporary variables will also involve a data copy process).
So, how to allocate the stack, whether to use static or temporary variables, needs to be considered based on your project’s situation.
———— END ————

●Column “Embedded Tools”
●Column “Embedded Development”
●Column “Keil Tutorial”
●Selected Tutorials in the Embedded Column
Follow the public accountReply “Join Group” to join the technical exchange group as per the rules, reply “1024” to see more content.
Click “Read Original” to see more shares.