From f1231acebbdb784692273d6b891d23708f397bd0 Mon Sep 17 00:00:00 2001
From: Gijs <schoonderbeek@astron.nl>
Date: Fri, 9 Feb 2024 09:55:51 +0100
Subject: [PATCH] Update apspu_lib.py and I2C_serial_iss.pu for the APSPU
 production setup.

---
 apspu_lib.py | 162 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 151 insertions(+), 11 deletions(-)

diff --git a/apspu_lib.py b/apspu_lib.py
index 77a8417..21c4c9d 100644
--- a/apspu_lib.py
+++ b/apspu_lib.py
@@ -90,7 +90,62 @@ class ApspuClass:
             pol.on_off(True)
             pol.write_to_nvm()
         print("Done")
-    
+
+    def set_pols_lv(self):
+            #
+            # Function to set output voltage level on the APSPU
+            # Values are read from the APSPU_I2C
+            #
+        #print("--------- \nProgram Pols\n---------")
+        for pol in self.pols:
+            print(f"Set Pol {pol.name} to {VOUT_POLS[pol.name]:4.2f} V")
+            pol.set_vout_pol(VOUT_POLS[pol.name])
+            vout = pol.read_vout_set()
+            if not (0.9*VOUT_POLS[pol.name] < vout < 1.1*VOUT_POLS[pol.name]):
+#            if not (0.9*VOUT_POLS[pol.name] < 10 < 1.1*VOUT_POLS[pol.name]): # Test communication with Labview
+                print(f"POL {pol.name:10} Error setting Vout, "
+                      f"set to {VOUT_POLS[pol.name]} read back {vout:4.3f}")
+                returnstring = (f"POL {pol.name:10} Error setting Vout, "
+                      f"set to {VOUT_POLS[pol.name]} read back {vout:4.3f}")
+                return returnstring
+                exit()
+            pol.set_vout_ov_limit_pol(1.2*VOUT_POLS[pol.name])
+            ov_out = pol.read_ov_limit()
+            if not (1.1*VOUT_POLS[pol.name] < ov_out < 1.3*VOUT_POLS[pol.name]):
+#            if not (1.1*VOUT_POLS[pol.name] < 20 < 1.3*VOUT_POLS[pol.name]): # Test communication with Labview
+                print(f"POL {pol.name:10} Error setting output overvoltage"
+                      f"set {1.2*VOUT_POLS[pol.name]} read back {ov_out:4.3f}")
+                returnstring = (f"POL {pol.name:10} Error setting output overvoltage"
+                      f"set {1.2*VOUT_POLS[pol.name]} read back {ov_out:4.3f}")
+                return returnstring
+                exit()
+            pol.set_on_off_config()
+            pol.on_off(True)
+            pol.write_to_nvm()
+        returnstring = ("Programming OK")
+        print("Done")
+        return returnstring
+
+    def reset_pols_lv(self):
+        for pol in self.pols:
+            vreset = 9
+            print(f"Set Pol {pol.name} to {vreset} V")
+            pol.set_vout_pol(vreset)
+            vout = pol.read_vout_set()
+            if not (0.9*vreset < vout < 1.1*vreset):
+                print(f"POL {pol.name:10} Error setting Vout, "
+                      f"set to {vreset} read back {vout:4.3f}")
+                returnstring = (f"POL {pol.name:10} Error setting Vout, "
+                      f"set to {vreset} read back {vout:4.3f}")
+                return returnstring
+                exit()
+            pol.set_on_off_config()
+            pol.on_off(True)
+            pol.write_to_nvm()
+        returnstring = ("Programming OK")
+        print("Done")
+        return returnstring
+        
     def check_apspu(self):
         #
         # Function to check values of read_all() Used during production
@@ -172,7 +227,37 @@ class EepromClass:
             str_return = bytes.fromhex(ret_value[:nof_bytes*2]).decode('UTF-8')
             return str_return
 
-    def wr_rd_eeprom(self, value="APSPU-1", address=0):
+    def read_eeprom_lv(self, address=0, nof_bytes=18):
+        #
+        # Read the EEPROM with the serial number etc.
+        #
+        # Address = address to read from
+        # nof_bytes = number of bytes to read
+        # return string with the data from the flash
+        #
+        ret_ack, ret_value = self.dev_i2c_eeprom.read_last_reg(1)
+        if ret_ack < 1:
+            print("no EEPROM found during read")
+            return "no EEPROM found"
+        else:
+            ret_ack, ret_value = self.dev_i2c_eeprom.read_bytes(address, nof_bytes)
+            str_return = bytes.fromhex(ret_value[:nof_bytes*2]).decode('UTF-8')
+            return str_return
+
+    def read_eeprom_id_lv(self):
+        #
+        # Read the ID from the EEPROM.
+        #
+        ret_ack, ret_value = self.dev_i2c_eeprom.read_last_reg(1)
+        if ret_ack < 1:
+            print("no EEPROM found during read")
+            return "no EEPROM found"
+        else:
+            ret_ack, ret_value = self.dev_i2c_eeprom.read_bytes(0xfc, 4)
+            return ret_value
+
+
+    def wr_rd_eeprom(self, data="APSPU-1", address=0):
         #
         # Write and Read the EEPROM to check functionality
         #
