- This example show how to initialize µC/TCP-IP with mutliple multiple interface:
- Initialize Stack tasks and objects
- Initialize an Ethernet Interface
- Start that Ethernet Interface
- Configure IP addresses of the Ethernet Interface
- Initialize an Wireless Interface
- Start that Wireless Interface
- Scan for Wireless networks available
- Analyze scan result
- Join a Wireless network
- 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 | ||||
---|---|---|---|---|
| ||||
#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; /* -------- INITIALIZEINIT 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 interfaceIF ready to receiveRX and transmitTX. */ if (err_net != NET_IF_ERR_NONE) { return (DEF_FAIL); } #ifdef NET_IPv4_MODULE_EN /* --------- CONFIGURECFG IPV4 STATIC ADDRESSESADDR ---------- */ /* For Dynamic IPv4 configurationcfg, µC/DHCPc is required */ /* Update IPv4 AddressesAddr following your network ... */ /* ... network requirements. */ */ NetASCII_Str_to_NetASCII_Str_to_IP("10.10.10.64", /* Convert Host IPv4 string addressaddr to 32 bit address. addr*/ &addr_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("255.255.255.0", /* Convert IPv4 mask string to 32 bit addressaddr. */ &msk_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("10.10.10.1", /* Convert Gateway string address to 32 bit addressaddr. */ &gateway_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetIPv4_CfgAddrAdd(if_nbr_ether, /* Add a statically-configured IPv4 host address, ... */ addr_ipv4, /* ... host addr, subnet mask, & default gateway to the ... */ msk_ipv4, /* ... interface.default gateway to the IF. */ 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) /* ----- CONFIGURESTART IPV6 STATICSTATELESS LINK LOCAL ADDRESS -AUTO-CFG ---- */ NetIPv6_AddrAutoCfgHookSet(if_nbr_ether, /* Set Hook to received Auto-Cfg result. */ &App_AutoCfgResult, /* IPv6 Autoconfiguration is not yet supported. */ &err_net); cfg_result = NetIPv6_AddrAutoCfgEn(if_nbr_ether, /* Enable and Start Auto-Cfg process. */ /* DHCPv6c is not yet available. DEF_YES, */ &err_net); if (cfg_result == DEF_FAIL) { return (DEF_FAIL); } #else /* TODO Update IPv6 Address following your network ... */ /* --- CFG IPV6 STATIC LINK LOCAL ADDR -- */ /* ... requirements. */ NetASCII_Str_to_IP("fe80::1111:1111", /* DHCPv6c is not yet available. */ /* Convert IPv6 string address to 128 bit address. */ &addr_ipv6, /* TODO Update IPv6 Addr following NET_IPv6_ADDR_SIZE,your...*/ &err_net); NetIPv6_CfgAddrAdd(if_nbr_ether, /* Add a statically-configured IPv6 host address to ...... network requirements. */ NetASCII_Str_to_IP("fe80::1111:1111", &addr_ipv6, /* Convert IPv6 string to 128 bit addr. */ /* ... the interface. &addr_ipv6, NET_IPv6_ADDR_SIZE, */ &err_net); ipv6_flags = 64,0; &err_net); if (err_net != NET_IPv6_ERR_NONE) { return (DEF_FAIL); } #endifDEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_BLOCK_EN); /* Set Addr Cfg as blocking. */ DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_DAD_EN); /* Enable DAD with Addr Configuration. */ cfg_result = NetIPv6_CfgAddrAdd(if_nbr_ether, /* -------------- ADD WIRELESS INTERFACE -------------- */ if_nbr_wifi = NetIF_Add((void *)&NetIF_API_WiFi, /* Add a statically-configured IPv6 ... */ (void *)&NetDevaddr_API_TemplateWiFiSpiipv6, /* Change following your Device driver API. /*/ ... host address to the interface. */ (void *)&NetDev_BSP_WiFi, /* Change for64, your BSP API. */ ipv6_flags, (void *)&NetDev_Cfg_WiFi_1, /* Change for Device configuration. &err_net); if (cfg_result == DEF_FAIL) { */ return (DEF_FAIL); } #endif #endif (void *)&NetWiFiMgr_API_Generic, /* ------- ADD WIRELESS DEF_NULL, INTERFACE ------- */ if_nbr_wifi = NetIF_Add(&NetIF_API_WiFi, &err_net);NetDev_API_TemplateWiFiSpi, /* if (err_net != NET_IF_ERR_NONE) { Change following your Device driver API*/ return (DEF_FAIL); } &NetDev_BSP_WiFi, /* Change for your BSP API. */ /* ------------- START WIRELESS INTERFACE ------------- */ NetIF_Start(if_nbr_wifi, &err_net);&NetDev_Cfg_WiFi_1, /* Change for Device configuration. */* Makes the interface ready to receive and transmit. */ if (err_net != NET_IF_ERR_NONE) { return (DEF_FAIL);&NetWiFiMgr_API_Generic, } DEF_NULL, &err_net); if (err_net != /* ------------ SCAN FOR WIRELESS NETWORKS ------------ */ ctn = NetIF_WiFi_Scan(if_nbr_wifi, NET_IF_ERR_NONE) { return (DEF_FAIL); } ap, /* Access point table. ------ START WIRELESS INTERFACE ------ */ NetIF_Start(if_nbr_wifi, &err_net); /* Makes the IF ready */to RX and TX. */ if (err_net != NET_IF_ERR_NONE) { 10, return (DEF_FAIL); } /* Access point table size. /*/ ----- SCAN FOR WIRELESS NETWORKS ----- */ ctn = NetIF_WiFi_Scan(if_nbr_wifi, DEF_NULL, ap, /* Hidden SSID. /* Access point table. */ NET_IF_WIFI_CH_ALL10, /* Channel to scan. /* Access point table size. */ */ DEF_NULL, &err_net); if (err_net != NET_IF_WIFI_ERR_NONE) { /* return (DEF_FAIL);Hidden SSID. } */ NET_IF_WIFI_CH_ALL, /* --------- ANALYSE WIRELESS NETWORKS FOUND ---------- */Channel to scan. found = DEF_NO; for (i = 0; i < ctn -*/ 1; i++) { &err_net); /* Browse table of access point found.if (err_net != NET_IF_WIFI_ERR_NONE) { return (DEF_FAIL); } */ p_ssid = &ap[i].SSID; result = Str_Cmp_N((CPU_CHAR *)p_ssid, /* Search for a specific Wireless Network SSID. /* --- ANALYSE WIRELESS */NETWORKS FOUND -- */ found = DEF_NO; for (i = 0; i < ctn - 1; i++) { "Wifi_AP_SSID", /* ChangeBrowse fortable yourof WiFiaccess Networkpoint SSIDfound. */ p_ssid = &ap[i].SSID; */ result = Str_Cmp_N((CPU_CHAR *)p_ssid, /* Search for a specific WiFi Network SSID*/ NET_IF_WIFI_STR_LEN_MAX_SSID); if (result == 0) { found = DEF_YES;"Wifi_AP_SSID", /* Change for your WiFi break;Network SSID. */ } NET_IF_WIFI_STR_LEN_MAX_SSID); } if (foundresult == DEF_NO0) { return ( found = DEF_FAIL)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. */ */ 88); /* 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, 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 /* --------- CONFIGURECFG IPV4 STATIC ADDRESSESADDR ---------- */ /* For Dynamic IPv4 configurationcfg, µC/DHCPc is required */ /* Update IPv4 AddressesAddr following your network ... */ /* ... network requirements. */ */ NetASCII_Str_to_NetASCII_Str_to_IP("192.168.1.10", /* Convert Host IPv4 string addressaddr to 32 bit address. addr*/ &addr_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("255.255.255.0", /* Convert IPv4 maskMask string to 32 bit addressaddr. */ &msk_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("192.168.1.1", /* Convert Gateway string address to 32 bit addressaddr. */ &gateway_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetIPv4_CfgAddrAdd(if_nbr_wifi, /* Add a statically-configured IPv4 host address, ... */ addr_ipv4, /* ... host addr, subnet mask, & default gateway to... the */ ... */ msk_ipv4, /* ... interface.default gateway to the IF. */ 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) /* ----- CONFIGURESTART IPV6 STATICSTATELESS LINK LOCAL ADDRESS AUTO-CFG ----- */ NetIPv6_AddrAutoCfgHookSet(if_nbr_wifi, /* Set hook to received Auto-Cfg result. */ &App_AutoCfgResult, /* IPv6 Autoconfiguration is not yet supported. /* TODO update ptr to hook defined in App.*/ &err_net); cfg_result = NetIPv6_AddrAutoCfgEn(if_nbr_wifi, /* Enable and Start Auto-Cfg process. */ /* DHCPv6c is not yet available. */ DEF_YES, &err_net); if (cfg_result == DEF_FAIL) { /* Update IPv6 Address following your network ... return (DEF_FAIL); } #else */ /* --- CFG IPV6 STATIC LINK LOCAL ADDR -- */* ... requirements. */ /* DHCPv6c is not yet available. */ /* See Note #5. /* Update IPv6 Addr following your ... */ NetASCII_Str_to_IP("fe80::4444:1111", /* Convert IPv6 string address to 128 bit address. */ /* ... network requirements. */ NetASCII_Str_to_IP("fe80::4444:1111", /* Convert IPv6 string to 128 bit addr. */ &addr_ipv6, NET_IPv6_ADDR_SIZE, &err_net); &addr_ipv6,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_ADDR_SIZE,FLAG_DAD_EN); /* Enable DAD with Address Configuration. */ cfg_result &err_net);= NetIPv6_CfgAddrAdd(if_nbr_wifi, /* NetIPv6_CfgAddrAdd(if_nbr_wifi, Add a statically-configured IPv6 ... */ /* Add a statically-configured IPv6 host address to ... */ &addr_ipv6, /* ... host address to &addr_ipv6, the interface. */ /* ... the interface. 64, */ ipv6_flags, 64, &err_net); if (errcfg_netresult !== NET_IPv6_ERR_NONEDEF_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. |