1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-02 14:22:55 +03:00

Implement esp_yield() as a replacement for delay(0)

esp_yield() now also calls esp_schedule(), original esp_yield() function renamed to esp_suspend().

Don't use delay(0) in the Core internals, libraries and examples. Use yield() when the code is
supposed to be called from CONT, use esp_yield() when the code can be called from either CONT or SYS.
Clean-up esp_yield() and esp_schedule() declarations across the code and use coredecls.h instead.

Implement helper functions for libraries that were previously using esp_yield(), esp_schedule() and
esp_delay() directly to wait for certain SYS context tasks to complete. Correctly use esp_delay()
for timeouts, make sure scheduled functions have a chance to run (e.g. LwIP_Ethernet uses recurrent)

Related issues:
- #6107 - discussion about the esp_yield() and esp_delay() usage in ClientContext
- #6212 - discussion about replacing delay() with a blocking loop
- #6680 - pull request introducing LwIP-based Ethernet
- #7146 - discussion that originated UART code changes
- #7969 - proposal to remove delay(0) from the example code
- #8291 - discussion related to the run_scheduled_recurrent_functions() usage in LwIP Ethernet
- #8317 - yieldUntil() implementation, similar to the esp_delay() overload with a timeout and a 0 interval
This commit is contained in:
Dirk O. Kaar
2021-10-16 23:19:01 +02:00
committed by GitHub
parent 40b26b769c
commit c312a2eaf1
32 changed files with 286 additions and 213 deletions

View File

@ -25,6 +25,7 @@
#include "PolledTimeout.h"
#include "ESP8266WiFiMulti.h"
#include <coredecls.h>
#include <limits.h>
#include <string.h>
@ -82,37 +83,29 @@ static void printWiFiStatus(wl_status_t status)
*/
static wl_status_t waitWiFiConnect(uint32_t connectTimeoutMs)
{
wl_status_t status;
wl_status_t status = WL_CONNECT_FAILED;
// The final argument, intvl_ms, to esp_delay influences how frequently
// the scheduled recurrent functions (Schedule.h) are probed.
esp_delay(connectTimeoutMs,
[&status]() {
status = WiFi.status();
return status != WL_CONNECTED && status != WL_CONNECT_FAILED;
}, 0);
// Set WiFi connect timeout
using esp8266::polledTimeout::oneShotMs;
oneShotMs connectTimeout(connectTimeoutMs);
// Check status
if (status == WL_CONNECTED) {
// Connected, print WiFi status
printWiFiStatus(status);
// Wait for WiFi status change or timeout
do {
// Refresh watchdog
delay(0);
// Get WiFi status
status = WiFi.status();
// Check status
if (status == WL_CONNECTED) {
// Connected, print WiFi status
printWiFiStatus(status);
// Return WiFi status
return status;
} else if (status == WL_CONNECT_FAILED) {
DEBUG_WIFI_MULTI("[WIFIM] Connect failed\n");
// Return WiFi connect failed
return WL_CONNECT_FAILED;
}
} while (!connectTimeout);
DEBUG_WIFI_MULTI("[WIFIM] Connect timeout\n");
// Return WiFi status
return status;
} else if (status == WL_CONNECT_FAILED) {
DEBUG_WIFI_MULTI("[WIFIM] Connect failed\n");
} else {
DEBUG_WIFI_MULTI("[WIFIM] Connect timeout\n");
}
// Return WiFi connect failed
return WL_CONNECT_FAILED;
}
@ -242,24 +235,19 @@ int8_t ESP8266WiFiMulti::startScan()
// Start wifi scan in async mode
WiFi.scanNetworks(true);
// Set WiFi scan timeout
using esp8266::polledTimeout::oneShotMs;
oneShotMs scanTimeout(WIFI_SCAN_TIMEOUT_MS);
// Wait for WiFi scan change or timeout
do {
// Refresh watchdog
delay(0);
// Check scan timeout which may occur when scan does not report completion
if (scanTimeout) {
DEBUG_WIFI_MULTI("[WIFIM] Scan timeout\n");
return WIFI_SCAN_FAILED;
}
// Get scan result
scanResult = WiFi.scanComplete();
} while (scanResult < 0);
// The final argument, intvl_ms, to esp_delay influences how frequently
// the scheduled recurrent functions (Schedule.h) are probed.
esp_delay(WIFI_SCAN_TIMEOUT_MS,
[&scanResult]() {
scanResult = WiFi.scanComplete();
return scanResult < 0;
}, 0);
// Check for scan timeout which may occur when scan does not report completion
if (scanResult < 0) {
DEBUG_WIFI_MULTI("[WIFIM] Scan timeout\n");
return WIFI_SCAN_FAILED;
}
// Print WiFi scan result
printWiFiScan();
@ -535,7 +523,7 @@ void ESP8266WiFiMulti::printWiFiScan()
rssi,
(encryptionType == ENC_TYPE_NONE) ? ' ' : '*',
ssid.c_str());
delay(0);
esp_yield();
}
#endif
}