From 11cdcf7739bca7cd39091a96030749a4f4bb672b Mon Sep 17 00:00:00 2001
From: Gijs Schoonderbeek <schoonderbeek@astron.nl>
Date: Tue, 10 Jan 2023 16:14:15 +0100
Subject: [PATCH] Added I2C-switch check

---
 apsct_lib.py | 43 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/apsct_lib.py b/apsct_lib.py
index 8bb7a19..32a4f15 100644
--- a/apsct_lib.py
+++ b/apsct_lib.py
@@ -18,6 +18,7 @@ import sys
 import APSCT_I2C
 import time
 import RPi.GPIO as gpio
+import random
 sys.path.insert(0, '.')
 import os
 if os.name == "posix":
@@ -41,6 +42,9 @@ class ApsctClass:
         self.pll_160 = PllClass("160MHz")
         self.sensors = ApsctSensors()
         self.pps = PpsClass()
+        self.i2cswitch = []
+        for addr in APSCT_I2C.i2c_switch_addr:
+            self.i2cswitch.append(I2cSwitch(address=addr))
 
     def read_IO_expanderis(self):
         #
@@ -103,6 +107,8 @@ class ApsctClass:
         #
         result = self.sensors.check_values()
         result = result & self.pps.check_timing()
+        for i2c_switch in self.i2cswitch:
+            result = result & i2c_switch.check_switch(data=random.randint(0,2**8))
         if self.frequency == "200MHz":
             self.pll_200.read_lock()
             lock = self.pll_200.lock
@@ -358,6 +364,8 @@ class ApsctSensors:
         self.power_supplies = list(APSCT_I2C.PWR_LOCATIONS.keys())
         self.voltages = {}
         self.temperature = 9999
+        self.dev_i2c_sensor.write_bytes(0xB0, 0xB8)
+
 
     def apsct_sensors(self):
         for sens_line in range(7):
@@ -367,6 +375,8 @@ class ApsctSensors:
     def read_all_voltages(self):
         for pwr in self.power_supplies:
             self.voltages[pwr] = self.read_voltage(APSCT_I2C.PWR_LOCATIONS[pwr])
+            if self.voltages[pwr] < 3:
+                self.voltages[pwr] = self.read_voltage(APSCT_I2C.PWR_LOCATIONS[pwr])
         return True
 
     def check_values(self):
@@ -384,6 +394,8 @@ class ApsctSensors:
             if not (0.9*expected < self.voltages[pwr] < 1.1*expected):
                 result = False
                 print(f"Error: {pwr: <9} expected: {expected} V read: {self.voltages[pwr]:4.2f} V")
+            else:
+                print(f"OK   : {pwr: <9} expected: {expected} V read: {self.voltages[pwr]:4.2f} V")
         if not (15 < self.temperature < 50):
             result = False
             print(f"Error temperature read {self.temperature:4.2f} °C")
@@ -405,7 +417,7 @@ class ApsctSensors:
             print(stri)
         sleep(0.2)
         self.dev_i2c_sensor.write_bytes(channel_select_word, 0xB8)
-        sleep(0.5)
+        sleep(0.2)
         ret_ack, ret_value = self.dev_i2c_sensor.read_last_reg(3)
         if DEBUG:
             stri = "Return value input 0 : 0x{0} ".format(ret_value)
@@ -485,3 +497,32 @@ class PpsClass:
             print(f", timing is OK: {timepps:4.2f} s")
             self.timing = True    
         return self.timing
+
+class I2cSwitch:
+    #
+    # Class to check a I2C-switch 
+    #
+    def __init__(self, address=0x70):
+        #
+        # Whats needed to measure the toggle on GPIO24
+        #
+        self.address = address
+        self.dev_i2c_switch = I2C(address)
+        self.dev_i2c_switch.bus_nr = 1
+
+    def check_switch(self, data=0xa5):
+        print(f"Check I2C switch at 0x{self.address:x}", end=' ')
+        ret_ack, ret_value = self.dev_i2c_switch.read_bytes(0)
+        if ret_ack < 1:
+            print("I2C-Switch not found")
+            return False
+        else:
+            self.dev_i2c_switch.write_bytes(0, data)
+            ret_ack, ret_value = self.dev_i2c_switch.read_last_reg(1)
+            read_value_int = int(ret_value, 16) 
+            if read_value_int == data:
+                print(f"OK   wrote 0x{data:02X} read back 0x{read_value_int:02X}")
+                return True
+            else:
+                print(f"ERROR wrote 0x{data:02X} read back 0x{read_value_int:02X}")
+                return False
-- 
GitLab