Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttachInterrupt causing ESP8266 to crash #1734

Closed
rafaeljohansen opened this issue Mar 8, 2016 · 1 comment
Closed

AttachInterrupt causing ESP8266 to crash #1734

rafaeljohansen opened this issue Mar 8, 2016 · 1 comment

Comments

@rafaeljohansen
Copy link

Basic Infos

OS: Windows 7
IDE: Arduino 1.6.5r5
USB to Serial: SiLabs CP2102

Hardware

Hardware: Nodemcu 1.0
Core Version: 2.1.0

Description

Problem description:
I'm receiving a PWM signal from a RC-servo receiver, using attachinterrupt() to detect the pin change. Seemingly the code runs smoothly but at random moments it dumps the memory with Exception (0). Using the Exception Decoder the dumped memory refers to _attachInterrupt on the file core_esp8266_wiring_digital.c line 156. But when I opened this file I realized line 156 is actually found in the detachInterrupt function, not attachInterrupt. Very strange..
Well, at least I don't find no way to prevent the memory dump.

Settings in IDE

Module: ?Generic ESP8266 Module?
Flash Size: ?4MB/3MB?
CPU Frequency: ?80Mhz?
Flash Mode: ?DIO?
Flash Frequency: ?40Mhz?
Upload Using: ?SERIAL?
Reset Method: ?nodemcu?

Sketch

extern "C" {
#include "user_interface.h"
}

uint8_t pwmPin1 = 12; 
uint8_t pwmPin2 = 13; 

struct ICACHE_RAM_ATTR pwmInterrupt
{
  uint32_t counter;
  uint32_t duration;
  uint8_t pwmPin;
  uint32_t previousPosition;
  uint32_t position;

  // Constructor
  pwmInterrupt(uint8_t t_pwmPin)
  {
    pwmPin = t_pwmPin;
  }
};

pwmInterrupt PWM_1(pwmPin1);
pwmInterrupt PWM_2(pwmPin2);

unsigned long ICACHE_RAM_ATTR customMicros()
{

  return system_get_time();

}

void ICACHE_RAM_ATTR rising1()
{
  //(pin, method, trigger state)
  PWM_1.counter = customMicros();
  attachInterrupt(PWM_1.pwmPin, falling1, FALLING);
  //Serial.println("Rising");
}

void ICACHE_RAM_ATTR falling1()
{
  //volatile uint8_t pwmDev = t_pwmDev;
  PWM_1.duration = customMicros() - PWM_1.counter;
  calculate(PWM_1);
  attachInterrupt(PWM_1.pwmPin, rising1, RISING);
  //Serial.println("Falling");
}

void ICACHE_RAM_ATTR rising2()
{
  //volatile uint8_t pwmDev = t_pwmDev;
  //(pin, method, trigger state)
  PWM_2.counter = customMicros();
  attachInterrupt(PWM_2.pwmPin, falling2, FALLING);
  //Serial.println("Rising");
}

void ICACHE_RAM_ATTR falling2()
{
  //volatile uint8_t pwmDev = t_pwmDev;
  PWM_2.duration = customMicros() - PWM_2.counter;
  calculate(PWM_2);
  attachInterrupt(PWM_2.pwmPin, rising2, RISING);
  //Serial.println("Falling");
}

void ICACHE_RAM_ATTR calculate(struct ICACHE_RAM_ATTR pwmInterrupt &pwmDev)
{
  pwmDev.previousPosition = pwmDev.position;
  pwmDev.position = (pwmDev.duration - 900) / 35 + 34;

  // Set position = 50 if out of range (less than 38 or more than 62)
  (pwmDev.position > 62 || pwmDev.position < 38) ? pwmDev.position = pwmDev.previousPosition : pwmDev.position;
}


void setup() 
{
  Serial.begin(250000);

  attachInterrupt(pwmPin1, falling1, FALLING);
  attachInterrupt(pwmPin2, falling2, FALLING);
}

void loop()
{
  delay(100);
  Serial.print(PWM_1.position);
  Serial.print("\t");
  Serial.println(PWM_2.position);
}

Debug Messages

Memory dump:


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

