2 # Copyright 2005,2006,2007 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
22 from gnuradio import gr, gru, blks2
23 from gnuradio import usrp
24 from gnuradio import eng_notation
30 from pick_bitrate import pick_tx_bitrate
32 # /////////////////////////////////////////////////////////////////////////////
34 # /////////////////////////////////////////////////////////////////////////////
36 class transmit_path(gr.hier_block2):
37 def __init__(self, modulator_class, options):
39 See below for what options should hold
41 gr.hier_block2.__init__(self, "transmit_path",
42 gr.io_signature(0, 0, 0), # Input signature
43 gr.io_signature(0, 0, 0)) # Output signature
45 options = copy.copy(options) # make a copy so we can destructively modify
47 self._which = options.which # the USRP board attached
48 self._verbose = options.verbose
49 self._tx_freq = options.tx_freq # tranmitter's center frequency
50 self._tx_amplitude = options.tx_amplitude # digital amplitude sent to USRP
51 self._tx_subdev_spec = options.tx_subdev_spec # daughterboard to use
52 self._bitrate = options.bitrate # desired bit rate
53 self._interp = options.interp # interpolating rate for the USRP (prelim)
54 self._samples_per_symbol = options.samples_per_symbol # desired samples/baud
55 self._fusb_block_size = options.fusb_block_size # usb info for USRP
56 self._fusb_nblocks = options.fusb_nblocks # usb info for USRP
57 self._use_whitener_offset = options.use_whitener_offset # increment start of whitener XOR data
59 self._modulator_class = modulator_class # the modulator_class we are using
61 if self._tx_freq is None:
62 sys.stderr.write("-f FREQ or --freq FREQ or --tx-freq FREQ must be specified\n")
65 # Set up USRP sink; also adjusts interp, samples_per_symbol, and bitrate
66 self._setup_usrp_sink()
68 # copy the final answers back into options for use by modulator
69 options.samples_per_symbol = self._samples_per_symbol
70 options.bitrate = self._bitrate
71 options.interp = self._interp
74 mod_kwargs = self._modulator_class.extract_kwargs_from_options(options)
76 # Set center frequency of USRP
77 ok = self.set_freq(self._tx_freq)
79 print "Failed to set Tx frequency to %s" % (eng_notation.num_to_str(self._tx_freq),)
83 self.packet_transmitter = \
84 blks2.mod_pkts(self._modulator_class(**mod_kwargs),
88 use_whitener_offset=options.use_whitener_offset)
91 # Set the USRP for maximum transmit gain
92 # (Note that on the RFX cards this is a nop.)
93 self.set_gain(self.subdev.gain_range()[1])
95 self.amp = gr.multiply_const_cc(1)
96 self.set_tx_amplitude(self._tx_amplitude)
98 # enable Auto Transmit/Receive switching
99 self.set_auto_tr(True)
101 # Display some information about the setup
103 self._print_verbage()
105 # Create and setup transmit path flow graph
106 self.connect(self.packet_transmitter, self.amp, self.u)
108 def _setup_usrp_sink(self):
110 Creates a USRP sink, determines the settings for best bitrate,
111 and attaches to the transmitter's subdevice.
113 self.u = usrp.sink_c(self._which,
114 fusb_block_size=self._fusb_block_size,
115 fusb_nblocks=self._fusb_nblocks)
116 dac_rate = self.u.dac_rate();
118 # derive values of bitrate, samples_per_symbol, and interp from desired info
119 (self._bitrate, self._samples_per_symbol, self._interp) = \
120 pick_tx_bitrate(self._bitrate, self._modulator_class.bits_per_symbol(),
121 self._samples_per_symbol, self._interp, dac_rate)
123 self.u.set_interp_rate(self._interp)
125 # determine the daughterboard subdevice we're using
126 if self._tx_subdev_spec is None:
127 self._tx_subdev_spec = usrp.pick_tx_subdevice(self.u)
128 self.u.set_mux(usrp.determine_tx_mux_value(self.u, self._tx_subdev_spec))
129 self.subdev = usrp.selected_subdev(self.u, self._tx_subdev_spec)
132 def set_freq(self, target_freq):
134 Set the center frequency we're interested in.
136 @param target_freq: frequency in Hz
139 Tuning is a two step process. First we ask the front-end to
140 tune as close to the desired frequency as it can. Then we use
141 the result of that operation and our target_frequency to
142 determine the value for the digital up converter.
144 r = self.u.tune(self.subdev.which(), self.subdev, target_freq)
150 def set_gain(self, gain):
152 Sets the analog gain in the USRP
155 self.subdev.set_gain(gain)
157 def set_tx_amplitude(self, ampl):
159 Sets the transmit amplitude sent to the USRP
160 @param: ampl 0 <= ampl < 32768. Try 8000
162 self._tx_amplitude = max(0.0, min(ampl, 32767.0))
163 self.amp.set_k(self._tx_amplitude)
165 def set_auto_tr(self, enable):
167 Turns on auto transmit/receive of USRP daughterboard (if exits; else ignored)
169 return self.subdev.set_auto_tr(enable)
171 def send_pkt(self, payload='', eof=False):
173 Calls the transmitter method to send a packet
175 return self.packet_transmitter.send_pkt(payload, eof)
180 def samples_per_symbol(self):
181 return self._samples_per_symbol
186 def add_options(normal, expert):
188 Adds transmitter-specific options to the Options Parser
190 add_freq_option(normal)
191 if not normal.has_option('--bitrate'):
192 normal.add_option("-r", "--bitrate", type="eng_float", default=None,
193 help="specify bitrate. samples-per-symbol and interp/decim will be derived.")
194 normal.add_option("-w", "--which", type="int", default=0,
195 help="select USRP board [default=%default]")
196 normal.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
197 help="select USRP Tx side A or B")
198 normal.add_option("", "--tx-amplitude", type="eng_float", default=12000, metavar="AMPL",
199 help="set transmitter digital amplitude: 0 <= AMPL < 32768 [default=%default]")
200 normal.add_option("-v", "--verbose", action="store_true", default=False)
202 expert.add_option("-S", "--samples-per-symbol", type="int", default=None,
203 help="set samples/symbol [default=%default]")
204 expert.add_option("", "--tx-freq", type="eng_float", default=None,
205 help="set transmit frequency to FREQ [default=%default]", metavar="FREQ")
206 expert.add_option("-i", "--interp", type="intx", default=None,
207 help="set fpga interpolation rate to INTERP [default=%default]")
208 expert.add_option("", "--log", action="store_true", default=False,
209 help="Log all parts of flow graph to file (CAUTION: lots of data)")
210 expert.add_option("","--use-whitener-offset", action="store_true", default=False,
211 help="make sequential packets use different whitening")
213 # Make a static method to call before instantiation
214 add_options = staticmethod(add_options)
216 def _print_verbage(self):
218 Prints information about the transmit path
220 print "Using TX d'board %s" % (self.subdev.side_and_name(),)
221 print "Tx amplitude %s" % (self._tx_amplitude)
222 print "modulation: %s" % (self._modulator_class.__name__)
223 print "bitrate: %sb/s" % (eng_notation.num_to_str(self._bitrate))
224 print "samples/symbol: %3d" % (self._samples_per_symbol)
225 print "interp: %3d" % (self._interp)
226 print "Tx Frequency: %s" % (eng_notation.num_to_str(self._tx_freq))
229 def add_freq_option(parser):
231 Hackery that has the -f / --freq option set both tx_freq and rx_freq
233 def freq_callback(option, opt_str, value, parser):
234 parser.values.rx_freq = value
235 parser.values.tx_freq = value
237 if not parser.has_option('--freq'):
238 parser.add_option('-f', '--freq', type="eng_float",
239 action="callback", callback=freq_callback,
240 help="set Tx and/or Rx frequency to FREQ [default=%default]",