I messed up the merge, so pushing it over to another repo so I don't lose it. Will tidy up and remove later

Dependencies:   BufferedSerial FatFileSystemCpp mbed

Files at this revision

API Documentation at this revision

Comitter:
AndyA
Date:
Tue Aug 03 14:33:40 2021 +0000
Parent:
29:b0eaeefa4e63
Child:
33:18e9f1d4be0d
Child:
34:c864a0c67dbf
Child:
43:d5b2e3514564
Commit message:
Moved ethernet to a different thread

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jul 21 09:07:53 2021 +0000
+++ b/main.cpp	Tue Aug 03 14:33:40 2021 +0000
@@ -17,7 +17,7 @@
     n = 0 - Preston
     n = 1 - Fuji passive listen mode (skycam)
     n = 2 - Fuji active mode
-
+    n = 3 - Canon
 
 FreeD_Port=pppp
     Sets the UDP port for FreeD network output.
@@ -44,11 +44,6 @@
 #include "EthernetInterface.h"
 
 //#define enableUSBStick
-
-EthernetInterface eth;
-UDPSocket* FreeDSocket = NULL;
-Endpoint FreeDTarget;
-
 //#define enableFakePPF
 
 // delay transmit to n ms after the frame
@@ -178,6 +173,8 @@
 struct outputFormat_s packetOut;
 struct D1MsgFormat_s fdPacket;
 
+volatile bool EthTxNow = false;
+
 void filterOff(void)
 {
     VIPS.EnableSmoothing(false);
@@ -439,33 +436,26 @@
 
 void UDPTxFreeD()
 {
-    if (!FreeDSocket) {
-        pc.printf("No UDP socket\n");
-        return;
-    }
-    int result = FreeDSocket->sendTo(FreeDTarget,(char *)&fdPacket, sizeof(struct D1MsgFormat_s));
-    if (result == sizeof(struct D1MsgFormat_s))
-        return;
-    if (result > 0)
-        pc.printf("UDP Tx was short???\r\n");
-    else if (result == 0)
-        pc.printf("UDP Tx sent 0 bytes???\r\n");
-    else
-        pc.printf("UDP Tx Failed! (%d)\r\n", result);
+    EthTxNow = true;
 }
 
 void sendPosition(position *posPtr)
 {
+    static bool NoDataWarn = true;
     if (!posPtr) {
-        pc.puts("No VIPS Data");
-    }
+        if (NoDataWarn) { // only warn once per dropout
+            pc.puts("No VIPS Data\r\n");
+            NoDataWarn = false;
+        }
+    } else
+        NoDataWarn= true;
 
 
-    // FreeD could be output by two different methods so calculate first if needed rather than at Tx.
+// FreeD could be output by two different methods so calculate first if needed rather than at Tx.
     if ((UserSettings.SerialOutMode==mode_FreeD) || UserSettings.UDPPort)
         createFreeDPacket(posPtr);
 
-    // Network is faster so send that first.
+// Network is faster so send that first.
     if (UserSettings.UDPPort) {
         UDPTxFreeD();
     }
@@ -608,11 +598,84 @@
     }
 }
 
