Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.

Dependents:   mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more

Library for ENC28J60 Ethernet modules.

/media/uploads/hudakz/enc28j60_module01.jpg

Ported to mbed from Norbert Truchsess's UIPEthernet library for Arduino. Thank you Norbert!

  • Full support for persistent (streaming) TCP/IP and UDP connections Client and Server each, ARP, ICMP, DHCP and DNS.
  • Works with both Mbed OS 2 and Mbed OS 5.

Usage:

  • Import the library into your project.
  • Add #include "UipEthernet.h" to main.cpp
  • Create one instance of the UipEthernet class initialized with the MAC address you'd like to use and SPI pins of the connected Mbed board.

Example programs:

Import programWebSwitch_ENC28J60

HTTP Server serving a simple webpage which enables to remotely turn a digital output on/off. Compile, download, run and type 'IP_address/secret/' (don't forget the last '/') into your web browser and hit ENTER.

Import programHTTPServer_Echo_ENC28J60

A simple HTTP server echoing received requests. Ethernet connection is over an ENC28J60 board. Usage: Type the server's IP address into you web browser and hit <ENTER>.

Import programTcpServer_ENC28J60

Simple TCP/IP Server using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programTcpClient_ENC28J60

Simple TCP/IP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programUdpServer_ENC28J60

Simple UDP Server using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programUdpClient_ENC28J60

Simple UDP Client using the UIPEthernet library for ENC28J60 Ethernet boards.

Import programMQTT_Hello_ENC28J60

MQTT Client example program. Ethernet connection is via an ENC28J60 module.

Committer:
hudakz
Date:
Tue Aug 27 15:01:10 2019 +0000
Revision:
9:a156d3de5647
Child:
11:647d53d146f1
Mbed OS 5 support added and API modified.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 9:a156d3de5647 1 /*
hudakz 9:a156d3de5647 2 UipEthernet.h - Arduino implementation of a UIP wrapper class.
hudakz 9:a156d3de5647 3 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
hudakz 9:a156d3de5647 4 All rights reserved.
hudakz 9:a156d3de5647 5
hudakz 9:a156d3de5647 6 Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com>
hudakz 9:a156d3de5647 7
hudakz 9:a156d3de5647 8 This program is free software: you can redistribute it and/or modify
hudakz 9:a156d3de5647 9 it under the terms of the GNU General Public License as published by
hudakz 9:a156d3de5647 10 the Free Software Foundation, either version 3 of the License, or
hudakz 9:a156d3de5647 11 (at your option) any later version.
hudakz 9:a156d3de5647 12
hudakz 9:a156d3de5647 13 This program is distributed in the hope that it will be useful,
hudakz 9:a156d3de5647 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 9:a156d3de5647 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 9:a156d3de5647 16 GNU General Public License for more details.
hudakz 9:a156d3de5647 17
hudakz 9:a156d3de5647 18 You should have received a copy of the GNU General Public License
hudakz 9:a156d3de5647 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 9:a156d3de5647 20 */
hudakz 9:a156d3de5647 21 #ifndef UIPETHERNET_H
hudakz 9:a156d3de5647 22 #define UIPETHERNET_H
hudakz 9:a156d3de5647 23
hudakz 9:a156d3de5647 24 //#define UIPETHERNET_DEBUG
hudakz 9:a156d3de5647 25 //#define UIPETHERNET_DEBUG_CHKSUM
hudakz 9:a156d3de5647 26 //#define UIPETHERNET_DEBUG_UDP
hudakz 9:a156d3de5647 27 //#define UIPETHERNET_DEBUG_CLIENT
hudakz 9:a156d3de5647 28 #include "mbed.h"
hudakz 9:a156d3de5647 29 #include "DhcpClient.h"
hudakz 9:a156d3de5647 30 #include "IpAddress.h"
hudakz 9:a156d3de5647 31 #include "utility/Enc28j60Py.h"
hudakz 9:a156d3de5647 32 #include "TcpClient.h"
hudakz 9:a156d3de5647 33 #include "TcpServer.h"
hudakz 9:a156d3de5647 34 #include "UdpSocket.h"
hudakz 9:a156d3de5647 35
hudakz 9:a156d3de5647 36 extern "C"
hudakz 9:a156d3de5647 37 {
hudakz 9:a156d3de5647 38 #include "utility/uip_timer.h"
hudakz 9:a156d3de5647 39 #include "utility/uip.h"
hudakz 9:a156d3de5647 40 }
hudakz 9:a156d3de5647 41 #define UIPETHERNET_FREEPACKET 1
hudakz 9:a156d3de5647 42 #define UIPETHERNET_SENDPACKET 2
hudakz 9:a156d3de5647 43
hudakz 9:a156d3de5647 44 #define uip_ip_addr(addr, ip) \
hudakz 9:a156d3de5647 45 do { \
hudakz 9:a156d3de5647 46 ((u16_t *) (addr))[0] = (((ip[1]) << 8) | (ip[0])); \
hudakz 9:a156d3de5647 47 ((u16_t *) (addr))[1] = (((ip[3]) << 8) | (ip[2])); \
hudakz 9:a156d3de5647 48 } while (0)
hudakz 9:a156d3de5647 49 #define ip_addr_uip(a) IpAddress(a[0] & 0xFF, a[0] >> 8, a[1] & 0xFF, a[1] >> 8) //TODO this is not IPV6 capable
hudakz 9:a156d3de5647 50
hudakz 9:a156d3de5647 51 #define uip_seteth_addr(eaddr) \
hudakz 9:a156d3de5647 52 do { \
hudakz 9:a156d3de5647 53 uip_ethaddr.addr[0] = eaddr[0]; \
hudakz 9:a156d3de5647 54 uip_ethaddr.addr[1] = eaddr[1]; \
hudakz 9:a156d3de5647 55 uip_ethaddr.addr[2] = eaddr[2]; \
hudakz 9:a156d3de5647 56 uip_ethaddr.addr[3] = eaddr[3]; \
hudakz 9:a156d3de5647 57 uip_ethaddr.addr[4] = eaddr[4]; \
hudakz 9:a156d3de5647 58 uip_ethaddr.addr[5] = eaddr[5]; \
hudakz 9:a156d3de5647 59 } while (0)
hudakz 9:a156d3de5647 60 #define BUF ((struct uip_tcpip_hdr*) &uip_buf[UIP_LLH_LEN])
hudakz 9:a156d3de5647 61
hudakz 9:a156d3de5647 62 class UipEthernet
hudakz 9:a156d3de5647 63 {
hudakz 9:a156d3de5647 64 public:
hudakz 9:a156d3de5647 65 static UipEthernet* ethernet;
hudakz 9:a156d3de5647 66 static IpAddress dnsServerAddress;
hudakz 9:a156d3de5647 67 Enc28j60Phy phy;
hudakz 9:a156d3de5647 68
hudakz 9:a156d3de5647 69 UipEthernet (const uint8_t mac[6], PinName mosi, PinName miso, PinName sck, PinName cs);
hudakz 9:a156d3de5647 70
hudakz 9:a156d3de5647 71 int connect();
hudakz 9:a156d3de5647 72 void set_network(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4);
hudakz 9:a156d3de5647 73 void set_network(IpAddress ip);
hudakz 9:a156d3de5647 74 void set_network(IpAddress ip, IpAddress dns);
hudakz 9:a156d3de5647 75 void set_network(IpAddress ip, IpAddress dns, IpAddress gateway);
hudakz 9:a156d3de5647 76 void set_network(IpAddress ip, IpAddress dns, IpAddress gateway, IpAddress subnet);
hudakz 9:a156d3de5647 77 void tick();
hudakz 9:a156d3de5647 78 IpAddress localIP();
hudakz 9:a156d3de5647 79 IpAddress subnetMask();
hudakz 9:a156d3de5647 80 IpAddress gatewayIP();
hudakz 9:a156d3de5647 81 IpAddress dnsServerIP();
hudakz 9:a156d3de5647 82 const char* get_ip_address();
hudakz 9:a156d3de5647 83 const char* get_netmask();
hudakz 9:a156d3de5647 84 const char* get_gateway();
hudakz 9:a156d3de5647 85 static uint16_t chksum(uint16_t sum, const uint8_t* data, uint16_t len);
hudakz 9:a156d3de5647 86 static uint16_t ipchksum();
hudakz 9:a156d3de5647 87 private:
hudakz 9:a156d3de5647 88 uint8_t *const _mac;
hudakz 9:a156d3de5647 89 IpAddress _ip;
hudakz 9:a156d3de5647 90 IpAddress _dns;
hudakz 9:a156d3de5647 91 IpAddress _gateway;
hudakz 9:a156d3de5647 92 IpAddress _subnet;
hudakz 9:a156d3de5647 93 static memhandle inPacket;
hudakz 9:a156d3de5647 94 static memhandle uipPacket;
hudakz 9:a156d3de5647 95 static uint8_t uipHeaderLen;
hudakz 9:a156d3de5647 96 static uint8_t packetState;
hudakz 9:a156d3de5647 97 DhcpClient dhcpClient;
hudakz 9:a156d3de5647 98 Timer periodicTimer;
hudakz 9:a156d3de5647 99 void init(const uint8_t* mac);
hudakz 9:a156d3de5647 100 bool network_send();
hudakz 9:a156d3de5647 101 friend class TcpServer;
hudakz 9:a156d3de5647 102 friend class TcpClient;
hudakz 9:a156d3de5647 103 friend class UdpSocket;
hudakz 9:a156d3de5647 104 #if UIP_UDP
hudakz 9:a156d3de5647 105 uint16_t upper_layer_chksum(uint8_t proto);
hudakz 9:a156d3de5647 106 #endif
hudakz 9:a156d3de5647 107 friend uint16_t uip_ipchksum();
hudakz 9:a156d3de5647 108 friend uint16_t uip_tcpchksum();
hudakz 9:a156d3de5647 109 friend uint16_t uip_udpchksum();
hudakz 9:a156d3de5647 110 friend void uipclient_appcall();
hudakz 9:a156d3de5647 111 friend void uipudp_appcall();
hudakz 9:a156d3de5647 112
hudakz 9:a156d3de5647 113 #if UIP_CONF_IPV6
hudakz 9:a156d3de5647 114 uint16_t uip_icmp6chksum();
hudakz 9:a156d3de5647 115 #endif
hudakz 9:a156d3de5647 116 };
hudakz 9:a156d3de5647 117 #endif