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
bring_up_network(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.
  example_check_status(ctl_mac_init(&nic));

  // 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.
  example_check_status(ctl_udp_init(0));
  example_check_status(ctl_icmp_init());

  // Start DHCP to assign us an IP address.
  example_check_status(ctl_dhcp_init());
}

int
main(void)
{
  char dot_ipaddr[16], dot_netmask[16];

  // Initialize platform.
  platform_initialize();

  // Initialize NIC for this platform.
  example_check_status(platform_configure_network(&nic));

  // Start network.
  bring_up_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()));
        }
      else
        {
          // Can't ping me yet.
          printf("DHCP: awaiting IP address assignment\n");
        }

      // Don't be too enthusiastic with messages.
      ctl_delay(1000);
    }
}

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 10.0.0.44 and subnet mask is 255.255.255.0
DHCP: IP address is 10.0.0.44 and subnet mask is 255.255.255.0
DHCP: IP address is 10.0.0.44 and subnet mask is 255.255.255.0

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?