Fix WBX tuning to allow DDC use in reaching 50MHz
[debian/gnuradio] / usrp2 / firmware / lib / db_wbxng.c
1 /*
2  * Copyright 2010 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19
20 #include "db_wbxng.h"
21 #include "adf4350.h"
22 #include <spi.h>
23 #include <memory_map.h>
24 #include <db_base.h>
25 #include <hal_io.h>
26 #include <mdelay.h>
27 #include <lsdac.h>
28 #include <clocks.h>
29 #include <stdio.h>
30 #include <stdint.h>
31
32 #define min(X,Y) ((X) < (Y) ? (X) : (Y))
33 #define max(X,Y) ((X) > (Y) ? (X) : (Y))
34
35 bool wbxng_init_rx(struct db_base *dbb);
36 bool wbxng_init_tx(struct db_base *dbb);
37 bool wbxng_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc);
38 bool wbxng_set_gain_rx(struct db_base *dbb, u2_fxpt_gain_t gain);
39 bool wbxng_set_gain_tx(struct db_base *dbb, u2_fxpt_gain_t gain);
40 bool wbxng_set_tx_enable(struct db_base *dbb, bool on);
41
42
43 /*
44  * The class instances
45  */
46 struct db_wbxng_rx db_wbxng_rx = {
47   .base.dbid = 0x0053,
48   .base.is_tx = false,
49   .base.output_enables = RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5|PLL_CE|PLL_PDBRF|ATTN_MASK,
50   .base.used_pins = 0xFFFF,
51   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(68.75e6),
52   .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2200e6),
53   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
54   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(31.5),
55   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(0.5),
56   .base.is_quadrature = true,
57   .base.i_and_q_swapped = false,
58   .base.spectrum_inverted = false,
59   .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
60   .base.init = wbxng_init_rx,
61   .base.set_freq = wbxng_set_freq,
62   .base.set_gain = wbxng_set_gain_rx,
63   .base.set_tx_enable = 0,
64   .base.atr_mask = RXBB_EN | RX2_RX1N,
65   .base.atr_txval = RX2_RX1N,
66   .base.atr_rxval = RXBB_EN,
67   // .base.atr_tx_delay =
68   // .base.atr_rx_delay =
69   .base.set_antenna = 0,
70   .common.adf4350_regs_int = UINT16_C(100),
71   .common.adf4350_regs_frac = 0,
72   .common.adf4350_regs_prescaler = 1,
73   .common.adf4350_regs_mod = UINT16_C(0xfff),
74   .common.adf4350_regs_10_bit_r_counter = UINT16_C(1),
75   .common.adf4350_regs_divider_select = 0,
76   .common.adf4350_regs_8_bit_band_select_clock_divider_value = 0,
77   .common.spi_mask = SPI_SS_RX_DB,
78   .common.freq_mult = 2
79 };
80
81
82 struct db_wbxng_tx db_wbxng_tx = {
83   .base.dbid = 0x0052,
84   .base.is_tx = true,
85   .base.output_enables = RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5|PLL_CE|PLL_PDBRF,
86   .base.used_pins = 0xFFFF,
87   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(68.75e6),
88   .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2200e6),
89   .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
90   .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(25),
91   .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(0.1),
92   .base.is_quadrature = true,
93   .base.i_and_q_swapped = false,
94   .base.spectrum_inverted = false,
95   .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
96   .base.init = wbxng_init_tx,
97   .base.set_freq = wbxng_set_freq,
98   .base.set_gain = wbxng_set_gain_tx,
99   .base.set_tx_enable = wbxng_set_tx_enable,
100   .base.atr_mask = RX_TXN | TXMOD_EN,
101   .base.atr_txval = TXMOD_EN,
102   .base.atr_rxval = RX_TXN,
103   // .base.atr_tx_delay =
104   // .base.atr_rx_delay =
105   .base.set_antenna = 0,
106   .common.adf4350_regs_int = UINT16_C(100),
107   .common.adf4350_regs_frac = 0,
108   .common.adf4350_regs_prescaler = 1,
109   .common.adf4350_regs_mod = UINT16_C(0xfff),
110   .common.adf4350_regs_10_bit_r_counter = UINT16_C(1),
111   .common.adf4350_regs_divider_select = 0,
112   .common.adf4350_regs_8_bit_band_select_clock_divider_value = 0,
113   .common.spi_mask = SPI_SS_TX_DB,
114   .common.freq_mult = 2
115 };
116
117
118 bool
119 wbxng_init_tx(struct db_base *dbb)
120 {
121   //struct db_wbxng_dummy *db = (struct db_wbxng_dummy *) dbb;
122   clocks_enable_tx_dboard(true, 0);
123   hal_gpio_write( GPIO_TX_BANK, ENABLE_5|ENABLE_33, ENABLE_5|ENABLE_33 );
124
125   adf4350_init(dbb);
126
127   // Set the freq now to get the one time 10ms delay out of the way.
128   u2_fxpt_freq_t        dc;
129   dbb->set_freq(dbb, dbb->freq_min, &dc);
130   return true;
131 }
132
133 bool
134 wbxng_init_rx(struct db_base *dbb)
135 {
136   //struct db_wbxng_dummy *db = (struct db_wbxng_dummy *) dbb;
137   clocks_enable_rx_dboard(true, 0);
138   hal_gpio_write( GPIO_RX_BANK, ENABLE_5|ENABLE_33, ENABLE_5|ENABLE_33 );
139
140   adf4350_init(dbb);
141
142   // test gain
143   dbb->set_gain(dbb,U2_DOUBLE_TO_FXPT_GAIN(20.0));
144
145   // Set the freq now to get the one time 10ms delay out of the way.
146   u2_fxpt_freq_t        dc;
147   dbb->set_freq(dbb, dbb->freq_min, &dc);
148
149   return true;
150 }
151
152 bool
153 wbxng_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc)
154 {
155   // clamp freq
156   u2_fxpt_freq_t clamp_freq = max(dbb->freq_min, min(freq, dbb->freq_max));
157   //printf("Requested LO freq = %u", (uint32_t) ((clamp_freq >> U2_FPF_RP)/1000));
158   bool ok = adf4350_set_freq(clamp_freq<<1,dbb);
159   *dc = adf4350_get_freq(dbb)>>1;
160
161   return ok;
162 }
163
164 bool
165 wbxng_set_gain_tx(struct db_base *dbb, u2_fxpt_gain_t gain)
166 {
167   struct db_wbxng_dummy *db = (struct db_wbxng_dummy *) dbb;
168
169   // clamp gain
170   //gain = max(db->gain_min, min(gain, db->gain_max));
171
172   int offset_q8 = (int)(1.4/3.3*4096*(1<<15));
173   int range_q15 = (int)(-0.9*4096/3.3*256*128);
174   int slope_q8 = range_q15/db->base.gain_max;
175
176   int dacword = ((slope_q8 * gain) + offset_q8)>>15;
177   //printf("DACWORD 0x%x\n",dacword);
178   lsdac_write_tx(0,dacword);
179   return true;
180 }
181
182 bool
183 wbxng_set_gain_rx(struct db_base *dbb, u2_fxpt_gain_t gain)
184 {
185   struct db_wbxng_dummy *db = (struct db_wbxng_dummy *) dbb;
186
187   // clamp gain
188   //gain = max(db->gain_min, min(gain, db->gain_max));
189
190   int iobits = (int) ((~((db->base.gain_max - gain) << 2)) & ATTN_MASK);
191   //printf("gain %d, gainmax %d, RX_ATTN_MASK = 0x%x, RX_ATTN_WORD = 0x%x\n", gain, db->base.gain_max, (int) (ATTN_MASK), (int) (iobits));
192
193   hal_gpio_write( GPIO_RX_BANK, (int) (iobits), ATTN_MASK );
194   return true;
195 }
196
197
198 bool
199 wbxng_set_tx_enable(struct db_base *dbb, bool on)
200 {
201   struct db_wbxng_dummy *db = (struct db_wbxng_dummy *) dbb;
202
203   // FIXME
204
205   return false;
206 }
207
208 bool
209 wbxng_lock_detect(struct db_base *dbb)
210 {
211   struct db_wbxng_dummy *db = (struct db_wbxng_dummy *) dbb;
212
213   int pins;
214   pins = hal_gpio_read( db->base.is_tx ? GPIO_TX_BANK : GPIO_RX_BANK );
215   if(pins & PLL_LOCK_DETECT)
216     //printf("Got Locked Status from Synth");
217     return true;
218
219   //printf("Got Unlocked Status from Synth");
220   return false;
221 }
222