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