From 2e9b3d14397e7141d723a0a11da1a6f2b646c37c Mon Sep 17 00:00:00 2001
From: Davide Mor <davide.mor@skywarder.eu>
Date: Thu, 14 Sep 2023 11:15:44 +0200
Subject: [PATCH] [wiz5500] Added checkVersion to test for HW presence

---
 src/shared/drivers/WIZ5500/WIZ5500.cpp | 17 ++++++++---------
 src/shared/drivers/WIZ5500/WIZ5500.h   | 16 +++++++++++-----
 src/tests/drivers/test-wiz5500.cpp     |  4 +++-
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/src/shared/drivers/WIZ5500/WIZ5500.cpp b/src/shared/drivers/WIZ5500/WIZ5500.cpp
index 3cac50057..6b4176df2 100644
--- a/src/shared/drivers/WIZ5500/WIZ5500.cpp
+++ b/src/shared/drivers/WIZ5500/WIZ5500.cpp
@@ -82,6 +82,13 @@ void Wiz5500::setOnDestUnreachable(OnDestUnreachableCb cb)
     on_dest_unreachable = cb;
 }
 
+bool Wiz5500::checkVersion()
+{
+    Lock<FastMutex> l(mutex);
+
+    return spiRead8(0, Wiz::Common::REG_VERSIONR) == Wiz::VERSION;
+}
+
 Wiz5500::PhyState Wiz5500::getPhyState()
 {
     Lock<FastMutex> l(mutex);
@@ -94,16 +101,10 @@ Wiz5500::PhyState Wiz5500::getPhyState()
     return {full_duplex, based_100mbps, link_up};
 }
 
-bool Wiz5500::reset()
+void Wiz5500::reset()
 {
     Lock<FastMutex> l(mutex);
 
-    // First check that the device is actually present
-    if (spiRead8(0, Wiz::Common::REG_VERSIONR) != Wiz::VERSION)
-    {
-        return false;
-    }
-
     // Perform a software reset
     spiWrite8(0, Wiz::Common::REG_MR, 1 << 7);
     // Enable all socket interrupts
@@ -115,8 +116,6 @@ bool Wiz5500::reset()
     {
         spiWrite8(Wiz::getSocketRegBlock(i), Wiz::Socket::REG_MR, 0);
     }
-
-    return true;
 }
 
 void Wiz5500::handleINTn()
diff --git a/src/shared/drivers/WIZ5500/WIZ5500.h b/src/shared/drivers/WIZ5500/WIZ5500.h
index 50053ca4b..9d76cec26 100644
--- a/src/shared/drivers/WIZ5500/WIZ5500.h
+++ b/src/shared/drivers/WIZ5500/WIZ5500.h
@@ -110,6 +110,15 @@ public:
      */
     void setOnDestUnreachable(OnDestUnreachableCb cb);
 
+    /**
+     * @brief Checks the VERSION register.
+     * Can be used to detect device presence.
+     *
+     * @returns False if the device is not connected properly (SPI comunication
+     * failure).
+     */
+    bool checkVersion();
+
     /**
      * @brief Get current PHY state, can be used to poll link status, and wait
      * for link up.
@@ -121,12 +130,9 @@ public:
     /**
      * @brief Resets the device.
      * Performs a software resets, resetting all registers and closing all
-     * sockets. Also checks for hardware presence.
-     *
-     * @returns False if the device is not connected properly (SPI comunication
-     * failure).
+     * sockets.
      */
-    bool reset();
+    void reset();
 
     /**
      * @brief Handle an interrupt from INTn.
diff --git a/src/tests/drivers/test-wiz5500.cpp b/src/tests/drivers/test-wiz5500.cpp
index a5064910a..0fb14ebdd 100644
--- a/src/tests/drivers/test-wiz5500.cpp
+++ b/src/tests/drivers/test-wiz5500.cpp
@@ -270,13 +270,15 @@ int main()
                       SPI::ClockDivider::DIV_64);
 
     // Start the driver
-    if (!wiz->reset())
+    if (!wiz->checkVersion())
     {
         printf("[wiz5500] Wiz failed to start!\n");
         while (1)
             ;
     }
 
+    wiz->reset();
+
     wiz->setOnDestUnreachable(
         [](WizIp ip, uint16_t port)
         {
-- 
GitLab