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