- 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 SSID. found. */ p_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= &ap[i].SSID; result = Str_Cmp_N((CPU_CHAR *)&p_ssid, /* Search for a specific WiFi Network SSID*/ "Wifi_AP_SSID", "Wifi_AP_SSID", /* Change for your WiFi Network SSID. */ */ NET_IF_WIFI_STR_LEN_MAX_SSID); if (result == 120); { found = DEF_YES; break; /* SSID string length. } } if (found == DEF_NO) { return (DEF_FAIL); } */ Str_Copy_N((CPU_CHAR *)&psk, "Password", /* Change for your WiFi Network Password.------ JOIN A WIRELESS NETWORK ------- */ Mem_Clr(&ssid, sizeof(ssid)); Mem_Clr(&psk, sizeof(psk)); */ Str_Copy_N((CPU_CHAR *)&ssid, 8); "Wifi_AP_SSID", /* Change for your WiFi Network SSID. */* PSK string length. 12); */ NetIF_WiFi_Join(if_nbr_wifi, /* SSID string length. /* See Note #9. */ Str_Copy_N((CPU_CHAR *)&psk, "Password", */ ap[i].NetType, /* Change for your WiFi Network Password. */ /* See Note #9a. 8); /* PSK string length. */ */ NETNetIF_IFWiFi_WIFIJoin(if_DATAnbr_RATE_AUTOwifi, /* See Note #9b.ap[i].NetType, /* WiFi Network type. */ */ ap[i].SecurityType, NET_IF_WIFI_DATA_RATE_AUTO, /* SeeData Note #9c. rate. */ NET_IF_WIFI_PWR_LEVEL_HIap[i].SecurityType, /* SeeWiFi Network Notesecurity #9dtype. */ NET_IF_WIFI_PWR_LEVEL_HI, */ /* Power level. ssid, */ /*ssid, See Note #9e. /* WiFi Network SSID. */ */ psk, psk, /* See Note #9f. /* WiFi Network PSK. */ &err_net); if (err_net != NET_IF_WIFI_ERR_NONE) { return (DEF_FAIL); } #ifdef NET_IPv4_MODULE_EN /* -------- CFG IPV4 STATIC ADDR -------- */ /* --------- CONFIGURE IPV4 STATIC ADDRESSES ---------- For Dynamic IPv4 cfg, DHCPc is required*/ /* Update IPv4 Addr following your ... */* For Dynamic IPv4 configuration, µC/DHCPc is required */ /* ... network requirements. /* Update IPv4 Addresses following your*/ network ... NetASCII_Str_to_IP("192.168.1.10", */ /* Convert IPv4 string addr to 32 bit addr*/ &addr_ipv4, /* ... requirements. NET_IPv4_ADDR_SIZE, */&err_net); NetASCII_Str_to_IP("192255.168255.1255.100", /* Convert HostMask IPv4 string address to 32 bit addressaddr. */ &addrmsk_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCII_Str_to_IP("255192.255168.2551.01", /* Convert IPv4 mask string to 32 bit address. /* Convert Gateway string to 32 bit addr. */ &mskgateway_ipv4, NET_IPv4_ADDR_SIZE, &err_net); NetASCIINetIPv4_StrCfgAddrAdd(if_to_IP("192.168.1.1"nbr_wifi, /* ConvertAdd Gatewaya stringstatically-configured address to 32 bit addressIPv4 ... */ &gatewayaddr_ipv4, NET_IPv4_ADDR_SIZE, /* ... host addr, subnet &err_net);mask, & NetIPv4_CfgAddrAdd(if_nbr_wifi,... */ msk_ipv4, /* Add a statically-configured IPv4 host address, ... */ /* ... default gateway to the IF. addr_ipv4, */ gateway_ipv4, /* ... subnet mask, & default gateway to the ... */ &err_net); if (err_net != NET_IPv4_ERR_NONE) { return msk_ipv4,(DEF_FAIL); } #endif #ifdef NET_IPv6_MODULE_EN #if (NET_IPv6_CFG_ADDR_AUTO_CFG_EN == DEF_ENABLED) /* ... interface. /*/ ---- START IPV6 STATELESS AUTO-CFG --- */ NetIPv6_AddrAutoCfgHookSet(if_nbr_wifi, gateway_ipv4, /* Set hook to received Auto-Cfg result. */ &err_net); if (err_net != NET_IPv4_ERR_NONE) { return (DEF_FAIL); &App_AutoCfgResult, } #endif #ifdef NET_IPv6_MODULE_EN /* TODO update ptr to hook defined in App.*/ &err_net); cfg_result = NetIPv6_AddrAutoCfgEn(if_nbr_wifi, /* ----- CONFIGURE IPV6 STATIC LINK LOCAL ADDRESS ----- */ Enable and Start Auto-Cfg process. */ DEF_YES, /* IPv6 Autoconfiguration is not yet supported. &err_net); */ if (cfg_result == DEF_FAIL) { return (DEF_FAIL); } #else /* DHCPv6c is not yet available. /* --- CFG IPV6 STATIC */LINK LOCAL ADDR -- */ /* UpdateDHCPv6c is IPv6not Addressyet followingavailable. your network ... */ /* Update IPv6 Addr following /*your ... requirements. */ */ /* ... network requirements. */ NetASCII_Str_to_IP("fe80::4444:1111", /* See Note #5. /* Convert IPv6 string to 128 bit addr. */ */ NetASCII_Str_to_IP("fe80::4444:1111"&addr_ipv6, /* Convert IPv6 string address to 128 bit address.NET_IPv6_ADDR_SIZE, */ &err_net); ipv6_flags = &addr_ipv6,0; DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_BLOCK_EN); /* Set Address Configuration as blocking. */ DEF_BIT_SET(ipv6_flags, NET_IPv6_FLAG_ADDR_SIZE,DAD_EN); /* Enable DAD with Address Configuration. */ &err_net); cfg_result = NetIPv6_CfgAddrAdd(if_nbr_wifi, /* Add a statically-configured IPv6 ... */ /* Add a statically-configured IPv6 host address to ... */ &addr_ipv6, /* ... host address to the interface. */ /* ... the interface. 64, */ 64,ipv6_flags, &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. |