From 114705eab2f0215102030ee450c4712cf9b398f1 Mon Sep 17 00:00:00 2001 From: Gijs Schoonderbeek <schoonderbeek@astron.nl> Date: Tue, 8 Dec 2020 17:31:03 +0100 Subject: [PATCH] Added Python scripts from the labtop --- I2C_serial.py | 92 ++++++++++++++++++ set_adc.py | 241 +++++++++++++++++++++++++++++++++++++++++++++ set_adc_tm.py | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++ set_clk.py | 159 ++++++++++++++++++++++++++++++ set_clk_tm.py | 160 ++++++++++++++++++++++++++++++ 5 files changed, 917 insertions(+) create mode 100644 I2C_serial.py create mode 100644 set_adc.py create mode 100644 set_adc_tm.py create mode 100644 set_clk.py create mode 100644 set_clk_tm.py diff --git a/I2C_serial.py b/I2C_serial.py new file mode 100644 index 0000000..b49776a --- /dev/null +++ b/I2C_serial.py @@ -0,0 +1,92 @@ +''' +I2C Class + +''' +import sys +sys.path.insert(0,'c:\python34\lib\site-packages') +import serial +from time import * + +DEBUG=False + +ser = serial.Serial() +ser.baudrate = 57600 +ser.port = 'COM11' +ser.parity = serial.PARITY_NONE +ser.timeout = 1 + +class I2C: + + def __init__(self, ADDRESS='040'): + self.I2C_Address = ADDRESS + + +# def chk_ack(self): +# return ret_ack +# def write_bytes(self,register,data): +# return ret_ack +# def write_last_reg(self,data): +# return ret_ack + + def read_bytes(self, register, bytes_to_read=2): + serial_string='S{0:x}{1:{fill}2x}PS{2:x}{3:02}45P'.format(self.I2C_Address*2, register, (self.I2C_Address*2+1), bytes_to_read, fill = '0') + serial_string=serial_string.upper() + ret_ack = 1 + if DEBUG: + print(serial_string) + ret_value="0F" + else: + ser.open() + ser.write(bytes(serial_string, 'utf-8')) + ret_value = ser.read(bytes_to_read*2) + ser.close() + ret_value = ret_value.decode("utf-8") + return ret_ack, ret_value + + + def read_last_reg(self, bytes_to_read): + serial_string='S{0:x}{1:02}P'.format((self.I2C_Address*2+1), bytes_to_read) + serial_string=serial_string.upper() + ret_ack = 1 + if DEBUG: + print(serial_string) + ret_value="0F" + else: + ser.open() + ser.write(bytes(serial_string, 'utf-8')) + ret_value = ser.read(bytes_to_read*2) + ser.close() + ret_value = ret_value.decode("utf-8") + return ret_ack,ret_value + + def write_bytes(self, register, data): + ret_value=[] + serial_string='S{0:x}{1:02x}{2:02x}P'.format(self.I2C_Address*2, register, data) + serial_string=serial_string.upper() + ret_ack = 1 + if DEBUG: + print(serial_string) + else: + ser.open() + ser.write(bytes(serial_string, 'utf-8')) + ser.close() + return ret_ack + + def write_pointer(self, register): + ret_value=[] + serial_string='S{0:x}{1:02x}P'.format(self.I2C_Address*2, register) + serial_string=serial_string.upper() + ret_ack = 1 + if DEBUG: + print(serial_string) + else: + ser.open() + ser.write(bytes(serial_string, 'utf-8')) + ser.close() + 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/set_adc.py b/set_adc.py new file mode 100644 index 0000000..8aabf03 --- /dev/null +++ b/set_adc.py @@ -0,0 +1,241 @@ +''' +Set ADC + +''' +import sys +import time +sys.path.insert(0,'.') +from I2C_serial import * + +sleep_time=0.05 +CHECK_I2C_DEVICES = False +WRITE_DATA = True +READ_BYTE = True +PWR_RST = False +SET_ADC = True + +CS = 0 + +SCLK = [1, 3, 5] +SDIO = [0, 2, 4] +ADC_ORDER = [0, 1, 2] + + + +def blink_led(address=0x76, times = 5): + print("Blink LED") + I2C_device = I2C(address) + I2C_device.write_bytes(0x06, 00) + I2C_device.write_bytes(0x07, 00) + for cnt in range(times): + I2C_device.write_bytes(0x03, 0x40) + sleep(0.5) + I2C_device.write_bytes(0x03, 0x80) + sleep(0.5) + I2C_device.write_bytes(0x03, 0x40) + + +def Write_byte_ADC(ADC_reg_address, ADC_data, ADC_bytes=1, ADC_NR = 0, ADDRESS=0x20): + # + # Write Byte to the ADC + # + I2C_device = I2C(ADDRESS) + ADC_rw = 0x00 # 0 for write, 1 for read + stri = "Write : {0:2x} to Address : {1:2x}".format(ADC_data, ADC_reg_address) + print(stri) + data = ( ADC_rw << 23) + ( ADC_bytes << 21 ) + (ADC_reg_address << 8) + int(ADC_data) + + I2C_device.write_bytes(0x06, 00) # Config registers IO-expander + I2C_device.write_bytes(0x07, 00) # Config registers IO-expander + I2C_device.write_bytes(0x03, (0x0F ^ (0x01 << ADC_NR))) + + bit_array = "{0:{fill}24b}".format(data, fill='0') +# print(bit_array) + for bit in bit_array: + for clk in range(2): + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | (int(bit) << SDIO[ADC_NR]) + I2C_device.write_bytes(0x02, Write_data) + # sleep(sleep_time) + + for extra_cylces in range(5): + for clk in range(2): + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | (0 << SDIO[ADC_NR]) + I2C_device.write_bytes(0x02, Write_data) + + Write_data = (0x00 | (0 << SCLK[ADC_NR]) | ( 0 << SDIO[ADC_NR] )) + I2C_device.write_bytes(0x02, Write_data) + I2C_device.write_bytes(0x03, 0x0F) + +def Read_byte_ADC(ADC_reg_address, ADC_bytes=0, ADC_NR = 0, ADDRESS=0x20 ): + # + # Read Byte from the ADC + # + I2C_device = I2C(ADDRESS) + ADC_rw = 0x01 # 0 for write, 1 for read + + stri = "Read ADC from Address {:8x}".format(ADC_reg_address) + print(stri) + + data = ( ADC_rw << 15) + ( ADC_bytes << 13 ) + ADC_reg_address + +# print("write read command") + I2C_device.write_bytes(0x06, 00) + I2C_device.write_bytes(0x07, 00) + I2C_device.write_bytes(0x03, (0x0F ^ (0x01 << ADC_NR))) + + bit_array = "{0:{fill}16b}".format(data, fill='0') + for bit in bit_array: + for clk in range(2): + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | ( int(bit) << SDIO[ADC_NR] ) + I2C_device.write_bytes(0x02, Write_data) +# sleep(sleep_time) + I2C_device.write_bytes(0x03, 0x0F) +# print("read byte") + I2C_device.write_bytes(0x06, (1 << SDIO[ADC_NR])) + I2C_device.write_bytes(0x03, (0x0F ^ (0x01 << ADC_NR))) + read_bit = '' + for cnt in range(8*(ADC_bytes+1)): + for clk in [1,0]: # Read after faling edge + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | ( int(bit) << SDIO[ADC_NR] ) + I2C_device.write_bytes(0x02, Write_data) + ret_ack, ret_value = I2C_device.read_bytes(0x00, 1) + if ret_ack: + read_bit += str((int(ret_value, 16) >> SDIO[ADC_NR]) & 0x01) + else: + print("ACK nok") + I2C_device.write_bytes(0x03, 0x0F) + I2C_device.write_bytes(0x07, 00) + Write_data = 0x00 | (0 << SCLK[ADC_NR]) | ( 0 << SDIO[ADC_NR] ) + I2C_device.write_bytes(0x02, Write_data) + I2C_device.write_bytes(0x03, 0x0F) + stri = "Read back data is: {0:2x} ".format(int(read_bit, 2)) + print(stri) + return read_bit; + +def rcu_sensors(): + addr = 0x14 + Vref = 3.3 + one_step = Vref/(2**(16+1)) + I2C_device = I2C(addr) + I2C_device.write_bytes(0xB8, 0xB0) + sleep(1) + ret_ack, ret_value = I2C_device.read_last_reg(3) + if ret_ack: + stri = "Return value input 0 : 0x{0} ".format(ret_value) + print(stri) + if int(ret_value, 16) >= 0xC00000: + print("over range") + else: + steps = (int(ret_value, 16) & 0x1FFFFF) >> 6 + voltage = one_step * steps + string = "Voltage is {0:.4f}".format(voltage) + print(string) + else: + print("ACK nok") + sleep(1) + temp_slope = 93.5E-6 * 2**(16+1) / Vref + I2C_device.write_bytes(0xA0, 0xE0) + sleep(1) + ret_ack, ret_value = I2C_device.read_last_reg(3) + if ret_ack: + raw_value = (int(ret_value, 16) & 0x1FFFFF) >> 6 + temperature_K = (raw_value/temp_slope) + temperature = temperature_K-273 + stri = "Return value : 0x{0} Temperature : {1:.2f} gr. C".format(ret_value, temperature) + print(stri) + else: + print("ACK nok") + + +def rw_eeprom(value=0xAB): + ADDR = 0x50 + I2C_eeprom = I2C(0x53) + I2C_eeprom.write_bytes(0x00, value) + I2C_eeprom.write_pointer(0x00) + ret_ack, ret_value = I2C_eeprom.read_last_reg(1) + if ret_ack: + stri = "EEPROM readback : {0} ".format(ret_value) + print(stri) + else: + print("ACK nok") + +def power(state): + ADDRESS_IO = 0x76 + I2C_IO_device = I2C(ADDRESS_IO) + I2C_IO_device.write_bytes(0x06, 00) + if state=='ON': + print("Switch ON") + bits_to_set = 0x40 + I2C_IO_device.write_bytes(0x02, bits_to_set) + else: + I2C_IO_device.write_bytes(0x06, 0xFF) + print("Switch OFF") + bits_to_set = 0x0 + + + +def set_gain(gain, channel): + string = "Set Channel {0} to a gain of {1} dB".format(channel, gain) + print(string) + if channel <= 1: + ADDRESS_IO = 0x75 + max_gain = 15 + min_gain = -6 + elif channel <= 2: + ADDRESS_IO = 0x76 + max_gain = 20 + min_gain = -4 + else: + print(" wrong channel number 0 <= Channel <= 2 channel set to 2") + channel = 2 + ADDRESS_IO = 0x76 + max_gain = 20 + min_gain = -4 + if min_gain <= gain <= max_gain: + set_gain = max_gain-gain + else: + set_gain = max_gain-min_gain + bits_to_set = set_gain + I2C_IO_device = I2C(ADDRESS_IO) + I2C_IO_device.write_bytes(0x06, 00) + I2C_IO_device.write_bytes(0x07, 00) + if channel == 0: + I2C_IO_device.write_bytes(0x02, bits_to_set) + elif channel == 1: + I2C_IO_device.write_bytes(0x03, bits_to_set) + elif channel == 2: + I2C_IO_device.write_bytes(0x02, bits_to_set | 0x40) + else: + print("no update done") + +if PWR_RST: + power('OFF') + sleep(1) + +power('ON') + +if CHECK_I2C_DEVICES: + blink_led(address = 0x76, times=6) + rw_eeprom(0xAC) + rcu_sensors() + +for cnt in range(3): + if SET_ADC : + ADC_address = 0x3A # see address table + ADC_data = 0x00 # 8 bits data + ADC_bytes = 0x00 # 00 / 11 + 1 bytes + ADCNR = ADC_ORDER[cnt] + set_gain(10, ADCNR) + stri = "Set channel {}".format(ADCNR) + print(stri) + + Write_byte_ADC(0x5F, 0x14, ADC_NR = ADCNR) # ILAS normal mode standard value + Write_byte_ADC(0x15, 0x07, ADC_NR = ADCNR) # CML level + Write_byte_ADC(0x3A, 0x01, ADC_NR=ADCNR) # SYNC / SYSREF control +# Write_byte_ADC(0x60, 0x80, ADC_NR=ADCNR) # Single ended SYNC pulse + if ADCNR == 2: + Write_byte_ADC(0x14, 0x05, ADC_NR=ADCNR) # ADC ON + Write_byte_ADC(0xFF, 0x01, ADC_NR = ADCNR) # transfer settings + Read_byte_ADC(0x0A, ADC_NR = ADCNR) + + diff --git a/set_adc_tm.py b/set_adc_tm.py new file mode 100644 index 0000000..4b1ff4e --- /dev/null +++ b/set_adc_tm.py @@ -0,0 +1,265 @@ +''' +Set ADC + +''' +import sys +import time +sys.path.insert(0,'.') +from I2C_serial import * + +sleep_time=0.05 +CHECK_I2C_DEVICES = False +WRITE_DATA = True +READ_BYTE = True +PWR_RST = False +SET_ADC = True + +CS = 0 + +SCLK = [1, 3, 5] +SDIO = [0, 2, 4] +ADC_ORDER = [0, 1, 2] + + +def off_on(address=0x76): + print("Off_on") + I2C_device = I2C(address) + I2C_device.write_bytes(0x06, 00) + I2C_device.write_bytes(0x07, 00) + I2C_device.write_bytes(0x02, 0x0) + I2C_device.write_bytes(0x03, 0x8) + sleep(5) + I2C_device.write_bytes(0x02, 0xF0) + I2C_device.write_bytes(0x03, 0xF0) + +def blink_led(address=0x76, times = 5): + print("Blink LED") + I2C_device = I2C(address) + I2C_device.write_bytes(0x07, 00) + for cnt in range(times): + I2C_device.write_bytes(0x03, 0x40) + sleep(0.5) + I2C_device.write_bytes(0x03, 0x80) + sleep(0.5) + I2C_device.write_bytes(0x03, 0x40) + + +def Write_byte_ADC(ADC_reg_address, ADC_data, ADC_bytes=1, ADC_NR = 0, ADDRESS=0x20): + # + # Write Byte to the ADC + # + I2C_device = I2C(ADDRESS) + ADC_rw = 0x00 # 0 for write, 1 for read + stri = "Write : {0:2x} to Address : {1:2x}".format(ADC_data, ADC_reg_address) + print(stri) + data = ( ADC_rw << 23) + ( ADC_bytes << 21 ) + (ADC_reg_address << 8) + int(ADC_data) + + I2C_device.write_bytes(0x06, 00) # Config registers IO-expander + I2C_device.write_bytes(0x07, 00) # Config registers IO-expander + I2C_device.write_bytes(0x03, (0x0F ^ (0x01 << ADC_NR))) + + bit_array = "{0:{fill}24b}".format(data, fill='0') + print(bit_array) + for bit in bit_array: + for clk in range(2): + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | (int(bit) << SDIO[ADC_NR]) + I2C_device.write_bytes(0x02, Write_data) + # sleep(sleep_time) + + for extra_cylces in range(5): + for clk in range(2): + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | (0 << SDIO[ADC_NR]) + I2C_device.write_bytes(0x02, Write_data) + + Write_data = (0x00 | (0 << SCLK[ADC_NR]) | ( 0 << SDIO[ADC_NR] )) + I2C_device.write_bytes(0x02, Write_data) + I2C_device.write_bytes(0x03, 0x0F) + +def Read_byte_ADC(ADC_reg_address, ADC_bytes=0, ADC_NR = 0, ADDRESS=0x20 ): + # + # Read Byte from the ADC + # + I2C_device = I2C(ADDRESS) + ADC_rw = 0x01 # 0 for write, 1 for read + + stri = "Read ADC from Address {:8x}".format(ADC_reg_address) + print(stri) + + data = ( ADC_rw << 15) + ( ADC_bytes << 13 ) + ADC_reg_address + +# print("write read command") + I2C_device.write_bytes(0x06, 00) + I2C_device.write_bytes(0x07, 00) + I2C_device.write_bytes(0x03, (0x0F ^ (0x01 << ADC_NR))) + + bit_array = "{0:{fill}16b}".format(data, fill='0') + for bit in bit_array: + for clk in range(2): + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | ( int(bit) << SDIO[ADC_NR] ) + I2C_device.write_bytes(0x02, Write_data) +# sleep(sleep_time) + I2C_device.write_bytes(0x03, 0x0F) +# print("read byte") + I2C_device.write_bytes(0x06, (1 << SDIO[ADC_NR])) + I2C_device.write_bytes(0x03, (0x0F ^ (0x01 << ADC_NR))) + read_bit = '' + for cnt in range(8*(ADC_bytes+1)): + for clk in [1,0]: # Read after faling edge + Write_data = 0x00 | (clk << SCLK[ADC_NR]) | ( int(bit) << SDIO[ADC_NR] ) + I2C_device.write_bytes(0x02, Write_data) + ret_ack, ret_value = I2C_device.read_bytes(0x00, 1) + if ret_ack: + read_bit += str((int(ret_value, 16) >> SDIO[ADC_NR]) & 0x01) + else: + print("ACK nok") + I2C_device.write_bytes(0x03, 0x0F) + I2C_device.write_bytes(0x07, 00) + Write_data = 0x00 | (0 << SCLK[ADC_NR]) | ( 0 << SDIO[ADC_NR] ) + I2C_device.write_bytes(0x02, Write_data) + I2C_device.write_bytes(0x03, 0x0F) + stri = "Read back data is: {0:2x} ".format(int(read_bit, 2)) + print(stri) + return read_bit; + +def rcu_sensors(): + addr = 0x14 + Vref = 3.3 + one_step = Vref/(2**(16+1)) + I2C_device = I2C(addr) + I2C_device.write_bytes(0xB8, 0xB0) + sleep(1) + ret_ack, ret_value = I2C_device.read_last_reg(3) + if ret_ack: + stri = "Return value input 0 : 0x{0} ".format(ret_value) + print(stri) + if int(ret_value, 16) >= 0xC00000: + print("over range") + else: + steps = (int(ret_value, 16) & 0x1FFFFF) >> 6 + voltage = one_step * steps + string = "Voltage is {0:.4f}".format(voltage) + print(string) + else: + print("ACK nok") + sleep(1) + temp_slope = 93.5E-6 * 2**(16+1) / Vref + I2C_device.write_bytes(0xA0, 0xE0) + sleep(1) + ret_ack, ret_value = I2C_device.read_last_reg(3) + if ret_ack: + raw_value = (int(ret_value, 16) & 0x1FFFFF) >> 6 + temperature_K = (raw_value/temp_slope) + temperature = temperature_K-273 + stri = "Return value : 0x{0} Temperature : {1:.2f} gr. C".format(ret_value, temperature) + print(stri) + else: + print("ACK nok") + + +def rw_eeprom(value=0xAB): + ADDR = 0x50 + I2C_eeprom = I2C(0x53) + I2C_eeprom.write_bytes(0x00, value) + I2C_eeprom.write_pointer(0x00) + ret_ack, ret_value = I2C_eeprom.read_last_reg(1) + if ret_ack: + stri = "EEPROM readback : {0} ".format(ret_value) + print(stri) + else: + print("ACK nok") + +def power(state): + ADDRESS_IO = 0x76 + I2C_IO_device = I2C(ADDRESS_IO) + I2C_IO_device.write_bytes(0x06, 00) + if state=='ON': + print("Switch ON") + bits_to_set = 0x40 + I2C_IO_device.write_bytes(0x02, bits_to_set) + else: + I2C_IO_device.write_bytes(0x06, 0xFF) + print("Switch OFF") + bits_to_set = 0x0 + + + +def set_gain(gain, channel): + string = "Set Channel {0} to a gain of {1} dB".format(channel, gain) + print(string) + if channel <= 1: + ADDRESS_IO = 0x75 + max_gain = 15 + min_gain = -6 + elif channel <= 2: + ADDRESS_IO = 0x76 + max_gain = 20 + min_gain = -4 + else: + print(" wrong channel number 0 <= Channel <= 2 channel set to 2") + channel = 2 + ADDRESS_IO = 0x76 + max_gain = 20 + min_gain = -4 + if min_gain <= gain <= max_gain: + set_gain = max_gain-gain + else: + set_gain = max_gain-min_gain + bits_to_set = set_gain + I2C_IO_device = I2C(ADDRESS_IO) + I2C_IO_device.write_bytes(0x06, 00) + I2C_IO_device.write_bytes(0x07, 00) + if channel == 0: + I2C_IO_device.write_bytes(0x02, bits_to_set) + elif channel == 1: + I2C_IO_device.write_bytes(0x03, bits_to_set) + elif channel == 2: + I2C_IO_device.write_bytes(0x02, bits_to_set | 0x40) + else: + print("no update done") + +if PWR_RST: + power(False) + sleep(1) + power(True) + +if CHECK_I2C_DEVICES: + blink_led(address = 0x76, times=6) + rw_eeprom(0xAC) + rcu_sensors() + + +ADC_address = 0x3A # see address table +ADC_data = 0x00 # 8 bits data +ADC_bytes = 0x00 # 00 / 11 + 1 bytes +ADCNR = 1 + +ADCNR = 1 +stri = "Set channel {} in test mode".format(ADCNR) +print(stri) +# Write_byte_ADC(0x5F, 0x14, ADC_NR=ADCNR) # OK ILAS normal mode standard value +# Write_byte_ADC(0x15, 0x07, ADC_NR=ADCNR) # OK CML level +#Write_byte_ADC(0x3A, 0x01, ADC_NR=ADCNR) # SYNC / SYSREF control +# Write_byte_ADC(0x60, 0x80, ADC_NR=ADCNR) # Single ended SYNC pulse +#Write_byte_ADC(0xFF, 0x01, ADC_NR = ADCNR) # ILAS normal mode standard value +#Read_byte_ADC(0x3A, ADC_NR = ADCNR) +#Write_byte_ADC(0x08, 0x35, ADC_NR = 0) # ADC OFF +#Write_byte_ADC(0x08, 0x35, ADC_NR = 1) # ADC OFF +#Write_byte_ADC(0x08, 0x35, ADC_NR = 2) # ADC OFF +#Write_byte_ADC(0x08, 0x00, ADC_NR = 0) # ADC ON +#Write_byte_ADC(0x08, 0x00, ADC_NR = 1) # ADC ON +#Write_byte_ADC(0x14, 0x05, ADC_NR = 2) # Invert signal +#Write_byte_ADC(0x0D, 0x06, ADC_NR = 0) # Ramp output +#Write_byte_ADC(0xFF, 0x01, ADC_NR = 0) # transfer settings +#Write_byte_ADC(0x0D, 0x06, ADC_NR = 1) # Ramp output +#Write_byte_ADC(0xFF, 0x01, ADC_NR = 1) # transfer settings +#Write_byte_ADC(0x0D, 0x06, ADC_NR = 2) # Ramp output +#Write_byte_ADC(0xFF, 0x01, ADC_NR = 2) # transfer settings +# +power('OFF') +blink_led(address=0x76, times=5) +power('ON') +blink_led(address=0x76, times=5) +power('OFF') +blink_led(address=0x76, times=5) +power('ON') + diff --git a/set_clk.py b/set_clk.py new file mode 100644 index 0000000..bd6da61 --- /dev/null +++ b/set_clk.py @@ -0,0 +1,159 @@ +''' +Set ADC + +''' +import sys +import time +sys.path.insert(0,'.') +from I2C_serial import * + +sleep_time=0.05 +PWR_RST = False +SET_PLL = True +READ_LOCK = True +INT_POWER_CYCLE = False +RESET_PLL = False +UPDATE_PLL = False +READ_ALL = False + +CS = 6 +SCLK = 4 +SDO = 5 +SDI = 7 + +def Write_byte_PLL(reg_address, wr_data, ADDRESS=0x20): + # + # Write Byte to the ADC + # + I2C_device = I2C(ADDRESS) + PLL_rw = 0x00 # 0 for write, 1 for read + stri = "Write : 0x{0:{fill}2x} to Address : 0x{1:{fill}2x}".format(wr_data, reg_address, fill='0') + print(stri) + I2C_device.write_bytes(0x06, 0x2C) + data = ( reg_address << 9 ) + ( PLL_rw << 8 )+ wr_data + + bit_array = "{0:{fill}16b}".format(data, fill='0') + I2C_device.write_bytes(0x02, 0x02 | (0x1 << CS)) + for bit in bit_array: + for clk in range(2): + Write_data = 0x02 | (0 << CS) | (clk << SCLK) | ( int(bit) << SDI) + I2C_device.write_bytes(0x02, Write_data) + for clk in range(2): + Write_data = 0x02 | (0 << CS) | (clk << SCLK) + I2C_device.write_bytes(0x02, Write_data) + for clk in range(2): + Write_data = 0x02 | (1 << CS) | (clk << SCLK) + I2C_device.write_bytes(0x02, Write_data) + + Write_data = 0x02 | (1 << CS) | (0 << SCLK) | (0 << SDI) + I2C_device.write_bytes(0x02, Write_data) + +def Read_byte_PLL(reg_address, nof_bytes=1, ADDRESS=0x20 ): + # + # Read Byte from the ADC + # + I2C_device = I2C(ADDRESS) + PLL_rw = 0x01 # 0 for write, 1 for read + + I2C_device.write_bytes(0x06, 0x2C) + data = ( reg_address << 7 ) + PLL_rw + +# print("write read command") + + bit_array = "{0:{fill}8b}".format(data, fill='0') + for bit in bit_array: + for clk in range(2): + Write_data = 0x02 | (0 << CS) | (clk << SCLK) | ( int(bit) << SDI) + I2C_device.write_bytes(0x02, Write_data) +# sleep(sleep_time) + +# print("read byte") + read_bit = '' + for cnt in range(8*nof_bytes): + for clk in [0, 1]: # Read after rizing edge + Write_data = 0x02 | (clk << SCLK) | ( int(bit) << SDI ) + I2C_device.write_bytes(0x02, Write_data) + ret_ack, ret_value = I2C_device.read_bytes(0x00, 1) +# stri= "ret_value = {}".format(int(ret_value,16)) +# print(stri) + if ret_ack: + read_bit += str((int(ret_value, 16) >> SDO) & 0x01) + else: + print("ACK nok") + Write_data = 0x02 | (1 << CS) | (0 << SCLK) | (0 << SDI) + I2C_device.write_bytes(0x02, Write_data) + stri = "Read back at address 0x{0:{fill}2x} result : 0x{1:{fill}2x} ".format(reg_address, int(read_bit, 2), fill='0') + print(stri) + return read_bit; + +def power(state): + ADDRESS_IO = 0x20 + I2C_IO_device = I2C(ADDRESS_IO) + I2C_IO_device.write_bytes(0x06, 0x2C) + I2C_IO_device.write_bytes(0x07, 00) + if state: + bits_to_set = 0x42 + else: + bits_to_set = 0x40 + I2C_IO_device.write_bytes(0x02, bits_to_set) + + +if PWR_RST : + power(False) + sleep(1) + power(True) + + +if INT_POWER_CYCLE : + print("Power OFF") + Write_byte_PLL(0x03, 0x88) # Device down + sleep(1) + print("Power ON") + Write_byte_PLL(0x03, 0x08) # Device up + print("Done") + +if RESET_PLL : + print("Reset PLL") + Write_byte_PLL(0x03, 0x0C) # Device reset + print("Enable PLL") + Write_byte_PLL(0x03, 0x08) # Device reset + sleep(0.1) + print("Done") + +if SET_PLL : + # Check PLL is in lock +# Read_byte_PLL(0x00, nof_bytes = 23) + +# Write_byte_PLL(0x01, 0x00) # cp inv = 0xF4 other 0xE4 +# Write_byte_PLL(0x02, 0x04) # cp inv = 0xF4 other 0xE4 + Write_byte_PLL(0x05, 0x97) + Write_byte_PLL(0x06, 0x10) # cp inv = 0xF4 other 0xE4 + Write_byte_PLL(0x07, 0x04) # Divider R = 1 dec + Write_byte_PLL(0x08, 0x01) + Write_byte_PLL(0x07, 0x00) + Write_byte_PLL(0x09, 0x10) # reset + Write_byte_PLL(0x0A, 0x14) + Write_byte_PLL(0x09, 0x00) +# Write_byte_PLL(0x0D, 0x02) # Dig CLK = 200/2 = 100 MHz + Write_byte_PLL(0x0D, 0x01) # Dig CLK = 200/1 = 200 MHz + Write_byte_PLL(0x0F, 0x01) # RCU CLK = 200/1 = 200 MHz + Write_byte_PLL(0x11, 0x01) # PPS ref CLK = 200/1 = 200 MHz + Write_byte_PLL(0x13, 0x01) # T.P. CLK = 200/1 = 200 MHz + + +if READ_LOCK: + ret_value = Read_byte_PLL(0x00, nof_bytes = 1) + status_pll = int(ret_value,2) + if status_pll == 0x04: + print("PLL in lock") + elif (status_pll & 0x10) > 0: + print("Not Locked --> No 10 MHz ref") + else: + print("Not locked --> PLL Error") + +if READ_ALL: + Read_byte_PLL(0x00, nof_bytes = 23) + + +if UPDATE_PLL: + Write_byte_PLL(0x05, 0x97) diff --git a/set_clk_tm.py b/set_clk_tm.py new file mode 100644 index 0000000..eb0b022 --- /dev/null +++ b/set_clk_tm.py @@ -0,0 +1,160 @@ +''' +Set ADC + +''' +import sys +import time +sys.path.insert(0,'.') +from I2C_serial import * + +sleep_time=0.05 +PWR_RST = False +SET_PLL = True +READ_LOCK = True +INT_POWER_CYCLE = False +RESET_PLL = False +UPDATE_PLL = False +READ_ALL = False + +CS = 6 +SCLK = 4 +SDO = 5 +SDI = 7 + +def Write_byte_PLL(reg_address, wr_data, ADDRESS=0x20): + # + # Write Byte to the ADC + # + I2C_device = I2C(ADDRESS) + PLL_rw = 0x00 # 0 for write, 1 for read + stri = "Write : 0x{0:{fill}2x} to Address : 0x{1:{fill}2x}".format(wr_data, reg_address, fill='0') + print(stri) + I2C_device.write_bytes(0x06, 0x2C) + data = ( reg_address << 9 ) + ( PLL_rw << 8 )+ wr_data + + bit_array = "{0:{fill}16b}".format(data, fill='0') + I2C_device.write_bytes(0x02, 0x02 | (0x1 << CS)) + for bit in bit_array: + for clk in range(2): + Write_data = 0x02 | (0 << CS) | (clk << SCLK) | ( int(bit) << SDI) + I2C_device.write_bytes(0x02, Write_data) + for clk in range(2): + Write_data = 0x02 | (0 << CS) | (clk << SCLK) + I2C_device.write_bytes(0x02, Write_data) + for clk in range(2): + Write_data = 0x02 | (1 << CS) | (clk << SCLK) + I2C_device.write_bytes(0x02, Write_data) + + Write_data = 0x02 | (1 << CS) | (0 << SCLK) | (0 << SDI) + I2C_device.write_bytes(0x02, Write_data) + +def Read_byte_PLL(reg_address, nof_bytes=1, ADDRESS=0x20 ): + # + # Read Byte from the ADC + # + I2C_device = I2C(ADDRESS) + PLL_rw = 0x01 # 0 for write, 1 for read + + I2C_device.write_bytes(0x06, 0x2C) + data = ( reg_address << 7 ) + PLL_rw + +# print("write read command") + + bit_array = "{0:{fill}8b}".format(data, fill='0') + for bit in bit_array: + for clk in range(2): + Write_data = 0x02 | (0 << CS) | (clk << SCLK) | ( int(bit) << SDI) + I2C_device.write_bytes(0x02, Write_data) +# sleep(sleep_time) + +# print("read byte") + read_bit = '' + for cnt in range(8*nof_bytes): + for clk in [0, 1]: # Read after rizing edge + Write_data = 0x02 | (clk << SCLK) | ( int(bit) << SDI ) + I2C_device.write_bytes(0x02, Write_data) + ret_ack, ret_value = I2C_device.read_bytes(0x00, 1) +# stri= "ret_value = {}".format(int(ret_value,16)) +# print(stri) + if ret_ack: + read_bit += str((int(ret_value, 16) >> SDO) & 0x01) + else: + print("ACK nok") + Write_data = 0x02 | (1 << CS) | (0 << SCLK) | (0 << SDI) + I2C_device.write_bytes(0x02, Write_data) + stri = "Read back at address 0x{0:{fill}2x} result : 0x{1:{fill}2x} ".format(reg_address, int(read_bit, 2), fill='0') + print(stri) + return read_bit; + +def power(state): + ADDRESS_IO = 0x20 + I2C_IO_device = I2C(ADDRESS_IO) + I2C_IO_device.write_bytes(0x06, 0x2C) + I2C_IO_device.write_bytes(0x07, 00) + if state: + bits_to_set = 0x42 + else: + bits_to_set = 0x40 + I2C_IO_device.write_bytes(0x02, bits_to_set) + + +if PWR_RST : + power(False) + sleep(1) + power(True) + + +if INT_POWER_CYCLE : + print("Power OFF") + Write_byte_PLL(0x03, 0x88) # Device down + sleep(1) + print("Power ON") + Write_byte_PLL(0x03, 0x08) # Device up + print("Done") + +if RESET_PLL : + print("Reset PLL") + Write_byte_PLL(0x03, 0x0C) # Device reset + print("Enable PLL") + Write_byte_PLL(0x03, 0x08) # Device reset + sleep(0.1) + print("Done") + +if SET_PLL : + # Check PLL is in lock +# Read_byte_PLL(0x00, nof_bytes = 23) + +# Write_byte_PLL(0x01, 0x00) # cp inv = 0xF4 other 0xE4 +# Write_byte_PLL(0x02, 0x04) # cp inv = 0xF4 other 0xE4 +# Write_byte_PLL(0x05, 0x97) +# Write_byte_PLL(0x06, 0x10) # cp inv = 0xF4 other 0xE4 +# Write_byte_PLL(0x07, 0x04) # Divider R = 1 dec +# Write_byte_PLL(0x08, 0x01) +# Write_byte_PLL(0x07, 0x00) +# Write_byte_PLL(0x09, 0x10) # Divider N = 20 dec +# Write_byte_PLL(0x0A, 0x14) +# Write_byte_PLL(0x09, 0x00) + Write_byte_PLL(0x0D, 0x02) # Dig CLK = 200/2 = 100 MHz +# Write_byte_PLL(0x0C, 0x81) # Dig CLK delayed +# Write_byte_PLL(0x0D, 0x01) # Dig CLK = 200/1 = 200 MHz +# Write_byte_PLL(0x0F, 0x01) # RCU CLK = 200/1 = 200 MHz +# Write_byte_PLL(0x11, 0x01) # PPS ref CLK = 200/1 = 200 MHz +# Write_byte_PLL(0x13, 0x01) # T.P. CLK = 200/1 = 200 MHz + + +if READ_LOCK: + ret_value = Read_byte_PLL(0x00, nof_bytes = 1) + status_pll = int(ret_value,2) + if status_pll == 0x04: + print("PLL in lock") + elif (status_pll & 0x10) > 0: + print("Not Locked --> No 10 MHz ref") + else: + print("Not locked --> PLL Error") + +if READ_ALL: + Read_byte_PLL(0x00, nof_bytes = 23) + + +if UPDATE_PLL: + Write_byte_PLL(0x05, 0x97) -- GitLab