-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support wired network interfaces (W5500, W5100, ENC28J60) (#1703)
Enable use of wired Ethernet modules as first-class LWIP citizens. All networking classes like MDNS, WebServer, HTTPClient, WiFiClient, and OTA can use a wired Ethernet adapter just like built-in WiFi. Two examples updated to show proper use. Uses the Async Context support built into the Pico SDK. When running on the Pico it will use the CYW43 async instance. Uses modified wired Ethernet drivers, thanks Nicholas Humfrey! Note, the classic, non-LWIP integrated `Ethernet` and related libraries should still work fine (but not be able to use WebServer/HTTPS/etc.) Fixes #775
- Loading branch information
1 parent
3950b94
commit 1f3d501
Showing
43 changed files
with
3,739 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
EthernetLWIP (Wired Ethernet) Support | ||
===================================== | ||
|
||
Wired Ethernet interfaces are supported for all the internal networking | ||
libraries (``WiFiClient``, ``WiFiClientSecure``, ``WiFiServer``, | ||
``WiFiServerSecure``, ``WiFiUDP``, ``WebServer``, ``Updater``, | ||
``HTTPClient``, etc.). | ||
|
||
Using these wired interfaces is very similar to using the Pico-W WiFi | ||
so most examples in the core only require minor modifications to use | ||
a wired interface. | ||
|
||
Supported Wired Ethernet Modules | ||
-------------------------------- | ||
|
||
* Wiznet W5100 | ||
|
||
* Wiznet W5500 | ||
|
||
* ENC28J60 | ||
|
||
|
||
Enabling Wired Ethernet | ||
----------------------- | ||
|
||
Simply replace the WiFi include at the top with: | ||
|
||
.. code:: cpp | ||
#include <W5500lwIP.h> // Or W5100lwIP.h or ENC28J60.h | ||
And add a global Ethernet object of the same type: | ||
|
||
.. code:: cpp | ||
Wiznet5500lwIP eth(1); // Parameter is the Chip Select pin | ||
In your ``setup()`` you may adjust the SPI pins you're using to | ||
match your hardware (be sure they are legal for the RP2040!), or | ||
skip this if you're using the default ones: | ||
|
||
.. code:: cpp | ||
void setup() { | ||
SPI.setRX(0); | ||
SPI.setCS(1); | ||
SPI.setSCK(2); | ||
SPI.setTX(3); | ||
.... | ||
} | ||
And finally replace the ``WiFi.begin()`` and ``WiFi.connected()`` | ||
calls with ``eth.begin()`` and ``eth.connected()``: | ||
|
||
.. code:: cpp | ||
void setup() { | ||
.... | ||
// WiFi.begin(SSID, PASS) | ||
eth.begin(); | ||
//while (!WiFi.connected()) { | ||
while (!eth.connected()) { | ||
Serial.print("."); | ||
} | ||
Serial.print("IP address: "); | ||
//Serial.println(WiFi.localIP()); | ||
Serial.println(eth.localIP()); | ||
.... | ||
} | ||
Adjusting LWIP Polling | ||
---------------------- | ||
|
||
LWIP operates in a polling mode for the wired Ethernet devices. By default it will run | ||
every 20ms, meaning that on average it will take half that time (10ms) before a packet | ||
received in the Ethernet module is received and operated upon by the Pico. This gives | ||
very low CPU utilization but in some cases this latency can affect performance. | ||
|
||
Adding a call to ``lwipPollingPeriod(XXX)`` (where ``XXXX`` is the polling period in | ||
milliseconds) can adjust this setting on the fly. Note that if you set it too low, the | ||
Pico may not have enough time to service the Ethernet port before the timer fires again, | ||
leading to a lock up and hang. | ||
|
||
|
||
Adjusting SPI Speed | ||
------------------- | ||
|
||
By default a 4MHz clock will be used to clock data into and out of the Ethernet module. | ||
Depending on the module and your wiring, a higher SPI clock may increase performance (but | ||
too high of a clock will cause communications problems or hangs). | ||
|
||
This value may be adjusted using the ``eth.setSPISpeed(hz)`` call **before** starting the | ||
device. (You may also use custom ``SPISettings`` instead via ``eth.setSPISettings(spis)```) | ||
|
||
For example, to set the W5500 to use a 30MHZ clock: | ||
|
||
.. code:: cpp | ||
#include <W5500lwIP.h> | ||
Wiznet5500lwIP eth(1); | ||
void setup() { | ||
eth.setSPISpeed(30000000); | ||
lwipPollingPeriod(3); | ||
... | ||
eth.begin(); | ||
... | ||
} | ||
Example Code | ||
------------ | ||
|
||
The following examples allow switching between WiFi and Ethernet: | ||
|
||
* ``WebServer/AdvancedWebServer`` | ||
|
||
* ``HTTPClient/BasicHTTPSClient`` | ||
|
||
Caveats | ||
------- | ||
|
||
The same restrictions for ``WiFi`` apply to these Ethernet classes, namely: | ||
|
||
* Only core 0 may run any networking related code. | ||
|
||
* In FreeRTOS, only the ``setup`` and ``loop`` task can call networking libraries, not any tasks. | ||
|
||
Special Thanks | ||
-------------- | ||
|
||
* LWIPEthernet classes come from the ESP8266 Arduino team | ||
|
||
* Individual Ethernet drivers were written by Nicholas Humfrey | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.