support for unmodified dbsrx boards
[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 = 0x0002,
73   .base.is_tx = false,
74   .base.output_enables = 0x0001,
75   .base.used_pins = 0x0001,
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 };
107
108 bool
109 db_dbsrx_init(struct db_base *dbb){
110   struct db_dbsrx_dummy *db = (struct db_dbsrx_dummy *) dbb;
111   db->base.set_gain(dbb, (db->base.gain_max + db->base.gain_min)/2);
112   clocks_enable_rx_dboard(true, REFCLK_DIVISOR);  // Gives 4 MHz clock
113
114   hal_gpio_set_sel(GPIO_RX_BANK, 0, '1');
115
116
117   return true;
118 }
119
120 /**************************************************
121  * Registers
122  **************************************************/
123 static int
124 _read_adc (void){
125   unsigned char readback[2];
126   i2c_read(I2C_ADDR, readback, 2*sizeof(unsigned char));
127   int adc_val = (readback[0] >> 2)&7;
128   printf("READBACK[0] %d, [1] %d\n",readback[0],readback[1]);
129   printf("ADC: %d\n",adc_val);
130   return adc_val;
131 }
132
133 static void
134 _write_reg (int regno, int v){
135   //regno is in [0,5], v is value to write to register"""
136   unsigned char args[2];
137   args[0] = (unsigned char)regno;
138   args[1] = (unsigned char)v;
139   i2c_write(I2C_ADDR, args, 2*sizeof(unsigned char));
140   printf("Reg %d, Val %x\n",regno,v);
141 }
142
143 static void _send_reg_0(struct db_dbsrx_dummy *db){
144   _write_reg(0,(db->common.d_div2<<7) + (db->common.d_n>>8));
145 }
146
147 static void _send_reg_1(struct db_dbsrx_dummy *db){
148   _write_reg(1,db->common.d_n & 255);
149 }
150
151 static void _send_reg_2(struct db_dbsrx_dummy *db){
152   _write_reg(2,db->common.d_osc + (db->common.d_cp<<3) + (db->common.d_r_reg<<5));
153 }
154
155 static void _send_reg_3(struct db_dbsrx_dummy *db){
156   _write_reg(3,db->common.d_fdac);
157 }
158
159 static void _send_reg_4(struct db_dbsrx_dummy *db){
160   _write_reg(4,db->common.d_m + (db->common.d_dl<<5) + (db->common.d_ade<<6) + (db->common.d_adl<<7));
161 }
162
163 static void _send_reg_5(struct db_dbsrx_dummy *db){
164   _write_reg(5,db->common.d_gc2 + (db->common.d_diag<<5));
165 }
166
167 /**************************************************
168  * Helpers for setting the freq
169  **************************************************/
170 static void
171 _set_div2(struct db_dbsrx_dummy *db, int div2){
172   db->common.d_div2 = div2;
173   _send_reg_0(db);
174 }
175
176 // FIXME  How do we handle ADE and ADL properly?
177 static void
178 _set_ade(struct db_dbsrx_dummy *db, int ade){
179   db->common.d_ade = ade;
180   _send_reg_4(db);
181 }
182
183 static void
184 _set_r(struct db_dbsrx_dummy *db, int r){
185   db->common.d_r_reg = r;
186   _send_reg_2(db);
187 }
188
189 static void
190 _set_n(struct db_dbsrx_dummy *db, int n){
191   db->common.d_n = n;
192   _send_reg_0(db);
193   _send_reg_1(db);
194 }
195
196 static void
197 _set_osc(struct db_dbsrx_dummy *db, int osc){
198   db->common.d_osc = osc;
199   _send_reg_2(db);
200 }
201
202 static void
203 _set_cp(struct db_dbsrx_dummy *db, int cp){
204   db->common.d_cp = cp;
205   _send_reg_2(db);
206 }
207
208 /**************************************************
209  * Set the freq
210  **************************************************/
211
212
213 bool
214 db_dbsrx_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc){
215   struct db_dbsrx_dummy *db = (struct db_dbsrx_dummy *) dbb;
216
217   if(!(freq>=db->base.freq_min && freq<=db->base.freq_max)) {
218     return false;
219   }
220
221   u2_fxpt_freq_t vcofreq;
222   if(freq < U2_DOUBLE_TO_FXPT_FREQ(1150e6)) {
223     _set_div2(db, 0);
224     vcofreq = 4 * freq;
225   }
226   else {
227     _set_div2(db, 1);
228     vcofreq = 2 * freq;
229   }
230   
231   _set_ade(db, 1);
232   int rmin = max(2, u2_fxpt_freq_round_to_int(REFCLK_FREQ/2e6)); //TODO? remove max()
233   //int rmax = min(128, u2_fxpt_freq_round_to_int(REFCLK_FREQ/500e3)); //TODO? remove min()
234   int n = 0;
235   u2_fxpt_freq_t best_delta = U2_DOUBLE_TO_FXPT_FREQ(10e6);
236   u2_fxpt_freq_t delta;
237
238   int r_reg = 0;
239   while ((r_reg<7) && ((2<<r_reg) < rmin)) {
240     r_reg++;
241   }
242   printf ("r_reg = %d, r = %d\n",r_reg,2<<r_reg);
243   int best_r = r_reg;
244   int best_n = 0;
245
246   while(r_reg <= 7) {
247     n = u2_fxpt_freq_round_to_int(freq/REFCLK_FREQ_INT*(2<<r_reg));
248     //printf("LOOP: r_reg %d, best_r %d, best_n %d, best_delta %d\n",
249     //r_reg,best_r,best_n,u2_fxpt_freq_round_to_int(best_delta));
250
251     //printf("N: %d\n",n);
252     if(n<256) {
253       r_reg++;
254       continue;
255     }
256     delta = abs(n*REFCLK_FREQ/(2<<r_reg) - freq);
257     if(delta < best_delta) {
258       best_r = r_reg;
259       best_n = n;
260       best_delta = delta;
261     }
262     if(best_delta < U2_DOUBLE_TO_FXPT_FREQ(75e3)) {
263       break;
264     }
265     r_reg++;
266   }
267
268   printf("BEST R: %d  Best Delta %d  Best N %d\n",
269          best_r,u2_fxpt_freq_round_to_int(best_delta),best_n);
270   _set_r(db, best_r);
271   _set_n(db, best_n);
272  
273   int vco;
274   if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(2433e6))
275     vco = 0;
276   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(2711e6))
277     vco=1;
278   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3025e6))
279     vco=2;
280   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3341e6))
281     vco=3;
282   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3727e6))
283     vco=4;
284   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(4143e6))
285     vco=5;
286   else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(4493e6))
287     vco=6;
288   else
289     vco=7;
290   printf("Initial VCO choice %d\n",vco);  
291   _set_osc(db, vco);
292   
293
294   int adc_val = 0;
295   while(adc_val == 0 || adc_val == 7) {
296     adc_val = _read_adc();
297     printf("adc %d\n",adc_val);
298
299     if(adc_val == 0) {
300       if(vco <= 0) {
301         return false;
302       }
303       else {
304         vco = vco - 1;
305       }
306     }
307     else if(adc_val == 7) {
308       if(vco >= 7) {
309         return false;
310       }
311       else {
312         vco = vco + 1;
313       }
314     }
315     _set_osc(db, vco);
316   }
317   
318   if(adc_val == 1 || adc_val == 2) {
319     _set_cp(db, 1);
320   }
321   else if(adc_val == 3 || adc_val == 4) {
322     _set_cp(db, 2);
323   }
324   else {
325     _set_cp(db, 3);
326   }
327   printf("Final VCO choice %d\n",vco);  
328
329   *dc = db->common.d_n * REFCLK_FREQ / (2<<db->common.d_r_reg);
330   return true;
331  
332 }
333
334 /**************************************************
335  * Helpers for setting the gain
336  **************************************************/
337
338 static void
339 _set_gc2(struct db_dbsrx_dummy *db, int gc2){
340   db->common.d_gc2 = gc2;
341   _send_reg_5(db);
342 }
343
344 /**************************************************
345  * Set the gain
346  **************************************************/
347 bool
348 db_dbsrx_set_gain(struct db_base *dbb, u2_fxpt_gain_t gain){
349   struct db_dbsrx_dummy *db = (struct db_dbsrx_dummy *) dbb;
350   
351   u2_fxpt_gain_t rfgain, bbgain;
352
353   if(!(gain >= db->base.gain_min && gain <= db->base.gain_max)) {
354     return false;
355   }
356   
357   if(gain < U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX)) {
358     rfgain = gain;
359     bbgain = 0;
360   }
361   else {
362     rfgain = U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX);
363     bbgain = gain - U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX);
364   }
365
366   int rf_gain_slope_q8 = 256 * 4096 * (VMAXGAIN-VMINGAIN) / RFGAINMAX / DACFULLSCALE;
367   int rf_gain_offset_q8 = 128 * 256 * 4096 * VMINGAIN / DACFULLSCALE;
368   
369   int rfdac = (rfgain*rf_gain_slope_q8 + rf_gain_offset_q8)>>15;
370
371   //printf("Set RF Gain %d, %d\n",rfgain,rfdac);
372   lsdac_write_rx(1,rfdac);
373   
374   // Set GC2
375   int bb_gain_slope_q8 = 256*(0-31)/(BBGAINMAX-0);
376
377   int gc2 = u2_fxpt_gain_round_to_int((bb_gain_slope_q8 * bbgain)>>8) + 31;
378   //printf("Set BB Gain: %d, gc2 %d\n",bbgain,gc2);
379
380   _set_gc2(db, gc2);
381
382   return true;
383 }
384
385 /**************************************************
386  * Helpers for setting the bw
387  **************************************************/
388 static void
389 _set_m(struct db_dbsrx_dummy *db, int m){
390   db->common.d_m = m;
391   _send_reg_4(db);
392 }
393   
394 static void
395 _set_fdac(struct db_dbsrx_dummy *db, int fdac){
396   db->common.d_fdac = fdac;
397   _send_reg_3(db);
398 }