From bee8434931bc03fcb0206df0bbd2e5490295b484 Mon Sep 17 00:00:00 2001
From: Gijs Schoonderbeek <schoonderbeek@astron.nl>
Date: Wed, 22 Feb 2023 14:19:32 +0100
Subject: [PATCH] Communication working, Setting not correct

---
 apsct_lib.py | 109 ++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 82 insertions(+), 27 deletions(-)

diff --git a/apsct_lib.py b/apsct_lib.py
index 886a37a..90140c2 100644
--- a/apsct_lib.py
+++ b/apsct_lib.py
@@ -27,7 +27,7 @@ else:
     from I2C_serial import *
 
 I2CBUSNR = 5
-sleep_time = 0.15
+sleep_time = 0.0001
 DEBUG = False
 
 
@@ -62,6 +62,11 @@ class ApsctClass:
                 stri = "Expander : 0x{:0>2x}, Reg 0x{:0>2x}, value 0x{}{}".format(addr, reg_cnt, ret_value[0],
                                                                                   ret_value[1])  # [start+2:start])
                 print(stri)
+    def read_all_io_device_registers(self, device, registers=8):
+        print(f"Read IO-expander at register 0x{device.I2C_Address:X}")
+        for register in range(registers):
+            reg_content = device.read_bytes(register, 1)
+            print(f"Register {register} 0x{reg_content}")
 
     def power(self, state):
         #
@@ -73,24 +78,30 @@ class ApsctClass:
         stri = "Power to {}".format(state)
         print(stri)
         i2c_io_device_a = I2C(0x20, BUSNR=I2CBUSNR)
+        i2c_io_device_a.write_bytes(0x04, 0)  # inversion off
+        i2c_io_device_a.write_bytes(0x05, 0)  # inversion off 
         i2c_io_device_a.write_bytes(0x06, 0x2C)  # '0' is output, '1' is input
         i2c_io_device_a.write_bytes(0x07, 0x00)  # '0' is output, '1' is input
         i2c_io_device_b = I2C(0x21, BUSNR=I2CBUSNR)
+        i2c_io_device_b.write_bytes(0x04, 0)  # inversion off
+        i2c_io_device_b.write_bytes(0x05, 0)  # inversion off 
         i2c_io_device_b.write_bytes(0x06, 0x2C)  # '0' is output, '1' is input
+        i2c_io_device_b.write_bytes(0x07, 0xFF)  # '0' is output, '1' is input
         if state:
             bits_to_set_a1 = 0x02 | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
-            bits_to_set_a2 = 0x04
+            bits_to_set_a2 = 0x08
             bits_to_set_b1 = 0x02 | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
         else:
-            bits_to_set_a1 = 0x00 | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
+            bits_to_set_a1 = 0x00 # | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
             bits_to_set_a2 = 0x00
-            bits_to_set_b1 = 0x00 | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
-        if DEBUG:
-            stri = "Bits to reg 0 0x{0:x}".format(bits_to_set_a1)
-            print(stri)
+            bits_to_set_b1 = 0x00 # | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
         i2c_io_device_a.write_bytes(0x02, bits_to_set_a1)
         i2c_io_device_a.write_bytes(0x03, bits_to_set_a2)
         i2c_io_device_b.write_bytes(0x02, bits_to_set_b1)
+        if DEBUG:
+            self.read_all_io_device_registers(i2c_io_device_a)
+            self.read_all_io_device_registers(i2c_io_device_b)
+
 
     def set_apsct(self):
         #
@@ -142,6 +153,23 @@ class PllClass:
             self.i2c_address = APSCT_I2C.PLL_200M
         self.dev_i2c_pll = I2C(self.i2c_address, BUSNR=I2CBUSNR)
 
