uhd: Add command line option for GPS reference

Unlike earlier versions of UHD, the current release (3.9.2)
does not automatically select on-board GPSDO as the reference
source. Modify the command line settings to allow explicit
selection of GPS in addition to the external setting.

Simultaneous GPS and external reference settingis disallowed.

Signed-off-by: Tom Tsou <tom.tsou@ettus.com>
diff --git a/Transceiver52M/UHDDevice.cpp b/Transceiver52M/UHDDevice.cpp
index e6a5326..4ed539e 100644
--- a/Transceiver52M/UHDDevice.cpp
+++ b/Transceiver52M/UHDDevice.cpp
@@ -269,7 +269,7 @@
 		   size_t chans, double offset);
 	~uhd_device();
 
-	int open(const std::string &args, bool extref, bool swap_channels);
+	int open(const std::string &args, int ref, bool swap_channels);
 	bool start();
 	bool stop();
 	bool restart();
@@ -770,8 +770,10 @@
 	return true;
 }
 
-int uhd_device::open(const std::string &args, bool extref, bool swap_channels)
+int uhd_device::open(const std::string &args, int ref, bool swap_channels)
 {
+	const char *refstr;
+
 	// Find UHD devices
 	uhd::device_addr_t addr(args);
 	uhd::device_addrs_t dev_addrs = uhd::device::find(addr);
@@ -828,8 +830,22 @@
 	rx_gains.resize(chans);
 	rx_buffers.resize(chans);
 
-	if (extref)
-		usrp_dev->set_clock_source("external");
+	switch (ref) {
+	case REF_INTERNAL:
+		refstr = "internal";
+		break;
+	case REF_EXTERNAL:
+		refstr = "external";
+		break;
+	case REF_GPS:
+		refstr = "gpsdo";
+		break;
+	default:
+		LOG(ALERT) << "Invalid reference type";
+		return -1;
+	}
+
+	usrp_dev->set_clock_source(refstr);
 
 	// Set rates
 	double _rx_rate = select_rate(dev_type, rx_sps, iface);
diff --git a/Transceiver52M/USRPDevice.cpp b/Transceiver52M/USRPDevice.cpp
index bf6ccca..1092600 100644
--- a/Transceiver52M/USRPDevice.cpp
+++ b/Transceiver52M/USRPDevice.cpp
@@ -89,7 +89,7 @@
 #endif
 }
 
-int USRPDevice::open(const std::string &, bool, bool)
+int USRPDevice::open(const std::string &, int, bool)
 {
   writeLock.unlock();
 
diff --git a/Transceiver52M/USRPDevice.h b/Transceiver52M/USRPDevice.h
index 6bc5f1d..b560339 100644
--- a/Transceiver52M/USRPDevice.h
+++ b/Transceiver52M/USRPDevice.h
@@ -99,7 +99,7 @@
   USRPDevice(size_t sps);
 
   /** Instantiate the USRP */
-  int open(const std::string &, bool, bool);
+  int open(const std::string &, int, bool);
 
   /** Start the USRP */
   bool start();
diff --git a/Transceiver52M/osmo-trx.cpp b/Transceiver52M/osmo-trx.cpp
index 29f0488..a13ec1b 100644
--- a/Transceiver52M/osmo-trx.cpp
+++ b/Transceiver52M/osmo-trx.cpp
@@ -72,6 +72,7 @@
 	unsigned rtsc;
 	unsigned rach_delay;
 	bool extref;
+	bool gpsref;
 	Transceiver::FillerType filler;
 	bool diversity;
 	bool mcbts;
@@ -183,10 +184,16 @@
 	}
 
 	edgestr = config->edge ? "Enabled" : "Disabled";
-	refstr = config->extref ? "Enabled" : "Disabled";
 	divstr = config->diversity ? "Enabled" : "Disabled";
 	mcstr = config->mcbts ? "Enabled" : "Disabled";
 
+	if (config->extref)
+		refstr = "External";
+	else if (config->gpsref)
+		refstr = "GPS";
+	else
+		refstr = "Internal";
+
 	switch (config->filler) {
 	case Transceiver::FILLER_DUMMY:
 		fillstr = "Dummy bursts";
@@ -215,7 +222,7 @@
 	ost << "   Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
 	ost << "   Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
 	ost << "   EDGE support............ " << edgestr << std::endl;
-	ost << "   External Reference...... " << refstr << std::endl;
+	ost << "   Reference............... " << refstr << std::endl;
 	ost << "   C0 Filler Table......... " << fillstr << std::endl;
 	ost << "   Multi-Carrier........... " << mcstr << std::endl;
 	ost << "   Diversity............... " << divstr << std::endl;
@@ -331,9 +338,10 @@
 		"  -i    IP address of GSM core\n"
 		"  -p    Base port number\n"
 		"  -e    Enable EDGE receiver\n"
-		"  -d    Enable dual channel diversity receiver\n"
+		"  -d    Enable dual channel diversity receiver (deprecated)\n"
 		"  -m    Enable multi-ARFCN transceiver (default=disabled)\n"
 		"  -x    Enable external 10 MHz reference\n"
+		"  -g    Enable GPSDO reference\n"
 		"  -s    Tx samples-per-symbol (1 or 4)\n"
 		"  -b    Rx samples-per-symbol (1 or 4)\n"
 		"  -c    Number of ARFCN channels (default=1)\n"
@@ -357,6 +365,7 @@
 	config->rtsc = 0;
 	config->rach_delay = 0;
 	config->extref = false;
+	config->gpsref = false;
 	config->filler = Transceiver::FILLER_ZERO;
 	config->mcbts = false;
 	config->diversity = false;
@@ -365,7 +374,7 @@
 	config->swap_channels = false;
 	config->edge = false;
 
-	while ((option = getopt(argc, argv, "ha:l:i:p:c:dmxfo:s:b:r:A:R:Se")) != -1) {
+	while ((option = getopt(argc, argv, "ha:l:i:p:c:dmxgfo:s:b:r:A:R:Se")) != -1) {
 		switch (option) {
 		case 'h':
 			print_help();
@@ -395,6 +404,9 @@
 		case 'x':
 			config->extref = true;
 			break;
+		case 'g':
+			config->gpsref = true;
+			break;
 		case 'f':
 			config->filler = Transceiver::FILLER_DUMMY;
 			break;
@@ -430,6 +442,12 @@
 		}
 	}
 
+	if (config->gpsref && config->extref) {
+		printf("External and GPSDO references unavailable at the same time\n\n");
+		print_help();
+		exit(0);
+	}
+
 	/* Force 4 SPS for EDGE or multi-ARFCN configurations */
 	if ((config->edge) || (config->mcbts)) {
 		config->tx_sps = 4;
@@ -461,7 +479,7 @@
 
 int main(int argc, char *argv[])
 {
-	int type, chans;
+	int type, chans, ref;
 	RadioDevice *usrp;
 	RadioInterface *radio = NULL;
 	Transceiver *trx = NULL;
@@ -486,9 +504,16 @@
 	if (config.mcbts)
 		iface = RadioDevice::MULTI_ARFCN;
 
+	if (config.extref)
+		ref = RadioDevice::REF_EXTERNAL;
+	else if (config.gpsref)
+		ref = RadioDevice::REF_GPS;
+	else
+		ref = RadioDevice::REF_INTERNAL;
+
 	usrp = RadioDevice::make(config.tx_sps, config.rx_sps, iface,
 				 config.chans, config.offset);
-	type = usrp->open(config.dev_args, config.extref, config.swap_channels);
+	type = usrp->open(config.dev_args, ref, config.swap_channels);
 	if (type < 0) {
 		LOG(ALERT) << "Failed to create radio device" << std::endl;
 		goto shutdown;
diff --git a/Transceiver52M/radioDevice.h b/Transceiver52M/radioDevice.h
index 142fcf6..711d678 100644
--- a/Transceiver52M/radioDevice.h
+++ b/Transceiver52M/radioDevice.h
@@ -44,11 +44,17 @@
     DIVERSITY,
   };
 
+  enum ReferenceType {
+    REF_INTERNAL,
+    REF_EXTERNAL,
+    REF_GPS,
+  };
+
   static RadioDevice *make(size_t tx_sps, size_t rx_sps, InterfaceType type,
                            size_t chans = 1, double offset = 0.0);
 
   /** Initialize the USRP */
-  virtual int open(const std::string &args = "", bool extref = false, bool swap_channels = false)=0;
+  virtual int open(const std::string &args, int ref, bool swap_channels)=0;
 
   virtual ~RadioDevice() { }