FreeRTOS Series 11th---freertos task control

Source: Internet
Author: User

FreeRTOS Task Control API function mainly realizes the functions of task delay, task suspending, lifting task suspending, task priority acquiring and setting.

1. Relative delay1.1 Function Description

void Vtaskdelay (Portticktypextickstodelay)

When the Vtaskdelay () function is called, the task goes into a blocking state, and the duration is specified by the parameter xtickstodelay of the Vtaskdelay () function, which is the system tick clock cycle. The constant Porttick_rate_ms is used to assist in calculating the real time, which is the period of the system beat clock interrupt, in milliseconds. In file FreeRTOSConfig.h, the macro include_vtaskdelay must be set to 1 for this function to be valid.

Vtaskdelay () specifies that the delay time is the relative time that is calculated from the call to Vtaskdelay (). such as Vtaskdelay (100), then from the call Vtaskdelay (), the task into the blocking state, after 100 system clock cycle, the task unblocked. Therefore, Vtaskdelay () does not apply to situations where the task is performed periodically. In addition, other tasks and interrupt activities can affect the invocation of vtaskdelay (for example, the pre-call high-priority task preempted the current task) and therefore affect the time of the next execution of the task. The API function Vtaskdelayuntil () can be used for fixed-frequency delay, which is used to delay an absolute time.

1.2 Parameter Description

    • Xtickstodelay: The total delay time, in units of the system clock cycle.

1.3 Usage Examples

