Skip to content

Commit ac8c05f

Browse files
HW semaphore`
1 parent 5fd15b9 commit ac8c05f

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

cores/rp2040/freertos/variantHooks.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void startFreeRTOS(void) {
253253
// LWIP runs on core 0 only
254254
__lwipQueue = xQueueCreate(16, sizeof(LWIPWork));
255255
//__hwMutex = xSemaphoreCreateMutex();
256-
xTaskCreate(lwipThread, "LWIP", 1024, 0, configMAX_PRIORITIES / 2 - 1, &__lwipTask);
256+
xTaskCreate(lwipThread, "LWIP", 1024, 0, configMAX_PRIORITIES - 1, &__lwipTask);
257257
vTaskCoreAffinitySet(__lwipTask, 1 << 0);
258258

259259
// Initialise and run the freeRTOS scheduler. Execution should never return here.
@@ -586,6 +586,7 @@ static void lwipThread(void *params) {
586586

587587
while (true) {
588588
if (xQueueReceive(__lwipQueue, &w, portMAX_DELAY)) {
589+
printf("+LWIPTHREAD %p\n", w.wakeup);
589590
switch (w.op) {
590591
case __lwip_init:
591592
{
@@ -938,6 +939,7 @@ static void lwipThread(void *params) {
938939
}
939940
}
940941
// Work done, return value set, just tickle the calling task
942+
printf("-LWIPTHREAD %p\n", w.wakeup);
941943
xTaskNotifyGiveIndexed(w.wakeup, TASK_NOTIFY_LWIP_WAKEUP);
942944
}
943945
}

cores/rp2040/lwip_wrap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ extern "C" {
820820
if (!__isLWIPThread()) {
821821
err_t ret;
822822
__ethernet_input_req req = { p, netif, &ret };
823+
printf("__ethernet_input_req\n");
823824
__lwip(__ethernet_input, &req);
824825
return ret;
825826
}

libraries/lwIP_Ethernet/src/LwipEthernet.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,26 +228,36 @@ static void update_next_timeout(async_context_t *context, async_when_pending_wor
228228
async_context_add_at_time_worker_in_ms(context, &ethernet_timeout_worker, _pollingPeriod);
229229
}
230230

231-
TaskHandle_t sctTask;
232231

233-
void sct(void *param) {
232+
// We have a background pump which calls sys_check_timeouts on a periodic basis
233+
// and polls all Ethernet interfaces
234+
static TaskHandle_t _ethernetTask;;
235+
236+
static void ethernetTask(void *param) {
234237
(void) param;
235238
while (true) {
236-
vTaskDelay(100);
237-
ethernet_arch_lwip_gpio_mask(); // Ensure non-polled devices won't interrupt us
239+
uint32_t sleep_ms = sys_timeouts_sleeptime();
240+
if (sleep_ms > _pollingPeriod) {
241+
sleep_ms = _pollingPeriod;
242+
}
243+
vTaskDelay(sleep_ms / portTICK_PERIOD_MS);
244+
// Scan the installed Ethernet drivers
238245
for (auto handlePacket : _handlePacketList) {
246+
// 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
239247
handlePacket.second();
240248
}
249+
// Do LWIP stuff as needed
241250
sys_check_timeouts();
242-
ethernet_arch_lwip_gpio_unmask();
243251
}
244252
}
253+
245254
void __startEthernetContext() {
246255
if (__ethernetContextInitted) {
247256
return;
248257
}
249-
// xTaskCreate(sct, "SCT", 256, nullptr, 1, &sctTask);
250-
#if 1
258+
xTaskCreate(ethernetTask, "Ethernet", 256, nullptr, 1, &_ethernetTask);
259+
// vTaskCoreAffinitySet(_ethernetTask, 1 << 0);
260+
#if 0
251261
//#if defined(PICO_CYW43_SUPPORTED)
252262
// if (rp2040.isPicoW()) {
253263
// _context = cyw43_arch_async_context();

libraries/lwIP_Ethernet/src/LwipIntfDev.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
#include "LwipEthernet.h"
4747
#include "wl_definitions.h"
4848

49+
#include "FreeRTOS.h"
50+
#include "semphr.h"
51+
4952
#ifndef DEFAULT_MTU
5053
#define DEFAULT_MTU 1500
5154
#endif
@@ -75,6 +78,7 @@ class LwipIntfDev: public LwipIntf, public RawDev {
7578
LwipIntfDev(int8_t cs = SS, SPIClass& spi = SPI, int8_t intr = -1) :
7679
RawDev(cs, spi, intr), _spiUnit(spi), _mtu(DEFAULT_MTU), _intrPin(intr), _started(false), _default(false) {
7780
memset(&_netif, 0, sizeof(_netif));
81+
_hwMutex = xSemaphoreCreateMutex();
7882
}
7983

8084
//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
@@ -181,6 +185,7 @@ class LwipIntfDev: public LwipIntf, public RawDev {
181185
public:
182186
// called on a regular basis or on interrupt
183187
err_t handlePackets();
188+
SemaphoreHandle_t _hwMutex;
184189
protected:
185190
// members
186191
SPIClass& _spiUnit;
@@ -205,6 +210,7 @@ class LwipIntfDev: public LwipIntf, public RawDev {
205210

206211
uint32_t _packetsReceived = 0;
207212
uint32_t _packetsSent = 0;
213+
208214
};
209215

210216

@@ -485,7 +491,9 @@ err_t LwipIntfDev<RawDev>::linkoutput_s(netif* netif, struct pbuf* pbuf) {
485491
LwipIntfDev* lid = (LwipIntfDev*)netif->state;
486492
printf("presend %d\n", pbuf->len);
487493
// ethernet_arch_lwip_begin();
494+
xSemaphoreTake(lid->_hwMutex, portMAX_DELAY);
488495
uint16_t len = lid->sendFrame((const uint8_t*)pbuf->payload, pbuf->len);
496+
xSemaphoreGive(lid->_hwMutex);
489497
lid->_packetsSent++;
490498
#if PHY_HAS_CAPTURE
491499
if (phy_capture) {
@@ -573,11 +581,14 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
573581
{
574582
return ERR_OK;
575583
}
576-
584+
printf("handlePackets1\n");
585+
xSemaphoreTake(_hwMutex, portMAX_DELAY);
577586
uint16_t tot_len = RawDev::readFrameSize();
578587
if (!tot_len) {
588+
xSemaphoreGive(_hwMutex);
579589
return ERR_OK;
580590
}
591+
printf("handlePackets2\n");
581592

582593
// from doc: use PBUF_RAM for TX, PBUF_POOL from RX
583594
// however:
@@ -588,15 +599,18 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
588599
// TODO: tweak the wiznet driver to allow copying partial chunk
589600
// of received data and use PBUF_POOL.
590601
pbuf* pbuf = pbuf_alloc(PBUF_RAW, tot_len, PBUF_RAM);
602+
printf("pballoc ret %p\n", pbuf);
591603
if (!pbuf || pbuf->len < tot_len) {
592604
if (pbuf) {
593605
pbuf_free(pbuf);
594606
}
595607
RawDev::discardFrame(tot_len);
608+
xSemaphoreGive(_hwMutex);
596609
return ERR_BUF;
597610
}
598611

599612
uint16_t len = RawDev::readFrameData((uint8_t*)pbuf->payload, tot_len);
613+
xSemaphoreGive(_hwMutex);
600614
if (len != tot_len) {
601615
// tot_len is given by readFrameSize()
602616
// and is supposed to be honoured by readFrameData()

0 commit comments

Comments
 (0)