Commit 500a9478 authored by Paolo Baesso's avatar Paolo Baesso

Merge branch 'PythonScripts'

parents 6d1984f9 9bac0797
......@@ -59,11 +59,13 @@ class TLU:
enableCore= True #Only need to run this once, after power-up
self.enableCore()
####### EEPROM AX3 testing
self.ax3eeprom= ATSHA204A(self.TLU_I2C, 0x64)
print "shiftR\tdatBit\tcrcBit\tcrcReg \n", self.ax3eeprom._CalculateCrc([255, 12, 54, 28, 134, 89], 3)
self.ax3eeprom._wake(True, True)
print self.ax3eeprom._GetCommandPacketSize(8)
#self.eepromAX3read()
doAtmel= False
if doAtmel:
self.ax3eeprom= ATSHA204A(self.TLU_I2C, 0x64)
print "shiftR\tdatBit\tcrcBit\tcrcReg \n", self.ax3eeprom._CalculateCrc([255, 12, 54, 28, 134, 89], 3)
self.ax3eeprom._wake(True, True)
print self.ax3eeprom._GetCommandPacketSize(8)
#self.eepromAX3read()
####### EEPROM AX3 testing end
# Instantiate clock chip and configure it (if necessary)
......@@ -74,7 +76,7 @@ class TLU:
if (int(parsed_cfg.get(section_name, "CONFCLOCK"), 16)):
#clkRegList= self.zeClock.parse_clk("./../../bitFiles/TLU_CLK_Config_v1e.txt")
clkRegList= self.zeClock.parse_clk(parsed_cfg.get(section_name, "CLOCK_CFG_FILE"))
self.zeClock.writeConfiguration(clkRegList)######
self.zeClock.writeConfiguration(clkRegList, self.verbose)######
self.zeClock.checkDesignID()
......@@ -118,12 +120,10 @@ class TLU:
dac_addr_module= int(parsed_cfg.get(section_name, "I2C_DACModule_Addr"), 16)
exp1_addr= int(parsed_cfg.get(section_name, "I2C_EXP1Module_Addr"), 16)
exp2_addr= int(parsed_cfg.get(section_name, "I2C_EXP2Module_Addr"), 16)
pmtCtrVMax= parsed_cfg.getfloat(section_name, "PMT_vCtrlMax")
self.pwdled= PWRLED(self.TLU_I2C, dac_addr_module, pmtCtrVMax, exp1_addr, exp2_addr)
self.pwdled= PWRLED(self.TLU_I2C, dac_addr_module, exp1_addr, exp2_addr)
self.pwdled.setVch(0, 1, True)
self.pwdled.setVch(1, 0.9, True)
self.pwdled.setVch(2, 0.8, True)
self.pwdled.setVch(3, 0.1, True)
#self.pwdled.setIndicatorRGB(1, [0, 0, 1])
#self.pwdled.setIndicatorRGB(2, [0, 0, 1])
#self.pwdled.setIndicatorRGB(3, [0, 0, 1])
......@@ -141,6 +141,8 @@ class TLU:
self.pwdled.allBlack()
#self.pwdled.kitt()
self.pwdled.allBlack()
self.pwdled.allRed()
self.pwdled.allWhite()
......@@ -847,6 +849,16 @@ class TLU:
cmd = int("0x0",16)
self.setInternalTrg(cmd)
# # Set the control voltages for the PMTs
PMT1_V= parsed_cfg.getfloat(section_name, "PMT1_V")
PMT2_V= parsed_cfg.getfloat(section_name, "PMT2_V")
PMT3_V= parsed_cfg.getfloat(section_name, "PMT3_V")
PMT4_V= parsed_cfg.getfloat(section_name, "PMT4_V")
self.pwdled.setVch(0, PMT1_V, True)
self.pwdled.setVch(1, PMT2_V, True)
self.pwdled.setVch(2, PMT3_V, True)
self.pwdled.setVch(3, PMT4_V, True)
# # Set pulse stretches
str0= parsed_cfg.getint(section_name, "in0_STR")
str1= parsed_cfg.getint(section_name, "in1_STR")
......
......@@ -16,6 +16,12 @@ HDMI2_clk = 0
HDMI3_clk = 0
HDMI4_clk = 0
# Control voltages for the PMTs
PMT1_V= 0.5
PMT2_V= 0.7
PMT3_V= 0.9
PMT4_V= 1
# Enable/disable differential LEMO CLOCK
LEMOclk = 1
......
......@@ -27,6 +27,8 @@ I2C_EXP1_Addr = 0x74
I2C_EXP2_Addr = 0x75
#I2C address of AD5665R on powermodule
I2C_DACModule_Addr = 0x1C
# Max value for control voltage on PMTs (usually 1 V)
PMT_vCtrlMax= 1.0
#I2C address of 1st expander PCA9539PW on powermodule
I2C_EXP1Module_Addr = 0x76
#I2C address of 2nd expander PCA9539PW on powermodule
......@@ -34,7 +36,7 @@ I2C_EXP2Module_Addr = 0x77
##CONFCLOCK 0= skip clock configuration, 1= configure si5345
CONFCLOCK= 0
CONFCLOCK= 1
CLOCK_CFG_FILE = /users/phpgb/workspace/myFirmware/AIDA/TLU_v1e/scripts/localClock.txt
......
# -*- coding: utf-8 -*-
import uhal
from I2CuHal import I2CCore
from I2CuHal2 import I2CCore
import StringIO
import time
class CFA632:
#Class to configure the CFA632 display
......@@ -16,8 +17,8 @@ class CFA632:
def writeSomething(self, i2ccmd):
mystop= True
print "Write random stuff"
print i2ccmd
print "Write to CFA632"
print "\t", i2ccmd
#myaddr= [int(i2ccmd)]
self.i2c.write( self.slaveaddr, i2ccmd, mystop)
return
......@@ -30,13 +31,72 @@ class LCD09052:
self.slaveaddr = slaveaddr
def test(self):
print "Testing the display"
print "\tTesting display (LCD09052)"
self.clear()
self.setBrightness(0)
time.sleep(0.2)
self.setBrightness(250)
time.sleep(0.2)
self.setBrightness(0)
time.sleep(0.2)
self.setBrightness(250)
for ipos in range(1, 18):
self.writeChar(33)
self.posCursor(1, ipos-1)
time.sleep(0.1)
self.writeChar(254)
self.posCursor(2, 1)
for ipos in range(1, 18):
self.writeChar(33)
self.posCursor(2, ipos-1)
time.sleep(0.1)
self.writeChar(254)
return
def clear(self):
### Clears the display and locates the curson on position (1,1), i.e. top left
i2ccmd= [4]
mystop= True
self.i2c.write( self.slaveaddr, i2ccmd, mystop)
def posCursor(self, line, pos):
### Position the cursor on a specific location
## line can be 1 (top) or 2 (bottom)
## pos can be [1, 16}
if ((line==1) or (line==2) and (1 <= pos <= 16)):
i2ccmd= [2, line, pos]
mystop= True
self.i2c.write( self.slaveaddr, i2ccmd, mystop)
else:
print "Cursor line can only be 1 or 2, position must be in range [1, 16]"
def setBrightness(self, value= 250):
### Sets the brightness level of the backlight.
## Value is an integer in range [0, 250]. 0= no light, 250= maximum light.
if value < 0:
print "setBrightness: minimum value= 0. Coherced to 0"
value = 0
if value > 250:
print "setBrightness: maximum value= 250. Coherced to 250"
value = 250
i2ccmd= [7, value]
mystop= True
self.i2c.write( self.slaveaddr, i2ccmd, mystop)
def writeChar(self, value):
### Writes a char in the current cursor position
## The curso is then shifted right one position
## value must be an integer corresponding to the ascii code of the character
i2ccmd= [1, value]
mystop= True
self.i2c.write( self.slaveaddr, i2ccmd, mystop)
def writeSomething(self, i2ccmd):
mystop= True
print "Write random stuff"
print i2ccmd
print "Write to LCD09052"
print "\t", i2ccmd
#myaddr= [int(i2ccmd)]
self.i2c.write( self.slaveaddr, i2ccmd, mystop)
return
......
# -*- coding: utf-8 -*-
"""
"""
import time
import uhal
verbose = True
################################################################################
# /*
# I2C CORE
# */
################################################################################
### Same as the class defined in I2CuHal.py but the register names are changed to
### comply with D. Newbold's notation. To be used in the Dune SFP Fanout (pc059a)
class I2CCore:
"""I2C communication block."""
# Define bits in cmd_stat register
startcmd = 0x1 << 7
stopcmd = 0x1 << 6
readcmd = 0x1 << 5
writecmd = 0x1 << 4
ack = 0x1 << 3
intack = 0x1
recvdack = 0x1 << 7
busy = 0x1 << 6
arblost = 0x1 << 5
inprogress = 0x1 << 1
interrupt = 0x1
def __init__(self, target, wclk, i2cclk, name="i2c", delay=None):
self.target = target
self.name = name
self.delay = delay
self.prescale_low = self.target.getNode("%s.ps_lo" % name)
self.prescale_high = self.target.getNode("%s.ps_hi" % name)
self.ctrl = self.target.getNode("%s.ctrl" % name)
self.data = self.target.getNode("%s.data" % name)
self.cmd_stat = self.target.getNode("%s.cmd_stat" % name)
self.wishboneclock = wclk
self.i2cclock = i2cclk
self.config()
def state(self):
status = {}
status["ps_low"] = self.prescale_low.read()
status["ps_hi"] = self.prescale_high.read()
status["ctrl"] = self.ctrl.read()
status["data"] = self.data.read()
status["cmd_stat"] = self.cmd_stat.read()
self.target.dispatch()
status["prescale"] = status["ps_hi"] << 8
status["prescale"] |= status["ps_low"]
for reg in status:
val = status[reg]
bval = bin(int(val))
if verbose:
print "\treg %s = %d, 0x%x, %s" % (reg, val, val, bval)
def clearint(self):
self.ctrl.write(0x1)
self.target.dispatch()
def config(self):
#INITIALIZATION OF THE I2S MASTER CORE
#Disable core
self.ctrl.write(0x0 << 7)
self.target.dispatch()
#Write pre-scale register
#prescale = int(self.wishboneclock / (5.0 * self.i2cclock)) - 1
#prescale = int(self.wishboneclock / (5.0 * self.i2cclock))
prescale = 0x0100 #FOR NOW HARDWIRED, TO BE MODIFIED
self.prescale_low.write(prescale & 0xff)
self.prescale_high.write((prescale & 0xff00) >> 8)
#Enable core
self.ctrl.write(0x1 << 7)
self.target.dispatch()
def checkack(self):
inprogress = True
ack = False
while inprogress:
cmd_stat = self.cmd_stat.read()
self.target.dispatch()
inprogress = (cmd_stat & I2CCore.inprogress) > 0
ack = (cmd_stat & I2CCore.recvdack) == 0
return ack
def delayorcheckack(self):
ack = True
if self.delay is None:
ack = self.checkack()
else:
time.sleep(self.delay)
ack = self.checkack()#Remove this?
return ack
################################################################################
# /*
# I2C WRITE
# */
################################################################################
def write(self, addr, data, stop=True):
"""Write data to the device with the given address."""
# Start transfer with 7 bit address and write bit (0)
nwritten = -1
addr &= 0x7f
addr = addr << 1
startcmd = 0x1 << 7
stopcmd = 0x1 << 6
writecmd = 0x1 << 4
#Set transmit register (write operation, LSB=0)
self.data.write(addr)
#Set Command Register to 0x90 (write, start)
self.cmd_stat.write(I2CCore.startcmd | I2CCore.writecmd)
self.target.dispatch()
ack = self.delayorcheckack()
if not ack:
self.cmd_stat.write(I2CCore.stopcmd)
self.target.dispatch()
print "no ack from I2C address", hex(addr>>1)
return nwritten
nwritten += 1
for val in data:
val &= 0xff
#Write slave memory address
self.data.write(val)
#Set Command Register to 0x10 (write)
self.cmd_stat.write(I2CCore.writecmd)
self.target.dispatch()
ack = self.delayorcheckack()
if not ack:
self.cmd_stat.write(I2CCore.stopcmd)
self.target.dispatch()
return nwritten
nwritten += 1
if stop:
self.cmd_stat.write(I2CCore.stopcmd)
self.target.dispatch()
return nwritten
################################################################################
# /*
# I2C READ
# */
################################################################################
def read(self, addr, n):
"""Read n bytes of data from the device with the given address."""
# Start transfer with 7 bit address and read bit (1)
data = []
addr &= 0x7f
addr = addr << 1
addr |= 0x1 # read bit
self.data.write(addr)
self.cmd_stat.write(I2CCore.startcmd | I2CCore.writecmd)
self.target.dispatch()
ack = self.delayorcheckack()
if not ack:
self.cmd_stat.write(I2CCore.stopcmd)
self.target.dispatch()
return data
for i in range(n):
if i < (n-1):
self.cmd_stat.write(I2CCore.readcmd) # <---
else:
self.cmd_stat.write(I2CCore.readcmd | I2CCore.ack | I2CCore.stopcmd) # <--- This tells the slave that it is the last word
self.target.dispatch()
ack = self.delayorcheckack()
val = self.data.read()
self.target.dispatch()
data.append(val & 0xff)
#self.cmd_stat.write(I2CCore.stopcmd)
#self.target.dispatch()
return data
################################################################################
# /*
# I2C WRITE-READ
# */
################################################################################
# def writeread(self, addr, data, n):
# """Write data to device, then read n bytes back from it."""
# nwritten = self.write(addr, data, stop=False)
# readdata = []
# if nwritten == len(data):
# readdata = self.read(addr, n)
# return nwritten, readdata
"""
SPI core XML:
<node description="SPI master controller" fwinfo="endpoint;width=3">
<node id="d0" address="0x0" description="Data reg 0"/>
<node id="d1" address="0x1" description="Data reg 1"/>
<node id="d2" address="0x2" description="Data reg 2"/>
<node id="d3" address="0x3" description="Data reg 3"/>
<node id="ctrl" address="0x4" description="Control reg"/>
<node id="divider" address="0x5" description="Clock divider reg"/>
<node id="ss" address="0x6" description="Slave select reg"/>
</node>
"""
class SPICore:
go_busy = 0x1 << 8
rising = 1
falling = 0
def __init__(self, target, wclk, spiclk, basename="io.spi"):
self.target = target
# Only a single data register is required since all transfers are
# 16 bit long
self.data = target.getNode("%s.d0" % basename)
self.control = target.getNode("%s.ctrl" % basename)
self.control_val = 0b0
self.divider = target.getNode("%s.divider" % basename)
self.slaveselect = target.getNode("%s.ss" % basename)
self.divider_val = int(wclk / spiclk / 2.0 - 1.0)
self.divider_val = 0x7f
self.configured = False
def config(self):
"Configure SPI interace for communicating with ADCs."
self.divider_val = int(self.divider_val) % 0xffff
if verbose:
print "Configuring SPI core, divider = 0x%x" % self.divider_val
self.divider.write(self.divider_val)
self.target.dispatch()
self.control_val = 0x0
self.control_val |= 0x0 << 13 # Automatic slave select
self.control_val |= 0x0 << 12 # No interrupt
self.control_val |= 0x0 << 11 # MSB first
# ADC samples data on rising edge of SCK
self.control_val |= 0x1 << 10 # change ouput on falling edge of SCK
# ADC changes output shortly after falling edge of SCK
self.control_val |= 0x0 << 9 # read input on rising edge
self.control_val |= 0x10 # 16 bit transfers
if verbose:
print "SPI control val = 0x%x = %s" % (self.control_val, bin(self.control_val))
self.configured = True
def transmit(self, chip, value):
if not self.configured:
self.config()
assert chip >= 0 and chip < 8
value &= 0xffff
self.data.write(value)
checkdata = self.data.read()
self.target.dispatch()
assert checkdata == value
self.control.write(self.control_val)
self.slaveselect.write(0xff ^ (0x1 << chip))
self.target.dispatch()
self.control.write(self.control_val | SPICore.go_busy)
self.target.dispatch()
busy = True
while busy:
status = self.control.read()
self.target.dispatch()
busy = status & SPICore.go_busy > 0
self.slaveselect.write(0xff)
data = self.data.read()
ss = self.slaveselect.read()
status = self.control.read()
self.target.dispatch()
#print "Received data: 0x%x, status = 0x%x, ss = 0x%x" % (data, status, ss)
return data
......@@ -8,13 +8,14 @@ from PCA9539PW import PCA9539PW # Library for serial line expander
class PWRLED:
#Class to configure the EEPROM
def __init__(self, i2ccore, DACaddr=0x1C, Exp1Add= 0x76, Exp2Add= 0x77):
def __init__(self, i2ccore, DACaddr=0x1C, PMTmaxV= 1, Exp1Add= 0x76, Exp2Add= 0x77):
print " TLU POWERMODULE Initializing..."
self.TLU_I2C = i2ccore
self.pwraddr = DACaddr
self.exp1addr= Exp1Add
self.exp2addr= Exp2Add
self.intRefOn= 0
self.vCtrlMax= PMTmaxV
self.verbose= True
#Map indicator color based on their position on the expanders: 0-15 are on expander 2, 16 to 31 on expander 1. One indicator is missing the blue component, hence
#the "-1" value.
......@@ -51,16 +52,20 @@ class PWRLED:
def setVch(self, channel, voltage, verbose=False):
# Note: the channel here is the DAC channel.
# The mapping with the power module is not one-to-one
if (verbose):
print " PWRModule: CONFIGURING VOLTAGE FOR PMT", channel+1
print "\tVcontrol=", voltage
if ((channel < 0) | (3 < channel )):
print "PWRModule: channel should be comprised between 0 and 3"
print "\tPWRModule: channel should be comprised between 0 and 3"
else:
if (voltage < 0):
print "PWRModule: voltage must be comprised between 0 and 1 V. Coherced to 0 V."
print "\tPWRModule: voltage cannot be negative. Coherced to 0 V."
voltage = 0
if (voltage > 1):
print "PWRModule: voltage must be comprised between 0 and 1 V. Coherced to 1 V."
voltage = 1
dacValue= voltage*65535
if (voltage > self.vCtrlMax):
print "\tPWRModule: voltage cannot exceed vCtrlMax. Coherced to vCtrlMax."
print "\tPWRModule: vCtrlMax=", self.vCtrlMax, "V. See config file to change this value."
voltage = self.vCtrlMax
dacValue= voltage*65535/self.vCtrlMax
self.zeDAC_pwr.writeDAC(int(dacValue), channel, verbose)
return
......
......@@ -3,6 +3,7 @@ import uhal
from I2CuHal import I2CCore
import StringIO
import csv
import sys
class si5345:
#Class to configure the Si5344 clock generator
......@@ -87,7 +88,7 @@ class si5345:
self.i2c.write( self.slaveaddr, myaddr, mystop)
#time.sleep(0.1)
res= self.i2c.read( self.slaveaddr, nwords)
print " Si5345 EPROM: "
print " Si5345 EEPROM: "
result="\t"
for iaddr in reversed(res):
result+="%#02x "%(iaddr)
......@@ -119,17 +120,28 @@ class si5345:
print "\t ", len(regSettingList), "elements"
return regSettingList
def writeConfiguration(self, regSettingList):
def writeConfiguration(self, regSettingList, verbose= 0):
print " Si5345 Writing configuration:"
toolbar_width = 38
if (verbose==1):
sys.stdout.write(" [%s]" % (" " * toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '['
#regSettingList= list(regSettingCsv)
counter=0
for item in regSettingList:
regAddr= int(item[0], 16)
regData=[0]
regData[0]= int(item[1], 16)
print "\t", counter, "Reg:", hex(regAddr), "Data:", regData
if (verbose > 1):
print "\t", counter, "Reg:", hex(regAddr), "Data:", regData
counter += 1
self.writeRegister(regAddr, regData, False)
if (not(counter % 10) and (verbose==1)):
sys.stdout.write("-")
sys.stdout.flush()
sys.stdout.write("\n")
print "\tSi5345 configuration done"
def checkDesignID(self):
regAddr= 0x026B
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment