dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 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 Tsou | c064124 | 2013-10-11 14:55:31 -0400 | [diff] [blame] | 24 | #include <usrp/usrp_standard.h> |
| 25 | #include <usrp/usrp_bytesex.h> |
| 26 | #include <usrp/usrp_prims.h> |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 27 | #include <sys/time.h> |
| 28 | #include <math.h> |
| 29 | #include <string> |
| 30 | #include <iostream> |
| 31 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 32 | #include <boost/shared_ptr.hpp> |
| 33 | typedef boost::shared_ptr<usrp_standard_tx> usrp_standard_tx_sptr; |
| 34 | typedef boost::shared_ptr<usrp_standard_rx> usrp_standard_rx_sptr; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 35 | |
| 36 | /** A class to handle a USRP rev 4, with a two RFX900 daughterboards */ |
| 37 | class USRPDevice: public RadioDevice { |
| 38 | |
| 39 | private: |
| 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.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 45 | |
| 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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 51 | 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 Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 78 | |
| 79 | long long pingOffset; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 80 | unsigned long hi32Timestamp; |
| 81 | unsigned long lastPktTimestamp; |
| 82 | |
| 83 | double rxGain; |
| 84 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 85 | #ifdef SWLOOPBACK |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 86 | short loopbackBuffer[1000000]; |
| 87 | int loopbackBufferSize; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 88 | double samplePeriod; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 89 | |
| 90 | struct timeval startTime; |
| 91 | struct timeval lastReadTime; |
| 92 | bool firstRead; |
| 93 | #endif |
| 94 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 95 | public: |
| 96 | |
| 97 | /** Object constructor */ |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 98 | 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); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 101 | |
| 102 | /** Instantiate the USRP */ |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 103 | int open(const std::string &, int, bool); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 104 | |
| 105 | /** Start the USRP */ |
| 106 | bool start(); |
| 107 | |
| 108 | /** Stop the USRP */ |
| 109 | bool stop(); |
| 110 | |
kurtis.heimerl | d936400 | 2011-11-26 03:17:15 +0000 | [diff] [blame] | 111 | /** Set priority not supported */ |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 112 | void setPriority(float prio = 0.5) { } |
kurtis.heimerl | d936400 | 2011-11-26 03:17:15 +0000 | [diff] [blame] | 113 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 114 | enum TxWindowType getWindowType() { return TX_WINDOW_USRP1; } |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 115 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 116 | /** |
| 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 Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 126 | int readSamples(std::vector<short *> &buf, int len, bool *overrun, |
| 127 | TIMESTAMP timestamp = 0xffffffff, bool *underrun = NULL, |
| 128 | unsigned *RSSI = NULL); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 129 | /** |
| 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 Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 138 | int writeSamples(std::vector<short *> &bufs, int len, bool *underrun, |
| 139 | TIMESTAMP timestamp = 0xffffffff, bool isControl = false); |
| 140 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 141 | /** Update the alignment between the read and write timestamps */ |
| 142 | bool updateAlignment(TIMESTAMP timestamp); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 143 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 144 | /** Set the transmitter frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 145 | bool setTxFreq(double wFreq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 146 | |
| 147 | /** Set the receiver frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 148 | bool setRxFreq(double wFreq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 149 | |
| 150 | /** Returns the starting write Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 151 | TIMESTAMP initialWriteTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 152 | |
| 153 | /** Returns the starting read Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 154 | TIMESTAMP initialReadTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 155 | |
| 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 Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 163 | double setRxGain(double dB, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 164 | |
| 165 | /** get the current receive gain */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 166 | double getRxGain(size_t chan = 0) { return rxGain; } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 167 | |
| 168 | /** return maximum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 169 | double maxRxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 170 | |
| 171 | /** return minimum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 172 | double minRxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 173 | |
| 174 | /** sets the transmit chan gain, returns the gain setting **/ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 175 | double setTxGain(double dB, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 176 | |
| 177 | /** return maximum Tx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 178 | double maxTxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 179 | |
| 180 | /** return minimum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 181 | double minTxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 182 | |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 183 | /** 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 Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 195 | /** return whether user drives synchronization of Tx/Rx of USRP */ |
| 196 | bool requiresRadioAlign(); |
| 197 | |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 198 | /** return whether user drives synchronization of Tx/Rx of USRP */ |
| 199 | virtual GSM::Time minLatency(); |
| 200 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 201 | /** Return internal status values */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 202 | 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; } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 205 | inline double numberRead() { return samplesRead; } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 206 | inline double numberWritten() { return samplesWritten; } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | #endif // _USRP_DEVICE_H_ |