UltimaSerial AVR lwIP Example:  How to enable Auto IP (APIPA) with a DHCP client on EVK1100

Data logger


Windaq add-ons
Windaq Add-ons




Ultimaserial XChart




Ultimaserial Classroom

Please take the basic lwIP DHCP client workshop  before trying out this one

When a DHCP server is not available, Windows Vista/XP will use Automatic Private Internet Protocol Addressing (APIPA) to assign itself an IP within the range of through inclusive (for more info, please google or wiki it)

lwIP support this option, too.

Here we will add this option to our basic lwIP DHCP client example

First, we pick a real piece of hardware from Atmel:  EVK1100. Its processor is 32UC3A0512, but the codes below should work in other hardware.  

Software Development Platform: AVR32 with lwIP and FreeRTOS.

To enable auto IP with a DHCP client on EVK1100, using lwIP on FreeRTOS, under AVR32

  1. AVR32->File->New->Example, select EVK1100-SERVICES-LWIP example
  2. Give a Project Name: MyTestFromLwIP, then click Finish. This will create a new project under the Project Explorer pane.
  3. Open MyTestFromLwIP->src->CONFIG->lwipopts.h:
    1. change 
              #define LWIP_DHCP 0
              #define LWIP_DHCP 1
    2. add  #define LWIP_AUTOIP 1
    3. add  #define LWIP_DHCP_AUTOIP_COOP 1
  4. Open MyTestFromLwIP->src->NETWORK->ethernet.c, and replace prvEthernetConfigureInterface with:

          static void prvEthernetConfigureInterface(void * param){
          struct ip_addr xIpAddr, xNetMask, xGateway;
          extern err_t ethernetif_init( struct netif *netif );
          unsigned portCHAR MacAddress[6];

    MacAddress[0] = ETHERNET_CONF_ETHADDR0;
    MacAddress[1] = ETHERNET_CONF_ETHADDR1;
    MacAddress[2] = ETHERNET_CONF_ETHADDR2;
    MacAddress[3] = ETHERNET_CONF_ETHADDR3;
    MacAddress[4] = ETHERNET_CONF_ETHADDR4;
    MacAddress[5] = ETHERNET_CONF_ETHADDR5;

    vMACBSetMACAddress( MacAddress );

    IP4_ADDR( &xIpAddr,0,0,0,0 );
    IP4_ADDR( &xNetMask,0,0,0,0 );
    IP4_ADDR( &xGateway,0,0,0,0 );

    netif_add( &MACB_if, &xIpAddr, &xNetMask, &xGateway, NULL, ethernetif_init, tcpip_input );
    netif_set_default( &MACB_if );


  5. Add the following codes to module portTASK_FUNCTION( vStartEthernetTask, pvParameters ), after prvlwIPInit():


    (MACB_if.ip_addr.addr==0) {
    if (mscnt >= DHCP_COARSE_TIMER_SECS*1000) {
    mscnt = 0;

    print_dbg("\nDone with DHCP");
    print_dbg("\nAcquired IP =");
           // print_dbg_ulong(0xff&(MACB_if.ip_addr.addr>>24));

  6. To use the debug outputs, you will need uncomment all the functions in the codes and add  #include "print_funcs.h"  to the header section of ethernet.c. To receive the debug outputs, start Windows' HyperTerminal and set Bits per second =115200, Data bits = 8, Parity = none, Stop bits = 1, Flow control = None.
  7. Connect your EVK1100 to a Windows Vista PC directly, so no DHCP server exists.
  8. Build and run
  9. If you enable the debug output, you will see the program will acquire a private IP after DHCP times out, and you can ping the IP.

  Click here for more lwIP examples

Find out how to upgrade to the latest lwIP



 Last update: 02/24/12