Deadly printfs turned off... Specified gain ranges for TV Rx and RFX boards.
[debian/gnuradio] / usrp2 / firmware / lib / db_tvrx.c
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 #include <i2c.h>
20 #include <lsdac.h>
21 #include <memory_map.h>
22 #include <db_base.h>
23 #include <hal_io.h>
24 #include <ad9510.h>
25 #include <stdio.h>
26 #include <mdelay.h>
27
28 bool tvrx_init(struct db_base *db);
29 bool tvrx_set_freq(struct db_base *db, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc);
30 bool tvrx_set_gain(struct db_base *db, u2_fxpt_gain_t gain);
31
32 #define I2C_ADDR 0x60
33 #define ref_freq (U2_DOUBLE_TO_FXPT_FREQ(4e6)/640*8)
34
35 #define ref_div 640  /* choices are 640, 512, 1024 */
36
37 #if (ref_div == 640)
38 #define ref_div_byte 0
39 #else
40 #if (ref_div == 512)
41 #define ref_div_byte 0x6
42 #else
43 #define ref_div_byte 0x2
44 #endif
45 #endif
46
47 #define fast_tuning 0x40
48
49 #define control_byte_1 (0x88|fast_tuning|ref_div_byte)
50
51
52 struct db_tvrx_common {
53   // TVRX common stuff
54   u2_fxpt_freq_t first_if;
55   u2_fxpt_freq_t second_if;
56 };
57
58 struct db_tvrx_dummy {
59   struct db_base        base;
60   struct db_tvrx_common common;
61 };
62
63 struct db_tvrx1 {
64   struct db_base        base;
65   struct db_tvrx_common common;
66 };
67
68 struct db_tvrx2 {
69   struct db_base        base;
70   struct db_tvrx_common common;
71 };
72
73 struct db_tvrx3 {
74   struct db_base        base;
75   struct db_tvrx_common common;
76 };
77
78 /* The class instances */
79 struct db_tvrx1 db_tvrx1 = {
80   .base.dbid = 0x0003,
81   .base.is_tx = false,
82   .base.output_enables = 0x0000,
83   .base.used_pins = 0x0000,
84   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(50e6),
85   .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(860e6),
86   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
87   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(95),
88   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(1),
89   .base.is_quadrature = false,
90   .base.i_and_q_swapped = false,
91   .base.spectrum_inverted = false,
92   //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
93   .base.init = tvrx_init,
94   .base.set_freq = tvrx_set_freq,
95   .base.set_gain = tvrx_set_gain,
96   .base.set_tx_enable = 0,
97   .base.atr_mask = 0x0000,
98   .base.atr_txval = 0,
99   .base.atr_rxval = 0,
100   // .base.atr_tx_delay =
101   // .base.atr_rx_delay =
102   .common.first_if = U2_DOUBLE_TO_FXPT_FREQ(43.75e6),
103   .common.second_if = U2_DOUBLE_TO_FXPT_FREQ(5.75e6),
104 };
105
106 struct db_tvrx2 db_tvrx2 = {
107   .base.dbid = 0x000c,
108   .base.is_tx = false,
109   .base.output_enables = 0x0000,
110   .base.used_pins = 0x0000,
111   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(50e6),
112   .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(860e6),
113   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
114   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(95),
115   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(1),
116   .base.is_quadrature = false,
117   .base.i_and_q_swapped = false,
118   .base.spectrum_inverted = true,
119   //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
120   .base.init = tvrx_init,
121   .base.set_freq = tvrx_set_freq,
122   .base.set_gain = tvrx_set_gain,
123   .base.set_tx_enable = 0,
124   .base.atr_mask = 0x0000,
125   .base.atr_txval = 0,
126   .base.atr_rxval = 0,
127   // .base.atr_tx_delay =
128   // .base.atr_rx_delay =
129   .common.first_if = U2_DOUBLE_TO_FXPT_FREQ(44e6),
130   .common.second_if = U2_DOUBLE_TO_FXPT_FREQ(44e6),
131 };
132
133 struct db_tvrx3 db_tvrx3 = {
134   .base.dbid = 0x0040,
135   .base.is_tx = false,
136   .base.output_enables = 0x0000,
137   .base.used_pins = 0x0000,
138   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(50e6),
139   .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(860e6),
140   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
141   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(95),
142   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(1),
143   .base.is_quadrature = false,
144   .base.i_and_q_swapped = false,
145   .base.spectrum_inverted = true,
146   //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
147   .base.init = tvrx_init,
148   .base.set_freq = tvrx_set_freq,
149   .base.set_gain = tvrx_set_gain,
150   .base.set_tx_enable = 0,
151   .base.atr_mask = 0x0000,
152   .base.atr_txval = 0,
153   .base.atr_rxval = 0,
154   // .base.atr_tx_delay =
155   // .base.atr_rx_delay =
156   .common.first_if = U2_DOUBLE_TO_FXPT_FREQ(44e6),
157   .common.second_if = U2_DOUBLE_TO_FXPT_FREQ(44e6),
158 };
159
160 bool
161 tvrx_init(struct db_base *dbb)
162 {
163   struct db_tvrx_dummy *db = (struct db_tvrx_dummy *) dbb;
164   db->base.set_gain(dbb,U2_DOUBLE_TO_FXPT_GAIN(94.0));
165   return true;
166 }
167
168 bool
169 tvrx_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc)
170 {
171   *dc = 0;
172   if (freq < dbb->freq_min || freq > dbb->freq_max)
173     return false;
174
175   struct db_tvrx_dummy *db = (struct db_tvrx_dummy *) dbb;
176
177   u2_fxpt_freq_t target_lo_freq = freq + db->common.first_if;
178   int N_DIV = u2_fxpt_freq_round_to_int(((1LL<<20) * target_lo_freq)/ref_freq);
179   
180   u2_fxpt_freq_t actual_lo_freq = ref_freq * N_DIV;
181   u2_fxpt_freq_t actual_freq = actual_lo_freq - db->common.first_if;
182   if(N_DIV > 32767)
183     return false;
184
185   if (0)
186     printf("N_DIV = %d, actual_freq = %d, actual_lo_freq = %d\n",
187            N_DIV, u2_fxpt_freq_round_to_int(actual_freq),
188            u2_fxpt_freq_round_to_int(actual_lo_freq));
189
190   unsigned char buf[4];
191   buf[0] = (N_DIV>>8) & 0xff;
192   buf[1] = N_DIV & 0xff;
193   buf[2] = control_byte_1;
194   buf[3] = (freq < U2_DOUBLE_TO_FXPT_FREQ(158e6)) ? 0xa8 :  // VHF LOW
195     (freq < U2_DOUBLE_TO_FXPT_FREQ(464e6)) ? 0x98 :  // VHF HIGH
196     0x38;  // UHF
197
198   *dc = actual_freq - db->common.second_if;
199   return i2c_write(I2C_ADDR,buf,4);
200 }
201
202 bool
203 tvrx_set_gain(struct db_base *dbb, u2_fxpt_gain_t gain)
204 {
205   //struct db_tvrx_dummy *db = (struct db_tvrx_dummy *) dbb;
206   int rfgain;
207   int ifgain;
208   if(gain>U2_DOUBLE_TO_FXPT_GAIN(95.0))
209     return false;
210   if(gain<0)
211     return false;
212
213   if(gain>U2_DOUBLE_TO_FXPT_GAIN(60.0)) {
214     rfgain = U2_DOUBLE_TO_FXPT_GAIN(60.0);
215     ifgain = gain-U2_DOUBLE_TO_FXPT_GAIN(60.0);
216   } else {
217     rfgain = gain;
218     ifgain = 0;
219   }
220   
221   int rf_slope_q8 = 256 * 4096 * 2.5 / 60.0 / 1.22 / 3.3;
222   int rf_offset_q8 = 128 * 256 * 4096 * 1.25 / 1.22 / 3.3;
223   int if_slope_q8 = 256 * 4096 * 2.25 / 35.0 / 1.22 / 3.3;
224   int if_offset_q8 = 128 * 256 * 4096 * 1.4 / 1.22 / 3.3;
225
226   
227   int rfdac = (rfgain*rf_slope_q8 + rf_offset_q8)>>15;
228   int ifdac = (ifgain*if_slope_q8 + if_offset_q8)>>15;
229   lsdac_write_rx(0,rfdac);
230   lsdac_write_rx(1,ifdac);
231
232   if (0)
233     printf("Setting gain %d, rf %d, if %d\n",gain,rfdac,ifdac);
234
235   return true;
236 }
237
238
239 bool
240 tvrx_lock_detect(struct db_base *dbb)
241 {
242   // struct db_tvrx_dummy *db = (struct db_tvrx_dummy *) dbb;
243   return true;
244 }