001--手把手教你创建 LWIP 模板例程__更新版

手把手教你创建 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, &ethernetif_init, &ethernet_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、测试结果

 

对啦!本人能力有限哈!文中有啥讲错 的地方,还请各位大神留言赐教哈!

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值