Merged VRT work-in-progress from eb/vrt2 (11518:11598) into trunk.
[debian/gnuradio] / gr-vrt / src / vrt_quadradio_source_32fc.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <vrt_quadradio_source_32fc.h>
25 #include <vrt/quadradio.h>
26 #include <vrt/rx_packet_handler.h>
27 #include <cstdio>
28
29 vrt_quadradio_source_32fc_sptr
30 vrt_make_quadradio_source_32fc(const std::string &ip, 
31                                size_t rx_bufsize,
32                                size_t samples_per_pkt)
33 {
34   return gnuradio::get_initial_sptr(new vrt_quadradio_source_32fc(ip,
35                                                                   rx_bufsize,
36                                                                   samples_per_pkt));
37 }
38
39 vrt_quadradio_source_32fc::vrt_quadradio_source_32fc(const std::string &ip,
40                                                      size_t rx_bufsize,
41                                                      size_t samples_per_pkt)
42   : vrt_source_32fc("quadradio_source_32fc"),
43     d_samples_per_pkt(samples_per_pkt == 0 ? 800 : samples_per_pkt),
44     d_qr(vrt::quadradio::sptr(new vrt::quadradio(ip, rx_bufsize)))
45 {
46 }
47
48 vrt_quadradio_source_32fc::~vrt_quadradio_source_32fc()
49 {
50   d_qr->stop_streaming();
51 }
52
53 vrt::rx::sptr 
54 vrt_quadradio_source_32fc::vrt_rx() const
55 {
56   return d_qr->vrt_rx();
57 }
58
59 bool 
60 vrt_quadradio_source_32fc::start()
61 {
62   // throw away any stale packets before starting
63   vrt::rx_packet_handler nop;
64   vrt_rx()->rx_packets(&nop, true);
65   d_checker.resync();
66
67   return d_qr->start_streaming(d_samples_per_pkt);
68 }  
69
70 bool
71 vrt_quadradio_source_32fc::stop()
72 {
73   return d_qr->stop_streaming();
74 }
75
76 bool 
77 vrt_quadradio_source_32fc::set_dboard_pins(int which_dboard, int v)
78 {
79   return d_qr->set_dboard_pins(which_dboard, v);
80 }
81
82 bool 
83 vrt_quadradio_source_32fc::set_center_freq(double target_freq)
84 {
85   return d_qr->set_center_freq(target_freq);
86 }
87
88 bool 
89 vrt_quadradio_source_32fc::set_band_select(const std::string &band)
90 {
91   return d_qr->set_band_select(band);
92 }
93
94 //void 
95 //vrt_quadradio_source_32fc::set_10dB_atten(bool on)
96 //{
97 //  return d_qr->set_10dB_atten(on);
98 //}
99
100 bool 
101 vrt_quadradio_source_32fc::select_rx_antenna(const std::string &ant)
102 {
103   return d_qr->select_rx_antenna(ant);
104 }
105
106 bool 
107 vrt_quadradio_source_32fc::set_attenuation0(int attenuation)
108 {
109   return d_qr->set_attenuation0(attenuation);
110 }
111
112 bool 
113 vrt_quadradio_source_32fc::set_attenuation1(int attenuation)
114 {
115   return d_qr->set_attenuation1(attenuation);
116 }
117
118 void
119 vrt_quadradio_source_32fc::set_adc_gain(bool on){
120   d_qr->set_adc_gain(on);
121 }
122
123 void
124 vrt_quadradio_source_32fc::set_dc_offset_comp(bool on){
125   d_qr->set_dc_offset_comp(on);
126 }
127
128 void
129 vrt_quadradio_source_32fc::set_digital_gain(float gain){
130   d_qr->set_digital_gain(gain);
131 }
132
133 void
134 vrt_quadradio_source_32fc::set_test_signal(int type){
135   d_qr->set_test_signal(static_cast<vrt_test_sig_t>(type));
136 }
137
138 bool
139 vrt_quadradio_source_32fc::set_setting_reg(int regno, int value)
140 {
141   return d_qr->set_setting_reg(regno, value);
142 }
143
144 bool
145 vrt_quadradio_source_32fc::set_hsadc_conf(int which_dboard, int regno, int value)
146 {
147   return d_qr->set_hsadc_conf(which_dboard, regno, value);
148 }
149
150 bool
151 vrt_quadradio_source_32fc::set_lsdac(int which_dboard, int which_dac, int value)
152 {
153   return d_qr->set_lsdac(which_dboard, which_dac, value);
154 }
155
156 bool
157 vrt_quadradio_source_32fc::set_mem32(int addr, int value)
158 {
159   return d_qr->set_mem32(addr, value);
160 }
161
162 bool
163 vrt_quadradio_source_32fc::set_lo_freq(double freq)
164 {
165   return d_qr->set_lo_freq(freq);
166 }
167
168 bool
169 vrt_quadradio_source_32fc::set_cal_freq(double freq)
170 {
171   return d_qr->set_cal_freq(freq);
172 }
173
174 /*--------------------------------------------------------------------*/
175 #define IQ_IMBAL_NUM_TAPS 30
176 #define IQ_IMBAL_REG_NO 162
177
178 //helper function to set the iq imbalance register with a tap
179 static int get_iq_imbal_reg(bool real, bool init, float tap){
180   int val = int(round(tap));
181   val &= 0x1ffffff; //lower 25 bits for tap
182   val |= (real?0:1) << 30; //30th bit for filter type
183   val |= (init?1:0) << 31; //31st bit for initialization
184   printf("Reg %d Val %x\n", IQ_IMBAL_REG_NO, val);
185   return val;
186 }
187
188 void
189 vrt_quadradio_source_32fc::set_iq_imbal_taps(const std::vector<gr_complex> taps){
190   int i = 0;
191   /* set the real part of the taps */
192   get_iq_imbal_reg(true, true, 0);
193   for (i = 0; i < IQ_IMBAL_NUM_TAPS; i++){
194       set_setting_reg(IQ_IMBAL_REG_NO, get_iq_imbal_reg(true, false, taps[IQ_IMBAL_NUM_TAPS-i-1].real()));
195   }
196   get_iq_imbal_reg(false, true, 0);
197   /* set the imaginary part of the taps */
198   for (i = 0; i < IQ_IMBAL_NUM_TAPS; i++){
199       set_setting_reg(IQ_IMBAL_REG_NO, get_iq_imbal_reg(false, false, taps[IQ_IMBAL_NUM_TAPS-i-1].imag()));
200   }
201 }
202
203 bool
204 vrt_quadradio_source_32fc::set_beamforming(std::vector<gr_complex> gains){
205   int32_t gains_ints[8];
206   for (int i = 0; i < 4; i++){
207     gains_ints[2*i] = int32_t(gains[i].real());
208     gains_ints[2*i+1] = int32_t(gains[i].imag());
209   }
210  return d_qr->set_beamforming(gains_ints);
211 }
212