Follow+Star PublicNumber, don’t miss wonderful content
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
// 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··· }}
#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);
#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
// 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?
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?
void AppTaskCheck(void *p_arg){ static uint8_t aaa; // Static local variable
(void)p_arg;
for(;;) { // code··· }}
Reply with ‘Embedded Software Design and Development’ to read more related articles.
Click ‘Read the Original’ for more sharing, and welcome to share, collect, like, and view.