Your second TCP/IP Library application

It's not that common to use a fixed IP address for a network-attached device. Modern networks use dynamically-assigned IP addresses and a DHCP server to manage assignment: when a device powers on, it broadcasts a request to the network asking for a DHCP server to assign it an IP address. Using a DHCP server is now a necessity with so many devices attached to a LAN, there is no way that a human can possibly manage a large network without error.

Select and build the project

In the examples for your board, you'll find a Networking Projects solution, and within that a Minimal Network with Ping (DHCP IP address) project. Double-click that project to make it active and press F7 to build.

Double-click the file example_minimal_ping_fixed_ipaddr.c in the Source Files folder and it will open in the code editor:

// Set up network using a DHCP-assigned IP address.

#include "libnet/ctl_net_private.h"
#include "libplatform/platform.h"
#include "libplatform/platform_network.h"
#include "example_support.h"

// Assign a fixed MAC address to the NIC.  Normally this will be blown into
// OTP or some other nonvolatile medium when the device is personalized as
// part of production.
#define FIXED_MAC_ADDRESS        "bc-28-d6-ff-ff-ff"

// Network task thread priority
#define NET_TASK_PRIORITY        200

// Network interface,
static CTL_NET_INTERFACE_t nic;

static void
  // Assign a fixed MAC address to the NIC.
  example_check_status(ctl_net_scan_mac_addr(&nic.mac.mac_addr, FIXED_MAC_ADDRESS));

  // Initialize MAC.

  // Bring up network task and use DHCP to assign an IP address.
  example_check_status(ctl_net_init(NET_TASK_PRIORITY, 0));

  // Bring up UDP and ICMP: DHCP requires UDP, and ICMP will respond to pings.

  // Start DHCP to assign us an IP address.

  char dot_ipaddr[16], dot_netmask[16];

  // Initialize platform.

  // Initialize NIC for this platform.

  // Start network.

  // Idle away; when we're configured, dump our network.
  for (;;)
      // See if we've acquired an IP address yet...
      if (ctl_net_get_ip_address())
          // Dump message inviting a ping.
          printf("DHCP: IP address is %s and subnet mask is %s\n",
                 ctl_ip_sprint_addr(dot_ipaddr, ctl_net_get_ip_address()),
                 ctl_ip_sprint_addr(dot_netmask, ctl_net_get_subnet_mask()));
          // Can't ping me yet.
          printf("DHCP: awaiting IP address assignment\n");

      // Don't be too enthusiastic with messages.

There's no fixed IP address in this, but there is an option to start up the DHCP client subsystem to manage acquisition of DHCP-assigned IP addresses.

See if it works

Power up and attach a network cable to your evaluation board, and press F5 to run your code. The application downloads and, if CrossWorks is configured to stop at main, press F5 again to continue running the code.

In the CrossWorks Debug Terminal you should see something similar to the following, but with your DHCP-assigned IP address and subnet mask:

DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: awaiting IP address assignment
DHCP: IP address is and subnet mask is
DHCP: IP address is and subnet mask is
DHCP: IP address is and subnet mask is

You can ping the device to make sure that it does indeed work.

Job done!

You now have a functioning Ethernet connection between your PC and your target board, using a dynamically-assigned IP address. However, it's a bit of a bore to type in IP addresses each time, and as the IP address may change, how do you know which IP address to use?