Suspension and Resume of FreeRTOS Tasks | FreeRTOS Four

Table of contents

illustrate:

1. Task suspension and recovery

1.1. What is task suspension?

1.2. Task suspension function

1.3. What is task recovery?

1.4. Task recovery function

1.5. What is the interrupt task recovery function?

1.6. Interrupt task recovery function

2. Task suspension and recovery example code (in task)

2.1. Main code

2.2. Results

3. Task suspension and recovery example code (interrupted)

3.1. Main code

3.2. Results


illustrate:

About the content:

1) The following contents are mostly conceptual understanding and step analysis

2) There is no personal sample code yet, the sample code of FreeRTOS is used.

3) If you want to transplant the code for testing, please look elsewhere. There is no personal sample code for testing in the following content.

About others:

1) Operating system: win 10

2) Platform: keil 5 mdk

3) Language: c language

4) Board: STM32 series transplanted to FreeRTOS

1. Task suspension and recovery

1.1. What is task suspension?

1) Task suspension, meaning: Task suspension is equivalent to suspension. If the task is suspended, the task will be suspended and can be resumed.

2) Internal implementation of task suspension:

1. Get the task control block based on the task handle. If the task handle is NULL, it means the task itself is suspended.
2. Remove the task to be suspended from the corresponding status list and event list.
3. Remove the task status list of the task to be suspended. Insert to the end of the suspended task list
4. Determine whether the task scheduler is running. If it is running, update the next blocking time to prevent the suspended task from being blocked and timeout for the next time. The task scheduler is running and force a task switch;
5 , if the task itself is suspended

The scheduler is running, forcing a task switch;

The scheduler is not running. Determine whether the number of suspended tasks is equal to the total number of tasks:
Yes: means all tasks are suspended, then the current control block is assigned NULL. The scheduler is not running. Determine whether the number of suspended tasks is equal to the total number of tasks. No
: Find the next highest priority task through the function vTaskSwitchContext

1.2. Task suspension function

1) Function prototype: void vTaskSuspend(TaskHandle_t xTaskToSuspend)

2) Formal parameter description: xTaskToSuspend is the task handle of the task to be suspended

3) Conditions of use: This function is used to suspend tasks. When using it, the macro INCLUDE_ vTaskSuspend needs to be configured as 1.

4) Instructions for use: Regardless of the priority, the suspended task will no longer be executed until the task is resumed.
5) Note: When the parameter passed in is NULL, ) represents the currently running task of the suspended task itself. Task))

1.3. What is task recovery?

1) Task recovery, meaning: resume suspended tasks.

2) Internal implementation of task recovery:

1. The recovery task cannot be a running task and cannot be NULL.
2. Determine whether the task is suspended. If yes: the task will be removed from the suspended list and added to the ready list.
3.
Determine recovery. Whether the task priority is greater than the currently running task, if so, perform task switching

1.4. Task recovery function

1) Function prototype: void vTaskResume(TaskHandle_t xTaskToResume)
2) Formal parameter description: xTaskToResume, the task handle of the task to be resumed
3) Conditions of use: Macro definition INCLUDE _vTaskSuspend must be defined as 1
4) Instructions for use: Note: The task is suspended regardless of yTaskSuspend No matter how many times it is started, you only need to call vTakResume() in the task to resume it once, and you can continue running. And the resumed task will enter the ready state! )

1.5. What is the interrupt task recovery function?

Interrupt task recovery function, meaning: in the interrupt service function, resume the suspended task function.

1.6. Interrupt task recovery function

1) Function prototype: BaseType _t xTaskResumeFromlSR(TaskHandle_t xTaskToResume)
2) Formal parameter description: xTaskToResume, the task handle of the task to be resumed
3) Conditions of use: Macro definition INCLUDE _vTaskSuspene and INCLODE_ xTaskResumeFromISR must be defined as 1
4) Instructions for use: must be in Call this function in the interrupt service function

5) Return value description: The return value is pdTRUE, and task switching is required after task recovery; the return value is pdFALSE, and task switching is not required after task recovery; Note: Task switching requires us to perform it manually, and we need to call the switching function:
6 ) Note: When calling the API function of FreeRTOS in the interrupt service function, the interrupt priority cannot be higher than the highest priority managed by FreeRTOS.

2. Task suspension and recovery example code (in task)

2.1. Main code

The board is not around, so I can't test it. I will make it up later.

FreeRTOS task suspension function official website usage example:

void vAFunction( void ) {
TaskHandle_t xHandle; //Define task handle

// Create a task, storing the handle.
xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
//Created a task
// ...

// Use the handle to suspend the created task.
vTaskSuspend( xHandle ); //Call the suspended task and pass in the handle of the task to be suspended

// ...

// The created task will not run during this period, unless
// another task calls vTaskResume( xHandle ).

//...

// Suspend ourselves.
vTaskSuspend( NULL ); //Pass in NULL to suspend the running task itself

// We cannot get here unless another task calls vTaskResume
// with our handle as the parameter.
}

FreeRTOS task recovery official example:

void vAFunction( void ) {
TaskHandle_t xHandle;

// Create a task, storing the handle.
//Create the task first
xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );

// ...

// Use the handle to suspend the created task.
//suspend task
vTaskSuspend( xHandle );

// ...

// The created task will not run during this period, unless
// another task calls vTaskResume( xHandle ).

//...

// Resume the suspended task ourselves.
//Resume suspended tasks
vTaskResume( xHandle );

// The created task will once again get microcontroller processing
// time in accordance with its priority within the system.
}

2.2. Results

The board is not around, so I can't test it. I will make it up later.

3. Task suspension and recovery example code (interrupted)

3.1. Main code

The board is not around, so I can't test it. I will make it up later.

FreeRTOS task recovery function official website usage example (interrupted):

TaskHandle_t xHandle; //Define task handle

 void vAFunction( void )
 {
     // Create a task, storing the handle.
     xTaskCreate( vTaskCode, "NAME", STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );

     // ... Rest of code.
 }

 void vTaskCode( void *pvParameters )
 {
     // The task being suspended and resumed.
     for( ;; )
     {
         // ... Perform some function here.

         // The task suspends itself.
         vTaskSuspend( NULL );

         // The task is now suspended, so will not reach here until the ISR resumes it.
     }
 }


 void vAnExampleISR( void )
 {
     BaseType_t xYieldRequired;

     // Resume the suspended task.
     xYieldRequired = xTaskResumeFromISR( xHandle );

     // We should switch context so the ISR returns to a different task.
     // NOTE:  How this is done depends on the port you are using.  Check
     // the documentation and examples for your port.
     portYIELD_FROM_ISR( xYieldRequired );
 }

3.2. Results

The board is not around, so I can't test it. I will make it up later.

おすすめ

転載: blog.csdn.net/qq_57663276/article/details/128780150