-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_buffer.py
More file actions
174 lines (146 loc) · 5.56 KB
/
read_buffer.py
File metadata and controls
174 lines (146 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/env python
import random # For randint
import sys # For sys.argv and sys.exit
import uhal
import time
import optparse
from descrambler import descramble
from binaryFunctions import get_binary_string
def allignWord(binstring, wordsize, pattern, persist = 10, secPattern = None, offsetSecPattern = 0):
for x in range(len(binstring)):
alligned = True
for index in range(persist):
if(binstring[x+index*wordsize:x+index*wordsize+len(pattern)] != pattern):
alligned = False
elif(offsetSecPattern > 0 and binstring[x+offsetSecPattern+index*wordsize:x+offsetSecPattern+index*wordsize+len(secPattern)] != secPattern ):
alligned = False
if(alligned):
return x
print("-"*50)
print("Didn't find an offset for the \"{}\" pattern that persisted for \"{}\" words".format(pattern, persist))
def checkLockWrite(binstring, wordsize, pattern):
lockLoss = False
words = []
lastGoodBitIdx = 0
for x in range(0, len(binstring), wordsize):
lastGoodBitIdx = x
word = binstring[x:x+wordsize]
words.append(hex(int(word, 2)))
#print int(word, 2), hex(int(word, 2)), word
if(binstring[x:x+len(pattern)] != pattern):
#print "lock loss: ", x
lockLoss = True
break
return lockLoss, words, lastGoodBitIdx
def readout(reset = 0, mode = 'tdc', scrambled = False):
uhal.disableLogging()
connectionFilePath = "/home/daq/etltest_0721/etltests_0721/etl-kcu105-ipbus/Real_connections.xml"
deviceId = "KCU105real"
# PART 2: Creating the HwInterface
connectionMgr = uhal.ConnectionManager("file://" + connectionFilePath);
hw = connectionMgr.getDevice(deviceId);
TOFHIR_status = hw.getNode("LINK1_status")
debug_RAM1 = hw.getNode("Tx1_debug_RAM")
debug_RAM1_start = hw.getNode("Tx1_debug_RAM_start")
debug_RAM0 = hw.getNode("Tx0_debug_RAM")
debug_RAM0_start = hw.getNode("Tx0_debug_RAM_start")
rst_Rx1_addr_cnt = hw.getNode("rst_Rx1_addr_cnt")
rst_Rx0_addr_cnt = hw.getNode("rst_Rx0_addr_cnt")
Nword = 8
Value = []
MEM0 = []
MEM1 = []
wait = 2
TxValue = reset
Value = 1
depth = 128*2
Nword = depth*8
debug_RAM1_start.write(int(Value));
hw.dispatch();
#time.sleep(wait)
#"----------- data in output lpGBT uplink for each e-port ------------"
MEM0_decode = []
MEM1_decode = []
MEM0=debug_RAM0.readBlock(int(Nword));
hw.dispatch();
MEM1=debug_RAM1.readBlock(int(Nword));
hw.dispatch();
#"--------------------- link 0 -------------------------"
FrameData = 0
xx = 0
yy = 0
for x in range(7):
MEM0_decode.append( 0 )
MEM1_decode.append( 0 )
for x in range(int(Nword)):
if yy == 7:
yy = 0
xx = xx + 1
FrameData = 0
else :
FrameData = ((MEM0[x]&0xFFFFFFFF)<<xx*32)
MEM0_decode[yy] = MEM0_decode[yy] + FrameData;
yy = yy + 1
#"--------------------- link 1 -------------------------"
FrameData = 0
xx = 0
yy = 0
for x in range(int(Nword)-1, -1 , -1):
if yy == 7:
yy = 0
xx = xx + 1
FrameData = 0
else :
FrameData = ((MEM1[x]&0xFFFFFFFF)<<xx*32)
MEM1_decode[yy] = MEM1_decode[yy] + FrameData;
yy = yy + 1
#########################################
# Determine header offset for the buffer
#########################################
frame = bin(MEM1_decode[6])
offset = None
if mode == 'counter':
offset = allignWord(frame,32,"10", 16, "10101010", 2)
elif mode == 'tdc':
offset = allignWord(frame,32,"10", 16)
elif mode == 'constant':
offset = 11
os = allignWord(frame,32,"10", 16)
#print "offset for the allignment: ", offset
lockedFrame = frame[offset:]
#print lockedFrame
#offset = allignWord(frame,32,"01", 16)
#print "offset for the false allignment: ", offset
lockloss, words, lastGoodBitIdx = checkLockWrite(lockedFrame,32,"10")
tryAgain = True
if tryAgain and lockloss and lastGoodBitIdx > 32*10:
lockloss, words, lastGoodBitIdx = checkLockWrite(lockedFrame[:lastGoodBitIdx],32,"10")
if scrambled:
words = descramble(words)
if offset > 32:
lockloss = True
return lockloss, words, offset, os
if __name__ == '__main__':
parser = optparse.OptionParser("usage: %prog [options]\n")
parser.add_option('--nEvents', dest='nEvents', type='int', default = 1000, help="Set number of events to save to tmp data file")
parser.add_option('--mode', dest='mode', type='string', default = 'tdc', help="Set offset finder type")
parser.add_option('--scrambled', dest='scrambled', default = 'True', help="Descramble the data or not")
options, args = parser.parse_args()
nEvents = options.nEvents
mode = options.mode
scrambled = False if options.scrambled == 'False' else True
idx = 0
data = []
while( len(data) < nEvents):
lockloss, words, offset, os = readout(1, mode, scrambled)
if not lockloss:
data += words[1:-1] #First and Last word is always half empty
if idx % 50 == 0:
print("nEvents = {}, used offset = {}, found offset = {}".format(len(data), offset, os))
idx += 1
data = data[:nEvents]
fileData = open("etroc_readout.dat","w")
for d in data:
#print(get_binary_string(int(d,16),32))
fileData.write("{}\n".format(d))
fileData.close()