这次用的是CubeIDE + CubeMX
要把NX_APP的mem分配的大一些,在app_azure_rtos.c中,我给的是40*1024,如果给的不够,会导致后面无法分配pool和thread等等
需要用到filex 要在CubeMX里面勾选上,还要用到http_server和dhcp
netxduo/addons/auto_ip at v6.1.11_rel · eclipse-threadx/netxduo · GitHub
选择自己对应版本的即可
在nx_stm32_eth_config.h里添加
#define NX_ETH_CABLE_CONNECTION_CHECK_PERIOD 600
在app_filex.c里删除多余的东西,就保留这样
app_netxduo.c
/* USER CODE BEGIN Header */
/********************************************************************************* @file app_netxduo.c* @author MCD Application Team* @brief NetXDuo applicative file******************************************************************************* @attention** Copyright (c) 2021 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "app_netxduo.h"
#include "nx_stm32_eth_config.h"
#include "usart.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "tx_api.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern void fx_stm32_sram_driver(FX_MEDIA *media_ptr);
VOID nx_stm32_eth_driver(NX_IP_DRIVER *driver_req_ptr);
static VOID App_Main_Thread_Entry(ULONG thread_input);
TX_SEMAPHORE Semaphore;
FX_MEDIA ram_disk;
UCHAR ram_disk_memory[32000];
NX_HTTP_SERVER my_server;
NX_PACKET_POOL server_pool;
NX_PACKET_POOL tx_pool;
TX_THREAD AppWebServerThread;
TX_THREAD AppMainThread;
TX_THREAD AppLinkThread;
NX_IP ip_0;
NX_DHCP DHCPClient;
ULONG IpAddress;
ULONG NetMask;
static uint8_t nx_server_pool[SERVER_POOL_SIZE];
uint32_t DataBuffer[512];
void nx_server_thread_entry(ULONG thread_input);
static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr);
UINT webserver_request_notify_callback(NX_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr);
static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr);
static VOID App_Link_Thread_Entry(ULONG thread_input);
/* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* App memory pointer. *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP *//* USER CODE END PFP */
/*** @brief Application NetXDuo Initialization.* @param memory_ptr: memory pointer* @retval int*/UINT MX_NetXDuo_Init(VOID *memory_ptr)
{UCHAR *pointer ;UINT ret = NX_SUCCESS;TX_BYTE_POOL *byte_pool = (TX_BYTE_POOL*)memory_ptr;/* USER CODE BEGIN MX_NetXDuo_MEM_POOL *//* Initialize the NetX system. */nx_system_initialize();ret = tx_byte_allocate(byte_pool, (VOID **) &pointer, NX_PACKET_POOL_SIZE, TX_NO_WAIT);if (ret != NX_SUCCESS){printf("Packed pool memory allocation failed : 0x%02x\n", ret);Error_Handler();}/* Create a packet pool. */ret = nx_packet_pool_create(&tx_pool,"NetX Main txPacket Pool",1536,pointer ,NX_PACKET_POOL_SIZE);if (ret != NX_SUCCESS){printf("Packed creation failed : 0x%02x\n", ret);Error_Handler();}ret = tx_byte_allocate(byte_pool, (VOID **) &pointer, SERVER_POOL_SIZE, TX_NO_WAIT);if (ret != NX_SUCCESS){printf("Packed pool memory allocation failed : 0x%02x\n", ret);Error_Handler();}ret = nx_packet_pool_create(&server_pool, "HTTP Server Packet Pool", SERVER_PACKET_SIZE,nx_server_pool, SERVER_POOL_SIZE);if (ret != NX_SUCCESS){printf("Server pool creation failed : 0x%02x\n", ret);Error_Handler();}// ip instanceret = tx_byte_allocate(byte_pool, (VOID **) &pointer, 2 * DEFAULT_MEMORY_SIZE, TX_NO_WAIT);if (ret != NX_SUCCESS){printf("NetX IP Instance memory allocation failed : 0x%02x\n", ret);Error_Handler();}/* Create an IP instance. */ret = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(192, 168,3, 36), 0xFFFFFF00UL, &tx_pool, nx_stm32_eth_driver,pointer, 2 * DEFAULT_MEMORY_SIZE, DEFAULT_PRIORITY);if (ret != NX_SUCCESS){printf("IP Instance creation failed : 0x%02x\n",ret);Error_Handler();}// arpret = tx_byte_allocate(byte_pool, (VOID **) &pointer, ARP_CACHE_SIZE, TX_NO_WAIT);if (ret != NX_SUCCESS){printf("ARP cache memory allocation failed : 0x%02x\n", ret);Error_Handler();}ret = nx_arp_enable(&ip_0, (void *) pointer, ARP_CACHE_SIZE);if (ret != NX_SUCCESS){printf("ARP Enable for IP error : 0x%02x\n", ret);Error_Handler();}ret = nx_icmp_enable(&ip_0);if (ret != NX_SUCCESS){printf("ICMP enable for IP error : 0x%02x\n", ret);Error_Handler();}ret = nx_udp_enable(&ip_0);if (ret != NX_SUCCESS){printf("UDP enable for IP error : 0x%02x\n", ret);Error_Handler();}ret = nx_tcp_enable(&ip_0);if (ret!=NX_SUCCESS){printf("TCP enable for IP error : 0x%02x\n", ret);Error_Handler();}ret = tx_byte_allocate(byte_pool, (VOID **) &pointer, SERVER_STACK, TX_NO_WAIT);if (ret != NX_SUCCESS){printf("Server stack memory allocation failed : 0x%02x\n", ret);Error_Handler();}ret = nx_http_server_create(&my_server, "My HTTP Server", &ip_0, &ram_disk,pointer, SERVER_STACK, &server_pool, NX_NULL, webserver_request_notify_callback);if (ret != NX_SUCCESS){printf("HTTP WEB Server creation failed: 0x%02x\n", ret);Error_Handler();}// web threadret = tx_byte_allocate(byte_pool, (VOID **) &pointer, 2 * DEFAULT_MEMORY_SIZE, TX_NO_WAIT);if (ret != TX_SUCCESS){printf("Main thread memory allocation failed : 0x%02x\n", ret);Error_Handler();}ret = tx_thread_create(&AppMainThread, "App Main thread", App_Main_Thread_Entry, 0, pointer, 2 * DEFAULT_MEMORY_SIZE,DEFAULT_MAIN_PRIORITY, DEFAULT_MAIN_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);if (ret != TX_SUCCESS){return NX_NOT_ENABLED;}/* Allocate the TCP server thread stack. */ret = tx_byte_allocate(byte_pool, (VOID **) &pointer, 2 * DEFAULT_MEMORY_SIZE, TX_NO_WAIT);/* Check server thread memory allocation. */if (ret != TX_SUCCESS){printf("Server thread memory allocation failed : 0x%02x\n", ret);Error_Handler();}ret = tx_thread_create(&AppWebServerThread, "App Web Server Thread", nx_server_thread_entry, 0, pointer, 2 * DEFAULT_MEMORY_SIZE,DEFAULT_PRIORITY, DEFAULT_PRIORITY, TX_NO_TIME_SLICE, TX_DONT_START);if (ret != TX_SUCCESS){return NX_NOT_ENABLED;}/* Allocate the memory for Link thread */if (tx_byte_allocate(byte_pool, (VOID **) &pointer,2 * DEFAULT_MEMORY_SIZE, TX_NO_WAIT) != TX_SUCCESS){return TX_POOL_ERROR;}/* create the Link thread */ret = tx_thread_create(&AppLinkThread, "App Link Thread", App_Link_Thread_Entry, 0, pointer, 2 * DEFAULT_MEMORY_SIZE,LINK_PRIORITY, LINK_PRIORITY, TX_NO_TIME_SLICE, TX_AUTO_START);if (ret != TX_SUCCESS){return NX_NOT_ENABLED;}/* Create the DHCP instance. */ret = nx_dhcp_create(&DHCPClient, &ip_0, "dhcp_client");if (ret != NX_SUCCESS){printf("DHCP Instance creation failed : 0x%02x\n", ret);}ret = nx_ip_address_change_notify(&ip_0, ip_address_change_notify_callback, NULL);if (ret != NX_SUCCESS){Error_Handler();}ret = tx_semaphore_create(&Semaphore, "App Semaphore", 0);if (ret != TX_SUCCESS){printf("Semaphore creation failed : 0x%02x\n", ret);Error_Handler();}
/* USER CODE END MX_NetXDuo_MEM_POOL *//* USER CODE BEGIN MX_NetXDuo_Init *//* USER CODE END MX_NetXDuo_Init */return ret;
}/* USER CODE BEGIN 1 */
void nx_server_thread_entry(ULONG thread_input)
{UINT status;tx_thread_sleep(NX_IP_PERIODIC_RATE);NX_PARAMETER_NOT_USED(thread_input);
// status = fx_media_open(&ram_disk, "STM32_SDIO_DISK", fx_stm32_sram_driver, (VOID *)SRAM2_BASE, DataBuffer, sizeof(DataBuffer));
// if (status != FX_SUCCESS)
// {
// /*Print Media Opening error. */
// printf("FX media opening failed : 0x%02x\n", status);
// /* Error, call error handler.*/
// Error_Handler();
// }
// tx_thread_sleep(NX_IP_PERIODIC_RATE);status = nx_http_server_start(&my_server);if (status != NX_SUCCESS){/* Print HTTP WEB Server starting error. */printf("HTTP WEB Server Starting Failed, error: 0x%02x\n", status);/* Error, call error handler.*/Error_Handler();}tx_thread_sleep(NX_IP_PERIODIC_RATE);}UINT webserver_request_notify_callback(NX_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr)
{CHAR temp_string[30] = {'\0'};CHAR data[512] = {'\0'};UINT string_length;NX_PACKET *resp_packet_ptr;UINT status;NX_PARAMETER_NOT_USED(server_ptr);NX_PARAMETER_NOT_USED(request_type);NX_PARAMETER_NOT_USED(packet_ptr);if (strcmp(resource, "/GetTXData") == 0){/* Let HTTP server know the response has been sent. */sprintf(data, "%lu.%lu.%lu.%lu,%u", (IpAddress >> 24) & 0xff, (IpAddress >> 16) & 0xff, (IpAddress >> 8) & 0xff, IpAddress& 0xff, CONNECTION_PORT);}else{return NX_SUCCESS;}nx_http_server_type_get(server_ptr, server_ptr -> nx_http_server_request_resource, temp_string);temp_string[string_length] = '\0';status = nx_http_server_callback_generate_response_header(server_ptr, &resp_packet_ptr, NX_HTTP_STATUS_OK,strlen(data), temp_string, NX_NULL);status = nx_packet_data_append(resp_packet_ptr, data, strlen(data), server_ptr->nx_http_server_packet_pool_ptr, NX_WAIT_FOREVER);status = nx_http_server_callback_packet_send(server_ptr, resp_packet_ptr);if (status != NX_SUCCESS){nx_packet_release(resp_packet_ptr);return status;}return(NX_HTTP_CALLBACK_COMPLETED);
}static VOID ip_address_change_notify_callback(NX_IP *ip_instance, VOID *ptr)
{/* as soon as the IP address is ready, the semaphore is released to let the web server start */tx_semaphore_put(&Semaphore);
}static VOID App_Main_Thread_Entry(ULONG thread_input)
{UINT ret;ret = nx_ip_address_change_notify(&ip_0, ip_address_change_notify_callback, NULL);if (ret != NX_SUCCESS){Error_Handler();}ret = nx_dhcp_start(&DHCPClient);if (ret != NX_SUCCESS){Error_Handler();}/* wait until an IP address is ready */
// if(tx_semaphore_get(&Semaphore, TX_WAIT_FOREVER) != TX_SUCCESS)
// {
// Error_Handler();
// }/* get IP address */ret = nx_ip_address_get(&ip_0, &IpAddress, &NetMask);if (ret != TX_SUCCESS){Error_Handler();}/* the network is correctly initialized, start the nx_server_thread_entry */tx_thread_resume(&AppWebServerThread);/* this thread is not needed any more, we relinquish it */tx_thread_relinquish();return;
}static VOID App_Link_Thread_Entry(ULONG thread_input)
{ULONG actual_status;UINT linkdown = 0, status;while(1){/* Get Physical Link stackavailtus. */status = nx_ip_interface_status_check(&ip_0, 0, NX_IP_LINK_ENABLED,&actual_status, 10);if(status == NX_SUCCESS){if(linkdown == 1){linkdown = 0;status = nx_ip_interface_status_check(&ip_0, 0, NX_IP_ADDRESS_RESOLVED,&actual_status, 10);if(status == NX_SUCCESS){/* The network cable is connected again. */printf("The network cable is connected again.\n");/* Print Webserver Client is available again. */printf("Webserver Client is available again.\n");}else{/* The network cable is connected. */printf("The network cable is connected.\n");/* Send command to Enable Nx driver. */nx_ip_driver_direct_command(&ip_0, NX_LINK_ENABLE,&actual_status);/* Restart DHCP Client. */nx_dhcp_stop(&DHCPClient);nx_dhcp_start(&DHCPClient);}}}else{if(0 == linkdown){linkdown = 1;/* The network cable is not connected. */printf("The network cable is not connected.\n");}}tx_thread_sleep(NX_ETH_CABLE_CONNECTION_CHECK_PERIOD);}
}
/* USER CODE END 1 */
app_netxduo.h
/* USER CODE BEGIN Header */
/********************************************************************************* @file app_netxduo.h* @author MCD Application Team* @brief NetXDuo applicative header file******************************************************************************* @attention** Copyright (c) 2020-2021 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __APP_NETXDUO_H__
#define __APP_NETXDUO_H__#ifdef __cplusplus
extern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "nx_api.h"/* Private includes ----------------------------------------------------------*/
#include "nx_stm32_eth_driver.h"/* USER CODE BEGIN Includes */
#include "app_filex.h"
#include "tx_thread.h"
#include "main.h"
#include "nxd_http_server.h"
#include "tx_thread.h"
#include "nxd_dhcp_client.h"
/* USER CODE END Includes *//* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
#define DEFAULT_MEMORY_SIZE 1024
#define SERVER_PACKET_SIZE (NX_HTTP_SERVER_MIN_PACKET_SIZE * 2)
#define NX_PACKET_POOL_SIZE ((1536 + sizeof(NX_PACKET)) * 8)
#define SERVER_POOL_SIZE (SERVER_PACKET_SIZE * 4)
#define ARP_CACHE_SIZE 1024
#define SERVER_STACK 4096
#define DEFAULT_MAIN_PRIORITY 10
#define DEFAULT_PRIORITY 5
#define LINK_PRIORITY 11
#define CONNECTION_PORT 80
/* USER CODE END ET *//* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC *//* USER CODE END EC *//* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM *//* USER CODE END EM *//* Exported functions prototypes ---------------------------------------------*/
UINT MX_NetXDuo_Init(VOID *memory_ptr);/* USER CODE BEGIN EFP *//* USER CODE END EFP *//* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* USER CODE BEGIN 1 */
/* USER CODE END 1 */#ifdef __cplusplus
}
#endif
#endif /* __APP_NETXDUO_H__ */
在这里设置导航,我写了一个/GetTXData,让它返回当前IP地址,大家自己实现自己的东西就可以。