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, |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 61 | size_t tx_sps, size_t rx_sps) |
| 62 | : RadioInterface(wRadio, tx_sps, rx_sps, 1), |
| 63 | outerSendBuffer(NULL), outerRecvBuffer(NULL) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 67 | RadioInterfaceResamp::~RadioInterfaceResamp() |
| 68 | { |
| 69 | close(); |
| 70 | } |
| 71 | |
| 72 | void RadioInterfaceResamp::close() |
| 73 | { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 74 | delete outerSendBuffer; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 75 | delete outerRecvBuffer; |
| 76 | |
| 77 | delete upsampler; |
| 78 | delete dnsampler; |
| 79 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 80 | outerSendBuffer = NULL; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 81 | outerRecvBuffer = NULL; |
| 82 | |
| 83 | upsampler = NULL; |
| 84 | dnsampler = NULL; |
Thomas Tsou | de1648c | 2013-10-15 16:18:58 -0400 | [diff] [blame] | 85 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 86 | if (sendBuffer.size()) |
| 87 | sendBuffer[0] = NULL; |
| 88 | if (recvBuffer.size()) |
| 89 | recvBuffer[0] = NULL; |
| 90 | |
Thomas Tsou | de1648c | 2013-10-15 16:18:58 -0400 | [diff] [blame] | 91 | RadioInterface::close(); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* Initialize I/O specific objects */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 95 | bool RadioInterfaceResamp::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 96 | { |
| 97 | float cutoff = 1.0f; |
| 98 | |
| 99 | close(); |
| 100 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 101 | sendBuffer.resize(1); |
| 102 | recvBuffer.resize(1); |
| 103 | convertSendBuffer.resize(1); |
| 104 | convertRecvBuffer.resize(1); |
| 105 | mReceiveFIFO.resize(1); |
Thomas Tsou | af50644 | 2013-11-18 01:35:22 -0500 | [diff] [blame] | 106 | powerScaling.resize(1); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 107 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 108 | switch (type) { |
| 109 | case RadioDevice::RESAMP_64M: |
| 110 | resamp_inrate = RESAMP_64M_INRATE; |
| 111 | resamp_outrate = RESAMP_64M_OUTRATE; |
| 112 | break; |
| 113 | case RadioDevice::RESAMP_100M: |
| 114 | resamp_inrate = RESAMP_100M_INRATE; |
| 115 | resamp_outrate = RESAMP_100M_OUTRATE; |
| 116 | break; |
| 117 | case RadioDevice::NORMAL: |
| 118 | default: |
| 119 | LOG(ALERT) << "Invalid device configuration"; |
| 120 | return false; |
| 121 | } |
| 122 | |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 123 | resamp_inchunk = resamp_inrate * 4 * mSPSRx; |
| 124 | resamp_outchunk = resamp_outrate * 4 * mSPSRx; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 125 | |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 126 | if (mSPSTx == 4) |
| 127 | cutoff = RESAMP_TX4_FILTER; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 128 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 129 | dnsampler = new Resampler(resamp_inrate, resamp_outrate); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 130 | if (!dnsampler->init()) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 131 | LOG(ALERT) << "Rx resampler failed to initialize"; |
| 132 | return false; |
| 133 | } |
| 134 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 135 | upsampler = new Resampler(resamp_outrate, resamp_inrate); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 136 | if (!upsampler->init(cutoff)) { |
| 137 | LOG(ALERT) << "Tx resampler failed to initialize"; |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Allocate high and low rate buffers. The high rate receive |
| 143 | * buffer and low rate transmit vectors feed into the resampler |
| 144 | * and requires headroom equivalent to the filter length. Low |
| 145 | * rate buffers are allocated in the main radio interface code. |
| 146 | */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 147 | sendBuffer[0] = new RadioBuffer(NUMCHUNKS, resamp_inchunk, |
| 148 | upsampler->len(), true); |
| 149 | recvBuffer[0] = new RadioBuffer(NUMCHUNKS * 20, resamp_inchunk, 0, false); |
| 150 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 151 | outerSendBuffer = |
| 152 | new signalVector(NUMCHUNKS * resamp_outchunk); |
| 153 | outerRecvBuffer = |
| 154 | new signalVector(resamp_outchunk, dnsampler->len()); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 155 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 156 | convertSendBuffer[0] = new short[outerSendBuffer->size() * 2]; |
| 157 | convertRecvBuffer[0] = new short[outerRecvBuffer->size() * 2]; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 158 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /* Receive a timestamped chunk from the device */ |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 163 | int RadioInterfaceResamp::pullBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 164 | { |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 165 | bool local_underrun; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 166 | int rc, num_recv; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 167 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 168 | if (recvBuffer[0]->getFreeSegments() <= 0) |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 169 | return -1; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 170 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 171 | /* Outer buffer access size is fixed */ |
| 172 | num_recv = mRadio->readSamples(convertRecvBuffer, |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 173 | resamp_outchunk, |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 174 | &overrun, |
| 175 | readTimestamp, |
| 176 | &local_underrun); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 177 | if (num_recv != (int) resamp_outchunk) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 178 | LOG(ALERT) << "Receive error " << num_recv; |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 179 | return -1; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 180 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 181 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 182 | convert_short_float((float *) outerRecvBuffer->begin(), |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 183 | convertRecvBuffer[0], 2 * resamp_outchunk); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 184 | |
| 185 | underrun |= local_underrun; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 186 | readTimestamp += (TIMESTAMP) resamp_outchunk; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 187 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 188 | /* Write to the end of the inner receive buffer */ |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 189 | rc = dnsampler->rotate((float *) outerRecvBuffer->begin(), |
| 190 | resamp_outchunk, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 191 | recvBuffer[0]->getWriteSegment(), |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 192 | resamp_inchunk); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 193 | if (rc < 0) { |
| 194 | LOG(ALERT) << "Sample rate upsampling error"; |
| 195 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 196 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 197 | /* Set history for the next chunk */ |
| 198 | outerRecvBuffer->updateHistory(); |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 199 | return 0; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 202 | /* Send a timestamped chunk to the device */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 203 | bool RadioInterfaceResamp::pushBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 204 | { |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 205 | int rc; |
| 206 | size_t numSent; |
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 | if (sendBuffer[0]->getAvailSegments() <= 0) |
| 209 | return false; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 210 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 211 | /* Always send from the beginning of the buffer */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 212 | rc = upsampler->rotate(sendBuffer[0]->getReadSegment(), |
| 213 | resamp_inchunk, |
| 214 | (float *) outerSendBuffer->begin(), |
| 215 | resamp_outchunk); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 216 | if (rc < 0) { |
| 217 | LOG(ALERT) << "Sample rate downsampling error"; |
| 218 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 219 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 220 | convert_float_short(convertSendBuffer[0], |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 221 | (float *) outerSendBuffer->begin(), |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 222 | powerScaling[0], 2 * resamp_outchunk); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 223 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 224 | numSent = mRadio->writeSamples(convertSendBuffer, |
| 225 | resamp_outchunk, |
| 226 | &underrun, |
| 227 | writeTimestamp); |
| 228 | if (numSent != resamp_outchunk) { |
| 229 | LOG(ALERT) << "Transmit error " << numSent; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 232 | writeTimestamp += resamp_outchunk; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 233 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 234 | return true; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 235 | } |