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