This function helps you find out the priority level of a task in FreeRTOS. Knowing a task's priority helps you understand which tasks run first.
uxTaskPriorityGet() for reading priority in FreeRTOS
UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask );
TaskHandle_t xTask is the handle (ID) of the task you want to check. If you pass NULL, it returns the priority of the current task.
The function returns the priority as a number where higher means more important.
UBaseType_t priority = uxTaskPriorityGet( NULL );
UBaseType_t priority = uxTaskPriorityGet( myTaskHandle );
This program creates a task with priority 3. Inside the task, it reads its own priority using uxTaskPriorityGet(NULL) and prints it. Then the task deletes itself.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void vTaskFunction(void *pvParameters) { UBaseType_t priority = uxTaskPriorityGet(NULL); // Get current task priority printf("Current task priority: %u\n", (unsigned int)priority); vTaskDelete(NULL); // Delete this task after printing } int main(void) { TaskHandle_t taskHandle = NULL; xTaskCreate(vTaskFunction, "Task1", configMINIMAL_STACK_SIZE, NULL, 3, &taskHandle); vTaskStartScheduler(); return 0; }
Passing NULL to uxTaskPriorityGet() returns the priority of the task that calls the function.
Task priorities are numbers where a higher number means higher priority.
Make sure the task handle you pass is valid to avoid errors.
uxTaskPriorityGet() reads the priority of a task in FreeRTOS.
Pass NULL to get the current task's priority.
Useful for debugging and managing task scheduling.
Practice
What does the FreeRTOS function uxTaskPriorityGet() do?
Solution
Step 1: Understand the function purpose
uxTaskPriorityGet()is designed to read the priority of a task, not to modify or manage tasks.Step 2: Match function behavior to options
Only "It returns the priority of a specified task." correctly states it returns the priority of a specified task.Final Answer:
It returns the priority of a specified task. -> Option AQuick Check:
uxTaskPriorityGet() reads priority = It returns the priority of a specified task. [OK]
- Confusing 'get' with 'set' functions
- Thinking it creates or deletes tasks
- Assuming it returns task handle
Which of the following is the correct way to get the priority of the current task using uxTaskPriorityGet()?
TaskHandle_t xTaskHandle;
UBaseType_t uxPriority = ?;Solution
Step 1: Recall how to get current task priority
PassingNULLtouxTaskPriorityGet()returns the priority of the current task.Step 2: Check each option
uxPriority = uxTaskPriorityGet(xTaskHandle);uses uninitializedxTaskHandle(undefined behavior).uxPriority = uxTaskPriorityGet(&xTaskHandle);passes address (wrong type).uxPriority = uxTaskPriorityGet(*xTaskHandle);dereferences uninitialized handle (crash). OnlyuxPriority = uxTaskPriorityGet(NULL);is correct.Final Answer:
uxPriority = uxTaskPriorityGet(NULL); -> Option BQuick Check:
Use NULL for current task priority [OK]
- Passing address of handle instead of handle
- Dereferencing handle incorrectly
- Using uninitialized task handle
Given the following code snippet, what will be printed?
TaskHandle_t xHandle = xTaskGetHandle("Task1");
UBaseType_t uxPriority = uxTaskPriorityGet(xHandle);
printf("Priority: %u\n", uxPriority);Assume "Task1" exists and has priority 3.
Solution
Step 1: Understand task handle retrieval
xTaskGetHandle("Task1")returns the handle of the task named "Task1" which exists with priority 3.Step 2: Use uxTaskPriorityGet() to read priority
CallinguxTaskPriorityGet(xHandle)returns the priority of that task, which is 3.Final Answer:
Priority: 3 -> Option CQuick Check:
uxTaskPriorityGet returns task priority = 3 [OK]
- Assuming default priority 0
- Confusing handle with task name
- Expecting syntax error
Identify the error in this code snippet:
TaskHandle_t xHandle;
UBaseType_t uxPriority = uxTaskPriorityGet(xHandle);
printf("Priority: %u\n", uxPriority);Solution
Step 1: Check variable initialization
xHandleis declared but not assigned any value before use.Step 2: Understand consequences of uninitialized handle
Passing an uninitialized handle touxTaskPriorityGet()leads to undefined behavior or incorrect priority reading.Final Answer:
xHandle is uninitialized before use. -> Option AQuick Check:
Always initialize task handles before use [OK]
- Ignoring uninitialized variables
- Misunderstanding function parameter types
- Assuming pointer needed for uxPriority
You want to check if the current task has higher priority than another task named "WorkerTask". Which code snippet correctly compares their priorities?
TaskHandle_t xWorkerHandle = xTaskGetHandle("WorkerTask");
UBaseType_t uxCurrentPriority = uxTaskPriorityGet(NULL);
UBaseType_t uxWorkerPriority = uxTaskPriorityGet(xWorkerHandle);
// What next?Solution
Step 1: Understand priority comparison logic
Higher numeric value means higher priority in FreeRTOS.Step 2: Compare current and worker task priorities
Use > operator to check if current task priority is higher than worker's.Final Answer:
if (uxCurrentPriority > uxWorkerPriority) { /* current task higher */ } -> Option DQuick Check:
Higher number = higher priority [OK]
- Using '<' instead of '>' for priority comparison
- Confusing equality with higher priority
- Not retrieving correct task handles
Ready to prove it?
Pass the test and this pattern moves unknown
