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