For USRP2, implement auto scaling of TX pipeline such that [-1.0 1.0] input to
[debian/gnuradio] / usrp2 / host / lib / usrp2.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <usrp2/usrp2.h>
24 #include "usrp2_impl.h"
25 #include <vector>
26 #include <boost/thread.hpp>
27 #include <boost/weak_ptr.hpp>
28 #include <string>
29 #include <stdexcept>
30
31 namespace usrp2 {
32
33   // --- Table of weak pointers to usrps we know about ---
34
35   // (Could be cleaned up and turned into a template)
36
37   struct usrp_table_entry {
38     // inteface + normalized mac addr ("eth0:01:23:45:67:89:ab")
39     std::string key;
40     boost::weak_ptr<usrp2::usrp2>  value;
41
42     usrp_table_entry(const std::string &_key, boost::weak_ptr<usrp2::usrp2> _value)
43       : key(_key), value(_value) {}
44   };
45
46   typedef std::vector<usrp_table_entry> usrp_table;
47
48   static boost::mutex s_table_mutex;
49   static usrp_table s_table;
50
51   usrp2::sptr
52   usrp2::find_existing_or_make_new(const std::string &ifc, props *pr)
53   {
54     std::string key = ifc + ":" + pr->addr;
55
56     boost::mutex::scoped_lock   guard(s_table_mutex);
57
58     for (usrp_table::iterator p = s_table.begin(); p != s_table.end();){
59       if (p->value.expired())   // weak pointer is now dead
60         p = s_table.erase(p);   // erase it
61       else {
62         if (key == p->key)      // found it
63           return usrp2::sptr(p->value);
64         else                    
65           ++p;                  // keep looking
66       }
67     }
68
69     // We don't have the USRP2 we're looking for
70
71     // create a new one and stick it in the table.
72     usrp2::sptr r(new usrp2::usrp2(ifc, pr));
73     usrp_table_entry t(key, r);
74     s_table.push_back(t);
75
76     return r;
77   }
78
79   // --- end of table code ---
80
81   static bool
82   parse_mac_addr(const std::string &s, std::string &ns)
83   {
84     u2_mac_addr_t p;
85
86     p.addr[0] = 0x00;           // Matt's IAB
87     p.addr[1] = 0x50;
88     p.addr[2] = 0xC2;
89     p.addr[3] = 0x85;
90     p.addr[4] = 0x30;
91     p.addr[5] = 0x00;
92     
93     int len = s.size();
94     switch (len) {
95       
96     case 5:
97       if (sscanf(s.c_str(), "%hhx:%hhx", &p.addr[4], &p.addr[5]) != 2)
98         return false;
99       break;
100       
101     case 17:
102       if (sscanf(s.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
103                  &p.addr[0], &p.addr[1], &p.addr[2],
104                  &p.addr[3], &p.addr[4], &p.addr[5]) != 6)
105         return false;
106       break;
107
108     default:
109       return false;
110     }
111     
112     char buf[128];
113     snprintf(buf, sizeof(buf),
114              "%02x:%02x:%02x:%02x:%02x:%02x",
115              p.addr[0],p.addr[1],p.addr[2],
116              p.addr[3],p.addr[4],p.addr[5]);
117     ns = std::string(buf);
118     return true;
119   }
120
121   usrp2::sptr
122   usrp2::make(const std::string &ifc, const std::string &addr)
123   {
124     std::string naddr = "";
125     if (addr != "" && !parse_mac_addr(addr, naddr))
126       throw std::runtime_error("Invalid MAC address");
127
128     props_vector_t u2s = find(ifc, naddr);
129     int n = u2s.size();
130
131     if (n == 0) {
132       if (addr == "")
133         throw std::runtime_error("No USRPs found on interface " + ifc);
134       else
135         throw std::runtime_error("No USRP found with addr " + addr + " on interface " + ifc);
136     }
137
138     if (n > 1)
139       throw std::runtime_error("Multiple USRPs found on interface; must select by MAC address.");
140
141     return find_existing_or_make_new(ifc, &u2s[0]);
142   }
143
144   // Private constructor.  Sole function is to create an impl.
145   usrp2::usrp2(const std::string &ifc, props *p)
146     : d_impl(new usrp2::impl(ifc, p))
147   {
148     // NOP
149   }
150   
151   // Public class destructor.  d_impl will auto-delete.
152   usrp2::~usrp2()
153   {
154     // NOP
155   }
156   
157   std::string
158   usrp2::mac_addr()
159   {
160     return d_impl->mac_addr();
161   }
162
163   // Receive
164
165   bool 
166   usrp2::set_rx_gain(double gain)
167   {
168     return d_impl->set_rx_gain(gain);
169   }
170   
171   double
172   usrp2::rx_gain_min()
173   {
174     return d_impl->rx_gain_min();
175   }
176
177   double
178   usrp2::rx_gain_max()
179   {
180     return d_impl->rx_gain_max();
181   }
182
183   double
184   usrp2::rx_gain_db_per_step()
185   {
186     return d_impl->rx_gain_db_per_step();
187   }
188
189   bool
190   usrp2::set_rx_center_freq(double frequency, tune_result *result)
191   {
192     return d_impl->set_rx_center_freq(frequency, result);
193   }
194   
195   double
196   usrp2::rx_freq_min()
197   {
198     return d_impl->rx_freq_min();
199   }
200
201   double
202   usrp2::rx_freq_max()
203   {
204     return d_impl->rx_freq_max();
205   }
206
207   bool
208   usrp2::set_rx_decim(int decimation_factor)
209   {
210     return d_impl->set_rx_decim(decimation_factor);
211   }
212   
213   int
214   usrp2::rx_decim()
215   {
216     return d_impl->rx_decim();
217   }
218
219   bool
220   usrp2::set_rx_scale_iq(int scale_i, int scale_q)
221   {
222     return d_impl->set_rx_scale_iq(scale_i, scale_q);
223   }
224   
225   bool
226   usrp2::start_rx_streaming(unsigned int channel, unsigned int items_per_frame)
227   {
228     return d_impl->start_rx_streaming(channel, items_per_frame);
229   }
230   
231   bool
232   usrp2::rx_samples(unsigned int channel, rx_sample_handler *handler)
233   {
234     return d_impl->rx_samples(channel, handler);
235   }
236
237   bool
238   usrp2::stop_rx_streaming(unsigned int channel)
239   {
240     return d_impl->stop_rx_streaming(channel);
241   }
242
243   unsigned int
244   usrp2::rx_overruns()
245   {
246     return d_impl->rx_overruns();
247   }
248   
249   unsigned int
250   usrp2::rx_missing()
251   {
252     return d_impl->rx_missing();
253   }
254
255   // Transmit
256
257   bool 
258   usrp2::set_tx_gain(double gain)
259   {
260     return d_impl->set_tx_gain(gain);
261   }
262   
263   double
264   usrp2::tx_gain_min()
265   {
266     return d_impl->tx_gain_min();
267   }
268
269   double
270   usrp2::tx_gain_max()
271   {
272     return d_impl->tx_gain_max();
273   }
274
275   double
276   usrp2::tx_gain_db_per_step()
277   {
278     return d_impl->tx_gain_db_per_step();
279   }
280
281   bool
282   usrp2::set_tx_center_freq(double frequency, tune_result *result)
283   {
284     return d_impl->set_tx_center_freq(frequency, result);
285   }
286   
287   double
288   usrp2::tx_freq_min()
289   {
290     return d_impl->tx_freq_min();
291   }
292
293   double
294   usrp2::tx_freq_max()
295   {
296     return d_impl->tx_freq_max();
297   }
298
299
300   bool
301   usrp2::set_tx_interp(int interpolation_factor)
302   {
303     return d_impl->set_tx_interp(interpolation_factor);
304   }
305   
306   int
307   usrp2::tx_interp()
308   {
309     return d_impl->tx_interp();
310   }
311
312   void
313   usrp2::default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q)
314   {
315     d_impl->default_tx_scale_iq(interpolation_factor, scale_i, scale_q);
316   }
317
318   bool
319   usrp2::set_tx_scale_iq(int scale_i, int scale_q)
320   {
321     return d_impl->set_tx_scale_iq(scale_i, scale_q);
322   }
323   
324   bool
325   usrp2::tx_32fc(unsigned int channel,
326                  const std::complex<float> *samples,
327                  size_t nsamples,
328                  const tx_metadata *metadata)
329   {
330     return d_impl->tx_32fc(channel, samples, nsamples, metadata);
331   }
332
333   bool
334   usrp2::tx_16sc(unsigned int channel,
335                  const std::complex<int16_t> *samples,
336                  size_t nsamples,
337                  const tx_metadata *metadata)
338   {
339     return d_impl->tx_16sc(channel, samples, nsamples, metadata);
340   }
341
342   bool
343   usrp2::tx_raw(unsigned int channel,
344                 const uint32_t *items,
345                 size_t nitems,
346                 const tx_metadata *metadata)
347   {
348     return d_impl->tx_raw(channel, items, nitems, metadata);
349   }
350
351   // miscellaneous methods
352
353   bool
354   usrp2::config_mimo(int flags)
355   {
356     return d_impl->config_mimo(flags);
357   }
358
359   bool
360   usrp2::fpga_master_clock_freq(long *freq)
361   {
362     return d_impl->fpga_master_clock_freq(freq);
363   }
364
365   bool
366   usrp2::adc_rate(long *rate)
367   {
368     return d_impl->adc_rate(rate);
369   }
370
371   bool
372   usrp2::dac_rate(long *rate)
373   {
374     return d_impl->dac_rate(rate);
375   }
376
377   bool
378   usrp2::tx_daughterboard_id(int *dbid)
379   {
380     return d_impl->tx_daughterboard_id(dbid);
381   }
382
383   bool
384   usrp2::rx_daughterboard_id(int *dbid)
385   {
386     return d_impl->rx_daughterboard_id(dbid);
387   }
388   
389
390   // low level methods
391
392   bool
393   usrp2::burn_mac_addr(const std::string &new_addr)
394   {
395     return d_impl->burn_mac_addr(new_addr);
396   }
397
398   bool
399   usrp2::sync_to_pps()
400   {
401     return d_impl->sync_to_pps();
402   }
403
404   std::vector<uint32_t>
405   usrp2::peek32(uint32_t addr, uint32_t words)
406   {
407     return d_impl->peek32(addr, words);
408   }
409
410   bool
411   usrp2::poke32(uint32_t addr, const std::vector<uint32_t> &data)
412   {
413     return d_impl->poke32(addr, data);
414   }
415
416 } // namespace usrp2
417
418
419 std::ostream& operator<<(std::ostream &os, const usrp2::props &x)
420 {
421   os << x.addr;
422
423   char buf[128];
424   snprintf(buf, sizeof(buf)," hw_rev = 0x%04x", x.hw_rev);
425
426   os << buf;
427   return os;
428 }