下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

最新资讯
FreeRTOS-Plus-TCP 现具有统一的 IPv4 和 IPv6 功能,支持多接口。
为基于 FreeRTOS 的固件实现防砖化 MCU FOTA:
宣布停止支持 FreeRTOS 202012 LTS。
FreeRTOS 网站现已提供简体中文版本
新的 FreeRTOS Long Term Support 版本现已发布。
注意: 自 FreeRTOS V4.0.0 起,这些堆栈初始化 API 已被弃用 。请参阅 初始化 TCP/IP 堆栈 了解支持 IPv6、多个端点和多个接口的新 API。如需使用已弃用的 API ,请将 FreeRTOSIPConfig.h 头文件中的 ipconfigIPv4_BACKWARD_COMPATIBLE 设置为 1。

初始化 TCP/IP 堆栈
FreeRTOS-Plus-TCP 联网教程节选

此页详细描述了 FreeRTOS_IPInit() 以及当“网络启用”和“网络关闭”事件发生时被调用的回调函数

FreeRTOS_IPInit() 必须是第一个被调用的 FreeRTOS-Plus-TCP 函数。 FreeRTOS_IPInit() 可在 RTOS 调度器启动之前或之后调用。

FreeRTOS_IPInit () 会创建 FreeRTOS-Plus-TCP RTOS 任务。 FreeRTOS-Plus-TCP 任务 会配置和初始化网络接口。 如果 ipconfigUSE_NETWORK_EVENT_HOOK 在 FreeRTOSIPConfig.h 中设置为 1, 当网络准备就绪时,TCP/IP 堆栈 vIPNetworkEventHook() 将调用回调函数。

以下为两个示例。 第一个示例演示了 FreeRTOS_IPInit()。 第二个示例演示了 vIPNetworkEventHook()。



/* The MAC address array is not declared const as the MAC address will
normally be read from an EEPROM and not hard coded (in real deployed
applications).*/

static uint8_t ucMACAddress[ 6 ] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };

/* Define the network addressing. These parameters will be used if either
ipconfigUDE_DHCP is 0 or if ipconfigUSE_DHCP is 1 but DHCP auto configuration
failed. */

static const uint8_t ucIPAddress[ 4 ] = { 10, 10, 10, 200 };
static const uint8_t ucNetMask[ 4 ] = { 255, 0, 0, 0 };
static const uint8_t ucGatewayAddress[ 4 ] = { 10, 10, 10, 1 };

/* The following is the address of an OpenDNS server. */
static const uint8_t ucDNSServerAddress[ 4 ] = { 208, 67, 222, 222 };

int main( void )
{
/* Initialise the RTOS's TCP/IP stack. The tasks that use the network
are created in the vApplicationIPNetworkEventHook() hook function
below. The hook function is called when the network connects. */

FreeRTOS_IPInit( ucIPAddress,
ucNetMask,
ucGatewayAddress,
ucDNSServerAddress,
ucMACAddress );

/*
* Other RTOS tasks can be created here.
*/


/* Start the RTOS scheduler. */
vTaskStartScheduler();

/* If all is well, the scheduler will now be running, and the following
line will never be reached. If the following line does execute, then
there was insufficient FreeRTOS heap memory available for the idle and/or
timer tasks to be created. */

for( ;; );
}

Example use of the FreeRTOS_IPInit() API function




void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
{
static BaseType_t xTasksAlreadyCreated = pdFALSE;

/* Both eNetworkUp and eNetworkDown events can be processed here. */
if( eNetworkEvent == eNetworkUp )
{
/* Create the tasks that use the TCP/IP stack if they have not already
been created. */

if( xTasksAlreadyCreated == pdFALSE )
{
/*
* For convenience, tasks that use FreeRTOS-Plus-TCP can be created here
* to ensure they are not created before the network is usable.
*/


xTasksAlreadyCreated = pdTRUE;
}
}
}

Example vApplicationIPNetworkEventHook() definition


<< 返回 RTOS TCP 网络教程索引

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.