arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.33k stars 243 forks source link

Task.isEnabled() crashes ESP8266 when run in interrupt #190

Closed CheatingBoy6890 closed 3 months ago

CheatingBoy6890 commented 3 months ago

Hey, first off thanks for this great library, I personally really love it. Sadly I have a problem. I've built a Lasertag program for the ESP8266 and I'm using TaskScheduler in it. I have an ISR that gets triggered when a button ist pressed:

void IRAM_ATTR onShoot()
{ 
  static volatile uint32_t debounce;
  if (millis() > debounce && alive && !TaskShoot.isEnabled())
  {
    TaskShoot.restartDelayed(weapons[myWeaponIndex].getTimeBeforeShot());
    debounce = millis() + 100;
  }
} 

The TaskCallback is the following:

{
  if (millis() >= debounce_shoot && TaskReload.getRunCounter() < 1)
  {

    if (bullets > 0)
    {
      Serial.println(F("shooting"));
      sendMilesTag(myPlayerId, myTeamId, weapons[myWeaponIndex].getDammage());
      bullets--;
      pixels.setPixelColor(LED_SHOOTING, 0xFFFFFF);
      pixels.show();
      playAudio(SHOOT_SOUND);

      drawbullets(0, bullets);

      pixels.setPixelColor(LED_SHOOTING, 0);
      updatePixels();
    }
    else
    {
      playAudio(EMPTY_SOUND);
      // bullets = 0;
    }
    if (TaskShoot.getIterations() == 0 || bullets == 0) // starting the reloading when it's the last iteration of the task (for burst weapons) or the magazine is empty (for full auto)
    {

      Serial.println(F("starting Reload"));

      TaskReload.restartDelayed(weapons[myWeaponIndex].getTimeBeforeReload());
      if (TaskShoot.getIterations() == -1)
      {
        TaskShoot.setIterations(1);
      }
    }
    if (TaskShoot.isLastIteration())
    {
      debounce_shoot = millis() + weapons[myWeaponIndex].getTimebetwennshots();
    }
    if (TaskShoot.getIterations() == -1 && digitalRead(SHOOT_BUTTON))
    {
      TaskShoot.disable();
    }

  }

}

The callback is pretty complex because there are full-auto burst and semi-automatic weapons. So to make it short: the Task Callback sends an IR-packet and checks if the task should stay enabled to keep shooting or disables it self.

The Problem is when you keep pressing the shoot-button the ESP8266 will crash with the following exception:

Exception (0):
epc1=0x40236264 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

Illegal instruction
  epc1=0x40236264 in Task::isEnabled() at .pio/libdeps/d1_mini_debug/TaskScheduler/src/TaskScheduler.h:512

>>>stack>>>

