# -*- coding: utf-8 -*-
import uhal
from I2CuHal import I2CCore
import time
from AD5665R import AD5665R
from E24AA025E48T import E24AA025E48T


manager = uhal.ConnectionManager("file://./TLUconnection.xml")
hw = manager.getDevice("fnal_tlu")

reg = hw.getNode("version").read()
hw.dispatch()
print "Firmware version = ", hex(reg)


# #First I2C core
print ("Instantiating master I2C core:")
master_I2C= I2CCore(hw, 10, 5, "i2c_master", None)
master_I2C.state()




#
# #######################################
enableCore= True #Only need to run this once, after power-up
if (enableCore):
   mystop=True
   print "  Write RegDir to set I/O[7] to output:"
   myslave= 0x21
   mycmd= [0x01, 0x7F]
   nwords= 1
   master_I2C.write(myslave, mycmd, mystop)


   mystop=False
   mycmd= [0x01]
   master_I2C.write(myslave, mycmd, mystop)
   res= master_I2C.read( myslave, nwords)
   print "\tPost RegDir: ", res

   
#DAC CONFIGURATION BEGIN
zeDAC1=AD5665R(master_I2C, 0x13)
zeDAC1.setIntRef(intRef= False, verbose= True)
zeDAC1.writeDAC(0x0, 7, verbose= True)#7626

zeDAC2=AD5665R(master_I2C, 0x1F)
zeDAC2.setIntRef(intRef= False, verbose= True)
zeDAC2.writeDAC(0x2fff, 3, verbose= True)
#DAC CONFIGURATION END

#EEPROM BEGIN
zeEEPROM= E24AA025E48T(master_I2C, 0x50)
res=zeEEPROM.readEEPROM(0xfa, 6)
result="  EEPROM ID:\n\t"
for iaddr in res:
    result+="%02x "%(iaddr)
print result
#EEPROM END
