STM32H7通过CUBEMX初始化移植LWIP,DHCP建立RAW TCP服务器,不停发成功
在H743和H723测试成功。
1、针对H743的MPU配置
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x30040000;
MPU_InitStruct.Size = MPU_REGION_SIZE_256B;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;// MPU_ACCESS_SHAREABLE
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER1;
MPU_InitStruct.BaseAddress = 0x30044000;
MPU_InitStruct.Size = MPU_REGION_SIZE_16KB;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
TX Descriptor length 4 |
First TX Descriptor address 0x3004 0080 |
RX Descriptor length 4 |
First RX Descriptor address 0x3004 0000 |
Rx Buffers Address 0x3004 0100 |
Rx Buffers length 1536 |
LWIP RAM HEAP POINTER (RAM Heap Pointer) 0x3004 4000 |
MEM SIZE (Heap Memory Size) 14336 Byte(s) |
#define TCP_MSS 1460
2、针对H723的初始化配置
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER1;
MPU_InitStruct.BaseAddress = 0x30000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_256B;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
//MPU_ACCESS_NOT_BUFFERABLE
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x30004000;
MPU_InitStruct.Size = MPU_REGION_SIZE_16KB;
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
TX Descriptor length 4 |
First TX Descriptor address 0x3000 0080 |
RX Descriptor length 4 |
First RX Descriptor address 0x3000 0000 |
Rx Buffers Address 0x3000 0100 |
Rx Buffers length 1536 |
LWIP RAM HEAP POINTER (RAM Heap Pointer) 0x3000 4000 |
MEM SIZE (Heap Memory Size) 14336 Byte(s) |
#define TCP_MSS 1460
3、实现过程
中间走了很多弯路,确实不容易,看了正点原子的思路给了启发。
功能实现步骤:先初始化内核,查看网线连接状态,接着DHCP获取IP地址,接着建立pcb,绑定,监听。对客户端(即上位机)发送数据。
3.1正点原子的代码逻辑
lwip_comm_init()//lwIP初始化(lwIP启动的时候使用)
包含以下函数:
{
ethernet_init(); /* 初始化以太网IO */
lwip_init(); /* 初始化LWIP内核 */
netif_set_default(&g_lwip_netif); /* 设置netif为默认网口 */
lwip_link_status_updated(&g_lwip_netif); /* DHCP链接状态更新函数 */
netif_set_link_callback(&g_lwip_netif, lwip_link_status_updated); //DHCP链接状态更新的回调函数
#if LWIP_DHCP /* 如果使用DHCP的话 */
g_lwipdev.dhcpstatus = 0; /* DHCP状态标记为0 */
}
dhcp_supplied_address(netif)) //获取IP地址后,
g_lwipdev.dhcpstatus = 2; /* DHCP状态标记为2 */
开始循环调用,只要DHCP是连接上的状态
while{
lwip_pkt_handle();包含一个函数 ethernetif_input(&g_lwip_netif);
/* 从网络缓冲区中读取接收到的数据包并将其发送给LWIP处理 */
Ethernet_Link_Periodic_Handle()包含一个函数
sys_check_timeouts();
周期性检测网线连接状态
}
接着lwip_demo(void)
包含下面函数
struct tcp_pcb *tcppcbnew; /* 定义一个TCP服务器控制块 */
struct tcp_pcb *tcppcbconn; /* 定义一个TCP服务器控制块 */
tcppcbnew = tcp_new(); /* 创建一个新的pcb */
if (tcppcbnew) /* 创建成功 */
{
err = tcp_bind(tcppcbnew, IP_ADDR_ANY, LWIP_DEMO_PORT); /* 将本地IP与指定的端口号绑定在一起,IP_ADDR_ANY为绑定本地所有的IP地址 */
if (err == ERR_OK) /* 绑定完成 */
{
tcppcbconn = tcp_listen(tcppcbnew); /* 设置tcppcb进入监听状态 */
tcp_accept(tcppcbconn, lwip_tcp_server_accept); /* 初始化LWIP的tcp_accept的回调函数 */
}
else res = 1;
}
else res = 1;
g_point_color = BLUE;
while (res == 0)
3.2对比下stm32cubemx生成的代码。
MX_LWIP_Init(void)
{
lwip_init(); //lwip内核初始化
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);//添加网卡
netif_set_default(&gnetif); //设置默认网卡
netif_set_up(&gnetif);//启动一个网络接口,并使其可以处理网络数据包。
netif_set_link_callback(&gnetif, ethernet_link_status_updated);//网卡连接回调函数
dhcp_start(&gnetif);
}
这里我发现回调函数更新IP地址很慢,所以添加一个while循环。用来判定IP地址更新
{
//ethernetif_input(&gnetif);
//sys_check_timeouts();
MX_LWIP_Process();
}
最后看下,MX_LWIP_Process包含
void MX_LWIP_Process(void)
{
ethernetif_input(&gnetif); //接收数据
/* Handle timeouts */
sys_check_timeouts();//time表示当前定时事件成为链表第一个节点后,它还需要等待多久(毫秒)才会被执行
Ethernet_Link_Periodic_Handle(&gnetif);//周期性检查网线连接状态
}
4、完整代码
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 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 "main.h"
#include "lwip.h"
#include "memorymap.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "lwip/tcp.h"
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint32_t num;
uint8_t newipaddress[4];
err_t status_server, status_new;
/* USER CODE END PV */
char rxbuff[2048];
uint32_t address = 0;
uint32_t rx_flag = 0;
uint32_t count = 0;
char sendbuffer[] = "20250106hel20250106hello20250106hello20250106hello20250106hello\r\n";
//char sendbuffer[] = "20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello\r\n";
//char sendbuffer[] = "20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello\r\n";
//char sendbuffer[] = "20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello\r\n";
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
extern struct netif gnetif;
struct tcp_pcb *tcp_server_pcb; /* 定义一个TCP服务器控制块 */
struct tcp_pcb *tcppcbnew; /* 定义一个TCP服务器控制块 */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
int new_flag = 1;
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int c, FILE *stream) //重写fputc函数
{
/*
huart1是工具生成代码定义的UART1结构体,
如果以后要使用其他串口打印,只需要把这个结构体改成其他UART结构体。
*/
HAL_UART_Transmit(&huart1, (unsigned char *)&c, 1, 1000);
return 1;
}
/* USER CODE END PTD */
static err_t TCPServerCallback(void *arg, struct tcp_pcb *pcb, struct pbuf *tcp_recv_pbuf, err_t err)
{
struct pbuf *tcp_send_pbuf;
// char echoString[]="This is the client content echo:\r\n";
char echoString[] = "9999\r\n";
rx_flag = 1;
if (tcp_recv_pbuf != NULL)
{
/* 更新接收窗口 */
tcp_recved(pcb, tcp_recv_pbuf->tot_len);
//
// /* 将接收的数据拷贝给发送结构体 */
tcp_send_pbuf = tcp_recv_pbuf;
// address=&rxbuff;
// memcpy(rxbuff, tcp_recv_pbuf->payload, 100);
// status = tcp_write(pcb, echoString, strlen(echoString), 1);
// count = tcp_sndbuf(pcb); //查询剩余buf大小
// printf("before payload buffer is %d\r\n", count);
// printf("before payload status is %d\r\n", status);
// printf(" payload length is %d\r\n", tcp_send_pbuf->len);
/* 将接收到的数据再转发出去 */
status_server = tcp_write(pcb, tcp_send_pbuf->payload, tcp_send_pbuf->len, 1);
//
// count = tcp_sndbuf(pcb); //查询剩余buf大小
// printf("after payload buffer is %d\r\n", status);
rx_flag = 1;
printf("after payload buffer is %d\r\n", count);
pbuf_free(tcp_recv_pbuf);
// tcp_close(pcb);
}
// else if (err == ERR_OK)
// {
// return tcp_close(pcb);
// }
return ERR_OK;
}
static err_t TCPServerAccept(void *arg, struct tcp_pcb *pcb, err_t err)
{
/* 确认监听与连接 */
// tcp_arg(pcb, mem_calloc(sizeof(struct name), 1));
// /* 发送一个建立连接的字符串 */
// tcp_write(pcb, "hello my dream \n\r",strlen("hello my dream \n\r ")-2,1);
// /* 配置接收回调函数 */
// tcp_recv(pcb, tcp_server_recv);
tcp_recv(pcb, TCPServerCallback);
return ERR_OK;
}
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* Enable the CPU Cache */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
HAL_Delay(50);
/* USER CODE END 2 */
MX_LWIP_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (gnetif.ip_addr.addr == 0)
{
// ethernetif_input(&gnetif);
// sys_check_timeouts();
MX_LWIP_Process();
}
*(uint32_t *)newipaddress = (gnetif.ip_addr.addr);
if (new_flag)
{
new_flag = 0;
tcppcbnew = tcp_new();
err_t res = tcp_bind(tcppcbnew, IP_ADDR_ANY, 8080);
printf("743 ipaddress is %d.%d.%d.%d\r\n", newipaddress[0], newipaddress[1], newipaddress[2], newipaddress[3]);
printf(" tcp_bind res is %d\r\n", res);
/* 监听之前创建的结构体tcp_server_pcb */
tcp_server_pcb = tcp_listen(tcppcbnew);
// /* 初始化结构体接收回调函数 */
tcp_accept(tcp_server_pcb, TCPServerAccept);
}
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
MX_LWIP_Process();
// num++;
// HAL_Delay(1);
//
// if(num% 100000 ==0)
// {
// printf("ipaddress is %d.%d.%d.%d\r\n", newipaddress[0], newipaddress[1], newipaddress[2], newipaddress[3]);
// }
// count = tcp_sndbuf(tcppcbnew); //查询剩余buf大小
// HAL_Delay(2500);
// printf("before payload buffer is %d\r\n", count);
// if ((count / TCP_MSS) > 1)
// {
tcp_write(tcppcbnew, sendbuffer, strlen(sendbuffer), 1);
tcp_output(tcppcbnew);
count = tcp_sndbuf(tcppcbnew); //查询剩余buf大小
printf("after payload buffer is %d\r\n", count);
HAL_Delay(2500);
// }
// else
// {
HAL_Delay(1);
// tcp_write(tcppcbnew, sendbuffer, strlen(sendbuffer), 1);
// tcp_output(tcppcbnew);
// count = tcp_sndbuf(tcppcbnew); //查询剩余buf大小
// printf("after payload buffer is %d\r\n", count);
// HAL_Delay(2500);
// }
// printf("after payload buffer is %d\r\n", count);
// HAL_Delay(1000);
// if(rx_flag==1)
// {
// status_new= tcp_write(tcppcbnew, sendbuffer, strlen(sendbuffer), 1);
// printf("tcppcbnew status is %d\r\n",status_new);
// rx_flag=0;
//
// }
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 120;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2
| RCC_CLOCKTYPE_D3PCLK1 | RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_D2SRAM1_CLK_ENABLE();
__HAL_RCC_D2SRAM2_CLK_ENABLE();
__HAL_RCC_D2SRAM3_CLK_ENABLE();
__HAL_RCC_BKPRAM_CLKAM_ENABLE();
__HAL_RCC_D3SRAM1_CLKAM_ENABLE();
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/* MPU Configuration */
void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x30040000;
MPU_InitStruct.Size = MPU_REGION_SIZE_256B;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;// MPU_ACCESS_SHAREABLE
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Number = MPU_REGION_NUMBER1;
MPU_InitStruct.BaseAddress = 0x30044000;
MPU_InitStruct.Size = MPU_REGION_SIZE_16KB;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
LWIP.c中
还有,在lwip.c 网络连接状态更新回调函数里修改。
static void ethernet_link_status_updated(struct netif *netif)
{
printf("\r\nenter netif_status_updated func\r\n");
HAL_Delay(1000);
if (netif_is_up(netif))
{
printf("link available\r\n");
printf("IP:%hhu.%hhu.%hhu.%hhu\r\n",ip4_addr1_val(netif->ip_addr),ip4_addr2_val(netif->ip_addr),
ip4_addr3_val(netif->ip_addr),ip4_addr4_val(netif->ip_addr));
printf("NM:%hhu.%hhu.%hhu.%hhu\r\n",ip4_addr1_val(netif->netmask),ip4_addr2_val(netif->netmask),
ip4_addr3_val(netif->netmask),ip4_addr4_val(netif->netmask));
printf("GW:%hhu.%hhu.%hhu.%hhu\r\n",ip4_addr1_val(netif->gw),ip4_addr2_val(netif->gw),
ip4_addr3_val(netif->gw),ip4_addr4_val(netif->gw));
}
else
{
printf("link unavailable\r\n");
}
}
5、LWIP实战验证测试
5.1使用正点原子的网络助手测试
第一次测试
修改TCP_SND_QUEUELEN 9
#define TCP_SND_BUF (4* TCP_MSS) //(2 * TCP_MSS)
TCP_MSS 536
①、Sendbuf
20250106hello
1分钟发送453300B
速率7.5KB/S
②、Sendbuf
20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello
1分钟发送1162 000B
速率19KB/S
③、Sendbuf
20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello
1分钟发送366 788
速率6.1KB/S
第二次测试
修改TCP_SND_QUEUELEN 12 //9
#define TCP_SND_BUF (6 * TCP_MSS) //(2 * TCP_MSS)
TCP_MSS 536
①、Sendbuf
20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello
1分钟发送97 3824
速率16KB/S
第三次测试
修改TCP_SND_QUEUELEN 6//9
#define TCP_SND_BUF (3 * TCP_MSS) //(2 * TCP_MSS)
测试20s发送302 924
速率15KB/S
增加
tcp_output(tcppcbnew);
#define TCP_MSS 1460 //536
#define TCP_WND_UPDATE_THRESHOLD 1460//536
#if TCP_MSS > 536
#define INITIAL_MSS TCP_MSS //536
发送1分钟
15 739 425,速率达到0.25MB/s
不显示接收数据,再测试能到3Mb/s
5.2然后用netassist测试。
①char sendbuffer[] = "20250106hel20250106hello20250106hello20250106hello20250106hello\r\n";
测试时间:1分钟
发送数据:143 631 765 B
对应的网速为:19.150902 Mb/s
②修改
char sendbuffer[] = "20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello\r\n";
测试时间:1分钟
发送数据:143 658 285
对应的网速为:19.154438 Mb/s
③修改测试时间。
char sendbuffer[] = "20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello20250106hello\r\n";
测试时间:100秒
发送数据:325 879 580
对应的网速为:26.070366 Mb/s(超过25Mb/s)
对应的以太网帧数目:218867
每个帧长:1489
5.3用电脑测试
后来知道,用电脑也能测试网速,
打开任务管理器——性能——以太网
看到发送速度能达到34Mbps。
5.4对H723再次测试
发送速度能达到38Mbps。
原文地址:https://blog.csdn.net/weixin_42434684/article/details/145156234
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!