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