@@ -180,11 +265,11 @@ class EepromClass:
         # address = address to write the data to
         # return True if read back is same as write value
         #
-        if self.write_eeprom(value, address=0):
-            ret_value = self.read_eeprom(address=0, nof_bytes=len(value))
-            stri = "Wrote to EEPROM register 0x{2:x} : {0}, Read from EEPROM: {1}".format(value, ret_value, address)
+        if self.write_eeprom(data, address=0):
+            ret_value = self.read_eeprom(address=0, nof_bytes=len(data))
+            stri = "Wrote to EEPROM register 0x{2:x} : {0}, Read from EEPROM: {1}".format(data, ret_value, address)
             print(stri)
-            if ret_value == value:
+            if ret_value == data:
                 return True
             else:
                 return False
@@ -602,20 +687,25 @@ class FanmonitorClass:
             print(stri)
 
 
-def temperature(pol=0):
+# ----- Functions to be called from Labview -----
+
+def temperature(pol=0.0):
     apspu = ApspuClass()
+    pol = int(pol)
     measured_temperature = apspu.pols[pol].read_temp()
     print(f"Temparature of POL {apspu.pols[pol].name} is {measured_temperature:4.2f} deg C")
     return measured_temperature
 
-def voltage(pol=0):
+def voltage(pol=0.0):
     apspu = ApspuClass()
+    pol = int(pol)
     measured_voltage = apspu.pols[pol].read_vout()
     print(f"Output voltage of POL {apspu.pols[pol].name} is {measured_voltage:4.2f} V")
     return measured_voltage
 
-def current(pol=0):
+def current(pol=0.0):
     apspu = ApspuClass()
+    pol = int(pol)
     measured_current = apspu.pols[pol].read_iout()
     print(f"Output current of POL {apspu.pols[pol].name} is {measured_current:4.2f} A")
     return measured_current
@@ -624,22 +714,66 @@ def apspu_off():
     apspu = ApspuClass()
     apspu.apspu_on_off(False)
     print(f"All outputs off")
+    return True
 
 def apspu_on():
     apspu = ApspuClass()
     apspu.apspu_on_off(True)
     print(f"All outputs on")
+    return True
 
-def fan_speed(fan_nr):
+def fan_speed(fan_nr=0.0):
     apspu = ApspuClass()
+    fan_nr = int(fan_nr)
     fan_speed = apspu.fans.read_fan(fan_nr)
     print(f"Fan nr {fan_nr} has speed {fan_speed}")
     return fan_speed
 
 def program_dc_dc_converters():
     apspu = ApspuClass()
-    apspu.set_pols()
+    prog_ret_string = apspu.set_pols_lv()
     print("Set DC/DC converters on APSPU")
+    return prog_ret_string
+
+def reset_dc_dc_converters():
+    apspu = ApspuClass()
+    print("Reset DC/DC converters on APSPU")
+    prog_ret_string = apspu.reset_pols_lv()
+    return prog_ret_string
+
+def read_qr():
+    apspu = EepromClass()
+    print("Read QR code from EEPROM")
+    return apspu.read_eeprom_lv(address=0x20, nof_bytes=17)
+
+def read_version():
+    apspu = EepromClass()
+    print("Read APSPU version code from EEPROM")
+    return apspu.read_eeprom_lv(address=0, nof_bytes=12)
+
+def read_id():
+    apspu = EepromClass()
+    print("Read ID from EEPROM")
+    return apspu.read_eeprom_id_lv()
+
+def write_qr(value):
+    apspu = EepromClass()
+    print("Write QR code to EEPROM")
+    if apspu.write_eeprom(data=value, address=0x20) == True:
+        prog_ret_string = "Programming OK"
+    else:
+        prog_ret_string = "Programming went wrong"
+    return prog_ret_string
+
+def write_version(value):
+    apspu = EepromClass()
+    print(f"Write APSPU_version {value} to EEPROM")
+    if apspu.write_eeprom(data=value, address=0) == True:
+        prog_ret_string = "Programming OK"
+    else:
+        prog_ret_string = "Programming went wrong"
+    return prog_ret_string
+
 
 def main():
     #
@@ -667,6 +801,12 @@ def main():
             current(pol_cnt)
         for fan_cnt in range(3):
             fan_speed(fan_cnt)
+        print (write_qr("APSPU-22091-00100"))
+        qr = read_qr()
+        print (qr)
+        print (write_qr("APSPU-rev2.0"))
+        print(read_version())
+        print(read_id())
         apspu_off()
 
     else:
-- 
GitLab