Skip to content

Commit b94aa52

Browse files
Wrap ethernet_output
1 parent f412d28 commit b94aa52

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

cores/rp2040/freertos/variantHooks.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,13 @@ static void lwipThread(void *params) {
923923
__real_netif_remove(r->netif);
924924
break;
925925
}
926+
case __ethernet_input:
927+
{
928+
__ethernet_input_req *r = (__ethernet_input_req *)w.req;
929+
printf("__real_ethernet_input\n");
930+
*(r->ret) = __real_ethernet_input(r->p, r->netif);
931+
break;
932+
}
926933
default:
927934
{
928935
// Any new unimplemented calls = ERROR!!!

cores/rp2040/lwip_wrap.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,4 +814,19 @@ extern "C" {
814814
__real_netif_remove(netif);
815815
}
816816

817+
extern err_t __real_ethernet_input(struct pbuf *p, struct netif *netif);
818+
err_t __wrap_ethernet_input(struct pbuf *p, struct netif *netif) {
819+
#ifdef __FREERTOS
820+
if (!__isLWIPThread()) {
821+
err_t ret;
822+
__ethernet_input_req req = { p, netif, &ret };
823+
__lwip(__ethernet_input, &req);
824+
return ret;
825+
}
826+
#endif
827+
LWIPMutex m;
828+
return __real_ethernet_input(p, netif);
829+
}
830+
831+
817832
}; // extern "C"

lib/core_wrap.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
-Wl,--wrap=netif_add
7070
-Wl,--wrap=netif_remove
7171

72+
-Wl,--wrap=ethernet_input
73+
7274
-Wl,--wrap=cyw43_cb_process_ethernet
7375
-Wl,--wrap=cyw43_cb_tcpip_set_link_up
7476
-Wl,--wrap=cyw43_cb_tcpip_set_link_down

libraries/lwIP_Ethernet/src/LwipEthernet.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,33 @@
2525
#if defined(PICO_CYW43_SUPPORTED)
2626
#include <pico/cyw43_arch.h>
2727
#endif
28+
2829
#if defined(__FREERTOS)
2930
#include <pico/async_context_freertos.h>
30-
static async_context_freertos_t lwip_ethernet_async_context_threadsafe_background;
31+
#include "FreeRTOS.h"
32+
#include "task.h"
33+
static async_context_freertos_t lwip_ethernet_async_context;
3134
static StackType_t lwip_ethernet_async_context_freertos_task_stack[CYW43_TASK_STACK_SIZE];
3235

3336
static async_context_t *lwip_ethernet_init_default_async_context(void) {
3437
async_context_freertos_config_t config = async_context_freertos_default_config();
3538
#if configSUPPORT_STATIC_ALLOCATION && !CYW43_NO_DEFAULT_TASK_STACK
3639
config.task_stack = lwip_ethernet_async_context_freertos_task_stack;
3740
#endif
38-
if (async_context_freertos_init(&lwip_ethernet_async_context_threadsafe_background, &config)) {
39-
return &lwip_ethernet_async_context_threadsafe_background.core;
41+
if (async_context_freertos_init(&lwip_ethernet_async_context, &config)) {
42+
return &lwip_ethernet_async_context.core;
4043
}
4144
return NULL;
4245
}
4346

4447
#else
4548
#include <pico/async_context_threadsafe_background.h>
46-
static async_context_threadsafe_background_t lwip_ethernet_async_context_threadsafe_background;
49+
static async_context_threadsafe_background_t lwip_ethernet_async_context;
4750

4851
static async_context_t *lwip_ethernet_init_default_async_context(void) {
4952
async_context_threadsafe_background_config_t config = async_context_threadsafe_background_default_config();
50-
if (async_context_threadsafe_background_init(&lwip_ethernet_async_context_threadsafe_background, &config)) {
51-
return &lwip_ethernet_async_context_threadsafe_background.core;
53+
if (async_context_threadsafe_background_init(&lwip_ethernet_async_context, &config)) {
54+
return &lwip_ethernet_async_context.core;
5255
}
5356
return NULL;
5457
}
@@ -63,7 +66,7 @@ bool __ethernetContextInitted = false;
6366
// Async context that pumps the ethernet controllers
6467
static async_when_pending_worker_t always_pending_update_timeout_worker;
6568
static async_at_time_worker_t ethernet_timeout_worker;
66-
//static async_context_t *_context = nullptr;
69+
static async_context_t *_context = nullptr;
6770

6871
// Theoretically support multiple interfaces
6972
static std::map<int, std::function<void(void)>> _handlePacketList;
@@ -75,7 +78,7 @@ void ethernet_arch_lwip_begin() {
7578
// return;
7679
// }
7780
//#endif
78-
__startEthernetContext();
81+
// __startEthernetContext();
7982
// async_context_acquire_lock_blocking(_context);
8083
}
8184

@@ -224,11 +227,26 @@ static void update_next_timeout(async_context_t *context, async_when_pending_wor
224227
async_context_add_at_time_worker_in_ms(context, &ethernet_timeout_worker, _pollingPeriod);
225228
}
226229

230+
TaskHandle_t sctTask;
231+
232+
void sct(void *param) {
233+
(void) param;
234+
while (true) {
235+
vTaskDelay(100);
236+
ethernet_arch_lwip_gpio_mask(); // Ensure non-polled devices won't interrupt us
237+
for (auto handlePacket : _handlePacketList) {
238+
handlePacket.second();
239+
}
240+
sys_check_timeouts();
241+
ethernet_arch_lwip_gpio_unmask();
242+
}
243+
}
227244
void __startEthernetContext() {
228245
if (__ethernetContextInitted) {
229246
return;
230247
}
231-
#if 0
248+
// xTaskCreate(sct, "SCT", 256, nullptr, 1, &sctTask);
249+
#if 1
232250
//#if defined(PICO_CYW43_SUPPORTED)
233251
// if (rp2040.isPicoW()) {
234252
// _context = cyw43_arch_async_context();

libraries/lwIP_Ethernet/src/LwipIntfDev.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
606606
}
607607

608608
_packetsReceived++;
609-
609+
printf("recv pkt %d: ", tot_len);
610+
for (int i=0; i < tot_len; i++) printf("%02x ", ((uint8_t*)pbuf->payload)[i]);
611+
printf("\n");
610612
err_t err = _netif.input(pbuf, &_netif);
611613

612614
#if PHY_HAS_CAPTURE

0 commit comments

Comments
 (0)