2 # Copyright 2005,2006 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, blks
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_block):
37 def __init__(self, fg, modulator_class, options):
39 See below for what options should hold
42 options = copy.copy(options) # make a copy so we can destructively modify
44 self._verbose = options.verbose
45 self._tx_freq = options.tx_freq # tranmitter's center frequency
46 self._tx_amplitude = options.tx_amplitude # digital amplitude sent to USRP
47 self._tx_subdev_spec = options.tx_subdev_spec # daughterboard to use
48 self._bitrate = options.bitrate # desired bit rate
49 self._interp = options.interp # interpolating rate for the USRP (prelim)
50 self._samples_per_symbol = options.samples_per_symbol # desired samples/baud
51 self._fusb_block_size = options.fusb_block_size # usb info for USRP
52 self._fusb_nblocks = options.fusb_nblocks # usb info for USRP
53 self._use_whitener_offset = options.use_whitener_offset # increment start of whitener XOR data
55 self._modulator_class = modulator_class # the modulator_class we are using
57 if self._tx_freq is None:
58 sys.stderr.write("-f FREQ or --freq FREQ or --tx-freq FREQ must be specified\n")
61 # Set up USRP sink; also adjusts interp, samples_per_symbol, and bitrate
62 self._setup_usrp_sink()
64 # copy the final answers back into options for use by modulator
65 options.samples_per_symbol = self._samples_per_symbol
66 options.bitrate = self._bitrate
67 options.interp = self._interp
70 mod_kwargs = self._modulator_class.extract_kwargs_from_options(options)
72 # Set center frequency of USRP
73 ok = self.set_freq(self._tx_freq)
75 print "Failed to set Tx frequency to %s" % (eng_notation.num_to_str(self._tx_freq),)
79 self.packet_transmitter = \
81 self._modulator_class(fg, **mod_kwargs),
85 use_whitener_offset=options.use_whitener_offset)
88 # Set the USRP for maximum transmit gain
89 # (Note that on the RFX cards this is a nop.)
90 self.set_gain(self.subdev.gain_range()[1])
92 self.amp = gr.multiply_const_cc(1)
93 self.set_tx_amplitude(self._tx_amplitude)
95 # enable Auto Transmit/Receive switching
96 self.set_auto_tr(True)
98 # Display some information about the setup
100 self._print_verbage()
102 # Create and setup transmit path flow graph
103 fg.connect(self.packet_transmitter, self.amp, self.u)
104 gr.hier_block.__init__(self, fg, None, None)
106 def _setup_usrp_sink(self):
108 Creates a USRP sink, determines the settings for best bitrate,
109 and attaches to the transmitter's subdevice.
111 self.u = usrp.sink_c(fusb_block_size=self._fusb_block_size,
112 fusb_nblocks=self._fusb_nblocks)
113 dac_rate = self.u.dac_rate();
115 # derive values of bitrate, samples_per_symbol, and interp from desired info
116 (self._bitrate, self._samples_per_symbol, self._interp) = \
117 pick_tx_bitrate(self._bitrate, self._modulator_class.bits_per_symbol(),
118 self._samples_per_symbol, self._interp, dac_rate)
120 self.u.set_interp_rate(self._interp)
122 # determine the daughterboard subdevice we're using
123 if self._tx_subdev_spec is None:
124 self._tx_subdev_spec = usrp.pick_tx_subdevice(self.u)
125 self.u.set_mux(usrp.determine_tx_mux_value(self.u, self._tx_subdev_spec))
126 self.subdev = usrp.selected_subdev(self.u, self._tx_subdev_spec)
129 def set_freq(self, target_freq):
131 Set the center frequency we're interested in.
133 @param target_freq: frequency in Hz
136 Tuning is a two step process. First we ask the front-end to
137 tune as close to the desired frequency as it can. Then we use
138 the result of that operation and our target_frequency to
139 determine the value for the digital up converter.
141 r = self.u.tune(self.subdev._which, self.subdev, target_freq)
147 def set_gain(self, gain):
149 Sets the analog gain in the USRP
152 self.subdev.set_gain(gain)
154 def set_tx_amplitude(self, ampl):
156 Sets the transmit amplitude sent to the USRP
157 @param: ampl 0 <= ampl < 32768. Try 8000
159 self._tx_amplitude = max(0.0, min(ampl, 32767.0))
160 self.amp.set_k(self._tx_amplitude)
162 def set_auto_tr(self, enable):
164 Turns on auto transmit/receive of USRP daughterboard (if exits; else ignored)
166 return self.subdev.set_auto_tr(enable)
168 def send_pkt(self, payload='', eof=False):
170 Calls the transmitter method to send a packet
172 return self.packet_transmitter.send_pkt(payload, eof)
177 def samples_per_symbol(self):
178 return self._samples_per_symbol
183 def add_options(normal, expert):
185 Adds transmitter-specific options to the Options Parser
187 add_freq_option(normal)
188 if not normal.has_option('--bitrate'):
189 normal.add_option("-r", "--bitrate", type="eng_float", default=None,
190 help="specify bitrate. samples-per-symbol and interp/decim will be derived.")
191 normal.add_option("-T", "--tx-subdev-spec", type="subdev", default=None,
192 help="select USRP Tx side A or B")
193 normal.add_option("", "--tx-amplitude", type="eng_float", default=12000, metavar="AMPL",
194 help="set transmitter digital amplitude: 0 <= AMPL < 32768 [default=%default]")
195 normal.add_option("-v", "--verbose", action="store_true", default=False)
197 expert.add_option("-S", "--samples-per-symbol", type="int", default=None,
198 help="set samples/symbol [default=%default]")
199 expert.add_option("", "--tx-freq", type="eng_float", default=None,
200 help="set transmit frequency to FREQ [default=%default]", metavar="FREQ")
201 expert.add_option("-i", "--interp", type="intx", default=None,
202 help="set fpga interpolation rate to INTERP [default=%default]")
203 expert.add_option("", "--log", action="store_true", default=False,
204 help="Log all parts of flow graph to file (CAUTION: lots of data)")
205 expert.add_option("","--use-whitener-offset", action="store_true", default=False,
206 help="make sequential packets use different whitening")
208 # Make a static method to call before instantiation
209 add_options = staticmethod(add_options)
211 def _print_verbage(self):
213 Prints information about the transmit path
215 print "Using TX d'board %s" % (self.subdev.side_and_name(),)
216 print "Tx amplitude %s" % (self._tx_amplitude)
217 print "modulation: %s" % (self._modulator_class.__name__)
218 print "bitrate: %sb/s" % (eng_notation.num_to_str(self._bitrate))
219 print "samples/symbol: %3d" % (self._samples_per_symbol)
220 print "interp: %3d" % (self._interp)
221 print "Tx Frequency: %s" % (eng_notation.num_to_str(self._tx_freq))
224 def add_freq_option(parser):
226 Hackery that has the -f / --freq option set both tx_freq and rx_freq
228 def freq_callback(option, opt_str, value, parser):
229 parser.values.rx_freq = value
230 parser.values.tx_freq = value
232 if not parser.has_option('--freq'):
233 parser.add_option('-f', '--freq', type="eng_float",
234 action="callback", callback=freq_callback,
235 help="set Tx and/or Rx frequency to FREQ [default=%default]",