Skip to content
Snippets Groups Projects
Commit a350bbf3 authored by Gijs Schoonderbeek's avatar Gijs Schoonderbeek
Browse files
parents 8ba7b9fd 307b46bd
No related branches found
No related tags found
No related merge requests found
......@@ -26,12 +26,12 @@ else:
DEBUG = False
I2CBUSNR=5
sleep_time = 0.15
SET_PLL = True
READ_LOCK = False #True
READ_ALL = True #False
SET_PLL = False #True
READ_LOCK = True
READ_ALL = False
CHECK_EEPROM = False
PWR_RST = True #False
READ_SENSOR = True
PWR_RST = False #True #False
READ_SENSOR = False #True
READ_REGS = False #True
CLK_FREQ = '200MHz'
......@@ -151,13 +151,16 @@ def setup_pll(pll_frequency='200MHz') :
if pll_frequency == '160MHz':
print("Set PLL to 160 MHz mode")
pll_address = PLL_160M
I2C_device.write_bytes(0x03, 0x0F)
I2C_device.write_bytes(0x03, 0x08)
else:
print("Set PLL to 200 MHz mode")
pll_address=PLL_200M
I2C_device.write_bytes(0x03, 0xFF)
I2C_device.write_bytes(0x03, 0x28)
Write_byte_PLL(0x03, 0x0C, pll_address)
sleep(0.5)
Write_byte_PLL(0x03, 0x08, pll_address)
Write_byte_PLL(0x03, 0x08, pll_address)
Write_byte_PLL(0x04, 0x00, pll_address) # CF disable not used outputs, 00 enable all
Write_byte_PLL(0x04, 0xCF, pll_address) # CF disable not used outputs, 00 enable all
Write_byte_PLL(0x05, 0x97, pll_address)
Write_byte_PLL(0x06, 0x10, pll_address) # cp inv = 0xF4 other 0xE4
Write_byte_PLL(0x07, 0x04, pll_address) # Divider R = 1 dec
......@@ -169,11 +172,11 @@ def setup_pll(pll_frequency='200MHz') :
else:
Write_byte_PLL(0x0A, 0x14, pll_address)
Write_byte_PLL(0x09, 0x00, pll_address)
Write_byte_PLL(0x0C, 0x80, pll_address)
Write_byte_PLL(0x0D, 0x01, pll_address) # Dig CLK = 200/1 = 200 MHz
Write_byte_PLL(0x0F, 0x01, pll_address) # RCU CLK = 200/1 = 200 MHz
Write_byte_PLL(0x11, 0x01, pll_address) # PPS ref CLK = 200/1 = 200 MHz
Write_byte_PLL(0x13, 0x01, pll_address) # T.P. CLK = 200/1 = 200 MHz
Write_byte_PLL(0x0C, 0x8F, pll_address)
Write_byte_PLL(0x0D, 0x88, pll_address) # Dig CLK = 200/1 = 200 MHz
Write_byte_PLL(0x0F, 0x08, pll_address) # RCU CLK = 200/1 = 200 MHz
Write_byte_PLL(0x11, 0x08, pll_address) # PPS ref CLK = 200/1 = 200 MHz
Write_byte_PLL(0x13, 0x88, pll_address) # T.P. CLK = 200/1 = 200 MHz
def power(state):
......@@ -274,9 +277,9 @@ def read_temp():
one_step = Vref/(2**(16))
I2C_device = I2C(addr, BUSNR=I2CBUSNR)
temp_slope = 93.5E-6 * 2**(16+1) / Vref
sleep(0.5)
sleep(1.0)
I2C_device.write_bytes(0xA0, 0xC0)
sleep(0.5)
sleep(1.0)
ret_ack, ret_value = I2C_device.read_last_reg(3)
if ret_ack:
raw_value = (int(ret_value, 16) & 0x1FFFFF) >> 6
......@@ -287,9 +290,36 @@ def read_temp():
else:
print("Error reading tempeature")
def read_lol(pll_frequency='200MHz'):
I2C_IO_device_A = I2C(0x20, BUSNR=I2CBUSNR)
I2C_IO_device_A.write_bytes(0x06, 0x2C) # '0' is output
I2C_IO_device_A.write_bytes(0x07, 0x00) # '0' is output
I2C_IO_device_B = I2C(0x21, BUSNR=I2CBUSNR)
I2C_IO_device_B.write_bytes(0x06, 0x2C) # '0' is output
I2C_IO_device_B.write_bytes(0x07, 0xFF) # '0' is output
ack, ret_value = I2C_IO_device_B.read_bytes(0x01, 1)
status_reg = int(ret_value,16)
if (pll_frequency=='200MHz') & ((status_reg & 0x10) > 0):
print("lost lock 200MHz")
if ((status_reg & 0x20) > 0) & (pll_frequency=='160MHz'):
print("lost lock 160MHz")
ack, ret_value = I2C_IO_device_A.read_bytes(0x01, 1)
old_reg = int(ret_value,16)
I2C_IO_device_A.write_bytes(0x03, (old_reg | 0x10)) # '0' is output
sleep(1)
I2C_IO_device_A.write_bytes(0x03, (old_reg & 0xEF)) # '0' is output
#if READ_REGS:
# read_all_regs_pll(CLK_FREQ)
read_temp()
if CHECK_EEPROM :
wr_rd_eeprom()
......@@ -324,3 +354,4 @@ if READ_ALL:
if READ_SENSOR:
apsct_sensors()
read_lol(CLK_FREQ)
'''
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.
Set CCD
'''
import sys
import time
import math
sys.path.insert(0,'.')
import os
if os.name =="posix":
from I2C_serial_pi2 import *
else:
from I2C_serial import *
DEBUG = False
I2CBUSNR=1
sleep_time = 0.05
SET_PLL = True
READ_LOCK = True
READ_ALL = True #False
CHECK_EEPROM = False #True
PWR_RST = False
READ_SENSOR = False
READ_SET_FAN = True
CLK_FREQ = '200MHz'
dev_i2c_eeprom = I2C(0x50)
dev_i2c_eeprom.bus_nr = I2CBUSNR
CS = 6
SCLK = 4
SDO = 5
SDI = 7
PLL_addr = 0x20
def Write_byte_PLL(reg_address, wr_data, ADDRESS=0x20):
#
# Write Byte to the ADC
#
I2C_device = I2C(ADDRESS, BUSNR=I2CBUSNR)
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)
if DEBUG:
rd_bytes = I2C_device.read_bytes(0x06, 1)
stri = "IO expander wrote 0x{0:x}, read 0x{1}".format(0x2C, rd_bytes[1])
print(stri)
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)
if DEBUG:
read_bits = Read_byte_PLL(reg_address, nof_bytes=1, ADDRESS=ADDRESS)
# stri = "Bits written 0x{0:x} to register 0x{1:x} read from PLL are {2}".format(wr_data, reg_address, read_bits)
# print(stri)
def Read_byte_PLL(reg_address, nof_bytes=1, ADDRESS=0x20 ):
#
# Read Byte from the ADC
#
I2C_device = I2C(ADDRESS, BUSNR=I2CBUSNR)
PLL_rw = 0x01 # 0 for write, 1 for read
I2C_device.write_bytes(0x06, 0x2C)
data = ( reg_address << 7 ) + PLL_rw
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 setup_pll() :
I2C_device = I2C(0x20, BUSNR=I2CBUSNR) #clock selection
I2C_device.write_bytes(0x07, 0x00)
Write_byte_PLL(0x03, 0x08, PLL_addr)
Write_byte_PLL(0x04, 0xFF, PLL_addr) # CF disable not used outputs, 00 enable all
Write_byte_PLL(0x05, 0xD7, PLL_addr)
Write_byte_PLL(0x06, 0xE0, PLL_addr) # cp inv = 0xF4 other 0xE4
Write_byte_PLL(0x07, 0x04, PLL_addr) # Divider R = 1 dec
Write_byte_PLL(0x08, 0x01, PLL_addr)
Write_byte_PLL(0x07, 0x00, PLL_addr)
Write_byte_PLL(0x09, 0x10, PLL_addr)
Write_byte_PLL(0x0A, 0x01, PLL_addr)
Write_byte_PLL(0x09, 0x00, PLL_addr)
Write_byte_PLL(0x0B, 0x00, PLL_addr)
Write_byte_PLL(0x0D, 0x01, PLL_addr)
Write_byte_PLL(0x0E, 0x00, PLL_addr)
Write_byte_PLL(0x0F, 0x01, PLL_addr)
Write_byte_PLL(0x10, 0x00, PLL_addr)
Write_byte_PLL(0x11, 0x01, PLL_addr)
Write_byte_PLL(0x12, 0x00, PLL_addr)
Write_byte_PLL(0x13, 0x01, PLL_addr)
Write_byte_PLL(0x14, 0x01, PLL_addr)
def power(state):
I2C_IO_device_A = I2C(0x20, BUSNR=I2CBUSNR)
I2C_IO_device_A.write_bytes(0x06, 0x2C) # '0' is output
I2C_IO_device_A.write_bytes(0x07, 0x00) # '0' is output
I2C_IO_device_B = I2C(0x21, BUSNR=I2CBUSNR)
I2C_IO_device_B.write_bytes(0x06, 0x2C) # '0' is output
if state:
bits_to_set_A1 = 0x02 | (1 << CS) | (0 << SCLK) | (0 << SDI)
bits_to_set_A2 = 0x04
bits_to_set_B1 = 0x02 | (1 << CS) | (0 << SCLK) | (0 << SDI)
else:
bits_to_set_A1 = 0x00 | (1 << CS) | (0 << SCLK) | (0 << SDI)
bits_to_set_A2 = 0x00
bits_to_set_B1 = 0x00 | (1 << CS) | (0 << SCLK) | (0 << SDI)
if DEBUG :
stri = "Bits to reg 0 0x{0:x}".format(bits_to_set_A1)
print(stri)
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)
def write_eeprom( data=0x01):
#
# Write the EEPROM with the serial number etc.
#
ret_ack, ret_value = dev_i2c_eeprom.read_bytes(0)
if ret_ack < 1:
print("EEPROM not found during write")
return False
else:
dev_i2c_eeprom.write_bytes(0x00, data)
sleep(0.1)
return True
def read_eeprom():
#
# Read the EEPROM with the serial number etc.
#
ret_ack, ret_value = dev_i2c_eeprom.read_last_reg(1)
if ret_ack < 1:
print("no EEPROM found during read")
return False
else:
ret_ack, ret_value = dev_i2c_eeprom.read_bytes(0x00, 1)
return ret_value
def wr_rd_eeprom(value=0x34):
#
# Write and Read the EEPROM to check functionality
#
if write_eeprom(value):
ret_value = read_eeprom()
stri = "Wrote to EEPROM: 0x{0:X}, Read from EEPROM: 0x{1} ".format(value, ret_value)
print(stri)
return True
def ccd_sensors():
for sens_line in range(7):
read_voltage(sens_line)
read_temp()
def read_voltage(input_channel=0):
addr = 0x74
Vref = 3.0
one_step = Vref/(2**(16))
I2C_device = I2C(addr, BUSNR=I2CBUSNR)
channel_select_word = 0xB0 | ((input_channel%2) << 3) | ((input_channel >> 1) & 0x7)
if DEBUG:
stri = "Word to select sens input is 0x{0:x}".format(channel_select_word)
print(stri)
sleep(0.1)
I2C_device.write_bytes(channel_select_word, 0xB8)
sleep(0.1)
ret_ack, ret_value = I2C_device.read_last_reg(3)
while len(ret_value) < 5:
sleep(0.1)
ret_ack, ret_value = I2C_device.read_last_reg(3)
if ret_ack:
if DEBUG:
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
voltage = ((4.7+2.2)/2.2)*2*voltage
string = "Voltage sens line {1} is {0:.4f} V".format(voltage, input_channel)
print(string)
else:
stri = " No ACK on device 0x{0:x} ".format(addr)
print(stri)
def read_temp():
Vref = 3.0
addr = 0x74
one_step = Vref/(2**(16))
I2C_device = I2C(addr, BUSNR=I2CBUSNR)
temp_slope = 93.5E-6 * 2**(16+1) / Vref
sleep(0.5)
I2C_device.write_bytes(0xA0, 0xC0)
sleep(0.5)
ret_ack, ret_value = I2C_device.read_last_reg(3)
sleep(0.5)
if ret_ack:
raw_value = (int(ret_value, 16) & 0x1FFFFF) >> 6
temperature_K = (raw_value/temp_slope)
temperature = temperature_K-273
stri = "Temperature : {0:.2f} gr. C".format(temperature)
print(stri)
else:
print("Error reading tempeature")
def set_fan_off():
#
# Switch CCD fan off
#
print("Switch fan off")
MAX6620 = 0x29
fanmonitor_dev = I2C(MAX6620)
fanmonitor_dev.bus_nr = 1 # 3
fanmonitor_dev.write_bytes(0x00, 0x10)
fanmonitor_dev.write_bytes(0x02, 0x08)
fanmonitor_dev.write_bytes(0x28, 0x00)
fanmonitor_dev.write_bytes(0x29, 0x00)
sleep(1)
def set_fan_speed(speed):
#
# Set control voltage of fan PNP
#
stri = "Set fan to {} %".format(speed)
print(stri)
MAX6620 = 0x29
reg_a = int((speed/100)*127) + 100
fanmonitor_dev = I2C(MAX6620)
fanmonitor_dev.bus_nr = 1
fanmonitor_dev.write_bytes(0x00, 0x00)
fanmonitor_dev.write_bytes(0x02, 0x08)
fanmonitor_dev.write_bytes(0x01, 0x0F)
fanmonitor_dev.write_bytes(0x06, 0x60)
fanmonitor_dev.write_bytes(0x28, reg_a)
fanmonitor_dev.write_bytes(0x29, 0x80)
sleep(1)
def read_tacho():
#
# Read the fan speed
#
MAX6620 = 0x29
REG_TACH_MSP_REGS = [ 0x10, 0x12, 0x14]
REG_TACH_LSP_REGS = [ 0x11, 0x13, 0x15]
TACH_PERIODS = 16
TACH_COUNT_FREQ = 8192
FAN_TACHS = 1
DEBUG=1
fanmonitor_dev = I2C(MAX6620)
fanmonitor_dev.bus_nr = 1 #3
fan_nr=0
ret_ack, ret_value = fanmonitor_dev.read_bytes(1)
if ret_ack < 1:
stri = " Device {0} at address 0x{1:X} not found".format("MAX6620", MAX6620)
print(stri)
status = False
ret_ack, tach_msb = fanmonitor_dev.read_bytes(REG_TACH_MSP_REGS[0], 1)
tach_msb = int(tach_msb, 16) & 0xFF
if tach_msb > 254:
if DEBUG :
tach_lsb = 255
tach = 99999
rpm = 0
else:
ret_ack, tach_lsb = fanmonitor_dev.read_bytes(REG_TACH_LSP_REGS[0], 1)
tach_lsb = int(tach_lsb, 16) & 0xE0
tach = tach_msb*16 + tach_lsb/8
rpm = float((TACH_COUNT_FREQ*TACH_PERIODS*60))/(FAN_TACHS*tach)
if DEBUG:
stri = "MSB: {0}, LSB: {1}, TACH : {2}, RPM : {3:6.2f}".format(tach_msb, tach_lsb, tach, rpm)
print(stri)
if CHECK_EEPROM :
wr_rd_eeprom()
if PWR_RST :
power(False)
sleep(10)
power(True)
if SET_PLL :
setup_pll()
if READ_LOCK:
ret_value = Read_byte_PLL(0x00, nof_bytes = 1, ADDRESS=PLL_addr)
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_SET_FAN :
read_tacho()
set_fan_off()
read_tacho()
sleep(5)
set_fan_speed(50)
sleep(5)
read_tacho()
set_fan_speed(100)
sleep(5)
read_tacho()
read_temp()
ccd_sensors()
if READ_SENSOR:
read_tacho()
read_temp()
ccd_sensors()
""" Script for testing I2C monitor sensors
"""
from test_common import *
from time import sleep
connect("opc.tcp://10.87.6.89:4841/")
#POLS=["UNB2_POL_SWITCH_PHY","UNB2_POL_SWITCH_1V2", "UNB2_FPGA_POL_CORE"]
POLS=["UNB2_FPGA_POL_CORE", "UNB2_FPGA_POL_ERAM", "UNB2_FPGA_POL_RXGXB", "UNB2_FPGA_POL_TXGXB" , "UNB2_FPGA_POL_HGXB", "UNB2_FPGA_POL_PGM", "UNB2_DC_DC_48V_12V", "UNB2_POL_QSFP_N01", "UNB2_POL_QSFP_N23", "UNB2_POL_SWITCH_1V2", "UNB2_POL_SWITCH_PHY" , "UNB2_POL_CLOCK"]
POINTS = ["VOUT", "IOUT", "TEMP"]
def check_values(values_array, atteribute_to_get):
for check_value in values_array :
if check_value > 50:
stri = "Error in {} {}".format(atteribute_to_get, check_value)
print(stri)
for loops in range(350):
print(loops)
for sens_cnt in range(len(POLS)*len(POINTS)):
atteribute_to_get = POLS[int(sens_cnt/3)] + "_" + POINTS[sens_cnt%3] + "_R"
ret_value = get_value(atteribute_to_get)
check_values(ret_value, atteribute_to_get)
sleep(5)
print("done")
......@@ -31,6 +31,7 @@ import sys
import spidev
DEBUG=False
CRC = False #True # CRC or packet counter
# We only have SPI bus 0 available to us on the Pi
bus = 0
......@@ -136,7 +137,7 @@ def read_link_status():
stri = "| "
for cnt_port in range(16):
if ret[3] & (0x8000 >> cnt_port) :
stri += "U | "
stri += "P | "
else:
stri += " | "
print(stri)
......@@ -166,6 +167,9 @@ def read_link_status():
stri += "Rx: Er "
if ret[2] & 0x40:
stri += "Rx FIFO Er "
# ret_err = read_switch(0x10+cnt,0x2E, pr_stri = False)
# stri += f"CRC errs: 0x{ret_err[1]:2x}"
# rest = write_switch_bytes(0x01,0x23, [0x40], pr_stri = False)
else:
stri += "link down "
print(stri)
......@@ -208,7 +212,7 @@ def read_link_status():
read_switch(0x10+cnt,0x2e)
if 1:
# alternative status read out, works better for SerDes lines.
for prt_cnt in [9, 10, 11,12, 13,14, 15]:
for prt_cnt in [8, 9, 10, 11,12, 13,14, 15]:
ret = read_switch(0x10+prt_cnt,0x28, pr_stri = False)
stri = "Port status of " + str(prt_cnt) + " "
if ret[1] & 0x02:
......@@ -235,6 +239,19 @@ def read_link_status():
stri += "Rx Fifo Err "
if ret[2] & 0x80:
stri += "Tx Fifo Err "
ret = read_switch(0x10+prt_cnt,0x2E, pr_stri = False)
if CRC:
rest = write_switch_bytes(0x10+prt_cnt,0x34, [0x20, 0x10], pr_stri = False)
rest = write_switch_bytes(0x10+prt_cnt,0x3e, [0x00, 0x00], pr_stri = False)
rest = write_switch_bytes(0x10+prt_cnt,0x20, [0xD0, 0x01], pr_stri = False)
stri += f"CRC err: {ret[0]} "
else:
rest = write_switch_bytes(0x10+prt_cnt,0x3e, [0x00, 0x00], pr_stri = False)
rest = write_switch_bytes(0x10+prt_cnt,0x20, [0xD0, 0x09], pr_stri = False)
stri += f"Rx packet count: {ret[0]} "
rest = write_switch_bytes(0x10+prt_cnt,0x22, [0x41, 0x41], pr_stri = False)
rest = write_switch_bytes(0x10+prt_cnt,0x3e, [0x00, 0x00], pr_stri = False)
rest = write_switch_bytes(0x10+prt_cnt,0x2E, [0x00, 0x00], pr_stri = False)
else:
stri += "Down "
print(stri)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment