Sfoglia il codice sorgente

Merge pull request #53 from heyuanjie87/dev

优化执行流程
朱天龙 (Armink) 5 anni fa
parent
commit
7a9483ca34
2 ha cambiato i file con 151 aggiunte e 139 eliminazioni
  1. 146 34
      src/wiz.c
  2. 5 105
      src/wiz_device.c

+ 146 - 34
src/wiz.c

@@ -50,7 +50,7 @@ static char wiz_netdev_name[WIZ_ID_LEN];
 #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_device_init(const char *spi_dev_name);
 extern int wiz_inet_init(void);
 static int wiz_netdev_info_update(struct netdev *netdev, rt_bool_t reset);
 
@@ -61,6 +61,85 @@ static rt_timer_t  dns_tick_timer;
 #ifdef WIZ_USING_DHCP
 static rt_timer_t  dhcp_timer;
 #endif
+extern int wiz_recv_notice_cb(int socket);
+extern int wiz_closed_notice_cb(int socket);
+
+static rt_mailbox_t wiz_rx_mb = RT_NULL;
+
+static void wiz_isr(void)
+{
+    /* enter interrupt */
+    rt_interrupt_enter();
+
+    rt_mb_send(wiz_rx_mb, (rt_ubase_t) wiz_device);
+
+    /* leave interrupt */
+    rt_interrupt_leave();
+}
+
+static void wiz_data_thread_entry(void *parameter)
+{
+#define IR_SOCK(ch)         (0x01 << ch)   /**< check socket interrupt */
+
+    struct rt_spi_device* dev;
+
+    while (1)
+    {
+        if (rt_mb_recv(wiz_rx_mb, (rt_ubase_t*) &dev, RT_WAITING_FOREVER) == RT_EOK)
+        {
+            uint8_t ir, sir, sn_ir;
+            int8_t socket = -1;
+
+            /* get IR data than clean IR */
+            ir = getIR();
+            setIR(ir);
+
+            if ((ir & IR_CONFLICT) == IR_CONFLICT)
+            {
+                setIR(IR_CONFLICT);
+            }
+
+            if ((ir & IR_UNREACH) == IR_UNREACH)
+            {
+                setIR(IR_UNREACH);
+            }
+
+            /* get and process socket interrupt register */
+            sir = getSIR();
+
+            for (socket = 0; socket < 8; socket++)
+            {
+                sn_ir = 0;
+
+                if (sir & IR_SOCK(socket))
+                {
+                    /* save interrupt value*/
+                    sn_ir = getSn_IR(socket);
+
+                    if (sn_ir & Sn_IR_CON)
+                    {
+                        setSn_IR(socket, Sn_IR_CON);
+                    }
+                    if (sn_ir & Sn_IR_DISCON)
+                    {
+                        wiz_closed_notice_cb(socket);
+                        setSn_IR(socket, Sn_IR_DISCON);
+                    }
+                    if (sn_ir & Sn_IR_RECV)
+                    {
+                        wiz_recv_notice_cb(socket);
+                        setSn_IR(socket, Sn_IR_RECV);
+                    }
+                    if (sn_ir & Sn_IR_TIMEOUT)
+                    {
+                        /* deal with timeout event in the wiznet ioLibrary */
+                        //setSn_IR(socket, Sn_IR_TIMEOUT);
+                    }
+                }
+            }
+        }
+    }
+}
 
 static void spi_write_byte(uint8_t data)
 {
@@ -174,25 +253,6 @@ static int wiz_chip_cfg_init(void)
         return -RT_ERROR;
     }
 
-    start_tick = rt_tick_get();
-    do
-    {
-        now_tick = rt_tick_get();
-        if (now_tick - start_tick > CW_INIT_TIMEOUT)
-        {
-            LOG_E("WIZnet chip configure initialize timeout.");
-            return -RT_ETIMEOUT;
-        }
-
-        /* waiting for link status online */
-        if (ctlwizchip(CW_GET_PHYLINK, (void*) &phy_status) == -1)
-        {
-            LOG_E("Unknown PHY Link stauts.");
-        }
-
-        rt_thread_mdelay(100);
-    } while (phy_status == PHY_LINK_OFF);
-
     return RT_EOK;
 }
 
@@ -604,7 +664,7 @@ static void wiz_dhcp_work(struct rt_work *dhcp_work, void *dhcp_work_data)
     struct netdev *netdev = (struct netdev *)dhcp_work_data;
 
     uint8_t dhcp_times = 0;
-    uint8_t data_buffer[1024];
+    static uint8_t data_buffer[1024];
     uint32_t dhcp_status = 0;
 
     rt_timer_start(dhcp_timer);
@@ -689,7 +749,9 @@ static int wiz_network_dhcp(struct netdev *netdev)
     if (dhcp_work == RT_NULL)
         return -RT_ENOMEM;
 
