externstructnetif gnetif;
structdhcp *dhcp;
intmain(void){
/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* 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_LWIP_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */printf("lwip test\n");
/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1)
{
MX_LWIP_Process();
/* USER CODE END WHILE *//* USER CODE BEGIN 3 */
dhcp = netif_dhcp_data(&gnetif);
printf("Static IP address: %s\n", ip4addr_ntoa(&gnetif.ip_addr));
printf("Subnet mask: %s\n", ip4addr_ntoa(&gnetif.netmask));
printf("Default gateway: %s\n", ip4addr_ntoa(&gnetif.gw));
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
voidMX_LWIP_Init(void){
/* IP addresses initialization */
IP_ADDRESS[0] = 192;
IP_ADDRESS[1] = 168;
IP_ADDRESS[2] = 11;
IP_ADDRESS[3] = 196;
NETMASK_ADDRESS[0] = 255;
NETMASK_ADDRESS[1] = 255;
NETMASK_ADDRESS[2] = 255;
NETMASK_ADDRESS[3] = 0;
GATEWAY_ADDRESS[0] = 192;
GATEWAY_ADDRESS[1] = 168;
GATEWAY_ADDRESS[2] = 10;
GATEWAY_ADDRESS[3] = 1;
/* USER CODE BEGIN IP_ADDRESSES *//* USER CODE END IP_ADDRESSES *//* Initilialize the LwIP stack without RTOS */lwip_init();
/* IP addresses initialization without DHCP (IPv4) */IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
/* add the network interface (IPv4/IPv6) without RTOS */// 如果有多个接口则需多次调用// 需求供给一个init函数指针,这个指针指向咱们自己的硬件接口初始化函数,一般来说就是ethernetif.c中的ethernetif_init()netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/* Registers the default network interface */// 将网络接口设置为默许的网络接口netif_set_default(&gnetif);
// 查看是否有链接if (netif_is_link_up(&gnetif))
{
/* When the netif is fully configured this function must be called */// 使能网络接口 netif_set_up(&gnetif);
}
else
{
/* When the netif link is down this function must be called */// 关闭网络接口netif_set_down(&gnetif);
}
/* Set the link callback function, this function is called on change of link status*/netif_set_link_callback(&gnetif, ethernetif_update_config);
/* Create the Ethernet link handler thread *//* USER CODE BEGIN 3 *//* USER CODE END 3 */
}
/**
* Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the loopif is initialized
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/err_tethernetif_init(struct netif *netif){
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME/* Initialize interface hostname */
netif->hostname = "lwip";
#endif/* LWIP_NETIF_HOSTNAME */// 初始化name字段
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...) */#if LWIP_IPV4#if LWIP_ARP || LWIP_ETHERNET#if LWIP_ARP
netif->output = etharp_output;
#else/* The user should write its own code in low_level_output_arp_off function */
netif->output = low_level_output_arp_off;
#endif/* LWIP_ARP */#endif/* LWIP_ARP || LWIP_ETHERNET */#endif/* LWIP_IPV4 */#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif/* LWIP_IPV6 */
netif->linkoutput = low_level_output;
/* initialize the hardware */low_level_init(netif);
return ERR_OK;
}
11.2 MX_LWIP_Process
不断地接纳来自接口的信息,并检查是否延时
/**
* ----------------------------------------------------------------------
* Function given to help user to continue LwIP Initialization
* Up to user to complete or change this function ...
* Up to user to call this function in main.c in while (1) of main(void)
*-----------------------------------------------------------------------
* Read a received packet from the Ethernet buffers
* Send it to the lwIP stack for handling
* Handle timeouts if LWIP_TIMERS is set and without RTOS
* Handle the llink status if LWIP_NETIF_LINK_CALLBACK is set and without RTOS
*/voidMX_LWIP_Process(void){
/* USER CODE BEGIN 4_1 *//* USER CODE END 4_1 */ethernetif_input(&gnetif);
/* USER CODE BEGIN 4_2 *//* USER CODE END 4_2 *//* Handle timeouts */sys_check_timeouts();
/* USER CODE BEGIN 4_3 *//* USER CODE END 4_3 */
}
十二、注意事项
用户代码要加在 USER CODE BEGIN N 和 USER CODE END N 之间,否则下次运用 STM32CubeMX 重新生成代码后,会被删去。