+    def reset_interface(self):
+        #
+        # Reset the SPI interface 
+        #
+        self.dev_i2c_pll.write_bytes(0x06, 0x2C)
+        sleep(0.01)
+        write_data = 0x02 | (0 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
+        self.dev_i2c_pll.write_bytes(0x02, write_data)
+        for cnt in range(4):
+            for clk in [0,1,0]: 
+                write_data = 0x02 | (0 << APSCT_I2C.CS) | (clk << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
+                self.dev_i2c_pll.write_bytes(0x02, write_data)
+                sleep(sleep_time)
+        write_data = 0x02 | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
+        self.dev_i2c_pll.write_bytes(0x02, write_data)
+        sleep(0.01)
+
     def write_byte_pll(self, reg_address, wr_data):
         #
         # Write Byte to the PLL
@@ -150,26 +178,21 @@ class PllClass:
         if DEBUG:
             stri = "Write to address : 0x{1:{fill}2x} value  0x{0:{fill}2x}".format(wr_data, reg_address, fill='0')
             print(stri)
-        self.dev_i2c_pll.write_bytes(0x06, 0x2C)
+        rw_reg_pll = 0b00101101 # 1 is input
+        self.dev_i2c_pll.write_bytes(0x06, rw_reg_pll)
         rd_bytes = self.dev_i2c_pll.read_bytes(0x06, 1)
         if DEBUG:
-            stri = "IO expander wrote 0x{0:x}, read 0x{1}".format(0x2C, rd_bytes[1])
+            stri = "IO expander wrote 0x{0:x}, read 0x{1}".format(rw_reg_pll, rd_bytes[1])
             print(stri)
         nof_bytes = 1
-        data = (pll_rw << 23) + (nof_bytes << 21) + (reg_address << 8)  + wr_data
+        data = (pll_rw << 23) + ((nof_bytes-1) << 21) + (reg_address << 8)  + wr_data
         bit_array = "{0:{fill}24b}".format(data, fill='0')
         self.dev_i2c_pll.write_bytes(0x02, 0x02 | (0x1 << APSCT_I2C.CS))
         for bit in bit_array:
-            for clk in range(2):
+            for clk in [0, 1, 0]: #range(2):
                 write_data = 0x02 | (0 << APSCT_I2C.CS) | (clk << APSCT_I2C.SCLK) | (int(bit) << APSCT_I2C.SDI)
                 self.dev_i2c_pll.write_bytes(0x02, write_data)
-        for clk in range(2):
-            write_data = 0x02 | (0 << APSCT_I2C.CS) | (clk << APSCT_I2C.SCLK)
-            self.dev_i2c_pll.write_bytes(0x02, write_data)
-        for clk in range(2):
-            write_data = 0x02 | (1 << APSCT_I2C.CS) | (clk << APSCT_I2C.SCLK)
-            self.dev_i2c_pll.write_bytes(0x02, write_data)
-
+                sleep(sleep_time)
         write_data = 0x02 | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
         self.dev_i2c_pll.write_bytes(0x02, write_data)
 
@@ -178,24 +201,36 @@ class PllClass:
         # Read byte from the PLL
         #
         pll_rw = 0x01  # 0 for write, 1 for read
+        read_bit = ''
         self.dev_i2c_pll.write_bytes(0x06, 0x2C)
-        data = (pll_rw << 15) + (nof_bytes << 13) + reg_address
+        data = (pll_rw << 15) + ((nof_bytes-1) << 13) + reg_address
         bit_array = "{0:{fill}16b}".format(data, fill='0')
+        if DEBUG:
+            print(f"Write data: {bit_array}")
+        write_data = 0x02 | (0 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
+        self.dev_i2c_pll.write_bytes(0x02, write_data)
+        sleep(sleep_time)
         for bit in bit_array:
-            for clk in range(2):
+            for clk in [0,1,0]: 
                 write_data = 0x02 | (0 << APSCT_I2C.CS) | (clk << APSCT_I2C.SCLK) | (int(bit) << APSCT_I2C.SDI)
+                print(f"Write data to IO {write_data:08b}")
                 self.dev_i2c_pll.write_bytes(0x02, write_data)
                 sleep(sleep_time)
-        read_bit = ''
+#        write_data = 0x02 | (0 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
+#        self.dev_i2c_pll.write_bytes(0x02, write_data)
+        self.dev_i2c_pll.write_bytes(0x06, 0xAC)
         for cnt in range(8*nof_bytes):
-            for clk in [0, 1]:  # Read after rizing edge
-                write_data = 0x02 | (clk << APSCT_I2C.SCLK) | (int(bit_array[-1]) << APSCT_I2C.SDI)
+            for clk in [0, 1]: #[0, 1, 0]:  # Read after rizing edge
+                write_data = 0x02 | (0 << APSCT_I2C.CS) | (clk << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
                 self.dev_i2c_pll.write_bytes(0x02, write_data)
+                sleep(sleep_time)
             ret_ack, ret_value = self.dev_i2c_pll.read_bytes(0x00, 1)
             if ret_ack:
                 read_bit += str((int(ret_value, 16) >> APSCT_I2C.SDO) & 0x01)
             else:
                 print("ACK nok")
+        print(f"Read bits {read_bit}")
+        self.dev_i2c_pll.write_bytes(0x06, 0x2C)
         write_data = 0x02 | (1 << APSCT_I2C.CS) | (0 << APSCT_I2C.SCLK) | (0 << APSCT_I2C.SDI)
         self.dev_i2c_pll.write_bytes(0x02, write_data)
         if DEBUG:
@@ -212,7 +247,7 @@ class PllClass:
         divider_r = 1
         divider_a = 0
         divider_p = 2
-        divider_b = (self.frequency * divider_r) / (10 * divider_p)
+        divider_b = int((int(self.frequency[:-3]) * divider_r) / (10 * divider_p))
         charge_pump_current = 3  # 0 is low (0.6 mA), 7 is high (4.8 mA)
         if self.frequency == '160MHz':
             i2c_address = APSCT_I2C.PLL_200M
@@ -246,7 +281,7 @@ class PllClass:
         #
         # Read all registers on the PLL and print on screen
         #
-        bytes_to_read = 12
+        bytes_to_read = 90
         ret_value = self.read_byte_pll(0, nof_bytes=bytes_to_read)
         for cnt in range(bytes_to_read):
             start = cnt*8
@@ -263,8 +298,8 @@ class PllClass:
             i2_c_io_device = I2C(0x21, BUSNR=I2CBUSNR)
         i2_c_io_device.write_bytes(0x06, 0x2C)  # '0' is output
         i2_c_io_device.write_bytes(0x07, 0x00)  # '0' is output
-        ack, ret_value = i2_c_io_device_b.read_bytes(0x00, 1)
-        status_pll = ret_value & 0x02
+        ack, ret_value = i2_c_io_device.read_bytes(0x00, 1)
+        status_pll = int(ret_value) & 0x02
         if status_pll == 0x02:
             self.lock = True
             stri = f"PLL {self.frequency} is in lock"
@@ -609,3 +644,23 @@ class ApsctId:
         else:
             print(f"ERROR : Back ID is 0x{self.id:02X} expected 0x{63:2X}")
             return False
+
+
+def main():
+    apsct = ApsctClass()
+    apsct.power(False)
+    sleep(1)
+    apsct.power(True)
+    sleep(1)
+    if False:
+#        apsct.pll_200.reset_interface()
+        apsct.pll_200.write_byte_pll(0x06, 0xA5)
+        apsct.pll_200.write_byte_pll(0x5A, 0x01)
+#        apsct.pll_200.reset_interface()
+        apsct.pll_200.read_byte_pll(0x06)
+    apsct.pll_200.setup_pll()
+    apsct.pll_200.read_byte_pll(0x0A)
+    apsct.sensors.read_all_voltages()
+
+if __name__ == "__main__":
+    main()
-- 
GitLab