[SOLVED] Illegal instruction exception ESP-IDF FreeRTOS M5stack Core2 C++

Hello,
I am trying a modified version of the examples in section 4.4 of the excellent e-book Mastering the FreeRTOS Real Time Kernel (Barry et al.) and I’m running into an illegal instruction exception. Using monitor_filters = esp32_exception_decoder shows that the crash occurs in FreeRTOS, when it tries to return from an interrupt. I think it’s the hardware Tick timer but I can’t be sure.

Could this illegal instruction exception be a problem with the code generated for the Extensa port of FreeRTOS?

I pared down the example, except for using C++ and M5_LOGI. I can simplify further to plain old C, if that helps. I’m giving sufficient stack (16000) for each task so I don’t think it’s stack smashing.

Thanks in advance for any clues or hints.

Main program:

#include "stdio.h"

#include "M5Unified.h"

extern "C" {
    void app_main(void);
}

m5::M5Unified m5core2{};

void countup_task(void * TaskParameters);
void movedot_task(void * TaskParameters);

/*
* Define the strings that will be passed in as the task parameters.
* These are defined const and not on the stack to ensure they remain valid
* when the tasks are executing.
*/
static const char * pcTextForTask1 = "Task 1 count up is running";
static const char * pcTextForTask2 = "Task 2 is running";

void app_main() {
      /// Hardware configurations
    m5core2.begin();
    printf("Hello world Core2.");

/* Create the first task with a priority of 1. */
    xTaskCreate( countup_task,  /* Task Function */
        "Task 1",    /* Task Name */
        16000,    /* Task Stack Depth */
        ( void * ) pcTextForTask1, /* Task Parameter  */
        1,   /* Task Priority  */
        NULL );

    /* Create the second task at a higher priority of 2. */
    xTaskCreate( movedot_task,  /* Task Function */
        "Task 2", /* Task Name */
        16000, /* Task Stack Depth */
        ( void * ) pcTextForTask2, /* Task Parameter */
        2, /* Task Priority */
        NULL ); 

    /* Start the scheduler so the tasks start executing. */
    vTaskStartScheduler();

    /* Will not reach here. */
    while(true);
}

void countup_task(void * TaskParameters) {
    static uint32_t counter = 0;
    const TickType_t xDelay = pdMS_TO_TICKS(2000);
    char * task_name = static_cast<char *>(TaskParameters);

    while(true) {
        M5_LOGI("%4ld %s\n", counter, task_name);
        counter += 1;
        // Block for delay period.
        vTaskDelay( xDelay );
    }
}

void movedot_task(void * TaskParameters) {
    volatile uint32_t counter = 0;
    const TickType_t xDelay = pdMS_TO_TICKS(2000);
    char * task_name = static_cast<char *>(TaskParameters);

    while(true) {
        M5_LOGI("%4ld %s\n", counter, task_name);
        counter += 1;
        // Block for delay period.
        vTaskDelay( xDelay );
    }
}

Crash dump (it keeps repeating):

ELF file SHA256: fafcacc86

Rebooting...
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0040,len:6100
load:0x40078000,len:15776
ho 0 tail 12 room 4
load:0x40080400,len:3944
  #0  0x40080400 in _invalid_pc_placeholder at /home/louis/.platformio/packages/framework-espidf/components/xtensa/xtensa_vectors.S:2257

