-
Notifications
You must be signed in to change notification settings - Fork 21
/
adc.py
1202 lines (991 loc) · 47.5 KB
/
adc.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import logging
import numpy as np
import fpgalib.fpga as fpga
# named functions
from twisted.internet.defer import inlineCallbacks, returnValue
from labrad.devices import DeviceWrapper
from labrad import types as T
import labrad.support
from fpgalib.util import littleEndian, TimedLock
import fpgalib.mondict as mondict
### Base classes ###
class ADC(fpga.FPGA):
"""
Base class for ADC builds
Some functions are implemented here. I chose to do this in a few functions
that I don't expect to change. Of course you can just override them in
subclasses as necessary.
"""
MAC_PREFIX = '00:01:CA:AA:01:'
REG_PACKET_LEN = 59
READBACK_LEN = 46
# Start modes
RUN_MODE_REGISTER_READBACK = 1
RUN_MODE_AVERAGE_AUTO = 2
RUN_MODE_AVERAGE_DAISY = 3
RUN_MODE_DEMOD_AUTO = 4
RUN_MODE_DEMOD_DAISY = 5
RUN_MODE_CALIBRATE = 7
@classmethod
def macFor(cls, boardNumber):
"""Get the MAC address of an ADC board as a string."""
return cls.MAC_PREFIX + ('0'+hex(int(boardNumber))[2:])[-2:].upper()
@classmethod
def isMac(mac):
"""Return True if this mac is for an ADC, otherwise False"""
return mac.startswith(cls.MAC_PREFIX)
# Life cycle methods
@inlineCallbacks
def connect(self, name, group, de, port, board, build):
"""Establish a connection to the board."""
print('connecting to ADC board: %s (build #%d)'\
% (self.macFor(board), build))
self.boardGroup = group
self.server = de
self.cxn = de._cxn
self.ctx = de.context()
self.port = port
self.board = board
self.build = build
self.MAC = self.macFor(board)
self.devName = name
self.serverName = de._labrad_name
self.timeout = T.Value(1, 's')
# Set up our context with the ethernet server.
# This context is expired when the device shuts down.
p = self.makePacket()
p.connect(port)
# ADC boards send packets with different lengths:
# - register readback: 46 bytes
# - demodulator output: 48 bytes
# - average readout: 1024 bytes
# so we do not require a specific length for received packets.
p.destination_mac(self.MAC)
p.require_source_mac(self.MAC)
#p.source_mac(self.boardGroup.sourceMac)
p.timeout(self.timeout)
p.listen()
yield p.send()
@inlineCallbacks
def shutdown(self):
"""Called when this device is to be shutdown."""
yield self.cxn.manager.expire_context(self.server.ID,
context=self.ctx)
# Register byte methods
@classmethod
def regPing(cls):
"""Returns a numpy array of register bytes to ping ADC register"""
regs = np.zeros(cls.REG_PACKET_LEN, dtype='<u1')
regs[0] = 1
return regs
@classmethod
def regPllQuery(cls):
"""Returns a numpy array of register bytes to query PLL status"""
regs = np.zeros(cls.REG_PACKET_LEN, dtype='<u1')
regs[0] = 1
return regs
@classmethod
def regSerial(cls, bits):
"""Returns a numpy array of register bytes to write to PLL"""
regs = np.zeros(cls.REG_PACKET_LEN, dtype='<u1')
regs[0] = 6
regs[3:6] = littleEndian(bits, 3)
return regs
@classmethod
def regAdcRecalibrate(cls):
"""Returns a numpy array of register bytes to recalibrate ADC chips"""
regs = np.zeros(cls.REG_PACKET_LEN, dtype='<u1')
regs[0] = 7
return regs
# Methods to get byte arrays to be written to the board
@classmethod
def pktWriteSram(cls, derp, data):
"""
Get a numpy array of bytes to write one derp of SRAM
data - ndarray: numeric data to be written. This must be formatted such
that .tostring will yield the proper byte string for the direct
ethernet packet.
"""
assert 0 <= derp < cls.SRAM_WRITE_DERPS, \
'SRAM derp out of range: %d' % derp
# Ensure data is a numpy array.
# This should not be needed, as it should have happened already
data = np.asarray(data)
pkt = np.zeros(cls.SRAM_WRITE_PKT_LEN + 2, dtype='<u1')
pkt[0:2] = littleEndian(derp, 2)
pkt[2:2+len(data)] = data
return pkt
# Utility
@staticmethod
def readback2BuildNumber(resp):
"""Get build number from register readback"""
a = np.fromstring(resp, dtype='<u1')
return a[0]
class AdcRunner(object):
pass
class ADC_Branch1(ADC):
"""Superclass for first branch of ADC boards"""
# Direct ethernet server packet update methods
@classmethod
def makeFilter(cls, data, p):
"""
Update a packet for the ethernet server with SRAM commands to upload
the filter function.
"""
for derp in range(cls.FILTER_DERPS):
start = cls.SRAM_WRITE_PKT_LEN * derp
end = start + cls.SRAM_WRITE_PKT_LEN
pkt = cls.pktWriteSram(derp, data[start:end])
p.write(pkt.tostring())
@classmethod
def makeTrigLookups(cls, demods, p):
"""
Update a packet for the ethernet server with SRAM commands to upload
Trig lookup tables.
"""
derp = 4 # First 4 derps used for filter function
channel = 0
while channel < cls.DEMOD_CHANNELS:
data = []
for ofs in [0, 1]:
ch = channel + ofs
for func in ['cosine', 'sine']:
if ch in demods:
d = demods[ch][func]
else:
d = np.zeros(cls.LOOKUP_TABLE_LEN, dtype='<u1')
data.append(d)
data = np.hstack(data)
pkt = cls.pktWriteSram(derp, data)
p.write(pkt.tostring())
channel += 2 # two channels per sram packet
derp += 1 # each sram packet writes one derp
# board communication (can be called from within test mode)
@inlineCallbacks
def _runSerial(self, data):
"""Run a command or list of commands through the serial interface."""
for d in data:
regs = self.regSerial(d)
yield self._sendRegisters(regs, readback=False)
# Externally available board communication methods
# These run in test mode.
def recalibrate(self):
@inlineCallbacks
def func():
regs = self.regAdcRecalibrate()
yield self._sendRegisters(regs, readback=False)
return self.testMode(func)
def initPLL(self):
@inlineCallbacks
def func():
yield self._runSerial([0x1FC093, 0x1FC092, 0x100004, 0x000C11])
return self.testMode(func)
def runCalibrate(self):
raise Exception("Depricated. Use recalibrate instead")
@inlineCallbacks
def func():
# build register packet
filterFunc=np.zeros(self.FILTER_LEN, dtype='<u1')
filterStretchLen=0
filterStretchAt=0
demods={}
regs = self.regRun(RUN_MODE_CALIBRATE, 1, filterFunc,
filterStretchLen, filterStretchAt, demods)
# create packet for the ethernet server
p = self.makePacket()
p.write(regs.tostring()) # send registry packet
yield p.send() #Send the packet to the direct ethernet server
returnValue(None)
return self.testMode(func)
# Utility
@staticmethod
def extractAverage(packets):
"""Extract Average waveform from a list of packets (byte strings)."""
data = ''.join(packets)
Is, Qs = np.fromstring(data, dtype='<i2').reshape(-1, 2).astype(int).T
return (Is, Qs)
### Specific DAC build classes ###
class AdcRunner_Build1(AdcRunner):
def __init__(self, dev, reps, runMode, startDelay, filter, channels):
self.dev = dev
self.reps = reps
self.runMode = runMode
self.startDelay = startDelay
self.filter = filter
self.channels = channels
if self.runMode == 'average':
self.mode = self.dev.RUN_MODE_AVERAGE_DAISY
self.nPackets = self.dev.AVERAGE_PACKETS
elif self.runMode == 'demodulate':
self.mode = self.dev.RUN_MODE_DEMOD_DAISY
self.nPackets = reps
else:
raise Exception("Unknown run mode '%s' for board '%s'" \
% (self.runMode, self.dev.devName))
# 16us acquisition time + 10us packet transmit.
# Not sure why the extra +1 is here
self.seqTime = fpga.TIMEOUT_FACTOR * (26E-6 * self.reps) + 1
def pageable(self):
"""ADC sequence alone will never disable paging"""
return True
def loadPacket(self, page, isMaster):
"""
Create pipelined load packet
For this build, do nothing.
"""
if isMaster:
raise Exception("Cannot use ADC board '%s' as master." \
% self.dev.devName)
return None
def setupPacket(self):
""" Create non-pipelined setup packet."""
return self.dev.setup(self.filter, self.channels)
def runPacket(self, page, slave, delay, sync):
"""Create run packet.
The unused arguments page, slave, and sync, in the call signature
are there so that we could use the same call for DACs and ADCs.
This is cheesey and ought to be fixed.
"""
filterFunc, filterStretchLen, filterStretchAt = self.filter
startDelay = self.startDelay + delay
regs = self.dev.regRun(self.mode, self.reps, filterFunc,
filterStretchLen, filterStretchAt,
self.channels, startDelay)
return regs
def collectPacket(self, seqTime, ctx):
"""
Collect appropriate number of ethernet packets for this sequence, then
trigger run context.
"""
return self.dev.collect(self.nPackets, seqTime, ctx)
def triggerPacket(self, ctx):
"""Send a trigger to the master context"""
return self.dev.trigger(ctx)
def readPacket(self, timingOrder):
"""
Read (or discard) appropriate number of ethernet packets, depending on
whether timing results are wanted.
"""
keep = any(s.startswith(self.dev.devName) for s in timingOrder)
return self.dev.read(self.nPackets) if keep else \
self.dev.discard(self.nPackets)
def extract(self, packets):
"""Extract data coming back from a readPacket."""
if self.runMode == 'average':
return self.dev.extractAverage(packets)
elif self.runMode == 'demodulate':
return self.dev.extractDemod(packets,
self.dev.DEMOD_CHANNELS_PER_PACKET)
class ADC_Build1(ADC_Branch1):
"""ADC build 1"""
RUNNER_CLASS = AdcRunner_Build1
# Build-specific constants
DEMOD_CHANNELS = 4
DEMOD_CHANNELS_PER_PACKET = 11
DEMOD_PACKET_LEN = 46
DEMOD_TIME_STEP = 2 #ns
AVERAGE_PACKETS = 32 #Number of packets per average mode execution
AVERAGE_PACKET_LEN = 1024 #bytes
TRIG_AMP = 255
LOOKUP_TABLE_LEN = 256
FILTER_LEN = 4096
FILTER_DERPS = 4
SRAM_WRITE_DERPS = 9
SRAM_WRITE_PKT_LEN = 1024 # Length of the data portion, not address bytes
LOOKUP_ACCUMULATOR_BITS = 16
def buildRunner(self, reps, info):
"""Get a runner for this board"""
runMode = info['runMode']
startDelay = info['startDelay']
filter = (info['filterFunc'], info['filterStretchLen'], info['filterStretchAt'])
channels = dict((i, info[i]) for i in range(self.DEMOD_CHANNELS) \
if i in info)
runner = self.RUNNER_CLASS(self, reps, runMode, startDelay, filter,
channels)
return runner
# Methods to get bytes to be written to register
@classmethod
def regRun(cls, mode, reps, filterFunc, filterStretchAt,
filterStretchLen, demods, startDelay=0):
"""Returns a numpy array of register bytes to run the board"""
regs = np.zeros(cls.REG_PACKET_LEN, dtype='<u1')
regs[0] = mode
regs[1:3] = littleEndian(startDelay, 2) #Daisychain delay
regs[7:9] = littleEndian(reps, 2) #Number of repetitions
if len(filterFunc)<=1:
raise Exception('Filter function must be at least 2')
#Filter function end address. -1 comes from 0 indexing.
regs[9:11] = littleEndian(len(filterFunc)-1, 2)
#Stretch address for filter
regs[11:13] = littleEndian(filterStretchAt, 2)
#Filter function stretch length
regs[13:15] = littleEndian(filterStretchLen, 2)
for i in range(cls.DEMOD_CHANNELS):
if i not in demods:
continue
addr = 15 + 4*i
#Lookup table step per sample
regs[addr:addr+2] = littleEndian(demods[i]['dPhi'], 2)
#Lookup table start address
regs[addr+2:addr+4] = littleEndian(demods[i]['phi0'], 2)
return regs
# Direct ethernet server packet creation methods
def setup(self, filter, demods):
filterFunc, filterStretchLen, filterStretchAt = filter
p = self.makePacket()
self.makeFilter(filterFunc, p)
self.makeTrigLookups(demods, p)
amps = ''
for ch in xrange(self.DEMOD_CHANNELS):
if ch in demods:
cosineAmp = demods[ch]['cosineAmp']
sineAmp = demods[ch]['sineAmp']
else:
cosineAmp = sineAmp = 0
amps += '%s,%s;' % (cosineAmp, sineAmp)
setupState = '%s: filter=%s, trigAmps=%s' % (self.devName, \
filterFunc.tostring(), amps)
return p, setupState
# Direct ethernet server packet update methods
# board communication (can be called from within test mode)
@inlineCallbacks
def _sendSRAM(self, filter, demods={}):
"""Write filter and sine table SRAM to the FPGA."""
#Raise exception to see what's calling this method
raise RuntimeError("_sendSRAM was called")
p = self.makePacket()
self.makeFilter(filter, p)
self.makeTrigLookups(demods, p)
yield p.send()
# Externally available board communication methods
# These run in test mode.
def runAverage(self, filterFunc, filterStretchLen, filterStretchAt,
demods):
@inlineCallbacks
def func():
# build registry packet
regs = self.regRun(self, self.RUN_MODE_AVERAGE_AUTO, 1, filterFunc,
filterStretchLen, filterStretchAt, demods)
p = self.makePacket()
p.write(regs.tostring())
p.timeout(T.Value(10, 's'))
p.read(self.AVERAGE_PACKETS)
ans = yield p.send()
# parse the packets out and return data
packets = [data for src, dst, eth, data in ans.read]
returnValue(self.extractAverage(packets))
return self.testMode(func)
def runDemod(self, filterFunc, filterStretchLen, filterStretchAt, demods):
@inlineCallbacks
def func():
# build register packet
regs = self.regRun(self.RUN_MODE_DEMOD_AUTO, 1, filterFunc,
filterStretchLen, filterStretchAt, demods)
# create packet for the ethernet server
p = self.makePacket()
self.makeFilter(filterFunc, p) # upload filter function
# upload trig lookup tables, cosine and sine for each demod
# channel
self.makeTrigLookups(demods, p)
p.write(regs.tostring()) # send registry packet
p.timeout(T.Value(10, 's')) # set a conservative timeout
p.read(1) # read back one demodulation packet
ans = yield p.send() #Send the packet to the direct ethernet
# server parse the packets out and return data. packets is a list
# of 48-byte strings
packets = [data for src, dst, eth, data in ans.read]
returnValue(self.extractDemod(packets,
self.DEMOD_CHANNELS_PER_PACKET))
return self.testMode(func)
# Utility
@staticmethod
def processReadback(resp):
"""Process byte string returned by ADC register readback
Returns a dict with following keys
build - int: the build number of the board firmware
noPllLatch - bool: True is unlatch, False is latch (I think)
executionCounter - int: Number of executions since last start
"""
raise RuntimeError("Check this function for correctness")
a = np.fromstring(resp, dtype='<u1')
raise RuntimeError("check parity of pll latch bits")
return {
'build': a[0],
'noPllLatch': a[1]&1 == 1,
}
@staticmethod
def extractDemod(packets, nDemod):
"""Extract Demodulation data from a list of packets (byte strings)."""
#stick all data strings in packets together, chopping out last 4 bytes
#from each string
data = ''.join(data[:44] for data in packets)
#Convert string of bytes into numpy array of 16bit integers. <i2 means
#little endian 2 byte
vals = np.fromstring(data, dtype='<i2')
#Is,Qs are numpy arrays with the following format
#[I0,I1,...,I_numChannels, I0,I1,...,I_numChannels]
# 1st data run 2nd data run
Is, Qs = vals.reshape(-1, 2).astype(int).T
#Parse the IQ data into the following format
#[(Is ch0, Qs ch0), (Is ch1, Qs ch1),...,(Is chnDemod, Qs chnDemod)]
data = (Is, Qs)
#data = [(Is[i::nDemod], Qs[i::nDemod]) for i in xrange(nDemod)]
#data_saved = data
# compute overall max and min for I and Q
def getRange(pkt):
Irng, Qrng = [ord(i) for i in pkt[46:48]]
twosComp = lambda i: int(i if i < 0x8 else i - 0x10)
Imax = twosComp((Irng >> 4) & 0xF) # << 12
Imin = twosComp((Irng >> 0) & 0xF) # << 12
Qmax = twosComp((Qrng >> 4) & 0xF) # << 12
Qmin = twosComp((Qrng >> 0) & 0xF) # << 12
return Imax, Imin, Qmax, Qmin
ranges = np.array([getRange(pkt) for pkt in packets]).T
Imax = int(max(ranges[0]))
Imin = int(min(ranges[1]))
Qmax = int(max(ranges[2]))
Qmin = int(min(ranges[3]))
return (data, (Imax, Imin, Qmax, Qmin))
fpga.REGISTRY[('ADC', 1)] = ADC_Build1
class AdcRunner_Build2(AdcRunner_Build1):
pass
class ADC_Build2(ADC_Build1):
"""
ADC build 2
This build adds ability to read back the parity of the PLL latch bits.
This should allow us to
"""
RUNNER_CLASS = AdcRunner_Build2
@staticmethod
def processReadback(resp):
"""Process byte string returned by ADC register readback
Returns a dict with following keys
build - int: the build number of the board firmware
noPllLatch - bool: True is unlatch, False is latch (I think)
executionCounter - int: Number of executions since last start
"""
#raise RuntimeError("Check this function for correctness")
a = np.fromstring(resp, dtype='<u1')
#raise RuntimeError("check parity of pll latch bits")
return {
'build': a[0],
'noPllLatch': a[1]&1 == 1,
'executionCounter': (a[3]<<8) + a[2]
}
fpga.REGISTRY[('ADC', 2)] = ADC_Build2
class AdcRunner_Build3(AdcRunner_Build2):
pass
class ADC_Build3(ADC_Build2):
RUNNER_CLASS = AdcRunner_Build3
fpga.REGISTRY[('ADC', 3)] = ADC_Build3
class AdcRunner_Build6(AdcRunner_Build2):
pass
class ADC_Build6(ADC_Build2):
"""
This is the same as ADC_Build2 but has more demodulator channels.
"""
RUNNER_CLASS = AdcRunner_Build6
# Build-specific constants
DEMOD_CHANNELS = 6
fpga.REGISTRY[('ADC', 6)] = ADC_Build6
class AdcRunner_Build7(AdcRunner_Build2):
def __init__(self, dev, reps, runMode, startDelay, channels, info):
self.dev = dev
self.reps = reps
self.runMode = runMode
self.startDelay = startDelay
self.channels = channels
self.info = info
if self.runMode == 'average':
self.mode = self.dev.RUN_MODE_AVERAGE_DAISY
self.nPackets = self.dev.AVERAGE_PACKETS
elif self.runMode == 'demodulate':
self.mode = self.dev.RUN_MODE_DEMOD_DAISY
iqPairsPerExpt = sum([rcount*rchan for rcount, rdelay, rlen, rchan in info['triggerTable']])
packetsPerExpt = int(np.ceil(iqPairsPerExpt/float(self.dev.DEMOD_CHANNELS_PER_PACKET)))
self.nPackets = reps * packetsPerExpt
# print "(ADC) number of packets: %d" % (self.nPackets,)
# print "(ADC) rcount: %s, rdelay: %s, rlen: %s, rchan: %s, iqPairsPerExpt: %s, packetsPerExpt: %s, reps: %s" % (rcount, rdelay, rlen, rchan, iqPairsPerExpt, packetsPerExpt, reps)
else:
raise Exception("Unknown run mode '%s' for board '%s'" \
% (self.runMode, self.dev.devName))
# 16us acquisition time + 10us packet transmit.
# Not sure why the extra +1 is here
statTime = 4e-9*sum([count*(delay+rlen) for count, delay, rlen, chan in info['triggerTable']])
self.seqTime = fpga.TIMEOUT_FACTOR * (statTime * self.reps) + 1
# print "sequence time: %f, timeout: %f" % (statTime, self.seqTime)
def loadPacket(self, page, isMaster):
"""Create pipelined load packet. For ADC this is the trigger table."""
# print "making ADC load packet"
if isMaster:
raise Exception("Cannot use ADC board '%s' as master." \
% self.dev.devName)
p = self.dev.load(self.info)
# print("adc load packet: %s" % (str(p._packet)))
return p
def setupPacket(self):
p = self.dev.setup(self.info)
# print("ADC setup packet: %s" % (str(p[0]._packet),))
return p
def runPacket(self, page, slave, delay, sync):
"""Create run packet.
The unused arguments page, slave, and sync, in the call signature
are there so that we could use the same call for DACs and ADCs.
This is cheesey and ought to be fixed.
"""
startDelay = self.startDelay + delay
regs = self.dev.regRun(self.mode, self.info, self.reps, startDelay=startDelay)
# print("ADC run packet: %s" % (regs,))
return regs
def extract(self, packets):
"""Extract data coming back from a readPacket."""
if self.runMode == 'average':
return self.dev.extractAverage(packets)
elif self.runMode == 'demodulate':
return self.dev.extractDemod(packets,
self.info['triggerTable'], self.info.get('mode', 'iq'))
class ADC_Branch2(ADC):
"""Superclass for second branch of ADC boards"""
# Direct ethernet server packet update methods
@classmethod
def makeTriggerTable(cls, triggerTable, p):
"""
Page 0 of SRAM has a table defining ADC trigger repetition counts and delays
SRAM Write:
The controller must write to SRAM in the AD board to define two tables, a retrigger table to define multiple AD triggers for an experiment start, and a multiplier table for demodulation of the incoming signal for each demodulation channel.
(1) The retrigger table defines multiple AD triggers per master start. The table starts at address 0, then increments up (to a maximum 127) after performing the functions of each table entry, until it reaches a table entry with rdelay[15..8]=0, at which time the retriggering stops. Note that an empty table with rdelay[15..9]=0 at the address 0 will not ever trigger the AD. In the table entry, rdelay[15..0]+ 3 is the number of 4 ns cycles between the AD start (or last end of ADon) and the AD trigger. After this delay, the AD is turned on for rlength[7..0]+1 cycles, during which ADon demultiplexer (don) is high. The value rcount[15..0]+1 is the number of AD triggers per table entry. An AD done signal pulses 3 cycles after the last ADon goes low. Note that there is approximately 7 clock cycle delay that needs to be measured and taken into account to get demodulation time correct. Channels 0 to rchan[3..0]-1 is read out; maximum rchan[3..0] is 11 for 12 channels. If rchan[3..0]=0, then save data in bit readout mode - see below.
Note that you have multiple triggers, as for the DAC board. But for each trigger, you can have multiple retriggering to account for multiple AD demodulations during each sequence.
(1) The retrigger table is stored in SRAM memory with adrstart[20..8] = 0. The Ethernet packet is given by
l(0) length[15..8] set to 4; ( length[15..0]= 1024+2=1026 )
l(1) length[7..0] set to 2
d(0) adrstart[15..8] SRAM page for start address: middle bits = 0,
d(1) adrstart[23..16] upper bits; size of SRAM implies bits [23..19] = 0
d(2) sram(+0)[7..0] rcount[7..0](0) +1 = number AD cycles
d(3) sram(+1)[7..0] rcount[15..8](0)
d(4) sram(+2)[7..0] rdelay[7..0](0) +3= clock delay before Multiplier on
d(5) sram(+3)[7..0] rdelay[15..8](0) (units of 4 ns)
d(6) sram(+4)[7..0] rlength(7..0](0) +1 = clock length of Multiplier on
d(7) sram(+5)[7..0] rchan[3..0](0) Number channels saved, 0=bit mode
d(8) sram(+6)[7..0] spare
d(9) sram(+7)[7..0] spare
d(10) sram(+8)[7..0] rcount[7..0](1)
...
d(1022)sram(+1022)[7..0] rcount[15..8](127)
...
d(1025)sram(+1022)[7..0] spare
"""
# takes trigger table and turns it into a packet for ADC
if len(triggerTable) > 128: # no memory for more than 128 entries
raise Exception("Trigger table max len = 128")
if triggerTable[0][1] < 1:
raise Exception("Trigger table row0 rdelay is 0")
rlens = [row[1]<50 for row in triggerTable]
# if there is a spacing of <50 clock cycles between demods then the FIFO gets backed up
if any(rlens):
count0 = triggerTable[0][0]
rlen0 = triggerTable[0][1]
if rlen0<50 and count0==1 and not any(rlens[1:]): # if its only the start delay, no exception
pass
else:
raise Exception("rlen < 50 clock cycles (200 ns) can cause FIFO backup for 12 chans")
data = np.zeros(cls.SRAM_RETRIGGER_PKT_LEN, dtype='<u1')
data[0] = 0 # SRAM page for start address: middle bits = 0,
data[1] = 0 # upper bits; size of SRAM implies bits [23..19] = 0
for idx,entry in enumerate(triggerTable):
currCount, currDelay, currLength, currChans = entry
# WARNING: currChans defined in a funny way
# if you want a trigger to measure a subset of channels,
# those channels must be the lowest channels.
# i.e. you can only read out:
# (0, 0-1, 0-2, ... 0-11)
# you cannot cherry pick which channels to read out
# as far as JK and TW understand it.
# For now, we will not make use of currChans(rchan),
# rather default it all channels always read out.
# See documentation above
currCount -= 1 # compensate for FPGA offsets
currDelay -= 4 # compensate for FPGA offsets
currLength -=1 # compensate for FPGA offsets
midx = idx * 8 + 2
data[midx:midx+2] = littleEndian(currCount, 2)
data[midx+2:midx+4] = littleEndian(currDelay, 2)
data[midx+4] = currLength
data[midx+5] = currChans
data[midx+6:midx+8] = littleEndian(0, 2) # spare
p.write(data.tostring())
@classmethod
def makeMixerTable(cls, demods, p):
"""
Page 1-12 of SRAM has the mixer tables for demodulators 0-11 respectively.
(2) For the multiplier lookup tables, adrstart is taken from the following table
adrstart[20..8]= channel n + 1. This offsets by 1 from the above trigger page.
The Ethernet packet for channel n is:
l(1) length[15..8] set to 4; ( length[15..0]= 1024+2=1026 )
l(2) length[7..0] set to 2
d(1) adrstart[15..8] SRAM page for start address: middle bits = n+1
d(2) adrstart[23..16] upper bits; size of SRAM implies bits [23..19] = 0
d(3) sram(+0)[7..0] multsin(0) Multiplier time 0
d(4) sram(+1)[7..0] multcos(0)
d(5) sram(+2)[7..0] multsin(1) Multiplier time 1 (1/2 clock, 2ns)
d(6) sram(+3)[7..0] multcos(1)
...
d(1025)sram(+1022)[7..0] multsin(511)
d(1026)sram(+1023)[7..0] multcos(511)
"""
for idx,demod in enumerate(demods):
data = np.zeros(cls.SRAM_MIXER_PKT_LEN, dtype='<i1')
# retrigger table is page 0, mixer tables are pages 1-13, factor of 4 from stripping least significant bits
data[0:2] = littleEndian((idx+1),2) # SRAM page address
mixerTable = demod['mixerTable']
for tidx, row in enumerate(mixerTable):
I, Q = row
data[(tidx*2)+2] = I
data[(tidx*2+1)+2] = Q
p.write(data.tostring())
# board communication (can be called from within test mode)
@inlineCallbacks
def _runSerial(self, data):
"""Run a command or list of commands through the serial interface."""
for d in data:
regs = self.regSerial(d)
yield self._sendRegisters(regs, readback=False)
# Externally available board communication methods
# These run in test mode.
def recalibrate(self):
@inlineCallbacks
def func():
regs = self.regAdcRecalibrate()
yield self._sendRegisters(regs, readback=False)
return self.testMode(func)
def initPLL(self):
@inlineCallbacks
def func():
yield self._runSerial([0x1FC093, 0x1FC092, 0x100004, 0x000C11])
return self.testMode(func)
# Utility
@staticmethod
def extractAverage(packets):
"""Extract Average waveform from a list of packets (byte strings)."""
data = ''.join(packets)
Is, Qs = np.fromstring(data, dtype='<i2').reshape(-1, 2).astype(int).T
return (Is, Qs)
class ADC_Build7(ADC_Branch2):
"""ADC build 7"""
RUNNER_CLASS = AdcRunner_Build7
# Build-specific constants
DEMOD_CHANNELS = 12
DEMOD_CHANNELS_PER_PACKET = 11
DEMOD_PACKET_LEN = 46
DEMOD_TIME_STEP = 2 #ns
REGISTER_READBACK_PKT_LEN = 46
AVERAGE_PACKETS = 16 #Number of packets per average mode execution
AVERAGE_PACKET_LEN = 1024 #bytes
SRAM_RETRIGGER_PKT_LEN = 1026 # Length of the data portion, not address bytes
SRAM_MIXER_PKT_LEN = 1026 # Length of the data portion, not address bytes
def buildRunner(self, reps, info):
"""Get a runner for this board"""
logging.info("building runner with setting keys: {}".format(info.keys()))
runMode = info['runMode']
startDelay = info['startDelay']
channels = dict((i, info[i]) for i in range(self.DEMOD_CHANNELS) \
if i in info)
runner = self.RUNNER_CLASS(self, reps, runMode, startDelay,
channels, info)
return runner
@classmethod
def regRun(cls, mode, info, reps, startDelay=0):
"""
Returns a numpy array of register bytes to run the board
Register Write:
These registers control the AD functions. This card is always set in slave mode for daisychain initiation of AD functions (see GHzDAC board).
l(0) length[15..8] set to 0 ; ( length[15..0] = 59 )
l(1) length[7..0] set to 59
d(0) start[7..0] Output & command function
0 = off
1 = register readback
2 = average mode, auto start (use n=1)
3 = average mode, daisychain start
4 = demodulator mode, auto start (use n=1)
5 = demodulator mode, daisychain start
6 = set PLL of 1GHz clock with ser[23..0], no readback
7 = recalibrate AD converters, no readback
d(1) startdelay[7..0] Start delay after daisychain signal, compensates for dchain delays
d(2) startdelay[15..8] Note longer delays than for GHzDAC
d(3) ser1[7..0] 8 lowest PLL bits of serial interface
d(4) ser2[7..0] 8 middle PLL bits
d(5) ser3[7..0] 8 highest PLL bits
d(6) spare
d(7) n[7..0] n = Number of averages in average mode
d(8) n[15..8] n = Number of total events in demodulator mode
d(9) bitflip[7..0] XOR mask for bit readout
d(10) mon0[7..0] SMA mon0 and mon1 programming, like DAC board
d(11) mon1[7..0]
...
d(58) spare
"""
regs = np.zeros(cls.REG_PACKET_LEN, dtype='<u1')
regs[0] = mode
regs[1:3] = littleEndian(startDelay, 2) #Daisychain delay
regs[7:9] = littleEndian(reps, 2) #Number of repetitions
regs[9] = littleEndian(0, 1)[0] #XOR bit flip mask
mon0 = info.get('mon0', 'start')
mon1 = info.get('mon1', 'don')
if isinstance(mon0,str):
mon0 = mondict.MONDICT[mon0]
if isinstance(mon1,str):
mon1 = mondict.MONDICT[mon1]
regs[10] = mon0
regs[11] = mon1
return regs
# Direct ethernet server packet creation methods
def setup(self, info):
"""
A setup packet is something that cannot pipeline, e.g. changing microwave source freq.
"""
triggerTable = info['triggerTable']
demods = [info[idx] for idx in range(self.DEMOD_CHANNELS) if idx in info]
# demods = [info[idx] for idx in range(self.DEMOD_CHANNELS) if idx in info]
p = self.makePacket("setup")
self.makeTriggerTable(triggerTable, p)
self.makeMixerTable(demods, p)
triggerTableState = " ".join(['triggerTableState%d=%s' % (idx, triggerTable[idx]) for idx in range(len(triggerTable)) ])
mixTableState = " ".join(['mixTable%d=%s' % (idx, demod['mixerTable']) for idx,demod in enumerate(demods) ])
setupState = " ".join([triggerTableState,mixTableState])
return p, setupState
# Direct ethernet server packet update methods
def load(self, info):
"""
A load packet is something that can pipeline in some way, e.g. paging on the DAC boards.
Create a direct ethernet packet to load sequence data.
Sequence data is the trigger table and mixer table.
"""
p = self.makePacket("load")
return p
# board communication (can be called from within test mode)
@inlineCallbacks
def _sendSRAM(self, filter, demods={}):
"""Write filter and sine table SRAM to the FPGA."""
#Raise exception to see what's calling this method
raise RuntimeError("_sendSRAM was called")
p = self.makePacket()
self.makeFilter(filter, p)
self.makeTrigLookups(demods, p)
yield p.send()
# Externally available board communication methods
# These run in test mode.
def registerReadback(self):
@inlineCallbacks
def func():
# build registry packet
regs = self.regRun(self.RUN_MODE_REGISTER_READBACK, 0) # 0 reps?
p = self.makePacket("registerReadback")
p.write(regs.tostring())
p.timeout(T.Value(10, 's'))
p.read(1)
ans = yield p.send()
# parse the packets out and return data
packets = [data for src, dst, eth, data in ans.read]
returnValue(self.processReadback(packets[0]))
return self.testMode(func)
def runAverage(self):
@inlineCallbacks
def func():
# build registry packet
regs = self.regRun(self.RUN_MODE_AVERAGE_AUTO, {}, 1)
p = self.makePacket("runAverage")
p.write(regs.tostring())
p.timeout(T.Value(10, 's'))
p.read(self.AVERAGE_PACKETS)
ans = yield p.send()
# parse the packets out and return data
packets = [data for src, dst, eth, data in ans.read]
# print "average mode packets:"
# for p in packets:
# print "len: %d, first 64 byes:" % (len(p),)
# print labrad.support.hexdump(p[0:64])
returnValue(self.extractAverage(packets))
return self.testMode(func)
def runDemod(self, info):
triggerTable = info['triggerTable']
# Default to full length filter with half full scale amplitude
demods = [info[idx] for idx in range(self.DEMOD_CHANNELS) if idx in info]
#demods = dict((i, info[i]) for i in \
# range(self.DEMOD_CHANNELS) if i in info)
mode = info['mode']
@inlineCallbacks