blob: 8ae4aa1eaee8cade2b28bfd0b601161ae2d15a4a [file] [log] [blame]
kurtis.heimerlce317332011-11-26 03:18:39 +00001/*
2 * Radio device interface with sample rate conversion
kurtis.heimerlce317332011-11-26 03:18:39 +00003 *
Tom Tsou28670fb2015-08-21 19:32:58 -07004 * Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 * Copyright (C) 2015 Ettus Research LLC
6 *
7 * Author: Tom Tsou <tom@tsou.cc>
kurtis.heimerlce317332011-11-26 03:18:39 +00008 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See the COPYING file in the main directory for details.
22 */
23
24#include <radioInterface.h>
25#include <Logger.h>
26
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040027#include "Resampler.h"
28
29extern "C" {
30#include "convert.h"
31}
32
Thomas Tsoufe269fe2013-10-14 23:56:51 -040033/* Resampling parameters for 64 MHz clocking */
34#define RESAMP_64M_INRATE 65
35#define RESAMP_64M_OUTRATE 96
36
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040037/* Resampling parameters for 100 MHz clocking */
Thomas Tsoufe269fe2013-10-14 23:56:51 -040038#define RESAMP_100M_INRATE 52
39#define RESAMP_100M_OUTRATE 75
Thomas Tsou0e44ab32013-09-17 20:12:26 -040040
Thomas Tsou3952d802013-10-15 15:12:24 -040041/* Universal resampling parameters */
42#define NUMCHUNKS 24
43
Thomas Tsou0e44ab32013-09-17 20:12:26 -040044/*
45 * Resampling filter bandwidth scaling factor
46 * This narrows the filter cutoff relative to the output bandwidth
47 * of the polyphase resampler. At 4 samples-per-symbol using the
48 * 2 pulse Laurent GMSK approximation gives us below 0.5 degrees
49 * RMS phase error at the resampler output.
50 */
51#define RESAMP_TX4_FILTER 0.45
kurtis.heimerlce317332011-11-26 03:18:39 +000052
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040053static Resampler *upsampler = NULL;
54static Resampler *dnsampler = NULL;
Thomas Tsoue90a42b2013-11-13 23:38:09 -050055static size_t resamp_inrate = 0;
56static size_t resamp_inchunk = 0;
57static size_t resamp_outrate = 0;
58static size_t resamp_outchunk = 0;
Thomas Tsoufe269fe2013-10-14 23:56:51 -040059
Thomas Tsoucb69f082013-04-08 14:18:26 -040060RadioInterfaceResamp::RadioInterfaceResamp(RadioDevice *wRadio,
Tom Tsou8f0ccf62016-07-20 16:35:03 -070061 size_t tx_sps, size_t rx_sps)
62 : RadioInterface(wRadio, tx_sps, rx_sps, 1),
63 outerSendBuffer(NULL), outerRecvBuffer(NULL)
Thomas Tsoucb69f082013-04-08 14:18:26 -040064{
65}
66
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040067RadioInterfaceResamp::~RadioInterfaceResamp()
68{
69 close();
70}
71
72void RadioInterfaceResamp::close()
73{
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040074 delete outerSendBuffer;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040075 delete outerRecvBuffer;
76
77 delete upsampler;
78 delete dnsampler;
79
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040080 outerSendBuffer = NULL;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040081 outerRecvBuffer = NULL;
82
83 upsampler = NULL;
84 dnsampler = NULL;
Thomas Tsoude1648c2013-10-15 16:18:58 -040085
Thomas Tsou204a9f12013-10-29 18:34:16 -040086 if (sendBuffer.size())
87 sendBuffer[0] = NULL;
88 if (recvBuffer.size())
89 recvBuffer[0] = NULL;
90
Thomas Tsoude1648c2013-10-15 16:18:58 -040091 RadioInterface::close();
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040092}
93
94/* Initialize I/O specific objects */
Thomas Tsoufe269fe2013-10-14 23:56:51 -040095bool RadioInterfaceResamp::init(int type)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040096{
97 float cutoff = 1.0f;
98
99 close();
100
Thomas Tsou204a9f12013-10-29 18:34:16 -0400101 sendBuffer.resize(1);
102 recvBuffer.resize(1);
103 convertSendBuffer.resize(1);
104 convertRecvBuffer.resize(1);
105 mReceiveFIFO.resize(1);
Thomas Tsouaf506442013-11-18 01:35:22 -0500106 powerScaling.resize(1);
Thomas Tsou204a9f12013-10-29 18:34:16 -0400107
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400108 switch (type) {
109 case RadioDevice::RESAMP_64M:
110 resamp_inrate = RESAMP_64M_INRATE;
111 resamp_outrate = RESAMP_64M_OUTRATE;
112 break;
113 case RadioDevice::RESAMP_100M:
114 resamp_inrate = RESAMP_100M_INRATE;
115 resamp_outrate = RESAMP_100M_OUTRATE;
116 break;
117 case RadioDevice::NORMAL:
118 default:
119 LOG(ALERT) << "Invalid device configuration";
120 return false;
121 }
122
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700123 resamp_inchunk = resamp_inrate * 4 * mSPSRx;
124 resamp_outchunk = resamp_outrate * 4 * mSPSRx;
Thomas Tsou3952d802013-10-15 15:12:24 -0400125
Thomas Tsou0e44ab32013-09-17 20:12:26 -0400126 if (mSPSTx == 4)
127 cutoff = RESAMP_TX4_FILTER;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400128
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400129 dnsampler = new Resampler(resamp_inrate, resamp_outrate);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400130 if (!dnsampler->init()) {
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400131 LOG(ALERT) << "Rx resampler failed to initialize";
132 return false;
133 }
134
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400135 upsampler = new Resampler(resamp_outrate, resamp_inrate);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400136 if (!upsampler->init(cutoff)) {
137 LOG(ALERT) << "Tx resampler failed to initialize";
138 return false;
139 }
140
141 /*
142 * Allocate high and low rate buffers. The high rate receive
143 * buffer and low rate transmit vectors feed into the resampler
144 * and requires headroom equivalent to the filter length. Low
145 * rate buffers are allocated in the main radio interface code.
146 */
Tom Tsou28670fb2015-08-21 19:32:58 -0700147 sendBuffer[0] = new RadioBuffer(NUMCHUNKS, resamp_inchunk,
148 upsampler->len(), true);
149 recvBuffer[0] = new RadioBuffer(NUMCHUNKS * 20, resamp_inchunk, 0, false);
150
Thomas Tsou3952d802013-10-15 15:12:24 -0400151 outerSendBuffer =
152 new signalVector(NUMCHUNKS * resamp_outchunk);
153 outerRecvBuffer =
154 new signalVector(resamp_outchunk, dnsampler->len());
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400155
Thomas Tsou204a9f12013-10-29 18:34:16 -0400156 convertSendBuffer[0] = new short[outerSendBuffer->size() * 2];
157 convertRecvBuffer[0] = new short[outerRecvBuffer->size() * 2];
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400158
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400159 return true;
160}
161
162/* Receive a timestamped chunk from the device */
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200163int RadioInterfaceResamp::pullBuffer()
kurtis.heimerlce317332011-11-26 03:18:39 +0000164{
kurtis.heimerlce317332011-11-26 03:18:39 +0000165 bool local_underrun;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400166 int rc, num_recv;
Thomas Tsou3952d802013-10-15 15:12:24 -0400167
Tom Tsou28670fb2015-08-21 19:32:58 -0700168 if (recvBuffer[0]->getFreeSegments() <= 0)
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200169 return -1;
kurtis.heimerlce317332011-11-26 03:18:39 +0000170
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400171 /* Outer buffer access size is fixed */
172 num_recv = mRadio->readSamples(convertRecvBuffer,
Thomas Tsou3952d802013-10-15 15:12:24 -0400173 resamp_outchunk,
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400174 &overrun,
175 readTimestamp,
176 &local_underrun);
Thomas Tsoue90a42b2013-11-13 23:38:09 -0500177 if (num_recv != (int) resamp_outchunk) {
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400178 LOG(ALERT) << "Receive error " << num_recv;
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200179 return -1;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400180 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000181
Thomas Tsou9471d762013-08-20 21:24:24 -0400182 convert_short_float((float *) outerRecvBuffer->begin(),
Thomas Tsou204a9f12013-10-29 18:34:16 -0400183 convertRecvBuffer[0], 2 * resamp_outchunk);
kurtis.heimerlce317332011-11-26 03:18:39 +0000184
185 underrun |= local_underrun;
Thomas Tsou3952d802013-10-15 15:12:24 -0400186 readTimestamp += (TIMESTAMP) resamp_outchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000187
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400188 /* Write to the end of the inner receive buffer */
Thomas Tsou3952d802013-10-15 15:12:24 -0400189 rc = dnsampler->rotate((float *) outerRecvBuffer->begin(),
190 resamp_outchunk,
Tom Tsou28670fb2015-08-21 19:32:58 -0700191 recvBuffer[0]->getWriteSegment(),
Thomas Tsou3952d802013-10-15 15:12:24 -0400192 resamp_inchunk);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400193 if (rc < 0) {
194 LOG(ALERT) << "Sample rate upsampling error";
195 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000196
Tom Tsou28670fb2015-08-21 19:32:58 -0700197 /* Set history for the next chunk */
198 outerRecvBuffer->updateHistory();
Pau Espin Pedrol8e498bf2018-09-03 16:45:15 +0200199 return 0;
kurtis.heimerlce317332011-11-26 03:18:39 +0000200}
201
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400202/* Send a timestamped chunk to the device */
Tom Tsou28670fb2015-08-21 19:32:58 -0700203bool RadioInterfaceResamp::pushBuffer()
kurtis.heimerlce317332011-11-26 03:18:39 +0000204{
Tom Tsou28670fb2015-08-21 19:32:58 -0700205 int rc;
206 size_t numSent;
kurtis.heimerlce317332011-11-26 03:18:39 +0000207
Tom Tsou28670fb2015-08-21 19:32:58 -0700208 if (sendBuffer[0]->getAvailSegments() <= 0)
209 return false;
kurtis.heimerlce317332011-11-26 03:18:39 +0000210
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400211 /* Always send from the beginning of the buffer */
Tom Tsou28670fb2015-08-21 19:32:58 -0700212 rc = upsampler->rotate(sendBuffer[0]->getReadSegment(),
213 resamp_inchunk,
214 (float *) outerSendBuffer->begin(),
215 resamp_outchunk);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400216 if (rc < 0) {
217 LOG(ALERT) << "Sample rate downsampling error";
218 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000219
Thomas Tsou204a9f12013-10-29 18:34:16 -0400220 convert_float_short(convertSendBuffer[0],
Thomas Tsou9471d762013-08-20 21:24:24 -0400221 (float *) outerSendBuffer->begin(),
Tom Tsou28670fb2015-08-21 19:32:58 -0700222 powerScaling[0], 2 * resamp_outchunk);
kurtis.heimerlce317332011-11-26 03:18:39 +0000223
Tom Tsou28670fb2015-08-21 19:32:58 -0700224 numSent = mRadio->writeSamples(convertSendBuffer,
225 resamp_outchunk,
226 &underrun,
227 writeTimestamp);
228 if (numSent != resamp_outchunk) {
229 LOG(ALERT) << "Transmit error " << numSent;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400230 }
231
Tom Tsou28670fb2015-08-21 19:32:58 -0700232 writeTimestamp += resamp_outchunk;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400233
Tom Tsou28670fb2015-08-21 19:32:58 -0700234 return true;
kurtis.heimerlce317332011-11-26 03:18:39 +0000235}