Page 1 of 1

Getting started with FreeRtos

Posted: Thu Mar 02, 2017 8:09 am
by guillaume55
Hi,
I'm new in FreeRtos coding (and coding). I've red tutorials about Freertos and watched videos but my code does not execute like I tkinked.

I use platformio IDE and esp32 devKit

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"

void task_priority1(void *pvParameter)
{
  while(1) {
    printf("priority = 1\n");
    vTaskDelay(1000
      /portTICK_RATE_MS);
  }
}

void task_priority2(void *pvParameter)
{
  while(1) {
    printf("priority = 2\n");
    vTaskDelay(1000/portTICK_RATE_MS);
  }
}

void task_priority3(void *pvParameter)
{
  while(1) {
    printf("priority = 3\n");
    vTaskDelay(1000/portTICK_RATE_MS);
  }
}

void task_priority4(void *pvParameter)
{
  while(1) {
    printf("priority = 4\n");
    vTaskDelay(1000/portTICK_RATE_MS);
  }
}

void app_main()
{
    system_init();
     xTaskCreate(&task_priority1, "priority1", 2048, NULL, 1, NULL);
     xTaskCreate(&task_priority2, "priority2", 2048, NULL, 2, NULL);
     xTaskreate(&task_priority3, "priority3", 2048, NULL, 3, NULL);
     xTaskCreate(&task_priority4, "priority4", 2048, NULL, 4, NULL);
    vTaskStartScheduler();
}
What happend :

Code: Select all

priority = 2
priority = 1
priority = 3
priority = 4
a pause, and a lot of :

Code: Select all

Register dump:
PC      Guru Meditation Error: Core  0 panic'ed (Interrupt wdt timeout on CPU0)
and then :

Code: Select all

Register dump:
PC      Guru Meditation Error: Core  0 panic'ed (abort)

Backtrace: 0x40084d55:0x3ffc2440 0x00040023:0x3ffc2500 0x40084d55:0x3ffc2520 0x400
850e7:0x3ffc25a0 0x40080f9c:0x3ffc25e0

CPU halted.
I don't understand why this happend. On my mind this should print :

Code: Select all

priority = 4
priority = 3
priority = 2
priority = 1
priority = 4
priority = 3
priority = 2
priority = 1
[...]
I hope you can explain why we begin by priority 2 and then 1, 3, 4 and why it crashs.
Thanks
Guillaume

Re: Getting started with FreeRtos

Posted: Thu Mar 02, 2017 8:48 am
by ESP_Sprite
First of all, you should end your tasks with vTaskDelete(NULL) to kill it. Returning from the task function is not allowed in FreeRTOS. Secondly, as soon as you schedule a task, it will immediately start running. Chances are it gets some runtime in before you schedule the next task, allowing it to do a printf; chances are also that it may not. It doesn't help that you're not pinning your tasks to a core: the ESP32 has 2 cores, so two tasks can be running at the same time, even if one has a higher priority than the other. (And this is literally 'at the same time', not the 'FreeRTOS fakes multiple tasks running at the same time by quickly switching between them'.)

All in all: at this moment, you're just testing the timing your tasks happen to be started at rather than task priority.

Re: Getting started with FreeRtos

Posted: Thu Mar 02, 2017 10:57 am
by ESP_igrr
Also don't call vTaskStartScheduler(), this function is called before app_main starts. In fact, app_main runs within a FreeRTOS task already.

Re: Getting started with FreeRtos

Posted: Thu Mar 02, 2017 11:20 am
by guillaume55
The program works, thanks a lot

Re: Getting started with FreeRtos

Posted: Fri Oct 23, 2020 10:41 am
by mbelda
And what priority has app_main to run before the other tasks?