Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-examples / python / usrp / flexrf_debug.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004 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 2, 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, gru
24 from gnuradio import usrp
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from gnuradio.wxgui import stdgui, fftsink, scopesink, slider
28 from optparse import OptionParser
29 import wx
30
31 class app_flow_graph (stdgui.gui_flow_graph):
32     def __init__(self, frame, panel, vbox, argv):
33         stdgui.gui_flow_graph.__init__ (self, frame, panel, vbox, argv)
34
35         self.frame = frame
36         self.panel = panel
37         
38         parser = OptionParser (option_class=eng_option)
39         parser.add_option ("-d", "--decim", type="int", default=8,
40                            help="set fgpa decimation rate to DECIM")
41         parser.add_option ("-c", "--ddc-freq", type="eng_float", default=0,
42                            help="set Digital downconverter frequency to FREQ", metavar="FREQ")
43         parser.add_option ("-f", "--freq", type="eng_float", default=950e6,
44                            help="set RF downconverter frequency to FREQ", metavar="FREQ")
45         parser.add_option ("-m", "--mux", type="intx", default=0x32103210,
46                            help="set fpga FR_RX_MUX register to MUX")
47         parser.add_option ("-g", "--gain", type="eng_float", default=0,
48                            help="set Rx PGA gain in dB (default 0 dB)")
49         (options, args) = parser.parse_args ()
50
51         self.u = usrp.source_c (0, options.decim, 1, gru.hexint(options.mux), 0)
52         
53         self.u.set_verbose (0)
54         
55         input_rate = self.u.adc_freq () / self.u.decim_rate ()
56
57         block = fftsink.fft_sink_c (self, panel, fft_size=1024, sample_rate=input_rate)
58         self.connect (self.u, block)
59         vbox.Add (block.win, 10, wx.EXPAND)
60
61         if 0:
62             c2f_1 = gr.complex_to_float ()
63             scope = scopesink.scope_sink_f (self, panel, "Rx Data", input_rate)
64             vbox.Add (scope.win, 6, wx.EXPAND)
65
66             self.connect (self.u,c2f_1)
67             self.connect ((c2f_1, 0), (scope, 0))
68             self.connect ((c2f_1, 1), (scope, 1))
69
70         if 0:
71             rms_complex = gr.rms_cf(.0001)
72             rms_i = gr.rms_ff(.0001)
73             rms_q = gr.rms_ff(.0001)
74             
75             self.connect(self.u,rms_complex)
76             self.connect((c2f_1,0),rms_i)
77             self.connect((c2f_1,1),rms_q)
78             
79             ns1 = gr.null_sink(4)
80             ns2 = gr.null_sink(4)
81             ns3 = gr.null_sink(4)
82             
83             self.connect(rms_complex,ns1)
84             self.connect(rms_i,ns2)
85             self.connect(rms_q,ns3)
86
87         # sliders
88
89         #vbox.Add(slider.slider(panel, 0, 104, self.set_gain), 1, wx.ALIGN_CENTER)
90
91         #vbox.Add(slider.slider(panel, 0, 4095, self.set_gain_gc1), 1, wx.ALIGN_CENTER)
92         #vbox.Add(slider.slider(panel, 0, 31, self.set_gain_gc2), 1, wx.ALIGN_CENTER)
93         #vbox.Add(slider.slider(panel, 0, 1, self.set_gain_dl), 1, wx.ALIGN_CENTER)
94         #vbox.Add(slider.slider(panel, 0, 200, self.set_gain_i), 1, wx.ALIGN_CENTER)
95         #vbox.Add(slider.slider(panel, 0, 200, self.set_gain_q), 1, wx.ALIGN_CENTER)
96
97         self.offset = 0
98         #vbox.Add(slider.slider(panel, -200, 200, self.set_offset_i), 1, wx.ALIGN_CENTER)
99         #vbox.Add(slider.slider(panel, -200, 200, self.set_offset_q), 1, wx.ALIGN_CENTER)
100
101         vbox.Add(slider.slider(panel, 380, 480, self.set_rf_freq), 1, wx.EXPAND|wx.ALIGN_CENTER)
102         vbox.Add(slider.slider(panel, -32000, +32000, self.set_if_freq), 1, wx.EXPAND|wx.ALIGN_CENTER)
103         vbox.Add(slider.slider(panel, 0, 4095, self.set_gain), 1, wx.EXPAND|wx.ALIGN_CENTER)
104
105         # build small control area at bottom
106         hbox = wx.BoxSizer (wx.HORIZONTAL)
107         hbox.Add ((1, 1), 1, wx.EXPAND)
108         hbox.Add (wx.StaticText (panel, -1, "Set ddc freq: "), 0, wx.ALIGN_CENTER)
109         self.tc_freq = wx.TextCtrl (panel, -1, "", style=wx.TE_PROCESS_ENTER)
110         hbox.Add (self.tc_freq, 0, wx.ALIGN_CENTER)
111         wx.EVT_TEXT_ENTER (self.tc_freq, self.tc_freq.GetId(), self.handle_text_enter)
112         hbox.Add ((1, 1), 1, wx.EXPAND)
113         # add it to the main vbox
114         vbox.Add (hbox, 0, wx.EXPAND)
115
116         self.update_status_bar ()
117
118     def set_rf_freq (self,freq):
119         (success,actual_freq) = self.set_freq(1e6*freq)
120         if not success:
121             print "Failed on ",freq
122     def set_if_freq (self,freq):
123         self.u.set_rx_freq(0,freq*1e3)
124             
125     def set_gain (self,gain):
126         self.rfrx.set_gain(gain)
127
128     def set_gain_i (self,gain):
129         self.u.set_pga(0,gain/10.0)
130     def set_gain_q (self,gain):
131         self.u.set_pga(1,gain/10.0)
132
133     def set_offset_i(self,offset):
134         self.offset = (self.offset & 0x0000ffff) | ((offset&0xffff)<<16)
135         self.u._write_fpga_reg (3,self.offset)
136
137     def set_offset_q(self,offset):
138         self.offset = (self.offset & 0xffff0000) | (offset&0xffff)
139         self.u._write_fpga_reg (3,self.offset)
140
141     def handle_text_enter (self, event):
142         str = event.GetString ()
143         self.tc_freq.Clear ()
144         self.u.set_rx_freq (0, eng_notation.str_to_num (str))
145         self.update_status_bar ()
146
147     def update_status_bar (self):
148         ddc_freq = self.u.rx_freq (0)
149         decim_rate = self.u.decim_rate ()
150         sample_rate = self.u.adc_freq () / decim_rate
151         msg = "decim: %d  %sS/s  DDC: %s" % (
152             decim_rate,
153             eng_notation.num_to_str (sample_rate),
154             eng_notation.num_to_str (ddc_freq))
155             
156         self.frame.GetStatusBar().SetStatusText (msg, 1)
157
158     def set_gain(self,gain):
159         assert gain>=0 and gain<4096
160         self.u.write_aux_dac(0,0,int(gain))
161         
162 def main ():
163     app = stdgui.stdapp (app_flow_graph, "USRP FFT")
164     app.MainLoop ()
165
166 if __name__ == '__main__':
167     main ()
168
169