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 | |
| 24 | #ifdef HAVE_LIBUSRP_3_3 // [ |
| 25 | # include <usrp/usrp_standard.h> |
| 26 | # include <usrp/usrp_bytesex.h> |
| 27 | # include <usrp/usrp_prims.h> |
| 28 | #else // HAVE_LIBUSRP_3_3 ][ |
| 29 | # include "usrp_standard.h" |
| 30 | # include "usrp_bytesex.h" |
| 31 | # include "usrp_prims.h" |
| 32 | #endif // !HAVE_LIBUSRP_3_3 ] |
| 33 | #include <sys/time.h> |
| 34 | #include <math.h> |
| 35 | #include <string> |
| 36 | #include <iostream> |
| 37 | |
| 38 | |
| 39 | /** Define types which are not defined in libusrp-3.1 */ |
| 40 | #ifndef HAVE_LIBUSRP_3_2 |
| 41 | #include <boost/shared_ptr.hpp> |
| 42 | typedef boost::shared_ptr<usrp_standard_tx> usrp_standard_tx_sptr; |
| 43 | typedef boost::shared_ptr<usrp_standard_rx> usrp_standard_rx_sptr; |
| 44 | #endif // HAVE_LIBUSRP_3_2 |
| 45 | |
| 46 | |
| 47 | |
| 48 | /** A class to handle a USRP rev 4, with a two RFX900 daughterboards */ |
| 49 | class USRPDevice: public RadioDevice { |
| 50 | |
| 51 | private: |
| 52 | |
| 53 | static const double masterClockRate; ///< the USRP clock rate |
| 54 | double desiredSampleRate; ///< the desired sampling rate |
| 55 | usrp_standard_rx_sptr m_uRx; ///< the USRP receiver |
| 56 | usrp_standard_tx_sptr m_uTx; ///< the USRP transmitter |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 57 | |
| 58 | db_base_sptr m_dbRx; ///< rx daughterboard |
| 59 | db_base_sptr m_dbTx; ///< tx daughterboard |
| 60 | usrp_subdev_spec rxSubdevSpec; |
| 61 | usrp_subdev_spec txSubdevSpec; |
| 62 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame^] | 63 | int sps; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 64 | double actualSampleRate; ///< the actual USRP sampling rate |
| 65 | unsigned int decimRate; ///< the USRP decimation rate |
| 66 | |
| 67 | unsigned long long samplesRead; ///< number of samples read from USRP |
| 68 | unsigned long long samplesWritten; ///< number of samples sent to USRP |
| 69 | |
| 70 | bool started; ///< flag indicates USRP has started |
| 71 | bool skipRx; ///< set if USRP is transmit-only. |
| 72 | |
| 73 | static const unsigned int currDataSize_log2 = 21; |
| 74 | static const unsigned long currDataSize = (1 << currDataSize_log2); |
| 75 | short *data; |
| 76 | unsigned long dataStart; |
| 77 | unsigned long dataEnd; |
| 78 | TIMESTAMP timeStart; |
| 79 | TIMESTAMP timeEnd; |
| 80 | bool isAligned; |
| 81 | |
| 82 | Mutex writeLock; |
| 83 | |
| 84 | short *currData; ///< internal data buffer when reading from USRP |
| 85 | TIMESTAMP currTimestamp; ///< timestamp of internal data buffer |
| 86 | unsigned currLen; ///< size of internal data buffer |
| 87 | |
| 88 | TIMESTAMP timestampOffset; ///< timestamp offset b/w Tx and Rx blocks |
| 89 | TIMESTAMP latestWriteTimestamp; ///< timestamp of most recent ping command |
| 90 | TIMESTAMP pingTimestamp; ///< timestamp of most recent ping response |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame^] | 91 | |
| 92 | long long pingOffset; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 93 | unsigned long hi32Timestamp; |
| 94 | unsigned long lastPktTimestamp; |
| 95 | |
| 96 | double rxGain; |
| 97 | |
| 98 | #ifdef SWLOOPBACK |
| 99 | short loopbackBuffer[1000000]; |
| 100 | int loopbackBufferSize; |
| 101 | double samplePeriod; |
| 102 | |
| 103 | struct timeval startTime; |
| 104 | struct timeval lastReadTime; |
| 105 | bool firstRead; |
| 106 | #endif |
| 107 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 108 | /** Set the transmission frequency */ |
| 109 | bool tx_setFreq(double freq, double *actual_freq); |
| 110 | |
| 111 | /** Set the receiver frequency */ |
| 112 | bool rx_setFreq(double freq, double *actual_freq); |
| 113 | |
| 114 | public: |
| 115 | |
| 116 | /** Object constructor */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 117 | USRPDevice(int sps, bool skipRx); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 118 | |
| 119 | /** Instantiate the USRP */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 120 | int open(const std::string &); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 121 | |
| 122 | /** Start the USRP */ |
| 123 | bool start(); |
| 124 | |
| 125 | /** Stop the USRP */ |
| 126 | bool stop(); |
| 127 | |
kurtis.heimerl | d936400 | 2011-11-26 03:17:15 +0000 | [diff] [blame] | 128 | /** Set priority not supported */ |
| 129 | void setPriority() { return; } |
| 130 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 131 | enum TxWindowType getWindowType() { return TX_WINDOW_USRP1; } |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 132 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 133 | /** |
| 134 | Read samples from the USRP. |
| 135 | @param buf preallocated buf to contain read result |
| 136 | @param len number of samples desired |
| 137 | @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough |
| 138 | @param timestamp The timestamp of the first samples to be read |
| 139 | @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough |
| 140 | @param RSSI The received signal strength of the read result |
| 141 | @return The number of samples actually read |
| 142 | */ |
| 143 | int readSamples(short *buf, int len, bool *overrun, |
| 144 | TIMESTAMP timestamp = 0xffffffff, |
| 145 | bool *underrun = NULL, |
| 146 | unsigned *RSSI = NULL); |
| 147 | /** |
| 148 | Write samples to the USRP. |
| 149 | @param buf Contains the data to be written. |
| 150 | @param len number of samples to write. |
| 151 | @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough |
| 152 | @param timestamp The timestamp of the first sample of the data buffer. |
| 153 | @param isControl Set if data is a control packet, e.g. a ping command |
| 154 | @return The number of samples actually written |
| 155 | */ |
| 156 | int writeSamples(short *buf, int len, bool *underrun, |
| 157 | TIMESTAMP timestamp = 0xffffffff, |
| 158 | bool isControl = false); |
| 159 | |
| 160 | /** Update the alignment between the read and write timestamps */ |
| 161 | bool updateAlignment(TIMESTAMP timestamp); |
| 162 | |
| 163 | /** Set the transmitter frequency */ |
| 164 | bool setTxFreq(double wFreq); |
| 165 | |
| 166 | /** Set the receiver frequency */ |
| 167 | bool setRxFreq(double wFreq); |
| 168 | |
| 169 | /** Returns the starting write Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 170 | TIMESTAMP initialWriteTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 171 | |
| 172 | /** Returns the starting read Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 173 | TIMESTAMP initialReadTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 174 | |
| 175 | /** returns the full-scale transmit amplitude **/ |
| 176 | double fullScaleInputValue() {return 13500.0;} |
| 177 | |
| 178 | /** returns the full-scale receive amplitude **/ |
| 179 | double fullScaleOutputValue() {return 9450.0;} |
| 180 | |
| 181 | /** sets the receive chan gain, returns the gain setting **/ |
| 182 | double setRxGain(double dB); |
| 183 | |
| 184 | /** get the current receive gain */ |
| 185 | double getRxGain(void) {return rxGain;} |
| 186 | |
| 187 | /** return maximum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 188 | double maxRxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 189 | |
| 190 | /** return minimum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 191 | double minRxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 192 | |
| 193 | /** sets the transmit chan gain, returns the gain setting **/ |
| 194 | double setTxGain(double dB); |
| 195 | |
| 196 | /** return maximum Tx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 197 | double maxTxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 198 | |
| 199 | /** return minimum Rx Gain **/ |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 200 | double minTxGain(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | /** Return internal status values */ |
| 204 | inline double getTxFreq() { return 0;} |
| 205 | inline double getRxFreq() { return 0;} |
| 206 | inline double getSampleRate() {return actualSampleRate;} |
| 207 | inline double numberRead() { return samplesRead; } |
| 208 | inline double numberWritten() { return samplesWritten;} |
| 209 | |
| 210 | }; |
| 211 | |
| 212 | #endif // _USRP_DEVICE_H_ |
| 213 | |