Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of the FreeRTOS task control API function

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the function of FreeRTOS task control API function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "What is the function of FreeRTOS task control API function"!

1. Relative Delay 1.1 Function Description

void vTaskDelay( portTickTypexTicksToDelay )

When the vTaskDelay() function is called, the task enters a blocking state for the duration specified by the parameter xTicksToDelay of the vTaskDelay() function, in system tick clock cycles. The constant portTICK_RATE_MS is used to assist in calculating the true time. This value is the period of interruption of the system beat clock in milliseconds. In the file FreeRTOSConfig.h, the macro INCLUDE_vTaskDelay must be set to 1 for this function to be valid.

vTaskDelay() specifies the delay time as the relative time calculated from the invocation of vTaskDelay(). For example, vTaskDelay(100), then after calling vTaskDelay(), the task enters the blocking state, and after 100 system clock cycles, the task unblocks. Therefore, vTaskDelay() does not apply to situations where tasks are executed periodically. In addition, other tasks and interrupt activities affect the invocation of vTaskDelay()(e.g., a higher-priority task preempts the current task before invoking it), and thus affect the time when the task is next executed. The API function vTaskDelayUntil() can be used for fixed-frequency delays, which are used to delay an absolute time.

1.2 parameter description

xTicksToDelay: Total delay time in system clock cycles.

1.3 voidvTaskFunction( void * pvParameters ) { /* Block 500ms. */ constportTickType xDelay = 500 / portTICK_RATE_MS; for( ;; ) { /* Trigger LED every 500ms, enter blocking state after triggering */ vToggleLED(); vTaskDelay( xDelay ); }}2. Absolute delay 2.1 Function description

void vTaskDelayUntil( TickType_t *pxPreviousWakeTime,const TickType_txTimeIncrement );

Delay the mission for a specified time. Periodic tasks can use this function to ensure a constant frequency of execution. In the file FreeRTOSConfig.h, the macro INCLUDE_vTaskDelayUntil must be set to 1 for this function to work.

One important difference between this function and vTaskDelay() is that vTaskDelay() specifies a delay time that starts after vTaskDelay() is called (after the function is executed), whereas vTaskDelay () specifies an absolute delay time.

When the vTaskDelay() function is called, the task enters a blocking state for the duration specified by the argument to the vTaskDelay() function in cycles of the system clock. Therefore vTaskDelay() does not apply to periodic execution of tasks. Because the time between calling vTaskDelay() and a task unblocking is not always fixed and the time for the task to call vTaskDelay() next is not always fixed (the interval between two executions of the same task is inherently variable, and interrupts or high-priority task preemption may change the execution time per execution).

vTaskDelay() specifies a relative time from when the vTaskDelay() function is called until the task unblocks, while vTaskDelay () specifies an absolute time until the task unblocks.

It should be noted that if the specified wake-up time has been reached, vTaskDelayUntil() returns immediately (without blocking). Therefore, if a task that executes periodically using vTaskDelayUntil() stops periodic execution for any reason (e.g., if the task temporarily goes into suspension), causing the task to run one or more less execution cycles, then the required wake-up time needs to be recalculated. This can be detected by comparing the value pointed to by the pointer parameter pxPreviousWake passed to the function with the current system clock count value, which is not necessary in most cases.

The constant portTICK_RATE_MS is used to assist in calculating the true time. This value is the period of interruption of the system beat clock in milliseconds.

This function cannot be used when the vTaskSuspendAll() function is called to suspend the RTOS scheduler.

2.2 parameter description

pxPreviousWakeTime: Pointer to a variable that holds the time when the task was last unblocked. This variable must be initialized to the current time before it is used for the first time. This variable is then automatically updated in the vTaskDelayUntil() function.

xTimeIncrement: Cycle cycle time. When the time is equal to (*pxPreviousWakeTime + xTimeIncrease), the task unblocks. If you do not change the value of the parameter xTimeIncrement, the task that calls this function executes at a fixed frequency.

2.3 Example Usage//void vTaskFunction( void * pvParameters ) { static portTickType xLastWakeTime; const portTickType xFrequency = 10; //initialize variable xLastWakeTime with current time xLastWakeTime = xTaskGetTickCount(); for( ;; ) { //wait for the next cycle vTaskDelayUntil( &xLastWakeTime,xFrequency ); //Need periodic execution code here } }3. Get Task Priority 3.1 Function Description

UBaseType_t uxTaskPriorityGet(TaskHandle_t xTask );

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

3.2 parameter description

xTask: Task handle. NULL indicates the priority of getting the current task.

3.3 return value

Returns the priority of the specified task.

3.4 voidvAfunction( void ) { xTaskHandlexHandle; //Create task, save task handle xTaskCreate( vTaskCode, "NAME",STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle ); // ... //Use handles to get priority of created tasks if( uxTaskPriorityGet( xHandle ) != tskIDLE_PRIORITY ) { //tasks can change their priorities } // ... //Is the current task higher priority than the created task? if( uxTaskPriorityGet( xHandle )

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report