0

I was trying to learn ESP-IDF with FreeRTOS, and when I am using a code from the data sheet with very minimal changes (REF code : Documentation page:53 and 54. The Board is restarting.

#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

void vTask1(void* pvParameters){
    const char* pcTaskName = "Task 1 is running \n";
    for(;;){
        printf(pcTaskName);
        vTaskDelay(1000/ portTICK_PERIOD_MS);
    }
}

void vTask2(void* pvParameters){
    const char* pcTaskName = "Task 2 is running \n\n";
    for(;;){
        printf(pcTaskName);
        vTaskDelay(1000/ portTICK_PERIOD_MS);
    }
}



void app_main(void){
    xTaskCreate( vTask1,
                 "TASK 1",
                 1000,
                 NULL,
                 1,
                 NULL );
    xTaskCreate( vTask2, "TASK 2", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    while(true);
}


Now when I removed the vTaskStartScheduler() and the infinity while loop. the program is not restarting but the out put is not as expected . The Code used

#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

void vTask1(void* pvParameters){
    const char* pcTaskName = "Task 1 is running \n";
    for(;;){
        printf(pcTaskName);
        vTaskDelay(1000/ portTICK_PERIOD_MS);
    }
}

void vTask2(void* pvParameters){
    const char* pcTaskName = "Task 2 is running \n\n";
    for(;;){
        printf(pcTaskName);
        vTaskDelay(1000/ portTICK_PERIOD_MS);
    }
}



void app_main(void){
    xTaskCreate( vTask1,
                 "TASK 1",
                 1000,
                 NULL,
                 1,
                 NULL );
    xTaskCreate( vTask2, "TASK 2", 1000, NULL, 1, NULL);
}

The obtained out put is

enter image description here



  1. I want to learn why the first code as shown in the data sheet didn't work .
  2. Why the second code is not behaving like Task1->Task2 - Task1->Task2 ...
Lawliet
  • 77
  • 6
  • What error message did you get prior to the restart? It might help if you put a short `vTaskDelay()` into the `while(true)` loop, so that the scheduler has an opportunity to do stuff. That helped me in a similar situation. – Oliver Mason Oct 13 '20 at 09:19

1 Answers1

2

You don't need to call vTaskStartScheduler if you are using ESP-IDF. It might be different on other platform. It's already called before main() starts (see https://github.com/espressif/esp-idf/blob/master/components/freertos/xtensa/port.c#L619).

If you call it again, it causes problems as you have learned the hard way.

The output is as expected: task 1 and 2 print a line once a second.

As you have started them at virtually the same time, as they do the same work and as they pause for the same amount of time, it's more or less random whether task 1 or task 2 prints the message first every second.

Codo
  • 75,595
  • 17
  • 168
  • 206