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