karasevIA 2 лет назад
Родитель
Сommit
c0684d7388
5 измененных файлов с 195 добавлено и 168 удалено
  1. 6 0
      application.fam
  2. 181 164
      eth_worker.c
  3. 4 3
      eth_worker.h
  4. 1 0
      eth_worker_i.h
  5. 3 1
      finik_eth_app.c

+ 6 - 0
application.fam

@@ -14,6 +14,12 @@ App(
 	fap_icon_assets="images",
 	fap_private_libs=[
 		Lib(
+			cflags=[
+                "-Wunused-parameter",
+                "-Wunused-function",
+                "-Wredundant-decls",
+                "-Wunused-parameter",
+			],
 			name="ioLibrary_Driver",
 			fap_include_paths=[
                 "Ethernet/W5500",

+ 181 - 164
eth_worker.c

@@ -6,6 +6,7 @@
 #include "dhcp.h"
 #include "ping.h"
 #include "stm32wbxx_hal_gpio.h"
+#include "wizchip_conf.h"
 
 #define TAG "EthWorker"
 
@@ -18,7 +19,10 @@ EthWorker* eth_worker_alloc() {
     furi_thread_set_callback(eth_worker->thread, eth_worker_task);
     furi_thread_set_context(eth_worker->thread, eth_worker);
 
-    eth_worker_change_state(eth_worker, EthWorkerStateNotInited);
+    eth_worker_change_state(eth_worker, EthWorkerStateModuleInit);
+
+    //eth_worker->callback = eth_worker_change_state;
+
     return eth_worker;
 }
 
@@ -28,6 +32,11 @@ void eth_worker_free(EthWorker* eth_worker) {
     free(eth_worker);
 }
 
+void eth_worker_change_state(EthWorker* eth_worker, EthWorkerState state) {
+    furi_assert(eth_worker);
+    eth_worker->state = state;
+}
+
 /************************** Ethernet Worker Thread *****************************/
 
 int32_t eth_worker_task(void* context) {
@@ -38,6 +47,7 @@ int32_t eth_worker_task(void* context) {
     while(eth_worker->state != EthWorkerStateStop) {
         if(eth_worker->state == EthWorkerStateModuleInit) {
             eth_worker_w5500(eth_worker);
+            eth_worker_dhcp(eth_worker);
         }
         if(eth_worker->state == EthWorkerStateDHCP) {
             eth_worker_dhcp(eth_worker);
@@ -117,7 +127,7 @@ void W5500_WriteByte(uint8_t byte) {
 }
 
 void wait_ms(int ms) {
-    osDelay(ms);
+    furi_delay_ms(ms);
 }
 
 wiz_NetInfo gWIZNETINFO = {
@@ -163,140 +173,137 @@ void eth_worker_dhcp(EthWorker* eth_worker) {
     furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
     furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
 
-    while(eth_worker->state == EthWorkerStateDHCP) {
-        furi_hal_power_enable_otg();
-        eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
-        osDelay(1000);
-        furi_hal_gpio_write(&resetpin, false);
-        osDelay(10);
-        furi_hal_gpio_write(&resetpin, true);
+    furi_hal_power_enable_otg();
+    //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
+    furi_delay_ms(1000);
+    furi_hal_gpio_write(&resetpin, false);
+    furi_delay_ms(10);
+    furi_hal_gpio_write(&resetpin, true);
 
-        eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
+    //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
 
-        if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
-            FURI_LOG_I(TAG, "W5500 initialized fail");
-            eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
-            break;
-        }
+    if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
+        FURI_LOG_I(TAG, "W5500 initialized fail");
+        //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
+        while(1)
+            ;
+        //break;
+    }
 
-        FURI_LOG_I(TAG, "W5500 initialized success");
-        osDelay(200);
+    FURI_LOG_I(TAG, "W5500 initialized success");
+    furi_delay_ms(200);
 
-        wizchip_setnetinfo(&gWIZNETINFO);
-        FURI_LOG_I(TAG, "W5500 info setted 1");
+    wizchip_setnetinfo(&gWIZNETINFO);
+    FURI_LOG_I(TAG, "W5500 info setted 1");
 
-        setSHAR(gWIZNETINFO.mac);
-        FURI_LOG_I(TAG, "W5500 info setted 2");
+    setSHAR(gWIZNETINFO.mac);
+    FURI_LOG_I(TAG, "W5500 info setted 2");
 
-        //check phy status
-        do {
-            if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
-                FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
-            }
-            osDelay(1);
-        } while(temp == PHY_LINK_OFF);
-
-        FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
-        eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
-
-        osDelay(1000);
-
-        FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
-        reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
-
-        eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
-        if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
-            DHCP_init(DHCP_SOCKET, dhcp_buffer);
-
-            uint8_t dhcp_ret = DHCP_STOPPED;
-
-            while(
-                !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
-                  (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
-                dhcp_ret = DHCP_run();
-                switch(dhcp_ret) {
-                case DHCP_IP_ASSIGN:
-                case DHCP_IP_CHANGED:
-                case DHCP_IP_LEASED:
-                    getIPfromDHCP(gWIZNETINFO.ip);
-                    getGWfromDHCP(gWIZNETINFO.gw);
-                    getSNfromDHCP(gWIZNETINFO.sn);
-                    getDNSfromDHCP(gWIZNETINFO.dns);
-                    gWIZNETINFO.dhcp = NETINFO_DHCP;
-                    ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
-                    FURI_LOG_I(
-                        TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
-                    break;
-                case DHCP_FAILED:
-                    FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
-                    gWIZNETINFO.dhcp = NETINFO_STATIC;
-                    break;
-                }
-                osDelay(1);
+    //check phy status
+    do {
+        if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
+            FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
+        }
+        furi_delay_ms(1);
+    } while(temp == PHY_LINK_OFF);
+
+    FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
+    ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
+
+    furi_delay_ms(1000);
+
+    FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
+    reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
+
+    ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
+    if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
+        DHCP_init(DHCP_SOCKET, dhcp_buffer);
+
+        uint8_t dhcp_ret = DHCP_STOPPED;
+
+        while(
+            !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
+              (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
+            dhcp_ret = DHCP_run();
+            switch(dhcp_ret) {
+            case DHCP_IP_ASSIGN:
+            case DHCP_IP_CHANGED:
+            case DHCP_IP_LEASED:
+                getIPfromDHCP(gWIZNETINFO.ip);
+                getGWfromDHCP(gWIZNETINFO.gw);
+                getSNfromDHCP(gWIZNETINFO.sn);
+                getDNSfromDHCP(gWIZNETINFO.dns);
+                gWIZNETINFO.dhcp = NETINFO_DHCP;
+                ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
+                FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
+                break;
+            case DHCP_FAILED:
+                FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
+                gWIZNETINFO.dhcp = NETINFO_STATIC;
+                break;
             }
-
-            wizchip_getnetinfo(&gWIZNETINFO);
-            FURI_LOG_I(
-                TAG,
-                "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
-                gWIZNETINFO.mac[0],
-                gWIZNETINFO.mac[1],
-                gWIZNETINFO.mac[2],
-                gWIZNETINFO.mac[3],
-                gWIZNETINFO.mac[4],
-                gWIZNETINFO.mac[5]);
-            if(gWIZNETINFO.dhcp == NETINFO_DHCP)
-                FURI_LOG_I(TAG, "DHCP\n\r");
-            else
-                FURI_LOG_I(TAG, "Static IP\n\r");
-            FURI_LOG_I(
-                TAG,
-                "IP address : %d.%d.%d.%d\n\r",
-                gWIZNETINFO.ip[0],
-                gWIZNETINFO.ip[1],
-                gWIZNETINFO.ip[2],
-                gWIZNETINFO.ip[3]);
-            FURI_LOG_I(
-                TAG,
-                "SM Mask    : %d.%d.%d.%d\n\r",
-                gWIZNETINFO.sn[0],
-                gWIZNETINFO.sn[1],
-                gWIZNETINFO.sn[2],
-                gWIZNETINFO.sn[3]);
-            FURI_LOG_I(
-                TAG,
-                "Gate way   : %d.%d.%d.%d\n\r",
-                gWIZNETINFO.gw[0],
-                gWIZNETINFO.gw[1],
-                gWIZNETINFO.gw[2],
-                gWIZNETINFO.gw[3]);
-            FURI_LOG_I(
-                TAG,
-                "DNS Server : %d.%d.%d.%d\n\r",
-                gWIZNETINFO.dns[0],
-                gWIZNETINFO.dns[1],
-                gWIZNETINFO.dns[2],
-                gWIZNETINFO.dns[3]);
-            eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
-            osDelay(20000);
+            furi_delay_ms(1);
         }
 
+        wizchip_getnetinfo(&gWIZNETINFO);
+        FURI_LOG_I(
+            TAG,
+            "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
+            gWIZNETINFO.mac[0],
+            gWIZNETINFO.mac[1],
+            gWIZNETINFO.mac[2],
+            gWIZNETINFO.mac[3],
+            gWIZNETINFO.mac[4],
+            gWIZNETINFO.mac[5]);
+        if(gWIZNETINFO.dhcp == NETINFO_DHCP)
+            FURI_LOG_I(TAG, "DHCP\n\r");
+        else
+            FURI_LOG_I(TAG, "Static IP\n\r");
+        FURI_LOG_I(
+            TAG,
+            "IP address : %d.%d.%d.%d\n\r",
+            gWIZNETINFO.ip[0],
+            gWIZNETINFO.ip[1],
+            gWIZNETINFO.ip[2],
+            gWIZNETINFO.ip[3]);
+        FURI_LOG_I(
+            TAG,
+            "SM Mask    : %d.%d.%d.%d\n\r",
+            gWIZNETINFO.sn[0],
+            gWIZNETINFO.sn[1],
+            gWIZNETINFO.sn[2],
+            gWIZNETINFO.sn[3]);
+        FURI_LOG_I(
+            TAG,
+            "Gate way   : %d.%d.%d.%d\n\r",
+            gWIZNETINFO.gw[0],
+            gWIZNETINFO.gw[1],
+            gWIZNETINFO.gw[2],
+            gWIZNETINFO.gw[3]);
+        FURI_LOG_I(
+            TAG,
+            "DNS Server : %d.%d.%d.%d\n\r",
+            gWIZNETINFO.dns[0],
+            gWIZNETINFO.dns[1],
+            gWIZNETINFO.dns[2],
+            gWIZNETINFO.dns[3]);
+        ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
+        furi_delay_ms(20000);
+
         uint8_t pDestaddr[4] = {8, 8, 8, 8};
         uint8_t tmp = ping_auto(1, pDestaddr);
         //tmp = ping_count(0,3,pDestaddr);
         if(tmp == SUCCESS) {
-            eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
+            ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
             FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
         } else {
-            eth_worker->callback(EthCustomEventPingError, eth_worker->context);
+            ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
             FURI_LOG_I(TAG, "----------ERROR  = %d----------\r\n", tmp);
         }
-        osDelay(3000);
-
-        osDelay(2000);
-        eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
+        furi_delay_ms(3000);
 
-        break;
+        furi_delay_ms(2000);
+        ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
     }
 
     furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
@@ -328,68 +335,78 @@ void eth_worker_w5500(EthWorker* eth_worker) {
     //uint8_t temp;
 
     FURI_LOG_I(TAG, "Ehtping_Init");
+    furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
+    uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
+    FURI_LOG_I(TAG, "Registering W5500 callbacks");
 
-    while(eth_worker->state == EthWorkerStateW5500) {
-        furi_hal_power_enable_otg();
-        osDelay(1000);
-        eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
-        osDelay(2000);
-        furi_hal_gpio_write(&resetpin, false);
-        osDelay(10);
-        furi_hal_gpio_write(&resetpin, true);
-
-        eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
-
-        if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
-            FURI_LOG_I(TAG, "W5500 initialized fail.\r\n");
-            eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
-            break;
-        }
+    reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
+    reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
+    reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
 
-        FURI_LOG_I(TAG, "W5500 initialized success.\r\n");
-        osDelay(2000);
+    FURI_LOG_I(TAG, "Registered W5500 callbacks");
 
-        wizchip_setnetinfo(&gWIZNETINFO);
-        FURI_LOG_I(TAG, "W5500 info setted 1.\r\n");
+    GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
+    furi_hal_gpio_write(&resetpin, true);
+    furi_hal_gpio_write(&cspin, true);
+    furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
+    furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
 
-        setSHAR(gWIZNETINFO.mac);
-        FURI_LOG_I(TAG, "W5500 info setted 2.\r\n");
+    FURI_LOG_I(TAG, "GPIO inited");
 
-        //check phy status
-        //do
-        //{
-        //    if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1)
-        //    {
-        //        FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
-        //    }
-        //    osDelay(1);
-        //} while (temp == PHY_LINK_OFF);
+    furi_hal_power_enable_otg();
+    furi_delay_ms(1000);
+    //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
+    furi_delay_ms(2000);
+    furi_hal_gpio_write(&resetpin, false);
+    furi_delay_ms(10);
+    furi_hal_gpio_write(&resetpin, true);
 
-        //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
-        //eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
+    FURI_LOG_I(TAG, "GPIO used");
 
-        FURI_LOG_I(TAG, "W5500 before delay\r\n");
+    //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
 
-        osDelay(2000);
+    if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
+        FURI_LOG_I(TAG, "W5500 initialized fail.\r\n");
+        //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
+    }
 
-        FURI_LOG_I(TAG, "W5500 after delay\r\n");
+    FURI_LOG_I(TAG, "W5500 initialized success.\r\n");
+    furi_delay_ms(2000);
 
-        furi_hal_power_disable_otg();
+    wizchip_setnetinfo(&gWIZNETINFO);
+    FURI_LOG_I(TAG, "W5500 info setted 1.\r\n");
 
-        FURI_LOG_I(TAG, "W5500 power off\r\n");
+    setSHAR(gWIZNETINFO.mac);
+    FURI_LOG_I(TAG, "W5500 info setted 2.\r\n");
 
-        eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
+    //check phy status
+    //do
+    //{
+    //    if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1)
+    //    {
+    //        FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
+    //    }
+    //    furi_delay_ms(1);
+    //} while (temp == PHY_LINK_OFF);
 
-        osDelay(2000);
+    //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
+    ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
 
-        eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context);
+    FURI_LOG_I(TAG, "W5500 before delay\r\n");
 
-        while(eth_worker->state == EthWorkerStateW5500) {
-            osDelay(10);
-        }
+    furi_delay_ms(2000);
 
-        break;
-    }
+    FURI_LOG_I(TAG, "W5500 after delay\r\n");
+
+    //furi_hal_power_disable_otg();
+
+    //FURI_LOG_I(TAG, "W5500 power off\r\n");
+
+    ////eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
+
+    furi_delay_ms(2000);
+
+    ////eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context);
 
     furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
 }

+ 4 - 3
eth_worker.h

@@ -1,7 +1,5 @@
 #pragma once
 
-#include "eth_device.h"
-
 typedef struct EthWorker EthWorker;
 
 typedef enum {
@@ -25,6 +23,7 @@ typedef enum {
 typedef enum {
     EthCustomEventUpdate = 0,
     EthCustomEventModuleInit,
+    EthCustomEventModuleError,
     EthCustomEventModulePowerOn,
     EthCustomEventModuleConnect,
     EthCustomEventMACInit,
@@ -50,4 +49,6 @@ void eth_worker_start(
     EthWorkerCallback callback,
     void* context);
 
-void eth_worker_stop(EthWorker* eth_worker);
+void eth_worker_stop(EthWorker* eth_worker);
+void eth_worker_dhcp(EthWorker* eth_worker);
+void eth_worker_w5500(EthWorker* eth_worker);

+ 1 - 0
eth_worker_i.h

@@ -1,5 +1,6 @@
 #pragma once
 
+#include <furi.h>
 #include "eth_worker.h"
 
 struct EthWorker {

+ 3 - 1
finik_eth_app.c

@@ -55,7 +55,9 @@ FinikEthApp* finik_eth_app_alloc() {
 
     app->notifications = furi_record_open(RECORD_NOTIFICATION);
 
-    //app->eth_worker =
+    app->eth_worker = eth_worker_alloc();
+    
+    eth_worker_task(app->eth_worker);
 
     return app;
 }