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