Imported Upstream version 3.2.2
[debian/gnuradio] / gr-gpio / src / python / gpio_rx_sfile.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2008,2009 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
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)
10 # any later version.
11
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.
16
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.
21
22
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
27 import sys
28
29 from gnuradio import gpio
30
31 class my_top_block(gr.top_block):
32
33     def __init__(self, options):
34         gr.top_block.__init__(self)
35
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)
38
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)
44            u.set_mux(0x10)
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" 
48              #options.gain=0
49         else:
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))
53
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))
58
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
63
64
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)
68
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)
71         if not r:
72             sys.stderr.write('Failed to set frequency\n')
73             raise SystemExit, 1
74
75         # Connect pipeline
76         src = u
77         if options.nsamples is not None:
78             head = gr.head(gr.sizeof_short, int(options.nsamples)*2)
79             self.connect(u, head)
80             src = head
81
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)
86     
87         self.connect(src, ana_strip, ana_sink)
88         self.connect(src, dig_strip, dig_sink)
89
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 ()
106     if len(args) != 2:
107         parser.print_help()
108         raise SystemExit, 1
109     options.ana_filename = args[0]
110     options.dig_filename = args[1]
111
112     if options.freq is None:
113         parser.print_help()
114         sys.stderr.write('You must specify the frequency with -f FREQ\n');
115         raise SystemExit, 1
116
117     try:
118         tb = my_top_block(options)
119         tb.run()
120     except KeyboardInterrupt:
121         pass