Your first TCP/IP Library application

So you have a board, you have a network, and you're ready to attach your device to the network. The first thing to do is establish that basic Ethernet communication works between your PC and your evaluation board.

Install board support

Install the board support package for the evaluation board that you have purchased. From now on we will assume this is the SolderCore, but you can substitute your own board as required. So, install the SolderCore Board Support Package into CrossWorks using the package manager, Tools > Package Manager.

Load the board examples

The easiest way to load the examples for the board is to open up the Contents window, navigate to Board Support, expand the SolderCore Board Support Package item, and click the SolderCore Samples Solution.

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 (Fixed IP address) project. Double-click that project to make it active and press F7 to build. This will compile cleanly: we've tested this before release. If it doesn't build cleanly, that usually means that you're missing one of the packages that the board support package requires, or you've edited something within a support package—if this is the case, you'll need to figure out what you've done or get in touch with us.

Find a spare network address

As the example we are going to run uses a fixed IP address, you need to find a free one to assign to the evaluation board. On Windows, you can use ipconfig to view your network parameters:

> ipconfig
Windows IP Configuration

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : rowley.co.uk
   Link-local IPv6 Address . . . . . : fe80::9c2d:e057:8641:2281%10
   IPv4 Address. . . . . . . . . . . : 10.0.0.58
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 10.0.0.3

> _

Here we see that the subnet mask is 255.255.255.0 and the PC's IP address is 10.0.0.58. So, let's try a random IP address, by changing the last number, to see if it's free:

> ping 10.0.0.32

Pinging 10.0.0.32 with 32 bytes of data:
Reply from 10.0.0.32: bytes=32 time<1ms TTL=64
Reply from 10.0.0.32: bytes=32 time<1ms TTL=64
Reply from 10.0.0.32: bytes=32 time<1ms TTL=64
Reply from 10.0.0.32: bytes=32 time<1ms TTL=64

Ping statistics for 10.0.0.32:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

> _

Ahh, that one's in use. Let's try another:

> ping 10.0.0.44

Pinging 10.0.0.44 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 10.0.0.44:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

> _

OK, that one seems free as the host is not reachable on the network.

You might see a variation on the above:

> ping 10.0.0.44

Pinging 10.0.0.44 with 32 bytes of data:
Reply from 10.0.0.58: Destination host unreachable.
Reply from 10.0.0.58: Destination host unreachable.
Reply from 10.0.0.58: Destination host unreachable.
Reply from 10.0.0.58: Destination host unreachable.

Ping statistics for 10.0.0.44:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

> _

This indicates that the ping request was answered, in this case by 10.0.0.58, with a response that says that the IP address 10.0.0.44 cannot be reached.

Configure the board's network

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 fixed IP address.

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

// TODO: You must alter these to match your network!
#define FIXED_IP_ADDRESS   "10.0.0.44"
#define FIXED_NETMASK      "255.255.255.0"

// 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"

// Thread Priority
#define NET_TASK_PRIORITY  200

// Network interface,
static CTL_NET_INTERFACE_t nic;

static void
bring_up_network(void)
{
  CTL_IP_CONFIG_t ip_config;

  // Clear network IP configuration for population.
  memset(&ip_config, 0, sizeof(CTL_IP_CONFIG_t));

  // Assign fixed IP address and subnet mask.
  ip_config.ip_addr = ctl_net_scan_dot_decimal_ip_addr(FIXED_IP_ADDRESS);
  ip_config.subnet_mask = ctl_net_scan_dot_decimal_ip_addr(FIXED_NETMASK);

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

  // Bring up network.
  example_check_status(ctl_mac_init(&nic));

  // Bring up the IP network.
  example_check_status(ctl_net_init(NET_TASK_PRIORITY, &ip_config));

  // Bring up only ICMP to respond to pings.
  example_check_status(ctl_icmp_init());
}

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

  // Initialize platform.
  platform_initialize();

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

  // Start network.
  bring_up_network();

  // Idle away, the network task responds to pings.
  for (;;)
    {
      // Dump message inviting a ping.
      printf("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()));

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

Modify the definition of FIXED_IP_ADDRESS to match your selected IP address and FIXED_NETMASK to match your subnet mask.

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 selected IP address and subnet mask:

IP address is 10.0.0.44 and subnet mask is 255.255.255.0
IP address is 10.0.0.44 and subnet mask is 255.255.255.0
IP address is 10.0.0.44 and subnet mask is 255.255.255.0

This is inviting you to ping the board. So, do it:

> ping 10.0.0.44

Pinging 10.0.0.44 with 32 bytes of data:
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64
Reply from 10.0.0.44: bytes=32 time<1ms TTL=64

Ping statistics for 10.0.0.44:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

> _
That's it!

So, you now have a functioning Ethernet connection between your PC and your target board!