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.

Revision:
11:647d53d146f1
Parent:
9:a156d3de5647
Child:
13:95c00132cd98
--- a/UipEthernet.cpp	Tue Aug 27 22:08:54 2019 +0000
+++ b/UipEthernet.cpp	Fri Aug 30 08:11:40 2019 +0000
@@ -20,7 +20,7 @@
   */
 #include "mbed.h"
 #include "UipEthernet.h"
-#include "utility/Enc28j60Py.h"
+#include "utility/Enc28j60Eth.h"
 #include "UdpSocket.h"
 
 extern "C"
@@ -38,7 +38,6 @@
 uint8_t UipEthernet::       uipHeaderLen(0);
 uint8_t UipEthernet::       packetState(0);
 IpAddress UipEthernet::     dnsServerAddress;
-//DhcpClient UIPEthernet::    dhcpClient;
 
 /**
  * @brief
@@ -48,9 +47,9 @@
  */
 void enc28j60_mempool_block_move_callback(memaddress dest, memaddress src, memaddress len)
 {
-    //as ENC28J60 DMA is unable to copy single bytes:
+    //as ENC28J60 DMA is unable to copy single byte:
     if (len == 1) {
-        UipEthernet::ethernet->phy.writeByte(dest, UipEthernet::ethernet->phy.readByte(src));
+        UipEthernet::ethernet->enc28j60Eth.writeByte(dest, UipEthernet::ethernet->enc28j60Eth.readByte(src));
     }
     else {
         // calculate address of last byte
@@ -70,25 +69,25 @@
               prevent a never ending DMA operation which
               would overwrite the entire 8-Kbyte buffer.
        */
-        UipEthernet::ethernet->phy.writeRegPair(EDMASTL, src);
-        UipEthernet::ethernet->phy.writeRegPair(EDMADSTL, dest);
+        UipEthernet::ethernet->enc28j60Eth.writeRegPair(EDMASTL, src);
+        UipEthernet::ethernet->enc28j60Eth.writeRegPair(EDMADSTL, dest);
 
-        if ((src <= RXSTOP_INIT) && (len > RXSTOP_INIT))
-            len -= (RXSTOP_INIT - RXSTART_INIT);
-        UipEthernet::ethernet->phy.writeRegPair(EDMANDL, len);
+        if ((src <= RXEND_INIT) && (len > RXEND_INIT))
+            len -= ((RXEND_INIT + 1) - RXSTART_INIT);
+        UipEthernet::ethernet->enc28j60Eth.writeRegPair(EDMANDL, len);
 
         /* 2. If an interrupt at the end of the copy process is
               desired, set EIE.DMAIE and EIE.INTIE and
               clear EIR.DMAIF.
 
            3. Verify that ECON1.CSUMEN is clear. */
-        UipEthernet::ethernet->phy.writeOp(ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_CSUMEN);
+        UipEthernet::ethernet->enc28j60Eth.writeOp(ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_CSUMEN);
 
         /* 4. Start the DMA copy by setting ECON1.DMAST. */
-        UipEthernet::ethernet->phy.writeOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_DMAST);
+        UipEthernet::ethernet->enc28j60Eth.writeOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_DMAST);
 
         //    wait until runnig DMA is completed
-        while (UipEthernet::ethernet->phy.readOp(ENC28J60_READ_CTRL_REG, ECON1) & ECON1_DMAST);
+        while (UipEthernet::ethernet->enc28j60Eth.readOp(ENC28J60_READ_CTRL_REG, ECON1) & ECON1_DMAST);
     }
 }
 
@@ -97,7 +96,7 @@
  * variables, so we can only have one TCP/IP stack per program.
  */
 UipEthernet::UipEthernet(const uint8_t mac[6], PinName mosi, PinName miso, PinName sck, PinName cs) :
-    phy(mosi, miso, sck, cs),
+    enc28j60Eth(mosi, miso, sck, cs),
     _mac(new uint8_t[6]),
     _ip(),
     _dns(),
@@ -108,8 +107,6 @@
         _mac[i] = mac[i];
 }
 
