📄 usrp.py
字号:
## Copyright 2004,2005,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 3, 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 usrpm import usrp_primsfrom usrpm import usrp_dbidfrom gnuradio import usrp1 # usrp Rev 1 and laterfrom gnuradio import grufrom usrpm.usrp_fpga_regs import *import weakrefFPGA_MODE_NORMAL = usrp1.FPGA_MODE_NORMALFPGA_MODE_LOOPBACK = usrp1.FPGA_MODE_LOOPBACKFPGA_MODE_COUNTING = usrp1.FPGA_MODE_COUNTINGSPI_FMT_xSB_MASK = usrp1.SPI_FMT_xSB_MASKSPI_FMT_LSB = usrp1.SPI_FMT_LSBSPI_FMT_MSB = usrp1.SPI_FMT_MSBSPI_FMT_HDR_MASK = usrp1.SPI_FMT_HDR_MASKSPI_FMT_HDR_0 = usrp1.SPI_FMT_HDR_0SPI_FMT_HDR_1 = usrp1.SPI_FMT_HDR_1SPI_FMT_HDR_2 = usrp1.SPI_FMT_HDR_2SPI_ENABLE_FPGA = usrp1.SPI_ENABLE_FPGASPI_ENABLE_CODEC_A = usrp1.SPI_ENABLE_CODEC_ASPI_ENABLE_CODEC_B = usrp1.SPI_ENABLE_CODEC_BSPI_ENABLE_reserved = usrp1.SPI_ENABLE_reservedSPI_ENABLE_TX_A = usrp1.SPI_ENABLE_TX_ASPI_ENABLE_RX_A = usrp1.SPI_ENABLE_RX_ASPI_ENABLE_TX_B = usrp1.SPI_ENABLE_TX_BSPI_ENABLE_RX_B = usrp1.SPI_ENABLE_RX_B# Import all the daughterboard classes we know about.# This hooks them into the auto-instantiation framework.import db_instantiatorimport db_basicimport db_dbs_rximport db_flexrfimport db_flexrf_mimoimport db_tv_rximport db_wbximport db_dtt754import db_dtt768def _look_for_usrp(which): """ Try to open the specified usrp. @param which: int >= 0 specifying which USRP to open @type which: int @return: Returns version number, or raises RuntimeError @rtype: int """ d = usrp_prims.usrp_find_device(which) if not d: raise RuntimeError, "Unable to find USRP #%d" % (which,) return usrp_prims.usrp_hw_rev(d)def _ensure_rev2(which): v = _look_for_usrp(which) if not v in (2, 4): raise RuntimeError, "Sorry, unsupported USRP revision (rev=%d)" % (v,)class tune_result(object): """ Container for intermediate tuning information. """ def __init__(self, baseband_freq, dxc_freq, residual_freq, inverted): self.baseband_freq = baseband_freq self.dxc_freq = dxc_freq self.residual_freq = residual_freq self.inverted = inverteddef tune(u, chan, subdev, target_freq): """ Set the center frequency we're interested in. @param u: instance of usrp.source_* or usrp.sink_* @param chan: DDC/DUC channel @type chan: int @param subdev: daughterboard subdevice @param target_freq: frequency in Hz @returns False if failure else tune_result Tuning is a two step process. First we ask the front-end to tune as close to the desired frequency as it can. Then we use the result of that operation and our target_frequency to determine the value for the digital down converter. """ # Does this usrp instance do Tx or Rx? rx_p = True try: u.rx_freq except AttributeError: rx_p = False ok, baseband_freq = subdev.set_freq(target_freq) dxc_freq, inverted = calc_dxc_freq(target_freq, baseband_freq, u.converter_rate()) # If the spectrum is inverted, and the daughterboard doesn't do # quadrature downconversion, we can fix the inversion by flipping the # sign of the dxc_freq... (This only happens using the basic_rx board) if subdev.spectrum_inverted(): inverted = not(inverted) if inverted and not(subdev.is_quadrature()): dxc_freq = -dxc_freq inverted = not(inverted) if rx_p: ok = ok and u.set_rx_freq(chan, dxc_freq) else: dxc_freq = -dxc_freq ok = ok and u.set_tx_freq(chan, dxc_freq) if not(ok): return False # residual_freq is the offset left over because of dxc tuning step size if rx_p: residual_freq = dxc_freq - u.rx_freq(chan) else: residual_freq = dxc_freq - u.tx_freq(chan) return tune_result(baseband_freq, dxc_freq, residual_freq, inverted) # ------------------------------------------------------------------------# Build subclasses of raw usrp1.* class that add the db attribute# by automatically instantiating the appropriate daughterboard classes.# [Also provides keyword args.]# ------------------------------------------------------------------------class usrp_common(object): def __init__(self): # read capability register r = self._u._read_fpga_reg(FR_RB_CAPS) if r < 0: r += 2**32 if r == 0xaa55ff77: # value of this reg prior to being defined as cap reg r = ((2 << bmFR_RB_CAPS_NDUC_SHIFT) | (2 << bmFR_RB_CAPS_NDDC_SHIFT) | bmFR_RB_CAPS_RX_HAS_HALFBAND) self._fpga_caps = r if False: print "FR_RB_CAPS = %#08x" % (self._fpga_caps,) print "has_rx_halfband =", self.has_rx_halfband() print "nDDCs =", self.nddc() print "has_tx_halfband =", self.has_tx_halfband() print "nDUCs =", self.nduc() def __getattr__(self, name): return getattr(self._u, name) def tune(self, chan, subdev, target_freq): return tune(self, chan, subdev, target_freq) def has_rx_halfband(self): return self._fpga_caps & bmFR_RB_CAPS_RX_HAS_HALFBAND != 0 def has_tx_halfband(self): return self._fpga_caps & bmFR_RB_CAPS_TX_HAS_HALFBAND != 0 def nddc(self): """ Number of Digital Down Converters implemented in FPGA """ return (self._fpga_caps & bmFR_RB_CAPS_NDDC_MASK) >> bmFR_RB_CAPS_NDDC_SHIFT def nduc(self): """ Number of Digital Up Converters implemented in FPGA """ return (self._fpga_caps & bmFR_RB_CAPS_NDUC_MASK) >> bmFR_RB_CAPS_NDUC_SHIFTclass sink_c(usrp_common): def __init__(self, which=0, interp_rate=128, nchan=1, mux=0x98, fusb_block_size=0, fusb_nblocks=0, fpga_filename="", firmware_filename=""): _ensure_rev2(which) self._u = usrp1.sink_c(which, interp_rate, nchan, mux, 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 sink_s(usrp_common): def __init__(self, which=0, interp_rate=128, nchan=1, mux=0x98, fusb_block_size=0, fusb_nblocks=0, fpga_filename="", firmware_filename=""): _ensure_rev2(which) self._u = usrp1.sink_s(which, interp_rate, nchan, mux, 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -