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 | |
| 39 | /* |
| 40 | * Resampling filter bandwidth scaling factor |
| 41 | * This narrows the filter cutoff relative to the output bandwidth |
| 42 | * of the polyphase resampler. At 4 samples-per-symbol using the |
| 43 | * 2 pulse Laurent GMSK approximation gives us below 0.5 degrees |
| 44 | * RMS phase error at the resampler output. |
| 45 | */ |
| 46 | #define RESAMP_TX4_FILTER 0.45 |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 47 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 48 | static Resampler *upsampler = NULL; |
| 49 | static Resampler *dnsampler = NULL; |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 50 | static int resamp_inrate = 0; |
| 51 | static int resamp_inchunk = 0; |
| 52 | static int resamp_outrate = 0; |
| 53 | static int resamp_outchunk = 0; |
| 54 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 55 | short *convertRecvBuffer = NULL; |
| 56 | short *convertSendBuffer = NULL; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 57 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 58 | RadioInterfaceResamp::RadioInterfaceResamp(RadioDevice *wRadio, |
| 59 | int wReceiveOffset, |
| 60 | int wSPS, |
| 61 | GSM::Time wStartTime) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 62 | : RadioInterface(wRadio, wReceiveOffset, wSPS, wStartTime), |
| 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 | { |
| 75 | RadioInterface::close(); |
| 76 | |
| 77 | delete innerSendBuffer; |
| 78 | delete outerSendBuffer; |
| 79 | delete innerRecvBuffer; |
| 80 | delete outerRecvBuffer; |
| 81 | |
| 82 | delete upsampler; |
| 83 | delete dnsampler; |
| 84 | |
| 85 | innerSendBuffer = NULL; |
| 86 | outerSendBuffer = NULL; |
| 87 | innerRecvBuffer = NULL; |
| 88 | outerRecvBuffer = NULL; |
| 89 | |
| 90 | upsampler = NULL; |
| 91 | dnsampler = NULL; |
| 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 | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 101 | switch (type) { |
| 102 | case RadioDevice::RESAMP_64M: |
| 103 | resamp_inrate = RESAMP_64M_INRATE; |
| 104 | resamp_outrate = RESAMP_64M_OUTRATE; |
| 105 | break; |
| 106 | case RadioDevice::RESAMP_100M: |
| 107 | resamp_inrate = RESAMP_100M_INRATE; |
| 108 | resamp_outrate = RESAMP_100M_OUTRATE; |
| 109 | break; |
| 110 | case RadioDevice::NORMAL: |
| 111 | default: |
| 112 | LOG(ALERT) << "Invalid device configuration"; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | resamp_inchunk = resamp_inrate * 4; |
| 117 | resamp_outchunk = resamp_outrate * 4; |
| 118 | |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 119 | if (mSPSTx == 4) |
| 120 | cutoff = RESAMP_TX4_FILTER; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 121 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 122 | dnsampler = new Resampler(resamp_inrate, resamp_outrate); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 123 | if (!dnsampler->init()) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 124 | LOG(ALERT) << "Rx resampler failed to initialize"; |
| 125 | return false; |
| 126 | } |
| 127 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 128 | upsampler = new Resampler(resamp_outrate, resamp_inrate); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 129 | if (!upsampler->init(cutoff)) { |
| 130 | LOG(ALERT) << "Tx resampler failed to initialize"; |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Allocate high and low rate buffers. The high rate receive |
| 136 | * buffer and low rate transmit vectors feed into the resampler |
| 137 | * and requires headroom equivalent to the filter length. Low |
| 138 | * rate buffers are allocated in the main radio interface code. |
| 139 | */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 140 | innerSendBuffer = new signalVector(resamp_inchunk * 20, |
| 141 | upsampler->len()); |
| 142 | outerSendBuffer = new signalVector(resamp_outchunk * 20); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 143 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 144 | outerRecvBuffer = new signalVector(resamp_outchunk * 2, |
| 145 | dnsampler->len()); |
| 146 | innerRecvBuffer = new signalVector(resamp_inchunk * 20); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 147 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 148 | convertSendBuffer = new short[resamp_outchunk * 2 * 20]; |
| 149 | convertRecvBuffer = new short[resamp_outchunk * 2 * 2]; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 150 | |
| 151 | sendBuffer = innerSendBuffer; |
| 152 | recvBuffer = innerRecvBuffer; |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /* Receive a timestamped chunk from the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 158 | void RadioInterfaceResamp::pullBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 159 | { |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 160 | bool local_underrun; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 161 | int rc, num_recv; |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 162 | int inner_len = resamp_inchunk; |
| 163 | int outer_len = resamp_outchunk; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 164 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 165 | /* Outer buffer access size is fixed */ |
| 166 | num_recv = mRadio->readSamples(convertRecvBuffer, |
| 167 | outer_len, |
| 168 | &overrun, |
| 169 | readTimestamp, |
| 170 | &local_underrun); |
| 171 | if (num_recv != outer_len) { |
| 172 | LOG(ALERT) << "Receive error " << num_recv; |
| 173 | return; |
| 174 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 175 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 176 | convert_short_float((float *) outerRecvBuffer->begin(), |
| 177 | convertRecvBuffer, 2 * outer_len); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 178 | |
| 179 | underrun |= local_underrun; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 180 | readTimestamp += (TIMESTAMP) num_recv; |
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 | /* Write to the end of the inner receive buffer */ |
| 183 | rc = dnsampler->rotate((float *) outerRecvBuffer->begin(), outer_len, |
| 184 | (float *) (innerRecvBuffer->begin() + recvCursor), |
| 185 | inner_len); |
| 186 | if (rc < 0) { |
| 187 | LOG(ALERT) << "Sample rate upsampling error"; |
| 188 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 189 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 190 | recvCursor += inner_len; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 193 | /* Send a timestamped chunk to the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 194 | void RadioInterfaceResamp::pushBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 195 | { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 196 | int rc, chunks, num_sent; |
| 197 | int inner_len, outer_len; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 198 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 199 | if (sendCursor < resamp_inchunk) |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 200 | return; |
| 201 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 202 | chunks = sendCursor / resamp_inchunk; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 203 | if (chunks > 8) |
| 204 | chunks = 8; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 205 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame^] | 206 | inner_len = chunks * resamp_inchunk; |
| 207 | outer_len = chunks * resamp_outchunk; |
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 | /* Always send from the beginning of the buffer */ |
| 210 | rc = upsampler->rotate((float *) innerSendBuffer->begin(), inner_len, |
| 211 | (float *) outerSendBuffer->begin(), outer_len); |
| 212 | if (rc < 0) { |
| 213 | LOG(ALERT) << "Sample rate downsampling error"; |
| 214 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 215 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 216 | convert_float_short(convertSendBuffer, |
| 217 | (float *) outerSendBuffer->begin(), |
| 218 | powerScaling, 2 * outer_len); |
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 | num_sent = mRadio->writeSamples(convertSendBuffer, |
| 221 | outer_len, |
| 222 | &underrun, |
| 223 | writeTimestamp); |
| 224 | if (num_sent != outer_len) { |
| 225 | LOG(ALERT) << "Transmit error " << num_sent; |
| 226 | } |
| 227 | |
| 228 | /* Shift remaining samples to beginning of buffer */ |
| 229 | memmove(innerSendBuffer->begin(), |
| 230 | innerSendBuffer->begin() + inner_len, |
| 231 | (sendCursor - inner_len) * 2 * sizeof(float)); |
| 232 | |
| 233 | writeTimestamp += outer_len; |
| 234 | sendCursor -= inner_len; |
| 235 | assert(sendCursor >= 0); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 236 | } |