Interesting information | Is the size of the RTOS task stack related to the amount of code?
A friend asked this question: I have a task with a lot of code. Do I need to allocate a large stack for this task?
In fact, the larger the amount of code, the more stack space is allocated. It mainly depends on the "temporary variables" contained in your task.
1 RTOS task stack allocation
// 任务优先级
#define TASK_CHECK_PRIO 6
// 任务堆栈大小
#define TASK_CHECK_STK_SIZE 128
// 堆栈
OS_STK TaskCheckStk[TASK_CHECK_STK_SIZE];
// 创建任务 - 信号检测
OSTaskCreateExt((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));
// 任务应用实现
void AppTaskCheck(void *p_arg)
{
// 代码···
(void)p_arg;
for(;;)
{
// 代码···
}
}
#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);
QueueHandle_t xCLIRcvQueue = NULL;
/* 创建队列 */
if(xCLIRcvQueue == NULL)
{
xCLIRcvQueue = xQueueCreate(CLI_QUEUE_NUM, CLI_PACKAGE_LEN);
}
This is the allocation stack for creating tasks (or queues). As for the specific amount of allocation, it depends on your actual situation. I will describe it in the following chapters.
2 Task code amount
// 任务应用实现
void AppTaskCheck(void *p_arg)
{
// 代码···
(void)p_arg;
for(;;)
{
// 代码···
}
}
There may be thousands of lines of code written here, or hundreds of functions called, each of which contains a lot of code.
In this way, the amount of code for this task is very large.
3 Is there a relationship between the amount of task code and the stack size?
Answer: It does not necessarily require a large stack space, and the amount of task code is not directly related to the stack.
Many beginners may have this misunderstanding: saving a task means saving all the code of the task (in the stack).
The stack mainly saves the variables (control blocks) of the task itself, as well as temporary variables and other key variable information, rather than saving all the code.
4 How big is the appropriate size for stack allocation?
void AppTaskCheck(void *p_arg)
{
static uint8_t aaa; //静态局部变量
(void)p_arg;
for(;;)
{
// 代码···
}
}
Recommended reading
Add WeChat and reply " join group"
Invite you to join the technical exchange group!
Domestic chips|Automotive electronics|Internet of Things|New energy|Power supply|Industry|Embedded...
Reply to any content you want to search in
the
official
, such as question keywords, technical terms, bug codes, etc.,
and you can easily get feedback on related professional and technical content
. Go and try it!
If you want to see our articles more often, you can go to our homepage, click the "three dots" in the upper right corner of the screen, and click "Set as Star".
Welcome to scan the QR code to follow us