3 * Copyright 2009 Free Software Foundation, Inc.
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.
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.
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/>.
22 #include <memory_map.h>
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))
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)
40 #define DACFULLSCALE 3.3
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);
46 struct db_dbsrx_common {
61 struct db_dbsrx_dummy {
63 struct db_dbsrx_common common;
68 struct db_dbsrx_common common;
71 struct db_dbsrx db_dbsrx = {
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,
92 //.base.atr_tx_delay =
93 //.base.atr_rx_delay =
106 .base.set_antenna = 0,
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
118 /**************************************************
120 **************************************************/
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);
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);
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));
145 static void _send_reg_1(struct db_dbsrx_dummy *db){
146 _write_reg(1,db->common.d_n & 255);
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));
153 static void _send_reg_3(struct db_dbsrx_dummy *db){
154 _write_reg(3,db->common.d_fdac);
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));
161 static void _send_reg_5(struct db_dbsrx_dummy *db){
162 _write_reg(5,db->common.d_gc2 + (db->common.d_diag<<5));
165 /**************************************************
166 * Helpers for setting the freq
167 **************************************************/
169 _set_div2(struct db_dbsrx_dummy *db, int div2){
170 db->common.d_div2 = div2;
174 // FIXME How do we handle ADE and ADL properly?
176 _set_ade(struct db_dbsrx_dummy *db, int ade){
177 db->common.d_ade = ade;
182 _set_r(struct db_dbsrx_dummy *db, int r){
183 db->common.d_r_reg = r;
188 _set_n(struct db_dbsrx_dummy *db, int n){
195 _set_osc(struct db_dbsrx_dummy *db, int osc){
196 db->common.d_osc = osc;
201 _set_cp(struct db_dbsrx_dummy *db, int cp){
202 db->common.d_cp = cp;
206 /**************************************************
208 **************************************************/
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;
215 if(!(freq>=db->base.freq_min && freq<=db->base.freq_max)) {
219 u2_fxpt_freq_t vcofreq;
220 if(freq < U2_DOUBLE_TO_FXPT_FREQ(1150e6)) {
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()
233 u2_fxpt_freq_t best_delta = U2_DOUBLE_TO_FXPT_FREQ(10e6);
234 u2_fxpt_freq_t delta;
237 while ((r_reg<7) && ((2<<r_reg) < rmin)) {
240 //printf ("r_reg = %d, r = %d\n",r_reg,2<<r_reg);
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));
249 //printf("N: %d\n",n);
254 delta = abs(n*REFCLK_FREQ/(2<<r_reg) - freq);
255 if(delta < best_delta) {
260 if(best_delta < U2_DOUBLE_TO_FXPT_FREQ(75e3)) {
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);
272 if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(2433e6))
274 else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(2711e6))
276 else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3025e6))
278 else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3341e6))
280 else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(3727e6))
282 else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(4143e6))
284 else if(vcofreq < U2_DOUBLE_TO_FXPT_FREQ(4493e6))
288 //printf("Initial VCO choice %d\n",vco);
293 while(adc_val == 0 || adc_val == 7) {
294 adc_val = _read_adc();
295 //printf("adc %d\n",adc_val);
305 else if(adc_val == 7) {
316 if(adc_val == 1 || adc_val == 2) {
319 else if(adc_val == 3 || adc_val == 4) {
325 //printf("Final VCO choice %d\n",vco);
327 *dc = db->common.d_n * REFCLK_FREQ / (2<<db->common.d_r_reg);
332 /**************************************************
333 * Helpers for setting the gain
334 **************************************************/
337 _set_gc2(struct db_dbsrx_dummy *db, int gc2){
338 db->common.d_gc2 = gc2;
342 /**************************************************
344 **************************************************/
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;
349 u2_fxpt_gain_t rfgain, bbgain;
351 if(!(gain >= db->base.gain_min && gain <= db->base.gain_max)) {
355 if(gain < U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX)) {
360 rfgain = U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX);
361 bbgain = gain - U2_DOUBLE_TO_FXPT_GAIN(RFGAINMAX);
364 int rf_gain_slope_q8 = 256 * 4096 * (VMAXGAIN-VMINGAIN) / RFGAINMAX / DACFULLSCALE;
365 int rf_gain_offset_q8 = 128 * 256 * 4096 * VMINGAIN / DACFULLSCALE;
367 int rfdac = (rfgain*rf_gain_slope_q8 + rf_gain_offset_q8)>>15;
369 //printf("Set RF Gain %d, %d\n",rfgain,rfdac);
370 lsdac_write_rx(1,rfdac);
373 int bb_gain_slope_q8 = 256*(0-31)/(BBGAINMAX-0);
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);
383 /**************************************************
384 * Helpers for setting the bw
385 **************************************************/
387 _set_m(struct db_dbsrx_dummy *db, int m){
393 _set_fdac(struct db_dbsrx_dummy *db, int fdac){
394 db->common.d_fdac = fdac;