entry 0x40080634
I (29) boot: ESP-IDF 6.1.1 2nd stage bootloader
I (29) boot: compile time Dec  6 2026 05:01:59
I (29) boot: chip revision: v3.0
I (30) boot.esp32: SPI Speed      : 40MHz
I (34) boot.esp32: SPI Mode       : DIO
I (37) boot.esp32: SPI Flash Size : 16MB
I (41) boot: Partition Table:
I (44) boot: ## Label            Usage          Type ST Offset   Length
I (50) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (57) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (63) boot:  2 factory          factory app      00 00 00010000 00100000
I (70) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=131c4h ( 78276) map
I (105) esp_image: segment 1: paddr=000231ec vaddr=3ffb0000 size=02ea4h ( 11940) load
I (110) esp_image: segment 2: paddr=00026098 vaddr=40080000 size=09f80h ( 40832) load
I (127) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=3789ch (227484) map
I (209) esp_image: segment 4: paddr=000678c4 vaddr=40089f80 size=03dd8h ( 15832) load
I (215) esp_image: segment 5: paddr=0006b6a4 vaddr=50000000 size=00028h (    40) load
I (223) boot: Loaded app from partition at offset 0x10000
I (230) cpu_start: Multicore app
I (238) cpu_start: GPIO 3 and 1 are used as console UART I/O pins
I (238) cpu_start: Pro cpu start user code
I (238) cpu_start: cpu freq: 160000000 Hz
I (240) app_init: Application information:
I (244) app_init: Project name:     HouseCat
I (248) app_init: App version:      1
I (251) app_init: Compile time:     Dec  6 2026 05:01:10
I (256) app_init: ELF file SHA256:  fafcacc86...
I (261) app_init: ESP-IDF:          6.1.1
I (264) efuse_init: Min chip rev:     v0.0
I (268) efuse_init: Max chip rev:     v3.99 
I (272) efuse_init: Chip rev:         v3.0
I (276) heap_init: Initializing. RAM available for dynamic allocation:
I (282) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (287) heap_init: At 3FFB5250 len 0002ADB0 (171 KiB): DRAM
I (293) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (298) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (304) heap_init: At 4008DD58 len 000122A8 (72 KiB): IRAM
I (310) spi_flash: detected chip: generic
I (312) spi_flash: flash io: dio
I (318) main_task: Started on CPU0
I (318) main_task: Calling app_main()
I (378) board_detect_m5: ILI9342 read-back DDh:7f CBh:7f ID4:1342 -> ILI9342C
I (378) M5GFX: [Autodetect] M5StackCore2
Hello world Core2.[   680][I][main.cpp:75] movedot_task():    0 Task 2 is running

[   681][I][main.cpp:62] countup_task():    0 Task 1 count up is running

Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x40088fa0: 08e0ddce 00f01d00 ad004136
  #0  0x40088fa0 in vPortClearInterruptMaskFromISR at /home/louis/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h:547
      (inlined by) vPortExitCritical at /home/louis/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:528

Core  0 register dump:
PC      : 0x40088fa5  PS      : 0x00060830  A0      : 0x00000000  A1      : 0x3ffb6490  
  #0  0x40088fa5 in vPortExitCritical at /home/louis/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:531

A2      : 0x00000001  A3      : 0x3f40021c  A4      : 0x00060823  A5      : 0x3ffb0584  
A6      : 0x3ffb64a0  A7      : 0x00060823  A8      : 0x80088fa5  A9      : 0x3ffb6470  
A10     : 0x00000003  A11     : 0x00060a23  A12     : 0x00060a20  A13     : 0x3ffbfb30  
A14     : 0x3ffb03f4  A15     : 0x0000000c  SAR     : 0x00000020  EXCCAUSE: 0x00000000  
  #0  0x3ffb03f4 in ?? at /home/louis/.platformio/packages/framework-espidf/components/esp_libc/src/picolibc/picolibc_init.c:146

EXCVADDR: 0x00000000  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x3ffb6450  


Backtrace: 0x40088fa2:0x3ffb6490
  #0  0x40088fa2 in vPortClearInterruptMaskFromISR at /home/louis/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h:547
      (inlined by) vPortExitCritical at /home/louis/.platformio/packages/framework-espidf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:528

The code where it’s crashing is in FreeRTOS extensa/port.c

        //This is the last exit call, restore the saved interrupt level
        if ( nesting == 0 ) {
            portCLEAR_INTERRUPT_MASK_FROM_ISR(port_uxOldInterruptState[coreID]);
        }

PlatformIO platformio.ini:

[env:m5stack-core2]
platform = espressif32
board = m5stack-core2
framework = espidf
lib_deps = 
    m5stack/M5GFX@^0.3.1
    m5stack/M5Unified@^0.3.1

upload_speed = 1500000
monitor_speed = 115200
monitor_filters = esp32_exception_decoder

Absolutely not. app_main already runs in the context of a FreeRTOS task, the scheduler is already started. Trying to start the scheduler again after the task has been created and it was already running wreaks havoc.

See other ESP-IDF examples that create a task. They don’t need that, too.

Simply delete the 4 lines quotes above and reflash.

Unlike Vanilla FreeRTOS, users of FreeRTOS in ESP-IDF must never call vTaskStartScheduler() and vTaskEndScheduler(). Instead, ESP-IDF starts FreeRTOS automatically.

Main Task (main)
Task that simply calls app_main. This task will self delete when app_main returns

@maxgerhardt Thank you! That was it. I am now on my guard for other ESP-IDF adaptations to FreeRTOS.