+void ethernetTask(void const* settings)
+{
+
+    EthernetInterface eth;
+    UDPSocket* FreeDSocket = NULL;
+    Endpoint FreeDTarget;
+
+    UserSettings_t *settingsPtr = (UserSettings_t *) settings;
+
+    pc.puts("Ethernet task startup\r\n");
+    if (!settingsPtr->UDPPort) {
+        pc.puts("No UDP port set. Ethernet task exiting\r\n");
+        return;
+    }
+    int Result = -1;
+    while (Result != 0) {
+        if ((UserSettings.IPAddress[0] != 0) && (UserSettings.Gateway[0] != 0) && (UserSettings.Subnet[0] != 0)) {
+            pc.puts("Static IP init attempt\r\n");
+            Result = eth.init(UserSettings.IPAddress, UserSettings.Subnet, UserSettings.Gateway);
+        } else {
+            pc.puts("Dynamic IP init attempt\r\n");
+            Result = eth.init();
+        }
+
+        if (Result) {
+            pc.puts("Ethernet init failed\r\n");
+            Thread::wait(500);
+        }
+    }
+    pc.puts("Ethernet init complete\r\n");
+
+    while (true) {
+
+        Result = -1;
+        while (Result != 0) {
+            Result = eth.connect();
+            if (Result) {
+                pc.puts("Ethernet connect failed\r\n");
+                Thread::wait(500);
+            }
+        }
+        pc.puts("Ethernet connect complete\r\n");
+
+        if (FreeDSocket)
+            delete FreeDSocket;
+        FreeDSocket = new UDPSocket();
+        FreeDSocket->init();
+        FreeDSocket->set_broadcasting(true);
+        FreeDTarget.set_address(0xFFFFFFFF, UserSettings.UDPPort);
+
+        while (true) {
+            if (EthTxNow) {
+                EthTxNow = false;
+                if (!FreeDSocket) {
+                    pc.puts("No UDP socket\r\n");
+                    break;
+                }
+                Result = FreeDSocket->sendTo(FreeDTarget,(char *)&fdPacket, sizeof(struct D1MsgFormat_s));
+                if (Result != sizeof(struct D1MsgFormat_s)) {
+                    pc.puts("UDP Tx Failed!\r\n");
+                    break;
+                }
+//                pc.puts("UDP Tx\r\n");
+            } else
+                Thread::wait(5);
+        }
+        eth.disconnect();
+        pc.puts("Attempting to restart network\r\n");
+    }
+    pc.puts("Eth Task ending. Should never happen!\r\n");
+}
+
+
 
 int main()
 {
     pc.baud(115200);
-    pc.printf("\r\n\r\nStartup - v0.8\r\n");
+    pc.printf("\r\n\r\nStartup - v0.9\r\n");
     setRED();
 
     UserSettings.FIZmode = formatPreston;
@@ -648,41 +711,7 @@
 //    FIZPort = new FIZDigiPowerActive(p9, p10);
     COM1.baud(115200); // VIPS port
 
-    if (UserSettings.UDPPort) {
-        int Result = -1;
-        while (Result != 0) {
-
-            if ((UserSettings.IPAddress[0] != 0) && (UserSettings.Gateway[0] != 0) && (UserSettings.Subnet[0] != 0)) {
-                Result = eth.init(UserSettings.IPAddress, UserSettings.Subnet, UserSettings.Gateway);
-                pc.printf("Performing ethernet init with static IP address of %s\r\n",UserSettings.IPAddress);
-            } else {
-                Result = eth.init();
-                pc.printf("Performing ethernet init with DHCP\r\n");
-            }
-
-            if (Result) {
-                pc.printf("Ethernet init failed\r\n");
-                wait(1);
-            }
-        }
-        Result = -1;
-        while (Result != 0) {
-            Result = eth.connect();
-            if (Result) {
-                pc.printf("Ethernet connect failed: %d\r\n", Result);
-                wait(1);
-            }
-        }
-        char *ipAddress = eth.getIPAddress();
-        if (ipAddress && (strlen(ipAddress) > 4)) {
-            pc.printf("IP Address is %s\r\n",eth.getIPAddress());
-        }
-        FreeDSocket = new UDPSocket();
-        FreeDSocket->init();
-        FreeDSocket->set_broadcasting(true);
-//        FreeDSocket->bind(UserSettings.UDPPort);
-        FreeDTarget.set_address(0xFFFFFFFF, UserSettings.UDPPort);
-    }
+    Thread serialTask(ethernetTask, &UserSettings, osPriorityNormal, 256 * 4);
 
     led1 = 0;
     inputTimer.reset();