Connecting embedded microcontrollers to the internet is a crucial task in modern applications. The embedded controllers, particularly in consumer devices and wearables, now essentially require ping online. This is more important as the devices are getting smarter. Connecting to the internet is not just crucial but a hefty task as well. The typical TCP/IP stack is too resource-intensive for bare metals to implement. Microcontrollers can never implement TCP/IP stack due to low memory, RAM, and computational power, so they require something lightweight to emulate the same functionality.
Light-Weight Internet Protocol (LwIP) is a small independent implementation of a TCP/IP protocol suite for embedded systems. Originally developed by Adam Dunkels at the Swedish Institute of Computer Science (SICS), LwIP is a free, open-source TCP/IP stack now maintained at Savannah. Several reputed embedded systems manufacturers have adopted LwIP, like Texas Instruments, Altera Corporation, Xilinx, STMicroelectronics, Freescale, and Analog Devices, for network drivers of their embedded devices. The protocol is even used by Arduino Ethernet Shield and WiFi development boards like ESP32 and ESP8266 for implementing network functions. Any hardware manufacturer can use the protocol with the operating system or network driver on its platform as an open protocol under the modified BSD license. The protocol can also be used in applications provided the underlying operating system supports the LwIP port. The source for LwIP is available as a Git repository.
LwIP lets implement a full-fledged TCP/IP stack on embedded systems while consuming minimal system resources and memory. The stack is even modified to fit Arduino libraries. The stack only requires 40kb on ROM/flash memory and consumes just tens of kilobytes of RAM during runtime. Even a tailor-made API is available for the stack that does not require copying data.
What is LwIP capable of?
LwIP enables a full-fledged TCP/IP stack in a footprint smaller than 40 kb. It can be used to implement the following network protocols in an embedded application.
Internet Protocol (IP): LwIP enables manipulating Ipv4 and IPv6 addresses and packet forwarding over multiple network interfaces (netifs). It can assign a static IP address to the network interface, obtain an IP address from the DHCP server or pick an IP address from the local subnet. It can even assign an IP address to a network interface.
Internet Control Message Protocol (ICMP): LwIP allows network maintenance and debugging via ICMP protocol. The supported protocols include Echo Reply/ping, Destination Unreachable, and Time Exceeded.
Neighbor Discovery (ND): LwIP lets neighbor discovery and stateless address autoconfiguration for IPv6 in compliance with RFC 4861 (for Neighbor discovery) and RFC 4862 (for Address autoconfiguration) standards.
Multicast listener discovery for IPv6 (MLD): LwIP provides multicast listener discovery for IPv6 in compliance with RFC 2710 standard. Though, it does not support MLDv2.
User Datagram Protocol (UDP): LwIP includes experimental UDP-lite extensions compliant with RFC 768 (UDP) as well as RFC 3828 (Lightweight UDP) standards.
Transmission Control Protocol (TCP): LwIP enables TCP protocol compliant with RFC 793 (Transmission Control Protocol), RFC 1122 (Requirements for Internet Hosts), RFC 2001 (TCP Slow Start, Congestion Avoidance, Fast Retransmit, and Fast Recovery Algorithms), RFC 2581 (TCP Congestion Control), RFC 3390 (Increasing TCP’s Initial Window) standards.
Internet Group Management Protocol (IGMP): LwIP enables multicast traffic management compliant with IGMP version 0, IGMP version 1, IGMP version 2, and IGMP version 3.
Address Resolution Protocol (ARP): LwIP lets translate hardware address into a network layer address for Ethernet.
Point-to-Point Protocol (PPP): LwIP enables point-to-point protocol over Ethernet (PPPoE) as well as over Serial (PPPoS).
LwIP can configure DHCP client, DNS client, and SNMP client on a platform. Plus, it lets you configure a controller as an HTTP/HTTPS server, SMTP/SMTPS client, MQTT client, SNTP client, NetBIOS name server, TFTP server, or mDNS responder. Initially, LwIP was designed to run in single-thread environments; multi-threading support was added to it eventually. The functionalities from its several modules, including api.h, sockets.h, sys.h, netbuf.h, pppapi.h, netifapi.h and netdb.h can be safely run in separate threads. That means, you can run many protocols including IPv4/IPv6, PPP etc simultaneously on the embedded controller. The network interfaces (IoT and networking) supported by LwIP include WiFi (IEEE 802.1D bridge), LPWAN ((6LoWPAN), Bluetooth (6LoWPAN over BLE), PPP interfaces, SLIP interfaces, and Zigbee (ZEP).
Applications of LwIP
LwIP stack helps implement embedded networking. A common application of LwIP is implementing embedded WiFi networks over bare metals (microcontrollers). Popular WiFi development boards like ESP8266 and ESP32 utilize LwIP stack for wireless network functionalities. The microcontroller platforms like Arduino use the stack for communicating over Ethernet.
Many developers find it easier to implement networking functions over embedded Linux or RTOS. Though, even micro-RTOs can be overkill for embedded WiFi applications. With a bit of network programming over the LwIP stack, full-fledged TCP/IP applications can be developed on microcontroller platforms. These applications could run application layer protocols like HTTP, TCP, and UDP over a small client like a microcontroller.
For instance, check out how ESP8266/ESP32 can be configured as a TCP client/Server capable of loading or hosting an HTML webpage.
For several IoT use cases, running application layer protocols over a microcontroller can be a win-win situation compared to relying on protocols like MQTT or CoAP that are merely capable of latent messaging over the internet. LwIP stack lets you configure a microcontroller as an HTTP client, HTTP server, MQTT client, MDNS, SMTP client, SNTP, NetBIOS responder, Iperf server, SNMP agent, and TFTP server. If an embedded networking application does not require scaling or deal with network security issues or time constraints, the LwIP stack can be the best fit. Still, a developer must take care of expectations from the intended embedded application and choose wisely if the LwIP stack is sufficient or networking stack underlying Linux/BSD fits the bill. Most of the embedded networking developers do find LwIP stack comfortable to go along with as they are already familiar with TCP/IP suite and 802.11 PHY.
LwIP has a lot to offer in the embedded networking space that top-notch embedded manufacturers have adopted for their Ethernet and WiFi drivers. LwIP is a networking stack in operating systems like ReactOS and Genode. In some Linux/Unix-based operating systems, the stack is utilized for implementing network servers.
For popular embedded platforms like Arduino and ESP8266/ESP32, LwIP is available through BSD sockets API. So, electronic hobbyists can integrate LwIP features in an embedded application by accessing the API module, like the socket module. Many real-time operating systems (RTOS) rely on the LwIP stack for implementing network functions.
Embedded designers can find already available LwIP-based device drivers for several platforms. Otherwise, they are free to utilize the LwIP stack within an operating system or network driver for implementing an embedded network over a specific platform. If a platform runs an operating system containing a LwIP stack, developers can directly utilize the stack in application code.
Implementation of LwIP stack
The LwIP stack is available via three types of application programming interfaces – raw APIs, sequential-style APIs, and BSD-style socket APIs. The raw APIs are non-thread-safe low-level APIs that can be used to integrate LwIP at the operating system or Kernel level. These APIs let you make callbacks optimized for maximum performance with minimum footprint. The sequential-style APIs provide blocking functions that can be called in TCP/IP threads. BSD-style socket APIs provide thread-safe functions that are called in non-TCP/IP threads. The no-copy APIs do not put any overhead on the underlying platform (controller).
For microcontroller platforms like Arduino and ESP, you can find the LwIP stack already implemented in some libraries. The platform-specific libraries may implement the stack intrinsically or as an explicit LwIP driver. Some embedded firmware like MicroPython also uses and supports the LwIP stack.
LwIP with Arduino
The Ethernet library for the Arduino platform is based on the LwIP stack. The library can work with Arduino Ethernet Shield, Arduino Ethernet Shield 2, Leonardo Ethernet, and W5100/W5200/W5500-based networking boards. For explicitly using the LwIP stack on Arduino for networking functions, a modified LwIP source as per Arduino libraries is also available. An STM32 Ethernet Library for Arduino is also based on the LwIP stack and follows the Arduino’s Ethernet API. It can be used to program Ethernet applications on STM32-based boards. Some other Arduino libraries based on the LwIP stack include the following.
lwIP: an LWIP Library port for Arduino.
ESP8266-ping: used for sending and receiving ping messages on ESP8266.
QNEthernet: lwIP-based Ethernet library for Teensy 4.1.
AsyncWebServer_Ethernet: Asynchronous HTTP and WebSocket Server Library for ESP8266.
InqPortal: used for full-featured IoT server on ESP8266.
RTT-Ethernet: used to connect STM32 boards with Ethernet.
LwIP with ESP8266 and ESP32
LwIP stack is available for ESP-IDF as BSD socket API. The API can be used to configure ESP8266/ESP32 as TCP client, TCP server, UDP client, UDP server, UDP multicast, and ESP HTTP client. Some Arduino libraries are also available for ESP8266 (mentioned above), which are based on the LwIP stack. These libraries can configure ESP8266 for Ethernet or as an IoT server.
LwIP device drivers
Professional embedded systems developers can already find LwIP device drivers for several platforms. The drivers are available for CS8900A, MCF5223X, STM32F107xx, STM32F217, Altera NIOS II TSE, TI Stellaris LM3S, ARM Cortex-M3 FM3 MB9BD10S/T, MB9B610S/T, MB9B210S/T, TI RM48xx/RM46xx/TMS570xxx/RM57xx/TMS570LCxx, Analog Devices Blackfin 526/527/536/537, TI C674x, ARM Cortex-M(3), STM32F4x7 and PIC32MX795H512L microcontrollers. A generic LwIP link-layer driver is also available for Ethernet chips. If a platform requires integrating the LwIP stack in Linux/BSD Kernel or a network driver, developers can consult the LwIP platform developers manual. For integrating the LwIP stack into an application, the LwIP application developers manual is the available guide.
LwIP in real-time operating systems
Many real-time operating systems like FreeRTOS, RTLinux, QP, mbed, eCos, and eCos Pro include a LwIP port alongside a BSD-derived networking stack. A boot monitor Micromonitor/uMon also uses a LwIP stack as a companion.
You may also like:
Filed Under: Arduino Projects, ARM, ESP8266, IoT tutorials, STM32, Tech Articles, Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.