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.
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"
tomain.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.
UIPEthernet.h@8:4acb22344932, 2017-06-30 (annotated)
- Committer:
- hudakz
- Date:
- Fri Jun 30 19:51:28 2017 +0000
- Revision:
- 8:4acb22344932
- Parent:
- 4:d774541a34da
'UIPEthernet' renamed to 'uIPEthernet'; 'UIPEthernetClass' renamed to 'UIPEthernet'; added IPAddress::toString() to support printing; added print support for debugging; bugs preventing offline build with GCC ARM toolchain fixed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 4:d774541a34da | 1 | /* |
hudakz | 8:4acb22344932 | 2 | UIPEthernet.h - Arduino implementation of a UIP wrapper class. |
hudakz | 4:d774541a34da | 3 | Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> |
hudakz | 4:d774541a34da | 4 | All rights reserved. |
hudakz | 4:d774541a34da | 5 | |
hudakz | 4:d774541a34da | 6 | Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> |
hudakz | 4:d774541a34da | 7 | |
hudakz | 4:d774541a34da | 8 | This program is free software: you can redistribute it and/or modify |
hudakz | 4:d774541a34da | 9 | it under the terms of the GNU General Public License as published by |
hudakz | 4:d774541a34da | 10 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 4:d774541a34da | 11 | (at your option) any later version. |
hudakz | 4:d774541a34da | 12 | |
hudakz | 4:d774541a34da | 13 | This program is distributed in the hope that it will be useful, |
hudakz | 4:d774541a34da | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 4:d774541a34da | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 4:d774541a34da | 16 | GNU General Public License for more details. |
hudakz | 4:d774541a34da | 17 | |
hudakz | 4:d774541a34da | 18 | You should have received a copy of the GNU General Public License |
hudakz | 4:d774541a34da | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 4:d774541a34da | 20 | */ |
hudakz | 4:d774541a34da | 21 | #ifndef UIPETHERNET_H |
hudakz | 8:4acb22344932 | 22 | #define UIPETHERNET_H |
hudakz | 4:d774541a34da | 23 | |
hudakz | 4:d774541a34da | 24 | //#define UIPETHERNET_DEBUG |
hudakz | 4:d774541a34da | 25 | |
hudakz | 4:d774541a34da | 26 | //#define UIPETHERNET_DEBUG_CHKSUM |
hudakz | 4:d774541a34da | 27 | //#define UIPETHERNET_DEBUG_UDP |
hudakz | 4:d774541a34da | 28 | //#define UIPETHERNET_DEBUG_CLIENT |
hudakz | 8:4acb22344932 | 29 | #include "ethernet_comp.h" |
hudakz | 8:4acb22344932 | 30 | #include "mbed.h" |
hudakz | 8:4acb22344932 | 31 | #include "Dhcp.h" |
hudakz | 8:4acb22344932 | 32 | #include "IPAddress.h" |
hudakz | 8:4acb22344932 | 33 | #include "utility/Enc28J60Network.h" |
hudakz | 8:4acb22344932 | 34 | #include "UIPClient.h" |
hudakz | 8:4acb22344932 | 35 | #include "UIPServer.h" |
hudakz | 8:4acb22344932 | 36 | #include "UIPUdp.h" |
hudakz | 4:d774541a34da | 37 | |
hudakz | 4:d774541a34da | 38 | extern "C" |
hudakz | 4:d774541a34da | 39 | { |
hudakz | 8:4acb22344932 | 40 | #include "utility/uip_timer.h" |
hudakz | 8:4acb22344932 | 41 | #include "utility/uip.h" |
hudakz | 4:d774541a34da | 42 | } |
hudakz | 8:4acb22344932 | 43 | #define UIPETHERNET_FREEPACKET 1 |
hudakz | 8:4acb22344932 | 44 | #define UIPETHERNET_SENDPACKET 2 |
hudakz | 4:d774541a34da | 45 | |
hudakz | 8:4acb22344932 | 46 | #define uip_ip_addr(addr, ip) \ |
hudakz | 8:4acb22344932 | 47 | do { \ |
hudakz | 4:d774541a34da | 48 | ((u16_t *) (addr))[0] = HTONS(((ip[0]) << 8) | (ip[1])); \ |
hudakz | 4:d774541a34da | 49 | ((u16_t *) (addr))[1] = HTONS(((ip[2]) << 8) | (ip[3])); \ |
hudakz | 8:4acb22344932 | 50 | } while (0) |
hudakz | 8:4acb22344932 | 51 | #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 | 4:d774541a34da | 52 | |
hudakz | 8:4acb22344932 | 53 | #define uip_seteth_addr(eaddr) \ |
hudakz | 8:4acb22344932 | 54 | do { \ |
hudakz | 4:d774541a34da | 55 | uip_ethaddr.addr[0] = eaddr[0]; \ |
hudakz | 4:d774541a34da | 56 | uip_ethaddr.addr[1] = eaddr[1]; \ |
hudakz | 4:d774541a34da | 57 | uip_ethaddr.addr[2] = eaddr[2]; \ |
hudakz | 4:d774541a34da | 58 | uip_ethaddr.addr[3] = eaddr[3]; \ |
hudakz | 4:d774541a34da | 59 | uip_ethaddr.addr[4] = eaddr[4]; \ |
hudakz | 4:d774541a34da | 60 | uip_ethaddr.addr[5] = eaddr[5]; \ |
hudakz | 8:4acb22344932 | 61 | } while (0) |
hudakz | 8:4acb22344932 | 62 | #define BUF ((struct uip_tcpip_hdr*) &uip_buf[UIP_LLH_LEN]) |
hudakz | 8:4acb22344932 | 63 | class UIPEthernet |
hudakz | 4:d774541a34da | 64 | { |
hudakz | 4:d774541a34da | 65 | public: |
hudakz | 4:d774541a34da | 66 | Enc28J60Network network; |
hudakz | 4:d774541a34da | 67 | |
hudakz | 8:4acb22344932 | 68 | UIPEthernet(PinName mosi, PinName miso, PinName sck, PinName cs); |
hudakz | 4:d774541a34da | 69 | |
hudakz | 4:d774541a34da | 70 | int begin(const uint8_t* mac); |
hudakz | 4:d774541a34da | 71 | void begin(const uint8_t* mac, IPAddress ip); |
hudakz | 4:d774541a34da | 72 | void begin(const uint8_t* mac, IPAddress ip, IPAddress dns); |
hudakz | 4:d774541a34da | 73 | void begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway); |
hudakz | 4:d774541a34da | 74 | void begin(const uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet); |
hudakz | 4:d774541a34da | 75 | |
hudakz | 4:d774541a34da | 76 | // maintain() must be called at regular intervals to process the incoming serial |
hudakz | 4:d774541a34da | 77 | // data and issue IP events to the sketch. It does not return until all IP |
hudakz | 4:d774541a34da | 78 | // events have been processed. Renews dhcp-lease if required. |
hudakz | 4:d774541a34da | 79 | int maintain(void); |
hudakz | 4:d774541a34da | 80 | |
hudakz | 4:d774541a34da | 81 | IPAddress localIP(void); |
hudakz | 4:d774541a34da | 82 | IPAddress subnetMask(void); |
hudakz | 4:d774541a34da | 83 | IPAddress gatewayIP(void); |
hudakz | 4:d774541a34da | 84 | IPAddress dnsServerIP(void); |
hudakz | 4:d774541a34da | 85 | private: |
hudakz | 4:d774541a34da | 86 | static memhandle in_packet; |
hudakz | 4:d774541a34da | 87 | static memhandle uip_packet; |
hudakz | 4:d774541a34da | 88 | static uint8_t uip_hdrlen; |
hudakz | 4:d774541a34da | 89 | static uint8_t packetstate; |
hudakz | 4:d774541a34da | 90 | |
hudakz | 4:d774541a34da | 91 | static IPAddress _dnsServerAddress; |
hudakz | 4:d774541a34da | 92 | static DhcpClass* _dhcp; |
hudakz | 4:d774541a34da | 93 | |
hudakz | 4:d774541a34da | 94 | static unsigned long periodic_timer; |
hudakz | 4:d774541a34da | 95 | |
hudakz | 4:d774541a34da | 96 | void init(const uint8_t* mac); |
hudakz | 4:d774541a34da | 97 | static void configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet); |
hudakz | 4:d774541a34da | 98 | |
hudakz | 4:d774541a34da | 99 | void tick(void); |
hudakz | 4:d774541a34da | 100 | |
hudakz | 4:d774541a34da | 101 | bool network_send(void); |
hudakz | 4:d774541a34da | 102 | |
hudakz | 4:d774541a34da | 103 | friend class UIPServer; |
hudakz | 4:d774541a34da | 104 | |
hudakz | 4:d774541a34da | 105 | friend class UIPClient; |
hudakz | 4:d774541a34da | 106 | |
hudakz | 4:d774541a34da | 107 | friend class UIPUDP; |
hudakz | 4:d774541a34da | 108 | |
hudakz | 4:d774541a34da | 109 | static uint16_t chksum(uint16_t sum, const uint8_t* data, uint16_t len); |
hudakz | 4:d774541a34da | 110 | static uint16_t ipchksum(void); |
hudakz | 8:4acb22344932 | 111 | #if UIP_UDP |
hudakz | 4:d774541a34da | 112 | uint16_t upper_layer_chksum(uint8_t proto); |
hudakz | 8:4acb22344932 | 113 | #endif |
hudakz | 4:d774541a34da | 114 | friend uint16_t uip_ipchksum(void); |
hudakz | 4:d774541a34da | 115 | friend uint16_t uip_tcpchksum(void); |
hudakz | 4:d774541a34da | 116 | friend uint16_t uip_udpchksum(void); |
hudakz | 4:d774541a34da | 117 | |
hudakz | 4:d774541a34da | 118 | friend void uipclient_appcall(void); |
hudakz | 4:d774541a34da | 119 | friend void uipudp_appcall(void); |
hudakz | 4:d774541a34da | 120 | |
hudakz | 8:4acb22344932 | 121 | #if UIP_CONF_IPV6 |
hudakz | 4:d774541a34da | 122 | uint16_t uip_icmp6chksum(void); |
hudakz | 8:4acb22344932 | 123 | #endif /* UIP_CONF_IPV6 */ |
hudakz | 4:d774541a34da | 124 | }; |
hudakz | 4:d774541a34da | 125 | |
hudakz | 8:4acb22344932 | 126 | extern UIPEthernet uIPEthernet; |
hudakz | 4:d774541a34da | 127 | #endif |