ctx: sys 
sp: 3ffffc20 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffdc0:  000005e0 00000000 401029bd 3ffe8ef0  
3ffffdd0:  40106fdc 00000000 00000002 4010702a  
3ffffde0:  ffffffff 00000020 00000001 00000000  
3ffffdf0:  00000000 40103c9f 00000000 00000022  
3ffffe00:  3fffc200 40106fa4 3fffc258 4000050c  
3ffffe10:  4000438f 00000030 00000016 ffffffff  
3ffffe20:  60000200 00000005 7c7d7c7c 80000000  
3ffffe30:  20000000 3fff06d0 80000000 203fc1a0  
3ffffe40:  00000000 3fffc6fc 00000001 3fff06d4  
3ffffe50:  00000154 003fc1a0 60000600 00000030  
3ffffe60:  00000124 0000000c 3ffeea4c 40106f38  
3ffffe70:  40107040 00080000 00000000 4010702a  
3ffffe80:  ffffffff 00000020 00000000 4000050c  
3ffffe90:  00000000 00000000 0000001f 401016d1  
3ffffea0:  4000050c 40106fa4 3fffc258 4000050c  
3ffffeb0:  40000f68 00000030 00000011 ffffffff  
3ffffec0:  40000f58 00000000 00000020 00000000  
3ffffed0:  00405ba5 00000001 00000000 bfffffff  
3ffffee0:  ffffffff 3fffc6fc 00000001 3fffdab0  
3ffffef0:  00000000 3fffdcb0 3ffe8898 00000030  
3fffff00:  00000000 400042db 3ffe9481 60000600  
3fffff10:  40004b31 3fff0524 000002f4 003fc000  
3fffff20:  4010234e 3ffe8870 3ffe9010 40107364  
3fffff30:  40211bbd 3ffe9010 3ffe8870 00ce889d  
3fffff40:  3fff0524 00001000 40212056 00000008  
3fffff50:  40213fd8 3ffe9164 40212103 3ffe90c4  
3fffff60:  3ffe8870 40205029 3ffe8cdc 3ffe8870  
3fffff70:  40205029 60000600 00000000 40205029  
3fffff80:  4020506e 3fffdab0 00000000 3fffdcb0  
3fffff90:  3ffe8880 3fffdad0 3ffeeb1c 402024e7  
3fffffa0:  40000f49 0001cb29 3fffdab0 40000f49  
<<<stack<<<

Decoded memory dump:


Decoding 33 results
0x402014e8: __attachInterrupt at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_wiring_digital.c line 156
0x401029bd: lmacIsIdle at ?? line ?
0x40106fdc: interrupt_handler at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_wiring_digital.c line 156
0x4010702a: interrupt_handler at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_wiring_digital.c line 156
0x40103c9f: ppProcessTxQ at ?? line ?
0x40106fa4: interrupt_handler at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_wiring_digital.c line 156
0x40106f38: falling1() at C:\Program Files (x86)\Arduino/ESP_servo_pwm_readV3.ino line 46
0x40107040: interrupt_handler at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_wiring_digital.c line 156
0x4010702a: interrupt_handler at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_wiring_digital.c line 156
0x401016d1: ets_timer_disarm at ?? line ?
0x40106fa4: interrupt_handler at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_wiring_digital.c line 156
0x4010234e: spi_flash_read at ?? line ?
0x40107364: pvPortZalloc at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/heap.c line 33
0x40211bbd: pm_set_sleep_time at ?? line ?
0x40212056: pm_get_sleep_type at ?? line ?
0x40213fd8: pp_noise_test at ?? line ?
0x40212103: pm_get_sleep_type at ?? line ?
0x40205029: ets_timer_handler_isr at ?? line ?
0x40205029: ets_timer_handler_isr at ?? line ?
0x40205029: ets_timer_handler_isr at ?? line ?
0x4020506e: ets_timer_handler_isr at ?? line ?
0x402024e7: loop_task at C:\Users\Rafael Johansen\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.1.0\cores\esp8266/core_esp8266_main.cpp line 43
@igrr
Copy link
Member

igrr commented Mar 8, 2016

Well, attachInterrupt is not in IRAM, so calling it from an interrupt handler is not safe.
Need to free up some space in IRAM and move attach/detachInterrupt there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants