1 | # |
---|
2 | # Copyright 2009 Free Software Foundation, Inc. |
---|
3 | # |
---|
4 | # This file is part of GNU Radio |
---|
5 | # |
---|
6 | # GNU Radio is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 3, or (at your option) |
---|
9 | # any later version. |
---|
10 | # |
---|
11 | # GNU Radio is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with GNU Radio; see the file COPYING. If not, write to |
---|
18 | # the Free Software Foundation, Inc., 51 Franklin Street, |
---|
19 | # Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | |
---|
22 | USRP_TYPE = 'usrp' |
---|
23 | USRP2_TYPE = 'usrp2' |
---|
24 | |
---|
25 | from gnuradio import gr, usrp, usrp2 |
---|
26 | |
---|
27 | ######################################################################## |
---|
28 | # generic usrp common stuff |
---|
29 | ######################################################################## |
---|
30 | class _generic_usrp_base(object): |
---|
31 | |
---|
32 | def __init__(self, which=0, subdev_spec=None, interface="", mac_addr="", fusb_block_size=0, fusb_nblocks=0, usrpx=None): |
---|
33 | self._gain = 0 |
---|
34 | self._usrpx = usrpx |
---|
35 | #usrp options |
---|
36 | self._which = which |
---|
37 | self._subdev_spec = subdev_spec |
---|
38 | #usrp2 options |
---|
39 | self._interface = interface |
---|
40 | self._mac_addr = mac_addr |
---|
41 | #fusb options |
---|
42 | self._fusb_block_size = fusb_block_size |
---|
43 | self._fusb_nblocks = fusb_nblocks |
---|
44 | |
---|
45 | def __str__(self): |
---|
46 | if self._type == USRP_TYPE: return self._subdev.side_and_name() |
---|
47 | elif self._type == USRP2_TYPE: return "D-Board ID 0x%x\n"%self._u.daughterboard_id() |
---|
48 | |
---|
49 | def gain(self): return self._gain |
---|
50 | |
---|
51 | def set_gain(self, gain=None): |
---|
52 | if gain is None: |
---|
53 | r = self.gain_range() |
---|
54 | gain = (r[0] + r[1])/2 # set gain to midpoint |
---|
55 | self._gain = gain |
---|
56 | if self._type == USRP_TYPE: return self._subdev.set_gain(gain) |
---|
57 | elif self._type == USRP2_TYPE: return self._u.set_gain(gain) |
---|
58 | |
---|
59 | def gain_range(self): |
---|
60 | if self._type == USRP_TYPE: return self._subdev.gain_range() |
---|
61 | elif self._type == USRP2_TYPE: return self._u.gain_range() |
---|
62 | |
---|
63 | def set_center_freq(self, target_freq): |
---|
64 | if self._type == USRP_TYPE: |
---|
65 | return bool(self._u.tune(self._dxc, self._subdev, target_freq)) |
---|
66 | elif self._type == USRP2_TYPE: |
---|
67 | return self._u.set_center_freq(target_freq) |
---|
68 | |
---|
69 | def set_auto_tr(self, enable): |
---|
70 | if self._type == USRP_TYPE: return self._subdev.set_auto_tr(enable) |
---|
71 | |
---|
72 | def __del__(self): |
---|
73 | # Avoid weak reference error |
---|
74 | if self._type == USRP_TYPE: del self._subdev |
---|
75 | |
---|
76 | ######################################################################## |
---|
77 | # generic usrp source |
---|
78 | ######################################################################## |
---|
79 | class generic_usrp_source_c(_generic_usrp_base, gr.hier_block2): |
---|
80 | """ |
---|
81 | Create a generic usrp source that represents usrp and usrp2. |
---|
82 | Take usrp and usrp2 constructor arguments and try to figure out usrp or usrp2. |
---|
83 | Provide generic access methods so the API looks the same for both. |
---|
84 | """ |
---|
85 | |
---|
86 | def __init__(self, **kwargs): |
---|
87 | _generic_usrp_base.__init__(self, **kwargs) |
---|
88 | gr.hier_block2.__init__(self, "generic_usrp_source", |
---|
89 | gr.io_signature(0, 0, 0), # Input signature |
---|
90 | gr.io_signature(1, 1, gr.sizeof_gr_complex)) # Output signature |
---|
91 | #pick usrp or usrp2 |
---|
92 | if self._usrpx == '1' or self._subdev_spec: |
---|
93 | self._setup_usrp_source() |
---|
94 | elif self._usrpx == '2' or self._mac_addr: |
---|
95 | self._setup_usrp2_source() |
---|
96 | else: #automatic |
---|
97 | try: self._setup_usrp2_source() |
---|
98 | except: self._setup_usrp_source() |
---|
99 | self.connect(self._u, self) |
---|
100 | self.set_auto_tr(True) |
---|
101 | |
---|
102 | #################################################################### |
---|
103 | # generic access methods |
---|
104 | #################################################################### |
---|
105 | def set_decim(self, decim): |
---|
106 | if self._type == USRP_TYPE: return self._u.set_decim_rate(decim) |
---|
107 | elif self._type == USRP2_TYPE: return self._u.set_decim(decim) |
---|
108 | |
---|
109 | def adc_rate(self): return self._u.adc_rate() |
---|
110 | |
---|
111 | #################################################################### |
---|
112 | # setup usrp methods |
---|
113 | #################################################################### |
---|
114 | def _setup_usrp_source(self): |
---|
115 | self._u = usrp.source_c (self._which, |
---|
116 | fusb_block_size=self._fusb_block_size, |
---|
117 | fusb_nblocks=self._fusb_nblocks) |
---|
118 | # determine the daughterboard subdevice we're using |
---|
119 | if self._subdev_spec is None: |
---|
120 | self._subdev_spec = usrp.pick_rx_subdevice(self._u) |
---|
121 | self._subdev = usrp.selected_subdev(self._u, self._subdev_spec) |
---|
122 | self._u.set_mux(usrp.determine_rx_mux_value(self._u, self._subdev_spec)) |
---|
123 | self._type = USRP_TYPE |
---|
124 | self._dxc = 0 |
---|
125 | |
---|
126 | def _setup_usrp2_source(self): |
---|
127 | self._u = usrp2.source_32fc(self._interface, self._mac_addr) |
---|
128 | self._type = USRP2_TYPE |
---|
129 | |
---|
130 | ######################################################################## |
---|
131 | # generic usrp sink |
---|
132 | ######################################################################## |
---|
133 | class generic_usrp_sink_c(_generic_usrp_base, gr.hier_block2): |
---|
134 | """ |
---|
135 | Create a generic usrp sink that represents usrp and usrp2. |
---|
136 | Take usrp and usrp2 constructor arguments and try to figure out usrp or usrp2. |
---|
137 | Provide generic access methods so the API looks the same for both. |
---|
138 | """ |
---|
139 | |
---|
140 | def __init__(self, **kwargs): |
---|
141 | _generic_usrp_base.__init__(self, **kwargs) |
---|
142 | gr.hier_block2.__init__(self, "generic_usrp_sink", |
---|
143 | gr.io_signature(1, 1, gr.sizeof_gr_complex), # Input signature |
---|
144 | gr.io_signature(0, 0, 0)) # Output signature |
---|
145 | |
---|
146 | #pick usrp or usrp2 |
---|
147 | if self._usrpx == '1' or self._subdev_spec: |
---|
148 | self._setup_usrp_source() |
---|
149 | elif self._usrpx == '2' or self._mac_addr: |
---|
150 | self._setup_usrp2_source() |
---|
151 | else: #automatic |
---|
152 | try: self._setup_usrp2_source() |
---|
153 | except: self._setup_usrp_source() |
---|
154 | self.connect(self, self._u) |
---|
155 | self.set_auto_tr(True) |
---|
156 | |
---|
157 | #################################################################### |
---|
158 | # generic access methods |
---|
159 | #################################################################### |
---|
160 | def set_interp(self, interp): |
---|
161 | if self._type == USRP_TYPE: return self._u.set_interp_rate(interp) |
---|
162 | elif self._type == USRP2_TYPE: return self._u.set_interp(interp) |
---|
163 | |
---|
164 | def dac_rate(self): return self._u.dac_rate() |
---|
165 | |
---|
166 | def ampl_range(self): |
---|
167 | if self._type == USRP_TYPE: return (0.0, 2.**15-1) |
---|
168 | elif self._type == USRP2_TYPE: return (0.0, 1.0) |
---|
169 | |
---|
170 | #################################################################### |
---|
171 | # setup usrp methods |
---|
172 | #################################################################### |
---|
173 | def _setup_usrp_source(self): |
---|
174 | self._u = usrp.sink_c (self._which, |
---|
175 | fusb_block_size=self._fusb_block_size, |
---|
176 | fusb_nblocks=self._fusb_nblocks) |
---|
177 | # determine the daughterboard subdevice we're using |
---|
178 | if self._subdev_spec is None: |
---|
179 | self._subdev_spec = usrp.pick_tx_subdevice(self._u) |
---|
180 | self._subdev = usrp.selected_subdev(self._u, self._subdev_spec) |
---|
181 | self._u.set_mux(usrp.determine_tx_mux_value(self._u, self._subdev_spec)) |
---|
182 | self._type = USRP_TYPE |
---|
183 | self._dxc = self._subdev.which() |
---|
184 | |
---|
185 | def _setup_usrp2_source(self): |
---|
186 | self._u = usrp2.sink_32fc(self._interface, self._mac_addr) |
---|
187 | self._type = USRP2_TYPE |
---|