Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This example show how to initialize µC/TCP-IP with multiple interface:

  1. Initialize Stack tasks and objects
  2. Initialize an Ethernet Interface
  3. Start that Ethernet Interface
  4. Configure IP addresses of the Ethernet Interface
  5. Initialize an Wireless Interface
  6. Start that Wireless Interface
  7. Scan for Wireless networks available
  8. Analyze scan result
  9. Join a Wireless network
  10. Configure IP addresses of that Wireless Interface

This example is based on template files so some modifications will be required, insert the appropriate project/board specific code to perform the stated actions. Note that the file init_multiple_if.c, located in the folder $/Micrium/Software/uC-TCPIP/Examples/Init, contains this sample application:

Code Block
languagecpp
linenumberstrue
#include  <cpu_core.h>
#include  <lib_mem.h>
#include  <Source/net.h>
#include  <Source/net_ascii.h>
#include  <IF/net_if.h>
#include  <IF/net_if_wifi.h>

#ifdef NET_IPv4_MODULE_EN
#include  <IP/IPv4/net_ipv4.h>
#endif

#ifdef NET_IPv6_MODULE_EN
#include  <IP/IPv6/net_ipv6.h>
#endif

#include  <Cfg/Template/net_dev_cfg.h>                      /* Device configuration header.           */
#include  <Dev/Ether/Template/net_dev_ether_template_dma.h> /* Device driver header.                  */
#include  <Dev/WiFi/Template/net_dev_wifi_template_spi.h>   /* Device driver header.                  */
#include  <Dev/Ether/PHY/Generic/net_phy.h>                 /* PHY driver header.                     */
#include  <Dev/WiFi/Manager/Generic/net_wifi_mgr.h>
#include  <BSP/Template/net_bsp_ether.h>                    /* BSP header.                            */
#include  <BSP/Template/net_bsp_wifi.h>                     /* BSP header.                            */
 
 

CPU_BOOLEAN  AppInit_TCPIP_MultipleIF (void)
{
#ifdef NET_IPv4_MODULE_EN
    NET_IPv4_ADDR       addr_ipv4;
    NET_IPv4_ADDR       msk_ipv4;
    NET_IPv4_ADDR       gateway_ipv4;
#endif
#ifdef NET_IPv6_MODULE_EN
    CPU_BOOLEAN         cfg_result;
#if (NET_IPv6_CFG_ADDR_AUTO_CFG_EN == DEF_DISABLED)
    NET_FLAGS           ipv6_flags;
    NET_IPv6_ADDR       addr_ipv6;
#endif
#endif
    NET_IF_NBR          if_nbr_ether;
    NET_IF_NBR          if_nbr_wifi;
    NET_IF_WIFI_AP      ap[10];
    NET_IF_WIFI_SSID   *p_ssid;
    NET_IF_WIFI_SSID    ssid;
    NET_IF_WIFI_PSK     psk;
    CPU_INT16U          ctn;
    CPU_INT16U          i;
    CPU_INT16S          result;
    CPU_BOOLEAN         found;
    NET_ERR             err_net;

                                                                /* -------- INITIALIZE NETWORK TASKS & OBJECTS -------- */
    err_net = Net_Init(&NetRxTaskCfg,
                       &NetTxDeallocTaskCfg,
                       &NetTmrTaskCfg);
    if (err_net != NET_ERR_NONE) {
        return (DEF_FAIL);
    }

                                                                /* -------------- ADD ETHERNET INTERFACE -------------- */
    if_nbr_ether = NetIF_Add((void    *)&NetIF_API_Ether,       
                             (void    *)&NetDev_API_TemplateEtherDMA, /* Device driver API                              */
                             (void    *)&NetDev_BSP_BoardDev_Nbr,     /* BSP API                                        */
                             (void    *)&NetDev_Cfg_Ether_1,          /* Device configuration                           */
                             (void    *)&NetPhy_API_Generic,          /* PHY driver API                                 */
                             (void    *)&NetPhy_Cfg_Ether_1,          /* PHY configuration                              */
                                        &err_net);
    if (err_net != NET_IF_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ------------- START ETHERNET INTERFACE ------------- */
    NetIF_Start(if_nbr_ether, &err_net);                        /* Makes the interface ready to receive and transmit.   */
    if (err_net != NET_IF_ERR_NONE) {
        return (DEF_FAIL);
    }

#ifdef NET_IPv4_MODULE_EN
                                                                /* --------- CONFIGURE IPV4 STATIC ADDRESSES ---------- */
                                                                /* For Dynamic IPv4 configuration, µC/DHCPc is required */
                                                                /* Update IPv4 Addresses following your network ...     */
                                                                /* ... requirements.                                    */
    NetASCII_Str_to_IP("10.10.10.64",                           /* Convert Host IPv4 string address to 32 bit address.  */
                       &addr_ipv4,
                        NET_IPv4_ADDR_SIZE,
                       &err_net);
    NetASCII_Str_to_IP("255.255.255.0",                         /* Convert IPv4 mask string to 32 bit address.          */
                       &msk_ipv4,
                        NET_IPv4_ADDR_SIZE,
                       &err_net);
    NetASCII_Str_to_IP("10.10.10.1",                            /* Convert Gateway string address to 32 bit address.    */
                       &gateway_ipv4,
                        NET_IPv4_ADDR_SIZE,
                       &err_net);
    NetIPv4_CfgAddrAdd(if_nbr_ether,                            /* Add a statically-configured IPv4 host address,   ... */
                       addr_ipv4,                               /* ... subnet mask, & default gateway to the        ... */
                       msk_ipv4,                                /* ... interface.                                       */
                       gateway_ipv4,
                      &err_net);
    if (err_net != NET_IPv4_ERR_NONE) {
        return (DEF_FAIL);
    }
#endif

#ifdef NET_IPv6_MODULE_EN
#if (NET_IPv6_CFG_ADDR_AUTO_CFG_EN == DEF_ENABLED)
                                                                /* ----- START IPV6 STATELESS AUTO-CONFIGURATION ------ */
    NetIPv6_AddrAutoCfgHookSet(if_nbr_ether,                    /* Set Hook function to received Auto-Cfg result.       */
                              &App_AutoCfgResult,
                              &err_net);
    
    cfg_result = NetIPv6_AddrAutoCfgEn(if_nbr_ether,            /* Enable and Start Auto-Configuration process.         */
                                       DEF_YES,
                                      &err_net);
    if (cfg_result == DEF_FAIL) {
        return (DEF_FAIL);
    }
    
#else
                                                                /* ----- CONFIGURE IPV6 STATIC LINK LOCAL ADDRESS ----- */
                                                                /* DHCPv6c is not yet available.                        */
                                                                /* TODO Update IPv6 Address following your network ...  */
                                                                /* ... requirements.                                    */
    NetASCII_Str_to_IP("fe80::1111:1111",                       /* Convert IPv6 string address to 128 bit address.      */
                       &addr_ipv6,
                        NET_IPv6_ADDR_SIZE,
                       &err_net);
 
    ipv6_flags = 0;                                             
    DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_BLOCK_EN);            /* Set Address Configuration as blocking.               */
    DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_DAD_EN);              /* Enable DAD with Address Configuration.               */
 
    cfg_result = NetIPv6_CfgAddrAdd(if_nbr_ether,               /* Add a statically-configured IPv6 host address to ... */
                                   &addr_ipv6,                  /* ... the interface.                                   */
                                    64,
                                    ipv6_flags,
                                   &err_net);
    if (cfg_result == DEF_FAIL) {
        return (DEF_FAIL);
    }
#endif
#endif

                                                                /* -------------- ADD WIRELESS INTERFACE -------------- */
    if_nbr_wifi = NetIF_Add((void    *)&NetIF_API_WiFi,
                            (void    *)&NetDev_API_TemplateWiFiSpi,  /* Change following your Device driver API.        */
                            (void    *)&NetDev_BSP_WiFi,             /* Change for your BSP API.                        */
                            (void    *)&NetDev_Cfg_WiFi_1,           /* Change for Device configuration.                */
                            (void    *)&NetWiFiMgr_API_Generic,
                                        DEF_NULL,
                                       &err_net);
    if (err_net != NET_IF_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ------------- START WIRELESS INTERFACE ------------- */
    NetIF_Start(if_nbr_wifi, &err_net);                         /* Makes the interface ready to receive and transmit.   */
    if (err_net != NET_IF_ERR_NONE) {
        return (DEF_FAIL);
    }

                                                                /* ------------ SCAN FOR WIRELESS NETWORKS ------------ */
    ctn = NetIF_WiFi_Scan(if_nbr_wifi,
                          ap,                                   /* Access point table.                                  */
                          10,                                   /* Access point table size.                             */
                          DEF_NULL,                             /* Hidden SSID.                                         */
                          NET_IF_WIFI_CH_ALL,                   /* Channel to scan.                                     */
                         &err_net);
    if (err_net != NET_IF_WIFI_ERR_NONE) {
        return (DEF_FAIL);
    }

                                                                /* --------- ANALYSE WIRELESS NETWORKS FOUND ---------- */
    found = DEF_NO;
    for (i = 0; i < ctn - 1; i++) {                             /* Browse table of access point found.                  */
        p_ssid = &ap[i].SSID;
        result = Str_Cmp_N((CPU_CHAR *)p_ssid,                  /* Search for a specific Wireless Network SSID.         */
                                      "Wifi_AP_SSID",           /* Change for your WiFi Network SSID.                   */
                                       NET_IF_WIFI_STR_LEN_MAX_SSID);
        if (result == 0) {
            found = DEF_YES;
            break;
        }
    }
    if (found == DEF_NO) {
        return (DEF_FAIL);
    }

                                                                /* ------------- JOIN A WIRELESS NETWORK -------------- */
    Mem_Clr(&ssid, sizeof(ssid));
    Mem_Clr(&psk,  sizeof(psk));
    Str_Copy_N((CPU_CHAR *)&ssid,
                           "Wifi_AP_SSID",                      /* Change for your WiFi Network SSID.                   */
                            12);                                /* SSID string length.                                  */
    Str_Copy_N((CPU_CHAR *)&psk,
                           "Password",                          /* Change for your WiFi Network Password.               */
                            8);                                 /* PSK string length.                                   */
    NetIF_WiFi_Join(if_nbr_wifi, 
                    ap[i].NetType,                              /* WiFi Network type.                                   */
                    NET_IF_WIFI_DATA_RATE_AUTO,                 /* Data rate.                                           */
                    ap[i].SecurityType,                         /* WiFi Network security type.                          */
                    NET_IF_WIFI_PWR_LEVEL_HI,                   /* Power level.                                         */
                    ssid,                                       /* WiFi Network SSID.                                   */
                    psk,                                        /* WiFi Network PSK.                                    */
                   &err_net);
    if (err_net != NET_IF_WIFI_ERR_NONE) {
        return (DEF_FAIL);
    }

#ifdef NET_IPv4_MODULE_EN
                                                                /* --------- CONFIGURE IPV4 STATIC ADDRESSES ---------- */
                                                                /* For Dynamic IPv4 configuration, µC/DHCPc is required */
                                                                /* Update IPv4 Addresses following your network ...     */
                                                                /* ... requirements.                                    */
    NetASCII_Str_to_IP("192.168.1.10",                          /* Convert Host IPv4 string address to 32 bit address.  */
                       &addr_ipv4,
                        NET_IPv4_ADDR_SIZE,
                       &err_net);
    NetASCII_Str_to_IP("255.255.255.0",                         /* Convert IPv4 mask string to 32 bit address.          */
                       &msk_ipv4,
                        NET_IPv4_ADDR_SIZE,
                       &err_net);
    NetASCII_Str_to_IP("192.168.1.1",                           /* Convert Gateway string address to 32 bit address.    */
                       &gateway_ipv4,
                        NET_IPv4_ADDR_SIZE,
                       &err_net);
    NetIPv4_CfgAddrAdd(if_nbr_wifi,                             /* Add a statically-configured IPv4 host address,   ... */
                       addr_ipv4,                               /* ... subnet mask, & default gateway to the        ... */
                       msk_ipv4,                                /* ... interface.                                       */
                       gateway_ipv4,
                      &err_net);
    if (err_net != NET_IPv4_ERR_NONE) {
        return (DEF_FAIL);
    }
#endif

#ifdef NET_IPv6_MODULE_EN
#if (NET_IPv6_CFG_ADDR_AUTO_CFG_EN == DEF_ENABLED)
                                                                /* ----- START IPV6 STATELESS AUTO-CONFIGURATION ------ */
    NetIPv6_AddrAutoCfgHookSet(if_nbr_wifi,                     /* Set hook function to received Auto-Cfg result.       */
                              &App_AutoCfgResult,				/* TODO update pointer to hook fnct implemented in App.	*/
                              &err_net);
    
    cfg_result = NetIPv6_AddrAutoCfgEn(if_nbr_wifi,             /* Enable and Start Auto-Configuration process.         */
                                       DEF_YES,
                                      &err_net);
    if (cfg_result == DEF_FAIL) {
        return (DEF_FAIL);
    }
    
#else
                                                                /* ----- CONFIGURE IPV6 STATIC LINK LOCAL ADDRESS ----- */
                                                                /* DHCPv6c is not yet available.                        */
                                                                /* Update IPv6 Address following your network ...       */
                                                                /* ... requirements.                                    */
      
    NetASCII_Str_to_IP("fe80::4444:1111",                       /* Convert IPv6 string address to 128 bit address.      */
                       &addr_ipv6,
                        NET_IPv6_ADDR_SIZE,
                       &err_net);
 
    ipv6_flags = 0;
    DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_BLOCK_EN);            /* Set Address Configuration as blocking.               */
    DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_DAD_EN);              /* Enable DAD with Address Configuration.               */
 
    cfg_result = NetIPv6_CfgAddrAdd(if_nbr_wifi,                /* Add a statically-configured IPv6 host address to ... */
                                   &addr_ipv6,                  /* ... the interface.                                   */
                                    64,
                                    ipv6_flags,
                                   &err_net);
    if (cfg_result == DEF_FAIL) {
        return (DEF_FAIL);
    }
#endif

    return (DEF_OK);
}

 

 

Info

Refer to the sample codes in section Ethernet Sample Application and WiFi Sample Application for more details on the diverse function calls.