dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2009 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This software is distributed under the terms of the GNU Affero Public License. |
| 5 | * See the COPYING file in the main directory for details. |
| 6 | * |
| 7 | * This use of this software may be subject to additional restrictions. |
| 8 | * See the LEGAL file in the main directory for details. |
| 9 | |
| 10 | This program is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU Affero General Public License as published by |
| 12 | the Free Software Foundation, either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU Affero General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU Affero General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | */ |
| 24 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 25 | #include "radioInterface.h" |
| 26 | #include <Logger.h> |
| 27 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 28 | bool started = false; |
| 29 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 30 | /* Device side buffers */ |
| 31 | static short rx_buf[OUTCHUNK * 2 * 2]; |
| 32 | static short tx_buf[INCHUNK * 2 * 2]; |
| 33 | |
| 34 | /* Complex float to short conversion */ |
| 35 | static void floatToShort(short *out, float *in, int num) |
| 36 | { |
| 37 | for (int i = 0; i < num; i++) { |
| 38 | out[2 * i + 0] = (short) in[2 * i + 0]; |
| 39 | out[2 * i + 1] = (short) in[2 * i + 1]; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /* Complex short to float conversion */ |
| 44 | static void shortToFloat(float *out, short *in, int num) |
| 45 | { |
| 46 | for (int i = 0; i < num; i++) { |
| 47 | out[2 * i + 0] = (float) in[2 * i + 0]; |
| 48 | out[2 * i + 1] = (float) in[2 * i + 1]; |
| 49 | } |
| 50 | } |
| 51 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 52 | RadioInterface::RadioInterface(RadioDevice *wRadio, |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 53 | int wReceiveOffset, |
Thomas Tsou | 312e387 | 2013-04-08 13:45:55 -0400 | [diff] [blame] | 54 | int wSPS, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 55 | GSM::Time wStartTime) |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 56 | : underrun(false), sendCursor(0), rcvCursor(0), mOn(false), |
| 57 | mRadio(wRadio), receiveOffset(wReceiveOffset), |
Thomas Tsou | 312e387 | 2013-04-08 13:45:55 -0400 | [diff] [blame] | 58 | samplesPerSymbol(wSPS), powerScaling(1.0), |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 59 | loadTest(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 60 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 61 | mClock.set(wStartTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 62 | } |
| 63 | |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 64 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 65 | RadioInterface::~RadioInterface(void) { |
| 66 | if (rcvBuffer!=NULL) delete rcvBuffer; |
| 67 | //mReceiveFIFO.clear(); |
| 68 | } |
| 69 | |
| 70 | double RadioInterface::fullScaleInputValue(void) { |
| 71 | return mRadio->fullScaleInputValue(); |
| 72 | } |
| 73 | |
| 74 | double RadioInterface::fullScaleOutputValue(void) { |
| 75 | return mRadio->fullScaleOutputValue(); |
| 76 | } |
| 77 | |
| 78 | |
kurtis.heimerl | 58d6a01 | 2011-11-26 03:17:38 +0000 | [diff] [blame] | 79 | void RadioInterface::setPowerAttenuation(double atten) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 80 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 81 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 82 | |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 83 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - atten); |
| 84 | digAtten = atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 85 | |
| 86 | if (digAtten < 1.0) |
| 87 | powerScaling = 1.0; |
| 88 | else |
| 89 | powerScaling = 1.0/sqrt(pow(10, (digAtten/10.0))); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 92 | int RadioInterface::radioifyVector(signalVector &wVector, |
| 93 | float *retVector, |
| 94 | float scale, |
| 95 | bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 96 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 97 | int i; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 98 | signalVector::iterator itr = wVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 99 | |
| 100 | if (zero) { |
| 101 | memset(retVector, 0, wVector.size() * 2 * sizeof(float)); |
| 102 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 103 | } |
| 104 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 105 | for (i = 0; i < wVector.size(); i++) { |
| 106 | retVector[2 * i + 0] = itr->real() * scale; |
| 107 | retVector[2 * i + 1] = itr->imag() * scale; |
| 108 | itr++; |
| 109 | } |
| 110 | |
| 111 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 112 | } |
| 113 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 114 | int RadioInterface::unRadioifyVector(float *floatVector, |
| 115 | signalVector& newVector) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 116 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 117 | int i; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 118 | signalVector::iterator itr = newVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 119 | |
| 120 | for (i = 0; i < newVector.size(); i++) { |
| 121 | *itr++ = Complex<float>(floatVector[2 * i + 0], |
| 122 | floatVector[2 * i + 1]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 123 | } |
| 124 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 125 | return newVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | bool RadioInterface::tuneTx(double freq) |
| 129 | { |
| 130 | return mRadio->setTxFreq(freq); |
| 131 | } |
| 132 | |
| 133 | bool RadioInterface::tuneRx(double freq) |
| 134 | { |
| 135 | return mRadio->setRxFreq(freq); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void RadioInterface::start() |
| 140 | { |
| 141 | LOG(INFO) << "starting radio interface..."; |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 142 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 143 | mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 144 | (void*)this); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 145 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 146 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 147 | readTimestamp = mRadio->initialReadTimestamp(); |
| 148 | mRadio->start(); |
| 149 | LOG(DEBUG) << "Radio started"; |
| 150 | mRadio->updateAlignment(writeTimestamp-10000); |
| 151 | mRadio->updateAlignment(writeTimestamp-10000); |
| 152 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 153 | sendBuffer = new float[2*2*INCHUNK*samplesPerSymbol]; |
| 154 | rcvBuffer = new float[2*2*OUTCHUNK*samplesPerSymbol]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 155 | |
| 156 | mOn = true; |
| 157 | |
| 158 | } |
| 159 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 160 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 161 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 162 | { |
| 163 | while (1) { |
| 164 | radioInterface->alignRadio(); |
| 165 | pthread_testcancel(); |
| 166 | } |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | void RadioInterface::alignRadio() { |
| 171 | sleep(60); |
| 172 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 173 | } |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 174 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 175 | |
| 176 | void RadioInterface::driveTransmitRadio(signalVector &radioBurst, bool zeroBurst) { |
| 177 | |
| 178 | if (!mOn) return; |
| 179 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 180 | radioifyVector(radioBurst, sendBuffer + 2 * sendCursor, powerScaling, zeroBurst); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 181 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 182 | sendCursor += radioBurst.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 183 | |
| 184 | pushBuffer(); |
| 185 | } |
| 186 | |
| 187 | void RadioInterface::driveReceiveRadio() { |
| 188 | |
| 189 | if (!mOn) return; |
| 190 | |
| 191 | if (mReceiveFIFO.size() > 8) return; |
| 192 | |
| 193 | pullBuffer(); |
| 194 | |
| 195 | GSM::Time rcvClock = mClock.get(); |
| 196 | rcvClock.decTN(receiveOffset); |
| 197 | unsigned tN = rcvClock.TN(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 198 | int rcvSz = rcvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 199 | int readSz = 0; |
| 200 | const int symbolsPerSlot = gSlotLen + 8; |
| 201 | |
| 202 | // while there's enough data in receive buffer, form received |
| 203 | // GSM bursts and pass up to Transceiver |
| 204 | // Using the 157-156-156-156 symbols per timeslot format. |
| 205 | while (rcvSz > (symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol) { |
| 206 | signalVector rxVector((symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol); |
| 207 | unRadioifyVector(rcvBuffer+readSz*2,rxVector); |
| 208 | GSM::Time tmpTime = rcvClock; |
| 209 | if (rcvClock.FN() >= 0) { |
| 210 | //LOG(DEBUG) << "FN: " << rcvClock.FN(); |
| 211 | radioVector *rxBurst = NULL; |
| 212 | if (!loadTest) |
| 213 | rxBurst = new radioVector(rxVector,tmpTime); |
| 214 | else { |
| 215 | if (tN % 4 == 0) |
| 216 | rxBurst = new radioVector(*finalVec9,tmpTime); |
| 217 | else |
| 218 | rxBurst = new radioVector(*finalVec,tmpTime); |
| 219 | } |
| 220 | mReceiveFIFO.put(rxBurst); |
| 221 | } |
| 222 | mClock.incTN(); |
| 223 | rcvClock.incTN(); |
| 224 | //if (mReceiveFIFO.size() >= 16) mReceiveFIFO.wait(8); |
| 225 | //LOG(DEBUG) << "receiveFIFO: wrote radio vector at time: " << mClock.get() << ", new size: " << mReceiveFIFO.size() ; |
| 226 | readSz += (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol; |
| 227 | rcvSz -= (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol; |
| 228 | |
| 229 | tN = rcvClock.TN(); |
| 230 | } |
| 231 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 232 | if (readSz > 0) { |
| 233 | rcvCursor -= readSz; |
| 234 | memmove(rcvBuffer,rcvBuffer+2*readSz,sizeof(float) * 2 * rcvCursor); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 235 | } |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | bool RadioInterface::isUnderrun() |
| 239 | { |
| 240 | bool retVal = underrun; |
| 241 | underrun = false; |
| 242 | |
| 243 | return retVal; |
| 244 | } |
| 245 | |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 246 | double RadioInterface::setRxGain(double dB) |
| 247 | { |
| 248 | if (mRadio) |
| 249 | return mRadio->setRxGain(dB); |
| 250 | else |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | double RadioInterface::getRxGain() |
| 255 | { |
| 256 | if (mRadio) |
| 257 | return mRadio->getRxGain(); |
| 258 | else |
| 259 | return -1; |
| 260 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 261 | |
| 262 | /* Receive a timestamped chunk from the device */ |
| 263 | void RadioInterface::pullBuffer() |
| 264 | { |
| 265 | bool local_underrun; |
| 266 | |
| 267 | /* Read samples. Fail if we don't get what we want. */ |
| 268 | int num_rd = mRadio->readSamples(rx_buf, OUTCHUNK, &overrun, |
| 269 | readTimestamp, &local_underrun); |
| 270 | |
| 271 | LOG(DEBUG) << "Rx read " << num_rd << " samples from device"; |
| 272 | assert(num_rd == OUTCHUNK); |
| 273 | |
| 274 | underrun |= local_underrun; |
| 275 | readTimestamp += (TIMESTAMP) num_rd; |
| 276 | |
| 277 | shortToFloat(rcvBuffer + 2 * rcvCursor, rx_buf, num_rd); |
| 278 | rcvCursor += num_rd; |
| 279 | } |
| 280 | |
| 281 | /* Send timestamped chunk to the device with arbitrary size */ |
| 282 | void RadioInterface::pushBuffer() |
| 283 | { |
| 284 | if (sendCursor < INCHUNK) |
| 285 | return; |
| 286 | |
| 287 | floatToShort(tx_buf, sendBuffer, sendCursor); |
| 288 | |
| 289 | /* Write samples. Fail if we don't get what we want. */ |
| 290 | int num_smpls = mRadio->writeSamples(tx_buf, |
| 291 | sendCursor, |
| 292 | &underrun, |
| 293 | writeTimestamp); |
| 294 | assert(num_smpls == sendCursor); |
| 295 | |
| 296 | writeTimestamp += (TIMESTAMP) num_smpls; |
| 297 | sendCursor = 0; |
| 298 | } |