Merged r9834:9855 from jcorgan/u2-wip into trunk. Catches up gr-usrp2 with the lates...
[debian/gnuradio] / gr-utils / src / python / usrp2_siggen.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2008 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 usrp2
25 from gnuradio.eng_option import eng_option
26 from optparse import OptionParser
27 import sys
28
29 n2s = eng_notation.num_to_str
30
31 class siggen_top_block(gr.top_block):
32     def __init__(self, options):
33         gr.top_block.__init__(self)
34
35         # Create a USRP2 sink with the requested interpolation rate
36         self._u = usrp2.sink_32fc(options.interface, options.mac_addr)
37         self._u.set_interp(options.interp)
38
39         # Set the Tx daughterboard gain as requested
40         if options.gain is None:
41             #g = self._u.gain_range()
42             #options.gain = float(g[0]+g[1])/2
43             options.gain = 0 # Until gain range is implemented
44         self._u.set_gain(options.gain)
45
46         # Tune the USRP2 FPGA and daughterboard to the requested center frequency
47         tr = self._u.set_center_freq(options.tx_freq)
48         if tr == None:
49             sys.stderr.write('Failed to set center frequency\n')
50             raise SystemExit, 1
51
52         #eth_rate = self._u.dac_rate()/self._u.interp_rate()
53         eth_rate = 100e6/options.interp # FIXME
54         
55         # Create a source for the requested waveform type
56         if options.type == gr.GR_SIN_WAVE or options.type == gr.GR_CONST_WAVE:
57             self._src = gr.sig_source_c(eth_rate,              # Sample rate
58                                         options.type,          # Waveform type
59                                         options.waveform_freq, # Waveform frequency
60                                         options.amplitude,     # Waveform amplitude
61                                         options.offset)        # Waveform offset
62             
63         elif options.type == gr.GR_GAUSSIAN or options.type == gr.GR_UNIFORM:
64             self._src = gr.noise_source_c(options.type, options.amplitude)
65         else:
66             sys.stderr.write('Unknown waveform type\n')
67             raise SystemExit, 1
68
69         if options.verbose:
70             print "Network interface:", options.interface
71             print "USRP2 address:", self._u.mac_addr()
72             #print "Using TX d'board %s" % (self._u.tx_name(),)
73             print "Tx gain:", options.gain
74             print "Tx baseband frequency:", n2s(tr.baseband_freq), "Hz"
75             print "Tx DDC frequency:", n2s(tr.dxc_freq), "Hz"
76             print "Tx residual frequency:", n2s(tr.residual_freq), "Hz"
77             print "Tx interpolation rate:", options.interp
78             print "Tx GbE sample rate:", n2s(eth_rate), "samples/sec"
79             if options.type == gr.GR_SIN_WAVE:
80                 print "Baseband waveform type: Sine wave"
81                 print "Baseband waveform frequency:", n2s(options.waveform_freq), "Hz"
82             elif options.type == gr.GR_CONST_WAVE:
83                 print "Baseband waveform type: Constant"
84             elif options.type == gr.GR_GAUSSIAN:
85                 print "Baseband waveform type: Gaussian noise"
86             elif options.type == gr.GR_UNIFORM:
87                 print "Baseband waveform type: Uniform noise"           
88                             
89         # Wire the flowgraph
90         self.connect(self._src, self._u)
91             
92 def get_options():
93     usage="%prog: [options]"
94
95     parser = OptionParser(option_class=eng_option, usage=usage)
96     parser.add_option("-e", "--interface", type="string", default="eth0",
97                       help="use specified Ethernet interface [default=%default]")
98     parser.add_option("-m", "--mac-addr", type="string", default="",
99                       help="use USRP2 at specified MAC address [default=None]")  
100     parser.add_option("-i", "--interp", type="int", default=16,
101                       help="set fgpa decimation rate to DECIM [default=%default]")
102     parser.add_option("-g", "--gain", type="eng_float", default=None,
103                       help="set output gain to GAIN [default=%default]")
104     parser.add_option("-f", "--tx-freq", type="eng_float", default=None,
105                       help="set frequency to FREQ", metavar="FREQ")
106     parser.add_option("-v", "--verbose", action="store_true", default=False,
107                       help="verbose output")
108     parser.add_option("-w", "--waveform-freq", type="eng_float", default=0,
109                       help="set waveform frequency to FREQ [default=%default]")
110     parser.add_option("-a", "--amplitude", type="eng_float", default=0.5,
111                       help="set waveform amplitude to AMPLITUDE (0-1.0) [default=%default]", metavar="AMPL")
112     parser.add_option("--offset", type="eng_float", default=0,
113                       help="set waveform offset to OFFSET [default=%default]")
114     parser.add_option("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
115                       help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
116     parser.add_option("--const", dest="type", action="store_const", const=gr.GR_CONST_WAVE, 
117                       help="generate a constant output")
118     parser.add_option("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
119                       help="generate Gaussian random output")
120     parser.add_option("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
121                       help="generate Uniform random output")
122
123     (options, args) = parser.parse_args ()
124     if len(args) != 0:
125         parser.print_help()
126         raise SystemExit, 1
127     
128     if options.tx_freq is None:
129         parser.print_help()
130         sys.stderr.write('You must specify the frequency with -f FREQ\n');
131         raise SystemExit, 1
132     
133     return options
134
135
136 if __name__ == '__main__':
137     options = get_options()
138     tb = siggen_top_block(options)
139     
140     try:
141         tb.run()
142     except KeyboardInterrupt:
143         pass