kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Radio device interface with sample rate conversion |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 3 | * |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 4 | * Copyright (C) 2011-2014 Free Software Foundation, Inc. |
| 5 | * Copyright (C) 2015 Ettus Research LLC |
| 6 | * |
| 7 | * Author: Tom Tsou <tom@tsou.cc> |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation, either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * See the COPYING file in the main directory for details. |
| 22 | */ |
| 23 | |
| 24 | #include <radioInterface.h> |
| 25 | #include <Logger.h> |
| 26 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 27 | #include "Resampler.h" |
| 28 | |
| 29 | extern "C" { |
| 30 | #include "convert.h" |
| 31 | } |
| 32 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 33 | /* Resampling parameters for 64 MHz clocking */ |
| 34 | #define RESAMP_64M_INRATE 65 |
| 35 | #define RESAMP_64M_OUTRATE 96 |
| 36 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 37 | /* Resampling parameters for 100 MHz clocking */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 38 | #define RESAMP_100M_INRATE 52 |
| 39 | #define RESAMP_100M_OUTRATE 75 |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 40 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 41 | /* Universal resampling parameters */ |
| 42 | #define NUMCHUNKS 24 |
| 43 | |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 44 | /* |
| 45 | * Resampling filter bandwidth scaling factor |
| 46 | * This narrows the filter cutoff relative to the output bandwidth |
| 47 | * of the polyphase resampler. At 4 samples-per-symbol using the |
| 48 | * 2 pulse Laurent GMSK approximation gives us below 0.5 degrees |
| 49 | * RMS phase error at the resampler output. |
| 50 | */ |
| 51 | #define RESAMP_TX4_FILTER 0.45 |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 52 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 53 | static Resampler *upsampler = NULL; |
| 54 | static Resampler *dnsampler = NULL; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 55 | static size_t resamp_inrate = 0; |
| 56 | static size_t resamp_inchunk = 0; |
| 57 | static size_t resamp_outrate = 0; |
| 58 | static size_t resamp_outchunk = 0; |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 59 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 60 | RadioInterfaceResamp::RadioInterfaceResamp(RadioDevice *wRadio, |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 61 | size_t sps, size_t chans) |
| 62 | : RadioInterface(wRadio, sps, chans), |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 63 | outerSendBuffer(NULL), |
| 64 | outerRecvBuffer(NULL) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 68 | RadioInterfaceResamp::~RadioInterfaceResamp() |
| 69 | { |
| 70 | close(); |
| 71 | } |
| 72 | |
| 73 | void RadioInterfaceResamp::close() |
| 74 | { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 75 | delete outerSendBuffer; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 76 | delete outerRecvBuffer; |
| 77 | |
| 78 | delete upsampler; |
| 79 | delete dnsampler; |
| 80 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 81 | outerSendBuffer = NULL; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 82 | outerRecvBuffer = NULL; |
| 83 | |
| 84 | upsampler = NULL; |
| 85 | dnsampler = NULL; |
Thomas Tsou | de1648c | 2013-10-15 16:18:58 -0400 | [diff] [blame] | 86 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 87 | if (sendBuffer.size()) |
| 88 | sendBuffer[0] = NULL; |
| 89 | if (recvBuffer.size()) |
| 90 | recvBuffer[0] = NULL; |
| 91 | |
Thomas Tsou | de1648c | 2013-10-15 16:18:58 -0400 | [diff] [blame] | 92 | RadioInterface::close(); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* Initialize I/O specific objects */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 96 | bool RadioInterfaceResamp::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 97 | { |
| 98 | float cutoff = 1.0f; |
| 99 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 100 | if (mChans != 1) { |
| 101 | LOG(ALERT) << "Unsupported channel configuration " << mChans; |
| 102 | return false; |
| 103 | } |
| 104 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 105 | close(); |
| 106 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 107 | sendBuffer.resize(1); |
| 108 | recvBuffer.resize(1); |
| 109 | convertSendBuffer.resize(1); |
| 110 | convertRecvBuffer.resize(1); |
| 111 | mReceiveFIFO.resize(1); |
Thomas Tsou | af50644 | 2013-11-18 01:35:22 -0500 | [diff] [blame] | 112 | powerScaling.resize(1); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 113 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 114 | switch (type) { |
| 115 | case RadioDevice::RESAMP_64M: |
| 116 | resamp_inrate = RESAMP_64M_INRATE; |
| 117 | resamp_outrate = RESAMP_64M_OUTRATE; |
| 118 | break; |
| 119 | case RadioDevice::RESAMP_100M: |
| 120 | resamp_inrate = RESAMP_100M_INRATE; |
| 121 | resamp_outrate = RESAMP_100M_OUTRATE; |
| 122 | break; |
| 123 | case RadioDevice::NORMAL: |
| 124 | default: |
| 125 | LOG(ALERT) << "Invalid device configuration"; |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | resamp_inchunk = resamp_inrate * 4; |
| 130 | resamp_outchunk = resamp_outrate * 4; |
| 131 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 132 | if (resamp_inchunk * NUMCHUNKS < 157 * mSPSTx * 2) { |
| 133 | LOG(ALERT) << "Invalid inner chunk size " << resamp_inchunk; |
| 134 | return false; |
| 135 | } |
| 136 | |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 137 | if (mSPSTx == 4) |
| 138 | cutoff = RESAMP_TX4_FILTER; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 139 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 140 | dnsampler = new Resampler(resamp_inrate, resamp_outrate); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 141 | if (!dnsampler->init()) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 142 | LOG(ALERT) << "Rx resampler failed to initialize"; |
| 143 | return false; |
| 144 | } |
| 145 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 146 | upsampler = new Resampler(resamp_outrate, resamp_inrate); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 147 | if (!upsampler->init(cutoff)) { |
| 148 | LOG(ALERT) << "Tx resampler failed to initialize"; |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Allocate high and low rate buffers. The high rate receive |
| 154 | * buffer and low rate transmit vectors feed into the resampler |
| 155 | * and requires headroom equivalent to the filter length. Low |
| 156 | * rate buffers are allocated in the main radio interface code. |
| 157 | */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 158 | sendBuffer[0] = new RadioBuffer(NUMCHUNKS, resamp_inchunk, |
| 159 | upsampler->len(), true); |
| 160 | recvBuffer[0] = new RadioBuffer(NUMCHUNKS * 20, resamp_inchunk, 0, false); |
| 161 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 162 | outerSendBuffer = |
| 163 | new signalVector(NUMCHUNKS * resamp_outchunk); |
| 164 | outerRecvBuffer = |
| 165 | new signalVector(resamp_outchunk, dnsampler->len()); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 166 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 167 | convertSendBuffer[0] = new short[outerSendBuffer->size() * 2]; |
| 168 | convertRecvBuffer[0] = new short[outerRecvBuffer->size() * 2]; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 169 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /* Receive a timestamped chunk from the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 174 | void RadioInterfaceResamp::pullBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 175 | { |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 176 | bool local_underrun; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 177 | int rc, num_recv; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 178 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 179 | if (recvBuffer[0]->getFreeSegments() <= 0) |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 180 | return; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 181 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 182 | /* Outer buffer access size is fixed */ |
| 183 | num_recv = mRadio->readSamples(convertRecvBuffer, |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 184 | resamp_outchunk, |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 185 | &overrun, |
| 186 | readTimestamp, |
| 187 | &local_underrun); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 188 | if (num_recv != (int) resamp_outchunk) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 189 | LOG(ALERT) << "Receive error " << num_recv; |
| 190 | return; |
| 191 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 192 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 193 | convert_short_float((float *) outerRecvBuffer->begin(), |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 194 | convertRecvBuffer[0], 2 * resamp_outchunk); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 195 | |
| 196 | underrun |= local_underrun; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 197 | readTimestamp += (TIMESTAMP) resamp_outchunk; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 198 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 199 | /* Write to the end of the inner receive buffer */ |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 200 | rc = dnsampler->rotate((float *) outerRecvBuffer->begin(), |
| 201 | resamp_outchunk, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 202 | recvBuffer[0]->getWriteSegment(), |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 203 | resamp_inchunk); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 204 | if (rc < 0) { |
| 205 | LOG(ALERT) << "Sample rate upsampling error"; |
| 206 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 207 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 208 | /* Set history for the next chunk */ |
| 209 | outerRecvBuffer->updateHistory(); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 212 | /* Send a timestamped chunk to the device */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 213 | bool RadioInterfaceResamp::pushBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 214 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 215 | int rc; |
| 216 | size_t numSent; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 217 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 218 | if (sendBuffer[0]->getAvailSegments() <= 0) |
| 219 | return false; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 220 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 221 | /* Always send from the beginning of the buffer */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 222 | rc = upsampler->rotate(sendBuffer[0]->getReadSegment(), |
| 223 | resamp_inchunk, |
| 224 | (float *) outerSendBuffer->begin(), |
| 225 | resamp_outchunk); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 226 | if (rc < 0) { |
| 227 | LOG(ALERT) << "Sample rate downsampling error"; |
| 228 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 229 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 230 | convert_float_short(convertSendBuffer[0], |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 231 | (float *) outerSendBuffer->begin(), |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 232 | powerScaling[0], 2 * resamp_outchunk); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 233 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 234 | numSent = mRadio->writeSamples(convertSendBuffer, |
| 235 | resamp_outchunk, |
| 236 | &underrun, |
| 237 | writeTimestamp); |
| 238 | if (numSent != resamp_outchunk) { |
| 239 | LOG(ALERT) << "Transmit error " << numSent; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 242 | writeTimestamp += resamp_outchunk; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 243 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 244 | return true; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 245 | } |