Commit 6a247f09 authored by Jan Pospisil's avatar Jan Pospisil

added debug trace for SV testbench; added WaitForNotBusy function

parent 75016619
#!/usr/bin/env python
import sys
import time
sys.path.append('/usr/local/encore')
import encoreio
......@@ -19,7 +20,17 @@ class ConColors:
m = encoreio.Module.get_instance('fmc_fpg', 0)
WB_DEBUG = False
WB_DEBUG = True
WB_DEBUG_2_SV = True
WB_DEBUG_2_SV_FILE = 'wb_trace.svh'
###################################################################
## debug preparation
###################################################################
if WB_DEBUG_2_SV:
WB_DEBUG_2_SV_FP = open(WB_DEBUG_2_SV_FILE, 'w')
WB_DEBUG_2_SV_FP.write('uint32_t dataOut; // how to not-to-use output port from task?\n\n')
###################################################################
## WishBone access overlay
......@@ -29,11 +40,18 @@ def WbRead(register):
result = m.read(register)[0]
if WB_DEBUG:
print(ConColors.MAGENTA+'WB_READ:'+ConColors.NONE+' ['+register+'] = '+hex(result))
if WB_DEBUG_2_SV:
address = int(m[register]['offset'], 16)
WB_DEBUG_2_SV_FP.write('WbRead('+str(address)+', dataOut, "WB_READ: '+register+':");\n')
WB_DEBUG_2_SV_FP.write('assert (dataOut == '+str(result)+') else $error("WB_READ different, result should be '+hex(int(result))+'!");\n')
return result
def WbWrite(register, data):
if WB_DEBUG:
print(ConColors.CYAN+'WB_WRITE:'+ConColors.NONE+' ['+register+'] <- '+hex(data))
if WB_DEBUG_2_SV:
address = int(m[register]['offset'], 16)
WB_DEBUG_2_SV_FP.write('WbWrite('+str(address)+', '+str(data)+', "WB_WRITE: '+register+':");\n')
m.write(register, (data,))
def WbSetBits(register, mask, bits):
......@@ -64,7 +82,25 @@ def WbSetBitsMulti(register, mask, bits, start = 0, to = -1):
values[i] &= (~mask[i])
values[i] |= (bits[i] & mask[i])
WbWriteMulti(register, values, start, to)
# timeOut in [s] (floating point)
# polarity = 1: (register.bit == 1) <=> BUSY, wait for 0
# polarity = 0: (register.bit == 0) <=> BUSY, wait for 1
def WaitForNotBusy(register, bit, timeOut, tries, message = '', polarity = 1):
busy = True
for i in range(tries):
if (WbRead(register) & (1<<bit)) != polarity:
busy = False
break
# wait for timeOut seconds
timeToWait = timeOut
while timeToWait > 0:
timeBegin = time.time()
time.sleep(timeToWait)
timeToWait -= time.time() - timeBegin
if busy:
raise Exception('WaitForNotBusy error: '+message)
###################################################################
## SPI functions
###################################################################
......@@ -75,12 +111,14 @@ def SpiInit():
WbWrite('spi_ctrl', 0x2618)
def SpiRead(register):
WaitForNotBusy('spi_ctrl', 8, 0.001, 2, 'SPI read')
inst = 0x800000 | ((register & 0xff) << 8)
WbWrite('spi_tx_rx_0', inst)
WbSetBits('spi_ctrl', 0x100, 0x100)
return WbRead('spi_tx_rx_0')
def SpiWrite(register, data):
WaitForNotBusy('spi_ctrl', 8, 0.001, 2, 'SPI write')
inst = 0x000000 | ((register & 0xff) << 8) | (data & 0xff)
WbWrite('spi_tx_rx_0', inst)
WbSetBits('spi_ctrl', 0x100, 0x100)
......@@ -94,7 +132,7 @@ def Ad9512Init():
SpiWrite(0x3f, 0x03)
# fine delay
SpiWrite(0x34, 0x0) # use fine delay
delay = 2 # 0-31
delay = 1 # 0-31
SpiWrite(0x36, (delay&0x1f)<<1) # set fine delay
# select CLK1 input, power down CLK2 input
SpiWrite(0x45, 0x05)
......@@ -264,7 +302,7 @@ def Init():
WbWrite('control', 0)
SpiInit()
Ad9512Init()
SetTriggerThreshold(0.5)
SetTriggerThreshold(1)
SelectClock(0)
SetVcxoFrequency(-0.2105) # 125.0000 MHz
ClearMemory(0, '*')
......@@ -275,17 +313,25 @@ def TestPulse(channel = 1):
WbWrite('trig_latency', 8)
start = 5
length = 1
length = 20
stop = start + length
CreatePulse(channel, start + 2*channel, stop + 2*channel)
# CreatePulse(channel, 2, 2, 1)
start += 1
CreatePulse(channel, start + 2*channel, stop + 2*channel)
start += 1
CreatePulse(channel, start + 2*channel, stop + 2*channel)
start += 1
CreatePulse(channel, start + 2*channel, stop + 2*channel)
start += 1
CreatePulse(channel, start + 2*channel, stop + 2*channel)
# CreatePulse(channel, 0, 0, 1)
# CreatePulse(channel, 5, 10)
# CreatePulse(channel, 25, 30)
# CreatePulse(channel, 125, 130)
WbWrite('ch'+str(channel)+'_delay_set', 1)
WbWrite('ch'+str(channel)+'_delay_res', 100)
WbWrite('ch'+str(channel)+'_delay_set', 100)
WbWrite('ch'+str(channel)+'_delay_res', 500)
# start channel
channel -= 1
channel %= 2
......@@ -297,6 +343,6 @@ Init()
Control()
Status()
TestPulse(1)
TestPulse(2)
# TestPulse(2)
Control()
Status()
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