-#if UIP_UDP
-
 /**
  * @brief
  * @note
@@ -147,7 +144,22 @@
         return 1;
     }
 }
-#endif
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+void UipEthernet::set_network(const char *ip_address, const char *netmask, const char *gateway)
+{
+    _ip = IpAddress(ip_address, strlen(ip_address));
+    _dns = _ip;
+    _dns[3] = 1;
+    _gateway = IpAddress(gateway, strlen(gateway));
+    _subnet = IpAddress(netmask, strlen(netmask));
+    set_network(_ip, _dns, _gateway, _subnet);
+}
 
 /**
  * @brief
@@ -202,6 +214,7 @@
     _dns = dns;
     _gateway = gateway;
     _subnet = IpAddress(255, 255, 255, 0);
+    set_network(_ip, _dns, _gateway, _subnet);
 }
 
 /**
@@ -324,7 +337,7 @@
 void UipEthernet::tick()
 {
     if (inPacket == NOBLOCK) {
-        inPacket = phy.receivePacket();
+        inPacket = enc28j60Eth.receivePacket();
 #ifdef UIPETHERNET_DEBUG
         if (in_packet != NOBLOCK) {
             printf("--------------\r\nreceivePacket: %d\r\n", in_packet);
@@ -334,9 +347,9 @@
 
     if (inPacket != NOBLOCK) {
         packetState = UIPETHERNET_FREEPACKET;
-        uip_len = phy.blockSize(inPacket);
+        uip_len = enc28j60Eth.blockSize(inPacket);
         if (uip_len > 0) {
-            phy.readPacket(inPacket, 0, (uint8_t*)uip_buf, UIP_BUFSIZE);
+            enc28j60Eth.readPacket(inPacket, 0, (uint8_t*)uip_buf, UIP_BUFSIZE);
             if (ETH_HDR->type == HTONS(UIP_ETHTYPE_IP)) {
                 uipPacket = inPacket;   //required for upper_layer_checksum of in_packet!
 #ifdef UIPETHERNET_DEBUG
@@ -367,7 +380,7 @@
 #ifdef UIPETHERNET_DEBUG
             printf("freeing packet: %d\r\n", in_packet);
 #endif
-            phy.freePacket();
+            enc28j60Eth.freePacket();
             inPacket = NOBLOCK;
         }
     }
@@ -431,25 +444,25 @@
 #ifdef UIPETHERNET_DEBUG
         printf("Enc28J60Network_send uip_packet: %d, hdrlen: %d\r\n", uip_packet, uip_hdrlen);
 #endif
-        phy.writePacket(uipPacket, 0, uip_buf, uipHeaderLen);
+        enc28j60Eth.writePacket(uipPacket, 0, uip_buf, uipHeaderLen);
         packetState &= ~UIPETHERNET_SENDPACKET;
         goto sendandfree;
     }
 
-    uipPacket = Enc28j60Phy::allocBlock(uip_len);
+    uipPacket = Enc28j60Eth::allocBlock(uip_len);
     if (uipPacket != NOBLOCK)
     {
 #ifdef UIPETHERNET_DEBUG
         printf("Enc28J60Network_send uip_buf (uip_len): %d, packet: %d\r\n", uip_len, uip_packet);
 #endif
-        phy.writePacket(uipPacket, 0, uip_buf, uip_len);
+        enc28j60Eth.writePacket(uipPacket, 0, uip_buf, uip_len);
         goto sendandfree;
     }
 
     return false;
 sendandfree:
-    phy.sendPacket(uipPacket);
-    Enc28j60Phy::freeBlock(uipPacket);
+    enc28j60Eth.sendPacket(uipPacket);
+    Enc28j60Eth::freeBlock(uipPacket);
     uipPacket = NOBLOCK;
     return true;
 }
@@ -464,7 +477,7 @@
 {
     periodicTimer.start();
 
-    phy.init((uint8_t*)mac);
+    enc28j60Eth.init((uint8_t*)mac);
     uip_seteth_addr(mac);
 
     uip_init();
@@ -599,7 +612,7 @@
         );
 #endif
     if (upper_layer_memlen < upper_layer_len) {
-        sum = phy.chksum
+        sum = enc28j60Eth.chksum
             (
                 sum, UipEthernet::uipPacket, UIP_IPH_LEN +
                 UIP_LLH_LEN +
@@ -658,4 +671,5 @@
 
     return sum;
 }
+
 #endif