From 0119994969133435498aa6cf6fd6d1ef93cb23b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu>
Date: Wed, 2 Oct 2024 15:17:05 +0200
Subject: [PATCH] [AutomatedAntennas] Implement ethernet support

---
 cmake/dependencies.cmake                      |  1 +
 src/boards/Groundstation/Automated/Hub.cpp    |  4 +-
 .../Automated/Ports/Ethernet.cpp              | 59 +++++++++++++++++++
 .../Groundstation/Automated/Ports/Ethernet.h  | 38 ++++++++++++
 .../Groundstation/Base/Ports/Ethernet.cpp     | 19 +++---
 .../Automated/automated-antennas-entry.cpp    |  4 ++
 6 files changed, 115 insertions(+), 10 deletions(-)
 create mode 100644 src/boards/Groundstation/Automated/Ports/Ethernet.cpp
 create mode 100644 src/boards/Groundstation/Automated/Ports/Ethernet.h

diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake
index c5e3e55e1..57ac1915f 100644
--- a/cmake/dependencies.cmake
+++ b/cmake/dependencies.cmake
@@ -143,6 +143,7 @@ set(GROUNDSTATION_AUTOMATED
     src/boards/Groundstation/Automated/Radio/Radio.cpp
     src/boards/Groundstation/Automated/Radio/RadioStatus.cpp
     src/boards/Groundstation/Automated/Follower/Follower.cpp
+    src/boards/Groundstation/Automated/Ports/Ethernet.cpp
     src/boards/Groundstation/Automated/Hub.cpp
 )
 
diff --git a/src/boards/Groundstation/Automated/Hub.cpp b/src/boards/Groundstation/Automated/Hub.cpp
index 688f94964..25d724f23 100644
--- a/src/boards/Groundstation/Automated/Hub.cpp
+++ b/src/boards/Groundstation/Automated/Hub.cpp
@@ -23,6 +23,7 @@
 #include "Hub.h"
 
 #include <Groundstation/Automated/Follower/Follower.h>
+#include <Groundstation/Automated/Ports/Ethernet.h>
 #include <Groundstation/Automated/Radio/Radio.h>
 #include <Groundstation/Automated/Radio/RadioStatus.h>
 #include <Groundstation/Common/Config/GeneralConfig.h>
@@ -95,7 +96,8 @@ void Hub::dispatchIncomingMsg(const mavlink_message_t& msg)
         lastRocketGpsState = gpsState;
     }
 
