Why does this code throws IllegalInstruction after starting the scheduler?

noobiewan
Posts: 4
Joined: Tue Apr 20, 2021 12:42 pm

Why does this code throws IllegalInstruction after starting the scheduler?

Postby noobiewan » Tue Apr 20, 2021 2:51 pm

I'm trying to run the code below. It uses an interruption to detect a button being pressed.

The way it is, it won't run properly. It throws an exception right after Starting scheduler on APP CPU.

Funny thing is, if I add an infinite loop or vTaskDelete(NULL); in app_main, no exception is thrown.

Could someone help me understand the issue?

Thank you
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "freertos/queue.h"
  5. #include "driver/gpio.h"
  6.  
  7. #define PIN_SWITCH 15
  8.  
  9. xQueueHandle interputQueue;
  10.  
  11. static void IRAM_ATTR gpio_isr_handler(void *args)
  12. {
  13.     int pinNumber = (int)args;
  14.     xQueueSendFromISR(interputQueue, &pinNumber, NULL);
  15. }
  16.  
  17. void buttonPushedTask(void *params)
  18. {
  19.     int pinNumber, count = 0;
  20.     while (true)
  21.     {
  22.         if (xQueueReceive(interputQueue, &pinNumber, portMAX_DELAY))
  23.         {
  24.             printf("GPIO %d was pressed %d times. The state is %d\n", pinNumber, count++, gpio_get_level(PIN_SWITCH));
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. void app_main()
  31. {
  32.     gpio_pad_select_gpio(PIN_SWITCH);
  33.     gpio_set_direction(PIN_SWITCH, GPIO_MODE_INPUT);
  34.     gpio_pulldown_en(PIN_SWITCH);
  35.     gpio_pullup_dis(PIN_SWITCH);
  36.     gpio_set_intr_type(PIN_SWITCH, GPIO_INTR_POSEDGE);
  37.  
  38.     interputQueue = xQueueCreate(10, sizeof(int));
  39.     xTaskCreate(buttonPushedTask, "buttonPushedTask", 2048, NULL, 1, NULL);
  40.  
  41.     gpio_install_isr_service(0);
  42.     gpio_isr_handler_add(PIN_SWITCH, gpio_isr_handler, (void *)PIN_SWITCH);
  43.    
  44.     //  vTaskDelete(NULL); <= If I add this, no exception is thrown
  45. }
Log

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (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:0x3fff0030,len:4
load:0x3fff0034,len:7016
load:0x40078000,len:13212
load:0x40080400,len:4568
0x40080400: _init at ??:?

entry 0x400806f4
I (29) boot: ESP-IDF v4.2-dirty 2nd stage bootloader
I (29) boot: compile time 21:13:37
I (29) boot: chip revision: 1
I (32) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode       : DIO
I (49) boot.esp32: SPI Flash Size : 2MB
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (84) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot: End of partition table
I (96) boot_comm: chip revision: 1, min. application chip revision: 0
I (103) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x064cc ( 25804) map
I (122) esp_image: segment 1: paddr=0x000164f4 vaddr=0x3ffb0000 size=0x0206c (  8300) load
I (126) esp_image: segment 2: paddr=0x00018568 vaddr=0x40080000 size=0x00x40080000: _WindowOverflow4 at C:/esp/esp-idf/components/freertos/xtensa/xtensa_vectors.S:1730

I (130) esp_image: segment 3: paddr=0x00018974 vaddr=0x40080404 size=0x076a4 ( 30372) load
I (152) esp_image: segment 4: paddr=0x00020020 vaddr=0x400d0020 size=0x134e0 ( 79072) map
0x400d0020: _stext at ??:?

I (183) esp_image: segment 5: paddr=0x00033508 vaddr=0x40087aa8 size=0x02520 (  9504) load
0x40087aa8: esp_log_write at C:/esp/esp-idf/components/log/log.c:196

I (193) boot: Loaded app from partition at offset 0x10000
I (193) boot: Disabling RNG early entropy source...
I (194) cpu_start: Pro cpu up.
I (198) cpu_start: Application information:
I (203) cpu_start: Project name:     blink
I (208) cpu_start: App version:      1
I (212) cpu_start: Compile time:     Apr 18 2021 21:12:57
I (218) cpu_start: ELF file SHA256:  84c3f343b23feb95...
I (224) cpu_start: ESP-IDF:          v4.2-dirty
I (229) cpu_start: Starting app cpu, entry point is 0x400815c8
0x400815c8: call_start_cpu1 at C:/esp/esp-idf/components/esp32/cpu_start.c:289

I (0) cpu_start: App cpu up.
I (240) heap_init: Initializing. RAM available for dynamic allocation:
I (247) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (253) heap_init: At 3FFB28A0 len 0002D760 (181 KiB): DRAM
I (259) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (265) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (272) heap_init: At 40089FC8 len 00016038 (88 KiB): IRAM
I (278) cpu_start: Pro cpu start user code
I (296) spi_flash: detected chip: generic
I (297) spi_flash: flash io: dio
W (297) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (307) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d189c: e5b7f665 00000131 0c004136
0x400d189c: main_task at C:/esp/esp-idf/components/esp32/cpu_start.c:594 (discriminator 2)

Core  0 register dump:
PC      : 0x400d18a2  PS      : 0x00060030  A0      : 0x800846b0  A1      : 0x3ffb4710
0x400d18a2: main_task at cpu_start.c:?

A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00060023  A5      : 0x3ffb22c8
A6      : 0x3ffb47c8  A7      : 0x00000000  A8      : 0x800d18a2  A9      : 0x3ffb46e0
A10     : 0x3ff48000  A11     : 0x00000001  A12     : 0x00000001  A13     : 0x00060023
A14     : 0x00000001  A15     : 0x00060023  SAR     : 0x00000011  EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff

Backtrace:0x400d189f:0x3ffb4710 0x400846ad:0x3ffb4740
0x400d189f: main_task at C:/esp/esp-idf/components/esp32/cpu_start.c:602 (discriminator 2)

0x400846ad: vPortTaskWrapper at C:/esp/esp-idf/components/freertos/xtensa/port.c:143



ELF file SHA256: 84c3f343b23feb95

Rebooting...

ESP_Sprite
Posts: 8999
Joined: Thu Nov 26, 2015 4:08 am

Re: Why does this code throws IllegalInstruction after starting the scheduler?

Postby ESP_Sprite » Wed Apr 21, 2021 1:44 am

That certainly is odd, especially given the first thing ESP-IDF does after app_main returns is calling vTaskDelete(NULL) itself... it almost sounds like you managed to corrupt the stack somehow, but there's nothing in your code that would do that.

Just to be sure, this is with an otherwise unmodified esp-idf? (Asking as the git tag says '-dirty')

noobiewan
Posts: 4
Joined: Tue Apr 20, 2021 12:42 pm

Re: Why does this code throws IllegalInstruction after starting the scheduler?

Postby noobiewan » Wed Apr 21, 2021 2:42 am

ESP_Sprite wrote:
Wed Apr 21, 2021 1:44 am
Just to be sure, this is with an otherwise unmodified esp-idf? (Asking as the git tag says '-dirty')
Wow! Just wow! I cloned the esp-idf again to make sure and it just worked! I have no idea what happened, maybe something went wrong while I was trying to setup my environment.

I'm going to bed happy tonight :lol:

Many thanks, awesome catch!

ESP_Sprite
Posts: 8999
Joined: Thu Nov 26, 2015 4:08 am

Re: Why does this code throws IllegalInstruction after starting the scheduler?

Postby ESP_Sprite » Wed Apr 21, 2021 8:02 am

Heh, usually diagnosing these kinds of problems is harder :) Great that the issue is solved.

Who is online

Users browsing this forum: GilchristT and 89 guests