#
# Script to exercise AIDA mini-TLU
#
# David Cussans, December 2012
# 
# Hacked to only test internal triggers

from PyChipsUser import *
from FmcTluI2c import *

import sys
import time


def mean(TS):
    val=0
    for i in range(1,len(TS)):
        val+=TS[i]-TS[i-1]
    return val/(len(TS)-1)



bAddrTab = AddressTable("./aida_mini_tlu_addr_map.txt")

# Assume DIP-switch controlled address. Switches at 1 
board = ChipsBusUdp(bAddrTab,"192.168.200.32",50001)

# Check the bus for I2C devices
boardi2c = FmcTluI2c(board)

firmwareID=board.read("FirmwareId")

print "Firmware = " , hex(firmwareID)

print "Scanning I2C bus:"
scanResults = boardi2c.i2c_scan()
print scanResults

boardId = boardi2c.get_serial_number()
print "FMC-TLU serial number = " , boardId

resetClocks = 0
resetSerdes = 0 

# set DACs to -200mV
print "Setting all threshold DAC to -200mV "
boardi2c.set_threshold_voltage(7, -0.05)

clockStatus = board.read("LogicClocksCSR")
print "Clock status = " , hex(clockStatus)

if resetClocks:
    print "Resetting clocks"
    board.write("LogicRst", 1 )

    clockStatus = board.read("LogicClocksCSR")
    print "Clock status after reset = " , hex(clockStatus)

inputStatus = board.read("SerdesRstR")
print "Input status = " , hex(inputStatus)

if resetSerdes:
    board.write("SerdesRstW", 0x00000003 )
    inputStatus = board.read("SerdesRstR")
    print "Input status during reset = " , hex(inputStatus)

    board.write("SerdesRstW", 0x00000000 )
    inputStatus = board.read("SerdesRstR")
    print "Input status after reset = " , hex(inputStatus)

    board.write("SerdesRstW", 0x00000004 )
    inputStatus = board.read("SerdesRstR")
    print "Input status during calibration = " , hex(inputStatus)

    board.write("SerdesRstW", 0x00000000 )
    inputStatus = board.read("SerdesRstR")
    print "Input status after calibration = " , hex(inputStatus)



inputStatus = board.read("SerdesRstR")
print "Input status = " , hex(inputStatus)

count0 = board.read("ThrCount0R")
print " Count 0 = " , count0

count1 = board.read("ThrCount1R")
print " Count 1 = " , count1

count2 = board.read("ThrCount2R")
print " Count 2 = " , count2

count3 = board.read("ThrCount3R")
print " Count 3 = " , count3


board.write("InternalTriggerIntervalW",0)

print "Enabling DUT 1"
board.write("DUTMaskW",7)
board.write("TriggerMaskW",0x0)
board.write("TriggerVetoW",0)
print "Trigger inputs enabled: ", board.read("TriggerMaskR")

print "Reseting FIFO"
board.write("EventFifoCSR",0x2)

print "Disabling data recording"
board.write("Enable_Record_Data",0)

print "Enabling handshake: No-handshake"
board.write("HandshakeTypeW",1)

TriggerInterval = 16000000
print "Setting internal trigger interval to " , TriggerInterval
board.write("InternalTriggerIntervalW",TriggerInterval)  #0->Internal pulse generator disabled. Any other value will generate pulses with a frequency of n*6.25ns
trigInterval = board.read("InternalTriggerIntervalW")
print "Trigger interval read back as ", trigInterval


numLoops = 500000

for iLoop in range(0,numLoops):
    preVetotriggerCount = board.read("PreVetoTriggersR")
    postVetotriggerCount = board.read("PostVetoTriggersR")
    print "pre , post  veto triggers = " , preVetotriggerCount , postVetotriggerCount
    time.sleep( 0.5)
    board.write("DUTMaskW",0xFFFFFFF7)
    time.sleep( 0.5)
    board.write("DUTMaskW",0x00000007)