-    wiz_dhcp_work(dhcp_work, netdev);
+    rt_work_init(dhcp_work, wiz_dhcp_work, (void *)netdev);
+    rt_work_submit(dhcp_work, WIZ_DHCP_WORK_RETRY_TIME);
+
     return RT_EOK;
 }
 #endif /* WIZ_USING_DHCP */
@@ -735,8 +797,49 @@ static void wiz_link_status_thread_entry(void *parameter)
                 LOG_I("%s netdev link status becomes link down", wiz_netdev_name);
             }
         }
-        rt_thread_mdelay(1000);
+        rt_thread_mdelay(4000);
+    }
+}
+
+static int wiz_interrupt_init(rt_base_t isr_pin)
+{
+    rt_thread_t tid;
+
+    /* initialize RX mailbox */
+    wiz_rx_mb = rt_mb_create("wiz_mb", WIZ_RX_MBOX_NUM, RT_IPC_FLAG_FIFO);
+    if (wiz_rx_mb == RT_NULL)
+    {
+        LOG_E("WIZnet create receive data mailbox error.");
+        return -RT_ENOMEM;
     }
+
+    /* create WIZnet SPI RX thread  */
+    tid = rt_thread_create("wiz", wiz_data_thread_entry, RT_NULL, 1024, RT_THREAD_PRIORITY_MAX / 6, 20);
+    if (tid != RT_NULL)
+    {
+        rt_thread_startup(tid);
+    }
+
+    /* initialize interrupt pin */
+    rt_pin_mode(isr_pin, PIN_MODE_INPUT_PULLUP);
+    rt_pin_attach_irq(isr_pin, PIN_IRQ_MODE_FALLING, (void (*)(void*)) wiz_isr, RT_NULL);
+    rt_pin_irq_enable(isr_pin, PIN_IRQ_ENABLE);
+
+    return 0;
+}
+
+static int wiz_is_exist(void)
+{
+    wiz_NetInfo ni;
+    int ret;
+
+    wiz_set_mac();
+    ctlnetwork(CN_SET_NETINFO, (void *)&wiz_net_info);
+    ctlnetwork(CN_GET_NETINFO, (void *)&ni);
+
+    ret = rt_memcmp(wiz_net_info.mac, ni.mac, sizeof(ni.mac));
+
+    return (ret == 0);
 }
 
 /* #include "stm32f4xx_hal.h" */
@@ -744,7 +847,6 @@ static void wiz_link_status_thread_entry(void *parameter)
 int wiz_init(void)
 {
     int result = RT_EOK;
-    rt_bool_t b_config = RT_TRUE;
     rt_thread_t tid;
 
     if (wiz_init_ok == RT_TRUE)
@@ -753,34 +855,42 @@ int wiz_init(void)
         return RT_EOK;
     }
 
+        /* initialize reset pin */
+    rt_pin_mode(WIZ_RST_PIN, PIN_MODE_OUTPUT);
+
     /* 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);
+    result = wiz_device_init(WIZ_SPI_DEVICE);
     if (result != RT_EOK)
     {
         goto __exit;
     }
 
-    /* Add wiz to the netdev list */
-    ctlwizchip(CW_GET_ID, (void *)wiz_netdev_name);
-    wiz_netdev_add(wiz_netdev_name);
-
     /* WIZnet SPI device reset */
     wiz_reset();
     /* set WIZnet device read/write data callback */
     wiz_callback_register();
-    /* WIZnet chip configure initialize */
-    result = wiz_chip_cfg_init();
-    if (result != RT_EOK)
+
+    if (!wiz_is_exist())
     {
-        b_config = RT_FALSE;
+        result = -1;
+        LOG_E("Wiznet chip not detected");
+        goto __exit;
     }
+
+    /* Add wiz to the netdev list */
+    ctlwizchip(CW_GET_ID, (void *)wiz_netdev_name);
+    wiz_netdev_add(wiz_netdev_name);
+
+    /* WIZnet chip configure initialize */
+    wiz_chip_cfg_init();
+
     /* WIZnet socket initialize */
     wiz_socket_init();
     /* WIZnet network initialize */
-    result = wiz_network_init(b_config);
+    result = wiz_network_init(0);
     if (result != RT_EOK)
     {
         goto __exit;
@@ -796,6 +906,8 @@ int wiz_init(void)
         rt_thread_startup(tid);
     }
 
