TaskScheduler: Task.isEnabled() crashes ESP8266 when run in interrupt

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.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

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.