From c612ca69ce339efee64c96787b80d494ef89c203 Mon Sep 17 00:00:00 2001
From: Gijs Schoonderbeek <schoonderbeek@astron.nl>
Date: Tue, 10 Jan 2023 17:45:28 +0100
Subject: [PATCH] Added check backplane-ID

---
 APSCT_I2C.py        |  6 ++++++
 apsct_lib.py        | 49 ++++++++++++++++++++++++++++++++++++++-------
 production_apsct.py | 18 ++++++++++-------
 3 files changed, 59 insertions(+), 14 deletions(-)

diff --git a/APSCT_I2C.py b/APSCT_I2C.py
index 1715b6f..f9625df 100644
--- a/APSCT_I2C.py
+++ b/APSCT_I2C.py
@@ -64,3 +64,9 @@ EEPROM = 0x50
 # I2C switch addresses
 #
 i2c_switch_addr = [0x70, 0x71, 0x72, 0x73]
+
+
+#
+# ID Pins 
+#
+ID_PINS = [8, 7, 12, 16, 20, 21]
diff --git a/apsct_lib.py b/apsct_lib.py
index 32a4f15..eee2b32 100644
--- a/apsct_lib.py
+++ b/apsct_lib.py
@@ -45,6 +45,7 @@ class ApsctClass:
         self.i2cswitch = []
         for addr in APSCT_I2C.i2c_switch_addr:
             self.i2cswitch.append(I2cSwitch(address=addr))
+        self.apsct_id = ApsctId()
 
     def read_IO_expanderis(self):
         #
@@ -107,6 +108,7 @@ class ApsctClass:
         #
         result = self.sensors.check_values()
         result = result & self.pps.check_timing()
+        result = result & self.apsct_id.check_id()
         for i2c_switch in self.i2cswitch:
             result = result & i2c_switch.check_switch(data=random.randint(0,2**8))
         if self.frequency == "200MHz":
@@ -241,24 +243,24 @@ class PllClass:
             stri = "Reg nr 0x{:0>2x} value: 0x{:0>2x}".format(cnt, int(ret_value[start:start+8], 2))
             print(stri)
 
-    def read_lock(self):
+    def read_lock(self, PRINT_ON=True):
         #
         # Read lock status
         #
         ret_value = self.Read_byte_PLL(0x00, nof_bytes=1)
         status_pll = int(ret_value, 2)
+        stri = ""
         if status_pll == 0x04:
             self.lock = True
-            if DEBUG:
-                print("PLL in lock")
+            stri = f"PLL {self.frequency} is in lock"
         elif (status_pll & 0x10) > 0:
             self.lock = False
-            if DEBUG:
-                print("Not Locked --> No 10 MHz ref")
+            stri = f"PLL {self.frequency} Not Locked --> No 10 MHz ref"
         else:
             self.lock = False
-            if DEBUG:
-                print("Not locked --> PLL Error")
+            stri = f"PLL {self.frequency} Not locked --> PLL Error"
+        if PRINT_ON:
+            print(stri)
 
     def read_lol(self):
         #
@@ -526,3 +528,36 @@ class I2cSwitch:
             else:
                 print(f"ERROR wrote 0x{data:02X} read back 0x{read_value_int:02X}")
                 return False
+
+
+class ApsctId:
+    #
+    # Class to check a I2C-switch 
+    #
+    def __init__(self):
+        #
+        # APSCT Backplane ID 
+        #
+        self.id = 9999
+        gpio.setmode(gpio.BCM)
+        for pin in APSCT_I2C.ID_PINS:
+            gpio.setup(pin, gpio.IN)
+
+    def read_id(self):
+        id = 0
+        for pin in APSCT_I2C.ID_PINS:
+            id = id * 2
+            bit = gpio.input(8)
+            id = id + bit
+        self.id = id
+        return self.id
+
+    def check_id(self):
+        self.read_id()
+        if self.id == 63:
+            print(f"OK   : Back ID is 0x{self.id:02X}")
+            return True
+        else:
+            print(f"ERROR : Back ID is 0x{self.id:02X} expected 0x{63:2X}")
+            return False
+            return True 
diff --git a/production_apsct.py b/production_apsct.py
index d186bab..8751713 100644
--- a/production_apsct.py
+++ b/production_apsct.py
@@ -28,21 +28,25 @@ if len(sys.argv) < 2:
 
 apsct = apsct_lib.ApsctClass(CLK_FREQ)
 state = True
-modi = ["200MHz"] # , "160MHz", "OFF"]
-if False : #for mode in modi:
+modi = ["200MHz", "160MHz", "OFF"]
+for mode in modi:
     print(f"Check APSCT in {mode} mode")
     apsct.frequency = mode
     apsct.set_apsct()
-    apsct.pll_200.read_lock()
-    apsct.pll_160.read_lock()
-    apsct.sensors.apsct_sensors()
-    state = state & apsct.check_apsct()
+    if mode == "200MHz":
+        apsct.pll_200.read_lock()
+    if mode == "160MHz":
+        apsct.pll_160.read_lock()
+
+apsct.frequency = "200MHz"
+apsct.set_apsct()
+apsct.pll_200.read_lock()
 apsct.sensors.apsct_sensors()
 state = state & apsct.check_apsct()
 
 if READ_ALL:
     apsct.pll_200.read_all_regs_pll()
-#    apsct.pll_160.read_all_regs_pll()
+    apsct.pll_160.read_all_regs_pll()
     apsct.read_IO_expanderis()
 
 if state:
-- 
GitLab