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 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: AGPL-3.0+ |
| 10 | * |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 11 | * This program is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Affero General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Affero General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Affero General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * See the COPYING file in the main directory for details. |
| 24 | */ |
| 25 | |
| 26 | #include <radioInterface.h> |
| 27 | #include <Logger.h> |
| 28 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 29 | #include "Resampler.h" |
| 30 | |
| 31 | extern "C" { |
| 32 | #include "convert.h" |
| 33 | } |
| 34 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 35 | /* Resampling parameters for 64 MHz clocking */ |
| 36 | #define RESAMP_64M_INRATE 65 |
| 37 | #define RESAMP_64M_OUTRATE 96 |
| 38 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 39 | /* Resampling parameters for 100 MHz clocking */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 40 | #define RESAMP_100M_INRATE 52 |
| 41 | #define RESAMP_100M_OUTRATE 75 |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 42 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 43 | /* Universal resampling parameters */ |
| 44 | #define NUMCHUNKS 24 |
| 45 | |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 46 | /* |
| 47 | * Resampling filter bandwidth scaling factor |
| 48 | * This narrows the filter cutoff relative to the output bandwidth |
| 49 | * of the polyphase resampler. At 4 samples-per-symbol using the |
| 50 | * 2 pulse Laurent GMSK approximation gives us below 0.5 degrees |
| 51 | * RMS phase error at the resampler output. |
| 52 | */ |
| 53 | #define RESAMP_TX4_FILTER 0.45 |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 54 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 55 | static Resampler *upsampler = NULL; |
| 56 | static Resampler *dnsampler = NULL; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 57 | static size_t resamp_inrate = 0; |
| 58 | static size_t resamp_inchunk = 0; |
| 59 | static size_t resamp_outrate = 0; |
| 60 | static size_t resamp_outchunk = 0; |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 61 | |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 62 | RadioInterfaceResamp::RadioInterfaceResamp(RadioDevice *wDevice, |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 63 | size_t tx_sps, size_t rx_sps) |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 64 | : RadioInterface(wDevice, tx_sps, rx_sps, 1), |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 65 | outerSendBuffer(NULL), outerRecvBuffer(NULL) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 69 | RadioInterfaceResamp::~RadioInterfaceResamp() |
| 70 | { |
| 71 | close(); |
| 72 | } |
| 73 | |
| 74 | void RadioInterfaceResamp::close() |
| 75 | { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 76 | delete outerSendBuffer; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 77 | delete outerRecvBuffer; |
| 78 | |
| 79 | delete upsampler; |
| 80 | delete dnsampler; |
| 81 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 82 | outerSendBuffer = NULL; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 83 | outerRecvBuffer = NULL; |
| 84 | |
| 85 | upsampler = NULL; |
| 86 | dnsampler = NULL; |
Thomas Tsou | de1648c | 2013-10-15 16:18:58 -0400 | [diff] [blame] | 87 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 88 | if (sendBuffer.size()) |
| 89 | sendBuffer[0] = NULL; |
| 90 | if (recvBuffer.size()) |
| 91 | recvBuffer[0] = NULL; |
| 92 | |
Thomas Tsou | de1648c | 2013-10-15 16:18:58 -0400 | [diff] [blame] | 93 | RadioInterface::close(); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Initialize I/O specific objects */ |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 97 | bool RadioInterfaceResamp::init(int type) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 98 | { |
| 99 | float cutoff = 1.0f; |
| 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 | |
Tom Tsou | 8f0ccf6 | 2016-07-20 16:35:03 -0700 | [diff] [blame] | 116 | resamp_inchunk = resamp_inrate * 4 * mSPSRx; |
| 117 | resamp_outchunk = resamp_outrate * 4 * mSPSRx; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 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 | */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 140 | sendBuffer[0] = new RadioBuffer(NUMCHUNKS, resamp_inchunk, |
| 141 | upsampler->len(), true); |
| 142 | recvBuffer[0] = new RadioBuffer(NUMCHUNKS * 20, resamp_inchunk, 0, false); |
| 143 | |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 144 | outerSendBuffer = |
| 145 | new signalVector(NUMCHUNKS * resamp_outchunk); |
| 146 | outerRecvBuffer = |
| 147 | new signalVector(resamp_outchunk, dnsampler->len()); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 148 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 149 | convertSendBuffer[0] = new short[outerSendBuffer->size() * 2]; |
| 150 | convertRecvBuffer[0] = new short[outerRecvBuffer->size() * 2]; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 151 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 152 | return true; |
| 153 | } |
| 154 | |
| 155 | /* Receive a timestamped chunk from the device */ |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 156 | int RadioInterfaceResamp::pullBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 157 | { |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 158 | bool local_underrun; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 159 | int rc, num_recv; |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 160 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 161 | if (recvBuffer[0]->getFreeSegments() <= 0) |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 162 | return -1; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 163 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 164 | /* Outer buffer access size is fixed */ |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 165 | num_recv = mDevice->readSamples(convertRecvBuffer, |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 166 | resamp_outchunk, |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 167 | &overrun, |
| 168 | readTimestamp, |
| 169 | &local_underrun); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 170 | if (num_recv != (int) resamp_outchunk) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 171 | LOG(ALERT) << "Receive error " << num_recv; |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 172 | return -1; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 173 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 174 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 175 | convert_short_float((float *) outerRecvBuffer->begin(), |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 176 | convertRecvBuffer[0], 2 * resamp_outchunk); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 177 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 178 | osmo_trx_sync_or_and_fetch(&underrun, local_underrun); |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 179 | readTimestamp += (TIMESTAMP) resamp_outchunk; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 180 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 181 | /* Write to the end of the inner receive buffer */ |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 182 | rc = dnsampler->rotate((float *) outerRecvBuffer->begin(), |
| 183 | resamp_outchunk, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 184 | recvBuffer[0]->getWriteSegment(), |
Thomas Tsou | 3952d80 | 2013-10-15 15:12:24 -0400 | [diff] [blame] | 185 | resamp_inchunk); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 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 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 190 | /* Set history for the next chunk */ |
| 191 | outerRecvBuffer->updateHistory(); |
Pau Espin Pedrol | 8e498bf | 2018-09-03 16:45:15 +0200 | [diff] [blame] | 192 | return 0; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 195 | /* Send a timestamped chunk to the device */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 196 | bool RadioInterfaceResamp::pushBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 197 | { |
Pau Espin Pedrol | 25383a3 | 2019-09-13 18:23:47 +0200 | [diff] [blame] | 198 | bool local_underrun; |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 199 | int rc; |
| 200 | size_t numSent; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 201 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 202 | if (sendBuffer[0]->getAvailSegments() <= 0) |
| 203 | return false; |
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 | /* Always send from the beginning of the buffer */ |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 206 | rc = upsampler->rotate(sendBuffer[0]->getReadSegment(), |
| 207 | resamp_inchunk, |
| 208 | (float *) outerSendBuffer->begin(), |
| 209 | resamp_outchunk); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 210 | if (rc < 0) { |
| 211 | LOG(ALERT) << "Sample rate downsampling error"; |
| 212 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 213 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 214 | convert_float_short(convertSendBuffer[0], |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 215 | (float *) outerSendBuffer->begin(), |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 216 | powerScaling[0], 2 * resamp_outchunk); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 217 | |
Pau Espin Pedrol | a801ae5 | 2019-09-13 15:59:29 +0200 | [diff] [blame] | 218 | numSent = mDevice->writeSamples(convertSendBuffer, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 219 | resamp_outchunk, |
Pau Espin Pedrol | 25383a3 | 2019-09-13 18:23:47 +0200 | [diff] [blame] | 220 | &local_underrun, |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 221 | writeTimestamp); |
| 222 | if (numSent != resamp_outchunk) { |
| 223 | LOG(ALERT) << "Transmit error " << numSent; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Pau Espin Pedrol | e503c98 | 2019-09-13 18:56:08 +0200 | [diff] [blame] | 226 | osmo_trx_sync_or_and_fetch(&underrun, local_underrun); |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 227 | writeTimestamp += resamp_outchunk; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 228 | |
Tom Tsou | 28670fb | 2015-08-21 19:32:58 -0700 | [diff] [blame] | 229 | return true; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 230 | } |