From c1f2beb0915a9e2c65bc77384e9c2aa3d40c99f4 Mon Sep 17 00:00:00 2001 From: Gijs Schoonderbeek <schoonderbeek@astron.nl> Date: Fri, 14 May 2021 18:33:56 +0200 Subject: [PATCH] i2c interface working, tested on the hardware --- I2C_serial_pi.py | 13 +++--- rd_unb2c.py | 112 ++++++++++++++++++++++++++--------------------- 2 files changed, 71 insertions(+), 54 deletions(-) diff --git a/I2C_serial_pi.py b/I2C_serial_pi.py index 559d110..726cd2b 100644 --- a/I2C_serial_pi.py +++ b/I2C_serial_pi.py @@ -42,7 +42,7 @@ class I2C: # sleep(0.2) except IOError, err: ret_ack = 0 - ret_value = 0 + ret_value = 'ffff' if DEBUG: print("Reading error") return ret_ack, ret_value @@ -50,17 +50,20 @@ class I2C: def read_last_reg(self, bytes_to_read): bus = smbus.SMBus(self.bus_nr) - ret_value=[] - for cnt in bytes_to_read: + rd_value = [] + ret_value = '' + for cnt in range(bytes_to_read): try: - ret_value.append(bus.read_byte(self.I2C_Address)) + rd_value.append(bus.read_byte(self.I2C_Address)) ret_ack = 1 # sleep(0.2) except IOError, err: ret_ack = 0 - ret_value = 0 + rd_value.append(0) if DEBUG: print("Reading error") + for cnt in range(bytes_to_read): + ret_value += (hex(rd_value[cnt])[2:]) return ret_ack,ret_value def write_bytes(self, register, data): diff --git a/rd_unb2c.py b/rd_unb2c.py index 2e8ce4e..bd52fc3 100644 --- a/rd_unb2c.py +++ b/rd_unb2c.py @@ -30,7 +30,6 @@ sys.path.insert(0, '.') I2CBUSNR = 3 DEBUG = False - class Unb2cClass: # # Class that contains all parts on a UniBoard2 @@ -42,13 +41,13 @@ class Unb2cClass: for node_cnt in range(4): self.nodes.append(NodeClass(node_cnt)) for pol in list(CTR_POLS.keys()): - self.pols.append(PolClass(pol, location="central")) + self.pols.append(PolClass(pol, location = "central")) self.dev_i2c_eeprom = I2C(EEPROM) - self.dev_i2c_eeprom.bus = I2CBUSNR + self.dev_i2c_eeprom.bus_nr = I2CBUSNR self.main_switch = I2C(MAIN_I2C_SWITCH) - self.main_switch.bus = I2CBUSNR + self.main_switch.bus_nr = I2CBUSNR self.front = I2C(LED_DRIVER) - self.front.bus = I2CBUSNR + self.front.bus_nr = I2CBUSNR def write_eeprom(self, data=0x01): # @@ -125,6 +124,7 @@ class Unb2cClass: self.front_led(LED_COLORS[color]) for node in self.nodes: node.print_status() + print("Status central UniBoard2 components") for pol in self.pols: pol.print_status() self.wr_rd_eeprom() @@ -182,14 +182,14 @@ class NodeClass: # Before a node can ba accessed the i2c switches have to be set, this function can be used. # main_switch = I2C(MAIN_I2C_SWITCH) - main_switch.bus = I2CBUSNR + main_switch.bus_nr = I2CBUSNR ret_ack = main_switch.write_bytes(0x0, 0x01 << self.node_number) # select Node if ret_ack < 1: if DEBUG: print("Main I2C switch not found") else: node_switch = I2C(NODE_I2C_SWITCH) - node_switch.bus = I2CBUSNR + node_switch.bus_nr = I2CBUSNR ret_ack = node_switch.write_bytes(0x0, 0x20) # select DDR4 if ret_ack < 1: if DEBUG: @@ -210,14 +210,14 @@ class QsfpClass: self.status = False self.select_qsfp() self.qsfp_cage = I2C(QSFP_I2C_ADDR) - self.qsfp_cage.bus = I2CBUSNR + self.qsfp_cage.bus_nr = I2CBUSNR def select_qsfp(self): # # Function to set the I2C switch to access a QSFP cage # node_switch = I2C(NODE_I2C_SWITCH) - node_switch.bus = I2CBUSNR + node_switch.bus_nr = I2CBUSNR ret_ack = node_switch.write_bytes(0x0, QSFP_PORT[self.port]) if ret_ack < 1: if DEBUG: @@ -232,17 +232,20 @@ class QsfpClass: # self.select_qsfp() ret_ack, raw_ret = self.qsfp_cage.read_bytes(QSFP_TEMP, 2) - if (ret_ack < 1) | (raw_ret[:2] == 'ff'): + if ret_ack < 1: if DEBUG: stri = "No QSFP found in port {0}".format(self.port) print(stri) self.status = False else: - ret_value = [] - ret_value.append(int(raw_ret[:2], 16)) - ret_value.append(int(raw_ret[2:], 16)) - self.temp = (ret_value[0] * 256 + ret_value[1]) / 256 - self.status = True + if (raw_ret[0:2] == 'ff') | (len(raw_ret) < 4): + self.status = False + else: + ret_value = [] + ret_value.append(int(raw_ret[:2], 16)) + ret_value.append(int(raw_ret[2:], 16)) + self.temp = (ret_value[0] * 256 + ret_value[1]) / 256 + self.status = True def read_volt(self): # @@ -250,17 +253,20 @@ class QsfpClass: # self.select_qsfp() ret_ack, raw_ret = self.qsfp_cage.read_bytes(QSFP_VOLT, 2) - if (ret_ack < 1) | (raw_ret[:2] == 'ff'): + if ret_ack < 1: if DEBUG: stri = "No QSFP found in port {0}".format(self.port) print(stri) self.status = False else: - ret_value = [] - ret_value.append(int(raw_ret[:2], 16)) - ret_value.append(int(raw_ret[2:], 16)) - self.volt = (ret_value[0] * 256 + ret_value[1]) * 0.0001 - self.status = True + if (raw_ret[0:2] == 'ff') | (len(raw_ret) < 4): + self.status = False + else: + ret_value = [] + ret_value.append(int(raw_ret[:2], 16)) + ret_value.append(int(raw_ret[2:], 16)) + self.volt = (ret_value[0] * 256 + ret_value[1]) * 0.0001 + self.status = True def read_all(self): # @@ -293,14 +299,14 @@ class DdrClass: self.ddr_dev = I2C(MB_I_TEMP_I2C_ADDR) else: self.ddr_dev = I2C(MB_II_TEMP_I2C_ADDR) - self.ddr_dev.bus = I2CBUSNR + self.ddr_dev.bus_nr = I2CBUSNR def set_i2c_switch(self): # # Function to set the I2C switch to access the DDR4 modules # node_switch = I2C(NODE_I2C_SWITCH) - node_switch.bus = I2CBUSNR + node_switch.bus_nr = I2CBUSNR ret_ack = node_switch.write_bytes(0x0, DDR4) if ret_ack < 1: if DEBUG: @@ -355,18 +361,19 @@ class PolClass: self.vout = 0 self.iout = 0 self.temp = 0 + self.set_i2c_switch() if self.location == "node": - self.set_i2c_switch() self.pol_dev = I2C(LOC_POLS[self.name]) else: self.pol_dev = I2C(CTR_POLS[self.name]) - self.pol_dev.bus = I2CBUSNR - sleep(0.1) - ret_ack, ret_value = self.pol_dev.read_bytes(0) + self.pol_dev.bus_nr = I2CBUSNR + ret_ack, ret_value = self.pol_dev.read_bytes(1) if ret_ack < 1: - if DEBUG: + if self.location == "node": # DEBUG: stri = " Device {0} at address 0x{1:X} not found".format(self.name, LOC_POLS[self.name]) - print(stri) + else: + stri = " Device {0} at address 0x{1:X} not found".format(self.name, CTR_POLS[self.name]) + print(stri) self.status = False else: self.status = True @@ -375,15 +382,27 @@ class PolClass: # # Function to set the I2C switch to access the Point of Load DC/DC converter # - node_switch = I2C(NODE_I2C_SWITCH) - node_switch.bus = I2CBUSNR - ret_ack = node_switch.write_bytes(0x0, LOC_POWER) - if ret_ack < 1: - if DEBUG: - print("Node I2C switch not found") - self.status = True + if self.location == 'node': + node_switch = I2C(NODE_I2C_SWITCH) + node_switch.bus_nr = I2CBUSNR + ret_ack = node_switch.write_bytes(0x0, LOC_POWER) + if ret_ack < 1: + if DEBUG: + print("Node I2C switch not found") + self.status = False + else: + self.status = True else: - self.status = False + main_switch = I2C(MAIN_I2C_SWITCH) + main_switch.bus_nr = I2CBUSNR + ret_ack = main_switch.write_bytes(0x10, 0x10) + if ret_ack < 1: + if DEBUG: + print("Main I2C switch not found") + self.status = False + else: + self.status = True + return False def read_vout(self): # @@ -392,10 +411,8 @@ class PolClass: if self.status: if type == "node": self.set_i2c_switch() - sleep(0.1) ret_ack, vout_mod = self.pol_dev.read_bytes(LP_VOUT_MODE, 1) - sleep(0.1) - ret_ack, raw_value = self.pol_dev.read_bytes(LP_VOUT, 2) + ret_ack, raw_value = self.pol_dev.read_bytes(LP_VOUT, 2) vout_mod = int(vout_mod, 16) ret_value = [] ret_value.append(int(raw_value[:2], 16)) @@ -409,9 +426,7 @@ class PolClass: # Function to read the output current of the Point of Load DC/DC converter # if self.status: - if type == "node": - self.set_i2c_switch() - sleep(0.1) + self.set_i2c_switch() ret_ack, raw_value = self.pol_dev.read_bytes(LP_IOUT, 2) ret_value = [] ret_value.append(int(raw_value[:2], 16)) @@ -425,10 +440,8 @@ class PolClass: # Function to read the temperature of the Point of Load DC/DC converter # if self.status: - if type == "node": - self.set_i2c_switch() - sleep(0.1) - ret_ack, raw_value = self.pol_dev.read_bytes(LP_temp, 2) + self.set_i2c_switch() + ret_ack, raw_value = self.pol_dev.read_bytes(LP_temp, 2) ret_value = [] ret_value.append(int(raw_value[:2], 16)) ret_value.append(int(raw_value[2:], 16)) @@ -461,8 +474,9 @@ def main(): # Function to test the class, read all info and dump on the screen # unb = Unb2cClass() - unb.read_all() - unb.print_status() + if unb.wr_rd_eeprom(value=0x34): + unb.read_all() + unb.print_status() if __name__ == "__main__": -- GitLab