Skip to content
Snippets Groups Projects
Commit c1f2beb0 authored by Gijs Schoonderbeek's avatar Gijs Schoonderbeek
Browse files

i2c interface working, tested on the hardware

parent 20ba6b1b
Branches
No related tags found
1 merge request!2Modified the scripts to run on Raspberry Pi.
......@@ -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):
......
......@@ -30,7 +30,6 @@ sys.path.insert(0, '.')
I2CBUSNR = 3
DEBUG = False
class Unb2cClass:
#
# Class that contains all parts on a UniBoard2
......@@ -44,11 +43,11 @@ class Unb2cClass:
for pol in list(CTR_POLS.keys()):
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,11 +232,14 @@ 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:
if (raw_ret[0:2] == 'ff') | (len(raw_ret) < 4):
self.status = False
else:
ret_value = []
ret_value.append(int(raw_ret[:2], 16))
......@@ -250,11 +253,14 @@ 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:
if (raw_ret[0:2] == 'ff') | (len(raw_ret) < 4):
self.status = False
else:
ret_value = []
ret_value.append(int(raw_ret[:2], 16))
......@@ -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,17 +361,18 @@ class PolClass:
self.vout = 0
self.iout = 0
self.temp = 0
if self.location == "node":
self.set_i2c_switch()
if self.location == "node":
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])
else:
stri = " Device {0} at address 0x{1:X} not found".format(self.name, CTR_POLS[self.name])
print(stri)
self.status = False
else:
......@@ -375,15 +382,27 @@ class PolClass:
#
# Function to set the I2C switch to access the Point of Load DC/DC converter
#
if self.location == 'node':
node_switch = I2C(NODE_I2C_SWITCH)
node_switch.bus = I2CBUSNR
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:
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,9 +411,7 @@ 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)
vout_mod = int(vout_mod, 16)
ret_value = []
......@@ -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)
ret_ack, raw_value = self.pol_dev.read_bytes(LP_IOUT, 2)
ret_value = []
ret_value.append(int(raw_value[:2], 16))
......@@ -425,9 +440,7 @@ 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)
ret_value = []
ret_value.append(int(raw_value[:2], 16))
......@@ -461,6 +474,7 @@ def main():
# Function to test the class, read all info and dump on the screen
#
unb = Unb2cClass()
if unb.wr_rd_eeprom(value=0x34):
unb.read_all()
unb.print_status()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment