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" |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 26 | #include "Resampler.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 27 | #include <Logger.h> |
| 28 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 29 | extern "C" { |
| 30 | #include "convert.h" |
| 31 | } |
| 32 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 33 | #define CHUNK 625 |
| 34 | #define NUMCHUNKS 4 |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 35 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 36 | RadioInterface::RadioInterface(RadioDevice *wRadio, size_t tx_sps, |
| 37 | size_t rx_sps, size_t chans, size_t diversity, |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 38 | int wReceiveOffset, GSM::Time wStartTime) |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 39 | : mRadio(wRadio), mSPSTx(tx_sps), mSPSRx(rx_sps), mChans(chans), |
| 40 | mMIMO(diversity), sendCursor(0), recvCursor(0), underrun(false), |
| 41 | overrun(false), receiveOffset(wReceiveOffset), mOn(false) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 42 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 43 | mClock.set(wStartTime); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 46 | RadioInterface::~RadioInterface(void) |
| 47 | { |
| 48 | close(); |
| 49 | } |
kurtis.heimerl | 5435404 | 2011-11-26 03:18:49 +0000 | [diff] [blame] | 50 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 51 | bool RadioInterface::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 52 | { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 53 | if ((type != RadioDevice::NORMAL) || (mMIMO > 1) || !mChans) { |
| 54 | LOG(ALERT) << "Invalid configuration"; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 58 | close(); |
| 59 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 60 | sendBuffer.resize(mChans); |
| 61 | recvBuffer.resize(mChans); |
| 62 | convertSendBuffer.resize(mChans); |
| 63 | convertRecvBuffer.resize(mChans); |
| 64 | mReceiveFIFO.resize(mChans); |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 65 | powerScaling.resize(mChans); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 66 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 67 | for (size_t i = 0; i < mChans; i++) { |
| 68 | sendBuffer[i] = new signalVector(CHUNK * mSPSTx); |
| 69 | recvBuffer[i] = new signalVector(NUMCHUNKS * CHUNK * mSPSRx); |
| 70 | |
| 71 | convertSendBuffer[i] = new short[sendBuffer[i]->size() * 2]; |
| 72 | convertRecvBuffer[i] = new short[recvBuffer[i]->size() * 2]; |
Alexander Chemeris | 19174f5 | 2016-06-18 11:16:54 +0300 | [diff] [blame] | 73 | |
| 74 | powerScaling[i] = 1.0; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 75 | } |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 76 | |
| 77 | sendCursor = 0; |
| 78 | recvCursor = 0; |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void RadioInterface::close() |
| 84 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 85 | for (size_t i = 0; i < sendBuffer.size(); i++) |
| 86 | delete sendBuffer[i]; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 87 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 88 | for (size_t i = 0; i < recvBuffer.size(); i++) |
| 89 | delete recvBuffer[i]; |
| 90 | |
| 91 | for (size_t i = 0; i < convertSendBuffer.size(); i++) |
| 92 | delete convertSendBuffer[i]; |
| 93 | |
| 94 | for (size_t i = 0; i < convertRecvBuffer.size(); i++) |
| 95 | delete convertRecvBuffer[i]; |
| 96 | |
| 97 | sendBuffer.resize(0); |
| 98 | recvBuffer.resize(0); |
| 99 | convertSendBuffer.resize(0); |
| 100 | convertRecvBuffer.resize(0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | double RadioInterface::fullScaleInputValue(void) { |
| 104 | return mRadio->fullScaleInputValue(); |
| 105 | } |
| 106 | |
| 107 | double RadioInterface::fullScaleOutputValue(void) { |
| 108 | return mRadio->fullScaleOutputValue(); |
| 109 | } |
| 110 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 111 | int RadioInterface::setPowerAttenuation(int atten, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 112 | { |
kurtis.heimerl | 16c6540 | 2011-11-26 03:18:11 +0000 | [diff] [blame] | 113 | double rfGain, digAtten; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 114 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 115 | if (chan >= mChans) { |
| 116 | LOG(ALERT) << "Invalid channel requested"; |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 117 | return -1; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 120 | if (atten < 0.0) |
| 121 | atten = 0.0; |
| 122 | |
| 123 | rfGain = mRadio->setTxGain(mRadio->maxTxGain() - (double) atten, chan); |
| 124 | digAtten = (double) atten - mRadio->maxTxGain() + rfGain; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 125 | |
| 126 | if (digAtten < 1.0) |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 127 | powerScaling[chan] = 1.0; |
kurtis.heimerl | ee5347a | 2011-11-26 03:18:05 +0000 | [diff] [blame] | 128 | else |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 129 | powerScaling[chan] = 1.0 / sqrt(pow(10, digAtten / 10.0)); |
Tom Tsou | a4d1a41 | 2014-11-25 15:46:56 -0800 | [diff] [blame] | 130 | |
| 131 | return atten; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 132 | } |
| 133 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 134 | int RadioInterface::radioifyVector(signalVector &wVector, |
| 135 | float *retVector, |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 136 | bool zero) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 137 | { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 138 | if (zero) { |
| 139 | memset(retVector, 0, wVector.size() * 2 * sizeof(float)); |
| 140 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 143 | memcpy(retVector, wVector.begin(), wVector.size() * 2 * sizeof(float)); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 144 | |
| 145 | return wVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 146 | } |
| 147 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 148 | int RadioInterface::unRadioifyVector(float *floatVector, |
| 149 | signalVector& newVector) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 150 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 151 | signalVector::iterator itr = newVector.begin(); |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 152 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 153 | if (newVector.size() > recvCursor) { |
| 154 | LOG(ALERT) << "Insufficient number of samples in receive buffer"; |
| 155 | return -1; |
| 156 | } |
| 157 | |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 158 | for (size_t i = 0; i < newVector.size(); i++) { |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 159 | *itr++ = Complex<float>(floatVector[2 * i + 0], |
| 160 | floatVector[2 * i + 1]); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 161 | } |
| 162 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 163 | return newVector.size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 166 | bool RadioInterface::tuneTx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 167 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 168 | return mRadio->setTxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 171 | bool RadioInterface::tuneRx(double freq, size_t chan) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 172 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 173 | return mRadio->setRxFreq(freq, chan); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 176 | bool RadioInterface::start() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 177 | { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 178 | if (mOn) |
| 179 | return true; |
| 180 | |
| 181 | LOG(INFO) << "Starting radio device"; |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 182 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 183 | mAlignRadioServiceLoopThread.start((void * (*)(void*))AlignRadioServiceLoopAdapter, |
| 184 | (void*)this); |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 185 | #endif |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 186 | |
| 187 | if (!mRadio->start()) |
| 188 | return false; |
| 189 | |
Tom Tsou | d7610cf | 2015-05-07 17:39:49 -0700 | [diff] [blame] | 190 | recvCursor = 0; |
| 191 | sendCursor = 0; |
| 192 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 193 | writeTimestamp = mRadio->initialWriteTimestamp(); |
| 194 | readTimestamp = mRadio->initialReadTimestamp(); |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 195 | |
| 196 | mRadio->updateAlignment(writeTimestamp-10000); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 197 | mRadio->updateAlignment(writeTimestamp-10000); |
| 198 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 199 | mOn = true; |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 200 | LOG(INFO) << "Radio started"; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Stop the radio device |
| 206 | * |
| 207 | * This is a pass-through call to the device interface. Because the underlying |
| 208 | * stop command issuance generally doesn't return confirmation on device status, |
| 209 | * this call will only return false if the device is already stopped. |
| 210 | */ |
| 211 | bool RadioInterface::stop() |
| 212 | { |
| 213 | if (!mOn || !mRadio->stop()) |
| 214 | return false; |
| 215 | |
| 216 | mOn = false; |
| 217 | return true; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 220 | #ifdef USRP1 |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 221 | void *AlignRadioServiceLoopAdapter(RadioInterface *radioInterface) |
| 222 | { |
| 223 | while (1) { |
| 224 | radioInterface->alignRadio(); |
| 225 | pthread_testcancel(); |
| 226 | } |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | void RadioInterface::alignRadio() { |
| 231 | sleep(60); |
| 232 | mRadio->updateAlignment(writeTimestamp+ (TIMESTAMP) 10000); |
| 233 | } |
Thomas Tsou | f2293b8 | 2013-04-08 13:35:36 -0400 | [diff] [blame] | 234 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 235 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 236 | void RadioInterface::driveTransmitRadio(std::vector<signalVector *> &bursts, |
| 237 | std::vector<bool> &zeros) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 238 | { |
| 239 | if (!mOn) |
| 240 | return; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 241 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 242 | for (size_t i = 0; i < mChans; i++) { |
| 243 | radioifyVector(*bursts[i], |
| 244 | (float *) (sendBuffer[i]->begin() + sendCursor), zeros[i]); |
| 245 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 246 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 247 | sendCursor += bursts[0]->size(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 248 | |
| 249 | pushBuffer(); |
| 250 | } |
| 251 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 252 | bool RadioInterface::driveReceiveRadio() |
| 253 | { |
| 254 | radioVector *burst = NULL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 255 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 256 | if (!mOn) |
| 257 | return false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 258 | |
| 259 | pullBuffer(); |
| 260 | |
| 261 | GSM::Time rcvClock = mClock.get(); |
| 262 | rcvClock.decTN(receiveOffset); |
| 263 | unsigned tN = rcvClock.TN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 264 | int recvSz = recvCursor; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 265 | int readSz = 0; |
| 266 | const int symbolsPerSlot = gSlotLen + 8; |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 267 | int burstSize; |
| 268 | |
| 269 | if (mSPSRx == 4) |
| 270 | burstSize = 625; |
| 271 | else |
| 272 | burstSize = symbolsPerSlot + (tN % 4 == 0); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 273 | |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 274 | /* |
| 275 | * Pre-allocate head room for the largest correlation size |
| 276 | * so we can later avoid a re-allocation and copy |
| 277 | * */ |
| 278 | size_t head = GSM::gRACHSynchSequence.size(); |
| 279 | |
| 280 | /* |
| 281 | * Form receive bursts and pass up to transceiver. Use repeating |
| 282 | * pattern of 157-156-156-156 symbols per timeslot |
| 283 | */ |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 284 | while (recvSz > burstSize) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 285 | for (size_t i = 0; i < mChans; i++) { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 286 | burst = new radioVector(rcvClock, burstSize, head, mMIMO); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 287 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 288 | for (size_t n = 0; n < mMIMO; n++) { |
| 289 | unRadioifyVector((float *) |
| 290 | (recvBuffer[mMIMO * i + n]->begin() + readSz), |
| 291 | *burst->getVector(n)); |
| 292 | } |
| 293 | |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 294 | if (mReceiveFIFO[i].size() < 32) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 295 | mReceiveFIFO[i].write(burst); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 296 | else |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 297 | delete burst; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 298 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 299 | |
| 300 | mClock.incTN(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 301 | rcvClock.incTN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 302 | readSz += burstSize; |
| 303 | recvSz -= burstSize; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 304 | |
| 305 | tN = rcvClock.TN(); |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 306 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 307 | if (mSPSRx != 4) |
| 308 | burstSize = (symbolsPerSlot + (tN % 4 == 0)) * mSPSRx; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 309 | } |
| 310 | |
kurtis.heimerl | 9b55783 | 2011-11-26 03:18:34 +0000 | [diff] [blame] | 311 | if (readSz > 0) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 312 | for (size_t i = 0; i < recvBuffer.size(); i++) { |
| 313 | memmove(recvBuffer[i]->begin(), |
| 314 | recvBuffer[i]->begin() + readSz, |
| 315 | (recvCursor - readSz) * 2 * sizeof(float)); |
| 316 | } |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 317 | |
| 318 | recvCursor -= readSz; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 319 | } |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 320 | |
| 321 | return true; |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | bool RadioInterface::isUnderrun() |
| 325 | { |
| 326 | bool retVal = underrun; |
| 327 | underrun = false; |
| 328 | |
| 329 | return retVal; |
| 330 | } |
| 331 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 332 | VectorFIFO* RadioInterface::receiveFIFO(size_t chan) |
| 333 | { |
| 334 | if (chan >= mReceiveFIFO.size()) |
| 335 | return NULL; |
| 336 | |
| 337 | return &mReceiveFIFO[chan]; |
| 338 | } |
| 339 | |
| 340 | double RadioInterface::setRxGain(double dB, size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 341 | { |
| 342 | if (mRadio) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 343 | return mRadio->setRxGain(dB, chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 344 | else |
| 345 | return -1; |
| 346 | } |
| 347 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 348 | double RadioInterface::getRxGain(size_t chan) |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 349 | { |
| 350 | if (mRadio) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 351 | return mRadio->getRxGain(chan); |
kurtis.heimerl | e724d6d | 2011-11-26 03:18:46 +0000 | [diff] [blame] | 352 | else |
| 353 | return -1; |
| 354 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 355 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 356 | /* Receive a timestamped chunk from the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 357 | void RadioInterface::pullBuffer() |
| 358 | { |
| 359 | bool local_underrun; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 360 | int num_recv; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 361 | float *output; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 362 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 363 | if (recvCursor > recvBuffer[0]->size() - CHUNK) |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 364 | return; |
| 365 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 366 | /* Outer buffer access size is fixed */ |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 367 | num_recv = mRadio->readSamples(convertRecvBuffer, |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 368 | CHUNK, |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 369 | &overrun, |
| 370 | readTimestamp, |
| 371 | &local_underrun); |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 372 | if (num_recv != CHUNK) { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 373 | LOG(ALERT) << "Receive error " << num_recv; |
| 374 | return; |
| 375 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 376 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 377 | for (size_t i = 0; i < mChans; i++) { |
| 378 | output = (float *) (recvBuffer[i]->begin() + recvCursor); |
| 379 | convert_short_float(output, convertRecvBuffer[i], 2 * num_recv); |
| 380 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 381 | |
| 382 | underrun |= local_underrun; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 383 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 384 | readTimestamp += num_recv; |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 385 | recvCursor += num_recv; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 388 | /* Send timestamped chunk to the device with arbitrary size */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 389 | void RadioInterface::pushBuffer() |
| 390 | { |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 391 | int num_sent; |
| 392 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 393 | if (sendCursor < CHUNK) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 394 | return; |
| 395 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 396 | if (sendCursor > sendBuffer[0]->size()) |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 397 | LOG(ALERT) << "Send buffer overflow"; |
| 398 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 399 | for (size_t i = 0; i < mChans; i++) { |
| 400 | convert_float_short(convertSendBuffer[i], |
| 401 | (float *) sendBuffer[i]->begin(), |
Thomas Tsou | cb269a3 | 2013-11-15 14:15:47 -0500 | [diff] [blame] | 402 | powerScaling[i], 2 * sendCursor); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 403 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 404 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 405 | /* Send the all samples in the send buffer */ |
| 406 | num_sent = mRadio->writeSamples(convertSendBuffer, |
| 407 | sendCursor, |
| 408 | &underrun, |
| 409 | writeTimestamp); |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 410 | writeTimestamp += num_sent; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 411 | sendCursor = 0; |
| 412 | } |