123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- /*!
- \file netconf.c
- \brief network connection configuration
- */
- /*
- Copyright (C) 2016 GigaDevice
- 2016-08-15, V1.0.0, firmware for GD32F4xx
- */
- #include "netconf.h"
- #include "ethernetif.h"
- #include "lwip/dhcp.h"
- #include "lwip/mem.h"
- #include "lwip/memp.h"
- #include "lwip/netif.h"
- #include "lwip/tcpip.h"
- #include "main.h"
- #include <stdio.h>
- #define MAX_DHCP_TRIES 4
- typedef enum
- {
- DHCP_START = 0,
- DHCP_WAIT_ADDRESS,
- DHCP_ADDRESS_ASSIGNED,
- DHCP_TIMEOUT
- } dhcp_state_enum;
- #ifdef USE_DHCP
- dhcp_state_enum dhcp_state = DHCP_START;
- ip4_addr_t ip_address = {0};
- #endif /* USE_DHCP */
- struct netif gnetif;
- /*!
- \brief initializes the LwIP stack
- \param[in] none
- \param[out] none
- \retval none
- */
- void lwip_setup(void)
- {
- ip4_addr_t ipaddr;
- ip4_addr_t netmask;
- ip4_addr_t gw;
- /* create tcp_ip stack thread */
- tcpip_init(NULL, NULL);
- /* IP address setting */
- #ifdef USE_DHCP
- ipaddr.addr = 0;
- netmask.addr = 0;
- gw.addr = 0;
- #else
- IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
- IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
- IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
- #endif /* USE_DHCP */
- /* - netif_add(struct netif *netif,
- struct ip_addr *ipaddr,
- struct ip_addr *netmask,
- struct ip_addr *gw,
- void *state,
- err_t (* init)(struct netif *netif),
- err_t (* input)(struct pbuf *p,
- struct netif *netif))
- 将您的网络接口添加到 netif_list。
- 分配结构netif,并将指向该结构的指针作为第一个参数传递。
- 使用DHCP时,请提供指向已清除的ip_addr结构的指针,否则,请使用健全的数字填充它们。
- 状态指针可以为NULL。
- 初始化函数指针必须指向您的以太网netif接口的初始化函数。 以下代码说明了它的用法。*/
- netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input);
- printf("LwIP Registers the default network interface....\r\n");
- /* 注册默认网络接口.*/
- netif_set_default(&gnetif);
- if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
- {
- printf("LwIP Finish interface....\r\n");
- gnetif.flags |= NETIF_FLAG_LINK_UP;
- /* 配置完成网卡后启动网卡*/
- netif_set_up(&gnetif);
- }
- else
- {
- netif_set_down(&gnetif);
- }
- netif_set_link_callback(&gnetif, ETH_link_callback);
- }
- #ifdef USE_DHCP
- /*!
- \brief lwip_dhcp_process_handle
- \param[in] none
- \param[out] none
- \retval none
- */
- void dhcp_task(void *pvParameters)
- {
- ip4_addr_t ipaddr;
- ip4_addr_t netmask;
- ip4_addr_t gw;
- struct dhcp *dhcp_client;
- dhcp_client = netif_dhcp_data(&xnetif);
- for (;;)
- {
- switch (dhcp_state)
- {
- case DHCP_START:
- dhcp_start(&xnetif);
- ip_address.addr = 0;
- dhcp_state = DHCP_WAIT_ADDRESS;
- break;
- case DHCP_WAIT_ADDRESS:
- /* read the new IP address */
- ip_address.addr = xnetif.ip_addr.u_addr.ip4.addr;
- if (ip_address.addr != 0)
- {
- dhcp_state = DHCP_ADDRESS_ASSIGNED;
- /* stop DHCP */
- dhcp_stop(&xnetif);
- printf("\r\nDHCP -- eval board ip address: %d.%d.%d.%d \r\n", ip4_addr1_16(&ip_address),
- ip4_addr2_16(&ip_address), ip4_addr3_16(&ip_address), ip4_addr4_16(&ip_address));
- OSTaskSuspend(OS_PRIO_SELF);
- }
- else
- {
- /* DHCP timeout */
- if (dhcp_client->tries > MAX_DHCP_TRIES)
- {
- dhcp_state = DHCP_TIMEOUT;
- /* stop DHCP */
- dhcp_stop(&xnetif);
- /* static address used */
- IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
- IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
- IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
- netif_set_addr(&xnetif, &ipaddr, &netmask, &gw);
- OSTaskSuspend(OS_PRIO_SELF);
- }
- }
- break;
- default:
- break;
- }
- /* wait 500 ms */
- OSTimeDlyHMSM(0, 0, 0, 500);
- }
- }
- #endif /* USE_DHCP */
|