From 9d5dc31970bb503540f8d4b0a0f5e2af8535e5fe Mon Sep 17 00:00:00 2001 From: Gijs Schoonderbeek <schoonderbeek@astron.nl> Date: Mon, 23 Jan 2023 17:11:15 +0100 Subject: [PATCH] Version used for prodution run L2TS, 4 boards --- APSPU_I2C.py | 6 +- I2C_serial_pi3.py | 146 ++++++++++++++++++++++++++++++++++++++++++++++ apspu_lib.py | 24 ++++---- 3 files changed, 163 insertions(+), 13 deletions(-) create mode 100644 I2C_serial_pi3.py diff --git a/APSPU_I2C.py b/APSPU_I2C.py index 4362f40..ca6f97e 100644 --- a/APSPU_I2C.py +++ b/APSPU_I2C.py @@ -37,9 +37,9 @@ VOUT_POLS = {"CTR_LBA": 8.0, "CTR_RCU2_A": 5.60009765625, "CTR_RCU2_D": 3.2998046875} -IOUT_POLS = {"CTR_LBA": 0.45, - "CTR_RCU2_A": 0.7, - "CTR_RCU2_D": 0.3} +IOUT_POLS = {"CTR_LBA": 0.2, + "CTR_RCU2_A": 0.6, + "CTR_RCU2_D": 0.2} LP_VIN = 0x88 LP_VOUT_MODE = 0x20 diff --git a/I2C_serial_pi3.py b/I2C_serial_pi3.py new file mode 100644 index 0000000..adfa428 --- /dev/null +++ b/I2C_serial_pi3.py @@ -0,0 +1,146 @@ +''' +Copyright 2021 Stichting Nederlandse Wetenschappelijk Onderzoek Instituten, +ASTRON Netherlands Institute for Radio Astronomy +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +I2C_serial_Pi +Started by Gijs + +Class for using the I2C bus of the I2C. This class is used for the +basic I2C scripts to read and write the RCU2, PCC etc. + +''' +import smbus2 +import sys +from time import * + +DEBUG = False #True +SLOW = False + + +class I2C: + + def __init__(self, ADDRESS='040',BUSNR=3): + self.I2C_Address = ADDRESS + self.bus_nr = BUSNR + + def close(self): + bus = smbus2.SMBus(self.bus_nr) + bus.close() + return True + + def open(self): + bus = smbus2.SMBus(self.bus_nr) + bus.open(self.bus_nr) + return True + + def read_bytes(self, register, bytes_to_read=2): + bus = smbus2.SMBus(self.bus_nr) + try: + rd_value = bus.read_i2c_block_data(self.I2C_Address, register, bytes_to_read) + ret_value = '' + for cnt in range(bytes_to_read): + ret_value += (hex(rd_value[cnt])[2:]) + ret_ack = 1 + if SLOW: + sleep(0.2) + except IOError: + ret_ack = 0 + ret_value = 'ffff' + if DEBUG: + print("Reading IO-error") + return ret_ack, ret_value + + + def read_last_reg(self, bytes_to_read): + bus = smbus2.SMBus(self.bus_nr) + rd_value = [] + ret_value = '' + for cnt in range(bytes_to_read): + try: + rd_value.append(bus.read_byte(self.I2C_Address)) + ret_ack = 1 + if SLOW: + sleep(0.2) + except IOError: + ret_ack = 0 + rd_value.append(0) + if DEBUG: + print("IO-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): + bus = smbus2.SMBus(self.bus_nr) + if type(data) is not list: + data = [data] + try: + bus.write_i2c_block_data(self.I2C_Address, register, data) + ret_ack = 1 + if SLOW: + sleep(0.3) + except IOError: + ret_ack = 0 + ret_value = 0 + if DEBUG: + print("Write IO-error") + return ret_ack + + def write_register(self, register): + bus = smbus2.SMBus(self.bus_nr) + try: + bus.write_byte(self.I2C_Address, register) +# print(f"Wrote {register:x}") + ret_ack = 1 + if SLOW: + sleep(0.3) + except IOError: + ret_ack = 0 + ret_value = 0 +# if DEBUG: + print("Write IO-error") + return ret_ack + + def write_pointer(self, register): + bus = smbus2.SMBus(self.bus_nr) + try: + ret_value = bus.read_i2c_block_data(self.I2C_Address, register, 1) + ret_ack = 1 + if SLOW: + sleep(0.3) + except IOError: + ret_ack = 0 + ret_value = 0 + if DEBUG: + print("Write IO-error") + return ret_ack + + def ack_check(self): + bus = smbus2.SMBus(self.bus_nr) + try: + print("check ACK") + ret_value = bus.write_quick(self.I2C_Address) + ret_ack = 1 + if SLOW: + sleep(0.3) + except IOError: + ret_ack = 0 + ret_value = 0 + if DEBUG: + print("No ACK IO-Error") + return ret_ack + +if __name__ == "__main__": + I2C_Device = I2C(0x40) + I2C_Device.write_bytes(0x00, 0x00) + ret_ack, ret_value = I2C_Device.read_bytes(0x8C, 2) + print(ret_value) diff --git a/apspu_lib.py b/apspu_lib.py index 57e76c9..be387ca 100644 --- a/apspu_lib.py +++ b/apspu_lib.py @@ -25,13 +25,13 @@ from APSPU_I2C import * if os.name == "posix": - from I2C_serial_pi import * + from I2C_serial_pi3 import * else: from I2C_serial import * I2CBUSNR = 1 # Bus used on the Pi DEBUG = False # Set True to print debug information on the screen - +I_OK_MARGIN = 0.5 # I_error in Amps class ApspuClass: # @@ -308,12 +308,16 @@ class PolClass: # # return is always True # - ret_ack = self.pol_dev.write_register(0x15) -# command = f"i2cset -y 1 0x{CTR_POLS[self.name]:02X} 0x15 cp" -# print(command) -# os.system(command) -# os.system(command) - sleep(5) + print(f"Store to NVM for POL {self.name}") + if False: + ret_ack = self.pol_dev.write_register(0x15) + sleep(1) + else: + self.pol_dev.close() + command = f"i2cset -y 1 0x{CTR_POLS[self.name]:02X} 0x15 cp" + os.system(command) + os.system(command) + self.pol_dev.open() return True def read_vin(self): @@ -467,8 +471,8 @@ class PolClass: check_ok = False print(f"POL {self.name:10} TEMP not OK, expected {temp_low} C - {temp_high} C, measured {self.temp} C ") return check_ok - i_low = 0.75*IOUT_POLS[self.name] - i_high = 1.25*IOUT_POLS[self.name] + i_low = (1-I_OK_MARGIN)*IOUT_POLS[self.name] + i_high = (1+I_OK_MARGIN)*IOUT_POLS[self.name] if i_low < self.iout < i_high: check_ok = True else: -- GitLab