-    // TODO: Add UDP dispatch
+    Ethernet* ethernet = ModuleManager::getInstance().get<Ethernet>();
+    ethernet->sendMsg(msg);
 }
 
 /**
diff --git a/src/boards/Groundstation/Automated/Ports/Ethernet.cpp b/src/boards/Groundstation/Automated/Ports/Ethernet.cpp
new file mode 100644
index 000000000..3636012af
--- /dev/null
+++ b/src/boards/Groundstation/Automated/Ports/Ethernet.cpp
@@ -0,0 +1,59 @@
+/* Copyright (c) 2023 Skyward Experimental Rocketry
+ * Author: Davide Mor
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "Ethernet.h"
+
+#include <Groundstation/Automated/Buses.h>
+#include <interfaces-impl/hwmapping.h>
+
+using namespace Groundstation;
+using namespace Antennas;
+using namespace Boardcore;
+using namespace miosix;
+
+void __attribute__((used)) MIOSIX_ETHERNET_IRQ()
+{
+    ModuleManager::getInstance().get<Ethernet>()->handleINTn();
+}
+
+bool Ethernet::start()
+{
+    std::unique_ptr<Wiz5500> wiz5500 = std::make_unique<Wiz5500>(
+        ModuleManager::getInstance().get<Buses>()->ethernet_bus,
+        ethernet::cs::getPin(), ethernet::intr::getPin(),
+        SPI::ClockDivider::DIV_64);
+
+    // First check if the device is even connected
+    bool present = wiz5500->checkVersion();
+
+    if (!present)
+    {
+        return false;
+    }
+
+    if (!EthernetBase::start(std::move(wiz5500)))
+    {
+        return false;
+    }
+
+    return true;
+}
diff --git a/src/boards/Groundstation/Automated/Ports/Ethernet.h b/src/boards/Groundstation/Automated/Ports/Ethernet.h
new file mode 100644
index 000000000..a49803d78
--- /dev/null
+++ b/src/boards/Groundstation/Automated/Ports/Ethernet.h
@@ -0,0 +1,38 @@
+/* Copyright (c) 2023 Skyward Experimental Rocketry
+ * Author: Davide Mor
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include <Groundstation/Common/Ports/EthernetBase.h>
+
+#include <utils/ModuleManager/ModuleManager.hpp>
+
+namespace Antennas
+{
+
+class Ethernet : public Groundstation::EthernetBase, public Boardcore::Module
+{
+public:
+    [[nodiscard]] bool start();
+};
+
+}  // namespace Antennas
diff --git a/src/boards/Groundstation/Base/Ports/Ethernet.cpp b/src/boards/Groundstation/Base/Ports/Ethernet.cpp
index 94e80dd04..77102bdc1 100644
--- a/src/boards/Groundstation/Base/Ports/Ethernet.cpp
+++ b/src/boards/Groundstation/Base/Ports/Ethernet.cpp
@@ -22,12 +22,12 @@
 
 #include "Ethernet.h"
 
-#include <Groundstation/Base/BoardStatus.h>
-#include <Groundstation/Base/Buses.h>
+#include <Groundstation/Automated/Buses.h>
 #include <interfaces-impl/hwmapping.h>
 
 using namespace Groundstation;
 using namespace GroundstationBase;
+using namespace Antennas;
 using namespace Boardcore;
 using namespace miosix;
 
@@ -46,14 +46,15 @@ bool Ethernet::start()
     // First check if the device is even connected
     bool present = wiz5500->checkVersion();
 
-    ModuleManager::getInstance().get<BoardStatus>()->setEthernetPresent(
-        present);
+    if (!present)
+    {
+        return false;
+    }
 
-    if(present) {
-        if(!EthernetBase::start(std::move(wiz5500))) {
-            return false;
-        }
+    if (!EthernetBase::start(std::move(wiz5500)))
+    {
+        return false;
     }
 
     return true;
-}
\ No newline at end of file
+}
diff --git a/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp b/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp
index ac0f5ee69..726dc29a0 100644
--- a/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp
+++ b/src/entrypoints/Groundstation/Automated/automated-antennas-entry.cpp
@@ -24,6 +24,7 @@
 #include <Groundstation/Automated/Buses.h>
 #include <Groundstation/Automated/Follower/Follower.h>
 #include <Groundstation/Automated/Hub.h>
+#include <Groundstation/Automated/Ports/Ethernet.h>
 #include <Groundstation/Automated/Radio/Radio.h>
 #include <Groundstation/Automated/Radio/RadioStatus.h>
 #include <Groundstation/Automated/Sensors/Sensors.h>
@@ -175,6 +176,7 @@ int main()
     Actuators *actuators      = new Actuators();
     Sensors *sensors          = new Sensors();
     Follower *follower        = new Follower();
+    Ethernet *ethernet        = new Ethernet();
 
     // Inserting Modules
     {
@@ -186,6 +188,7 @@ int main()
         ok &= modules.insert(radio_status);
         ok &= modules.insert(actuators);
         ok &= modules.insert(sensors);
+        ok &= modules.insert(ethernet);
 
         // If insertion failed, stop right here
         if (!ok)
@@ -207,6 +210,7 @@ int main()
         START_MODULE("Scheduler", [&] { return scheduler->start(); });
         START_MODULE("Serial", [&] { return serial->start(); });
         START_MODULE("Main Radio", [&] { return radio_main->start(); });
+        START_MODULE("Ethernet", [&] { return ethernet->start(); });
         START_MODULE("Radio Status", [&] { return radio_status->start(); });
         START_MODULE("Sensors", [&] { return sensors->start(); });
         actuators->start();
-- 
GitLab