blob: 451b5a96de23f4c79179197bcf0e8512a1bfa992 [file] [log] [blame]
dburgessb3a0ca42011-10-12 07:44:40 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4* This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribuion.
5*
6* This use of this software may be subject to additional restrictions.
7* See the LEGAL file in the main directory for details.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13*/
14
15#ifndef _USRP_DEVICE_H_
16#define _USRP_DEVICE_H_
17
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include "radioDevice.h"
23
Thomas Tsouc0641242013-10-11 14:55:31 -040024#include <usrp/usrp_standard.h>
25#include <usrp/usrp_bytesex.h>
26#include <usrp/usrp_prims.h>
dburgessb3a0ca42011-10-12 07:44:40 +000027#include <sys/time.h>
28#include <math.h>
29#include <string>
30#include <iostream>
31
dburgessb3a0ca42011-10-12 07:44:40 +000032#include <boost/shared_ptr.hpp>
33typedef boost::shared_ptr<usrp_standard_tx> usrp_standard_tx_sptr;
34typedef boost::shared_ptr<usrp_standard_rx> usrp_standard_rx_sptr;
dburgessb3a0ca42011-10-12 07:44:40 +000035
36/** A class to handle a USRP rev 4, with a two RFX900 daughterboards */
37class USRPDevice: public RadioDevice {
38
39private:
40
41 static const double masterClockRate; ///< the USRP clock rate
42 double desiredSampleRate; ///< the desired sampling rate
43 usrp_standard_rx_sptr m_uRx; ///< the USRP receiver
44 usrp_standard_tx_sptr m_uTx; ///< the USRP transmitter
kurtis.heimerl79e71c92011-11-26 03:16:48 +000045
46 db_base_sptr m_dbRx; ///< rx daughterboard
47 db_base_sptr m_dbTx; ///< tx daughterboard
48 usrp_subdev_spec rxSubdevSpec;
49 usrp_subdev_spec txSubdevSpec;
50
dburgessb3a0ca42011-10-12 07:44:40 +000051 double actualSampleRate; ///< the actual USRP sampling rate
52 unsigned int decimRate; ///< the USRP decimation rate
53
54 unsigned long long samplesRead; ///< number of samples read from USRP
55 unsigned long long samplesWritten; ///< number of samples sent to USRP
56
57 bool started; ///< flag indicates USRP has started
58 bool skipRx; ///< set if USRP is transmit-only.
59
60 static const unsigned int currDataSize_log2 = 21;
61 static const unsigned long currDataSize = (1 << currDataSize_log2);
62 short *data;
63 unsigned long dataStart;
64 unsigned long dataEnd;
65 TIMESTAMP timeStart;
66 TIMESTAMP timeEnd;
67 bool isAligned;
68
69 Mutex writeLock;
70
71 short *currData; ///< internal data buffer when reading from USRP
72 TIMESTAMP currTimestamp; ///< timestamp of internal data buffer
73 unsigned currLen; ///< size of internal data buffer
74
75 TIMESTAMP timestampOffset; ///< timestamp offset b/w Tx and Rx blocks
76 TIMESTAMP latestWriteTimestamp; ///< timestamp of most recent ping command
77 TIMESTAMP pingTimestamp; ///< timestamp of most recent ping response
Thomas Tsouc1f7c422013-10-11 13:49:55 -040078
79 long long pingOffset;
dburgessb3a0ca42011-10-12 07:44:40 +000080 unsigned long hi32Timestamp;
81 unsigned long lastPktTimestamp;
82
83 double rxGain;
84
Pau Espin Pedrolf58cd8a2018-02-05 13:04:41 +010085#ifdef SWLOOPBACK
dburgessb3a0ca42011-10-12 07:44:40 +000086 short loopbackBuffer[1000000];
87 int loopbackBufferSize;
Pau Espin Pedrolf58cd8a2018-02-05 13:04:41 +010088 double samplePeriod;
dburgessb3a0ca42011-10-12 07:44:40 +000089
90 struct timeval startTime;
91 struct timeval lastReadTime;
92 bool firstRead;
93#endif
94
dburgessb3a0ca42011-10-12 07:44:40 +000095 public:
96
97 /** Object constructor */
Harald Welte61707e82018-06-13 23:21:57 +020098 USRPDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, size_t chans, double lo_offset,
99 const std::vector<std::string>& tx_paths,
100 const std::vector<std::string>& rx_paths);
dburgessb3a0ca42011-10-12 07:44:40 +0000101
102 /** Instantiate the USRP */
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700103 int open(const std::string &, int, bool);
dburgessb3a0ca42011-10-12 07:44:40 +0000104
105 /** Start the USRP */
106 bool start();
107
108 /** Stop the USRP */
109 bool stop();
110
kurtis.heimerld9364002011-11-26 03:17:15 +0000111 /** Set priority not supported */
Thomas Tsou7553aa92013-11-08 12:50:03 -0500112 void setPriority(float prio = 0.5) { }
kurtis.heimerld9364002011-11-26 03:17:15 +0000113
Thomas Tsou02d88d12013-04-05 15:36:30 -0400114 enum TxWindowType getWindowType() { return TX_WINDOW_USRP1; }
kurtis.heimerle380af32011-11-26 03:18:55 +0000115
dburgessb3a0ca42011-10-12 07:44:40 +0000116 /**
117 Read samples from the USRP.
118 @param buf preallocated buf to contain read result
119 @param len number of samples desired
120 @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough
121 @param timestamp The timestamp of the first samples to be read
122 @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough
123 @param RSSI The received signal strength of the read result
124 @return The number of samples actually read
125 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400126 int readSamples(std::vector<short *> &buf, int len, bool *overrun,
127 TIMESTAMP timestamp = 0xffffffff, bool *underrun = NULL,
128 unsigned *RSSI = NULL);
dburgessb3a0ca42011-10-12 07:44:40 +0000129 /**
130 Write samples to the USRP.
131 @param buf Contains the data to be written.
132 @param len number of samples to write.
133 @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough
134 @param timestamp The timestamp of the first sample of the data buffer.
135 @param isControl Set if data is a control packet, e.g. a ping command
136 @return The number of samples actually written
137 */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400138 int writeSamples(std::vector<short *> &bufs, int len, bool *underrun,
139 TIMESTAMP timestamp = 0xffffffff, bool isControl = false);
140
dburgessb3a0ca42011-10-12 07:44:40 +0000141 /** Update the alignment between the read and write timestamps */
142 bool updateAlignment(TIMESTAMP timestamp);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400143
dburgessb3a0ca42011-10-12 07:44:40 +0000144 /** Set the transmitter frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400145 bool setTxFreq(double wFreq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000146
147 /** Set the receiver frequency */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400148 bool setRxFreq(double wFreq, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000149
150 /** Returns the starting write Timestamp*/
kurtis.heimerlc8739b82011-11-02 00:06:34 +0000151 TIMESTAMP initialWriteTimestamp(void) { return 20000;}
dburgessb3a0ca42011-10-12 07:44:40 +0000152
153 /** Returns the starting read Timestamp*/
kurtis.heimerlc8739b82011-11-02 00:06:34 +0000154 TIMESTAMP initialReadTimestamp(void) { return 20000;}
dburgessb3a0ca42011-10-12 07:44:40 +0000155
156 /** returns the full-scale transmit amplitude **/
157 double fullScaleInputValue() {return 13500.0;}
158
159 /** returns the full-scale receive amplitude **/
160 double fullScaleOutputValue() {return 9450.0;}
161
162 /** sets the receive chan gain, returns the gain setting **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400163 double setRxGain(double dB, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000164
165 /** get the current receive gain */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400166 double getRxGain(size_t chan = 0) { return rxGain; }
dburgessb3a0ca42011-10-12 07:44:40 +0000167
168 /** return maximum Rx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000169 double maxRxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000170
171 /** return minimum Rx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000172 double minRxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000173
174 /** sets the transmit chan gain, returns the gain setting **/
Thomas Tsou204a9f12013-10-29 18:34:16 -0400175 double setTxGain(double dB, size_t chan = 0);
dburgessb3a0ca42011-10-12 07:44:40 +0000176
177 /** return maximum Tx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000178 double maxTxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000179
180 /** return minimum Rx Gain **/
kurtis.heimerl79e71c92011-11-26 03:16:48 +0000181 double minTxGain(void);
dburgessb3a0ca42011-10-12 07:44:40 +0000182
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100183 /** sets the RX path to use, returns true if successful and false otherwise */
184 bool setRxAntenna(const std::string &ant, size_t chan = 0);
185
186 /* return the used RX path */
187 std::string getRxAntenna(size_t chan = 0);
188
189 /** sets the RX path to use, returns true if successful and false otherwise */
190 bool setTxAntenna(const std::string &ant, size_t chan = 0);
191
192 /* return the used RX path */
193 std::string getTxAntenna(size_t chan = 0);
194
Pau Espin Pedrol0fc20d12018-04-24 17:48:52 +0200195 /** return whether user drives synchronization of Tx/Rx of USRP */
196 bool requiresRadioAlign();
197
Pau Espin Pedrole564f0f2018-04-24 18:43:51 +0200198 /** return whether user drives synchronization of Tx/Rx of USRP */
199 virtual GSM::Time minLatency();
200
dburgessb3a0ca42011-10-12 07:44:40 +0000201 /** Return internal status values */
Thomas Tsou204a9f12013-10-29 18:34:16 -0400202 inline double getTxFreq(size_t chan = 0) { return 0; }
203 inline double getRxFreq(size_t chan = 0) { return 0; }
204 inline double getSampleRate() { return actualSampleRate; }
dburgessb3a0ca42011-10-12 07:44:40 +0000205 inline double numberRead() { return samplesRead; }
Thomas Tsou204a9f12013-10-29 18:34:16 -0400206 inline double numberWritten() { return samplesWritten; }
dburgessb3a0ca42011-10-12 07:44:40 +0000207};
208
209#endif // _USRP_DEVICE_H_