|
|
@@ -47,17 +47,20 @@
|
|
|
#define WIZ_ID_LEN 6
|
|
|
static char wiz_netdev_name[WIZ_ID_LEN];
|
|
|
|
|
|
-#define WIZ_DHCP_SOCKET 7
|
|
|
+#define WIZ_DHCP_SOCKET 7
|
|
|
|
|
|
extern struct rt_spi_device *wiz_device;
|
|
|
extern int wiz_device_init(const char *spi_dev_name, rt_base_t rst_pin, rt_base_t isr_pin);
|
|
|
extern int wiz_inet_init(void);
|
|
|
-static int wiz_netdev_info_update(struct netdev *netdev);
|
|
|
+static int wiz_netdev_info_update(struct netdev *netdev, rt_bool_t reset);
|
|
|
|
|
|
rt_bool_t wiz_init_ok = RT_FALSE;
|
|
|
static wiz_NetInfo wiz_net_info;
|
|
|
static rt_timer_t dns_tick_timer;
|
|
|
+
|
|
|
+#ifdef WIZ_USING_DHCP
|
|
|
static rt_timer_t dhcp_timer;
|
|
|
+#endif
|
|
|
|
|
|
static void spi_write_byte(uint8_t data)
|
|
|
{
|
|
|
@@ -213,7 +216,7 @@ static void wiz_ip_assign(void)
|
|
|
getDNSfromDHCP(wiz_net_info.dns);
|
|
|
wiz_net_info.dhcp = NETINFO_DHCP;
|
|
|
|
|
|
- ctlnetwork(CN_SET_NETINFO, (void*) &wiz_net_info);
|
|
|
+ ctlnetwork(CN_SET_NETINFO, (void*) &wiz_net_info);
|
|
|
}
|
|
|
|
|
|
static void wiz_ip_conflict(void)
|
|
|
@@ -293,25 +296,27 @@ static int wiz_netstr_to_array(const char *net_str, uint8_t *net_array)
|
|
|
/* set WIZnet device MAC address */
|
|
|
RT_WEAK void wiz_user_config_mac(char *mac_buf, rt_uint8_t buf_len)
|
|
|
{
|
|
|
- RT_ASSERT(mac_buf != RT_NULL);
|
|
|
- RT_ASSERT(buf_len > 0);
|
|
|
-
|
|
|
- rt_memset(mac_buf, 0x0, buf_len);
|
|
|
- rt_strncpy(mac_buf, WIZ_DEFAULT_MAC, buf_len);
|
|
|
+ RT_ASSERT(mac_buf != RT_NULL);
|
|
|
+ RT_ASSERT(buf_len > 0);
|
|
|
+
|
|
|
+ rt_memset(mac_buf, 0x0, buf_len);
|
|
|
+ rt_strncpy(mac_buf, WIZ_DEFAULT_MAC, buf_len);
|
|
|
}
|
|
|
|
|
|
static void wiz_set_mac(void)
|
|
|
{
|
|
|
- char mac_str[32];
|
|
|
-
|
|
|
- wiz_user_config_mac(mac_str, sizeof(mac_str));
|
|
|
- if (wiz_netstr_to_array(mac_str, wiz_net_info.mac) != RT_EOK)
|
|
|
- {
|
|
|
- wiz_netstr_to_array(WIZ_DEFAULT_MAC, wiz_net_info.mac);
|
|
|
- }
|
|
|
+ char mac_str[32];
|
|
|
+
|
|
|
+ wiz_user_config_mac(mac_str, sizeof(mac_str));
|
|
|
+ if (wiz_netstr_to_array(mac_str, wiz_net_info.mac) != RT_EOK)
|
|
|
+ {
|
|
|
+ wiz_netstr_to_array(WIZ_DEFAULT_MAC, wiz_net_info.mac);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+#ifdef WIZ_USING_DHCP
|
|
|
static int wiz_network_dhcp(struct netdev *netdev);
|
|
|
+#endif
|
|
|
|
|
|
/* initialize WIZnet network configures */
|
|
|
static int wiz_network_init(rt_bool_t b_config)
|
|
|
@@ -327,7 +332,8 @@ static int wiz_network_init(rt_bool_t b_config)
|
|
|
#ifndef WIZ_USING_DHCP
|
|
|
if(wiz_netstr_to_array(WIZ_IPADDR, wiz_net_info.ip) != RT_EOK ||
|
|
|
wiz_netstr_to_array(WIZ_MSKADDR, wiz_net_info.sn) != RT_EOK ||
|
|
|
- wiz_netstr_to_array(WIZ_GWADDR, wiz_net_info.gw) != RT_EOK)
|
|
|
+ wiz_netstr_to_array(WIZ_GWADDR, wiz_net_info.dns) != RT_EOK ||
|
|
|
+ wiz_netstr_to_array(WIZ_GWADDR, wiz_net_info.gw) != RT_EOK)
|
|
|
{
|
|
|
netdev_low_level_set_status(netdev, RT_FALSE);
|
|
|
netdev_low_level_set_link_status(netdev, RT_FALSE);
|
|
|
@@ -336,13 +342,13 @@ static int wiz_network_init(rt_bool_t b_config)
|
|
|
wiz_net_info.dhcp = NETINFO_STATIC;
|
|
|
#endif
|
|
|
|
|
|
- int result = RT_EOK;
|
|
|
- rt_bool_t b_status = b_config;
|
|
|
-
|
|
|
- /* set mac information */
|
|
|
- wiz_set_mac();
|
|
|
+ int result = RT_EOK;
|
|
|
+ rt_bool_t b_status = b_config;
|
|
|
+
|
|
|
+ /* set mac information */
|
|
|
+ wiz_set_mac();
|
|
|
/* set static WIZnet network information */
|
|
|
- ctlnetwork(CN_SET_NETINFO, (void*) &wiz_net_info);
|
|
|
+ ctlnetwork(CN_SET_NETINFO, (void*) &wiz_net_info);
|
|
|
|
|
|
#ifdef WIZ_USING_DHCP
|
|
|
/* alloc IP address through DHCP */
|
|
|
@@ -350,20 +356,20 @@ static int wiz_network_init(rt_bool_t b_config)
|
|
|
result = wiz_network_dhcp(netdev);
|
|
|
if (result != RT_EOK)
|
|
|
{
|
|
|
- b_status = RT_FALSE;
|
|
|
- LOG_E("WIZnet network initialize failed, DHCP timeout.");
|
|
|
+ b_status = RT_FALSE;
|
|
|
+ LOG_E("WIZnet network initialize failed, DHCP timeout.");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ b_status = RT_TRUE;
|
|
|
+ LOG_D("WIZnet network initialize success.");
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- b_status = RT_TRUE;
|
|
|
- LOG_D("WIZnet network initialize success.");
|
|
|
- }
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
netdev_low_level_set_status(netdev, b_status);
|
|
|
netdev_low_level_set_link_status(netdev, b_status);
|
|
|
- wiz_netdev_info_update(netdev);
|
|
|
+ wiz_netdev_info_update(netdev, RT_FALSE);
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
@@ -392,17 +398,27 @@ static int wiz_socket_init(void)
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
|
|
|
-static void wiz_dns_time_handler(void* arg)
|
|
|
+static void wiz_dns_time_handler(void *arg)
|
|
|
{
|
|
|
extern void DNS_time_handler(void);
|
|
|
DNS_time_handler();
|
|
|
}
|
|
|
|
|
|
-static int wiz_netdev_info_update(struct netdev *netdev)
|
|
|
+static int wiz_netdev_info_update(struct netdev *netdev, rt_bool_t reset)
|
|
|
{
|
|
|
wiz_NetInfo net_info;
|
|
|
+ rt_memset(&net_info, 0, sizeof(net_info));
|
|
|
|
|
|
- ctlnetwork(CN_GET_NETINFO, (void *)&net_info);
|
|
|
+ if(reset == RT_FALSE)
|
|
|
+ {
|
|
|
+ ctlnetwork(CN_GET_NETINFO, (void *)&net_info);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ /* clean dns server information */
|
|
|
+ netdev->dns_servers->addr = 0;
|
|
|
+ ctlnetwork(CN_SET_NETINFO, (void *)&net_info);
|
|
|
+ }
|
|
|
netdev_low_level_set_ipaddr(netdev, (const ip_addr_t *)&net_info.ip);
|
|
|
netdev_low_level_set_gw(netdev, (const ip_addr_t *)&net_info.gw);
|
|
|
netdev_low_level_set_netmask(netdev, (const ip_addr_t *)&net_info.sn);
|
|
|
@@ -577,21 +593,21 @@ static struct netdev *wiz_netdev_add(const char *netdev_name)
|
|
|
#ifdef WIZ_USING_DHCP
|
|
|
static void wiz_dhcp_work(struct rt_work *dhcp_work, void *dhcp_work_data)
|
|
|
{
|
|
|
-#define WIZ_DHCP_WORK_RETRY 1
|
|
|
-#define WIZ_DHCP_WORK_RETRY_TIME (2 * RT_TICK_PER_SECOND)
|
|
|
-
|
|
|
- RT_ASSERT(dhcp_work_data != RT_NULL);
|
|
|
-
|
|
|
- struct netdev *netdev = (struct netdev *)dhcp_work_data;
|
|
|
-
|
|
|
- uint8_t dhcp_times = 0;
|
|
|
- uint8_t data_buffer[1024];
|
|
|
- uint32_t dhcp_status = 0;
|
|
|
-
|
|
|
- rt_timer_start(dhcp_timer);
|
|
|
+#define WIZ_DHCP_WORK_RETRY 1
|
|
|
+#define WIZ_DHCP_WORK_RETRY_TIME (2 * RT_TICK_PER_SECOND)
|
|
|
+
|
|
|
+ RT_ASSERT(dhcp_work_data != RT_NULL);
|
|
|
+
|
|
|
+ struct netdev *netdev = (struct netdev *)dhcp_work_data;
|
|
|
+
|
|
|
+ uint8_t dhcp_times = 0;
|
|
|
+ uint8_t data_buffer[1024];
|
|
|
+ uint32_t dhcp_status = 0;
|
|
|
+
|
|
|
+ rt_timer_start(dhcp_timer);
|
|
|
DHCP_init(WIZ_DHCP_SOCKET, data_buffer);
|
|
|
|
|
|
- while (1)
|
|
|
+ while (1)
|
|
|
{
|
|
|
/* DHCP start, return DHCP_IP_LEASED is success. */
|
|
|
dhcp_status = DHCP_run();
|
|
|
@@ -600,23 +616,23 @@ static void wiz_dhcp_work(struct rt_work *dhcp_work, void *dhcp_work_data)
|
|
|
{
|
|
|
case DHCP_IP_ASSIGN:
|
|
|
case DHCP_IP_CHANGED:
|
|
|
- {
|
|
|
- /* to update netdev information */
|
|
|
- wiz_netdev_info_update(netdev);
|
|
|
+ {
|
|
|
+ /* to update netdev information */
|
|
|
+ wiz_netdev_info_update(netdev, RT_FALSE);
|
|
|
break;
|
|
|
}
|
|
|
case DHCP_IP_LEASED:
|
|
|
{
|
|
|
DHCP_stop();
|
|
|
rt_timer_stop(dhcp_timer);
|
|
|
- /* to update netdev information */
|
|
|
- wiz_netdev_info_update(netdev);
|
|
|
- if (dhcp_work)
|
|
|
- {
|
|
|
- /* according to leaset time, config next DHCP produce */
|
|
|
- rt_work_init(dhcp_work, wiz_dhcp_work, (void *)netdev);
|
|
|
- rt_work_submit(dhcp_work, (getDHCPLeasetime() - 60) * RT_TICK_PER_SECOND);
|
|
|
- }
|
|
|
+ /* to update netdev information */
|
|
|
+ wiz_netdev_info_update(netdev, RT_FALSE);
|
|
|
+ if (dhcp_work)
|
|
|
+ {
|
|
|
+ /* according to the DHCP leaset time, config next DHCP produce */
|
|
|
+ rt_work_init(dhcp_work, wiz_dhcp_work, (void *)netdev);
|
|
|
+ rt_work_submit(dhcp_work, (getDHCPLeasetime() / 2) * RT_TICK_PER_SECOND);
|
|
|
+ }
|
|
|
return;
|
|
|
}
|
|
|
case DHCP_FAILED:
|
|
|
@@ -624,26 +640,26 @@ static void wiz_dhcp_work(struct rt_work *dhcp_work, void *dhcp_work_data)
|
|
|
dhcp_times++;
|
|
|
break;
|
|
|
}
|
|
|
- case DHCP_STOPPED:
|
|
|
- {
|
|
|
- dhcp_times = WIZ_DHCP_WORK_RETRY;
|
|
|
- break;
|
|
|
- }
|
|
|
+ case DHCP_STOPPED:
|
|
|
+ {
|
|
|
+ dhcp_times = WIZ_DHCP_WORK_RETRY;
|
|
|
+ break;
|
|
|
+ }
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (dhcp_times >= WIZ_DHCP_WORK_RETRY)
|
|
|
{
|
|
|
DHCP_stop();
|
|
|
rt_timer_stop(dhcp_timer);
|
|
|
|
|
|
- if (dhcp_work)
|
|
|
- {
|
|
|
- /* according to WIZ_DHCP_WORK_RETRY_TIME, config reconfig after 2 secs */
|
|
|
- rt_work_init(dhcp_work, wiz_dhcp_work, (void *)netdev);
|
|
|
- rt_work_submit(dhcp_work, WIZ_DHCP_WORK_RETRY_TIME);
|
|
|
- }
|
|
|
+ if (dhcp_work)
|
|
|
+ {
|
|
|
+ /* according to WIZ_DHCP_WORK_RETRY_TIME, config reconfig after 2 secs */
|
|
|
+ rt_work_init(dhcp_work, wiz_dhcp_work, (void *)netdev);
|
|
|
+ rt_work_submit(dhcp_work, WIZ_DHCP_WORK_RETRY_TIME);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -651,27 +667,27 @@ static void wiz_dhcp_work(struct rt_work *dhcp_work, void *dhcp_work_data)
|
|
|
|
|
|
static int wiz_network_dhcp(struct netdev *netdev)
|
|
|
{
|
|
|
- if (netdev == RT_NULL)
|
|
|
- return -RT_EINVAL;
|
|
|
-
|
|
|
- /* set default MAC address for DHCP */
|
|
|
+ if (netdev == RT_NULL)
|
|
|
+ return -RT_EINVAL;
|
|
|
+
|
|
|
+ /* set default MAC address for DHCP */
|
|
|
setSHAR(wiz_net_info.mac);
|
|
|
/* DHCP configure initialize, clear information other than MAC address */
|
|
|
setSn_RXBUF_SIZE(WIZ_DHCP_SOCKET, 0x02);
|
|
|
setSn_TXBUF_SIZE(WIZ_DHCP_SOCKET, 0x02);
|
|
|
- /* register to assign IP address and conflict callback */
|
|
|
+ /* register to assign IP address and conflict callback */
|
|
|
reg_dhcp_cbfunc(wiz_ip_assign, wiz_ip_assign, wiz_ip_conflict);
|
|
|
|
|
|
- dhcp_timer = rt_timer_create("wiz_dhcp", wiz_dhcp_timer_entry, RT_NULL, 1 * RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
|
|
|
+ dhcp_timer = rt_timer_create("wiz_dhcp", wiz_dhcp_timer_entry, RT_NULL, 1 * RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
|
|
|
if (dhcp_timer == RT_NULL)
|
|
|
return -RT_ERROR;
|
|
|
|
|
|
- struct rt_work *dhcp_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work));
|
|
|
- if (dhcp_work == RT_NULL)
|
|
|
- return -RT_ENOMEM;
|
|
|
-
|
|
|
- wiz_dhcp_work(dhcp_work, netdev);
|
|
|
- return RT_EOK;
|
|
|
+ struct rt_work *dhcp_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work));
|
|
|
+ if (dhcp_work == RT_NULL)
|
|
|
+ return -RT_ENOMEM;
|
|
|
+
|
|
|
+ wiz_dhcp_work(dhcp_work, netdev);
|
|
|
+ return RT_EOK;
|
|
|
}
|
|
|
#endif /* WIZ_USING_DHCP */
|
|
|
|
|
|
@@ -700,15 +716,18 @@ static void wiz_link_status_thread_entry(void *parameter)
|
|
|
if (phycfgr & WIZ_PHYCFGR_LINK_STATUS)
|
|
|
{
|
|
|
#ifdef WIZ_USING_DHCP
|
|
|
- wiz_dhcp_work(RT_NULL, netdev);
|
|
|
-#endif
|
|
|
+ wiz_dhcp_work(RT_NULL, netdev);
|
|
|
+#else
|
|
|
+ wiz_network_init(RT_TRUE);
|
|
|
+#endif
|
|
|
netdev_low_level_set_link_status(netdev, phycfgr & WIZ_PHYCFGR_LINK_STATUS);
|
|
|
- wiz_netdev_info_update(netdev);
|
|
|
+ wiz_netdev_info_update(netdev, RT_FALSE);
|
|
|
LOG_I("%s netdev link status becomes link up", wiz_netdev_name);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
netdev_low_level_set_link_status(netdev, phycfgr & WIZ_PHYCFGR_LINK_STATUS);
|
|
|
+ wiz_netdev_info_update(netdev, RT_TRUE);
|
|
|
LOG_I("%s netdev link status becomes link down", wiz_netdev_name);
|
|
|
}
|
|
|
}
|
|
|
@@ -716,6 +735,7 @@ static void wiz_link_status_thread_entry(void *parameter)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/* #include "stm32f4xx_hal.h" */
|
|
|
/* WIZnet initialize device and network */
|
|
|
int wiz_init(void)
|
|
|
{
|
|
|
@@ -729,6 +749,9 @@ int wiz_init(void)
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
|
|
|
+ /* I think you can attach w5500 into spi bus at here. You can use this function to realize.*/
|
|
|
+ /* extern rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, uint16_t cs_gpio_pin); */
|
|
|
+
|
|
|
/* WIZnet SPI device and pin initialize */
|
|
|
result = wiz_device_init(WIZ_SPI_DEVICE, WIZ_RST_PIN, WIZ_IRQ_PIN);
|
|
|
if (result != RT_EOK)
|
|
|
@@ -744,7 +767,7 @@ int wiz_init(void)
|
|
|
wiz_reset();
|
|
|
/* set WIZnet device read/write data callback */
|
|
|
wiz_callback_register();
|
|
|
- /* WIZnet chip configure initialize */
|
|
|
+ /* WIZnet chip configure initialize */
|
|
|
result = wiz_chip_cfg_init();
|
|
|
if (result != RT_EOK)
|
|
|
{
|
|
|
@@ -759,7 +782,7 @@ int wiz_init(void)
|
|
|
/* WIZnet socket initialize */
|
|
|
wiz_socket_init();
|
|
|
|
|
|
- dns_tick_timer = rt_timer_create("dns_tick", wiz_dns_time_handler, RT_NULL, 1*RT_TICK_PER_SECOND, RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_PERIODIC);
|
|
|
+ dns_tick_timer = rt_timer_create("dns_tick", wiz_dns_time_handler, RT_NULL, 1 * RT_TICK_PER_SECOND, RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_PERIODIC);
|
|
|
rt_timer_start(dns_tick_timer);
|
|
|
|
|
|
/* create WIZnet link status Polling thread */
|