已更新 2025年7月

初始化 TCP/IP 堆栈

FreeRTOS-Plus-TCP 网络教程节选

本页介绍了 FreeRTOS_IPInit_Multi() 以及发生“网络开启”和“网络关闭”事件时调用的回调函数。

在调用

FreeRTOS_IPInit_Multi()
函数之前,应用程序必须添加网络接口和 至少一个与网络接口对应的端点。网络接口是
EMAC + PHY
(或 WiFi 适配器)的驱动程序。端点是一组网络参数,包括 IP 地址、MAC 地址、 网关、DNS 等。可以有多个接口,每个接口可以拥有一个或多个端点。

用于初始化接口的函数是

px${port_name}_FillInterfaceDescriptor
(例如
pxSAM_FillInterfaceDescriptor
)。用于添加端点的函数是
FreeRTOS_FillEndPoint
FreeRTOS_FillEndPoint_IPv6

FreeRTOS_IPInit_Multi()
可创建 FreeRTOS-Plus-TCP RTOS 任务。FreeRTOS-Plus-TCP 任务可配置 并初始化网络接口。如果
ipconfigUSE_NETWORK_EVENT_HOOK
在 FreeRTOSIPConfig.h 中设置为 1, 则当网络可用时,TCP/IP 堆栈将调用
vApplicationIPNetworkEventHook_Multi()
回调函数。

下文提供了两个示例。第一个示例演示了

FreeRTOS_IPInit_Multi()
。第二个示例 演示了
vApplicationIPNetworkEventHook_Multi()

IPv4 示例:

1/* The MAC address array is not declared const as the MAC address will
2 normally be read from an EEPROM and not hard coded (in real deployed
3 applications).*/
4static uint8_t ucMACAddress[ 6 ] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
5
6/* Define the network addressing. These parameters will be used if either
7 ipconfigUDE_DHCP is 0 or if ipconfigUSE_DHCP is 1 but DHCP auto configuration
8 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 };
12
13/* The following is the address of an OpenDNS server. */
14static const uint8_t ucDNSServerAddress[ 4 ] = { 208, 67, 222, 222 };
15
16int main( void )
17{
18
19 /* Initialise the interface descriptor for WinPCap for example. */
20 pxWinPcap_FillInterfaceDescriptor( 0, &( xInterfaces[ 0 ] ) );
21
22 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 ) */
30
31 /* Initialise the RTOS's TCP/IP stack. The tasks that use the network
32 are created in the vApplicationIPNetworkEventHook() hook function
33 below. The hook function is called when the network connects. */
34 FreeRTOS_IPInit_Multi();
35
36 /*
37 * Other RTOS tasks can be created here.
38 */
39
40 /* Start the RTOS scheduler. */
41 vTaskStartScheduler();
42
43 /* If all is well, the scheduler will now be running, and the following
44 line will never be reached. If the following line does execute, then
45 there was insufficient FreeRTOS heap memory available for the idle and/or
46 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;
5
6 /* 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 already
10 been created. */
11 if( xTasksAlreadyCreated == pdFALSE )
12 {
13 /*
14 * For convenience, tasks that use FreeRTOS-Plus-TCP can be created here
15 * to ensure they are not created before the network is usable.
16 */
17
18 xTasksAlreadyCreated = pdTRUE;
19 }
20 }
21 /* Print out the network configuration, which may have come from a DHCP
22 * server. */
23 showEndPoint( pxEndPoint );
24}

vApplicationIPNetworkEventHook_Multi() 定义的示例用法

IPv6 示例:

1/* The MAC address array is not declared const as the MAC address will
2 normally be read from an EEPROM and not hard coded (in real deployed
3 applications).*/
4static uint8_t ucMACAddress[ 6 ] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
5
6int main( void )
7{
8 IPv6_Address_t xIPAddress;
9 IPv6_Address_t xPrefix;
10 IPv6_Address_t xGateWay;
11
12 /* Initialise the interface descriptor for WinPCap for example. */
13 pxWinPcap_FillInterfaceDescriptor( 0, &( xInterfaces[ 0 ] ) );
14
15 /* Add Endpoint - IPv6 needs minimum 2 EndPoints on the same network interface */
16
17 /*
18 * End-point-1 : public
19 * Network: 2001:470:ed44::/64
20 * IPv6 : 2001:470:ed44::4514:89d5:4589:8b79/128
21 * Gateway: fe80::ba27:ebff:fe5a:d751 // obtained from Router Advertisement
22 */
23 FreeRTOS_inet_pton6( "2001:470:ed44::", xPrefix.ucBytes );
24 FreeRTOS_CreateIPv6Address( &xIPAddress, &xPrefix, 64, pdTRUE );
25
26 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 }
38
39 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 );
51
52 /*
53 * End-point-2: private
54 * Network: fe80::/10 (link-local)
55 * IPv6 : fe80::d80e:95cc:3154:b76a/128
56 * Gateway: -
57 */
58
59 FreeRTOS_inet_pton6( "fe80::", xPrefix.ucBytes );
60 FreeRTOS_inet_pton6( "fe80::7009", xIPAddress.ucBytes );
61
62 #if ( ipconfigUSE_DHCPv6 != 0 )
63 {
64 /* End-point 2 wants to use DHCPv6. */
65 xEndPoints[ 1 ].bits.bWantDHCP = pdTRUE;
66 }
67
68 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 );
77
78 /* Initialise the RTOS's TCP/IP stack. The tasks that use the network
79 are created in the vApplicationIPNetworkEventHook() hook function
80 below. The hook function is called when the network connects. */
81 FreeRTOS_IPInit_Multi();
82
83 /*
84 * Other RTOS tasks can be created here.
85 */
86
87 /* Start the RTOS scheduler. */
88 vTaskStartScheduler();
89
90 /* If all is well, the scheduler will now be running, and the following
91 line will never be reached. If the following line does execute, then
92 there was insufficient FreeRTOS heap memory available for the idle and/or
93 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;
5
6 /* 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 already
10 been created. */
11 if( xTasksAlreadyCreated == pdFALSE )
12 {
13 /*
14 * For convenience, tasks that use FreeRTOS-Plus-TCP can be created here
15 * to ensure they are not created before the network is usable.
16 */
17
18 xTasksAlreadyCreated = pdTRUE;
19 }
20 }
21
22 /* Print out the network configuration, which may have come from a DHCP
23 * server. */
24 showEndPoint( pxEndPoint );
25}

vApplicationIPNetworkEventHook_Multi() 定义的示例用法

返回 RTOS TCP 网络教程索引