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 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 30 | RadioInterface::RadioInterface(RadioDevice *wRadio, |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 31 | int wReceiveOffset, |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 32 | int wRadioOversampling, |
| 33 | int wTransceiverOversampling, |
| 34 | GSM::Time wStartTime) |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 35 | : underrun(false), sendCursor(0), rcvCursor(0), mOn(false), |
| 36 | mRadio(wRadio), receiveOffset(wReceiveOffset), |
| 37 | samplesPerSymbol(wRadioOversampling), powerScaling(1.0), |
| 38 | loadTest(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 39 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 40 | mClock.set(wStartTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 41 | } |
| 42 | |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 43 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 44 | RadioInterface::~RadioInterface(void) { |
| 45 | if (rcvBuffer!=NULL) delete rcvBuffer; |
| 46 | //mReceiveFIFO.clear(); |
| 47 | } |
| 48 | |
| 49 | double RadioInterface::fullScaleInputValue(void) { |
| 50 | return mRadio->fullScaleInputValue(); |
| 51 | } |
| 52 | |
| 53 | double RadioInterface::fullScaleOutputValue(void) { |
| 54 | return mRadio->fullScaleOutputValue(); |
| 55 | } |
| 56 | |
| 57 | |
kurtis.heimerl | 58d6a01 | 2011-11-26 03:17:38 +0000 | [diff] [blame] | 58 | void RadioInterface::setPowerAttenuation(double atten) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 59 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 60 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 61 | |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 62 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - atten); |
| 63 | digAtten = atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 64 | |
| 65 | if (digAtten < 1.0) |
| 66 | powerScaling = 1.0; |
| 67 | else |
| 68 | powerScaling = 1.0/sqrt(pow(10, (digAtten/10.0))); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 71 | int RadioInterface::radioifyVector(signalVector &wVector, |
| 72 | float *retVector, |
| 73 | float scale, |
| 74 | bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 75 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 76 | int i; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 77 | signalVector::iterator itr = wVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 78 | |
| 79 | if (zero) { |
| 80 | memset(retVector, 0, wVector.size() * 2 * sizeof(float)); |
| 81 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 82 | } |
| 83 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 84 | for (i = 0; i < wVector.size(); i++) { |
| 85 | retVector[2 * i + 0] = itr->real() * scale; |
| 86 | retVector[2 * i + 1] = itr->imag() * scale; |
| 87 | itr++; |
| 88 | } |
| 89 | |
| 90 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 91 | } |
| 92 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 93 | int RadioInterface::unRadioifyVector(float *floatVector, |
| 94 | signalVector& newVector) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 95 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 96 | int i; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 97 | signalVector::iterator itr = newVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 98 | |
| 99 | for (i = 0; i < newVector.size(); i++) { |
| 100 | *itr++ = Complex<float>(floatVector[2 * i + 0], |
| 101 | floatVector[2 * i + 1]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 102 | } |
| 103 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 104 | return newVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool RadioInterface::tuneTx(double freq) |
| 108 | { |
| 109 | return mRadio->setTxFreq(freq); |
| 110 | } |
| 111 | |
| 112 | bool RadioInterface::tuneRx(double freq) |
| 113 | { |
| 114 | return mRadio->setRxFreq(freq); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void RadioInterface::start() |
| 119 | { |
| 120 | LOG(INFO) << "starting radio interface..."; |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame^] | 121 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 122 | mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 123 | (void*)this); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame^] | 124 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 125 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 126 | readTimestamp = mRadio->initialReadTimestamp(); |
| 127 | mRadio->start(); |
| 128 | LOG(DEBUG) << "Radio started"; |
| 129 | mRadio->updateAlignment(writeTimestamp-10000); |
| 130 | mRadio->updateAlignment(writeTimestamp-10000); |
| 131 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 132 | sendBuffer = new float[2*2*INCHUNK*samplesPerSymbol]; |
| 133 | rcvBuffer = new float[2*2*OUTCHUNK*samplesPerSymbol]; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 134 | |
| 135 | mOn = true; |
| 136 | |
| 137 | } |
| 138 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame^] | 139 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 140 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 141 | { |
| 142 | while (1) { |
| 143 | radioInterface->alignRadio(); |
| 144 | pthread_testcancel(); |
| 145 | } |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | void RadioInterface::alignRadio() { |
| 150 | sleep(60); |
| 151 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 152 | } |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame^] | 153 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 154 | |
| 155 | void RadioInterface::driveTransmitRadio(signalVector &radioBurst, bool zeroBurst) { |
| 156 | |
| 157 | if (!mOn) return; |
| 158 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 159 | radioifyVector(radioBurst, sendBuffer + 2 * sendCursor, powerScaling, zeroBurst); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 160 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 161 | sendCursor += radioBurst.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 162 | |
| 163 | pushBuffer(); |
| 164 | } |
| 165 | |
| 166 | void RadioInterface::driveReceiveRadio() { |
| 167 | |
| 168 | if (!mOn) return; |
| 169 | |
| 170 | if (mReceiveFIFO.size() > 8) return; |
| 171 | |
| 172 | pullBuffer(); |
| 173 | |
| 174 | GSM::Time rcvClock = mClock.get(); |
| 175 | rcvClock.decTN(receiveOffset); |
| 176 | unsigned tN = rcvClock.TN(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 177 | int rcvSz = rcvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 178 | int readSz = 0; |
| 179 | const int symbolsPerSlot = gSlotLen + 8; |
| 180 | |
| 181 | // while there's enough data in receive buffer, form received |
| 182 | // GSM bursts and pass up to Transceiver |
| 183 | // Using the 157-156-156-156 symbols per timeslot format. |
| 184 | while (rcvSz > (symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol) { |
| 185 | signalVector rxVector((symbolsPerSlot + (tN % 4 == 0))*samplesPerSymbol); |
| 186 | unRadioifyVector(rcvBuffer+readSz*2,rxVector); |
| 187 | GSM::Time tmpTime = rcvClock; |
| 188 | if (rcvClock.FN() >= 0) { |
| 189 | //LOG(DEBUG) << "FN: " << rcvClock.FN(); |
| 190 | radioVector *rxBurst = NULL; |
| 191 | if (!loadTest) |
| 192 | rxBurst = new radioVector(rxVector,tmpTime); |
| 193 | else { |
| 194 | if (tN % 4 == 0) |
| 195 | rxBurst = new radioVector(*finalVec9,tmpTime); |
| 196 | else |
| 197 | rxBurst = new radioVector(*finalVec,tmpTime); |
| 198 | } |
| 199 | mReceiveFIFO.put(rxBurst); |
| 200 | } |
| 201 | mClock.incTN(); |
| 202 | rcvClock.incTN(); |
| 203 | //if (mReceiveFIFO.size() >= 16) mReceiveFIFO.wait(8); |
| 204 | //LOG(DEBUG) << "receiveFIFO: wrote radio vector at time: " << mClock.get() << ", new size: " << mReceiveFIFO.size() ; |
| 205 | readSz += (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol; |
| 206 | rcvSz -= (symbolsPerSlot+(tN % 4 == 0))*samplesPerSymbol; |
| 207 | |
| 208 | tN = rcvClock.TN(); |
| 209 | } |
| 210 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 211 | if (readSz > 0) { |
| 212 | rcvCursor -= readSz; |
| 213 | memmove(rcvBuffer,rcvBuffer+2*readSz,sizeof(float) * 2 * rcvCursor); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 214 | } |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | bool RadioInterface::isUnderrun() |
| 218 | { |
| 219 | bool retVal = underrun; |
| 220 | underrun = false; |
| 221 | |
| 222 | return retVal; |
| 223 | } |
| 224 | |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 225 | double RadioInterface::setRxGain(double dB) |
| 226 | { |
| 227 | if (mRadio) |
| 228 | return mRadio->setRxGain(dB); |
| 229 | else |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | double RadioInterface::getRxGain() |
| 234 | { |
| 235 | if (mRadio) |
| 236 | return mRadio->getRxGain(); |
| 237 | else |
| 238 | return -1; |
| 239 | } |