|
| 1 | +EthernetLWIP (Wired Ethernet) Support |
| 2 | +===================================== |
| 3 | + |
| 4 | +Wired Ethernet interfaces are supported for all the internal networking |
| 5 | +libraries (``WiFiClient``, ``WiFiClientSecure``, ``WiFiServer``, |
| 6 | +``WiFiServerSecure``, ``WiFiUDP``, ``WebServer``, ``Updater``, |
| 7 | +``HTTPClient``, etc.). |
| 8 | + |
| 9 | +Using these wired interfaces is very similar to using the Pico-W WiFi |
| 10 | +so most examples in the core only require minor modifications to use |
| 11 | +a wired interface. |
| 12 | + |
| 13 | +Supported Wired Ethernet Modules |
| 14 | +-------------------------------- |
| 15 | + |
| 16 | +* Wiznet W5100 |
| 17 | + |
| 18 | +* Wiznet W5500 |
| 19 | + |
| 20 | +* ENC28J60 |
| 21 | + |
| 22 | + |
| 23 | +Enabling Wired Ethernet |
| 24 | +----------------------- |
| 25 | + |
| 26 | +Simply replace the WiFi include at the top with: |
| 27 | + |
| 28 | +.. code:: cpp |
| 29 | +
|
| 30 | + #include <W5500lwIP.h> // Or W5100lwIP.h or ENC28J60.h |
| 31 | + |
| 32 | +
|
| 33 | +And add a global Ethernet object of the same type: |
| 34 | + |
| 35 | +.. code:: cpp |
| 36 | +
|
| 37 | + Wiznet5500lwIP eth(1); // Parameter is the Chip Select pin |
| 38 | +
|
| 39 | +In your ``setup()`` you may adjust the SPI pins you're using to |
| 40 | +match your hardware (be sure they are legal for the RP2040!), or |
| 41 | +skip this if you're using the default ones: |
| 42 | + |
| 43 | +.. code:: cpp |
| 44 | +
|
| 45 | + void setup() { |
| 46 | + SPI.setRX(0); |
| 47 | + SPI.setCS(1); |
| 48 | + SPI.setSCK(2); |
| 49 | + SPI.setTX(3); |
| 50 | + .... |
| 51 | + } |
| 52 | +
|
| 53 | +And finally replace the ``WiFi.begin()`` and ``WiFi.connected()`` |
| 54 | +calls with ``eth.begin()`` and ``eth.connected()``: |
| 55 | + |
| 56 | +.. code:: cpp |
| 57 | +
|
| 58 | + void setup() { |
| 59 | + .... |
| 60 | + // WiFi.begin(SSID, PASS) |
| 61 | + eth.begin(); |
| 62 | + |
| 63 | + //while (!WiFi.connected()) { |
| 64 | + while (!eth.connected()) { |
| 65 | + Serial.print("."); |
| 66 | + } |
| 67 | +
|
| 68 | + Serial.print("IP address: "); |
| 69 | + //Serial.println(WiFi.localIP()); |
| 70 | + Serial.println(eth.localIP()); |
| 71 | +
|
| 72 | + .... |
| 73 | + } |
| 74 | +
|
| 75 | +Adjusting LWIP Polling |
| 76 | +---------------------- |
| 77 | + |
| 78 | +LWIP operates in a polling mode for the wired Ethernet devices. By default it will run |
| 79 | +every 20ms, meaning that on average it will take half that time (10ms) before a packet |
| 80 | +received in the Ethernet module is received and operated upon by the Pico. This gives |
| 81 | +very low CPU utilization but in some cases this latency can affect performance. |
| 82 | + |
| 83 | +Adding a call to ``lwipPollingPeriod(XXX)`` (where ``XXXX`` is the polling period in |
| 84 | +milliseconds) can adjust this setting on the fly. Note that if you set it too low, the |
| 85 | +Pico may not have enough time to service the Ethernet port before the timer fires again, |
| 86 | +leading to a lock up and hang. |
| 87 | + |
| 88 | + |
| 89 | +Adjusting SPI Speed |
| 90 | +------------------- |
| 91 | + |
| 92 | +By default a 4MHz clock will be used to clock data into and out of the Ethernet module. |
| 93 | +Depending on the module and your wiring, a higher SPI clock may increase performance (but |
| 94 | +too high of a clock will cause communications problems or hangs). |
| 95 | + |
| 96 | +This value may be adjusted using the ``eth.setSPISpeed(hz)`` call **before** starting the |
| 97 | +device. (You may also use custom ``SPISettings`` instead via ``eth.setSPISettings(spis)```) |
| 98 | + |
| 99 | +For example, to set the W5500 to use a 30MHZ clock: |
| 100 | + |
| 101 | +.. code:: cpp |
| 102 | +
|
| 103 | + #include <W5500lwIP.h> |
| 104 | + Wiznet5500lwIP eth(1); |
| 105 | +
|
| 106 | + void setup() { |
| 107 | + eth.setSPISpeed(30000000); |
| 108 | + lwipPollingPeriod(3); |
| 109 | + ... |
| 110 | + eth.begin(); |
| 111 | + ... |
| 112 | + } |
| 113 | +
|
| 114 | +
|
| 115 | +Example Code |
| 116 | +------------ |
| 117 | + |
| 118 | +The following examples allow switching between WiFi and Ethernet: |
| 119 | + |
| 120 | +* ``WebServer/AdvancedWebServer`` |
| 121 | + |
| 122 | +* ``HTTPClient/BasicHTTPSClient`` |
| 123 | + |
| 124 | +Caveats |
| 125 | +------- |
| 126 | + |
| 127 | +The same restrictions for ``WiFi`` apply to these Ethernet classes, namely: |
| 128 | + |
| 129 | +* Only core 0 may run any networking related code. |
| 130 | + |
| 131 | +* In FreeRTOS, only the ``setup`` and ``loop`` task can call networking libraries, not any tasks. |
| 132 | + |
| 133 | +Special Thanks |
| 134 | +-------------- |
| 135 | + |
| 136 | +* LWIPEthernet classes come from the ESP8266 Arduino team |
| 137 | + |
| 138 | +* Individual Ethernet drivers were written by Nicholas Humfrey |
| 139 | + |
0 commit comments