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