switch source package format to 3.0 quilt
[debian/gnuradio] / usrp / host / lib / legacy / db_xcvr2450.cc
index c65080e6cfa98291a474057093344930cc06dd25..9ce3168e2d9f264e0958df0d9821ed98e3673f27 100644 (file)
@@ -1,5 +1,5 @@
 //
-// Copyright 2008 Free Software Foundation, Inc.
+// Copyright 2008,2009 Free Software Foundation, Inc.
 // 
 // This file is part of GNU Radio
 // 
@@ -21,6 +21,9 @@
 #include <db_xcvr2450.h>
 #include <db_base_impl.h>
 #include <cmath>
+#include <boost/thread.hpp>
+#include <boost/weak_ptr.hpp>
+#include <cstdio>
 
 #if 0
 #define LO_OFFSET 4.25e6
  */
 
 
+
+// TX IO Pins
+#define HB_PA_OFF      (1 << 15)    // 5GHz PA, 1 = off, 0 = on
+#define LB_PA_OFF      (1 << 14)    // 2.4GHz PA, 1 = off, 0 = on
+#define ANTSEL_TX1_RX2 (1 << 13)    // 1 = Ant 1 to TX, Ant 2 to RX
+#define ANTSEL_TX2_RX1 (1 << 12)    // 1 = Ant 2 to TX, Ant 1 to RX
+#define TX_EN          (1 << 11)    // 1 = TX on, 0 = TX off
+#define AD9515DIV      (1 << 4)     // 1 = Div  by 3, 0 = Div by 2
+
+#define TX_OE_MASK HB_PA_OFF|LB_PA_OFF|ANTSEL_TX1_RX2|ANTSEL_TX2_RX1|TX_EN|AD9515DIV
+#define TX_SAFE_IO HB_PA_OFF|LB_PA_OFF|ANTSEL_TX1_RX2|AD9515DIV
+
+// RX IO Pins
+#define LOCKDET (1 << 15)           // This is an INPUT!!!
+#define EN      (1 << 14)
+#define RX_EN   (1 << 13)           // 1 = RX on, 0 = RX off
+#define RX_HP   (1 << 12)
+#define RX_OE_MASK EN|RX_EN|RX_HP
+#define RX_SAFE_IO EN
+
+struct xcvr2450_key {
+  std::string serial_no;
+  int which;
+
+  bool operator==(const xcvr2450_key &x){
+    return x.serial_no ==serial_no && x.which == which;
+  }
+};
+
+class xcvr2450
+{
+private:
+  usrp_basic *d_raw_usrp;
+  int d_which;
+
+  bool d_is_shutdown;
+  int d_spi_format, d_spi_enable;
+  
+  int d_mimo, d_int_div, d_frac_div, d_highband, d_five_gig;
+  int d_cp_current, d_ref_div, d_rssi_hbw;
+  int d_txlpf_bw, d_rxlpf_bw, d_rxlpf_fine, d_rxvga_ser;
+  int d_rssi_range, d_rssi_mode, d_rssi_mux;
+  int d_rx_hp_pin, d_rx_hpf, d_rx_ant;
+  int d_tx_ant, d_txvga_ser, d_tx_driver_lin;
+  int d_tx_vga_lin, d_tx_upconv_lin, d_tx_bb_gain;
+  int d_pabias_delay, d_pabias, rx_rf_gain, rx_bb_gain, d_txgain;
+  int d_rx_rf_gain, d_rx_bb_gain;
+
+  int d_reg_standby, d_reg_int_divider, d_reg_frac_divider, d_reg_bandselpll;
+  int d_reg_cal, dsend_reg, d_reg_lpf, d_reg_rxrssi_ctrl, d_reg_txlin_gain;
+  int d_reg_pabias, d_reg_rxgain, d_reg_txgain;
+
+  int d_ad9515_div;
+
+  void _set_rfagc(float gain);
+  void _set_ifagc(float gain);
+  void _set_pga(float pga_gain);
+
+public:
+  usrp_basic *usrp(){
+    return d_raw_usrp;
+  }
+
+  xcvr2450(usrp_basic_sptr usrp, int which);
+  ~xcvr2450();
+  void shutdown();
+
+  void set_reg_standby();
+  
+  // Integer-Divider Ratio (3)
+  void set_reg_int_divider();
+  
+  // Fractional-Divider Ratio (4)
+  void set_reg_frac_divider();
+  
+  // Band Select and PLL (5)
+  void set_reg_bandselpll();
+  
+  // Calibration (6)
+  void set_reg_cal();
+
+  // Lowpass Filter (7)
+  void set_reg_lpf();
+  
+  // Rx Control/RSSI (8)
+  void set_reg_rxrssi_ctrl();
+  
+  // Tx Linearity/Baseband Gain (9)
+  void set_reg_txlin_gain();
+  
+  // PA Bias DAC (10)
+  void set_reg_pabias();
+  
+  // Rx Gain (11)
+  void set_reg_rxgain();
+  
+  // Tx Gain (12)
+  void set_reg_txgain();
+  
+  // Send register write to SPI
+  void send_reg(int v);
+
+  void set_gpio();
+  bool lock_detect();
+  bool set_rx_gain(float gain);
+  bool set_tx_gain(float gain);
+
+  struct freq_result_t set_freq(double target_freq);
+};
+
+
 /*****************************************************************************/
 
 
 xcvr2450::xcvr2450(usrp_basic_sptr _usrp, int which)
