Page 1 of 1

Timer interrupt and reading/writing flash memory causes a crash

Posted: Thu Jun 28, 2018 5:35 pm
by itart2
I can read and write to the flash memory without problems in the main program. Doing the same in a timer interrupt routine (watchdog) causes a crash. The objective is to save some status values when the watchdog gets triggered.
I thought it might be a timing issue, but increasing the watchdog timeout from 300 to 900ms does not help.

Code: Select all

hw_timer_t *timer = NULL;

void IRAM_ATTR resetModule() {
  int32_t tempValue = 0;
  nvs_handle my_handle;
  esp_err_t err;

  ESP_ERROR_CHECK(nvs_open("storage", NVS_READWRITE, &my_handle));
  ets_printf("**** Program will crash now: ****\n");
  err = nvs_get_i32(my_handle, "TEST", &tempValue);
  ets_printf("value read\n");   // never reaches that point
  esp_restart();
}

void initWatchdog(int wdtTimeout)
{
   timer = timerBegin(0, 80, true);                  //timer 0, div 80
   timerAttachInterrupt(timer, &resetModule, true);  //attach callback
   timerAlarmWrite(timer, wdtTimeout * 1000 * 1000, false); //set time in us
   timerAlarmEnable(timer);
   timerWrite(timer, 0);
}

void app_main()
{
    initWatchdog(2);

    while(1){
      vTaskDelay(50000 / portTICK_RATE_MS);    //
      printf("...\n");
    }
}

Code: Select all

**** Program will crash now: ****
Guru Meditation Error: Core  0 panic'ed (Interrupt wdt timeout on CPU0)
Core 0 register dump:
PC      : 0x40086542  PS      : 0x00060034  A0      : 0x8008594e  A1      : 0x3ffb02e0  
0x40086542: vListInsert at /Users/spadmin/esp/esp-idf/components/freertos/list.c:188 (discriminator 1)

A2      : 0x3ffafcf8  A3      : 0x3ffb4df4  A4      : 0x00060021  A5      : 0x00000001  
A6      : 0x00060021  A7      : 0x3ffb04a4  A8      : 0x3ffb4df4  A9      : 0x3ffb4df4  
A10     : 0x00000019  A11     : 0x00000019  A12     : 0x3ffb04a4  A13     : 0x00000001  
A14     : 0x00060023  A15     : 0xff000000  SAR     : 0x0000001e  EXCCAUSE: 0x00000005  
EXCVADDR: 0x00000000  LBEG    : 0x40001609  LEND    : 0x4000160d  LCOUNT  : 0x00000000  
Core 0 was running in ISR context:
EPC1    : 0x40084493  EPC2    : 0x00000000  EPC3    : 0x00000000  EPC4    : 0x40086542
0x40084493: xQueueGenericSend at /Users/spadmin/esp/esp-idf/components/freertos/queue.c:2037

0x40086542: vListInsert at /Users/spadmin/esp/esp-idf/components/freertos/list.c:188 (discriminator 1)


Backtrace: 0x40086542:0x3ffb02e0 0x4008594b:0x3ffb0300 0x4008482c:0x3ffb0320 0x400d1490:0x3ffb0360 0x400d1532:0x3ffb0380 0x40083108:0x3ffb03a0 0x400835c6:0x3ffb03c0 0x400e0631:0x3ffb0430 0x400e09a7:0x3ffb0450 0x400e0a6d:0x3ffb04a0 0x400dfee3:0x3ffb0510 0x400df8d9:0x3ffb0560 0x400dfacd:0x3ffb05a0 0x40082aa0:0x3ffb05c0 0x40083e9d:0x3ffb05f0 0x400823b5:0x3ffb0610 0x400d1c2b:0x00000000
0x40086542: vListInsert at /Users/spadmin/esp/esp-idf/components/freertos/list.c:188 (discriminator 1)

Re: Timer interrupt and reading/writing flash memory causes a crash

Posted: Thu Jun 28, 2018 11:13 pm
by ESP_igrr

Re: Timer interrupt and reading/writing flash memory causes a crash

Posted: Thu Jun 28, 2018 11:34 pm
by WiFive
He doesn't want to do an interrupt during a flash operation, he wants to do a flash operation during an interrupt.

Even if timerAttachInterrupt didn't set the iram flag calling nvs functions from an interrupt is no bueno.

Re: Timer interrupt and reading/writing flash memory causes a crash

Posted: Fri Jun 29, 2018 3:57 am
by ESP_igrr
Thanks WiFive, I misread the comment. Yes, doing NVS operations from ISR is not supported. Main problem is that NVS uses a mutex to prevent concurrent operations. If there is an NVS operation in progress and your interrupt triggers on the same CPU, NVS call from ISR will try to obtain the mutex which is held by a task. Obviously this is not a good situation, as task can not release the mutex while it is preempted.

Re: Timer interrupt and reading/writing flash memory causes a crash

Posted: Sat Sep 29, 2018 6:29 pm
by czuvich
Is there a way to persist values so that I know how to handle the watchdog trigger after the board is reset? Or is the watchdog trigger not really designed for that?

Re: Timer interrupt and reading/writing flash memory causes a crash

Posted: Sun Sep 30, 2018 11:42 am
by ESP_igrr
You can use RTC_NOINIT_ATTR to place variables into RTC memory, and prevent them from being reinitialized after WDT (or exception/panic) reset.

Re: Timer interrupt and reading/writing flash memory causes a crash

Posted: Sun Sep 30, 2018 5:33 pm
by czuvich
Thank you! Works great.