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 | |
| 16 | |
| 17 | #include "sigProcLib.h" |
| 18 | #include "GSMCommon.h" |
| 19 | #include "LinkedLists.h" |
| 20 | #include "radioDevice.h" |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 21 | #include "radioVector.h" |
| 22 | #include "radioClock.h" |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame^] | 23 | #include "Resampler.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 24 | |
Alexander Chemeris | d734e2d | 2013-06-16 14:30:58 +0400 | [diff] [blame] | 25 | static const unsigned gSlotLen = 148; ///< number of symbols per slot, not counting guard periods |
| 26 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 27 | /** class to interface the transceiver with the USRP */ |
| 28 | class RadioInterface { |
| 29 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 30 | protected: |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 31 | |
| 32 | Thread mAlignRadioServiceLoopThread; ///< thread that synchronizes transmit and receive sections |
| 33 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 34 | std::vector<VectorFIFO> mReceiveFIFO; ///< FIFO that holds receive bursts |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 35 | |
| 36 | RadioDevice *mRadio; ///< the USRP object |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 37 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 38 | size_t mSPSTx; |
| 39 | size_t mSPSRx; |
| 40 | size_t mChans; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame^] | 41 | size_t mMIMO; |
| 42 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 43 | std::vector<signalVector *> sendBuffer; |
| 44 | std::vector<signalVector *> recvBuffer; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 45 | unsigned sendCursor; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 46 | unsigned recvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 47 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 48 | std::vector<short *> convertRecvBuffer; |
| 49 | std::vector<short *> convertSendBuffer; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 50 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 51 | bool underrun; ///< indicates writes to USRP are too slow |
| 52 | bool overrun; ///< indicates reads from USRP are too slow |
| 53 | TIMESTAMP writeTimestamp; ///< sample timestamp of next packet written to USRP |
| 54 | TIMESTAMP readTimestamp; ///< sample timestamp of next packet read from USRP |
| 55 | |
| 56 | RadioClock mClock; ///< the basestation clock! |
| 57 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 58 | int receiveOffset; ///< offset b/w transmit and receive GSM timestamps, in timeslots |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 59 | |
| 60 | bool mOn; ///< indicates radio is on |
| 61 | |
| 62 | double powerScaling; |
| 63 | |
| 64 | bool loadTest; |
| 65 | int mNumARFCNs; |
| 66 | signalVector *finalVec, *finalVec9; |
| 67 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 68 | private: |
| 69 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 70 | /** format samples to USRP */ |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 71 | int radioifyVector(signalVector &wVector, |
| 72 | float *floatVector, |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 73 | bool zero); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 74 | |
| 75 | /** format samples from USRP */ |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 76 | int unRadioifyVector(float *floatVector, signalVector &wVector); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 77 | |
| 78 | /** push GSM bursts into the transmit buffer */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 79 | virtual void pushBuffer(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 80 | |
| 81 | /** pull GSM bursts from the receive buffer */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 82 | virtual void pullBuffer(void); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 83 | |
| 84 | public: |
| 85 | |
| 86 | /** start the interface */ |
| 87 | void start(); |
| 88 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 89 | /** intialization */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 90 | virtual bool init(int type); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 91 | virtual void close(); |
| 92 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 93 | /** constructor */ |
| 94 | RadioInterface(RadioDevice* wRadio = NULL, |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame^] | 95 | size_t sps = 4, size_t chans = 1, size_t diversity = 1, |
| 96 | int receiveOffset = 3, GSM::Time wStartTime = GSM::Time(0)); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 97 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 98 | /** destructor */ |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 99 | virtual ~RadioInterface(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 100 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 101 | /** check for underrun, resets underrun value */ |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 102 | bool isUnderrun(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 103 | |
| 104 | /** return the receive FIFO */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 105 | VectorFIFO* receiveFIFO(size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 106 | |
| 107 | /** return the basestation clock */ |
| 108 | RadioClock* getClock(void) { return &mClock;}; |
| 109 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 110 | /** set transmit frequency */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 111 | bool tuneTx(double freq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 112 | |
| 113 | /** set receive frequency */ |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame^] | 114 | virtual bool tuneRx(double freq, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 115 | |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 116 | /** set receive gain */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 117 | double setRxGain(double dB, size_t chan = 0); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 118 | |
| 119 | /** get receive gain */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 120 | double getRxGain(size_t chan = 0); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 121 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 122 | /** drive transmission of GSM bursts */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 123 | void driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 124 | std::vector<bool> &zeros); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 125 | |
| 126 | /** drive reception of GSM bursts */ |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 127 | bool driveReceiveRadio(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 128 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 129 | void setPowerAttenuation(double atten, size_t chan = 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 130 | |
| 131 | /** returns the full-scale transmit amplitude **/ |
| 132 | double fullScaleInputValue(); |
| 133 | |
| 134 | /** returns the full-scale receive amplitude **/ |
| 135 | double fullScaleOutputValue(); |
| 136 | |
kurtis.heimerl | 6b495a5 | 2011-11-26 03:17:21 +0000 | [diff] [blame] | 137 | /** set thread priority on current thread */ |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 138 | void setPriority(float prio = 0.5) { mRadio->setPriority(prio); } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 139 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 140 | /** get transport window type of attached device */ |
| 141 | enum RadioDevice::TxWindowType getWindowType() { return mRadio->getWindowType(); } |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 142 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 143 | #if USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 144 | protected: |
| 145 | |
| 146 | /** drive synchronization of Tx/Rx of USRP */ |
| 147 | void alignRadio(); |
| 148 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 149 | friend void *AlignRadioServiceLoopAdapter(RadioInterface*); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 150 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 153 | #if USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 154 | /** synchronization thread loop */ |
| 155 | void *AlignRadioServiceLoopAdapter(RadioInterface*); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 156 | #endif |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 157 | |
| 158 | class RadioInterfaceResamp : public RadioInterface { |
| 159 | |
| 160 | private: |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 161 | signalVector *innerSendBuffer; |
| 162 | signalVector *outerSendBuffer; |
| 163 | signalVector *innerRecvBuffer; |
| 164 | signalVector *outerRecvBuffer; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 165 | |
| 166 | void pushBuffer(); |
| 167 | void pullBuffer(); |
| 168 | |
| 169 | public: |
| 170 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame^] | 171 | RadioInterfaceResamp(RadioDevice* wRadio, size_t wSPS = 4, size_t chans = 1); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 172 | |
| 173 | ~RadioInterfaceResamp(); |
| 174 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 175 | bool init(int type); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 176 | void close(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 177 | }; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame^] | 178 | |
| 179 | class RadioInterfaceDiversity : public RadioInterface { |
| 180 | public: |
| 181 | RadioInterfaceDiversity(RadioDevice* wRadio, |
| 182 | size_t sps = 4, size_t chans = 2); |
| 183 | |
| 184 | ~RadioInterfaceDiversity(); |
| 185 | |
| 186 | bool init(int type); |
| 187 | void close(); |
| 188 | bool tuneRx(double freq, size_t chan); |
| 189 | |
| 190 | private: |
| 191 | std::vector<Resampler *> dnsamplers; |
| 192 | std::vector<float> phases; |
| 193 | signalVector *outerRecvBuffer; |
| 194 | |
| 195 | bool mDiversity; |
| 196 | double mFreqSpacing; |
| 197 | |
| 198 | bool setupDiversityChannels(); |
| 199 | void pullBuffer(); |
| 200 | }; |