+    wiz_interrupt_init(WIZ_IRQ_PIN);
+
 __exit:
     if (result == RT_EOK)
     {

+ 5 - 105
src/wiz_device.c

@@ -24,91 +24,14 @@
 #define DBG_COLOR
 #include <rtdbg.h>
 
-extern int wiz_recv_notice_cb(int socket);
-extern int wiz_closed_notice_cb(int socket);
-
-static rt_mailbox_t wiz_rx_mb = RT_NULL;
 struct rt_spi_device *wiz_device = RT_NULL;
 
-static void wiz_isr(void)
-{
-    /* enter interrupt */
-    rt_interrupt_enter();
-
-    rt_mb_send(wiz_rx_mb, (rt_uint32_t) wiz_device);
-
-    /* leave interrupt */
-    rt_interrupt_leave();
-}
-
-static void wiz_data_thread_entry(void *parameter)
-{
-#define IR_SOCK(ch)         (0x01 << ch)   /**< check socket interrupt */
-
-    struct rt_spi_device* dev;
-
-    while (1)
-    {
-        if (rt_mb_recv(wiz_rx_mb, (rt_ubase_t*) &dev, RT_WAITING_FOREVER) == RT_EOK)
-        {
-            uint8_t ir, sir, sn_ir;
-            int8_t socket = -1;
-
-            /* get IR data than clean IR */
-            ir = getIR();
-            setIR(ir);
-
-            if ((ir & IR_CONFLICT) == IR_CONFLICT)
-            {
-                setIR(IR_CONFLICT);
-            }
-
-            if ((ir & IR_UNREACH) == IR_UNREACH)
-            {
-                setIR(IR_UNREACH);
-            }
-
-            /* get and process socket interrupt register */
-            sir = getSIR();
-
-            for (socket = 0; socket < 8; socket++)
-            {
-                sn_ir = 0;
-
-                if (sir & IR_SOCK(socket))
-                {
-                    /* save interrupt value*/
-                    sn_ir = getSn_IR(socket);
-
-                    if (sn_ir & Sn_IR_CON)
-                    {
-                        setSn_IR(socket, Sn_IR_CON);
-                    }
-                    if (sn_ir & Sn_IR_DISCON)
-                    {
-                        wiz_closed_notice_cb(socket);
-                        setSn_IR(socket, Sn_IR_DISCON);
-                    }
-                    if (sn_ir & Sn_IR_RECV)
-                    {
-                        wiz_recv_notice_cb(socket);
-                        setSn_IR(socket, Sn_IR_RECV);
-                    }
-                    if (sn_ir & Sn_IR_TIMEOUT)
-                    {
-                        /* deal with timeout event in the wiznet ioLibrary */
-                        //setSn_IR(socket, Sn_IR_TIMEOUT);
-                    }
-                }
-            }
-        }
-    }
-}
+#ifndef WIZ_SPI_FREQ_MAX
+#define WIZ_SPI_FREQ_MAX 10000000
+#endif
 
 static int wiz_spi_init(const char *spi_dev_name)
 {
-    rt_thread_t tid;
-
     RT_ASSERT(spi_dev_name);
 
     if (wiz_device != RT_NULL)
@@ -131,7 +54,7 @@ static int wiz_spi_init(const char *spi_dev_name)
         struct rt_spi_configuration cfg;
         cfg.data_width = 8;
         cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;  /* SPI Compatible Modes 0 */
-        cfg.max_hz = 40 * 1000 * 1000;                          /* SPI Interface with Clock Speeds Up to 40 MHz */
+        cfg.max_hz = WIZ_SPI_FREQ_MAX;                          /* SPI Interface with Clock Speeds Up to 40 MHz */
         rt_spi_configure(wiz_device, &cfg);
     }
 
@@ -141,25 +64,10 @@ static int wiz_spi_init(const char *spi_dev_name)
         return -RT_ERROR;
     }
 
-    /* initialize RX mailbox */
-    wiz_rx_mb = rt_mb_create("wiz_mb", WIZ_RX_MBOX_NUM, RT_IPC_FLAG_FIFO);
-    if (wiz_rx_mb == RT_NULL)
-    {
-        LOG_E("WIZnet create receive data mailbox error.");
-        return -RT_ENOMEM;
-    }
-
-    /* create WIZnet SPI RX thread  */
-    tid = rt_thread_create("wiz", wiz_data_thread_entry, RT_NULL, 512, RT_THREAD_PRIORITY_MAX / 6, 20);
-    if (tid != RT_NULL)
-    {
-        rt_thread_startup(tid);
-    }
-
     return RT_EOK;
 }
 
-int wiz_device_init(const char *spi_dev_name, rt_base_t rst_pin, rt_base_t isr_pin)
+int wiz_device_init(const char *spi_dev_name)
 {
     int result = RT_EOK;
 
@@ -171,13 +79,5 @@ int wiz_device_init(const char *spi_dev_name, rt_base_t rst_pin, rt_base_t isr_p
         return result;
     }
 
-    /* initialize reset pin */
-    rt_pin_mode(rst_pin, PIN_MODE_OUTPUT);
-
-    /* initialize interrupt pin */
-    rt_pin_mode(isr_pin, PIN_MODE_INPUT_PULLUP);
-    rt_pin_attach_irq(isr_pin, PIN_IRQ_MODE_FALLING, (void (*)(void*)) wiz_isr, RT_NULL);
-    rt_pin_irq_enable(isr_pin, PIN_IRQ_ENABLE);
-
     return RT_EOK;
 }