Does the Stack Size of RTOS Tasks Relate to Code Volume?

Follow+Star PublicNumber, don’t miss wonderful content

Does the Stack Size of RTOS Tasks Relate to Code Volume?

Author | strongerHuang

WeChat Public Account | Embedded Column

Recently, a friend asked a question: I have a lot of code in a task, does this task need to allocate a large stack?

Below, I will describe the relevant content around task code volume and stack allocation.

1RTOS Task Stack Allocation

Many RTOS tasks on the market require the stack size to be allocated in advance, which means allocating the stack size when creating the task.
For example, creating a Check task in uCOS:
// 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, where 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, message queues, message mailboxes, etc., also need to allocate stacks in advance.
For example, creating a CLI message queue in FreeRTOS:
#define CLI_QUEUE_NUM             256                      // Number of CLI receive queues#define CLI_PACKAGE_LEN           2                        // Length of CLI data packets
QueueHandle_t xCLIRcvQueue = NULL;
/* Create queue */if(xCLIRcvQueue == NULL){  xCLIRcvQueue = xQueueCreate(CLI_QUEUE_NUM, CLI_PACKAGE_LEN);}

This is the allocation of stack when creating tasks (or queues), and how much to allocate depends on your actual situation, which I will describe in the following sections.

2Task Code Volume

The code volume of a task is the code you call within that task.
For example, the code in the above example:
// Task application implementationvoid AppTaskCheck(void *p_arg){  // code···
  (void)p_arg;
  for(;;)  {    // code···  }}

There might be thousands of lines of code written here, or hundreds of functions called, each containing a significant amount of code.

As a result, the code volume of this single task can be quite large.

3Is There a Relationship Between Task Code Volume 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 task’s code volume is large, shouldn’t it need a large stack space?

Answer:Not necessarily. The task’s code volume does not have a direct relationship with the stack.

Many beginners may have a misunderstanding: saving a task means saving all the task’s code (in the stack).

The stack mainly saves the task’s own variables (control blocks), as well as temporary variables and other key variable information, rather than saving all the code.

4How Much Stack to Allocate?

The size of the task stack mainly depends on the number of temporary variables in your task.
Note: Temporary variables include all temporary variables in your nested functions.
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 aaa variable here will not occupy the task’s stack space, but it will occupy global variable (RAM) space.
Using static local variables or temporary variables depends on your project’s specific situation, such as: RAM resources, code execution efficiency, etc. (Temporary variables will also have a data copy process).
Therefore, how to allocate the stack and whether to use static or temporary variables needs to be considered based on your project’s situation.
———— END ————

Reply with ‘Embedded Software Design and Development’ to read more related articles.

Welcome to follow my public account, reply ‘Join Group’ to join the technical exchange group according to the rules, reply ‘1024’ to see more content.
Welcome to follow my video account:

Does the Stack Size of RTOS Tasks Relate to Code Volume?

Click ‘Read the Original’ for more sharing, and welcome to share, collect, like, and view.

Leave a Comment

×