freeRTOS Changing the Priority of a Task

vTaskPrioritySet()
The vTaskPrioritySet() API function can be used to change the priority of any task after the scheduler has been started. Note that the vTaskPrioritySet() API function is available only when INCLUDE_vTaskPrioritySet is set to 1 in FreeRTOSConfig.h.

void vTaskPrioritySet( TaskHandle_t pxTask, UBaseType_t uxNewPriority );

pxTask
The handle of the task whose priority is being modified (the subject task)—see the pxCreatedTask parameter of the xTaskCreate() API function for information on obtaining handles to tasks.
A task can change its own priority by passing NULL in place of a valid task handle.

uxNewPriority
The priority to which the subject task is to be set. This is capped automatically to the maximum available priority of (configMAX_PRIORITIES – 1), where configMAX_PRIORITIES is a compile time constant set in the FreeRTOSConfig.h header file

uxTaskPriorityGet()
The uxTaskPriorityGet() API function can be used to query the priority of a task. Note that the uxTaskPriorityGet() API function is available only when INCLUDE_uxTaskPriorityGet is set to 1 in FreeRTOSConfig.h.

UBaseType_t uxTaskPriorityGet( TaskHandle_t pxTask );

pxTask
The handle of the task whose priority is being queried (the subject task)—see the pxCreatedTask parameter of the xTaskCreate() API function for information on obtaining handles to tasks.
A task can query its own priority by passing NULL in place of a valid task handle.

Returned value
The priority currently assigned to the task being queried


Example

#include "FreeRTOS.h"
#include "task.h"
#include "stm32f4xx.h"

TaskHandle_t xTask2Handle = NULL;
//Task1
// ----------------------------------------------------------------------------
void vTask1( void *pvParameters )
{
volatile uint16_t count1=0;
UBaseType_t uxPriority;
/* This task will always run before Task 2 as it is created with the higher
priority. Neither Task 1 nor Task 2 ever block so both will always be in
either the Running or the Ready state.
Query the priority at which this task is running - passing in NULL means
"return the calling task’s priority". */
uxPriority = uxTaskPriorityGet( NULL );
for( ;; )
{
count1++;
/* Setting the Task 2 priority above the Task 1 priority will cause
Task 2 to immediately start running (as then Task 2 will have the higher
priority of the two created tasks). Note the use of the handle to task
2 (xTask2Handle) in the call to vTaskPrioritySet(). Listing 35 shows how
the handle was obtained. */

vTaskPrioritySet( xTask2Handle, ( uxPriority + 1 ) );
/* Task 1 will only run when it has a priority higher than Task 2.
Therefore, for this task to reach this point, Task 2 must already have
executed and set its priority back down to below the priority of this
task. */
}
}

////Task2
//// ----------------------------------------------------------------------------
void vTask2( void *pvParameters )
{
volatile uint16_t count2=0;
UBaseType_t uxPriority;
/* Task 1 will always run before this task as Task 1 is created with the
higher priority. Neither Task 1 nor Task 2 ever block so will always be
in either the Running or the Ready state.
Query the priority at which this task is running - passing in NULL means
"return the calling task’s priority". */
uxPriority = uxTaskPriorityGet( NULL );
for( ;; )
{
/* For this task to reach this point Task 1 must have already run and
set the priority of this task higher than its own. */
count2++;
/* Set the priority of this task back down to its original value.
Passing in NULL as the task handle means "change the priority of the
calling task". Setting the priority below that of Task 1 will cause
Task 1 to immediately start running again – pre-empting this task. */
vTaskPrioritySet( NULL, ( uxPriority - 2 ) );
}
}
void GPIOInit(){
    GPIO_InitTypeDef g;
    g.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; // ?? 12,13,14,15 ?
    g.GPIO_Mode = GPIO_Mode_OUT; // ???????
    g.GPIO_Speed = GPIO_Speed_100MHz; // ?? GPIO ???100 MHz
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // ?? GPIOD
    GPIO_Init(GPIOD, &g); // ??? GPIO D
}


int main( void )
{
/* Create the first task at priority 2. The task parameter is not used
and set to NULL. The task handle is also not used so is also set to NULL. */
xTaskCreate( vTask1, "Task 1", 1000, NULL, 2, NULL );
/* The task is created at priority 2 ______^. */
/* Create the second task at priority 1 - which is lower than the priority
given to Task 1. Again the task parameter is not used so is set to NULL -
BUT this time the task handle is required so the address of xTask2Handle
is passed in the last parameter. */
xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, &xTask2Handle );
/* The task handle is the last parameter _____^^^^^^^^^^^^^ */
/* Start the scheduler so the tasks start executing. */
vTaskStartScheduler();
/* If all is well then main() will never reach here as the scheduler will
now be running the tasks. If main() does reach here then it is likely there
was insufficient heap memory available for the idle task to be created.
Chapter 2 provides more information on heap memory management. */
for( ;; );
}

留言

這個網誌中的熱門文章

freeRTOS Deleting a Task

05 Software Timer Management

Interrupt Management