📄 usrp.py
字号:
self._u = None # will fire usrp1.* destructor class source_c(usrp_common): def __init__(self, which=0, decim_rate=64, nchan=1, mux=0x32103210, mode=0, fusb_block_size=0, fusb_nblocks=0, fpga_filename="", firmware_filename=""): _ensure_rev2(which) self._u = usrp1.source_c(which, decim_rate, nchan, mux, mode, fusb_block_size, fusb_nblocks, fpga_filename, firmware_filename) # Add the db attribute, which contains a 2-tuple of tuples of daughterboard classes self.db = (db_instantiator.instantiate(self._u, 0), db_instantiator.instantiate(self._u, 1)) usrp_common.__init__(self) def __del__(self): self.db = None # will fire d'board destructors self._u = None # will fire usrp1.* destructorclass source_s(usrp_common): def __init__(self, which=0, decim_rate=64, nchan=1, mux=0x32103210, mode=0, fusb_block_size=0, fusb_nblocks=0, fpga_filename="", firmware_filename=""): _ensure_rev2(which) self._u = usrp1.source_s(which, decim_rate, nchan, mux, mode, fusb_block_size, fusb_nblocks, fpga_filename, firmware_filename) # Add the db attribute, which contains a 2-tuple of tuples of daughterboard classes self.db = (db_instantiator.instantiate(self._u, 0), db_instantiator.instantiate(self._u, 1)) usrp_common.__init__(self) def __del__(self): self.db = None # will fire d'board destructors self._u = None # will fire usrp1.* destructor # ------------------------------------------------------------------------# utilities# ------------------------------------------------------------------------def determine_rx_mux_value(u, subdev_spec): """ Determine appropriate Rx mux value as a function of the subdevice choosen and the characteristics of the respective daughterboard. @param u: instance of USRP source @param subdev_spec: return value from subdev option parser. @type subdev_spec: (side, subdev), where side is 0 or 1 and subdev is 0 or 1 @returns: the Rx mux value """ # Figure out which A/D's to connect to the DDC. # # Each daughterboard consists of 1 or 2 subdevices. (At this time, # all but the Basic Rx have a single subdevice. The Basic Rx # has two independent channels, treated as separate subdevices). # subdevice 0 of a daughterboard may use 1 or 2 A/D's. We determine this # by checking the is_quadrature() method. If subdevice 0 uses only a single # A/D, it's possible that the daughterboard has a second subdevice, subdevice 1, # and it uses the second A/D. # # If the card uses only a single A/D, we wire a zero into the DDC Q input. # # (side, 0) says connect only the A/D's used by subdevice 0 to the DDC. # (side, 1) says connect only the A/D's used by subdevice 1 to the DDC. # side = subdev_spec[0] # side A = 0, side B = 1 if not(side in (0, 1)): raise ValueError, "Invalid subdev_spec: %r:" % (subdev_spec,) db = u.db[side] # This is a tuple of length 1 or 2 containing the subdevice # classes for the selected side. # compute bitmasks of used A/D's if db[0].is_quadrature(): subdev0_uses = 0x3 # uses A/D 0 and 1 else: subdev0_uses = 0x1 # uses A/D 0 only if len(db) > 1: subdev1_uses = 0x2 # uses A/D 1 only else: subdev1_uses = 0x0 # uses no A/D (doesn't exist) if subdev_spec[1] == 0: uses = subdev0_uses elif subdev_spec[1] == 1: uses = subdev1_uses else: raise ValueError, "Invalid subdev_spec: %r: " % (subdev_spec,) if uses == 0: raise RuntimeError, "Daughterboard doesn't have a subdevice 1: %r: " % (subdev_spec,) swap_iq = db[0].i_and_q_swapped() truth_table = { # (side, uses, swap_iq) : mux_val (0, 0x1, False) : 0xf0f0f0f0, (0, 0x2, False) : 0xf0f0f0f1, (0, 0x3, False) : 0x00000010, (0, 0x3, True) : 0x00000001, (1, 0x1, False) : 0xf0f0f0f2, (1, 0x2, False) : 0xf0f0f0f3, (1, 0x3, False) : 0x00000032, (1, 0x3, True) : 0x00000023 } return gru.hexint(truth_table[(side, uses, swap_iq)])def determine_tx_mux_value(u, subdev_spec): """ Determine appropriate Tx mux value as a function of the subdevice choosen. @param u: instance of USRP source @param subdev_spec: return value from subdev option parser. @type subdev_spec: (side, subdev), where side is 0 or 1 and subdev is 0 @returns: the Rx mux value """ # This is simpler than the rx case. Either you want to talk # to side A or side B. If you want to talk to both sides at once, # determine the value manually. side = subdev_spec[0] # side A = 0, side B = 1 if not(side in (0, 1)): raise ValueError, "Invalid subdev_spec: %r:" % (subdev_spec,) return gru.hexint([0x0098, 0x9800][side])def selected_subdev(u, subdev_spec): """ Return the user specified daughterboard subdevice. @param u: an instance of usrp.source_* or usrp.sink_* @param subdev_spec: return value from subdev option parser. @type subdev_spec: (side, subdev), where side is 0 or 1 and subdev is 0 or 1 @returns: an weakref to an instance derived from db_base """ side, subdev = subdev_spec # Note: This allows db to go out of scope at the right time return weakref.proxy(u.db[side][subdev])def calc_dxc_freq(target_freq, baseband_freq, fs): """ Calculate the frequency to use for setting the digital up or down converter. @param target_freq: desired RF frequency (Hz) @type target_freq: number @param baseband_freq: the RF frequency that corresponds to DC in the IF. @type baseband_freq: number @param fs: converter sample rate @type fs: number @returns: 2-tuple (ddc_freq, inverted) where ddc_freq is the value for the ddc and inverted is True if we're operating in an inverted Nyquist zone. """ delta = target_freq - baseband_freq if delta >= 0: while delta > fs: delta -= fs if delta <= fs/2: return (-delta, False) # non-inverted region else: return (delta - fs, True) # inverted region else: while delta < -fs: delta += fs if delta >= -fs/2: return (-delta, False) # non-inverted region else: return (delta + fs, True) # inverted region # ------------------------------------------------------------------------# Utilities# ------------------------------------------------------------------------def pick_tx_subdevice(u): """ The user didn't specify a tx subdevice on the command line. Try for one of these, in order: FLEX_400, FLEX_900, FLEX_1200, FLEX_2400, BASIC_TX, whatever's on side A. @return a subdev_spec """ return pick_subdev(u, (usrp_dbid.FLEX_400_TX, usrp_dbid.FLEX_900_TX, usrp_dbid.FLEX_1200_TX, usrp_dbid.FLEX_2400_TX, usrp_dbid.BASIC_TX))def pick_rx_subdevice(u): """ The user didn't specify an rx subdevice on the command line. Try for one of these, in order: FLEX_400, FLEX_900, FLEX_1200, FLEX_2400, TV_RX, DBS_RX, BASIC_RX, whatever's on side A. @return a subdev_spec """ return pick_subdev(u, (usrp_dbid.FLEX_400_RX, usrp_dbid.FLEX_900_RX, usrp_dbid.FLEX_1200_RX, usrp_dbid.FLEX_2400_RX, usrp_dbid.TV_RX, usrp_dbid.TV_RX_REV_2, usrp_dbid.DBS_RX, usrp_dbid.DBS_RX_REV_2_1, usrp_dbid.BASIC_RX))def pick_subdev(u, candidates): """ @param u: usrp instance @param candidates: list of dbids @returns: subdev specification """ db0 = u.db[0][0].dbid() db1 = u.db[1][0].dbid() for c in candidates: if c == db0: return (0, 0) if c == db1: return (1, 0) if db0 >= 0: return (0, 0) if db1 >= 0: return (1, 0) raise RuntimeError, "No suitable daughterboard found!"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -