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 |
| 57 | |
| 58 | double actualSampleRate; ///< the actual USRP sampling rate |
| 59 | unsigned int decimRate; ///< the USRP decimation rate |
| 60 | |
| 61 | unsigned long long samplesRead; ///< number of samples read from USRP |
| 62 | unsigned long long samplesWritten; ///< number of samples sent to USRP |
| 63 | |
| 64 | bool started; ///< flag indicates USRP has started |
| 65 | bool skipRx; ///< set if USRP is transmit-only. |
| 66 | |
| 67 | static const unsigned int currDataSize_log2 = 21; |
| 68 | static const unsigned long currDataSize = (1 << currDataSize_log2); |
| 69 | short *data; |
| 70 | unsigned long dataStart; |
| 71 | unsigned long dataEnd; |
| 72 | TIMESTAMP timeStart; |
| 73 | TIMESTAMP timeEnd; |
| 74 | bool isAligned; |
| 75 | |
| 76 | Mutex writeLock; |
| 77 | |
| 78 | short *currData; ///< internal data buffer when reading from USRP |
| 79 | TIMESTAMP currTimestamp; ///< timestamp of internal data buffer |
| 80 | unsigned currLen; ///< size of internal data buffer |
| 81 | |
| 82 | TIMESTAMP timestampOffset; ///< timestamp offset b/w Tx and Rx blocks |
| 83 | TIMESTAMP latestWriteTimestamp; ///< timestamp of most recent ping command |
| 84 | TIMESTAMP pingTimestamp; ///< timestamp of most recent ping response |
| 85 | static const TIMESTAMP PINGOFFSET = 272; ///< undetermined delay b/w ping response timestamp and true receive timestamp |
| 86 | unsigned long hi32Timestamp; |
| 87 | unsigned long lastPktTimestamp; |
| 88 | |
| 89 | double rxGain; |
| 90 | |
| 91 | #ifdef SWLOOPBACK |
| 92 | short loopbackBuffer[1000000]; |
| 93 | int loopbackBufferSize; |
| 94 | double samplePeriod; |
| 95 | |
| 96 | struct timeval startTime; |
| 97 | struct timeval lastReadTime; |
| 98 | bool firstRead; |
| 99 | #endif |
| 100 | |
| 101 | /** Mess of constants used to control various hardware on the USRP */ |
| 102 | static const unsigned POWER_UP = (1 << 7); |
| 103 | static const unsigned RX_TXN = (1 << 6); |
| 104 | static const unsigned RX2_RX1N = (1 << 6); |
| 105 | static const unsigned ENABLE = (1 << 5); |
| 106 | static const unsigned PLL_LOCK_DETECT = (1 << 2); |
| 107 | |
| 108 | static const unsigned SPI_ENABLE_TX_A = 0x10; |
| 109 | static const unsigned SPI_ENABLE_RX_A = 0x20; |
| 110 | static const unsigned SPI_ENABLE_TX_B = 0x40; |
| 111 | static const unsigned SPI_ENABLE_RX_B = 0x80; |
| 112 | |
| 113 | static const unsigned SPI_FMT_MSB = (0 << 7); |
| 114 | static const unsigned SPI_FMT_HDR_0 = (0 << 5); |
| 115 | |
| 116 | static const float LO_OFFSET; |
| 117 | //static const float LO_OFFSET = 4.0e6; |
| 118 | |
| 119 | static const unsigned R_DIV = 16; |
| 120 | static const unsigned P = 1; |
| 121 | static const unsigned CP2 = 7; |
| 122 | static const unsigned CP1 = 7; |
| 123 | static const unsigned DIVSEL = 0; |
| 124 | unsigned DIV2; // changes with GSM band |
| 125 | unsigned freq_mult; // changes with GSM band |
| 126 | static const unsigned CPGAIN = 0; |
| 127 | |
| 128 | // R-Register Common Values |
| 129 | static const unsigned R_RSV = 0; // bits 23,22 |
| 130 | static const unsigned BSC = 3; // bits 21,20 Div by 8 to be safe |
| 131 | static const unsigned TEST = 0; // bit 19 |
| 132 | static const unsigned LDP = 1; // bit 18 |
| 133 | static const unsigned ABP = 0; // bit 17,16 3ns |
| 134 | |
| 135 | // N-Register Common Values |
| 136 | static const unsigned N_RSV = 0; // bit 7 |
| 137 | |
| 138 | // Control Register Common Values |
| 139 | static const unsigned PD = 0; // bits 21,20 Normal operation |
| 140 | static const unsigned PL = 0; // bits 13,12 11mA |
| 141 | static const unsigned MTLD = 1; // bit 11 enabled |
| 142 | static const unsigned CPG = 0; // bit 10 CP setting 1 |
| 143 | static const unsigned CP3S = 0; // bit 9 Normal |
| 144 | static const unsigned PDP = 1; // bit 8 Positive |
| 145 | static const unsigned MUXOUT = 1;// bits 7:5 Digital Lock Detect |
| 146 | static const unsigned CR = 0; // bit 4 Normal |
| 147 | static const unsigned PC = 1; // bits 3,2 Core power 10mA |
| 148 | |
| 149 | // ATR register value |
| 150 | static const int FR_ATR_MASK_0 = 20; |
| 151 | static const int FR_ATR_TXVAL_0 = 21; |
| 152 | static const int FR_ATR_RXVAL_0 = 22; |
| 153 | |
| 154 | /** Compute register values to tune daughterboard to desired frequency */ |
| 155 | bool compute_regs(double freq, |
| 156 | unsigned *R, |
| 157 | unsigned *control, |
| 158 | unsigned *N, |
| 159 | double *actual_freq); |
| 160 | |
| 161 | /** Set the transmission frequency */ |
| 162 | bool tx_setFreq(double freq, double *actual_freq); |
| 163 | |
| 164 | /** Set the receiver frequency */ |
| 165 | bool rx_setFreq(double freq, double *actual_freq); |
| 166 | |
| 167 | public: |
| 168 | |
| 169 | /** Object constructor */ |
| 170 | USRPDevice (double _desiredSampleRate); |
| 171 | |
| 172 | /** Instantiate the USRP */ |
| 173 | bool make(bool skipRx = false); |
| 174 | |
| 175 | /** Start the USRP */ |
| 176 | bool start(); |
| 177 | |
| 178 | /** Stop the USRP */ |
| 179 | bool stop(); |
| 180 | |
| 181 | /** |
| 182 | Read samples from the USRP. |
| 183 | @param buf preallocated buf to contain read result |
| 184 | @param len number of samples desired |
| 185 | @param overrun Set if read buffer has been overrun, e.g. data not being read fast enough |
| 186 | @param timestamp The timestamp of the first samples to be read |
| 187 | @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough |
| 188 | @param RSSI The received signal strength of the read result |
| 189 | @return The number of samples actually read |
| 190 | */ |
| 191 | int readSamples(short *buf, int len, bool *overrun, |
| 192 | TIMESTAMP timestamp = 0xffffffff, |
| 193 | bool *underrun = NULL, |
| 194 | unsigned *RSSI = NULL); |
| 195 | /** |
| 196 | Write samples to the USRP. |
| 197 | @param buf Contains the data to be written. |
| 198 | @param len number of samples to write. |
| 199 | @param underrun Set if USRP does not have data to transmit, e.g. data not being sent fast enough |
| 200 | @param timestamp The timestamp of the first sample of the data buffer. |
| 201 | @param isControl Set if data is a control packet, e.g. a ping command |
| 202 | @return The number of samples actually written |
| 203 | */ |
| 204 | int writeSamples(short *buf, int len, bool *underrun, |
| 205 | TIMESTAMP timestamp = 0xffffffff, |
| 206 | bool isControl = false); |
| 207 | |
| 208 | /** Update the alignment between the read and write timestamps */ |
| 209 | bool updateAlignment(TIMESTAMP timestamp); |
| 210 | |
| 211 | /** Set the transmitter frequency */ |
| 212 | bool setTxFreq(double wFreq); |
| 213 | |
| 214 | /** Set the receiver frequency */ |
| 215 | bool setRxFreq(double wFreq); |
| 216 | |
| 217 | /** Returns the starting write Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame^] | 218 | TIMESTAMP initialWriteTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 219 | |
| 220 | /** Returns the starting read Timestamp*/ |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame^] | 221 | TIMESTAMP initialReadTimestamp(void) { return 20000;} |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 222 | |
| 223 | /** returns the full-scale transmit amplitude **/ |
| 224 | double fullScaleInputValue() {return 13500.0;} |
| 225 | |
| 226 | /** returns the full-scale receive amplitude **/ |
| 227 | double fullScaleOutputValue() {return 9450.0;} |
| 228 | |
| 229 | /** sets the receive chan gain, returns the gain setting **/ |
| 230 | double setRxGain(double dB); |
| 231 | |
| 232 | /** get the current receive gain */ |
| 233 | double getRxGain(void) {return rxGain;} |
| 234 | |
| 235 | /** return maximum Rx Gain **/ |
| 236 | double maxRxGain(void) {return 97.0;} |
| 237 | |
| 238 | /** return minimum Rx Gain **/ |
| 239 | double minRxGain(void) {return 7.0;} |
| 240 | |
| 241 | /** sets the transmit chan gain, returns the gain setting **/ |
| 242 | double setTxGain(double dB); |
| 243 | |
| 244 | /** return maximum Tx Gain **/ |
| 245 | double maxTxGain(void) {return 0.0;} |
| 246 | |
| 247 | /** return minimum Rx Gain **/ |
| 248 | double minTxGain(void) {return -20.0;} |
| 249 | |
| 250 | |
| 251 | /** Return internal status values */ |
| 252 | inline double getTxFreq() { return 0;} |
| 253 | inline double getRxFreq() { return 0;} |
| 254 | inline double getSampleRate() {return actualSampleRate;} |
| 255 | inline double numberRead() { return samplesRead; } |
| 256 | inline double numberWritten() { return samplesWritten;} |
| 257 | |
| 258 | }; |
| 259 | |
| 260 | #endif // _USRP_DEVICE_H_ |
| 261 | |