Skip to content

Crashes and libraries that use interrupts #1020

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

Closed
adrian-dybwad opened this issue Nov 14, 2015 · 19 comments
Closed

Crashes and libraries that use interrupts #1020

adrian-dybwad opened this issue Nov 14, 2015 · 19 comments

Comments

@adrian-dybwad
Copy link
Contributor

I wanted to let others know that if you use a library that uses interrupts, those interrupt events may cause your ESP to crash if you call WiFi or WiFiClient functions and an interrupt happens while the call is in progress.

I experienced this with the excellent SoftwareSerial port from here: https://github.com/plerup/espsoftwareserial.

The solution was to add functions to the library to disable the interrupts and then call that disable function before calling the WiFi and Client functions, then re-enable interrupts after.

Hope this helps some who may be experiencing crashes that may be hard to stop or explain!

@me-no-dev
Copy link
Collaborator

maybe if the library starts to use our attachInterrupt/detachInterrupt routines, then it might work.

@adrian-dybwad
Copy link
Contributor Author

Thanks for the tip, I will look at that.

@igrr
Copy link
Member

igrr commented Nov 15, 2015

I have taken a look at this library and couldn't find any obvious reason why it would have to crash, especially during WiFi events. This needs some investigation, perhaps @plerup can give some pointers.

@kdsoo
Copy link

kdsoo commented Nov 17, 2015

I'm having similar symptom not with some specific library but attachInterrupt().
First I suspected #797 issue but problem still remains after I change to staged branch.
Strange thing is when I get rid of whole code except attachinterrupt() and interrupt handler callback function, the problem disappears.
The extra code I get rid of is only about esp8266WebServer server(80)... server.handleClient() and EEPROM code which is only called on http request.
esp node crashes and reboot when external interrupt happens.
I attached PIR sensor on gpio 12 by the way. And it gives rising signal on presence detection.
esp node hangs exactly when signal rises and reboots after few secs.

@adrian-dybwad
Copy link
Contributor Author

I had to disable the interrupts while processing WebServer requests too.

@plerup
Copy link
Contributor

plerup commented Nov 17, 2015

@adrionics What baud rate are you using?

@adrian-dybwad
Copy link
Contributor Author

9600 or 2400, both have the problem.

@kdsoo
Copy link

kdsoo commented Nov 18, 2015

I tried disabling interrupt while handling http request and enabling back afterward but still crashing.
I tried like follows..
......

const int PIR = 12;

void loop(void) {
disableInterrupt();
server.handleClient();
enableInterrupt();
}

void enableInterrupt() {
attachInterrupt(PIR, processPresence, RISING);
}

void disableInterrupt() {
detachInterrupt(PIR);
}

void processPresence() {
digitalWrite(13, 1);
}

....

@adrian-dybwad
Copy link
Contributor Author

I fixed it by adding these two functions to the ESP SoftwareSerial library:
void SoftwareSerial::disable() {
ETS_GPIO_INTR_DISABLE();
}
void SoftwareSerial::enable() {
ETS_GPIO_INTR_ENABLE();
}

Then, call them before and after.

@kdsoo
Copy link

kdsoo commented Nov 18, 2015

I found a glitch in my code. I forgot to comment out a single debugging message using softwareserial....
anyway it seems to be working fine with @adrionics 's workaround for now.
cheers!

@plerup
Copy link
Contributor

plerup commented Nov 18, 2015

If it helps just to disable the interrupts then there must be something triggering them when you get the crash. Maybe noise on the used rx pin.

As I stated in the comment in the source code the interrupt seems to trigger on any change on the pin although GPIO_PIN_INTR_NEGEDGE has been set.

I assume it's not real serial traffic in your case because then you would have missed it by disabling the interrupts.

@adrian-dybwad
Copy link
Contributor Author

plerup, I suspect it may be noise or real serial traffic that triggers the crash - as you say, any change on the pin. I sometimes do miss serial packets while the interrupt is disabled, but luckily in my case this does not matter.

I believe that if an interrupt is triggered for any reason, and the device is in the process of handling some routine like WiFi.begin, it crashes.

@adrian-dybwad
Copy link
Contributor Author

PS, it becomes more apparent with a fast stream of serial data. If the serial data is at a low rate, you may need to wait for an exact "while the WiFi / server" functions are called interrupt crash.

@supersjimmie
Copy link

I too can confirm this issue. My code keeps crashing during wificlient sessions with SofwareSerial enabled. After using @adrionics disable/enable functions it seems to be working better.

@adrian-dybwad
Copy link
Contributor Author

@plerup has a new version of the library that seems to work really well and incorporates more features of the original like inverted serial and multiple ports. The enable / disable function seems to fix the interrupt problems. Well done @plerup!!

@chakri1vr1
Copy link

@adrionics You sir, are a life saver !!

Was using a interrupt based power measurement on ESP while making HTTP requests. It used to crash randomly and was unable to debug. Boy oh boy did this post come handy !! Switched to non-interrupt based power measurement and its working fine now :)

@dforco
Copy link

dforco commented Mar 16, 2017

Im having similar problem, using WIFI_AP_STA mode, WebServer on port 80, and simultaneous connecting to a local network. When enabling a timer 0 or 1 interrupt to blink a led, ESP start to crash randomly.

@paidforby
Copy link

Are you using the generic C wifi functions or the Arduino ESP8266WiFi header? I had a dreadful problem where my ESP8266 kept crashing on an interrupt. The interrupt was triggering a sequence of wifi, webserver, and dnsserver setup functions. At first I assumed it was just something with the interrupt. However, after narrowing down the possibilities, I discovered it was caused by mix and matching generic C wifi functions and Arduino WiFi methods. I had something like this:

//THIS IS WRONG
wifi_station_disconnect();
wifi_set_opmode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(ap);

instead of something like this:

//THIS IS RIGHT
WiFi.disconnect();
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(ap);

Hope this helps some people!

@faraonc
Copy link

faraonc commented Dec 6, 2017

AltSoftSerial solved the problem for me.

I initially had SoftwareSerial and my AJAX requests would get errors and disrupts Serial communications.

https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

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

10 participants