ESP-IDF and FreeRTOS DATA Sheet CODE Compatibility

341 views Asked by At

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 ...
1

There are 1 answers

0
Codo On

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.