3 # Copyright 2008 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.
22 from gnuradio import gr, gru
23 from gnuradio import usrp
24 from gnuradio.eng_option import eng_option
25 from gnuradio import eng_notation
26 from optparse import OptionParser
29 from gnuradio import gpio
31 class my_top_block(gr.top_block):
33 gr.top_block.__init__(self)
37 self.waveform_type = gr.GR_CONST_WAVE
38 self.waveform_ampl = 16000
39 self.waveform_freq = 100.12345e3
40 self.waveform_offset = 0
41 self._instantiate_blocks ()
42 self.set_waveform_type (self.waveform_type)
45 return self.u.dac_freq() / self.interp
47 def usb_throughput (self):
48 return self.usb_freq () * 4
50 def set_waveform_type (self, type):
52 valid waveform types are: gr.GR_SIN_WAVE, gr.GR_CONST_WAVE,
53 gr.GR_UNIFORM and gr.GR_GAUSSIAN
55 self._configure_graph (type)
56 self.waveform_type = type
58 def set_waveform_ampl (self, ampl):
59 self.waveform_ampl = ampl
60 self.siggen.set_amplitude (ampl)
61 self.noisegen.set_amplitude (ampl)
63 def set_waveform_freq (self, freq):
64 self.waveform_freq = freq
65 self.siggen.set_frequency (freq)
67 def set_waveform_offset (self, offset):
68 self.waveform_offset = offset
69 self.siggen.set_offset (offset)
71 def set_interpolator (self, interp):
73 self.siggen.set_sampling_freq (self.usb_freq ())
74 self.u.set_interp_rate (interp)
76 def _instantiate_blocks (self):
78 self.u = usrp.sink_c (0, self.interp,fpga_filename=gpio.fpga_filename)
80 self.siggen = gr.sig_source_c (self.usb_freq (),
86 self.noisegen = gr.noise_source_c (gr.GR_UNIFORM,
88 self.vecgen = gr.vector_source_c ([complex(1.0,0.0),complex(0.0,0.0),complex(1.0,1.0),complex(0.0,1.0)],True)
90 # self.file_sink = gr.file_sink (gr.sizeof_gr_complex, "siggen.dat")
92 def _configure_graph (self, type):
95 self.disconnect_all ()
96 if type == gr.GR_SIN_WAVE:
97 self.connect (self.siggen, self.u)
98 # self.connect (self.siggen, self.file_sink)
99 self.siggen.set_waveform (type)
100 self.src = self.siggen
101 elif type == gr.GR_UNIFORM or type == gr.GR_GAUSSIAN:
102 self.connect (self.noisegen, self.u)
103 self.noisegen.set_type (type)
104 self.src = self.noisegen
105 elif type == gr.GR_CONST_WAVE:
106 self.connect (self.vecgen, self.u)
107 self.src = self.vecgen
109 raise ValueError, type
113 def set_freq(self, target_freq):
115 Set the center frequency we're interested in.
117 @param target_freq: frequency in Hz
120 Tuning is a two step process. First we ask the front-end to
121 tune as close to the desired frequency as it can. Then we use
122 the result of that operation and our target_frequency to
123 determine the value for the digital up converter.
125 r = self.u.tune(self.subdev._which, self.subdev, target_freq)
127 #print "r.baseband_freq =", eng_notation.num_to_str(r.baseband_freq)
128 #print "r.dxc_freq =", eng_notation.num_to_str(r.dxc_freq)
129 #print "r.residual_freq =", eng_notation.num_to_str(r.residual_freq)
130 #print "r.inverted =", r.inverted
138 parser = OptionParser (option_class=eng_option)
139 parser.add_option ("-T", "--tx-subdev-spec", type="subdev", default=(0, 0),
140 help="select USRP Tx side A or B")
141 parser.add_option ("-f", "--rf-freq", type="eng_float", default=None,
142 help="set RF center frequency to FREQ")
143 parser.add_option ("-i", "--interp", type="int", default=512,
144 help="set fgpa interpolation rate to INTERP [default=%default]")
145 parser.add_option ("--sine", dest="type", action="store_const", const=gr.GR_SIN_WAVE,
146 help="generate a complex sinusoid [default]", default=gr.GR_SIN_WAVE)
148 parser.add_option ("--gaussian", dest="type", action="store_const", const=gr.GR_GAUSSIAN,
149 help="generate Gaussian random output")
150 parser.add_option ("--uniform", dest="type", action="store_const", const=gr.GR_UNIFORM,
151 help="generate Uniform random output")
153 parser.add_option ("-w", "--waveform-freq", type="eng_float", default=100e3,
154 help="set waveform frequency to FREQ [default=%default]")
155 parser.add_option ("-a", "--amplitude", type="eng_float", default=16e3,
156 help="set waveform amplitude to AMPLITUDE [default=%default]", metavar="AMPL")
157 parser.add_option ("-g", "--gain", type="eng_float", default=None,
158 help="set output gain to GAIN [default=%default]")
159 parser.add_option ("-o", "--offset", type="eng_float", default=0,
160 help="set waveform offset to OFFSET [default=%default]")
161 parser.add_option ("--digital", dest="type", action="store_const", const=gr.GR_CONST_WAVE,
162 help="generate (only) a digital wave on lsb (will be output on gpio pins with special usrp firmware)")
163 (options, args) = parser.parse_args ()
169 if options.rf_freq is None:
170 sys.stderr.write("usrp_siggen: must specify RF center frequency with -f RF_FREQ\n")
175 tb.set_interpolator (options.interp)
176 tb.set_waveform_type (options.type)
177 tb.set_waveform_freq (options.waveform_freq)
178 tb.set_waveform_ampl (options.amplitude)
179 tb.set_waveform_offset (options.offset)
181 # determine the daughterboard subdevice we're using
182 if options.tx_subdev_spec is None:
183 options.tx_subdev_spec = usrp.pick_tx_subdevice(tb.u)
185 m = usrp.determine_tx_mux_value(tb.u, options.tx_subdev_spec)
186 #print "mux = %#04x" % (m,)
188 tb.subdev = usrp.selected_subdev(tb.u, options.tx_subdev_spec)
189 print "Using TX d'board %s" % (tb.subdev.side_and_name(),)
191 if options.gain is None:
192 tb.subdev.set_gain(tb.subdev.gain_range()[1]) # set max Tx gain
194 tb.subdev.set_gain(options.gain) # set max Tx gain
196 if not tb.set_freq(options.rf_freq):
197 sys.stderr.write('Failed to set RF frequency\n')
200 tb.subdev.set_enable(True) # enable transmitter
204 except KeyboardInterrupt:
207 if __name__ == '__main__':