Voidvtaskfunction (void * pvparameters) {/     * * Block 500ms. */     Constportticktype xdelay = 500/porttick_rate_ms;      for (;;)     {         /* Triggers an LED once every 500ms, triggering to enter the blocking state *         /vtoggleled ();         Vtaskdelay (Xdelay);     }}

2. Absolute delay2.1 Function Description

void Vtaskdelayuntil (ticktype_t *pxpreviouswaketime,

Const ticktype_txtimeincrement);

The task is delayed by a specified time. A recurring task can use this function to ensure a constant frequency of execution. In file FreeRTOSConfig.h, the macro include_vtaskdelayuntil must be set to 1 for this function to be valid.

One important thing that this function differs from the Vtaskdelay () function is that vtaskdelay () specifies that the delay time is calculated from the beginning of the call to Vtaskdelay () (after executing the function), but Vtaskdelayuntil () The specified delay time is an absolute time.

When the Vtaskdelay () function is called, the task goes into a blocking state, and the duration is specified by the parameters of the Vtaskdelay () function, which is the system tick clock cycle. therefore Vtaskdelay () does not apply to occasions where tasks are performed periodically. Because the time to call Vtaskdelay () to the task unblocked is not always fixed and the time of the next call to the Vtaskdelay () function of the task is not always fixed (two times the time interval of the same task is not fixed by itself, Interrupts or high-priority task preemption can also change each execution time).

Vtaskdelay () specifies a time to start from the call of the Vtaskdelay () function to the relative times until the task is unblocked, while Vtaskdelayuntil () specifies an absolute time at which the task is unblocked whenever the time arrives.

It should be noted that if the specified wake-up time has been reached, Vtaskdelayuntil () returns immediately (no blocking). Therefore, tasks that are performed periodically with vtaskdelayuntil (), for any reason (for example, when a task is temporarily in a suspended state) are stopped for periodic execution, so that the task runs one or more execution cycles less, then the required wake-up time needs to be recalculated. This can be detected by comparing the value pointed to the current system clock count value by passing the pointer parameter pxpreviouswake to the function, which is not required in most cases.

The constant Porttick_rate_ms is used to assist in calculating the real time, which is the period of the system beat clock interrupt, in milliseconds.

You cannot use this function when you call the Vtasksuspendall () function to suspend the RTOS scheduler.

2.2 Parameter Description

    • Pxpreviouswaketime: Pointer to a variable that holds the last time the task was unblocked. The variable must be initialized to the current time before being used for the first time. This variable is then automatically updated within the Vtaskdelayuntil () function.
    • Xtimeincrement: Cycle cycle time. When time equals (*pxpreviouswaketime + xtimeincrement), the task is unblocked. If you do not change the value of the parameter xtimeincrement, the task that invokes the function executes at a fixed frequency.

2.3 Usage Examples

Every 10 system beats are performed once void vtaskfunction (void * pvparameters) {     staticportticktype xlastwaketime;     Constportticktype xfrequency = ten;      Initializes the variable with the current time xlastwaketime     xlastwaketime = Xtaskgettickcount ();      for (;;)     {         //wait for the next cycle         vtaskdelayuntil (&xlastwaketime,xfrequency);          Need to put the periodic execution code here     }}

3. Get task Priority3.1 Function Description

ubasetype_t Uxtaskpriorityget (taskhandle_t xtask);

Gets the priority of the specified task. In file FreeRTOSConfig.h, the macro include_vtaskpriorityget must be set to 1 for this function to be valid.

3.2 Parameter Description

    • Xtask: the task handle. Null indicates the priority of getting the current task.

3.3 return value

Returns the priority of the specified task.

3.4 Usage Examples

Voidvafunction (void) {     xtaskhandlexhandle;     Create task, save task handle     xtaskcreate (Vtaskcode, "NAME", Stack_size, NULL, tskidle_priority, &xhandle);     // ...     Use the handle to get the priority of the created task     if (Uxtaskpriorityget (xhandle)!=tskidle_priority)     {//         the task can change its priority     }     // ...     Is the current task priority higher than the task created?     if (Uxtaskpriorityget (xhandle) <uxtaskpriorityget (NULL))     {         //higher Current priority     }}

4. Set task Priority4.1 Function Description

void Vtaskpriorityset (Taskhandle_txtask,

ubasetype_tuxnewpriority);

Sets the priority of the specified task. If the setting has a higher priority than the currently running task, a context switch is made before the function returns. In file FreeRTOSConfig.h, the macro include_vtaskpriorityset must be set to 1 for this function to be valid.

4.2 Parameter Description

    • Xtask: To set a handle to a priority task, the null representation sets the currently running task.
    • Uxnewpriority: The new priority to set.

4.3 usage examples

Voidvafunction (void) {     xtaskhandlexhandle;     Create a task to save the task handle.     xtaskcreate (Vtaskcode, "NAME", Stack_size, NULL, tskidle_priority, &xhandle);     // ...     Use a handle to increase the priority of creating Tasks     Vtaskpriorityset (xhandle,tskidle_priority + 1);     // ...     Use the null parameter to increase the priority of the current task, and set sing woo to create the same task.     Vtaskpriorityset (NULL, tskidle_priority +1);}

5. Task Hangs5.1 Function Description

void Vtasksuspend (Taskhandle_txtasktosuspend);

Suspends the specified task. A suspended task never gets the processor time, regardless of the priority of the task.

Calling the Vtasksuspend function does not accumulate: even if you call the Vtasksuspend () function multiple times to suspend a task, you only need to call the Vtaskresume () function once to get the suspended task out of the pending state. In file FreeRTOSConfig.h, the macro include_vtasksuspend must be set to 1 for this function to be valid.

5.2 Parameter Description

    • Xtasktosuspend: The task handle to suspend. Null indicates that the current task is suspended.

5.3 Usage Examples

Voidvafunction (void) {     xtaskhandlexhandle;     Create a task to save the task handle.     Xtaskcreate (Vtaskcode, "NAME", Stack_size, NULL, tskidle_priority, &xhandle);     // ...     Use the handle to suspend the created task.     Vtasksuspend (xhandle);     // ...     The task no longer runs unless other tasks call Vtaskresume (Xhandle)     //...     Suspends this task.     Vtasksuspend (NULL);     Unless another task uses handle to call Vtaskresume, it will never be executed here}

6. Resuming a suspended task6.1 Function Description

void Vtaskresume (Taskhandle_txtasktoresume);

Resumes a suspended task.

You can call the Vtaskresume () function once to resume the run again by calling the Vtasksuspend () suspend task one or more times. In file FreeRTOSConfig.h, the macro include_vtasksuspend must be set to 1 for this function to be valid.

6.2 Parameter Description

    • Xtasktoresume: The task handle to resume running.

6.3 Usage Examples

Voidvafunction (void) {         xtaskhandle xhandle;     Create task, save task handle     xtaskcreate (Vtaskcode, "NAME", Stack_size, NULL, tskidle_priority, &xhandle);     // ...     Use the handle to suspend the created task     vtasksuspend (xhandle);     // ...     The task no longer runs unless other tasks call Vtaskresume (Xhandle)              //...     Resumes a suspended task.     Vtaskresume (xhandle);     Task gets processor Time again     //task priority same as before}

7. Resuming a suspended task (used in an interrupt service function)7.1 Function Description

basetype_t Xtaskresumefromisr (taskhandle_t xtasktoresume);

Used to restore a suspended task, which is used in the ISR.

Tasks that are suspended by calling one or more vtasksuspend () functions can be resumed by calling the Xtaskresumefromisr () function once.

XTASKRESUMEFROMISR () is not available for synchronization between tasks and interrupts, and if interrupts happen to arrive before the task is suspended, this can result in one interrupt loss (the task has not been suspended and the call to the XTASKRESUMEFROMISR () function is meaningless, Can only wait for the next interrupt). In this case, the semaphore can be used as the synchronization mechanism. In file FreeRTOSConfig.h, the macro include_vtasksuspend and INCLUDE_XTASKRESUMEFROMISR must be set to 1 for this function to be valid.

7.2 Parameter Description

    • Xtasktoresume: The task handle to resume running.

7.3 return value

Returns PDFALSE If a context switch is required to return pdtrue after the task is resumed. The ISR determines whether context switching is required.

7.4 Usage Examples

Xtaskhandlexhandle;               Note that this is a global variable  void vafunction (void) {     //Create task and save the task handle     xtaskcreate (Vtaskcode, "NAME", Stack_size, NULL, Tskidle_priority, &xhandle);      // ... The remaining code. }  void Vtaskcode (void *pvparameters) {for     (;;)     {         // ... Perform some other functions here          //Suspend yourself         vtasksuspend (NULL);          Until the ISR recovers it, the task will remain suspended     }}  void Vanexampleisr (void) {     portbase_typexyieldrequired;      Resuming a suspended task     xyieldrequired = XTASKRESUMEFROMISR (xhandle);      if (xyieldrequired = = pdtrue)     {         //We should make a context switch//         Note:  How to do it depends on your specific use, you can view the description document and routines         Portyield_ From_isr ();     } }


FreeRTOS Series 11th---freertos task control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.