From d1da970663224c070a7e33e53ebbab257da278c7 Mon Sep 17 00:00:00 2001
From: cgottard <carlo.gottardo@cern.ch>
Date: Tue, 5 Nov 2024 11:39:03 +0100
Subject: [PATCH] fix for FF configuration button

---
 controllers/sfp.py | 10 +++++++++-
 models/sfp.py      | 38 ++++++++++++++++++++++++++++++++++++++
 routes/__init__.py |  1 +
 util/sfp.py        | 37 ++++++-------------------------------
 4 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/controllers/sfp.py b/controllers/sfp.py
index 1c9fdd3..2fbb587 100644
--- a/controllers/sfp.py
+++ b/controllers/sfp.py
@@ -36,10 +36,18 @@ class SFP_Controller():
 
 		return redirect(f"/{self.name}")
 
+
+	def configure_all(self):
+		if self.skip_io:
+			return redirect(f"/{self.name}")
+
+		self.model.configure_all()
+		return redirect(f"/{self.name}")
+
+
 	def reset(self):
 		if self.skip_io:
 			return redirect(f"/{self.name}")
 	
 		self.model.reset()
-
 		return redirect(f"/{self.name}")
diff --git a/models/sfp.py b/models/sfp.py
index 42c6028..9eaa613 100644
--- a/models/sfp.py
+++ b/models/sfp.py
@@ -69,12 +69,50 @@ class SFP(SelfTestExecutor):
             msg = "OK"
 
         return { "status" : msg_level, "result" : msg}
+
     
     def configure(self, device_name, config):
         cfg = self.devices[device_name]
         device = SFP_Device(cfg)
         device.configure(config)
 
+
+    def configure_all(self):
+        B04_link_speed = 0
+        Y12_link_speed = 0
+
+        #Read all devices to determine link speeds
+        for dev_cfg in self.devices.values():
+            device = SFP_Device(dev_cfg)
+            part = device.get_part_number()
+            if ("B04" in part and B04_link_speed == 0):
+                if "B0428" in part:
+                    B04_link_speed = 28
+                elif "B0425" in part:
+                    B04_link_speed = 25
+                elif "B0414" in part:
+                    B04_link_speed = 14
+            if ("Y12" in part and Y12_link_speed == 0):
+                if ("BY12" in part or "Y1216" in part):
+                    Y12_link_speed = 16
+                if "Y1225" in part:
+                    Y12_link_speed = 25
+
+        if (Y12_link_speed == 16 and B04_link_speed == 14):
+            devmem_write(0x20100020008, int(0x00).to_bytes(4, 'little'))
+
+        elif (Y12_link_speed == 16 and B04_link_speed >= 25):
+            devmem_write(0x20100020008, int(0x10).to_bytes(4, 'little'))
+
+        elif  (Y12_link_speed == 25 and B04_link_speed == 14):
+            devmem_write(0x20100020008, int(0x01).to_bytes(4, 'little'))
+
+        elif (Y12_link_speed == 25 and B04_link_speed >= 25):
+            devmem_write(0x20100020008, int(0x11).to_bytes(4, 'little'))
+        else:
+            raise ValueError(f"Unsupported link speed. Detected: Y12 {Y12_link_speed}G B04 {B04_link_speed}G")
+
+
     def reset(self):
         devmem_write(0x20100020000, int(0xFFFFFFFF).to_bytes(4, 'little'))
         time.sleep(0.1)
diff --git a/routes/__init__.py b/routes/__init__.py
index def5f40..6869c3b 100644
--- a/routes/__init__.py
+++ b/routes/__init__.py
@@ -201,6 +201,7 @@ def create_app(name, config):
         @app.route("/configure_firefly", methods=["POST"])
         def configure_firefly():
             si5345.load_default_preset()
+            sfp.configure_all()
             sfp.reset()
             return redirect(request.referrer)
 
diff --git a/util/sfp.py b/util/sfp.py
index ee9f323..daeead9 100644
--- a/util/sfp.py
+++ b/util/sfp.py
@@ -240,42 +240,17 @@ class SFP():
                 "error" : str(err)
             } 
             return data
+
+
+    def get_part_number(self):
+        with self.open_device():
+            return self.__read_field("part_number")
+
     
     def configure(self, config):
         with self.open_device():
             part_number = self.__read_field("part_number")
 
-            # Configure link speeds according to part number
-            B04_link_speed = 0
-            if "B04" in part_number:
-                if "B0428" in part_number:
-                    B04_link_speed = 28
-                elif "B0425" in part_number:
-                    B04_link_speed = 25
-                elif "B0414" in part_number:
-                    B04_link_speed = 14
-
-            Y12_link_speed = 0
-            if "Y12" in part_number:
-                if ("BY12" in part_number or "Y1216" in part_numer):
-                    Y12_link_speed = 16
-                if "Y1225" in part_number:
-                    Y12_link_speed = 25
-
-            if (Y12_link_speed == 16 and B04_link_speed == 14):
-                pass
-
-            elif (Y12_link_speed == 16 and B04_link_speed >= 25):
-                devmem_write(0x20100020008, int(0x10).to_bytes(4, 'little'))
-
-            elif  (Y12_link_speed == 25 and B04_link_speed == 14):
-                devmem_write(0x20100020008, int(0x01).to_bytes(4, 'little'))
-
-            elif (Y12_link_speed == 25 and B04_link_speed >= 25):
-                devmem_write(0x20100020008, int(0x11).to_bytes(4, 'little'))
-            else:
-                raise ValueError(f"Unsupported link speed. Detected: Y12 {Y12_link_speed}G B04 {B04_link_speed}G")
-
             # Only applies to the 28G transceivers
             if part_number == "B042804005170":
                 cdr = 0
-- 
GitLab