For basic embedded network designs, a single Ethernet or Wi-Fi interface will suffice. As projects and consumer expectations grow, more complex connectivity requirements, such as multiple interfaces, become more prevalent. With our network stack, it is possible to meet this project requirement.
If you are familiar with µC/TCP-IP or Cs/NET's API function calls you’ve seen our example start-up code for creating a single interface over Ethernet. When dealing with multiple interfaces it is as simple as calling the same creation and startup process for each desired interface. The following pseudo-code will elaborate on this:
The flowchart shown below details how to setup a multiple-interface initialization for your application. Once the application initializes the core Network stack, a new network interface will be added. With Wi-Fi connections, subsequently scanning for wireless access points to join may be executed. For hidden networks (whose name is known to the application code already) the scan operation could be skipped. Once that is completed, the application must configure the interface for the desired IP host address, subnet mask, and default gateway.
A sudo-code representation of this process would look similar to:
net_err = Net_Init(...); /* Initialize core stack */
/* BEGIN INTERFACE CONFIGURATION */
if_1 = NetIF_Add(...); /* Add Interface 1 Initialization */
/* Specifics */
NetIF_Start(if_1, /* Start Network Interface 1 */
...);
NetIPv4_CfgAddrAdd(if_1, /* Add static IPv4 address */
...);
NetIF_LinkStateSubscribe(if_1, /* Create link-state callback for */
...); /* specified interface */
/* BEGIN CONFIGURATION OF "N" */
/* OTHER INTERFACES */
if_N = NetIF_Add(...); /* Add Interface N Initialization */
/* Specifics */
NetIF_Start(if_N, /* Start Network Interface N */
...);
NetIPv4_CfgAddrAdd(if_N, /* Add static IPv4 address */
...);
NetIF_LinkStateSubscribe(if_N, /* Create link-state callback for */
...); /* specified interface */
After completely initializing your first interface, the application should follow the same procedure for any additional interfaces needed by the application. Once the initialization process finalizes each of those interfaces should be open for communication. Simple ping tests can check if each interface is functioning properly. The establishing of socket connections will then be handled by the user’s application code design for sending and receiving data via these interfaces.
Along with the creation of each interface, there are other configuration parameters to consider for applications utilizing multiple interfaces in their design. Those projects incorporating uC/TCP-IP or Cs/NET and similar products contain a net_dev_cfg.c file that declares configuration structs for each of the underlying hardware’s network interfaces. These structs contain configuration fields for buffer size, memory pool type, and various other network interface characteristics. These configurations will be applied by uC/TCP-IP and Cs/NET to all appropriate network interfaces and directly influence the network stack’s RAM consumption.
The quickest way to approximate your system's RAM usage for each of these interfaces is to count the total number of buffers set aside for each interface. As demonstrated below, as per your application’s configurations in net_dev_cfg.c, these configurations are clearly shown as the interface’s receive (Rx) buffers, and a pair of values for large and small transmit (Tx) buffers. Total RAM consumption credited to individual interfaces is calculated by multiplying the number of buffers with their corresponding buffer size configurations. Taking the number for these three different buffer types and adding them together.
const NET_DEV_CFG_ETHER_DEMO NetDev_Cfg_Demo_EtherC0 = {
NET_IF_MEM_TYPE_MAIN, /* Desired receive buffer memory */
/* pool type: */
/* NET_IF_MEM_TYPE_MAIN */
/* Buffers use main memory */
/* NET_IF_MEM_TYPE_DEDICATED */
/* Buffers have dedicated memory */
1520u, /* Size of Rx buffers */
10u, /* Number of Rx buffers */
32u, /* Alignment of Rx buffers */
0u, /* Offset of Rx buffers */
.....
.....
1520u, /* Size of large Tx buffers */
10u, /* Number of large Tx buffers */
60u, /* Size of small Tx buffers */
10u, /* Number of small Tx buffers */
32u, /* Alignment of Tx buffers */
0u, /* Offset of Tx buffers */
If your application needs to track the connection link state of an interface, there is a link-subscribe function and callback available for use in the uC/TCP-IP and Cs/NET stacks NetIF_LinkStateSubscribe(). Using this functionality, the application establishes a callback routine that will trigger whenever the state of the specified interface changes. This callback is useful when setting up control routines to handle changes to a suddenly unresponsive connection.
Consult our online documentation for more details on NetIF_LinkStateSubscribe() and other net API functions. There is also a multiple-interface example application amongst the documentation with more examples on the procedures described in this article.