ctx: cont
sp: 3ffff970 end: 3fffffc0 offset: 0150
3ffffac0:  40100a64 00000000 00000000 40100b19  
3ffffad0:  00000020 3fffc6fc 00000079 0031a000  
3ffffae0:  c0034034 00000000 0000001f 40100760  
3ffffaf0:  401006dc 3fff2200 3fffc228 00000022  
3ffffb00:  3fffc200 40100a28 3fffc258 4000050c  
3ffffb10:  4000437d 00000030 00000019 ffffffff  
3ffffb20:  60000200 00000008 ff73ff40 80000000  
3ffffb30:  20000000 3fff22bc 80000000 203098c0  
3ffffb40:  80000000 00000003 00003227 3fff22c0  
3ffffb50:  00000040 003098c0 00309800 00000030  
3ffffb60:  40004b31 3fff2200 00000100 0031a000  
3ffffb70:  401070ac 0030a100 0030a100 00000030  
3ffffb80:  401007b5 3fff2200 00000100 3fff2200  
3ffffb90:  4022af5a 3fff0a7c 0031a000 40227599  
3ffffba0:  00000000 00000000 00000000 40227635  
3ffffbb0:  ffffffff 3fffc6fc 00000079 00000000  
3ffffbc0:  3fff2200 00000100 00000100 00000030  
3ffffbd0:  401006dc 2c9f0300 4000050c 3fffc278  
3ffffbe0:  40103904 40100634 3fffc270 4000050c  
3ffffbf0:  00000000 400042db 0000000a 00000000  
3ffffc00:  00000000 400042db 00000100 00307300  
3ffffc10:  40004b31 3fff2200 00000100 00309800  
3ffffc20:  401070ac 3fff1fd8 00000100 3fff1fd8  
3ffffc30:  401007b5 3fff2200 00000100 3fff2200  
3ffffc40:  00000000 00000000 00309800 40227599  
3ffffc50:  3ffffd42 3ffffd43 00000000 40227635  
3ffffc60:  4022fdb1 3fff17bc 4023a621 3ffe8368  
3ffffc70:  00000001 00000000 3ffffdb0 4023ee40  
3ffffc80:  00f42400 03953bb1 00000000 00000000  
3ffffc90:  00000000 00000000 0000001f 3ffffdb0  
3ffffca0:  00000000 00000000 00000001 40100760  
3ffffcb0:  0000002e 3ffffcf0 0031a000 00000100  
3ffffcc0:  00000100 3fff2200 0031a000 4022af5a  
3ffffcd0:  00000000 0000000a 3ffffd43 3fff17bc  
3ffffce0:  00000002 0001ac00 000001a0 4022fbfc  
3ffffcf0:  00008002 000000f8 000095ba 65722f01  
3ffffd00:  64616f6c 7661772e 00000000 00000000  
3ffffd10:  00000000 00000000 00000000 00000000  
3ffffd20:  0000000b 3ffe9358 ff000000 00766177  
3ffffd30:  00000000 00000000 00000020 3fff1cb2  
3ffffd40:  3fff17bc 0000000b 0000001f 4022fe81  
3ffffd50:  00000002 3ffffdbe 00000000 00000000  
3ffffd60:  00000100 00000080 4022fb88 00000000  
3ffffd70:  00000100 3ffffdde 00000000 00000100  
3ffffd80:  00000100 3fff1fd8 00307300 00000100  
3ffffd90:  00000100 3fff2200 00309800 4022e2c2  
3ffffda0:  00309800 3fff1e9c 3fff17bc 4022bf84  
3ffffdb0:  00000005 3ffffdf0 00000128 40232747  
3ffffdc0:  00000005 3ffffe00 3fff21ec 00000000  
3ffffdd0:  4022bf7c 40104b4c 3fffc228 401064f1  
3ffffde0:  4000050c 40100634 3fffc270 00000100  
3ffffdf0:  3fff17bc 00000020 00009800 4022f928  
3ffffe00:  000000bf 3fff3644 004effd0 3ffffef4  
3ffffe10:  00000018 00000098 3fff1e64 00000013  
3ffffe20:  fffffeb0 00000000 00000001 00000098  
3ffffe30:  000000fb 00000100 3fff17bc 40232134  
3ffffe40:  400001ac 00000030 00000010 ffffffff  
3ffffe50:  3fff1e64 000012a1 00000013 3fff3703  
3ffffe60:  3fff1b9c 00000000 000011e2 000001f4  
3ffffe70:  3fff1e64 3fffc6fc 00000000 000001f4  
3ffffe80:  000095ba 00000000 3fff17bc 4022e529  
3ffffe90:  3fff1e64 00000000 00000000 fffffffe  
3ffffea0:  3fff3644 3fffc6fc 00000000 3ffef8c8  
3ffffeb0:  00000000 00000001 3ffef684 00000001  
3ffffec0:  00000001 3fff24cc 3fff24bc 4022ec42  
3ffffed0:  00000001 3fff2504 3fff24bc 4022c2e8  
3ffffee0:  00000000 3fffdad0 3fff08d8 40237510  
3ffffef0:  00330033 aaaaaaaa 3fff2504 4022646c  
3fffff00:  3fff24bc 3fff2504 3fff2504 40237260  
3fffff10:  00000000 00000000 00000001 3ffef45c  
3fffff20:  00000001 3fff24cc 3fff24bc 402266d0  
3fffff30:  00000000 d2f1a9fb 0000ab52 3ffef684  
3fffff40:  00000919 00000000 00000919 4020427e  
3fffff50:  00000000 051eb851 3ffef8c8 40235f8a  
3fffff60:  00000001 00000000 3ffef8c8 40100101  
3fffff70:  00000000 00000000 3ffef8c8 4020500a  
3fffff80:  00000000 00000000 00000001 3fff08d8  
3fffff90:  3fffdad0 00000000 3fffdab0 4020503f  
3fffffa0:  3fffdad0 00000000 3fffdab0 4022b062  
3fffffb0:  feefeffe feefeffe feefeffe 40101775  
<<<stack<<<

