Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By McChubby007
#76212 No - it has nothing to do with 'mixing system calls' with arduino code. Firstly, 'arduino code' : what does that mean? Second, it is absolutely do do with 'mixing' a delay() call inside a callback most likely called from an ISR - delay() can interact with interruts itself (via get time and so forth) which then becomes recursive/problematic and the stack gets upset - you cannot mix things like that together. It's difficult to make a simple explanation, other than 'don't specifically put a delay() call in there. Same goes for yield(), as you shouldn't hold-up ISRs, but otherwise you will most likely be OK, with what I think you mean by 'arduino' code. You still need to know what you are doing however. It would be too simplistic to say that anything will work in this context as each system/function/api call behaves differently and has its own requirements. In other words, just because a system/api call can be compiled does not make it sensible or correct.

I would also refer to my previous post, and ask why would you want a delay in there, what are you trying to do? It is often better to express what you want to do, and then we can provide a solution rather than just explaining interaction issues such as these. In real 'real-time'/event driven programming using something like delay() in a non-preemptive system would be frowned upon.
User avatar
By McChubby007
#76213 To give you more detail - if you look at delay :
void delay(unsigned long ms) {
if(ms) {
os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
os_timer_arm(&delay_timer, ms, ONCE);
} else {
esp_schedule();
}
esp_yield();
if(ms) {
os_timer_disarm(&delay_timer);
}
}

You can see that it calls 'esp_schedule' - this calls ets_post() which flags the main loop for execution, yield() is then called which allows the effects of ets_post to take place, causing loop() to run. If this is done inside a function already 'called-back' from another function (whether that is a system function or not, which itslef might have been called from loop() in the first place) then the system 'shoots off' in a asymetric direction and never recovers because the stack never unwinds correctly.
In general, any published api, it should mention restrictions on how it can be used. But suffice to say that yield() and delay() should always be treated with care.