Skip to content

Commit 6f23078

Browse files
Undo #1864, fix LWIP offline error
Fixes #1973 The periodic LWIP pump/Ethernet packet reader async_context stopped firing occasionally under high packet loads, causing the LWIP stack to become unresponsive to any incoming data. Re-implement the 2-step process for polling (like the CYW43 driver from the RPI folks does) and undoes #1864 change.
1 parent 9c66d97 commit 6f23078

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

libraries/lwIP_Ethernet/src/LwipEthernet.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bool __ethernetContextInitted = false;
3131

3232
// Async context that pumps the ethernet controllers
3333
static async_context_threadsafe_background_t lwip_ethernet_async_context_threadsafe_background;
34+
static async_when_pending_worker_t always_pending_update_timeout_worker;
3435
static async_at_time_worker_t ethernet_timeout_worker;
3536
static async_context_t *_context = nullptr;
3637

@@ -127,10 +128,12 @@ static async_context_t *lwip_ethernet_init_default_async_context(void) {
127128
return NULL;
128129
}
129130

131+
uint32_t __ethernet_timeout_reached_calls = 0;
130132
static uint32_t _pollingPeriod = 20;
131133
// This will only be called under the protection of the async context mutex, so no re-entrancy checks needed
132-
static void ethernet_timeout_reached(async_context_t *context, __unused async_at_time_worker_t *worker) {
134+
static void ethernet_timeout_reached(__unused async_context_t *context, __unused async_at_time_worker_t *worker) {
133135
assert(worker == &ethernet_timeout_worker);
136+
__ethernet_timeout_reached_calls++;
134137
for (auto handlePacket : _handlePacketList) {
135138
handlePacket.second();
136139
}
@@ -141,6 +144,11 @@ static void ethernet_timeout_reached(async_context_t *context, __unused async_at
141144
#else
142145
sys_check_timeouts();
143146
#endif
147+
}
148+
149+
static void update_next_timeout(async_context_t *context, async_when_pending_worker_t *worker) {
150+
assert(worker == &always_pending_update_timeout_worker);
151+
worker->work_pending = true;
144152
async_context_add_at_time_worker_in_ms(context, &ethernet_timeout_worker, _pollingPeriod);
145153
}
146154

@@ -158,7 +166,9 @@ void __startEthernetContext() {
158166
_context = lwip_ethernet_init_default_async_context();
159167
#endif
160168
ethernet_timeout_worker.do_work = ethernet_timeout_reached;
161-
async_context_add_at_time_worker_in_ms(_context, &ethernet_timeout_worker, _pollingPeriod);
169+
always_pending_update_timeout_worker.work_pending = true;
170+
always_pending_update_timeout_worker.do_work = update_next_timeout;
171+
async_context_add_when_pending_worker(_context, &always_pending_update_timeout_worker);
162172
__ethernetContextInitted = true;
163173
}
164174

libraries/lwIP_Ethernet/src/LwipIntfDev.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ class LwipIntfDev: public LwipIntf, public RawDev {
137137
_spiSettings = s;
138138
}
139139

140+
uint32_t packetsReceived() {
141+
return _packetsReceived;
142+
}
143+
uint32_t packetsSent() {
144+
return _packetsSent;
145+
}
146+
140147
// ESP8266WiFi API compatibility
141148

142149
wl_status_t status();
@@ -172,6 +179,9 @@ class LwipIntfDev: public LwipIntf, public RawDev {
172179

173180
// Packet handler number
174181
int _phID = -1;
182+
183+
uint32_t _packetsReceived = 0;
184+
uint32_t _packetsSent = 0;
175185
};
176186

177187

@@ -416,7 +426,7 @@ err_t LwipIntfDev<RawDev>::linkoutput_s(netif* netif, struct pbuf* pbuf) {
416426
LwipIntfDev* lid = (LwipIntfDev*)netif->state;
417427
ethernet_arch_lwip_begin();
418428
uint16_t len = lid->sendFrame((const uint8_t*)pbuf->payload, pbuf->len);
419-
429+
lid->_packetsSent++;
420430
#if PHY_HAS_CAPTURE
421431
if (phy_capture) {
422432
phy_capture(lid->_netif.num, (const char*)pbuf->payload, pbuf->len, /*out*/ 1,
@@ -530,6 +540,8 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
530540
return ERR_BUF;
531541
}
532542

543+
_packetsReceived++;
544+
533545
err_t err = _netif.input(pbuf, &_netif);
534546

535547
#if PHY_HAS_CAPTURE

0 commit comments

Comments
 (0)