0x40100a64 in interrupt_handler at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_wiring_digital.cpp:144
0x40100b19 in esp8266::InterruptLock::~InterruptLock() at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/interrupts.h:29
 (inlined by) interrupt_handler at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_wiring_digital.cpp:154
0x40100760 in ets_post at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:238
0x401006dc in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x40100a28 in interrupt_handler at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_wiring_digital.cpp:135
0x401070ac in spi_flash_read at ??:?
0x401007b5 in __wrap_spi_flash_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_phy.cpp:320
0x4022af5a in yield at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:193
0x40227599 in EspClass::flashRead(unsigned int, unsigned int*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:932
0x40227635 in EspClass::flashRead(unsigned int, unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:911
0x401006dc in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x40103904 in wDev_ProcessFiq at ??:?
0x40100634 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:167
0x401070ac in spi_flash_read at ??:?
0x401007b5 in __wrap_spi_flash_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_phy.cpp:320
0x40227599 in EspClass::flashRead(unsigned int, unsigned int*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:932
0x40227635 in EspClass::flashRead(unsigned int, unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:911
0x4022fdb1 in spiffs_obj_lu_find_entry_visitor at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:166 (discriminator 1)
0x4023a621 in _printf_i at /workdir/repo/newlib/newlib/libc/stdio/nano-vfprintf_i.c:196 (discriminator 1)
0x4023ee40 in __ssputs_r at /workdir/repo/newlib/newlib/libc/stdio/nano-vfprintf.c:232
0x40100760 in ets_post at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:238
0x4022af5a in yield at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:193
0x4022fbfc in spiffs_object_find_object_index_header_by_name_v at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:1690
0x4022fe81 in spiffs_obj_lu_find_entry_visitor at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:186 (discriminator 4)
0x4022fb88 in spiffs_object_find_object_index_header_by_name_v at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:1679
0x4022e2c2 in flash_hal_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/flash_hal.cpp:36
0x4022bf84 in spiffs_impl::SPIFFSImpl::spiffs_hal_read(unsigned int, unsigned int, unsigned char*) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs_api.h:343
0x40232747 in spiffs_phys_rd at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_cache.cpp:163
0x4022bf7c in spiffs_impl::SPIFFSImpl::spiffs_hal_read(unsigned int, unsigned int, unsigned char*) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs_api.h:342
0x40104b4c in lmacProcessTXStartData at ??:?
0x401064f1 in ets_timer_disarm at ??:?
0x40100634 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:167
0x4022f928 in spiffs_page_data_check at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:28
0x40232134 in spiffs_object_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:2053
0x4022e529 in spiffs_hydro_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_hydrogen.cpp:417
0x4022ec42 in SPIFFS_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_hydrogen.cpp:430
0x4022c2e8 in spiffs_impl::SPIFFSFileImpl::read(unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs_api.h:395 (discriminator 1)
0x40237510 in fs::File::read(unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/FS.cpp:73
0x4022646c in AudioFileSourceFS::read(void*, unsigned int) at .pio/libdeps/d1_mini_debug/ESP8266Audio/src/AudioFileSourceFS.cpp:49
0x40237260 in AudioGeneratorWAV::GetBufferedData(int, void*) at .pio/libdeps/d1_mini_debug/ESP8266Audio/src/AudioGeneratorWAV.cpp:67 (discriminator 4)
0x402266d0 in AudioGeneratorWAV::loop() at .pio/libdeps/d1_mini_debug/ESP8266Audio/src/AudioGeneratorWAV.cpp:98
0x4020427e in loopAudio() at src/main.cpp:480
0x40235f8a in std::_Function_handler<void (), void (*)()>::_M_invoke(std::_Any_data const&) at /home/silas/.platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/10.3.0/bits/std_function.h:293
0x40100101 in std::function<void ()>::operator()() const at /home/silas/.platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/10.3.0/bits/std_function.h:623
0x4020500a in Scheduler::execute() at .pio/libdeps/d1_mini_debug/TaskScheduler/src/TaskScheduler.h:1554
0x4020503f in loop at src/main.cpp:311
0x4022b062 in loop_wrapper() at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:258
0x40101775 in cont_wrapper at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/cont.S:81

The full source code of my project can be found here.

Thank you for you're help.

arkhipenko commented 3 months ago

I think the problem is the ISR code - it is typically advisable to do as little as possible in it:

void IRAM_ATTR onShoot()
...
    TaskShoot.restartDelayed(weapons[myWeaponIndex].getTimeBeforeShot());
...
} 

The code for getTimeBeforeShot() may not be in memory during ISR call, and an attempt to load it from flash fails. I would use values instead of method calls and maybe place those in IRAM as well.

CheatingBoy6890 commented 3 months ago

I tried replacing the

TaskShoot.restartDelayed(...);

with just

TaskShoot.restart();

and then with a 2 millisecond delay (I know you shouldn't use delay in interrupts). But even then the exception states that exception stays the same:

epc1=0x40236240 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

Illegal instruction
  epc1=0x40236240 in Task::isEnabled() at .pio/libdeps/d1_mini_debug/TaskScheduler/src/TaskScheduler.h:512

>>>stack>>>

ctx: cont
sp: 3ffff900 end: 3fffffc0 offset: 0150
3ffffa50:  40100a28 f0bb379c 77d67f28 40100add  
3ffffa60:  00000020 3ffffa90 3fff0d4c 40242fc8  
3ffffa70:  c0034034 3fff32f4 3ffffab0 402584f2  
3ffffa80:  401006a0 00000002 00000001 00000022  
3ffffa90:  3fffc200 401009ec 3fffc258 4000050c  
3ffffaa0:  4000437d 00000030 00000016 ffffffff  
3ffffab0:  60000200 00000008 ffffffff 80000000  
3ffffac0:  20000000 3fff2140 80000000 20344060  
3ffffad0:  80000000 00000002 00000734 3fff2144  
3ffffae0:  000000a0 00344060 00344000 00000030  
3ffffaf0:  401006a0 401005f8 3fffc270 4000050c  
3ffffb00:  400043e9 401005f8 3fffc270 4000050c  
3ffffb10:  400043a3 00000030 00000016 ffffffff  
3ffffb20:  60000200 00000006 1c261ac4 80000000  
3ffffb30:  20000000 3fff2400 80000000 203438e0  
3ffffb40:  00000000 00000004 000006a4 3fff2404  
3ffffb50:  00000020 003438e0 00343800 00000030  
3ffffb60:  40004b31 3fff1ebc 00000100 00338400  
3ffffb70:  40107070 00000030 0000001c ffffffff  
3ffffb80:  40100779 3fff1ebc 00000100 3fff1ebc  
3ffffb90:  00000000 400042db 00338400 40227575  
3ffffba0:  40004b31 3fff20e4 00000100 00344000  
3ffffbb0:  40107070 00000000 00000020 40100724  
3ffffbc0:  40100779 3fff20e4 00000100 3fff20e4  
3ffffbd0:  3ffe9e05 40105c1b 00344000 40227575  
3ffffbe0:  4010350f 3ffed868 0000000a 40227611  
3ffffbf0:  00007fff 0097443e 3ffee124 401036ec  
3ffffc00:  00000000 400042db 00000000 3fff0000  
3ffffc10:  40004b31 3fff1fd0 00000100 00348100  
3ffffc20:  40107070 7fffffff 00000000 00000001  
3ffffc30:  40100779 3fff1fd0 00000100 3fff1fd0  
3ffffc40:  3ffea6a4 00000000 00348100 40227575  
3ffffc50:  401006a0 2c9f0300 4000050c 40227611  
3ffffc60:  401038c8 401005f8 3fffc270 4000050c  
3ffffc70:  401006a0 00000030 0000001c ffffffff  
3ffffc80:  402358c4 401005f8 3fffc270 4000050c  
3ffffc90:  00000000 00000000 0000001f 40100724  
3ffffca0:  4022fdf2 00002000 3fffc228 401064b5  
3ffffcb0:  4000050c 00002000 00000429 00000100  
3ffffcc0:  00000100 00000030 00000017 ffffffff  
3ffffcd0:  4023266c 00000002 00010000 00000100  
3ffffce0:  401006a0 3fff1ebc 00338400 4022e29e  
3ffffcf0:  00000000 00000000 0000001f 40100724  
3ffffd00:  00000100 00000030 3fffc228 401064b5  
3ffffd10:  00000005 3ffffd50 3fff22b0 00000100  
3ffffd20:  00000100 3fff20e4 00344000 4022e29e  
3ffffd30:  00344000 3fff1e94 3fff17b4 4022bf60  
3ffffd40:  3fff17b4 00038400 00000384 40232723  
3ffffd50:  00000100 3fff1c94 3fff20d0 00000000  
3ffffd60:  4022bf58 00000003 3ffffe10 00000000  
3ffffd70:  00000100 00388014 3fff08a4 3fff17b4  
3ffffd80:  3fff17b4 00000000 00000100 4022fdce  
3ffffd90:  00000100 00000e6c 00000000 00000001  
3ffffda0:  00000022 00000080 4022f684 00000000  
3ffffdb0:  00000000 3fff1c94 00008003 00044000  
3ffffdc0:  00000000 3ffffe00 00000128 00000000  
3ffffdd0:  401006a0 00000030 3fffc228 00000161  
3ffffde0:  3ffffe40 00000003 3fff17b4 402302a2  
3ffffdf0:  00000000 3ffffe10 3ffffe00 3ffffe04 <
3ffffe00:  0000001c 00000003 00000005 3ffffef4  
3ffffe10:  00000003 00000000 3fff1dcc 00000160  
3ffffe20:  fffff6d0 00000001 00000001 00000161  
3ffffe30:  00015bf6 00000003 3fff17b4 40232024  
3ffffe40:  40000384 00000030 00000010 401005f8  
3ffffe50:  3fff1dcc 00015a1b 00000161 3fff3655  
3ffffe60:  3fff1b94 00000002 00015a02 000001f4  
3ffffe70:  3fff1dcc 4025cc45 00000000 000001f4  
3ffffe80:  0002cc42 00000000 3fff17b4 4022e505  
3ffffe90:  3fff1dcc 3fffdab0 00000000 3fffdad0  
3ffffea0:  3fff363c 00000030 00000000 fffffffe  
3ffffeb0:  ffffffff 3fffc6fc 00000001 00000001  
3ffffec0:  00000001 3fff24c4 3fff24b4 4022ec1e  
3ffffed0:  00000001 3fff24fc 3fff24b4 4022c2c4  
3ffffee0:  00000000 3fffdad0 3fff08d0 402374ec  
3ffffef0:  0a910a91 aab556aa 3fff08d0 40226448  
3fffff00:  40227bf4 3fff0704 3ffe8ac0 4023723c  
3fffff10:  40227bf4 3fff0704 3ffe8ac0 3ffef454  
3fffff20:  00000001 3fff24c4 3fff24b4 402266ac  
3fffff30:  3ffef7a8 5ed8fc37 3ffef860 3ffef67c  
3fffff40:  000000d3 00000000 000000d3 4020427e  
3fffff50:  00000000 0189374b 3ffef8c0 40235f66  
3fffff60:  00000001 00000000 3ffef8c0 40100101  
3fffff70:  00000001 00000000 3ffef8c0 4020500a  
3fffff80:  00000000 00000000 00000001 3fff08d0  
3fffff90:  3fffdad0 00000000 3fffdab0 40205049  
3fffffa0:  3fffdad0 00000000 3fffdab0 4022b03e  
3fffffb0:  feefeffe feefeffe feefeffe 40101739  
<<<stack<<<

0x40100a28 in interrupt_handler at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_wiring_digital.cpp:144
0x40100add in esp8266::InterruptLock::~InterruptLock() at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/interrupts.h:29
 (inlined by) interrupt_handler at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_wiring_digital.cpp:154
0x40242fc8 in netif_do_set_ipaddr at /local/users/gauchard/arduino/arduino_esp8266/origin/tools/sdk/lwip2/builder/lwip2-src/src/core/netif.c:490
0x402584f2 in wpa_receive at ??:?
0x401006a0 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x401009ec in interrupt_handler at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_wiring_digital.cpp:135
0x401006a0 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x401005f8 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:167
0x401005f8 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:167
0x40107070 in spi_flash_read at ??:?
0x40100779 in __wrap_spi_flash_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_phy.cpp:320
0x40227575 in EspClass::flashRead(unsigned int, unsigned int*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:932
0x40107070 in spi_flash_read at ??:?
0x40100724 in ets_post at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:238
0x40100779 in __wrap_spi_flash_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_phy.cpp:320
0x40105c1b in lmacRxDone at ??:?
0x40227575 in EspClass::flashRead(unsigned int, unsigned int*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:932
0x4010350f in rcReachRetryLimit at ??:?
0x40227611 in EspClass::flashRead(unsigned int, unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:911
0x401036ec in rcReachRetryLimit at ??:?
0x40107070 in spi_flash_read at ??:?
0x40100779 in __wrap_spi_flash_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_phy.cpp:320
0x40227575 in EspClass::flashRead(unsigned int, unsigned int*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:932
0x401006a0 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x40227611 in EspClass::flashRead(unsigned int, unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/Esp.cpp:911
0x401038c8 in wDev_ProcessFiq at ??:?
0x401005f8 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:167
0x401006a0 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x402358c4 in Twi::write_bit(bool) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_si2c.cpp:323
0x401005f8 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:167
0x40100724 in ets_post at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:238
0x4022fdf2 in spiffs_obj_lu_find_entry_visitor at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:173
0x401064b5 in ets_timer_disarm at ??:?
0x4023266c in spiffs_phys_rd at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_cache.cpp:135
0x401006a0 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x4022e29e in flash_hal_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/flash_hal.cpp:36
0x40100724 in ets_post at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:238
0x401064b5 in ets_timer_disarm at ??:?
0x4022e29e in flash_hal_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/flash_hal.cpp:36
0x4022bf60 in spiffs_impl::SPIFFSImpl::spiffs_hal_read(unsigned int, unsigned int, unsigned char*) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs_api.h:343
0x40232723 in spiffs_phys_rd at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_cache.cpp:163
0x4022bf58 in spiffs_impl::SPIFFSImpl::spiffs_hal_read(unsigned int, unsigned int, unsigned char*) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs_api.h:342
0x4022fdce in spiffs_obj_lu_find_entry_visitor at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:168
0x4022f684 in spiffs_obj_lu_find_id_and_span_v at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:509
0x401006a0 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:194
0x402302a2 in spiffs_obj_lu_find_id_and_span at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:539 (discriminator 4)
0x40232024 in spiffs_object_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_nucleus.cpp:2014
0x401005f8 in i2s_slc_isr at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_i2s.cpp:167
0x4025cc45 in cont_continue at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/cont.S:51
0x4022e505 in spiffs_hydro_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_hydrogen.cpp:417
0x4022ec1e in SPIFFS_read at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs/spiffs_hydrogen.cpp:430
0x4022c2c4 in spiffs_impl::SPIFFSFileImpl::read(unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/spiffs_api.h:395 (discriminator 1)
0x402374ec in fs::File::read(unsigned char*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/FS.cpp:73
0x40226448 in AudioFileSourceFS::read(void*, unsigned int) at .pio/libdeps/d1_mini_debug/ESP8266Audio/src/AudioFileSourceFS.cpp:49
0x40227bf4 in HardwareSerial::write(unsigned char const*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/HardwareSerial.h:191
0x4023723c in AudioGeneratorWAV::GetBufferedData(int, void*) at .pio/libdeps/d1_mini_debug/ESP8266Audio/src/AudioGeneratorWAV.cpp:67 (discriminator 4)
0x40227bf4 in HardwareSerial::write(unsigned char const*, unsigned int) at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/HardwareSerial.h:191
0x402266ac in AudioGeneratorWAV::loop() at .pio/libdeps/d1_mini_debug/ESP8266Audio/src/AudioGeneratorWAV.cpp:98
0x4020427e in loopAudio() at src/main.cpp:470
0x40235f66 in std::_Function_handler<void (), void (*)()>::_M_invoke(std::_Any_data const&) at /home/silas/.platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/10.3.0/bits/std_function.h:293
0x40100101 in std::function<void ()>::operator()() const at /home/silas/.platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/10.3.0/bits/std_function.h:623
0x4020500a in Scheduler::execute() at .pio/libdeps/d1_mini_debug/TaskScheduler/src/TaskScheduler.h:1554
0x40205049 in loop at src/main.cpp:303
0x4022b03e in loop_wrapper() at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:258
0x40101739 in cont_wrapper at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/cont.S:81

So I think it's a problem with the TaskShoot.isEnabled method.

arkhipenko commented 3 months ago

Possible. Can you try and get rid of it? Just restart (for now).

CheatingBoy6890 commented 3 months ago

I did and I had the same problem, but when I make the Task::isEnabled function an IRAM_ATTR as well as the TASK::restart function and the Task::enable functions the crash is getting really rare. So it seems that I have to make all the functions or methods IRAM_ATTR.

arkhipenko commented 3 months ago

I thought about that - I will have to make it specific to ESP chips - this is not a problem on other architectures where the code is in memory all the time. For now - go ahead and modify the library until I come up with a perm solution. I hit the same issue on esp32 for one of my projects - so this needs to be fixed.

CheatingBoy6890 commented 3 months ago

Ok thank you

CheatingBoy6890 commented 3 months ago

BTW it seems like the _task_millis() function needs to be in IRAM as well.

arkhipenko commented 3 months ago

That's just crazy - but ok! Seems line esp8266 is a lot more sensitive - esp32 does not have those issues.

arkhipenko commented 3 months ago

just verified - esp32 millis() implementation is already IRAM_ATTR

CheatingBoy6890 commented 3 months ago

So I added the IRAM_ATTR to the isEnabled and other TaskScheduler functions. Sadly I noticed another problem: I'm using painlessmesh and as soon as I connect to another device in the mesh the ESP crashes this time with this exception:

Panic core_esp8266_main.cpp:191 __yield

>>>stack>>>

ctx: sys
sp: 3fffff50 end: 3fffffb0 offset: 0010
3fffff60:  00000000 00000000 3ffef8d0 4022b059  
3fffff70:  00000000 00000000 3ffef8d0 40205097  
3fffff80:  00000000 00000000 00000001 3fff08e0  
3fffff90:  3fffdad0 00000000 3fffdab0 402050bf  
3fffffa0:  3fffdad0 00000000 3fffdab0 4022b14e  
<<<stack<<<

0x4022b059 in yield at ??:?
0x40205097 in Scheduler::execute() at .pio/libdeps/d1_mini_debug/TaskScheduler/src/TaskScheduler.h:1480
0x402050bf in loop at src/main.cpp:311
0x4022b14e in loop_wrapper() at /home/silas/.platformio/packages/framework-arduinoespressif8266/cores/esp8266/core_esp8266_main.cpp:258

What I think is happening (but could also be anything else) is that when the ESP connects to the mesh there are new Tasks created and because the methods from all these are in IRAM it overflows and the ESP crashes.

TD-er commented 3 months ago

Or delay() is called from an ISR function.

arkhipenko commented 3 months ago

I recently prefer to not do much in the ISRs at all - just set some volatile variable and restart an ISR servicing task that runs immediately - that way you are out of the interrupt context. If fast reaction is required - use layered priority to make sure isr servicing task is always scheduled next

chrisalbertson commented 3 months ago

On Aug 14, 2024, at 2:05 PM, Anatoli Arkhipenko @.***> wrote:

I recently prefer to not do much in the ISRs at all - just set some volatile variable and restart an ISR servicing task that runs immediately - that way you are out of the interrupt context.

This has been the best practice for as long as I can remember, going back to the mid-1970s.

That said there are some cases where people have reenable interrupts from within the ISR but that is just asking for trouble that is VERY hard to debug.

CheatingBoy6890 commented 3 months ago

So should I create another Task that is started instantly from the interrupt like

//interrupt handler
void IRAM_ATTR onShoot(){
TaskHandleInterrupt.restart();
}

or just set a volatile variable and check if it changed in a Task?

CheatingBoy6890 commented 3 months ago

I think I fixed it now, the reason for the crash was that painlessmesh doesn't work in debug builds aka build_mode = debug.

arkhipenko commented 3 months ago

So should I create another Task that is started instantly from the interrupt like

Yes, you can do that, OR, if you want to check for stray events from time to time you can create a task that runs every N seconds (for instance) and, if running on a schedule checks for stray events (you need to figure out how). For the true interrupts:

//interrupt handler
volatile bool intOccurred = false; 
void IRAM_ATTR onShoot(){
intOccurred = true; 
TaskHandleInterrupt.forceNextIteration();
}