手把手教你创建 LWIP 模板例程
开发环境:CubeMX5.6.1(packages--F4--1.25.0)、Keil5(5.30.0.0--Keil.STM32F4xx_DFP.2.9.0)
硬件开发板平台:野火STM32F429IG-V2
第1步:打开Cube MX软件
第2步:新建工程
第3步:选择自己使用的芯片并开始工程配置
第4步:开启仿真接口
第5步:选择外部高速时钟HSE
第6步:配置USART串行通讯(为方便后续调试和使用打印函数printf();)
第7步:配置 RMII 以太网 ETH(自动获取IP)
第7步:开启ETH中断
第7步:根据自己的开发板选择引脚
第8步:使能并配置 LWIP
第9步:设置工程名、路径、编译环境、堆栈
第10步:设置为单独的 .c 和 .h 文件(个人习惯)
第11步:添加 printf 打印代码
第12步:生成代码只需要在main.c中调用 MX_LWIP_Process();
==============================
更新内容
再lwip.c增加打印自动获取到的IP地址、掩码和网关信息
/* DHCP Variables initialization ---------------------------------------------*/
uint32_t DHCPfineTimer = 0;
uint32_t DHCPcoarseTimer = 0;
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* Variables Initialization */
struct netif gnetif;
ip4_addr_t ipaddr;
ip4_addr_t netmask;
ip4_addr_t gw;
/* USER CODE BEGIN 2 */
//==========================================================================【BEGIN】
#include "stdio.h"
static uint8_t DHCP_DHCP = 0 ;
void lwip_Get_IP(void)
{
if(DHCP_DHCP == 0)
{
if(gnetif.ip_addr.addr > 0)
{
//IP地址
printf("\r\nIP地址:%d.%d.%d.%d\r\n",\
(gnetif.ip_addr.addr >> 0) & 0xff,\
(gnetif.ip_addr.addr >> 8) & 0xff,\
(gnetif.ip_addr.addr >> 16) & 0xff,\
(gnetif.ip_addr.addr >> 24) & 0xff);
//掩码地址
printf("\r\n掩码地址:%d.%d.%d.%d\r\n",\
(gnetif.netmask.addr >> 0) & 0xff,\
(gnetif.netmask.addr >> 8) & 0xff,\
(gnetif.netmask.addr >> 16) & 0xff,\
(gnetif.netmask.addr >> 24) & 0xff);
//网关地址
printf("\r\n网关地址:%d.%d.%d.%d\r\n",\
(gnetif.gw.addr >> 0) & 0xff,\
(gnetif.gw.addr >> 8) & 0xff,\
(gnetif.gw.addr >> 16) & 0xff,\
(gnetif.gw.addr >> 24) & 0xff);
DHCP_DHCP = 1;
}
}
}
//==========================================================================【END】
/* USER CODE END 2 */
/**
* LwIP initialization function
*/
void MX_LWIP_Init(void)
{
/* Initilialize the LwIP stack without RTOS */
lwip_init();
/* IP addresses initialization with DHCP (IPv4) */
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
/* add the network interface (IPv4/IPv6) without RTOS */
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 */
/* Start DHCP negotiation for a network interface (IPv4) */
dhcp_start(&gnetif);
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}
==============================
第13步:测试结果验证
/**********************************************************************************/
分割线:下方为固定IP地址测试
/**********************************************************************************/
1、开启固定IP地址
2、设置固定IP地址、掩码、网关(注:不能使用网络中已在使用的IP地址)
3、添加打印代码
4、测试结果
对啦!本人能力有限哈!文中有啥讲错 的地方,还请各位大神留言赐教哈!