3769f912482cd4b4824d873ed04dbe47c7ff8160
[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(int band)
90 {
91   return d_qr->set_band_select(static_cast<vrt_band_sel_t>(band));
92 }
93
94 int 
95 vrt_quadradio_source_32fc::get_band_select(void)
96 {
97   return static_cast<int>(d_qr->get_band_select());
98 }
99
100 //void 
101 //vrt_quadradio_source_32fc::set_10dB_atten(bool on)
102 //{
103 //  return d_qr->set_10dB_atten(on);
104 //}
105
106 bool 
107 vrt_quadradio_source_32fc::select_rx_antenna(const std::string &ant)
108 {
109   return d_qr->select_rx_antenna(ant);
110 }
111
112 bool 
113 vrt_quadradio_source_32fc::set_attenuation0(int attenuation)
114 {
115   return d_qr->set_attenuation0(attenuation);
116 }
117
118 bool 
119 vrt_quadradio_source_32fc::set_attenuation1(int attenuation)
120 {
121   return d_qr->set_attenuation1(attenuation);
122 }
123
124 void
125 vrt_quadradio_source_32fc::set_adc_gain(bool on){
126   d_qr->set_adc_gain(on);
127 }
128
129 void
130 vrt_quadradio_source_32fc::set_dc_offset_comp(bool on){
131   d_qr->set_dc_offset_comp(on);
132 }
133
134 void
135 vrt_quadradio_source_32fc::set_digital_gain(float gain){
136   d_qr->set_digital_gain(gain);
137 }
138
139 void
140 vrt_quadradio_source_32fc::set_test_signal(int type){
141   d_qr->set_test_signal(static_cast<vrt_test_sig_t>(type));
142 }
143
144 bool
145 vrt_quadradio_source_32fc::set_setting_reg(int regno, int value)
146 {
147   return d_qr->set_setting_reg(regno, value);
148 }
149
150 bool
151 vrt_quadradio_source_32fc::set_hsadc_conf(int which_dboard, int regno, int value)
152 {
153   return d_qr->set_hsadc_conf(which_dboard, regno, value);
154 }
155
156 bool
157 vrt_quadradio_source_32fc::set_lsdac(int which_dboard, int which_dac, int value)
158 {
159   return d_qr->set_lsdac(which_dboard, which_dac, value);
160 }
161
162 bool
163 vrt_quadradio_source_32fc::set_mem32(int addr, int value)
164 {
165   return d_qr->set_mem32(addr, value);
166 }
167
168 bool
169 vrt_quadradio_source_32fc::set_lo_freq(double freq)
170 {
171   return d_qr->set_lo_freq(freq);
172 }
173
174 bool
175 vrt_quadradio_source_32fc::set_cal_freq(double freq)
176 {
177   return d_qr->set_cal_freq(freq);
178 }
179
180 /*--------------------------------------------------------------------*/
181 #define IQ_IMBAL_NUM_TAPS 30
182 #define IQ_IMBAL_REG_NO 162
183
184 //helper function to set the iq imbalance register with a tap
185 static int get_iq_imbal_reg(bool real, bool init, float tap){
186   int val = int(round(tap));
187   val &= 0x1ffffff; //lower 25 bits for tap
188   val |= (real?0:1) << 30; //30th bit for filter type
189   val |= (init?1:0) << 31; //31st bit for initialization
190   printf("Reg %d Val %x\n", IQ_IMBAL_REG_NO, val);
191   return val;
192 }
193
194 void
195 vrt_quadradio_source_32fc::set_iq_imbal_taps(const std::vector<gr_complex> taps){
196   int i = 0;
197   /* set the real part of the taps */
198   get_iq_imbal_reg(true, true, 0);
199   for (i = 0; i < IQ_IMBAL_NUM_TAPS; i++){
200       set_setting_reg(IQ_IMBAL_REG_NO, get_iq_imbal_reg(true, false, taps[IQ_IMBAL_NUM_TAPS-i-1].real()));
201   }
202   get_iq_imbal_reg(false, true, 0);
203   /* set the imaginary part of the taps */
204   for (i = 0; i < IQ_IMBAL_NUM_TAPS; i++){
205       set_setting_reg(IQ_IMBAL_REG_NO, get_iq_imbal_reg(false, false, taps[IQ_IMBAL_NUM_TAPS-i-1].imag()));
206   }
207 }
208
209 bool
210 vrt_quadradio_source_32fc::set_beamforming(std::vector<gr_complex> gains){
211   int32_t gains_ints[8];
212   for (int i = 0; i < 4; i++){
213     gains_ints[2*i] = int32_t(gains[i].real());
214     gains_ints[2*i+1] = int32_t(gains[i].imag());
215   }
216  return d_qr->set_beamforming(gains_ints);
217 }
218