Skip to content

Commit

Permalink
Allow non-CYW43 network interfaces (W5500, etc.)
Browse files Browse the repository at this point in the history
WIP - Builds and can get a DHCP address using W5500.

Lots of infrastructure work still required:
[ ] Separate CYW43 stuff and LWIP stuff (mutex, etc.)
[ ] Add async worker for pumping frames
[ ] Real LWIP-only mutex
[ ] Should WiFiClient be available as TCPClient?
etc.

Fixes #775
  • Loading branch information
earlephilhower committed Sep 9, 2023
1 parent 5639ede commit e1274dc
Show file tree
Hide file tree
Showing 9 changed files with 1,236 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cores/rp2040/lwip_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
class LWIPMutex {
public:
LWIPMutex() {
cyw43_arch_lwip_begin();
// cyw43_arch_lwip_begin();
}

~LWIPMutex() {
cyw43_arch_lwip_end();
// cyw43_arch_lwip_end();
}
};

Expand Down
6 changes: 4 additions & 2 deletions libraries/lwIP_Ethernet/src/LwipEthernet.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

#include <LwipEthernet.h>
//#include <SPI.h>
#include <SPI.h>
#include <lwip/init.h>

//#ifndef ETHERNET_SPI_CLOCK_DIV
//#define ETHERNET_SPI_CLOCK_DIV SPI_CLOCK_DIV4 // 4MHz (SPI.h)
//#endif

void SPI4EthInit() {
//SPI.begin();
SPI.begin();
lwip_init();
// SPI.setClockDivider(ETHERNET_SPI_CLOCK_DIV);
// SPI.setBitOrder(MSBFIRST);
// SPI.setDataMode(SPI_MODE0);
Expand Down
4 changes: 2 additions & 2 deletions libraries/lwIP_Ethernet/src/LwipIntfDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ class LwipIntfDev: public LwipIntf, public RawDev {
static err_t netif_init_s(netif* netif);
static err_t linkoutput_s(netif* netif, struct pbuf* p);
static void netif_status_callback_s(netif* netif);

public:
// called on a regular basis or on interrupt
err_t handlePackets();

protected:
// members

netif _netif;
Expand Down
98 changes: 98 additions & 0 deletions libraries/lwIP_w5500/examples/TelnetConnect/TelnetConnect.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <LwipEthernet.h>
#include <W5500lwIP.h>
#include <WiFi.h>

Wiznet5500lwIP eth(/*SS*/ 1); // <== adapt to your hardware

const char* host = "djxmmx.net";
const uint16_t port = 17;

void setup() {
SPI.setSCK(2);
SPI.setTX(3);
SPI.setRX(0);
SPI.setCS(1);

delay(5000);
Serial.begin(115200);

Serial.println("\nEthernet\n");

// 1. Currently when no default is set, esp8266-Arduino uses the first
// DHCP client interface receiving a valid address and gateway to
// become the new lwIP default interface.
// 2. Otherwise - when using static addresses - lwIP for every packets by
// defaults selects automatically the best suited output interface
// matching the destination address. If several interfaces match,
// the first one is picked. On esp8266/Arduno: WiFi interfaces are
// checked first.
// 3. Or, use `::setDefault()` to force routing through this interface.
// eth.setDefault(); // default route set through this interface

if (!ethInitDHCP(eth)) {
Serial.printf("no hardware found\n");
while (1) {
delay(1000);
}
}

while (!eth.connected()) {
eth.handlePackets();
sys_check_timeouts();
Serial.printf(".");
delay(1000);
}

Serial.printf("Ethernet: IP Address: %s\n",
eth.localIP().toString().c_str());
}

void loop() {

Serial.print("connecting to ");
Serial.print(host);
Serial.print(':');
Serial.println(port);

Serial.printf("Link sense: %d (detectable: %d)\n", eth.isLinked(), eth.isLinkDetectable());

// Use WiFiClient class to create TCP connections
// (this class could have been named TCPClient)
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
return;
}

// This will send a string to the server
Serial.println("sending data to server");
if (client.connected()) {
client.println("hello from ESP8266");
}

// wait for data to be available
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
delay(60000);
return;
}
}

// Read all the lines of the reply from server and print them to Serial
Serial.println("receiving from remote server");
while (client.available()) {
char ch = static_cast<char>(client.read());
Serial.print(ch);
}

// Close the connection
Serial.println();
Serial.println("closing connection");
client.stop();

delay(300000); // execute once every 5 minutes, don't flood remote service
}
10 changes: 10 additions & 0 deletions libraries/lwIP_w5500/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=lwIP_w5500
version=1
author=Nicholas Humfrey
maintainer=esp8266/Arduino
sentence=Ethernet driver
paragraph=Wiznet5500 ethernet drivers for lwIP and esp8266 Arduino from https://github.com/njh/W5500MacRaw
category=Communication
url=https://github.com/esp8266/Arduino
architectures=rp2040
dot_a_linkage=true
10 changes: 10 additions & 0 deletions libraries/lwIP_w5500/src/W5500lwIP.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#ifndef _W5500LWIP_H
#define _W5500LWIP_H

#include <LwipIntfDev.h>
#include <utility/w5500.h>

using Wiznet5500lwIP = LwipIntfDev<Wiznet5500>;

#endif // _W5500LWIP_H
Loading

0 comments on commit e1274dc

Please sign in to comment.