1919*/
2020
2121#include < LwipEthernet.h>
22+ #include < lwip_wrap.h>
2223#include < lwip/timeouts.h>
2324#include < lwip/dns.h>
2425#include < pico/mutex.h>
3031#include < pico/async_context_freertos.h>
3132#include " FreeRTOS.h"
3233#include " task.h"
33- static async_context_freertos_t lwip_ethernet_async_context;
34- static StackType_t lwip_ethernet_async_context_freertos_task_stack[CYW43_TASK_STACK_SIZE];
35-
36- static async_context_t *lwip_ethernet_init_default_async_context (void ) {
37- async_context_freertos_config_t config = async_context_freertos_default_config ();
38- #if configSUPPORT_STATIC_ALLOCATION && !CYW43_NO_DEFAULT_TASK_STACK
39- config.task_stack = lwip_ethernet_async_context_freertos_task_stack;
40- #endif
41- if (async_context_freertos_init (&lwip_ethernet_async_context, &config)) {
42- return &lwip_ethernet_async_context.core ;
43- }
44- return NULL ;
45- }
34+ // static async_context_freertos_t lwip_ethernet_async_context;
35+ // static StackType_t lwip_ethernet_async_context_freertos_task_stack[CYW43_TASK_STACK_SIZE];
36+
37+ // static async_context_t *lwip_ethernet_init_default_async_context(void) {
38+ // async_context_freertos_config_t config = async_context_freertos_default_config();
39+ // #if configSUPPORT_STATIC_ALLOCATION && !CYW43_NO_DEFAULT_TASK_STACK
40+ // config.task_stack = lwip_ethernet_async_context_freertos_task_stack;
41+ // #endif
42+ // if (async_context_freertos_init(&lwip_ethernet_async_context, &config)) {
43+ // return &lwip_ethernet_async_context.core;
44+ // }
45+ // return NULL;
46+ // }
4647
4748#else
4849#include < pico/async_context_threadsafe_background.h>
@@ -72,24 +73,27 @@ static async_context_t *_context = nullptr;
7273static std::map<int , std::function<void (void )>> _handlePacketList;
7374
7475void ethernet_arch_lwip_begin () {
75- // #if defined(PICO_CYW43_SUPPORTED)
76- // if (rp2040.isPicoW()) {
77- // cyw43_arch_lwip_begin();
78- // return;
79- // }
80- // #endif
81- // __startEthernetContext();
82- // async_context_acquire_lock_blocking(_context);
76+ #ifdef __FREERTOS
77+ panic (" oops" );
78+ #endif
79+ #if defined(PICO_CYW43_SUPPORTED)
80+ if (rp2040.isPicoW ()) {
81+ cyw43_arch_lwip_begin ();
82+ return ;
83+ }
84+ #endif
85+ __startEthernetContext ();
86+ async_context_acquire_lock_blocking (_context);
8387}
8488
8589void ethernet_arch_lwip_end () {
86- // #if defined(PICO_CYW43_SUPPORTED)
87- // if (rp2040.isPicoW()) {
88- // cyw43_arch_lwip_end();
89- // return;
90- // }
91- // #endif
92- // async_context_release_lock(_context);
90+ #if defined(PICO_CYW43_SUPPORTED)
91+ if (rp2040.isPicoW ()) {
92+ cyw43_arch_lwip_end ();
93+ return ;
94+ }
95+ #endif
96+ async_context_release_lock (_context);
9397}
9498
9599int __addEthernetPacketHandler (std::function<void (void )> _packetHandler) {
@@ -206,19 +210,12 @@ static uint32_t _pollingPeriod = 20;
206210// This will only be called under the protection of the async context mutex, so no re-entrancy checks needed
207211static void ethernet_timeout_reached (__unused async_context_t *context, __unused async_at_time_worker_t *worker) {
208212 assert (worker == ðernet_timeout_worker);
209- printf (" __ethernet_timeout_reached_calls %d\n " , __ethernet_timeout_reached_calls);
210213 __ethernet_timeout_reached_calls++;
211214 ethernet_arch_lwip_gpio_mask (); // Ensure non-polled devices won't interrupt us
212215 for (auto handlePacket : _handlePacketList) {
213216 handlePacket.second ();
214217 sys_check_timeouts ();
215218 }
216- // #if defined(PICO_CYW43_SUPPORTED)
217- // if (!rp2040.isPicoW()) {
218- // sys_check_timeouts();
219- // }
220- // #else
221- // #endif
222219 ethernet_arch_lwip_gpio_unmask ();
223220}
224221
@@ -231,6 +228,7 @@ static void update_next_timeout(async_context_t *context, async_when_pending_wor
231228
232229// We have a background pump which calls sys_check_timeouts on a periodic basis
233230// and polls all Ethernet interfaces
231+ #ifdef __FREERTOS
234232static TaskHandle_t _ethernetTask;;
235233
236234static void stage2 (void *cbData) {
@@ -246,7 +244,6 @@ static void stage2(void *cbData) {
246244 sys_check_timeouts ();
247245}
248246
249- #include < lwip_wrap.h>
250247static void ethernetTask (void *param) {
251248 (void ) param;
252249 while (true ) {
@@ -256,34 +253,26 @@ static void ethernetTask(void *param) {
256253 }
257254 vTaskDelay (sleep_ms / portTICK_PERIOD_MS);
258255 lwip_callback (stage2, nullptr );
259- #if 0
260- // Scan the installed Ethernet drivers
261- for (auto handlePacket : _handlePacketList) {
262- // Note that each NIC needs to use its own mutex to ensure LWIP isn't doing something with it at the time we want to poll
263- handlePacket.second();
264- }
265- // Do LWIP stuff as needed
266- sys_check_timeouts();
267- #endif
268256 }
269257}
258+ #endif
270259
271260void __startEthernetContext () {
272261 if (__ethernetContextInitted) {
273262 return ;
274263 }
275- xTaskCreate (ethernetTask, " Ethernet " , 256 , nullptr , 1 , &_ethernetTask);
276- // vTaskCoreAffinitySet(_ethernetTask , 1 << 0 );
277- #if 0
278- // #if defined(PICO_CYW43_SUPPORTED)
279- // if (rp2040.isPicoW()) {
280- // _context = cyw43_arch_async_context();
281- // } else {
282- // _context = lwip_ethernet_init_default_async_context();
283- // }
284- // #else
264+ # ifdef __FREERTOS
265+ xTaskCreate (ethernetTask, " EthPoll " , 256 , nullptr , 1 , &_ethernetTask );
266+ #else
267+ #if defined(PICO_CYW43_SUPPORTED)
268+ if (rp2040.isPicoW ()) {
269+ _context = cyw43_arch_async_context ();
270+ } else {
271+ _context = lwip_ethernet_init_default_async_context ();
272+ }
273+ #else
285274 _context = lwip_ethernet_init_default_async_context ();
286- // #endif
275+ #endif
287276 ethernet_timeout_worker.do_work = ethernet_timeout_reached;
288277 always_pending_update_timeout_worker.work_pending = true ;
289278 always_pending_update_timeout_worker.do_work = update_next_timeout;
0 commit comments