Skip to content

Commit c54ec2b

Browse files
Restore bare-metal compilation
Simple CYW43 WiFiClient test runs
1 parent c447d4f commit c54ec2b

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

cores/rp2040/freertos/heap_3a.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Hacked to remove the vTaskSuspendAll from heap_3.c
1+
#ifdef __FREERTOS
2+
// Hacked to remove the vTaskSuspendAll from heap_3.c. We handle that in malloc wrappers
23

34
/*
45
FreeRTOS Kernel <DEVELOPMENT BRANCH>
@@ -101,3 +102,4 @@ void vPortHeapResetState(void) {
101102
/* No state needs to be re-initialised in heap_3. */
102103
}
103104
/*-----------------------------------------------------------*/
105+
#endif

libraries/lwIP_Ethernet/src/LwipEthernet.cpp

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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>
@@ -30,19 +31,19 @@
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;
7273
static std::map<int, std::function<void(void)>> _handlePacketList;
7374

7475
void 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

8589
void 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

9599
int __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
207211
static void ethernet_timeout_reached(__unused async_context_t *context, __unused async_at_time_worker_t *worker) {
208212
assert(worker == &ethernet_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
234232
static TaskHandle_t _ethernetTask;;
235233

236234
static void stage2(void *cbData) {
@@ -246,7 +244,6 @@ static void stage2(void *cbData) {
246244
sys_check_timeouts();
247245
}
248246

249-
#include <lwip_wrap.h>
250247
static 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

271260
void __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;

libraries/lwIP_Ethernet/src/LwipIntfDev.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ class LwipIntfDev: public LwipIntf, public RawDev {
8181
LwipIntfDev(int8_t cs = SS, SPIClass& spi = SPI, int8_t intr = -1) :
8282
RawDev(cs, spi, intr), _spiUnit(spi), _mtu(DEFAULT_MTU), _intrPin(intr), _started(false), _default(false) {
8383
memset(&_netif, 0, sizeof(_netif));
84+
#ifdef __FREERTOS
8485
_hwMutex = xSemaphoreCreateMutex();
86+
#endif
8587
}
8688

8789
//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
@@ -188,7 +190,9 @@ class LwipIntfDev: public LwipIntf, public RawDev {
188190
public:
189191
// called on a regular basis or on interrupt
190192
err_t handlePackets();
193+
#ifdef __FREERTOS
191194
SemaphoreHandle_t _hwMutex;
195+
#endif
192196
uint8_t _irqBuffer[LWIP_CALLBACK_BUFFER_SIZE];
193197
protected:
194198
// members
@@ -505,18 +509,28 @@ EthernetLinkStatus LwipIntfDev<RawDev>::linkStatus() {
505509
template<class RawDev>
506510
err_t LwipIntfDev<RawDev>::linkoutput_s(netif* netif, struct pbuf* pbuf) {
507511
LwipIntfDev* lid = (LwipIntfDev*)netif->state;
508-
// ethernet_arch_lwip_begin();
512+
513+
#ifdef __FREERTOS
509514
xSemaphoreTake(lid->_hwMutex, portMAX_DELAY);
515+
#else
516+
ethernet_arch_lwip_begin();
517+
#endif
518+
510519
uint16_t len = lid->sendFrame((const uint8_t*)pbuf->payload, pbuf->len);
520+
521+
#ifdef __FREERTOS
511522
xSemaphoreGive(lid->_hwMutex);
523+
#else
524+
ethernet_arch_lwip_end();
525+
#endif
526+
512527
lid->_packetsSent++;
513528
#if PHY_HAS_CAPTURE
514529
if (phy_capture) {
515530
phy_capture(lid->_netif.num, (const char*)pbuf->payload, pbuf->len, /*out*/ 1,
516531
/*success*/ len == pbuf->len);
517532
}
518533
#endif
519-
// ethernet_arch_lwip_end();
520534
return len == pbuf->len ? ERR_OK : ERR_MEM;
521535
}
522536

@@ -596,10 +610,14 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
596610
return ERR_OK;
597611
}
598612

613+
#ifdef __FREERTOS
599614
xSemaphoreTake(_hwMutex, portMAX_DELAY);
615+
#endif
600616
uint16_t tot_len = RawDev::readFrameSize();
601617
if (!tot_len) {
618+
#ifdef __FREERTOS
602619
xSemaphoreGive(_hwMutex);
620+
#endif
603621
return ERR_OK;
604622
}
605623

@@ -617,12 +635,16 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
617635
pbuf_free(pbuf);
618636
}
619637
RawDev::discardFrame(tot_len);
638+
#ifdef __FREERTOS
620639
xSemaphoreGive(_hwMutex);
640+
#endif
621641
return ERR_BUF;
622642
}
623643

624644
uint16_t len = RawDev::readFrameData((uint8_t*)pbuf->payload, tot_len);
645+
#ifdef __FREERTOS
625646
xSemaphoreGive(_hwMutex);
647+
#endif
626648
if (len != tot_len) {
627649
// tot_len is given by readFrameSize()
628650
// and is supposed to be honoured by readFrameData()

0 commit comments

Comments
 (0)