From e283f4169f8db20d3ee9e97b8ad16ab522abb4c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=B2=20Caruso?= <niccolo.caruso@skywarder.eu>
Date: Thu, 27 Feb 2025 08:16:47 +0100
Subject: [PATCH] [Wiz5500] waitForINTn now returns a TimedWaitResult

Now instead of checking ourselves if the timeout expired we directly return the result from waitForINTn.
Also the hard timeout was not checked for expired timeout, while now does.
---
 src/shared/drivers/WIZ5500/WIZ5500.cpp | 14 ++++++++------
 src/shared/drivers/WIZ5500/WIZ5500.h   |  3 ++-
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/shared/drivers/WIZ5500/WIZ5500.cpp b/src/shared/drivers/WIZ5500/WIZ5500.cpp
index 721d0fa13..4f0159743 100644
--- a/src/shared/drivers/WIZ5500/WIZ5500.cpp
+++ b/src/shared/drivers/WIZ5500/WIZ5500.cpp
@@ -407,7 +407,7 @@ void Wiz5500::close(int sock_n, int timeout)
     socket_infos[sock_n].mode = Wiz5500::SocketMode::CLOSED;
 }
 
-void Wiz5500::waitForINTn(Lock<FastMutex>& l, long long until)
+TimedWaitResult Wiz5500::waitForINTn(Lock<FastMutex>& l, long long until)
 {
     TimedWaitResult result = TimedWaitResult::NoTimeout;
 
@@ -415,6 +415,7 @@ void Wiz5500::waitForINTn(Lock<FastMutex>& l, long long until)
     FastInterruptDisableLock il;
     while (intn.value() != 0 && result == TimedWaitResult::NoTimeout)
         result = Kernel::Thread::IRQenableIrqAndTimedWaitMs(il, until);
+    return result;
 }
 
 int Wiz5500::waitForSocketIrq(miosix::Lock<miosix::FastMutex>& l, int sock_n,
@@ -533,14 +534,15 @@ TimedWaitResult Wiz5500::runInterruptServiceRoutine(Lock<FastMutex>& l,
     long long start = Kernel::getOldTick();
     if (until == -1)
     {
-        waitForINTn(l, start + INTN_TIMEOUT);
+        // Wait for interrupts and check if we run out of time
+        if (waitForINTn(l, start + INTN_TIMEOUT) == TimedWaitResult::Timeout)
+            return TimedWaitResult::Timeout;
     }
     else
     {
-        waitForINTn(l, std::min(start + INTN_TIMEOUT, until));
-
-        // Did we run out of time?
-        if (Kernel::getOldTick() >= until)
+        // Wait for interrupts and check if we run out of time
+        if (waitForINTn(l, std::min(start + INTN_TIMEOUT, until)) ==
+            TimedWaitResult::Timeout)
             return TimedWaitResult::Timeout;
     }
 
diff --git a/src/shared/drivers/WIZ5500/WIZ5500.h b/src/shared/drivers/WIZ5500/WIZ5500.h
index 11d627cc2..c28d1b005 100644
--- a/src/shared/drivers/WIZ5500/WIZ5500.h
+++ b/src/shared/drivers/WIZ5500/WIZ5500.h
@@ -252,7 +252,8 @@ private:
     static constexpr int NUM_THREAD_WAIT_INFOS = 16;
     static constexpr int NUM_SOCKETS           = 8;
 
-    void waitForINTn(miosix::Lock<miosix::FastMutex>& l, long long until);
+    miosix::TimedWaitResult waitForINTn(miosix::Lock<miosix::FastMutex>& l,
+                                        long long until);
     int waitForSocketIrq(miosix::Lock<miosix::FastMutex>& l, int sock_n,
                          uint8_t irq_mask, int timeout);
 
-- 
GitLab