Skip to content

Commit ef0ce09

Browse files
Use driver local storage for IRQ request area
Trying not to end up on thedailywtf.com
1 parent 946f973 commit ef0ce09

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

cores/rp2040/lwip_wrap.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -828,28 +828,17 @@ extern "C" {
828828
return __real_ethernet_input(p, netif);
829829
}
830830

831-
void lwip_callback(void (*cb)(void *), void *cbData, bool fromISR) {
832-
#ifdef __FREERTOS
833-
if (fromISR) {
834-
// In ISR we can't check what the current thread is
835-
static __callback_req req = { cb, cbData }; // TODO HACK HERE
836-
// We pass in the address of an unknown sized request to the LWIP work queue
837-
// For normal mode that address is on the app stack, which will be frozen until
838-
// the callback is performed. So no problem, that stack address will remain valid.
839-
// Here, we're in an ISR and won't block, just put a pointer on the queue and
840-
// return from the interrupt. The stack address, there, is no longer safe and you
841-
// will see memory corruption when the actual app thread uses its stack.
842-
// We can't allocate memory in an IRQ in FreeRTOS, so for now we'll just have
843-
// a single heap-based (static) request which will live forever. As long as
844-
// only a single lwip_callback from IRQ is present we're OK. Should you have 2
845-
// IRQ driver network cards, this will fail.
846-
// A more satisfying method might have the NIC driver pass in its own class local
847-
// preallocated storage space which we can use.
848-
__lwip(__callback, &req, fromISR);
831+
void lwip_callback(void (*cb)(void *), void *cbData, void *buffer) {
832+
#ifdef __FREERTOS
833+
if (buffer) {
834+
__callback_req *req = (__callback_req *)buffer;
835+
req->cb = cb;
836+
req->cbData = cbData;
837+
__lwip(__callback, req, true);
849838
return;
850839
} else if (!__isLWIPThread()) {
851840
__callback_req req = { cb, cbData };
852-
__lwip(__callback, &req, fromISR);
841+
__lwip(__callback, &req, false);
853842
return;
854843
}
855844
#endif

libraries/lwIP_Ethernet/src/LwipEthernet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static void stage2(void *cbData) {
246246
sys_check_timeouts();
247247
}
248248

249-
extern "C" void lwip_callback(void (*cb)(void *), void *cbData, bool fromISR);
249+
#include <lwip_wrap.h>
250250
static void ethernetTask(void *param) {
251251
(void) param;
252252
while (true) {
@@ -255,7 +255,7 @@ static void ethernetTask(void *param) {
255255
sleep_ms = _pollingPeriod;
256256
}
257257
vTaskDelay(sleep_ms / portTICK_PERIOD_MS);
258-
lwip_callback(stage2, nullptr, false);
258+
lwip_callback(stage2, nullptr);
259259
#if 0
260260
// Scan the installed Ethernet drivers
261261
for (auto handlePacket : _handlePacketList) {

libraries/lwIP_Ethernet/src/LwipIntfDev.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ enum EthernetLinkStatus {
7272
LinkOFF
7373
};
7474

75-
extern "C" void lwip_callback(void (*cb)(void *), void *cbData, bool fromISR);
75+
#include <lwip_wrap.h>
7676

7777

7878
template<class RawDev>
@@ -189,6 +189,7 @@ class LwipIntfDev: public LwipIntf, public RawDev {
189189
// called on a regular basis or on interrupt
190190
err_t handlePackets();
191191
SemaphoreHandle_t _hwMutex;
192+
uint8_t _irqBuffer[LWIP_CALLBACK_BUFFER_SIZE];
192193
protected:
193194
// members
194195
SPIClass& _spiUnit;
@@ -483,7 +484,7 @@ template<class RawDev>
483484
void LwipIntfDev<RawDev>::_irq(void *param) {
484485
LwipIntfDev *d = static_cast<LwipIntfDev*>(param);
485486
ethernet_arch_lwip_gpio_mask(); // Disable other IRQs until we're done processing this one
486-
lwip_callback(_lwipCallback, param, true);
487+
lwip_callback(_lwipCallback, param, (void *)d->_irqBuffer);
487488
//ethernet_arch_lwip_begin();
488489
// d->handlePackets();
489490
// sys_check_timeouts();

0 commit comments

Comments
 (0)