diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 5ab2b84762..5dfcddb506 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -209,6 +209,7 @@ unsigned long millis(void); unsigned long micros(void); uint64_t micros64(void); void delay(unsigned long); +void interruptable_delay(unsigned long ms); void delayMicroseconds(unsigned int us); unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); diff --git a/cores/esp8266/core_esp8266_wiring.cpp b/cores/esp8266/core_esp8266_wiring.cpp index 247a02bbda..4df56b0e32 100644 --- a/cores/esp8266/core_esp8266_wiring.cpp +++ b/cores/esp8266/core_esp8266_wiring.cpp @@ -24,6 +24,7 @@ #include "osapi.h" #include "user_interface.h" #include "cont.h" +#include extern "C" { @@ -43,7 +44,7 @@ void delay_end(void* arg) { esp_schedule(); } -void delay(unsigned long ms) { +void interruptable_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); @@ -56,6 +57,22 @@ void delay(unsigned long ms) { } } +void delay(unsigned long ms) { + if (ms) { + using esp8266::polledTimeout::oneShotMs; + oneShotMs waitDone(ms); + while(!waitDone) { + os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0); + os_timer_arm(&delay_timer, 1, ONCE); + esp_yield(); + os_timer_disarm(&delay_timer); + } + } else { + esp_schedule(); + esp_yield(); + } +} + void micros_overflow_tick(void* arg) { (void) arg; uint32_t m = system_get_time(); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 31daf370a0..086e2ae6fc 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -565,7 +565,11 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul aResult = IPAddress(&addr); } else if(err == ERR_INPROGRESS) { _dns_lookup_pending = true; - delay(timeout_ms); + // Following delay will be interrupted by dns found callback + for (decltype(timeout_ms) i = 0; _dns_lookup_pending && i < timeout_ms; i++) { + // let a chance to recurrent scheduled functions (ethernet) + delay(1); + } _dns_lookup_pending = false; // will return here when dns_found_callback fires if(aResult.isSet()) { @@ -597,6 +601,7 @@ void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *ca if(ipaddr) { (*reinterpret_cast(callback_arg)) = IPAddress(ipaddr); } + _dns_lookup_pending = false; esp_schedule(); // resume the hostByName function }