DHCP client on STM32 Nucleo and W5100

Posted on 2015/12/07

3


I am still working on a device consisting of STM32 Nucleo F103RB with a W5100 Ethernet shield. This time I included a DHCP client, so that when the device is attached to a network, it will ask for an IP address instead of being statically allocated.

nucleo_w5100

STM32 Nucleo connected with Ethernet shield.

The DHCP protocol follows the RFC 2131 and RFC 2132, and I implemented the library following these specifications. The library depends on a generic POSIX interface that I implemented recently, with no direct dependence to the hardware below, but I also created a wrapper for W5100 that can be used to automatically configure the IP address into the Ethernet interface when the DHCP binds the device to an address.

I fired Wireshark on a computer that is connected to the same router, and by sniffing the packets I can look at the interactions between the Nucleo and the router, shown in the figure below. At first there’s a Discover-Offer transaction between client and server that indicates an available IP address, and then a Request-Ack transaction to bind that address to the client. The transactions are also used to retrieve the gateway, subnet and DNS servers that the client can configure.

Screenshot_2015-12-07_20-43-54

Wireshark capture of DHCP address allocation.

The address is bound with a lease time, so when the lease is expiring it needs to be renewed, so the library needs to be called again after some time to renew the lease. In order to manage time, I also implemented a rudimentary implementation of POSIX time functions, that use Cortex-M SysTick interrupt to count the passage of time.

If you want to try this library, you need:

  • a Nucleo F103RB + W5100 Ethernet shield,
  • GCC ARM Embedded toolchain to compile the program,
  • OpenOCD to write the program into STM32 embedded flash,
  • a router that offers DHCP services.

Then you can:

  1. clone my GitHub repository,
  2. change directory to `tests/dhcp_allocate`,
  3. run `make flash` to compile and upload program,
  4. wait for the Ethernet shield LEDs to indicate that the link is up,
  5. connect a terminal to the Nucleo serial port with baud rate 57600, for example with `minicom -b 57600 -D /dev/ttyACM0`,
  6. press any key in the terminal to start DHCP address allocation.
Posted in: Embedded