Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / usrp2 / firmware / lib / db_rfx.c
index 10313844f7ee5647241cd5bda3dd4f73fe85d42f..546559010de4bc42a2b6236a77f9d3ebf8197b39 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2008 Free Software Foundation, Inc.
+ * Copyright 2008,2009 Free Software Foundation, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,7 +19,6 @@
 #include <memory_map.h>
 #include <db_base.h>
 #include <hal_io.h>
-#include <ad9510.h>
 #include <stdio.h>
 #include <mdelay.h>
 #include <lsdac.h>
@@ -72,7 +71,7 @@ struct db_rfx_common {
   unsigned char CP1;
   unsigned char CP2;
   int freq_mult;
-  int spi_mask;
+  int spi_mask;  
 };
 
 struct db_rfx_dummy {
@@ -142,13 +141,13 @@ struct db_rfx_400_rx db_rfx_400_rx = {
   .base.used_pins = 0x00FF,
   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(400e6),
   .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(500e6),
-  //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
+  .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
+  .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(45),
+  .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(0.022),
   .base.is_quadrature = true,
   .base.i_and_q_swapped = true,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
   .base.init = rfx_init_rx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_rx,
@@ -158,6 +157,7 @@ struct db_rfx_400_rx db_rfx_400_rx = {
   .base.atr_rxval = POWER_UP|MIX_EN,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 0,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -177,9 +177,9 @@ struct db_rfx_400_tx db_rfx_400_tx = {
   //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   .base.is_quadrature = true,
-  .base.i_and_q_swapped = true,
+  .base.i_and_q_swapped = false,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(12.5e6),
   .base.init = rfx_init_tx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_tx,
@@ -189,6 +189,7 @@ struct db_rfx_400_tx db_rfx_400_tx = {
   .base.atr_rxval = POWER_UP|ANT_SW,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 1,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -201,15 +202,15 @@ struct db_rfx_900_rx db_rfx_900_rx = {
   .base.is_tx = false,
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
-  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(800e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1000e6),
-  //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
+  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(750e6),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1050e6),
+  .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
+  .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(70),
+  .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(0.034),
   .base.is_quadrature = true,
   .base.i_and_q_swapped = true,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
   .base.init = rfx_init_rx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_rx,
@@ -219,6 +220,7 @@ struct db_rfx_900_rx db_rfx_900_rx = {
   .base.atr_rxval = MIX_EN,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 1,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -232,15 +234,15 @@ struct db_rfx_900_tx db_rfx_900_tx = {
   .base.is_tx = true,
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
-  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(800e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1000e6),
+  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(750e6),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1050e6),
   //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   .base.is_quadrature = true,
-  .base.i_and_q_swapped = true,
+  .base.i_and_q_swapped = false,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(12.5e6),
   .base.init = rfx_init_tx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_tx,
@@ -250,6 +252,7 @@ struct db_rfx_900_tx db_rfx_900_tx = {
   .base.atr_rxval = ANT_SW,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 1,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -263,14 +266,14 @@ struct db_rfx_1200_rx db_rfx_1200_rx = {
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(1150e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1350e6),
-  //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1450e6),
+  .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
+  .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(70),
+  .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(0.034),
   .base.is_quadrature = true,
   .base.i_and_q_swapped = true,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
   .base.init = rfx_init_rx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_rx,
@@ -280,6 +283,7 @@ struct db_rfx_1200_rx db_rfx_1200_rx = {
   .base.atr_rxval = MIX_EN,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 1,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -294,14 +298,14 @@ struct db_rfx_1200_tx db_rfx_1200_tx = {
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(1150e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1350e6),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(1450e6),
   //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   .base.is_quadrature = true,
-  .base.i_and_q_swapped = true,
+  .base.i_and_q_swapped = false,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(12.5e6),
   .base.init = rfx_init_tx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_tx,
@@ -311,6 +315,7 @@ struct db_rfx_1200_tx db_rfx_1200_tx = {
   .base.atr_rxval = ANT_SW,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 1,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -323,15 +328,15 @@ struct db_rfx_1800_rx db_rfx_1800_rx = {
   .base.is_tx = false,
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
-  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(1600e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2000e6),
-  //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
+  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(1500e6),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2100e6),
+  .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
+  .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(70),
+  .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(0.034),
   .base.is_quadrature = true,
   .base.i_and_q_swapped = true,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
   .base.init = rfx_init_rx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_rx,
@@ -341,6 +346,7 @@ struct db_rfx_1800_rx db_rfx_1800_rx = {
   .base.atr_rxval = MIX_EN,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 0,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -354,15 +360,15 @@ struct db_rfx_1800_tx db_rfx_1800_tx = {
   .base.is_tx = true,
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
-  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(1600e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2000e6),
+  .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(1500e6),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2100e6),
   //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   .base.is_quadrature = true,
-  .base.i_and_q_swapped = true,
+  .base.i_and_q_swapped = false,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(12.5e6),
   .base.init = rfx_init_tx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_tx,
@@ -372,11 +378,12 @@ struct db_rfx_1800_tx db_rfx_1800_tx = {
   .base.atr_rxval = ANT_SW,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 0,
   .common.CP1 = 7,
   .common.CP2 = 7,
   .common.spi_mask = SPI_SS_TX_DB,
-  .common.freq_mult = 1
+  .common.freq_mult = 1  
 };
 
 
@@ -386,14 +393,14 @@ struct db_rfx_2400_rx db_rfx_2400_rx = {
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(2300e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2700e6),
-  //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
-  //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2900e6),
+  .base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(0),
+  .base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(70),
+  .base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(0.034),
   .base.is_quadrature = true,
   .base.i_and_q_swapped = true,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(0),
   .base.init = rfx_init_rx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_rx,
@@ -403,6 +410,7 @@ struct db_rfx_2400_rx db_rfx_2400_rx = {
   .base.atr_rxval = MIX_EN,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 0,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -417,14 +425,14 @@ struct db_rfx_2400_tx db_rfx_2400_tx = {
   .base.output_enables = 0x00E0,
   .base.used_pins = 0x00FF,
   .base.freq_min = U2_DOUBLE_TO_FXPT_FREQ(2300e6),
-  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2700e6),
+  .base.freq_max = U2_DOUBLE_TO_FXPT_FREQ(2900e6),
   //.base.gain_min = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_max = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   //.base.gain_step_size = U2_DOUBLE_TO_FXPT_GAIN(xxx),
   .base.is_quadrature = true,
-  .base.i_and_q_swapped = true,
+  .base.i_and_q_swapped = false,
   .base.spectrum_inverted = false,
-  //.base.lo_offset = U2_DOUBLE_TO_FXPT_FREQ(4e6),
+  .base.default_lo_offset = U2_DOUBLE_TO_FXPT_FREQ(12.5e6),
   .base.init = rfx_init_tx,
   .base.set_freq = rfx_set_freq,
   .base.set_gain = rfx_set_gain_tx,
@@ -434,6 +442,7 @@ struct db_rfx_2400_tx db_rfx_2400_tx = {
   .base.atr_rxval = ANT_SW,
   // .base.atr_tx_delay =
   // .base.atr_rx_delay =
+  .base.set_antenna = 0,
   .common.DIV2 = 0,
   .common.CP1 = 7,
   .common.CP2 = 7,
@@ -447,6 +456,10 @@ rfx_init_tx(struct db_base *dbb)
 {
   //struct db_rfx_dummy *db = (struct db_rfx_dummy *) dbb;
   clocks_enable_tx_dboard(true, 0);
+
+  // Set the freq now to get the one time 10ms delay out of the way.
+  u2_fxpt_freq_t       dc;
+  dbb->set_freq(dbb, dbb->freq_min, &dc);
   return true;
 }
 
@@ -458,18 +471,22 @@ rfx_init_rx(struct db_base *dbb)
 
   // test gain
   dbb->set_gain(dbb,U2_DOUBLE_TO_FXPT_GAIN(45.0));
-  printf("set the gain\n");
+
+  // Set the freq now to get the one time 10ms delay out of the way.
+  u2_fxpt_freq_t       dc;
+  dbb->set_freq(dbb, dbb->freq_min, &dc);
+
   return true;
 }
 
 bool
 rfx_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc)
 {
+  static unsigned char first = true;
+
   *dc = 0;
   struct db_rfx_dummy *db = (struct db_rfx_dummy *) dbb;
-  //u2_fxpt_freq_t desired_n = db->common.freq_mult*freq/phdet_freq;
-  //int N_DIV = u2_fxpt_freq_round_to_int(desired_n);
-  u2_fxpt_freq_t desired_n = ((1LL<<20) * db->common.freq_mult*freq)/phdet_freq;
+  u2_fxpt_freq_t desired_n = (U2_DOUBLE_TO_FXPT_FREQ(1.0)*db->common.freq_mult*freq)/phdet_freq;
   int N_DIV = u2_fxpt_freq_round_to_int(desired_n);
   int B = N_DIV/PRESCALER;
   int A = N_DIV - PRESCALER*B;
@@ -484,10 +501,13 @@ rfx_set_freq(struct db_base *dbb, u2_fxpt_freq_t freq, u2_fxpt_freq_t *dc)
 
   spi_transact(SPI_TXONLY,db->common.spi_mask,R,24,SPIF_PUSH_FALL);
   spi_transact(SPI_TXONLY,db->common.spi_mask,C,24,SPIF_PUSH_FALL);
-  mdelay(10);
+  if (first){
+    first = false;
+    mdelay(10);
+  }
   spi_transact(SPI_TXONLY,db->common.spi_mask,N,24,SPIF_PUSH_FALL);
 
-  printf("A = %d, B = %d, N_DIV = %d\n",A, B, N_DIV);
+  //printf("A = %d, B = %d, N_DIV = %d\n",A, B, N_DIV);
   *dc = (N_DIV * phdet_freq) / db->common.freq_mult;
   return true;
 }
@@ -504,43 +524,14 @@ rfx_set_gain_rx(struct db_base *dbb, u2_fxpt_gain_t gain)
 {
   struct db_rfx_dummy *db = (struct db_rfx_dummy *) dbb;
 
-  u2_fxpt_gain_t MAXGAIN = U2_DOUBLE_TO_FXPT_GAIN(70.0);
-
   int offset_q8 = (int)(1.2/3.3*4096*(1<<15));  
-  int slope_q8 = (int)(-1.0/45.0*4096/3.3*256); 
+  int range_q15 = (int)(-1.0*4096/3.3*256*128);
+  int slope_q8 = range_q15/db->base.gain_max;
+
   int dacword = ((slope_q8 * gain) + offset_q8)>>15;
-  printf("DACWORD %d\n",dacword);
+  //printf("DACWORD %d\n",dacword);
   lsdac_write_rx(1,dacword);
   return true;
-  /*
-    def set_gain(self, gain):
-        """
-        Set the gain.
-
-        @param gain:  gain in decibels
-        @returns True/False
-        """
-        maxgain = self.gain_range()[1] - self._u.pga_max()
-        mingain = self.gain_range()[0]
-        if gain > maxgain:
-            pga_gain = gain-maxgain
-            assert pga_gain <= self._u.pga_max()
-            agc_gain = maxgain
-        else:
-            pga_gain = 0
-            agc_gain = gain
-        V_maxgain = .2
-        V_mingain = 1.2
-        V_fullscale = 3.3
-        dac_value = (agc_gain*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale
-        assert dac_value>=0 and dac_value<4096
-        return self._u.write_aux_dac(self._which, 0, int(dac_value)) and \
-               self._set_pga(int(pga_gain))
-
-    def gain_range(self):
-        return (self._u.pga_min(), self._u.pga_max() + 70, 0.05) -- For 900-2400
-        return (self._u.pga_min(), self._u.pga_max() + 45, 0.035) -- For 400
-  */
 }