Page 1 of 1

Failed to change FreeRTOS task priority.

Posted: Wed Jun 27, 2018 12:18 pm
by weyoui
Hey guys,

I've tested the changing task priority during running board.
The expected result is print out "Task1 is running' and "Task2 is running" alternately.
But actual result is print out only "Task1 is running" so it looks to fail to change the priory of task.
Below is my test code that came from FreeRTOS book, please check this code and let me know there is a problem on changing priority.

Code: Select all

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_system.h"
#include "esp_event.h"

static TaskHandle_t task2Handle = NULL;

static void vTask1(void *pvParameters) {
  UBaseType_t uxPriority;
  uxPriority = uxTaskPriorityGet(NULL);
  for (;;) {
    printf("Task 1 is running with %d priority\r\n", uxPriority);
    printf("\tAbout to raise the Task 2 priority %p\r\n", task2Handle);
    vTaskPrioritySet(task2Handle, (uxPriority + 1));
  }
}

static void vTask2(void *pvParameters) {
  UBaseType_t uxPriority;
  uxPriority = uxTaskPriorityGet(NULL);
  for (;;) {
    printf("Task 2 is running with %d priority\r\n", uxPriority);
    printf("\tAbout to lower the Task 2 priority\r\n");
    vTaskPrioritySet(NULL, (uxPriority - 2));
  }
}

static void Test_Task(void) {
  xTaskCreatePinnedToCore(&vTask1, "Task 1", 2048, NULL, 2, NULL, PRO_CPU_NUM);
  xTaskCreatePinnedToCore(&vTask2, "Task 2", 2048, NULL, 1, &task2Handle, PRO_CPU_NUM);
  vTaskStartScheduler();
}

void app_main(void) {
  Test_Task();
}

Re: Failed to change FreeRTOS task priority.

Posted: Wed Jun 27, 2018 12:51 pm
by ESP_igrr
You probably need to remove vTaskStartScheduler() and add vTaskYield calls after changing the priorities.

Re: Failed to change FreeRTOS task priority.

Posted: Wed Jun 27, 2018 1:25 pm
by weyoui
Thanks @ESP_igrr for rapid reply.
I've tested as you commented but taskYIELD or portYIELD do not solve the problem.

I changed the PRO_CPU_NUM to APP_CPU_NUM when creating task. In this case, changing priority works well.
So I want to ask we can't use PRO_CPU_NUM for changing task priority?

Code: Select all

  xTaskCreatePinnedToCore(&vTask1, "Task 1", 2048, NULL, 2, NULL, APP_CPU_NUM); // using APP_CPU_NUM works
  xTaskCreatePinnedToCore(&vTask2, "Task 2", 2048, NULL, 1, &task2Handle, APP_CPU_NUM); // using APP_CPU_NUM works

Re: Failed to change FreeRTOS task priority.

Posted: Wed Jun 27, 2018 1:52 pm
by ESP_igrr
Is it intentional that you raise task 2 priority by 1, but lower by 2? So the priority will end up 0, at which point the task will not run.

Re: Failed to change FreeRTOS task priority.

Posted: Wed Jun 27, 2018 11:24 pm
by chegewara
ESP_igrr wrote:Is it intentional that you raise task 2 priority by 1, but lower by 2? So the priority will end up 0, at which point the task will not run.
He raise task 2 from within task 1 which priority is 2, so he ends up with 2+1=3. Then he decrease by 2.

Re: Failed to change FreeRTOS task priority.

Posted: Fri Oct 11, 2024 8:10 am
by snehapawar#
You should be writing this code like below:
am using esp32s3 devkit-m1.


void vTask21_test3(void *pvParameters)
{
printf("Task 2 is running with priority %d\r\n",uxTaskPriorityGet(NULL));
for (;;)
{
printf("About to lower the priority of task2 from %d to %d\r\n", uxTaskPriorityGet(NULL), (uxTaskPriorityGet(NULL)-2));
vTaskPrioritySet(NULL, uxTaskPriorityGet(NULL)-2);
}
}

void vTask11_test3(void *pvParameters)
{
printf("Task 1 is running with priority %d\r\n", uxTaskPriorityGet(NULL));
xTaskCreatePinnedToCore(vTask21_test3, "Task21_test3", 2048, NULL, 4, &xTask2test3Handle, tskNO_AFFINITY);

for (;;)
{
printf("About to increase the priority of task2 from %d to %d\r\n", uxTaskPriorityGet(NULL), (uxTaskPriorityGet(NULL)+1));
vTaskPrioritySet(xTask2test3Handle, uxTaskPriorityGet(NULL)+1);
}
}

void Test_Task_3_1(void) {
//Allow other core to finish initialization
vTaskDelay(pdMS_TO_TICKS(100));

xTaskCreatePinnedToCore(vTask11_test3, "Task11_test3", 2048, NULL, 3, &xTask1test3Handle, tskNO_AFFINITY); /
}