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