UltimaSerial AVR lwIP Example:  How to enable a DHCP client on EVK1100

Data logger


Windaq add-ons
Windaq Add-ons




Ultimaserial XChart




Ultimaserial Classroom

lwIP is a popular free TCP/IP stack for many embedded processors. The following workshop enables DHCP client so that it can acquire an IP from a DHCP server.

Here 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 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, and change

          #define LWIP_DHCP 0
          #define LWIP_DHCP 1

          #define MEMP_NUM_UDP_PCB 1
          #define MEMP_NUM_UDP_PCB 2

  4. Add #include "lwip/dhcp.h" to the header section of MyTestFromLwIP->src->NETWORK->ethernet.c
  5. 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 );

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

         int mscnt =0;
    (MACB_if.ip_addr.addr==0) {
                  mscnt += DHCP_FINE_TIMER_MSECS;
                  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));

  7. 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, connect a RS232 cable to UART 1 of EVK1100, start Windows' HyperTerminal and set Bits per second =115200, Data bits = 8, Parity = none, Stop bits = 1, Flow control = None.
  8. Build and run it in a network with a DHCP server, such as a DSL router, and you can ping the IP once it is acquired from a DHCP server.
  9. Watch out, due to the way FreeRTOS handles priority, make sure you either keep all threads in the same priority or make sure the higher priority thread goes into blocked state periodically, such as vTaskDelay( 1 ), or the highest priority thread will never let the lower ones to run.



 Click here for more lwIP examples

Find out how to upgrade to the latest lwIP