3 # Copyright 2008,2009 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING. If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
23 from gnuradio import gr, eng_notation
24 from gnuradio import usrp
25 from gnuradio.eng_option import eng_option
26 from optparse import OptionParser
29 from gnuradio import gpio
31 class my_top_block(gr.top_block):
33 def __init__(self, options):
34 gr.top_block.__init__(self)
36 # Create a USRP source with GPIO FPGA build, then configure
37 u = usrp.source_s(decim_rate=options.decim,fpga_filename=gpio.fpga_filename)
39 if options.force_complex_RXA:
40 # This is a dirty hack to force complex mode (receive both I and Q) on basicRX or LFRX
41 # This forces the receive board in RXA (side A) to be used
42 # FIXME: This has as a side effect that the gain for Q is not set. So only use with gain 0 (--gain 0)
43 options.rx_subdev_spec=(0,0)
45 if not (0==options.gain):
46 print "WARNING, you should set the gain to 0 with --gain 0 when using --force-complex-RXA"
47 print "The gain for Q will now still be zero while the gain for I is not"
50 if options.rx_subdev_spec is None:
51 options.rx_subdev_spec = usrp.pick_rx_subdevice(u)
52 u.set_mux(usrp.determine_rx_mux_value(u, options.rx_subdev_spec))
54 subdev = usrp.selected_subdev(u, options.rx_subdev_spec)
55 print "Using RX d'board %s" % (subdev.side_and_name(),)
56 input_rate = u.adc_freq()/u.decim_rate()
57 print "USB sample rate %s" % (eng_notation.num_to_str(input_rate))
59 if options.gain is None:
60 # if no gain was specified, use the mid-point in dB
61 g = subdev.gain_range()
62 options.gain = float(g[0]+g[1])/2
65 #TODO setting gain on basicRX only sets the I channel, use a second subdev to set gain of Q channel
66 #see gnuradio-examples/multi-antenna for possible solutions
67 subdev.set_gain(options.gain)
69 #TODO check if freq has same problem as gain when trying to use complex mode on basicRX
70 r = u.tune(0, subdev, options.freq)
72 sys.stderr.write('Failed to set frequency\n')
77 if options.nsamples is not None:
78 head = gr.head(gr.sizeof_short, int(options.nsamples)*2)
82 ana_strip = gr.and_const_ss(0xFFFE)
83 dig_strip = gr.and_const_ss(0x0001)
84 ana_sink = gr.file_sink(gr.sizeof_short, options.ana_filename)
85 dig_sink = gr.file_sink(gr.sizeof_short, options.dig_filename)
87 self.connect(src, ana_strip, ana_sink)
88 self.connect(src, dig_strip, dig_sink)
90 if __name__ == '__main__':
91 usage="%prog: [options] analog_filename digital_filename"
92 parser = OptionParser(option_class=eng_option, usage=usage)
93 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0),
94 help="select USRP Rx side A or B (default=A)")
95 parser.add_option("-d", "--decim", type="int", default=16,
96 help="set fgpa decimation rate to DECIM [default=%default]")
97 parser.add_option("-f", "--freq", type="eng_float", default=None,
98 help="set frequency to FREQ", metavar="FREQ")
99 parser.add_option("-g", "--gain", type="eng_float", default=None,
100 help="set gain in dB (default is midpoint)")
101 parser.add_option("-N", "--nsamples", type="eng_float", default=None,
102 help="number of samples to collect [default=+inf]")
103 parser.add_option("-F", "--force-complex-RXA", action="store_true", default=False,
104 help="enable basicRX hack to force complex mode on basicRX and LFRX. Only works on side A. Only use with --gain 0")
105 (options, args) = parser.parse_args ()
109 options.ana_filename = args[0]
110 options.dig_filename = args[1]
112 if options.freq is None:
114 sys.stderr.write('You must specify the frequency with -f FREQ\n');
118 tb = my_top_block(options)
120 except KeyboardInterrupt: