Lightweight TCP/IP (lwIP) Stack with SDK

Document created by Fang Li Employee on Sep 28, 2015Last modified by Fang Li Employee on Sep 29, 2015
Version 3Show Document
  • View in full screen mode

Recently I was told that there really lack of enough document && demo regarding the lwIP stack with SDK. So I would like to share more detail regarding this topic, and hope it will been helpful and useful.



  • Small independent implementations of the TCP/IP protocol suite
  • One of the most widely used TCP/IP stack
  • Under a BSD-style license
  • Support run in both bare metal and RTOS environment
  • Suitable for use in embedded system with tens of free RAM and room for around 40 kilobytes of code ROM


lwIP stack on KSDK

    - <ksdk_install_dir>/middleware/tcpip/lwip


lwIP stack on KSDK - Code Structure



  • lwIP code structure is shown as below:

  • src

    This subfolder includes the latest stable lwIP 1.4.1 source code which can be downloaded from this link:


  • port

    This subfolder includes the adapter code which adapts lwIP stack to SDK.


lwIP stack on KSDK - Source Code


Structure of source code under is shown below:


lwIP stack on KSDK – Adapter code


  • <ksdk_install_dir>/middleware/tcpip/lwip/port

  • <ksdk_install_dir>/middleware/tcpip/lwip/port/arch

  • These Adapter code could be divided into four types:

        Ethernet driver adapter code

        OSA adapter code

        Additional code

        lwIP stack configuration code


Ethernet Driver Adapter Code


  • Provide Ethernet relevant interface including

   − Ethernet hardware initialization

   − Network interface initialization

   − Send packet to Ethernet hardware

   − Receive packet from Ethernet hardware

   − Pass packet to lwIP stack


  • Both polling and interrupt mode are provided for packet receiving

    - In <ksdk_install_dir>/platform/drivers/inc/fsl_enet_driver.h,

         #define ENET_RECEIVE_ALL_INTERRUPT 0 to enable polling mode.

         Or set

         #define ENET_RECEIVE_ALL_INTERRUPT 1 to enable interrupt mode.


  • Ethernet driver adapter code provides ENET_receive API for polling mode
  • Under RTOS environment, a separate task executing ENET_receive for packet receiving is created in Ethernet hardware initialization code for polling mode
  • Under Bare Metal environment the ENET_receive API need to be called endlessly to do packet receiving


OSA Adapter Code


  • Provide OS dependent types and interface for RTOS environment (configured NO_SYS = 0)





  • Provide time tick for bare metal environment(NO_SYS = 1)
  • sys_now to get the current time
  • sys_assert to print an assertion messages and abort execution.


Additional Code

  • Provide definition and interface for:


   −Compiler hints for packing and platform specific

   −Diagnostic output


lwIP Stack Configuration Code


  • Provides a way to override much of the behavior of lwIP based on opt.h.

    − Module support (Code size)


          Default inclusions:



    TCP (LWIP_TCP) -- this is a big one!

    Statistics (LWIP_STATS)



        Default exclusions:







   − Memory management (RAM usage)

             lwIP’s custom heap-based mem_malloc

             C standard library malloc and free

             Memory pools


lwIP Stack Porting Guide


   Possible Situation for Porting

New Soc with Limited RAM

In current KSDK, the main RAM consumption for lwIP is show below:


  • If resource on the new platform is not enough, could reduce ram consumption for ram_heap and pbuf_pool.

        In lwip/port/lwipopts.h:

#define MEM_SIZE                (12*1024)    /**the size of ram_heap/

#define PBUF_POOL_SIZE          10      /*the number of buffers in the pbuf pool. */

#define PBUF_POOL_BUFSIZE       1518 /* the size of each pbuf in the pbuf pool. */


  • pbuf_pool also support dynamically allocate from ram_heap.

       In lwip/include/opt.h:

         #define MEMP_MEM_MALLOC     1 /*Use mem_malloc/mem_free to do allocate*/


  • Use libc malloc/free to manage the memory allocation instead of mem_malloc/mem_free, memory definition for ram_heap is not needed.

        #define MEM_LIBC_MALLOC       1 /*Use malloc/free/realloc provided by C-library*/


  New Soc NOT Support PIT timer

  • OSA adapter code should provide Bare metal lwIP stack with a 1ms period timer. Current the code use pit timer to do this and the definition in sys_arch.c is as below:

#define HWTIMER_LL_DEVIF    kPitDevif      // Use hardware timer PIT

#define HWTIMER_LL_SRCCLK   kBusClock     // Source Clock for PIT

#define HWTIMER_LL_ID       3

#define HWTIMER_PERIOD          1000      // 1 ms interval


  • If the new platform does not provide PIT, we could use other hardware timers to implement the 1ms period timer.




Lightweight TCP/IP (lwIP) Stack Porting v

Lightweight TCP/IP (lwIP) Stack Porting Guide

Lightweight TCP/IP (lwIP) Stack Porting Guide



3 people found this helpful