ESP32 - FreeRTOS program runs when compiled with Arduino IDE but not with PlatformIO

Hello, I recently started experimenting with FreeRTOS on the ESP32, I haven’t been able to run this simple program succesfully:

#include <Arduino.h>
#include <freertos/FreeRTOS.h>

void test_service(void *pvParams)
{
    while (1)
    {
        Serial.println("This is a test");
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

void setup()
{
    Serial.begin(9600);
    while (!Serial)
        ;
    
    xTaskCreate(test_service,
                "TEST",
                2048,
                NULL,
                1,
                NULL);

    vTaskStartScheduler();
}

void loop() {}

Here’s my platformio.ini file:

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino

Whenever I use PlatformIO to compile, upload and run this program, it crashes. Here’s the stack trace:

Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400e6db9  PS      : 0x00060530  A0      : 0x800d0c8a  A1      : 0x3ffb1f30  
A2      : 0x00000006  A3      : 0x00000001  A4      : 0x00000800  A5      : 0x00000000  
A6      : 0x00000001  A7      : 0x00000000  A8      : 0x800e6da9  A9      : 0x3ffb1b60  
A10     : 0x00000006  A11     : 0xffffffff  A12     : 0x00000000  A13     : 0x3f40516a  
A14     : 0x00000001  A15     : 0x00000001  SAR     : 0x0000001f  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000001  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  

ELF file SHA256: 0000000000000000

Backtrace: 0x400e6db9:0x3ffb1f30 0x400d0c87:0x3ffb1f80 0x400d1eca:0x3ffb1fb0 0x40086125:0x3ffb1fd0

Decoded:

PC: 0x400e6db9: snprintf at ../../../.././newlib/libc/stdio/snprintf.c line 121
EXCVADDR: 0x00000001
Decoding stack results
0x400e6db9: snprintf at ../../../.././newlib/libc/stdio/snprintf.c line 121
0x400d0c87: setup() at src/main.cpp line 88
0x400d1eca: loopTask(void*) at /home/mcontenla/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp line 18
0x40086125: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

However, if I use the Arduino IDE to compile and upload this exact program, it runs succesfully, giving me the output I would expect.

Does anyone have any clue about what’s going on? I’ve tried with different boards, but the result has been the same. Reinstalling PlatformIO has not worked either.
Any help would be appreciated.

Thank you.

When you enter setup(), the Arduino core has already initialized FreeRTOS. In fact, setup() is run in the loop task (see arduino-esp32/cores/esp32/main.cpp at master · espressif/arduino-esp32 · GitHub). Sou you should not be calling into vTaskStartScheduler().

This is weird – there is no reference to snprintf() in your setup() function, and it does not even 88 lines, only 29. Are you sure you’re uploading the program shown in this post or some other project? Use the project environment switcher to switch between projects.

And lastly in general, PlatformIO still uses Arduino-ESP32 version 1.0.6 as opposed to the newest version 2.0.2. So, if you have that installed in your Arduino IDE, the behavior will be different from PlatformIO. This is tracked in Support for the latest Arduino v2.0 · Issue #619 · platformio/platform-espressif32 · GitHub.

I had completely forgotten about that fact. Removing that function call solved my issue.
Thank you.