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
|
#
# Copyright 2007 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
from gnuradio import usrp1
import time,math
from usrpm import usrp_dbid
import db_base
import db_instantiator
from usrpm.usrp_fpga_regs import *
#debug_using_gui = True # Must be set to True or False
debug_using_gui = False # Must be set to True or False
#if debug_using_gui:
# import flexrf_debug_gui
# d'board i/o pin defs
# TX IO Pins
TX_POWER = (1 << 0) # TX Side Power
RX_TXN = (1 << 1) # T/R antenna switch for TX/RX port
# RX IO Pins
RX2_RX1N = (1 << 0) # antenna switch between RX2 and TX/RX port
RXENABLE = (1 << 1) # enables mixer
PLL_LOCK_DETECT = (1 << 2) # Muxout pin from PLL -- MUST BE INPUT
MReset = (1 << 3) # NB6L239 Master Reset, asserted low
SELA0 = (1 << 4) # NB6L239 SelA0
SELA1 = (1 << 5) # NB6L239 SelA1
SELB0 = (1 << 6) # NB6L239 SelB0
SELB1 = (1 << 7) # NB6L239 SelB1
PLL_ENABLE = (1 << 8) # CE Pin on PLL
AUX_SCLK = (1 << 9) # ALT SPI SCLK
AUX_SDO = (1 << 10) # ALT SPI SDO
AUX_SEN = (1 << 11) # ALT SPI SEN
SPI_ENABLE_TX_A = usrp1.SPI_ENABLE_TX_A
SPI_ENABLE_TX_B = usrp1.SPI_ENABLE_TX_B
SPI_ENABLE_RX_A = usrp1.SPI_ENABLE_RX_A
SPI_ENABLE_RX_B = usrp1.SPI_ENABLE_RX_B
"""
A few comments about the WBX boards:
They are half-duplex. I.e., transmit and receive are mutually exclusive.
There is a single LO for both the Tx and Rx sides.
The the shared control signals are hung off of the Rx side.
The shared io controls are duplexed onto the Rx side pins.
The wbx_high d'board always needs to be in 'auto_tr_mode'
"""
class wbx_base(db_base.db_base):
"""
Abstract base class for all wbx boards.
Derive board specific subclasses from db_wbx_base_{tx,rx}
"""
def __init__(self, usrp, which):
"""
@param usrp: instance of usrp.source_c
@param which: which side: 0 or 1 corresponding to side A or B respectively
@type which: int
"""
# sets _u _which _tx and _slot
db_base.db_base.__init__(self, usrp, which)
self.first = True
self.spi_format = usrp1.SPI_FMT_MSB | usrp1.SPI_FMT_HDR_0
# FIXME -- the write reg functions don't work with 0xffff for masks
self._rx_write_oe(int(PLL_ENABLE|MReset|SELA0|SELA1|SELB0|SELB1|RX2_RX1N|RXENABLE), 0x7fff)
self._rx_write_io((PLL_ENABLE|MReset|0|RXENABLE), (PLL_ENABLE|MReset|RX2_RX1N|RXENABLE))
self._tx_write_oe((TX_POWER|RX_TXN), 0x7fff)
self._tx_write_io((0|RX_TXN), (TX_POWER|RX_TXN)) # TX off, TR switch set to RX
self.spi_enable = (SPI_ENABLE_RX_A, SPI_ENABLE_RX_B)[which]
self.set_auto_tr(False)
#if debug_using_gui:
# title = "FlexRF Debug Rx"
# if self._tx:
# title = "FlexRF Debug Tx"
# self.gui = flexrf_debug_gui.flexrf_debug_gui(self, title)
# self.gui.Show(True)
def __del__(self):
#self._u.write_io(self._which, self.power_off, POWER_UP) # turn off power to board
#self._u._write_oe(self._which, 0, 0xffff) # turn off all outputs
self.set_auto_tr(False)
def _lock_detect(self):
"""
@returns: the value of the VCO/PLL lock detect bit.
@rtype: 0 or 1
"""
if self._rx_read_io() & PLL_LOCK_DETECT:
return True
else: # Give it a second chance
if self._rx_read_io() & PLL_LOCK_DETECT:
return True
else:
return False
# Both sides need access to the Rx pins.
# Write them directly, bypassing the convenience routines.
# (Sort of breaks modularity, but will work...)
def _tx_write_oe(self, value, mask):
return self._u._write_fpga_reg((FR_OE_0, FR_OE_2)[self._which],
((mask & 0xffff) << 16) | (value & 0xffff))
def _rx_write_oe(self, value, mask):
return self._u._write_fpga_reg((FR_OE_1, FR_OE_3)[self._which],
((mask & 0xffff) << 16) | (value & 0xffff))
def _tx_write_io(self, value, mask):
return self._u._write_fpga_reg((FR_IO_0, FR_IO_2)[self._which],
((mask & 0xffff) << 16) | (value & 0xffff))
def _rx_write_io(self, value, mask):
return self._u._write_fpga_reg((FR_IO_1, FR_IO_3)[self._which],
((mask & 0xffff) << 16) | (value & 0xffff))
def _rx_read_io(self):
t = self._u._read_fpga_reg((FR_RB_IO_RX_A_IO_TX_A, FR_RB_IO_RX_B_IO_TX_B)[self._which])
return (t >> 16) & 0xffff
def _tx_read_io(self):
t = self._u._read_fpga_reg((FR_RB_IO_RX_A_IO_TX_A, FR_RB_IO_RX_B_IO_TX_B)[self._which])
return t & 0xffff
def _compute_regs(self, freq):
"""
Determine values of registers, along with actual freq.
@param freq: target frequency in Hz
@type freq: float
@returns: (R, N, func, init, actual_freq)
@rtype: tuple(int, int, int, int, float)
Override this in derived classes.
"""
raise NotImplementedError
def _refclk_freq(self):
return float(self._u.fpga_master_clock_freq())/self._refclk_divisor()
def _refclk_divisor(self):
"""
Return value to stick in REFCLK_DIVISOR register
"""
return 1
# ----------------------------------------------------------------
def set_freq(self, freq):
"""
@returns (ok, actual_baseband_freq) where:
ok is True or False and indicates success or failure,
actual_baseband_freq is the RF frequency that corresponds to DC in the IF.
"""
raise NotImplementedError
def gain_range(self):
"""
Return range of gain that can be set by this d'board.
@returns (min_gain, max_gain, step_size)
Where gains are expressed in decibels (your mileage may vary)
"""
raise NotImplementedError
def set_gain(self, gain):
"""
Set the gain.
@param gain: gain in decibels
@returns True/False
"""
raise NotImplementedError
def _set_pga(self, pga_gain):
if(self._which == 0):
self._u.set_pga (0, pga_gain)
self._u.set_pga (1, pga_gain)
else:
self._u.set_pga (2, pga_gain)
self._u.set_pga (3, pga_gain)
def is_quadrature(self):
"""
Return True if this board requires both I & Q analog channels.
This bit of info is useful when setting up the USRP Rx mux register.
"""
return True
# ----------------------------------------------------------------
class wbx_base_tx(wbx_base):
def __init__(self, usrp, which):
"""
@param usrp: instance of usrp.sink_c
@param which: 0 or 1 corresponding to side TX_A or TX_B respectively.
"""
wbx_base.__init__(self, usrp, which)
# power up the transmit side, NO -- but set antenna to receive
self._u.write_io(self._which, (TX_POWER), (TX_POWER|RX_TXN))
self._lo_offset = 0e6
# Gain is not set by the PGA, but the PGA must be set at max gain in the TX
return self._set_pga(self._u.pga_max())
def __del__(self):
# Power down and leave the T/R switch in the R position
self._u.write_io(self._which, (RX_TXN), (TX_POWER|RX_TXN))
wbx_base.__del__(self)
def set_auto_tr(self, on):
if on:
self.set_atr_mask (RX_TXN)
self.set_atr_txval(0)
self.set_atr_rxval(RX_TXN)
else:
self.set_atr_mask (0)
self.set_atr_txval(0)
self.set_atr_rxval(0)
def set_enable(self, on):
"""
Enable transmitter if on is True
"""
if on:
v = 0
else:
v = RX_TXN
self._u.write_io(self._which, v, RX_TXN)
def set_lo_offset(self, offset):
"""
Set amount by which LO is offset from requested tuning frequency.
@param offset: offset in Hz
"""
self._lo_offset = offset
def lo_offset(self):
"""
Get amount by which LO is offset from requested tuning frequency.
@returns Offset in Hz
"""
return self._lo_offset
class wbx_base_rx(wbx_base):
def __init__(self, usrp, which):
"""
@param usrp: instance of usrp.source_c
@param which: 0 or 1 corresponding to side RX_A or RX_B respectively.
"""
wbx_base.__init__(self, usrp, which)
# set up for RX on TX/RX port
self.select_rx_antenna('TX/RX')
self.bypass_adc_buffers(True)
self._lo_offset = -4e6
def __del__(self):
# Power down
self._u.write_io(self._which, 0, (RXENABLE))
wbx_base.__del__(self)
def set_auto_tr(self, on):
if on:
self.set_atr_mask (ENABLE)
self.set_atr_txval( 0)
self.set_atr_rxval(ENABLE)
else:
self.set_atr_mask (0)
self.set_atr_txval(0)
self.set_atr_rxval(0)
def select_rx_antenna(self, which_antenna):
"""
Specify which antenna port to use for reception.
@param which_antenna: either 'TX/RX' or 'RX2'
"""
if which_antenna in (0, 'TX/RX'):
self._u.write_io(self._which, 0, RX2_RX1N)
elif which_antenna in (1, 'RX2'):
self._u.write_io(self._which, RX2_RX1N, RX2_RX1N)
else:
raise ValueError, "which_antenna must be either 'TX/RX' or 'RX2'"
def set_gain(self, gain):
"""
Set the gain.
@param gain: gain in decibels
@returns True/False
"""
maxgain = self.gain_range()[1] - self._u.pga_max()
mingain = self.gain_range()[0]
if gain > maxgain:
pga_gain = gain-maxgain
assert pga_gain <= self._u.pga_max()
agc_gain = maxgain
else:
pga_gain = 0
agc_gain = gain
V_maxgain = .2
V_mingain = 1.2
V_fullscale = 3.3
dac_value = (agc_gain*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale
assert dac_value>=0 and dac_value<4096
return self._u.write_aux_dac(self._which, 0, int(dac_value)) and \
self._set_pga(int(pga_gain))
def set_lo_offset(self, offset):
"""
Set amount by which LO is offset from requested tuning frequency.
@param offset: offset in Hz
"""
self._lo_offset = offset
def lo_offset(self):
"""
Get amount by which LO is offset from requested tuning frequency.
@returns Offset in Hz
"""
return self._lo_offset
def i_and_q_swapped(self):
"""
Return True if this is a quadrature device and ADC 0 is Q.
"""
return True
# ----------------------------------------------------------------
class _ADF410X_common(object):
def __init__(self):
# R-Register Common Values
self.R_RSV = 0 # bits 23,22,21
self.LDP = 1 # bit 20 Lock detect in 5 cycles
self.TEST = 0 # bit 19,18 Normal
self.ABP = 0 # bit 17,16 2.9ns
# N-Register Common Values
self.N_RSV = 0 # 23,22
self.CP_GAIN = 0 # 21
# Function Register Common Values
self.P = 0 # bits 23,22 0 = 8/9, 1 = 16/17, 2 = 32/33, 3 = 64/65
self.PD2 = 0 # bit 21 Normal operation
self.CP2 = 7 # bits 20,19,18 CP Gain = 5mA
self.CP1 = 7 # bits 17,16,15 CP Gain = 5mA
self.TC = 0 # bits 14-11 PFD Timeout
self.FL = 0 # bit 10,9 Fastlock Disabled
self.CP3S = 0 # bit 8 CP Enabled
self.PDP = 0 # bit 7 Phase detector polarity, Positive=1
self.MUXOUT = 1 # bits 6:4 Digital Lock Detect
self.PD1 = 0 # bit 3 Normal operation
self.CR = 0 # bit 2 Normal operation
def _compute_regs(self, freq):
"""
Determine values of R, control, and N registers, along with actual freq.
@param freq: target frequency in Hz
@type freq: float
@returns: (R, N, control, actual_freq)
@rtype: tuple(int, int, int, float)
"""
# Band-specific N-Register Values
phdet_freq = self._refclk_freq()/self.R_DIV
print "phdet_freq = %f" % (phdet_freq,)
desired_n = round(freq*self.freq_mult/phdet_freq)
print "desired_n %f" % (desired_n,)
actual_freq = desired_n * phdet_freq
print "actual freq %f" % (actual_freq,)
B = math.floor(desired_n/self._prescaler())
A = desired_n - self._prescaler()*B
print "A %d B %d" % (A,B)
self.B_DIV = int(B) # bits 20:8
self.A_DIV = int(A) # bit 6:2
#assert self.B_DIV >= self.A_DIV
if self.B_DIV < self.A_DIV:
return (0,0,0,0)
R = (self.R_RSV<<21) | (self.LDP<<20) | (self.TEST<<18) | \
(self.ABP<<16) | (self.R_DIV<<2)
N = (self.N_RSV<<22) | (self.CP_GAIN<<21) | (self.B_DIV<<8) | (self.A_DIV<<2)
control = (self.P<<22) | (self.PD2<<21) | (self.CP2<<18) | (self.CP1<<15) | \
(self.TC<<11) | (self.FL<<9) | (self.CP3S<<8) | (self.PDP<<7) | \
(self.MUXOUT<<4) | (self.PD1<<3) | (self.CR<<2)
return (R,N,control,actual_freq/self.freq_mult)
def _write_all(self, R, N, control):
"""
Write all PLL registers:
R counter latch,
N counter latch,
Function latch,
Initialization latch
Adds 10ms delay between writing control and N if this is first call.
This is the required power-up sequence.
@param R: 24-bit R counter latch
@type R: int
@param N: 24-bit N counter latch
@type N: int
@param control: 24-bit control latch
@type control: int
"""
self._write_R(R)
self._write_func(control)
self._write_init(control)
if self.first:
time.sleep(0.010)
self.first = False
self._write_N(N)
def _write_R(self, R):
self._write_it((R & ~0x3) | 0)
def _write_N(self, N):
self._write_it((N & ~0x3) | 1)
def _write_func(self, func):
self._write_it((func & ~0x3) | 2)
def _write_init(self, init):
self._write_it((init & ~0x3) | 3)
def _write_it(self, v):
s = ''.join((chr((v >> 16) & 0xff),
chr((v >> 8) & 0xff),
chr(v & 0xff)))
self._u._write_spi(0, self.spi_enable, self.spi_format, s)
def _prescaler(self):
if self.P == 0:
return 8
elif self.P == 1:
return 16
elif self.P == 2:
return 32
elif self.P == 3:
return 64
else:
raise ValueError, "Prescaler out of range"
#----------------------------------------------------------------------
class _lo_common(_ADF410X_common):
def __init__(self):
_ADF410X_common.__init__(self)
# Band-specific R-Register Values
self.R_DIV = 4 # bits 15:2
# Band-specific C-Register values
self.P = 0 # bits 23,22 0 = Div by 8/9
self.CP2 = 7 # bits 19:17
self.CP1 = 7 # bits 16:14
# Band specifc N-Register Values
self.DIVSEL = 0 # bit 23
self.DIV2 = 0 # bit 22
self.CPGAIN = 0 # bit 21
self.freq_mult = 1
self.div = 1
self.aux_div = 2
def freq_range(self): # FIXME
return (50e6, 1000e6, 16e6)
def set_divider(self, main_or_aux, divisor):
if main_or_aux not in (0, 'main', 1, 'aux'):
raise ValueError, "main_or_aux must be 'main' or 'aux'"
if main_or_aux in (0, 'main'):
if divisor not in (1,2,4,8):
raise ValueError, "Main Divider Must be 1, 2, 4, or 8"
for (div,val) in ((1,0),(2,1),(4,2),(8,3)):
if(div == divisor):
self.main_div = val
else:
if divisor not in (2,4,8,16):
raise ValueError, "Aux Divider Must be 2, 4, 8 or 16"
for (div,val) in ((2,0),(4,1),(8,2),(16,3)):
if(div == divisor):
self.aux_div = val
vala = self.main_div*SELA0
valb = self.aux_div*SELB0
mask = SELA0|SELA1|SELB0|SELB1
self._rx_write_io(((self.main_div*SELA0) | (self.aux_div*SELB0)),
(SELA0|SELA1|SELB0|SELB1))
def set_freq(self, freq):
#freq += self._lo_offset
if(freq < 20e6 or freq > 1200e6):
raise ValueError, "Requested frequency out of range"
div = 1
lo_freq = freq * 2
while lo_freq < 1e9 and div < 8:
div = div * 2
lo_freq = lo_freq * 2
print "For RF freq of %f, we set DIV=%d and LO Freq=%f" % (freq, div, lo_freq)
self.set_divider('main', div)
self.set_divider('aux', div*2)
R, N, control, actual_freq = self._compute_regs(lo_freq)
print "R %d N %d control %d actual freq %f" % (R,N,control,actual_freq)
if R==0:
return(False,0)
self._write_all(R, N, control)
return (self._lock_detect(), actual_freq/div/2)
#------------------------------------------------------------
class db_wbx_lo_tx(_lo_common, wbx_base_tx):
def __init__(self, usrp, which):
wbx_base_tx.__init__(self, usrp, which)
_lo_common.__init__(self)
def gain_range(self):
"""
Return range of gain that can be set by this d'board.
@returns (min_gain, max_gain, step_size)
Where gains are expressed in decibels (your mileage may vary)
Gain is controlled by a VGA in the output amplifier, not the PGA
"""
return (-56, 0, 0.1)
def set_gain(self, gain):
"""
Set the gain.
@param gain: gain in decibels
@returns True/False
"""
maxgain = self.gain_range()[1]
mingain = self.gain_range()[0]
if gain > maxgain:
txvga_gain = maxgain
elif gain < mingain:
txvga_gain = mingain
else:
txvga_gain = gain
V_maxgain = 1.4
V_mingain = 0.1
V_fullscale = 3.3
dac_value = ((txvga_gain-mingain)*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale
assert dac_value>=0 and dac_value<4096
print "DAC value %d" % (dac_value,)
return self._u.write_aux_dac(self._which, 1, int(dac_value))
class db_wbx_lo_rx(_lo_common, wbx_base_rx):
def __init__(self, usrp, which):
wbx_base_rx.__init__(self, usrp, which)
_lo_common.__init__(self)
def gain_range(self):
"""
Return range of gain that can be set by this d'board.
@returns (min_gain, max_gain, step_size)
Where gains are expressed in decibels (your mileage may vary)
"""
return (self._u.pga_min(), self._u.pga_max() + 45, 0.05)
#------------------------------------------------------------
# hook these daughterboard classes into the auto-instantiation framework
db_instantiator.add(usrp_dbid.WBX_LO_TX, lambda usrp, which : (db_wbx_lo_tx(usrp, which),))
db_instantiator.add(usrp_dbid.WBX_LO_RX, lambda usrp, which : (db_wbx_lo_rx(usrp, which),))
|