-  : d_weak_usrp(_usrp), d_which(which)
+  : d_raw_usrp(_usrp.get()), d_which(which), d_is_shutdown(false)
 {
   // Handler for Tv Rx daughterboards.
   // 
@@ -96,19 +210,19 @@ xcvr2450::xcvr2450(usrp_basic_sptr _usrp, int which)
 
   d_txgain = 63;       // 0 = min, 63 = max
 
-  // Initialize GPIO and ATR
-  tx_write_io(TX_SAFE_IO, TX_OE_MASK);
-  tx_write_oe(TX_OE_MASK, ~0);
-  tx_set_atr_txval(TX_SAFE_IO);
-  tx_set_atr_rxval(TX_SAFE_IO);
-  tx_set_atr_mask(TX_OE_MASK);
-
-  rx_write_io(RX_SAFE_IO, RX_OE_MASK);
-  rx_write_oe(RX_OE_MASK, ~0);
-  rx_set_atr_rxval(RX_SAFE_IO);
-  rx_set_atr_txval(RX_SAFE_IO);
-  rx_set_atr_mask(RX_OE_MASK);
-        
+  // Initialize GPIO and ATR  
+  usrp()->common_write_io(C_TX, d_which, TX_SAFE_IO, TX_OE_MASK);
+  usrp()->_common_write_oe(C_TX, d_which, TX_OE_MASK, 0xffff);
+  usrp()->common_write_atr_txval(C_TX, d_which, TX_SAFE_IO);
+  usrp()->common_write_atr_rxval(C_TX, d_which, TX_SAFE_IO);
+  usrp()->common_write_atr_mask(C_TX, d_which, TX_OE_MASK);
+
+  usrp()->common_write_io(C_RX, d_which, RX_SAFE_IO, RX_OE_MASK);
+  usrp()->_common_write_oe(C_RX, d_which, RX_OE_MASK, 0xffff);
+  usrp()->common_write_atr_txval(C_RX, d_which, RX_SAFE_IO);
+  usrp()->common_write_atr_rxval(C_RX, d_which, RX_SAFE_IO);
+  usrp()->common_write_atr_mask(C_RX, d_which, RX_OE_MASK);
+
   // Initialize chipset
   // TODO: perform reset sequence to ensure power up defaults
   set_reg_standby();
@@ -126,23 +240,22 @@ xcvr2450::xcvr2450(usrp_basic_sptr _usrp, int which)
 xcvr2450::~xcvr2450()
 {
   //printf("xcvr2450::destructor\n");
-  tx_set_atr_txval(TX_SAFE_IO);
-  tx_set_atr_rxval(TX_SAFE_IO);
-  rx_set_atr_rxval(RX_SAFE_IO);
-  rx_set_atr_txval(RX_SAFE_IO);
+  shutdown();
 }
 
-bool
-xcvr2450::operator==(xcvr2450_key x)
-{
-  if((x.serial_no == usrp()->serial_number()) && (x.which == d_which)) {
-    return true;
-  }
-  else {
-    return false;
+void
+xcvr2450::shutdown()
+{
+  if (!d_is_shutdown){
+    d_is_shutdown = true;
+    usrp()->common_write_atr_txval(C_TX, d_which, TX_SAFE_IO);
+    usrp()->common_write_atr_rxval(C_TX, d_which, TX_SAFE_IO);
+    usrp()->common_write_atr_txval(C_RX, d_which, RX_SAFE_IO);
+    usrp()->common_write_atr_rxval(C_RX, d_which, RX_SAFE_IO);
   }
 }
 
+
 void
 xcvr2450::set_reg_standby()
 {
@@ -274,151 +387,6 @@ xcvr2450::send_reg(int v)
   //printf("xcvr2450: Setting reg %d to %X\n", (v&15), v);
 }
 
-// --------------------------------------------------------------------
-// These methods control the GPIO bus.  Since the board has to access
-// both the io_rx_* and io_tx_* pins, we define our own methods to do so.
-// This bypasses any code in db_base.
-//
-// The board operates in ATR mode, always.  Thus, when the board is first
-// initialized, it is in receive mode, until bits show up in the TX FIFO.
-//
-
-// FIXME these should just call the similarly named common_* method on usrp_basic
-
-bool
-xcvr2450::tx_write_oe(int value, int mask)
-{
-  int reg;
-  if(d_which)
-    reg = FR_OE_2;
-  else
-    reg = FR_OE_0;
-  return usrp()->_write_fpga_reg(reg, (mask << 16) | value);
-}
-   
-bool
-xcvr2450::tx_write_io(int value, int mask)
-{
-  int reg;
-  if(d_which)
-    reg = FR_IO_2;
-  else
-    reg = FR_IO_0;
-  return usrp()->_write_fpga_reg(reg, (mask << 16) | value);
-}
-
-int
-xcvr2450::tx_read_io()
-{
-  int val;
-  if(d_which)
-    val = FR_RB_IO_RX_B_IO_TX_B;
-  else
-    val = FR_RB_IO_RX_A_IO_TX_A;
-  int t = usrp()->_read_fpga_reg(val);
-  return t & 0xffff;
-}
-
-bool
-xcvr2450::rx_write_oe(int value, int mask)
-{
-  int reg;
-  if(d_which)
-    reg = FR_OE_3;
-  else
-    reg = FR_OE_1;
-  return usrp()->_write_fpga_reg(reg, (mask << 16) | value);
-}
-
-bool
-xcvr2450::rx_write_io(int value, int mask)
-{
-  int reg;
-  if(d_which)
-    reg = FR_IO_3;
-  else
-    reg = FR_IO_1;
-  return usrp()->_write_fpga_reg(reg, (mask << 16) | value);
-}
-
-int
-xcvr2450::rx_read_io()
-{
-  int val;
-  if(d_which)
-    val = FR_RB_IO_RX_B_IO_TX_B;
-  else
-    val = FR_RB_IO_RX_A_IO_TX_A;
-  int t = usrp()->_read_fpga_reg(val);
-  return (t >> 16) & 0xffff;
-}
-
-bool
-xcvr2450::tx_set_atr_mask(int v)
-{
-  int reg;
-  if(d_which)
-    reg = FR_ATR_MASK_2;
-  else
-    reg = FR_ATR_MASK_0;
-  return usrp()->_write_fpga_reg(reg, v);
-}
-
-bool
-xcvr2450::tx_set_atr_txval(int v)
-{
-  int reg;
-  if(d_which)
-    reg = FR_ATR_TXVAL_2;
-  else
-    reg = FR_ATR_TXVAL_0;
-  return usrp()->_write_fpga_reg(reg, v);
-}
-
-bool
-xcvr2450::tx_set_atr_rxval(int v)
-{
-  int reg;
-  if(d_which)
-    reg = FR_ATR_RXVAL_2;
-  else
-    reg = FR_ATR_RXVAL_0;
-  return usrp()->_write_fpga_reg(reg, v);
-}
-
-bool
-xcvr2450::rx_set_atr_mask(int v)
-{
-  int reg;
-  if(d_which)
-    reg = FR_ATR_MASK_3;
-  else
-    reg = FR_ATR_MASK_1;
-  return usrp()->_write_fpga_reg(reg, v);
-}
-
-bool
-xcvr2450::rx_set_atr_txval(int v)
-{
-  int reg;
-  if(d_which)
-    reg = FR_ATR_TXVAL_3;
-  else
-    reg = FR_ATR_TXVAL_1;
-  return usrp()->_write_fpga_reg(reg, v);
-}
-
-bool
-xcvr2450::rx_set_atr_rxval(int v)
-{
-  int reg;
-  if(d_which)
-    reg = FR_ATR_RXVAL_3;
-  else
-    reg = FR_ATR_RXVAL_1;
-  return usrp()->_write_fpga_reg(reg, v);
-}
-
 // ----------------------------------------------------------------
 
 void
@@ -453,16 +421,21 @@ xcvr2450::set_gpio()
     tx_pa_sel = LB_PA_OFF;
   else
     tx_pa_sel = HB_PA_OFF;
+  // Reset GPIO and ATR
+  // FIXME: dont set io, oe, atr mask once basic code stops overriding our settings
+  usrp()->common_write_io(C_TX, d_which, TX_SAFE_IO, TX_OE_MASK);
+  usrp()->_common_write_oe(C_TX, d_which, TX_OE_MASK, 0xffff);
+  usrp()->common_write_atr_txval(C_TX, d_which, tx_pa_sel|tx_antsel|TX_EN|AD9515DIV);
+  usrp()->common_write_atr_rxval(C_TX, d_which, HB_PA_OFF|LB_PA_OFF|rx_antsel|AD9515DIV);
+  usrp()->common_write_atr_mask(C_TX, d_which, TX_OE_MASK);
+
+  usrp()->common_write_io(C_RX, d_which, RX_SAFE_IO, RX_OE_MASK);
+  usrp()->_common_write_oe(C_RX, d_which, RX_OE_MASK, 0xffff);
+  usrp()->common_write_atr_txval(C_RX, d_which, EN|rx_hp);
+  usrp()->common_write_atr_rxval(C_RX, d_which, EN|rx_hp|RX_EN);
+  usrp()->common_write_atr_mask(C_RX, d_which, RX_OE_MASK);
 
-  int io_rx_while_rx = EN|rx_hp|RX_EN;
-  int io_rx_while_tx = EN|rx_hp;
-  int io_tx_while_rx = HB_PA_OFF|LB_PA_OFF|rx_antsel|AD9515DIV;
-  int io_tx_while_tx = tx_pa_sel|tx_antsel|TX_EN|AD9515DIV;
-  rx_set_atr_rxval(io_rx_while_rx);
-  rx_set_atr_txval(io_rx_while_tx);
-  tx_set_atr_rxval(io_tx_while_rx);
-  tx_set_atr_txval(io_tx_while_tx);
-        
   //printf("GPIO: RXRX=%04X RXTX=%04X TXRX=%04X TXTX=%04X\n",
   //       io_rx_while_rx, io_rx_while_tx, io_tx_while_rx, io_tx_while_tx);
 }
@@ -501,7 +474,7 @@ xcvr2450::set_freq(double target_freq)
   double div = vco_freq/phdet_freq;
   d_int_div = int(floor(div));
   d_frac_div = int((div-d_int_div)*65536.0);
-  double actual_freq = phdet_freq*(d_int_div+(d_frac_div/65536.0))/scaler;
+  // double actual_freq = phdet_freq*(d_int_div+(d_frac_div/65536.0))/scaler;
   
   //printf("RF=%f VCO=%f R=%d PHD=%f DIV=%3.5f I=%3d F=%5d ACT=%f\n",
   //    target_freq, vco_freq, d_ref_div, phdet_freq,
@@ -532,11 +505,11 @@ xcvr2450::lock_detect()
     @returns: the value of the VCO/PLL lock detect bit.
     @rtype: 0 or 1
   */
-  if(rx_read_io() & LOCKDET) {
+  if(usrp()->common_read_io(C_RX, d_which) & LOCKDET) {
     return true;
   }
   else {      // Give it a second chance
-    if(rx_read_io() & LOCKDET)
+    if(usrp()->common_read_io(C_RX, d_which) & LOCKDET)
       return true;
     else
       return false;
@@ -593,32 +566,46 @@ xcvr2450::set_tx_gain(float gain)
 /*****************************************************************************/
 
 
-//_xcvr2450_inst = weakref.WeakValueDictionary()
-std::vector<xcvr2450_sptr> _xcvr2450_inst;
+struct xcvr2450_table_entry {
+  xcvr2450_key                         key;
+  boost::weak_ptr<xcvr2450>    value;
+
+  xcvr2450_table_entry(const xcvr2450_key &_key, boost::weak_ptr<xcvr2450> _value)
+    : key(_key), value(_value) {}
+};
+
+typedef std::vector<xcvr2450_table_entry> xcvr2450_table;
+
+static boost::mutex s_table_mutex;
+static xcvr2450_table s_table;
 
-xcvr2450_sptr
+static xcvr2450_sptr
 _get_or_make_xcvr2450(usrp_basic_sptr usrp, int which)
 {
-  xcvr2450_sptr inst;
   xcvr2450_key key = {usrp->serial_number(), which};
-  std::vector<xcvr2450_sptr>::iterator itr; // =
-  //std::find(_xcvr2450_inst.begin(), _xcvr2450_inst.end(), key);
-
-  for(itr = _xcvr2450_inst.begin(); itr != _xcvr2450_inst.end(); itr++) {
-    if(*(*itr) == key) {
-      //printf("Using existing xcvr2450 instance\n");
-      inst = *itr;
-      break;
+
+  boost::mutex::scoped_lock    guard(s_table_mutex);
+
+  for (xcvr2450_table::iterator p = s_table.begin(); p != s_table.end();){
+    if (p->value.expired())    // weak pointer is now dead
+      p = s_table.erase(p);    // erase it
+    else {
+      if (key == p->key){      // found it
+       return xcvr2450_sptr(p->value);
+      }
+      else                     
+       ++p;                    // keep looking
     }
   }
-  
-  if(itr == _xcvr2450_inst.end()) {
-    //printf("Creating new xcvr2450 instance\n");
-    inst = xcvr2450_sptr(new xcvr2450(usrp, which));
-    _xcvr2450_inst.push_back(inst);
-  }
 
-  return inst;
+  // We don't have the xcvr2450 we're looking for
+
+  // create a new one and stick it in the table.
+  xcvr2450_sptr r(new xcvr2450(usrp, which));
+  xcvr2450_table_entry t(key, r);
+  s_table.push_back(t);
+
+  return r;
 }
 
 
@@ -645,6 +632,22 @@ db_xcvr2450_base::~db_xcvr2450_base()
 {
 }
 
+void
+db_xcvr2450_base::shutdown_common()
+{
+  // If the usrp_basic in the xcvr2450 is the same as the usrp_basic
+  // in the daughterboard, shutdown the xcvr now (when only one of Tx
+  // and Rx is open, this is always true).
+
+  if (d_xcvr->usrp() == usrp()){
+    //std::cerr << "db_xcvr2450_base::shutdown_common: same -> shutting down\n";
+    d_xcvr->shutdown();
+  }
+  else {
+    //std::cerr << "db_xcvr2450_base::shutdown_common: different -> ignoring\n";
+  }
+}
+
 struct freq_result_t
 db_xcvr2450_base::set_freq(double target_freq)
 {
@@ -692,6 +695,16 @@ db_xcvr2450_tx::db_xcvr2450_tx(usrp_basic_sptr usrp, int which)
 
 db_xcvr2450_tx::~db_xcvr2450_tx()
 {
+  shutdown();
+}
+
+void
+db_xcvr2450_tx::shutdown()
+{
+  if (!d_is_shutdown){
+    d_is_shutdown = true;
+    shutdown_common();
+  }
 }
 
 float
@@ -741,6 +754,16 @@ db_xcvr2450_rx::db_xcvr2450_rx(usrp_basic_sptr usrp, int which)
 
 db_xcvr2450_rx::~db_xcvr2450_rx()
 {
+  shutdown();
+}
+
+void
+db_xcvr2450_rx::shutdown()
+{
+  if (!d_is_shutdown){
+    d_is_shutdown = true;
+    shutdown_common();
+  }
 }
 
 float