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