|
| 1 | +/**************************************************************************************************************************** |
| 2 | + Ethernet.cpp |
| 3 | +
|
| 4 | + EthernetWebServer is a library for the Ethernet shields to run WebServer |
| 5 | +
|
| 6 | + Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases |
| 7 | + Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer |
| 8 | + Licensed under MIT license |
| 9 | + Version: 1.0.9 |
| 10 | +
|
| 11 | + Copyright 2018 Paul Stoffregen |
| 12 | + |
| 13 | + Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 14 | + software and associated documentation files (the "Software"), to deal in the Software |
| 15 | + without restriction, including without limitation the rights to use, copy, modify, |
| 16 | + merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 17 | + permit persons to whom the Software is furnished to do so, subject to the following |
| 18 | + conditions: |
| 19 | + |
| 20 | + The above copyright notice and this permission notice shall be included in all |
| 21 | + copies or substantial portions of the Software. |
| 22 | + |
| 23 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 24 | + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 25 | + PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 26 | + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 27 | + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 28 | + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | +
|
| 30 | + Version Modified By Date Comments |
| 31 | + ------- ----------- ---------- ----------- |
| 32 | + 1.0.0 K Hoang 13/02/2020 Initial coding for Arduino Mega, Teensy, etc to support Ethernetx libraries |
| 33 | + 1.0.1 K Hoang 20/02/2020 Add support to lambda functions |
| 34 | + 1.0.2 K Hoang 20/02/2020 Add support to UIPEthernet library for ENC28J60 |
| 35 | + 1.0.3 K Hoang 23/02/2020 Add support to SAM DUE / SAMD21 boards |
| 36 | + 1.0.4 K Hoang 16/04/2020 Add support to SAMD51 boards |
| 37 | + 1.0.5 K Hoang 24/04/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, |
| 38 | + Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B30_ublox, etc. |
| 39 | + More Custom Ethernet libraries supported such as Ethernet2, Ethernet3, EthernetLarge |
| 40 | + 1.0.6 K Hoang 27/04/2020 Add support to ESP32/ESP8266 boards |
| 41 | + 1.0.7 K Hoang 30/04/2020 Add ENC28J60 support to ESP32/ESP8266 boards |
| 42 | + 1.0.8 K Hoang 12/05/2020 Fix W5x00 support for ESP8266 boards. |
| 43 | + 1.0.9 K Hoang 15/05/2020 Add EthernetWrapper.h for easier W5x00 support as well as more Ethernet libs in the future. |
| 44 | + *****************************************************************************************************************************/ |
| 45 | + |
| 46 | +#include <Arduino.h> |
| 47 | +#include "Ethernet.h" |
| 48 | +#include "utility/w5100.h" |
| 49 | +#include "Dhcp.h" |
| 50 | + |
| 51 | +#define ETHERNET_DEBUG 1 |
| 52 | + |
| 53 | + |
| 54 | +IPAddress EthernetClass::_dnsServerAddress; |
| 55 | +DhcpClass* EthernetClass::_dhcp = NULL; |
| 56 | + |
| 57 | +// KH |
| 58 | +void EthernetClass::setRstPin(uint8_t pinRST) |
| 59 | +{ |
| 60 | + _pinRST = pinRST; |
| 61 | + pinMode(_pinRST, OUTPUT); |
| 62 | + digitalWrite(_pinRST, HIGH); |
| 63 | +} |
| 64 | +void EthernetClass::setCsPin(uint8_t pinCS) |
| 65 | +{ |
| 66 | + _pinCS = pinCS; |
| 67 | + W5100.setSS(pinCS); |
| 68 | + |
| 69 | +#if ( ETHERNET_DEBUG > 0 ) |
| 70 | + Serial.print("Input pinCS = "); |
| 71 | + Serial.println(pinCS); |
| 72 | + Serial.print("_pinCS = "); |
| 73 | + Serial.println(_pinCS); |
| 74 | +#endif |
| 75 | +} |
| 76 | + |
| 77 | +void EthernetClass::initMaxSockNum(uint8_t maxSockNum) |
| 78 | +{ |
| 79 | + _maxSockNum = maxSockNum; |
| 80 | +} |
| 81 | + |
| 82 | +uint8_t EthernetClass::softreset() |
| 83 | +{ |
| 84 | + return W5100.softReset(); |
| 85 | +} |
| 86 | + |
| 87 | +void EthernetClass::hardreset() |
| 88 | +{ |
| 89 | + if(_pinRST != 0) |
| 90 | + { |
| 91 | + digitalWrite(_pinRST, LOW); |
| 92 | + delay(1); |
| 93 | + digitalWrite(_pinRST, HIGH); |
| 94 | + delay(150); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) |
| 99 | +{ |
| 100 | + DhcpClass s_dhcp; |
| 101 | + _dhcp = &s_dhcp; |
| 102 | + |
| 103 | +#if ( ETHERNET_DEBUG > 0 ) |
| 104 | + Serial.print("_pinCS = "); |
| 105 | + Serial.print(_pinCS); |
| 106 | +#endif |
| 107 | + |
| 108 | + // Initialise the basic info |
| 109 | + if (W5100.init() == 0) |
| 110 | + return 0; |
| 111 | + |
| 112 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 113 | + W5100.setMACAddress(mac); |
| 114 | + W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); |
| 115 | + SPI.endTransaction(); |
| 116 | + |
| 117 | + // Now try to get our config info from a DHCP server |
| 118 | + int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); |
| 119 | + if (ret == 1) |
| 120 | + { |
| 121 | + // We've successfully found a DHCP server and got our configuration |
| 122 | + // info, so set things accordingly |
| 123 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 124 | + W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); |
| 125 | + W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); |
| 126 | + W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); |
| 127 | + SPI.endTransaction(); |
| 128 | + _dnsServerAddress = _dhcp->getDnsServerIp(); |
| 129 | + socketPortRand(micros()); |
| 130 | + } |
| 131 | + return ret; |
| 132 | +} |
| 133 | + |
| 134 | +void EthernetClass::begin(uint8_t *mac, IPAddress ip) |
| 135 | +{ |
| 136 | + // Assume the DNS server will be the machine on the same network as the local IP |
| 137 | + // but with last octet being '1' |
| 138 | + IPAddress dns = ip; |
| 139 | + dns[3] = 1; |
| 140 | + begin(mac, ip, dns); |
| 141 | +} |
| 142 | + |
| 143 | +void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns) |
| 144 | +{ |
| 145 | + // Assume the gateway will be the machine on the same network as the local IP |
| 146 | + // but with last octet being '1' |
| 147 | + IPAddress gateway = ip; |
| 148 | + gateway[3] = 1; |
| 149 | + begin(mac, ip, dns, gateway); |
| 150 | +} |
| 151 | + |
| 152 | +void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) |
| 153 | +{ |
| 154 | + IPAddress subnet(255, 255, 255, 0); |
| 155 | + begin(mac, ip, dns, gateway, subnet); |
| 156 | +} |
| 157 | + |
| 158 | +void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) |
| 159 | +{ |
| 160 | + // Initialise the basic info |
| 161 | + if (W5100.init() == 0) |
| 162 | + return; |
| 163 | + |
| 164 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 165 | + W5100.setMACAddress(mac); |
| 166 | + |
| 167 | +#if ( defined(ESP8266) || defined(ESP32) ) |
| 168 | + W5100.setIPAddress(ip.raw_address()); |
| 169 | + W5100.setGatewayIp(gateway.raw_address()); |
| 170 | + W5100.setSubnetMask(subnet.raw_address()); |
| 171 | +#elif (ARDUINO > 106 || TEENSYDUINO > 121) |
| 172 | + W5100.setIPAddress(ip._address.bytes); |
| 173 | + W5100.setGatewayIp(gateway._address.bytes); |
| 174 | + W5100.setSubnetMask(subnet._address.bytes); |
| 175 | +#else |
| 176 | + W5100.setIPAddress(ip._address); |
| 177 | + W5100.setGatewayIp(gateway._address); |
| 178 | + W5100.setSubnetMask(subnet._address); |
| 179 | +#endif |
| 180 | + |
| 181 | + SPI.endTransaction(); |
| 182 | + _dnsServerAddress = dns; |
| 183 | +} |
| 184 | + |
| 185 | +void EthernetClass::init(uint8_t sspin) |
| 186 | +{ |
| 187 | + W5100.setSS(sspin); |
| 188 | +} |
| 189 | + |
| 190 | +EthernetLinkStatus EthernetClass::linkStatus() |
| 191 | +{ |
| 192 | + switch (W5100.getLinkStatus()) { |
| 193 | + case UNKNOWN: return Unknown; |
| 194 | + case LINK_ON: return LinkON; |
| 195 | + case LINK_OFF: return LinkOFF; |
| 196 | + default: return Unknown; |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +EthernetHardwareStatus EthernetClass::hardwareStatus() |
| 201 | +{ |
| 202 | + switch (W5100.getChip()) { |
| 203 | + case 51: return EthernetW5100; |
| 204 | + case 52: return EthernetW5200; |
| 205 | + case 55: return EthernetW5500; |
| 206 | + default: return EthernetNoHardware; |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +int EthernetClass::maintain() |
| 211 | +{ |
| 212 | + int rc = DHCP_CHECK_NONE; |
| 213 | + if (_dhcp != NULL) { |
| 214 | + // we have a pointer to dhcp, use it |
| 215 | + rc = _dhcp->checkLease(); |
| 216 | + switch (rc) |
| 217 | + { |
| 218 | + case DHCP_CHECK_NONE: |
| 219 | + //nothing done |
| 220 | + break; |
| 221 | + case DHCP_CHECK_RENEW_OK: |
| 222 | + case DHCP_CHECK_REBIND_OK: |
| 223 | + //we might have got a new IP. |
| 224 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 225 | + W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); |
| 226 | + W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); |
| 227 | + W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); |
| 228 | + SPI.endTransaction(); |
| 229 | + _dnsServerAddress = _dhcp->getDnsServerIp(); |
| 230 | + break; |
| 231 | + default: |
| 232 | + //this is actually an error, it will retry though |
| 233 | + break; |
| 234 | + } |
| 235 | + } |
| 236 | + return rc; |
| 237 | +} |
| 238 | + |
| 239 | + |
| 240 | +void EthernetClass::MACAddress(uint8_t *mac_address) |
| 241 | +{ |
| 242 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 243 | + W5100.getMACAddress(mac_address); |
| 244 | + SPI.endTransaction(); |
| 245 | +} |
| 246 | + |
| 247 | +IPAddress EthernetClass::localIP() |
| 248 | +{ |
| 249 | + IPAddress ret; |
| 250 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 251 | + W5100.getIPAddress(ret.raw_address()); |
| 252 | + SPI.endTransaction(); |
| 253 | + return ret; |
| 254 | +} |
| 255 | + |
| 256 | +IPAddress EthernetClass::subnetMask() |
| 257 | +{ |
| 258 | + IPAddress ret; |
| 259 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 260 | + W5100.getSubnetMask(ret.raw_address()); |
| 261 | + SPI.endTransaction(); |
| 262 | + return ret; |
| 263 | +} |
| 264 | + |
| 265 | +IPAddress EthernetClass::gatewayIP() |
| 266 | +{ |
| 267 | + IPAddress ret; |
| 268 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 269 | + W5100.getGatewayIp(ret.raw_address()); |
| 270 | + SPI.endTransaction(); |
| 271 | + return ret; |
| 272 | +} |
| 273 | + |
| 274 | +void EthernetClass::setMACAddress(const uint8_t *mac_address) |
| 275 | +{ |
| 276 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 277 | + W5100.setMACAddress(mac_address); |
| 278 | + SPI.endTransaction(); |
| 279 | +} |
| 280 | + |
| 281 | +void EthernetClass::setLocalIP(const IPAddress local_ip) |
| 282 | +{ |
| 283 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 284 | + IPAddress ip = local_ip; |
| 285 | + W5100.setIPAddress(ip.raw_address()); |
| 286 | + SPI.endTransaction(); |
| 287 | +} |
| 288 | + |
| 289 | +void EthernetClass::setSubnetMask(const IPAddress subnet) |
| 290 | +{ |
| 291 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 292 | + IPAddress ip = subnet; |
| 293 | + W5100.setSubnetMask(ip.raw_address()); |
| 294 | + SPI.endTransaction(); |
| 295 | +} |
| 296 | + |
| 297 | +void EthernetClass::setGatewayIP(const IPAddress gateway) |
| 298 | +{ |
| 299 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 300 | + IPAddress ip = gateway; |
| 301 | + W5100.setGatewayIp(ip.raw_address()); |
| 302 | + SPI.endTransaction(); |
| 303 | +} |
| 304 | + |
| 305 | +void EthernetClass::setRetransmissionTimeout(uint16_t milliseconds) |
| 306 | +{ |
| 307 | + if (milliseconds > 6553) milliseconds = 6553; |
| 308 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 309 | + W5100.setRetransmissionTime(milliseconds * 10); |
| 310 | + SPI.endTransaction(); |
| 311 | +} |
| 312 | + |
| 313 | +void EthernetClass::setRetransmissionCount(uint8_t num) |
| 314 | +{ |
| 315 | + SPI.beginTransaction(SPI_ETHERNET_SETTINGS); |
| 316 | + W5100.setRetransmissionCount(num); |
| 317 | + SPI.endTransaction(); |
| 318 | +} |
| 319 | + |
| 320 | +EthernetClass Ethernet; |
0 commit comments