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 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 51 | int sps; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 52 | double actualSampleRate; ///< the actual USRP sampling rate |
| 53 | unsigned int decimRate; ///< the USRP decimation rate |
| 54 | |
| 55 | unsigned long long samplesRead; ///< number of samples read from USRP |
| 56 | unsigned long long samplesWritten; ///< number of samples sent to USRP |
| 57 | |
| 58 | bool started; ///< flag indicates USRP has started |
| 59 | bool skipRx; ///< set if USRP is transmit-only. |
| 60 | |
| 61 | static const unsigned int currDataSize_log2 = 21; |
| 62 | static const unsigned long currDataSize = (1 << currDataSize_log2); |
| 63 | short *data; |
| 64 | unsigned long dataStart; |
| 65 | unsigned long dataEnd; |
| 66 | TIMESTAMP timeStart; |
| 67 | TIMESTAMP timeEnd; |
| 68 | bool isAligned; |
| 69 | |
| 70 | Mutex writeLock; |
| 71 | |
| 72 | short *currData; ///< internal data buffer when reading from USRP |
| 73 | TIMESTAMP currTimestamp; ///< timestamp of internal data buffer |
| 74 | unsigned currLen; ///< size of internal data buffer |
| 75 | |
| 76 | TIMESTAMP timestampOffset; ///< timestamp offset b/w Tx and Rx blocks |
| 77 | TIMESTAMP latestWriteTimestamp; ///< timestamp of most recent ping command |
| 78 | TIMESTAMP pingTimestamp; ///< timestamp of most recent ping response |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 79 | |
| 80 | long long pingOffset; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 81 | unsigned long hi32Timestamp; |
| 82 | unsigned long lastPktTimestamp; |
| 83 | |
| 84 | double rxGain; |
| 85 | |
| 86 | #ifdef SWLOOPBACK |
| 87 | short loopbackBuffer[1000000]; |
| 88 | int loopbackBufferSize; |
| 89 | double samplePeriod; |
| 90 | |
| 91 | struct timeval startTime; |
| 92 | struct timeval lastReadTime; |
| 93 | bool firstRead; |
| 94 | #endif |
| 95 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 96 | public: |
| 97 | |
| 98 | /** Object constructor */ |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 99 | USRPDevice(size_t sps); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 100 | |
| 101 | /** Instantiate the USRP */ |
Alexander Chemeris | 50747dc | 2015-06-07 01:07:45 -0400 | [diff] [blame] | 102 | int open(const std::string &, bool, bool); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 103 | |
| 104 | /** Start the USRP */ |
| 105 | bool start(); |
| 106 | |
| 107 | /** Stop the USRP */ |
| 108 | bool stop(); |
| 109 | |
kurtis.heimerl | d936400 | 2011-11-26 03:17:15 +0000 | [diff] [blame] | 110 | /** Set priority not supported */ |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 111 | void setPriority(float prio = 0.5) { } |
kurtis.heimerl | d936400 | 2011-11-26 03:17:15 +0000 | [diff] [blame] | 112 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 113 | enum TxWindowType getWindowType() { return TX_WINDOW_USRP1; } |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 114 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 115 | /** |
| 116 | Read samples from the USRP. |
| 117 | @param buf preallocated buf to contain read result |
| 118 | @param len number of samples desired |
| 119 | @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough |
| 120 | @param timestamp The timestamp of the first samples to be read |
| 121 | @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough |
| 122 | @param RSSI The received signal strength of the read result |
| 123 | @return The number of samples actually read |
| 124 | */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 125 | int readSamples(std::vector<short *> &buf, int len, bool *overrun, |
| 126 | TIMESTAMP timestamp = 0xffffffff, bool *underrun = NULL, |
| 127 | unsigned *RSSI = NULL); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 128 | /** |
| 129 | Write samples to the USRP. |
| 130 | @param buf Contains the data to be written. |
| 131 | @param len number of samples to write. |
| 132 | @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough |
| 133 | @param timestamp The timestamp of the first sample of the data buffer. |
| 134 | @param isControl Set if data is a control packet, e.g. a ping command |
| 135 | @return The number of samples actually written |
| 136 | */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 137 | int writeSamples(std::vector<short *> &bufs, int len, bool *underrun, |
| 138 | TIMESTAMP timestamp = 0xffffffff, bool isControl = false); |
| 139 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 140 | /** Update the alignment between the read and write timestamps */ |
| 141 | bool updateAlignment(TIMESTAMP timestamp); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 142 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 143 | /** Set the transmitter frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 144 | bool setTxFreq(double wFreq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 145 | |
| 146 | /** Set the receiver frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 147 | bool setRxFreq(double wFreq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 148 | |
| 149 | /** Returns the starting write Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 150 | TIMESTAMP initialWriteTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 151 | |
| 152 | /** Returns the starting read Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 153 | TIMESTAMP initialReadTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 154 | |
| 155 | /** returns the full-scale transmit amplitude **/ |
| 156 | double fullScaleInputValue() {return 13500.0;} |
| 157 | |
| 158 | /** returns the full-scale receive amplitude **/ |
| 159 | double fullScaleOutputValue() {return 9450.0;} |
| 160 | |
| 161 | /** sets the receive chan gain, returns the gain setting **/ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 162 | double setRxGain(double dB, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 163 | |
| 164 | /** get the current receive gain */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 165 | double getRxGain(size_t chan = 0) { return rxGain; } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 166 | |
| 167 | /** return maximum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 168 | double maxRxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 169 | |
| 170 | /** return minimum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 171 | double minRxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 172 | |
| 173 | /** sets the transmit chan gain, returns the gain setting **/ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 174 | double setTxGain(double dB, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 175 | |
| 176 | /** return maximum Tx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 177 | double maxTxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 178 | |
| 179 | /** return minimum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 180 | double minTxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 181 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 182 | /** Return internal status values */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 183 | inline double getTxFreq(size_t chan = 0) { return 0; } |
| 184 | inline double getRxFreq(size_t chan = 0) { return 0; } |
| 185 | inline double getSampleRate() { return actualSampleRate; } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 186 | inline double numberRead() { return samplesRead; } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 187 | inline double numberWritten() { return samplesWritten; } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 188 | |
| 189 | }; |
| 190 | |
| 191 | #endif // _USRP_DEVICE_H_ |
| 192 | |