【笔记】w5500 官方DHCP库 使用

官方版本 V4.0.0

无需改动

心得

1.适配其中的 时基函数

/*
 * @brief DHCP 1s Tick Timer handler
 * @note SHOULD BE register to your system 1s Tick timer handler 
 */
void DHCP_time_handler(void);

看情况,定时器,系统节拍中 都可以。实现 1s 调用一次该函数就可以

2. 创建消息缓冲区

目的为了接收解析udp消息使用。
是接收以下数据使用

.c

/*
 * @brief DHCP message format
 */ 
typedef struct {
	uint8_t  op;            ///< @ref DHCP_BOOTREQUEST or @ref DHCP_BOOTREPLY
	uint8_t  htype;         ///< @ref DHCP_HTYPE10MB or @ref DHCP_HTYPE100MB
	uint8_t  hlen;          ///< @ref DHCP_HLENETHERNET
	uint8_t  hops;          ///< @ref DHCP_HOPS
	uint32_t xid;           ///< @ref DHCP_XID  This increase one every DHCP transaction.
	uint16_t secs;          ///< @ref DHCP_SECS
	uint16_t flags;         ///< @ref DHCP_FLAGSBROADCAST or @ref DHCP_FLAGSUNICAST
	uint8_t  ciaddr[4];     ///< @ref Request IP to DHCP sever
	uint8_t  yiaddr[4];     ///< @ref Offered IP from DHCP server
	uint8_t  siaddr[4];     ///< No use 
	uint8_t  giaddr[4];     ///< No use
	uint8_t  chaddr[16];    ///< DHCP client 6bytes MAC address. Others is filled to zero
	uint8_t  sname[64];     ///< No use
	uint8_t  file[128];     ///< No use
	uint8_t  OPT[OPT_SIZE]; ///< Option
} RIP_MSG;

我们创建缓冲区的大小 : 在 .c 98 行有定义 这个结构体的空间大小

#define OPT_SIZE                 312               /// Max OPT size of @ref RIP_MSG
#define RIP_MSG_SIZE             (236+OPT_SIZE)    /// Max size of @ref RIP_MSG

所以 236+312 = 548; 感觉这个宏应该放在 .h 文件里,比较方便。就这样用吧,不改动了

创建的时候 最好动态 申请内存。 因为只有dhcp时候 用一下。(别浪费ram,ram量大无所谓)

缓冲区地址将会被这个函数使用,可以看到 转成 结构体类型了。所以空间大小要对称

void DHCP_init(uint8_t s, uint8_t * buf)
{
   uint8_t zeroip[4] = {0,0,0,0};
   getSHAR(DHCP_CHADDR);
   if((DHCP_CHADDR[0] | DHCP_CHADDR[1]  | DHCP_CHADDR[2] | DHCP_CHADDR[3] | DHCP_CHADDR[4] | DHCP_CHADDR[5]) == 0x00)
   {
      // assigning temporary mac address, you should be set SHAR before call this function. 
      DHCP_CHADDR[0] = 0x00;
      DHCP_CHADDR[1] = 0x08;
      DHCP_CHADDR[2] = 0xdc;      
      DHCP_CHADDR[3] = 0x00;
      DHCP_CHADDR[4] = 0x00;
      DHCP_CHADDR[5] = 0x00; 
      setSHAR(DHCP_CHADDR);     
   }

	DHCP_SOCKET = s; // SOCK_DHCP
	pDHCPMSG = (RIP_MSG*)buf;
	DHCP_XID = 0x12345678;
	{
		DHCP_XID += DHCP_CHADDR[3];
		DHCP_XID += DHCP_CHADDR[4];
		DHCP_XID += DHCP_CHADDR[5];
		DHCP_XID += (DHCP_CHADDR[3] ^ DHCP_CHADDR[4] ^ DHCP_CHADDR[5]);
	}
	// WIZchip Netinfo Clear
	setSIPR(zeroip);
	setGAR(zeroip);

	reset_DHCP_timeout();
	dhcp_state = STATE_DHCP_INIT;
}

其他问题

看到很多修改回调函数的 ip_assign,ip_update,ip_conflict 这几个。
其实不需要更改,.c里有默认的实现函数。
注册时候 直接 填空,就会使用默认函数。

reg_dhcp_cbfunc(NULL,NULL,NULL);

实际操作

设备上电 基础的操作。

  w5500_hard_reset();// 硬件复位
    w5500_interface_init();//我自己的函数  注册wizchip_conf 的操作函数

缓冲区默认

wizchip_init(NULL, NULL) 

1.PHY

好像可以自动 感兴趣可以试试

 // phy init
    phyconf.by = PHY_CONFBY_SW;
    phyconf.mode = PHY_MODE_MANUAL;
    phyconf.speed = PHY_SPEED_100;
    phyconf.duplex = PHY_DUPLEX_FULL;
    wizphy_setphyconf(&phyconf);

2.mac要设置

dhcp就是根据mac分配的
如果局域网内单台设备 不设置也行,有默认设置

setSHAR(netconf.mac);

3.初始化DHCP 注册函数

DHCP_init(1,DHCP_buf);
reg_dhcp_cbfunc(NULL,NULL,NULL);// 使用默认回调函数

4.动态申请DHCP

 dhcp_ret = DHCP_run();//重点是DHCP_run(),该函数实现动态申请IP的功能
        while(dhcp_ret != DHCP_IP_LEASED)//返回DHCP_IP_LEASED说明申请租赁IP地址成功,如果没成功,重新申请
        {
                HAL_Delay(1000);
                dhcp_ret = DHCP_run();
                 printf(".");
        }

5.成功打印

在这里插入图片描述
可以PING通

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值