已更新 2025年7月
初始化 TCP/IP 堆栈
本页介绍了 FreeRTOS_IPInit_Multi() 以及发生“网络开启”和“网络关闭”事件时调用的回调函数。
在调用
FreeRTOS_IPInit_Multi()
EMAC + PHY
用于初始化接口的函数是
px${port_name}_FillInterfaceDescriptor
pxSAM_FillInterfaceDescriptor
FreeRTOS_FillEndPoint
FreeRTOS_FillEndPoint_IPv6
FreeRTOS_IPInit_Multi()
ipconfigUSE_NETWORK_EVENT_HOOK
vApplicationIPNetworkEventHook_Multi()
下文提供了两个示例。第一个示例演示了
FreeRTOS_IPInit_Multi()
vApplicationIPNetworkEventHook_Multi()
IPv4 示例:
1/* The MAC address array is not declared const as the MAC address will2 normally be read from an EEPROM and not hard coded (in real deployed3 applications).*/4static uint8_t ucMACAddress[ 6 ] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };56/* Define the network addressing. These parameters will be used if either7 ipconfigUDE_DHCP is 0 or if ipconfigUSE_DHCP is 1 but DHCP auto configuration8 failed. */9static const uint8_t ucIPAddress[ 4 ] = { 10, 10, 10, 200 };10static const uint8_t ucNetMask[ 4 ] = { 255, 0, 0, 0 };11static const uint8_t ucGatewayAddress[ 4 ] = { 10, 10, 10, 1 };1213/* The following is the address of an OpenDNS server. */14static const uint8_t ucDNSServerAddress[ 4 ] = { 208, 67, 222, 222 };1516int main( void )17{1819 /* Initialise the interface descriptor for WinPCap for example. */20 pxWinPcap_FillInterfaceDescriptor( 0, &( xInterfaces[ 0 ] ) );2122 FreeRTOS_FillEndPoint( &( xInterfaces[ 0 ] ), &( xEndPoints[ 0 ] ), ucIPAddress,23 ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );24 #if ( ipconfigUSE_DHCP != 0 )25 {26 /* End-point 0 wants to use DHCPv4. */27 xEndPoints[ 0 ].bits.bWantDHCP = pdTRUE;28 }29 #endif /* ( ipconfigUSE_DHCP != 0 ) */3031 /* Initialise the RTOS's TCP/IP stack. The tasks that use the network32 are created in the vApplicationIPNetworkEventHook() hook function33 below. The hook function is called when the network connects. */34 FreeRTOS_IPInit_Multi();3536 /*37 * Other RTOS tasks can be created here.38 */3940 /* Start the RTOS scheduler. */41 vTaskStartScheduler();4243 /* If all is well, the scheduler will now be running, and the following44 line will never be reached. If the following line does execute, then45 there was insufficient FreeRTOS heap memory available for the idle and/or46 timer tasks to be created. */47 for( ;; );48}
FreeRTOS_IPInit_Multi() API 函数的示例用法
1void vApplicationIPNetworkEventHook_Multi( eIPCallbackEvent_t eNetworkEvent,2 struct xNetworkEndPoint * pxEndPoint )3{4static BaseType_t xTasksAlreadyCreated = pdFALSE;56 /* Both eNetworkUp and eNetworkDown events can be processed here. */7 if( eNetworkEvent == eNetworkUp )8 {9 /* Create the tasks that use the TCP/IP stack if they have not already10 been created. */11 if( xTasksAlreadyCreated == pdFALSE )12 {13 /*14 * For convenience, tasks that use FreeRTOS-Plus-TCP can be created here15 * to ensure they are not created before the network is usable.16 */1718 xTasksAlreadyCreated = pdTRUE;19 }20 }21 /* Print out the network configuration, which may have come from a DHCP22 * server. */23 showEndPoint( pxEndPoint );24}
vApplicationIPNetworkEventHook_Multi() 定义的示例用法
IPv6 示例:
1/* The MAC address array is not declared const as the MAC address will2 normally be read from an EEPROM and not hard coded (in real deployed3 applications).*/4static uint8_t ucMACAddress[ 6 ] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };56int main( void )7{8 IPv6_Address_t xIPAddress;9 IPv6_Address_t xPrefix;10 IPv6_Address_t xGateWay;1112 /* Initialise the interface descriptor for WinPCap for example. */13 pxWinPcap_FillInterfaceDescriptor( 0, &( xInterfaces[ 0 ] ) );1415 /* Add Endpoint - IPv6 needs minimum 2 EndPoints on the same network interface */1617 /*18 * End-point-1 : public19 * Network: 2001:470:ed44::/6420 * IPv6 : 2001:470:ed44::4514:89d5:4589:8b79/12821 * Gateway: fe80::ba27:ebff:fe5a:d751 // obtained from Router Advertisement22 */23 FreeRTOS_inet_pton6( "2001:470:ed44::", xPrefix.ucBytes );24 FreeRTOS_CreateIPv6Address( &xIPAddress, &xPrefix, 64, pdTRUE );2526 FreeRTOS_inet_pton6( "fe80::ba27:ebff:fe5a:d751", xGateWay.ucBytes );27 #if ( ipconfigUSE_RA != 0 )28 {29 /* End-point 1 wants to use Router Advertisement */30 xEndPoints[ 0 ].bits.bWantRA = pdTRUE;31 }32 #endif /* #if( ipconfigUSE_RA != 0 ) */33 #if ( ipconfigUSE_DHCPv6 != 0 )34 {35 /* End-point 1 wants to use DHCPv6. */36 xEndPoints[ 0 ].bits.bWantDHCP = pdTRUE;37 }3839 FreeRTOS_FillEndPoint_IPv6( &( xInterfaces[ 0 ] ),40 &( xEndPoints[ 0] ),41 &( xIPAddress ),42 &( xPrefix ),43 64uL, /* Prefix length. */44 &( xGateWay ),45 NULL, /* pxDNSServerAddress: Not used yet. */46 ucMACAddress );47 FreeRTOS_inet_pton6( "2001:4860:4860::8888", xEndPoints[ 0 ].ipv6_settings.xDNSServerAddresses[ 0 ].ucBytes );48 FreeRTOS_inet_pton6( "fe80::1", xEndPoints[ 0 ].ipv6_settings.xDNSServerAddresses[ 1 ].ucBytes );49 FreeRTOS_inet_pton6( "2001:4860:4860::8888", xEndPoints[ 0 ].ipv6_defaults.xDNSServerAddresses[ 0 ].ucBytes );50 FreeRTOS_inet_pton6( "fe80::1", xEndPoints[ 0 ].ipv6_defaults.xDNSServerAddresses[ 1 ].ucBytes );5152 /*53 * End-point-2: private54 * Network: fe80::/10 (link-local)55 * IPv6 : fe80::d80e:95cc:3154:b76a/12856 * Gateway: -57 */5859 FreeRTOS_inet_pton6( "fe80::", xPrefix.ucBytes );60 FreeRTOS_inet_pton6( "fe80::7009", xIPAddress.ucBytes );6162 #if ( ipconfigUSE_DHCPv6 != 0 )63 {64 /* End-point 2 wants to use DHCPv6. */65 xEndPoints[ 1 ].bits.bWantDHCP = pdTRUE;66 }6768 FreeRTOS_FillEndPoint_IPv6(69 &( xInterfaces[ 0 ] ),70 &( xEndPoints[ 1 ] ),71 &( xIPAddress ),72 &( xPrefix ),73 10U, /* Prefix length. */74 NULL, /* No gateway */75 NULL, /* pxDNSServerAddress: Not used yet. */76 ucMACAddress );7778 /* Initialise the RTOS's TCP/IP stack. The tasks that use the network79 are created in the vApplicationIPNetworkEventHook() hook function80 below. The hook function is called when the network connects. */81 FreeRTOS_IPInit_Multi();8283 /*84 * Other RTOS tasks can be created here.85 */8687 /* Start the RTOS scheduler. */88 vTaskStartScheduler();8990 /* If all is well, the scheduler will now be running, and the following91 line will never be reached. If the following line does execute, then92 there was insufficient FreeRTOS heap memory available for the idle and/or93 timer tasks to be created. */94 for( ;; );95}
FreeRTOS_IPInit_Multi() API 函数的示例用法
1void vApplicationIPNetworkEventHook_Multi( eIPCallbackEvent_t eNetworkEvent,2 struct xNetworkEndPoint * pxEndPoint )3{4static BaseType_t xTasksAlreadyCreated = pdFALSE;56 /* Both eNetworkUp and eNetworkDown events can be processed here. */7 if( eNetworkEvent == eNetworkUp )8 {9 /* Create the tasks that use the TCP/IP stack if they have not already10 been created. */11 if( xTasksAlreadyCreated == pdFALSE )12 {13 /*14 * For convenience, tasks that use FreeRTOS-Plus-TCP can be created here15 * to ensure they are not created before the network is usable.16 */1718 xTasksAlreadyCreated = pdTRUE;19 }20 }2122 /* Print out the network configuration, which may have come from a DHCP23 * server. */24 showEndPoint( pxEndPoint );25}
vApplicationIPNetworkEventHook_Multi() 定义的示例用法