title: load prohibited on esp32 during ISR execution
body: <p>I have set up an interrupt that changes a boolean to true and in void loop(), I am constantly checking if the boolean is true like so:</p>
  <pre><code>TTGOClass *ttgo;
  bool irq = false;
  
  void setup()
  {
      Serial.begin(115200);
      ttgo = TTGOClass::getWatch();
      ttgo-&gt;begin();
      ttgo-&gt;openBL();
  
      ttgo-&gt;tft-&gt;fillScreen(TFT_BLACK);
      ttgo-&gt;tft-&gt;drawString(&quot;T-Watch AXP202&quot;,  25, 50, 4);
      ttgo-&gt;tft-&gt;setTextFont(4);
      ttgo-&gt;tft-&gt;setTextColor(TFT_WHITE, TFT_BLACK);
  
      pinMode(AXP202_INT, INPUT_PULLUP);
      attachInterrupt(AXP202_INT, [] {
          irq = true;
      }, FALLING);
  
      //!Clear IRQ unprocessed  first
      ttgo-&gt;power-&gt;enableIRQ(AXP202_PEK_SHORTPRESS_IRQ | AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_IRQ, true);
      ttgo-&gt;power-&gt;clearIRQ();
  }
  
  void loop()
  {
      if (irq) {
          irq = false;
          ttgo-&gt;power-&gt;readIRQ();
          if (ttgo-&gt;power-&gt;isVbusPlugInIRQ()) {
              ttgo-&gt;tft-&gt;fillRect(20, 100, 200, 85, TFT_BLACK);
              ttgo-&gt;tft-&gt;drawString(&quot;Power Plug In&quot;, 25, 100);
          }
          if (ttgo-&gt;power-&gt;isVbusRemoveIRQ()) {
              ttgo-&gt;tft-&gt;fillRect(20, 100, 200, 85, TFT_BLACK);
              ttgo-&gt;tft-&gt;drawString(&quot;Power Remove&quot;, 25, 100);
          }
          if (ttgo-&gt;power-&gt;isPEKShortPressIRQ()) {
              ttgo-&gt;tft-&gt;fillRect(20, 100, 200, 85, TFT_BLACK);
              ttgo-&gt;tft-&gt;drawString(&quot;PowerKey Press&quot;, 25, 100);
          }
          ttgo-&gt;power-&gt;clearIRQ();
      }
      delay(1000);
  }
  </code></pre>
  <p>The t-watch 2020 (an esp32 smartwatch) runs on a battery and this weird method found in its example sketches (the code above is an example not the actual code because it is quite chaotic) wastes quite a lot of precious battery power. So I tried throwing the code from the <code>if</code> inside <code>void loop()</code> right into <code>attachInterrupt(AXP202_INT, []</code> but... I needed to execute lengthy commands (I think that's the cause of the problem)such as <code>Serial.print()</code> and <code>delay()</code> (they are absolutely necesarry), the esp32 crashes returning the following error:</p>
  <pre><code>16:18:32.486 -&gt; Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
  16:18:32.486 -&gt; Core 0 register dump:
  16:18:32.486 -&gt; PC      : 0x4009947b  PS      : 0x00060233  A0      : 0x8009894f  A1      : 0x3ffbd150  
  16:18:32.486 -&gt; A2      : 0x3ffba5a8  A3      : 0x3ffbd2dc  A4      : 0x00000001  A5      : 0x00000001  
  16:18:32.486 -&gt; A6      : 0x00060223  A7      : 0x00000000  A8      : 0x00000000  A9      : 0x3ffba5a8  
  16:18:32.486 -&gt; A10     : 0x3ffba5a8  A11     : 0x00060023  A12     : 0x00060021  A13     : 0x3ffc0910  
  16:18:32.486 -&gt; A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x00000000  EXCCAUSE: 0x0000001c  
  16:18:32.519 -&gt; EXCVADDR: 0x00000004  LBEG    : 0x40093948  LEND    : 0x40093964  LCOUNT  : 0x00000000  
  16:18:32.519 -&gt; 
  16:18:32.519 -&gt; ELF file SHA256: 0000000000000000
  16:18:32.519 -&gt; 
  16:18:32.519 -&gt; Backtrace: 0x4009947b:0x3ffbd150 0x4009894c:0x3ffbd170 0x4009726f:0x3ffbd190 0x400972fd:0x3ffbd1b0 0x40098fbe:0x3ffbd1d0 0x4009909f:0x3ffbd210 0x40096b06:0x3ffbd240
  16:18:32.519 -&gt; 
  16:18:32.519 -&gt; Rebooting...
  </code></pre>
  <p>the code is like this:</p>
  <pre><code>TTGOClass *ttgo;
  
  bool axpIrq = false;  //axpIrq for button press and power plug in/remove events.
  bool lenergy = false;
  bool BLaudio = false;
  bool keepAwake = false;
  
  //some initializations
  ttgo = TTGOClass::getWatch();
  ttgo-&gt;begin();
  ttgo-&gt;lvgl_begin();
  
    pinMode(AXP202_INT, INPUT_PULLUP);
    attachInterrupt(AXP202_INT, [] {
      axpIrq = true;
          ttgo-&gt;power-&gt;readIRQ();
      if (ttgo-&gt;power-&gt;isPEKShortPressIRQ()) {
        //ttgo-&gt;power-&gt;clearIRQ();
  
        Serial.println(&quot;button pressed&quot;);
        low_energy();
      }
      ttgo-&gt;power-&gt;clearIRQ();
    }, FALLING);
  </code></pre>
  <p>low energy is defined in another file:</p>
  <pre><code>void low_energy() {
    //portENTER_CRITICAL(&amp;synch);
  
    if (!keepAwake) {
      if (ttgo-&gt;bl-&gt;isOn()) {
        //Serial.println(&quot;backlight on, turning off&quot;);
  
        ttgo-&gt;closeBL();
        ttgo-&gt;stopLvglTick();
        ttgo-&gt;bma-&gt;enableStepCountInterrupt(false);
        ttgo-&gt;displaySleep();
  
        //lenergy = true;
        gpio_wakeup_enable ((gpio_num_t)AXP202_INT, GPIO_INTR_LOW_LEVEL);
        gpio_wakeup_enable ((gpio_num_t)BMA423_INT1, GPIO_INTR_HIGH_LEVEL);
        esp_sleep_enable_gpio_wakeup ();
  
        if (!BLaudio) {
          setCpuFrequencyMhz(20);
          esp_light_sleep_start();
          //Serial.println(&quot;BLaudio is off, light sleep starts&quot;);
        } else {
          //Serial.println(&quot;BLaudio is on, light sleep won't start&quot;);
        }
        //Serial.println(&quot;screen off&quot;);
  
      } else {
        //Serial.println(&quot;Waking up&quot;);
        setCpuFrequencyMhz(160);
        ttgo-&gt;startLvglTick();
        ttgo-&gt;displayWakeup();
        ttgo-&gt;rtc-&gt;syncToSystem();
        lv_disp_trig_activity(NULL);
        ttgo-&gt;openBL();
        ttgo-&gt;bma-&gt;enableStepCountInterrupt();
        displayTime(true);
      }
    }
    //portEXIT_CRITICAL(&amp;synch);
  }
  </code></pre>
  <p>I've tried replacing <code>bool axpIrq = false;</code> with <code>volatile bool axpIrq = false;</code>, uncommenting <code>portENTER_CRITICAL(&amp;synch);</code> and <code>portEXIT_CRITICAL(&amp;synch);</code> and still no results. If I am right when I think that lengthy commands are the problem, how can I execute the commands while the CPU continues in void loop normally(make a callback executed on the second core? I don't know)? If I am not right, what's the actual problem and how can I solve it?</p>