Added call to set antenna. Split XCVR into new thing cuz size.
[debian/gnuradio] / usrp2 / firmware / lib / db_init_wbx.c
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,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
20 #include <memory_map.h>
21 #include <i2c.h>
22 #include <usrp2_i2c_addr.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <db.h>
26 #include <db_base.h>
27 #include <hal_io.h>
28 #include <nonstdio.h>
29
30
31 struct db_base *tx_dboard;      // the tx daughterboard that's installed
32 struct db_base *rx_dboard;      // the rx daughterboard that's installed
33
34 extern struct db_base db_basic_tx;
35 extern struct db_base db_basic_rx;
36 extern struct db_base db_lf_tx;
37 extern struct db_base db_lf_rx;
38 extern struct db_base db_wbxng_rx;
39 extern struct db_base db_wbxng_tx;
40
41 struct db_base *all_dboards[] = {
42   &db_basic_tx,
43   &db_basic_rx,
44   &db_lf_tx,
45   &db_lf_rx,
46   &db_wbxng_rx,
47   &db_wbxng_tx,
48   0
49 };
50
51
52 typedef enum { UDBE_OK, UDBE_NO_EEPROM, UDBE_INVALID_EEPROM } usrp_dbeeprom_status_t;
53
54 static usrp_dbeeprom_status_t
55 read_raw_dboard_eeprom (unsigned char *buf, int i2c_addr)
56 {
57   if (!eeprom_read (i2c_addr, 0, buf, DB_EEPROM_CLEN))
58     return UDBE_NO_EEPROM;
59
60   if (buf[DB_EEPROM_MAGIC] != DB_EEPROM_MAGIC_VALUE)
61     return UDBE_INVALID_EEPROM;
62
63   int sum = 0;
64   unsigned int i;
65   for (i = 0; i < DB_EEPROM_CLEN; i++)
66     sum += buf[i];
67
68   if ((sum & 0xff) != 0)
69     return UDBE_INVALID_EEPROM;
70
71   return UDBE_OK;
72 }
73
74
75 /*
76  * Return DBID, -1 <none> or -2 <invalid eeprom contents>
77  */
78 int
79 read_dboard_eeprom(int i2c_addr)
80 {
81   unsigned char buf[DB_EEPROM_CLEN];
82
83   usrp_dbeeprom_status_t s = read_raw_dboard_eeprom (buf, i2c_addr);
84
85   //printf("\nread_raw_dboard_eeprom: %d\n", s);
86
87   switch (s){
88   case UDBE_OK:
89     return (buf[DB_EEPROM_ID_MSB] << 8) | buf[DB_EEPROM_ID_LSB];
90
91   case UDBE_NO_EEPROM:
92   default:
93     return -1;
94
95   case UDBE_INVALID_EEPROM:
96     return -2;
97   }
98 }
99
100
101 static struct db_base *
102 lookup_dbid(int dbid)
103 {
104   if (dbid < 0)
105     return 0;
106
107   int i;
108   for (i = 0; all_dboards[i]; i++)
109     if (all_dboards[i]->dbid == dbid)
110       return all_dboards[i];
111
112   return 0;
113 }
114
115 static struct db_base *
116 lookup_dboard(int i2c_addr, struct db_base *default_db, char *msg)
117 {
118   struct db_base *db;
119   int dbid = read_dboard_eeprom(i2c_addr);
120
121   // FIXME removing this printf has the system hang if there are two d'boards
122   // installed.  (I think the problem is in i2c_read/write or the way
123   // I kludge the zero-byte write to set the read address in eeprom_read.)
124   printf("%s dbid: 0x%x\n", msg, dbid);
125
126   if (dbid < 0){        // there was some kind of problem.  Treat as Basic Tx
127     return default_db;
128   }
129   else if ((db = lookup_dbid(dbid)) == 0){
130     printf("No daugherboard code for dbid = 0x%x\n", dbid);
131     return default_db;
132   }
133   return db;
134 }
135
136 void
137 set_atr_regs(int bank, struct db_base *db)
138 {
139   uint32_t      val[4];
140   int           shift;
141   int           mask;
142   int           i;
143
144   val[ATR_IDLE] = db->atr_rxval;
145   val[ATR_RX]   = db->atr_rxval;
146   val[ATR_TX]   = db->atr_txval;
147   val[ATR_FULL] = db->atr_txval;
148
149   if (bank == GPIO_TX_BANK){
150     mask = 0xffff0000;
151     shift = 16;
152   }
153   else {
154     mask = 0x0000ffff;
155     shift = 0;
156   }
157
158   for (i = 0; i < 4; i++){
159     int t = (atr_regs->v[i] & ~mask) | ((val[i] << shift) & mask);
160     //printf("atr_regs[%d] = 0x%x\n", i, t);
161     atr_regs->v[i] = t;
162   }
163 }
164
165 static void
166 set_gpio_mode(int bank, struct db_base *db)
167 {
168   int   i;
169
170   hal_gpio_set_ddr(bank, db->output_enables, 0xffff);
171   set_atr_regs(bank, db);
172
173   for (i = 0; i < 16; i++){
174     if (db->used_pins & (1 << i)){
175       // set to either GPIO_SEL_SW or GPIO_SEL_ATR
176       hal_gpio_set_sel(bank, i, (db->atr_mask & (1 << i)) ? 'a' : 's');
177     }
178   }
179 }
180
181 static int __attribute__((unused))
182 determine_tx_mux_value(struct db_base *db) 
183 {
184   if (db->i_and_q_swapped)
185     return 0x01;
186   else
187     return 0x10;
188 }
189
190 static int
191 determine_rx_mux_value(struct db_base *db)
192 {
193 #define ADC0 0x0
194 #define ADC1 0x1
195 #define ZERO 0x2
196   
197   static int truth_table[8] = {
198     /* swap_iq, uses */
199     /* 0, 0x0 */    (ZERO << 2) | ZERO,         // N/A
200     /* 0, 0x1 */    (ZERO << 2) | ADC0,
201     /* 0, 0x2 */    (ZERO << 2) | ADC1,
202     /* 0, 0x3 */    (ADC1 << 2) | ADC0,
203     /* 1, 0x0 */    (ZERO << 2) | ZERO,         // N/A
204     /* 1, 0x1 */    (ZERO << 2) | ADC0,
205     /* 1, 0x2 */    (ZERO << 2) | ADC1,
206     /* 1, 0x3 */    (ADC0 << 2) | ADC1,
207   };
208
209   int   subdev0_uses;
210   int   subdev1_uses;
211   int   uses;
212
213   if (db->is_quadrature)
214     subdev0_uses = 0x3;         // uses A/D 0 and 1
215   else
216     subdev0_uses = 0x1;         // uses A/D 0 only
217
218   // FIXME second subdev on Basic Rx, LF RX
219   // if subdev2 exists
220   // subdev1_uses = 0x2;
221   subdev1_uses = 0;
222
223   uses = subdev0_uses;
224
225   int swap_iq = db->i_and_q_swapped & 0x1;
226   int index = (swap_iq << 2) | uses;
227
228   return truth_table[index];
229 }
230
231
232 void
233 db_init(void)
234 {
235   int   m;
236
237   tx_dboard = lookup_dboard(I2C_ADDR_TX_A, &db_basic_tx, "Tx");
238   //printf("db_init: tx dbid = 0x%x\n", tx_dboard->dbid);
239   set_gpio_mode(GPIO_TX_BANK, tx_dboard);
240   tx_dboard->init(tx_dboard);
241   m = determine_tx_mux_value(tx_dboard);
242   dsp_tx_regs->tx_mux = m;
243   //printf("tx_mux = 0x%x\n", m);
244   tx_dboard->current_lo_offset = tx_dboard->default_lo_offset;
245
246   rx_dboard = lookup_dboard(I2C_ADDR_RX_A, &db_basic_rx, "Rx");
247   //printf("db_init: rx dbid = 0x%x\n", rx_dboard->dbid);
248   set_gpio_mode(GPIO_RX_BANK, rx_dboard);
249   rx_dboard->init(rx_dboard);
250   m = determine_rx_mux_value(rx_dboard);
251   dsp_rx_regs->rx_mux = m;
252   //printf("rx_mux = 0x%x\n", m);
253   rx_dboard->current_lo_offset = rx_dboard->default_lo_offset;
254 }
255
256 /*!
257  *  Calculate the frequency to use for setting the digital down converter.
258  *
259  *  \param[in] target_freq   desired RF frequency (Hz)
260  *  \param[in] baseband_freq the RF frequency that corresponds to DC in the IF.
261  * 
262  *  \param[out] dxc_freq is the value for the ddc
263  *  \param[out] inverted is true if we're operating in an inverted Nyquist zone.
264 */
265 void
266 calc_dxc_freq(u2_fxpt_freq_t target_freq, u2_fxpt_freq_t baseband_freq,
267               u2_fxpt_freq_t *dxc_freq, bool *inverted)
268 {
269   u2_fxpt_freq_t fs = U2_DOUBLE_TO_FXPT_FREQ(100e6);    // converter sample rate
270   u2_fxpt_freq_t delta = target_freq - baseband_freq;
271
272 #if 0
273   printf("calc_dxc_freq\n");
274   printf("  fs       = "); print_fxpt_freq(fs); newline();
275   printf("  target   = "); print_fxpt_freq(target_freq); newline();
276   printf("  baseband = "); print_fxpt_freq(baseband_freq); newline();
277   printf("  delta    = "); print_fxpt_freq(delta); newline();
278 #endif  
279
280   if (delta >= 0){
281     while (delta > fs)
282       delta -= fs;
283     if (delta <= fs/2){         // non-inverted region
284       *dxc_freq = -delta;
285       *inverted = false;
286     }
287     else {                      // inverted region
288       *dxc_freq = delta - fs;
289       *inverted = true;
290     }
291   }
292   else {
293     while (delta < -fs)
294       delta += fs;
295     if (delta >= -fs/2){        // non-inverted region
296       *dxc_freq = -delta;
297       *inverted = false;
298     }
299     else {                      // inverted region
300       *dxc_freq = delta + fs;
301       *inverted = true;
302     }
303   }
304 }
305
306 bool
307 db_set_lo_offset(struct db_base *db, u2_fxpt_freq_t offset)
308 {
309   db->current_lo_offset = offset;
310   return true;
311 }
312
313 bool
314 db_tune(struct db_base *db, u2_fxpt_freq_t target_freq, struct tune_result *result)
315 {
316   memset(result, 0, sizeof(*result));
317   bool inverted = false;
318   u2_fxpt_freq_t dxc_freq;
319   u2_fxpt_freq_t actual_dxc_freq;
320
321   // Ask the d'board to tune as closely as it can to target_freq+lo_offset
322   bool ok = db->set_freq(db, target_freq+db->current_lo_offset, &result->baseband_freq);
323
324   // Calculate the DDC setting that will downconvert the baseband from the
325   // daughterboard to our target frequency.
326   calc_dxc_freq(target_freq, result->baseband_freq, &dxc_freq, &inverted);
327
328   // If the spectrum is inverted, and the daughterboard doesn't do
329   // quadrature downconversion, we can fix the inversion by flipping the
330   // sign of the dxc_freq...  (This only happens using the basic_rx board)
331   
332   if (db->spectrum_inverted)
333     inverted = !inverted;
334
335   if (inverted && !db->is_quadrature){
336     dxc_freq = -dxc_freq;
337     inverted = !inverted;
338   }
339
340   if (db->is_tx){
341     dxc_freq = -dxc_freq;       // down conversion versus up conversion
342     ok &= db_set_duc_freq(dxc_freq, &actual_dxc_freq);
343   }
344   else {
345     ok &= db_set_ddc_freq(dxc_freq, &actual_dxc_freq);
346   }
347
348   result->dxc_freq = dxc_freq;
349   result->residual_freq = dxc_freq - actual_dxc_freq;
350   result->inverted = inverted;
351   return ok;
352 }
353
354 static int32_t
355 compute_freq_control_word(u2_fxpt_freq_t target_freq, u2_fxpt_freq_t *actual_freq)
356 {
357   // If we were using floating point, we'd calculate
358   //   master = 100e6;
359   //   v = (int) rint(target_freq / master_freq) * pow(2.0, 32.0);
360
361   //printf("compute_freq_control_word\n");
362   //printf("  target_freq = "); print_fxpt_freq(target_freq); newline();
363
364   int32_t master_freq = 100000000;      // 100M
365
366   int32_t v = ((target_freq << 12)) / master_freq;
367   //printf("  fcw = %d\n", v);
368
369   *actual_freq = (v * (int64_t) master_freq) >> 12;
370
371   //printf("  actual = "); print_fxpt_freq(*actual_freq); newline();
372
373   return v;
374 }
375
376
377 bool
378 db_set_ddc_freq(u2_fxpt_freq_t dxc_freq, u2_fxpt_freq_t *actual_dxc_freq)
379 {
380   int32_t v = compute_freq_control_word(dxc_freq, actual_dxc_freq);
381   dsp_rx_regs->freq = v;
382   return true;
383 }
384
385 bool
386 db_set_duc_freq(u2_fxpt_freq_t dxc_freq, u2_fxpt_freq_t *actual_dxc_freq)
387 {
388   int32_t v = compute_freq_control_word(dxc_freq, actual_dxc_freq);
389   dsp_tx_regs->freq = v;
390   return true;
391 }
392
393 bool
394 db_set_gain(struct db_base *db, u2_fxpt_gain_t gain)
395 {
396   return db->set_gain(db, gain);
397 }
398
399 bool
400 db_set_antenna(struct db_base *db, int ant)
401 {
402   return db->set_antenna(db, ant);
403 }