switch source package format to 3.0 quilt
[debian/gnuradio] / usrp2 / firmware / lib / db_dbsrx.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 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 <db_base.h>
21 #include <lsdac.h>
22 #include <memory_map.h>
23 #include <clocks.h>
24 #include <stdio.h>
25 #include <hal_io.h>
26
27 #define min(X,Y) ((X) < (Y) ? (X) : (Y))
28 #define max(X,Y) ((X) > (Y) ? (X) : (Y))
29 #define abs(X) ((X) < (0) ? ((-1)*(X)) : (X))
30
31 #define I2C_ADDR 0x67
32 #define REFCLK_DIVISOR 25   // Gives a 4 MHz clock
33 #define REFCLK_FREQ U2_DOUBLE_TO_FXPT_FREQ(MASTER_CLK_RATE/REFCLK_DIVISOR)
34 #define REFCLK_FREQ_INT u2_fxpt_freq_round_to_int(REFCLK_FREQ)
35
36 #define VMAXGAIN .75
37 #define VMINGAIN 2.6
38 #define RFGAINMAX 60
39 #define BBGAINMAX 24
40 #define DACFULLSCALE 3.3
41
42 bool db_dbsrx_init(struct db_base *db);
43 bool db_dbsrx_set_freq(struct db_base *db, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc);
44 bool db_dbsrx_set_gain(struct db_base *db, u2_fxpt_gain_t gain);
45
46 struct db_dbsrx_common {
47   int d_n;
48   int d_div2;
49   int d_osc;
50   int d_cp;
51   int d_r_reg;
52   int d_fdac;
53   int d_m;
54   int d_dl;
55   int d_ade;
56   int d_adl;
57   int d_gc2;
58   int d_diag;
59 };
60
61 struct db_dbsrx_dummy {
62   struct db_base base;
63   struct db_dbsrx_common common;
64 };
65
66 struct db_dbsrx {
67   struct db_base base;
68   struct db_dbsrx_common common;
69 };
70
71 struct db_dbsrx db_dbsrx = {
72   .base.dbid = 0x000d,
73   .base.is_tx = false,
74   .base.output_enables = 0x0000,
75   .base.used_pins = 0x0000,
76   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(500e6),
77   .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2.6e9),
78   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
79   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX+BBGAINMAX),
80   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(1),
81   .base.is_quadrature = true,
82   .base.i_and_q_swapped = false,
83   .base.spectrum_inverted = false,
84   .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
85   .base.init = db_dbsrx_init,
86   .base.set_freq = db_dbsrx_set_freq,
87   .base.set_gain = db_dbsrx_set_gain,
88   .base.set_tx_enable = 0,
89   .base.atr_mask = 0x0000,
90   .base.atr_txval = 0,
91   .base.atr_rxval = 0,
92   //.base.atr_tx_delay =
93   //.base.atr_rx_delay =
94   .common.d_n = 950,
95   .common.d_div2 = 0,
96   .common.d_osc = 5,
97   .common.d_cp = 3,
98   .common.d_r_reg = 1,
99   .common.d_fdac = 127,
100   .common.d_m = 2,
101   .common.d_dl = 1,
102   .common.d_ade = 0,
103   .common.d_adl = 0,
104   .common.d_gc2 = 31,
105   .common.d_diag = 0,
106   .base.set_antenna = 0,
107 };
108
109 bool
110 db_dbsrx_init(struct db_base *dbb){
111   struct db_dbsrx_dummy *db = (struct db_dbsrx_dummy *) dbb;
112   db->base.set_gain(dbb, (db->base.gain_max + db->base.gain_min)/2);
113   clocks_enable_rx_dboard(true, REFCLK_DIVISOR);  // Gives 4 MHz clock
114
115   return true;
116 }
117
118 /**************************************************
119  * Registers
120  **************************************************/
121 static int
122 _read_adc (void){
123   unsigned char readback[2];
124   i2c_read(I2C_ADDR, readback, 2*sizeof(unsigned char));
125   int adc_val = (readback[0] >> 2)&7;
126   //printf("READBACK[0] %d, [1] %d\n",readback[0],readback[1]);
127   //printf("ADC: %d\n",adc_val);
128   return adc_val;
129 }
130
131 static void
132 _write_reg (int regno, int v){
133   //regno is in [0,5], v is value to write to register"""
134   unsigned char args[2];
135   args[0] = (unsigned char)regno;
136   args[1] = (unsigned char)v;
137   i2c_write(I2C_ADDR, args, 2*sizeof(unsigned char));
138   //printf("Reg %d, Val %x\n",regno,v);
139 }
140
141 static void _send_reg_0(struct db_dbsrx_dummy *db){
142   _write_reg(0,(db->common.d_div2<<7) + (db->common.d_n>>8));
143 }
144
145 static void _send_reg_1(struct db_dbsrx_dummy *db){
146   _write_reg(1,db->common.d_n & 255);
147 }
148
149 static void _send_reg_2(struct db_dbsrx_dummy *db){
150   _write_reg(2,db->common.d_osc + (db->common.d_cp<<3) + (db->common.d_r_reg<<5));
151 }
152
153 static void _send_reg_3(struct db_dbsrx_dummy *db){
154   _write_reg(3,db->common.d_fdac);
155 }
156
157 static void _send_reg_4(struct db_dbsrx_dummy *db){
158   _write_reg(4,db->common.d_m + (db->common.d_dl<<5) + (db->common.d_ade<<6) + (db->common.d_adl<<7));
159 }
160
161 static void _send_reg_5(struct db_dbsrx_dummy *db){
162   _write_reg(5,db->common.d_gc2 + (db->common.d_diag<<5));
163 }
164
165 /**************************************************
166  * Helpers for setting the freq
167  **************************************************/
168 static void
169 _set_div2(struct db_dbsrx_dummy *db, int div2){
170   db->common.d_div2 = div2;
171   _send_reg_0(db);
172 }
173
174 // FIXME  How do we handle ADE and ADL properly?
175 static void
176 _set_ade(struct db_dbsrx_dummy *db, int ade){
177   db->common.d_ade = ade;
178   _send_reg_4(db);
179 }
180
181 static void
182 _set_r(struct db_dbsrx_dummy *db, int r){
183   db->common.d_r_reg = r;
184   _send_reg_2(db);
185 }
186
187 static void
188 _set_n(struct db_dbsrx_dummy *db, int n){
189   db->common.d_n = n;
190   _send_reg_0(db);
191   _send_reg_1(db);
192 }
193
194 static void
195 _set_osc(struct db_dbsrx_dummy *db, int osc){
196   db->common.d_osc = osc;
197   _send_reg_2(db);
198 }
199
200 static void
201 _set_cp(struct db_dbsrx_dummy *db, int cp){
202   db->common.d_cp = cp;
203   _send_reg_2(db);
204 }
205
206 /**************************************************
207  * Set the freq
208  **************************************************/
209
210
211 bool
212 db_dbsrx_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc){
213   struct db_dbsrx_dummy *db = (struct db_dbsrx_dummy *) dbb;
214
215   if(!(freq>=db->base.freq_min && freq<=db->base.freq_max)) {
216     return false;
217   }
218
219   u2_fxpt_freq_t vcofreq;
220   if(freq < U2_DOUBLE_TO_FXPT_FREQ(1150e6)) {
221     _set_div2(db, 0);
222     vcofreq = 4 * freq;
223   }
224   else {
225     _set_div2(db, 1);
226     vcofreq = 2 * freq;
227   }
228   
229   _set_ade(db, 1);
230   int rmin = max(2, u2_fxpt_freq_round_to_int(REFCLK_FREQ/2e6)); //TODO? remove max()
231   //int rmax = min(128, u2_fxpt_freq_round_to_int(REFCLK_FREQ/500e3)); //TODO? remove min()
232   int n = 0;
233   u2_fxpt_freq_t best_delta = U2_DOUBLE_TO_FXPT_FREQ(10e6);
234   u2_fxpt_freq_t delta;
235
236   int r_reg = 0;
237   while ((r_reg<7) && ((2<<r_reg) < rmin)) {
238     r_reg++;
239   }
240   //printf ("r_reg = %d, r = %d\n",r_reg,2<<r_reg);
241   int best_r = r_reg;
242   int best_n = 0;
243
244   while(r_reg <= 7) {
245     n = u2_fxpt_freq_round_to_int(freq/REFCLK_FREQ_INT*(2<<r_reg));
246     //printf("LOOP: r_reg %d, best_r %d, best_n %d, best_delta %d\n",
247     //r_reg,best_r,best_n,u2_fxpt_freq_round_to_int(best_delta));
248
249     //printf("N: %d\n",n);
250     if(n<256) {
251       r_reg++;
252       continue;
253     }
254     delta = abs(n*REFCLK_FREQ/(2<<r_reg) - freq);
255     if(delta < best_delta) {
256       best_r = r_reg;
257       best_n = n;
258       best_delta = delta;
259     }
260     if(best_delta < U2_DOUBLE_TO_FXPT_FREQ(75e3)) {
261       break;
262     }
263     r_reg++;
264   }
265
266   //printf("BEST R: %d  Best Delta %d  Best N %d\n",
267   // best_r,u2_fxpt_freq_round_to_int(best_delta),best_n);
268   _set_r(db, best_r);
269   _set_n(db, best_n);
270  
271   int vco;
272   if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(2433e6))
273     vco = 0;
274   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(2711e6))
275     vco=1;
276   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3025e6))
277     vco=2;
278   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3341e6))
279     vco=3;
280   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3727e6))
281     vco=4;
282   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(4143e6))
283     vco=5;
284   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(4493e6))
285     vco=6;
286   else
287     vco=7;
288   //printf("Initial VCO choice %d\n",vco);  
289   _set_osc(db, vco);
290   
291
292   int adc_val = 0;
293   while(adc_val == 0 || adc_val == 7) {
294     adc_val = _read_adc();
295     //printf("adc %d\n",adc_val);
296
297     if(adc_val == 0) {
298       if(vco <= 0) {
299         return false;
300       }
301       else {
302         vco = vco - 1;
303       }
304     }
305     else if(adc_val == 7) {
306       if(vco >= 7) {
307         return false;
308       }
309       else {
310         vco = vco + 1;
311       }
312     }
313     _set_osc(db, vco);
314   }
315   
316   if(adc_val == 1 || adc_val == 2) {
317     _set_cp(db, 1);
318   }
319   else if(adc_val == 3 || adc_val == 4) {
320     _set_cp(db, 2);
321   }
322   else {
323     _set_cp(db, 3);
324   }
325   //printf("Final VCO choice %d\n",vco);  
326
327   *dc = db->common.d_n * REFCLK_FREQ / (2<<db->common.d_r_reg);
328   return true;
329  
330 }
331
332 /**************************************************
333  * Helpers for setting the gain
334  **************************************************/
335
336 static void
337 _set_gc2(struct db_dbsrx_dummy *db, int gc2){
338   db->common.d_gc2 = gc2;
339   _send_reg_5(db);
340 }
341
342 /**************************************************
343  * Set the gain
344  **************************************************/
345 bool
346 db_dbsrx_set_gain(struct db_base *dbb, u2_fxpt_gain_t gain){
347   struct db_dbsrx_dummy *db = (struct db_dbsrx_dummy *) dbb;
348   
349   u2_fxpt_gain_t rfgain, bbgain;
350
351   if(!(gain >= db->base.gain_min && gain <= db->base.gain_max)) {
352     return false;
353   }
354   
355   if(gain < U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX)) {
356     rfgain = gain;
357     bbgain = 0;
358   }
359   else {
360     rfgain = U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX);
361     bbgain = gain - U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX);
362   }
363
364   int rf_gain_slope_q8 = 256 * 4096 * (VMAXGAIN-VMINGAIN) / RFGAINMAX / DACFULLSCALE;
365   int rf_gain_offset_q8 = 128 * 256 * 4096 * VMINGAIN / DACFULLSCALE;
366   
367   int rfdac = (rfgain*rf_gain_slope_q8 + rf_gain_offset_q8)>>15;
368
369   //printf("Set RF Gain %d, %d\n",rfgain,rfdac);
370   lsdac_write_rx(1,rfdac);
371   
372   // Set GC2
373   int bb_gain_slope_q8 = 256*(0-31)/(BBGAINMAX-0);
374
375   int gc2 = u2_fxpt_gain_round_to_int((bb_gain_slope_q8 * bbgain)>>8) + 31;
376   //printf("Set BB Gain: %d, gc2 %d\n",bbgain,gc2);
377
378   _set_gc2(db, gc2);
379
380   return true;
381 }
382
383 /**************************************************
384  * Helpers for setting the bw
385  **************************************************/
386 static void
387 _set_m(struct db_dbsrx_dummy *db, int m){
388   db->common.d_m = m;
389   _send_reg_4(db);
390 }
391   
392 static void
393 _set_fdac(struct db_dbsrx_dummy *db, int fdac){
394   db->common.d_fdac = fdac;
395   _send_reg_3(db);
396 }