blob: 95ed16d122529607acd96bb751948a26358250ba [file] [log] [blame]
kurtis.heimerlce317332011-11-26 03:18:39 +00001/*
2 * Radio device interface with sample rate conversion
Thomas Tsou03e6ecf2013-08-20 20:54:54 -04003 * Written by Thomas Tsou <tom@tsou.cc>
kurtis.heimerlce317332011-11-26 03:18:39 +00004 *
Thomas Tsou03e6ecf2013-08-20 20:54:54 -04005 * Copyright 2011, 2012, 2013 Free Software Foundation, Inc.
kurtis.heimerlce317332011-11-26 03:18:39 +00006 *
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 Tsou03e6ecf2013-08-20 20:54:54 -040025#include "Resampler.h"
26
27extern "C" {
28#include "convert.h"
29}
30
Thomas Tsoufe269fe2013-10-14 23:56:51 -040031/* Resampling parameters for 64 MHz clocking */
32#define RESAMP_64M_INRATE 65
33#define RESAMP_64M_OUTRATE 96
34
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040035/* Resampling parameters for 100 MHz clocking */
Thomas Tsoufe269fe2013-10-14 23:56:51 -040036#define RESAMP_100M_INRATE 52
37#define RESAMP_100M_OUTRATE 75
Thomas Tsou0e44ab32013-09-17 20:12:26 -040038
Thomas Tsou3952d802013-10-15 15:12:24 -040039/* Universal resampling parameters */
40#define NUMCHUNKS 24
41
Thomas Tsou0e44ab32013-09-17 20:12:26 -040042/*
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.heimerlce317332011-11-26 03:18:39 +000050
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040051static Resampler *upsampler = NULL;
52static Resampler *dnsampler = NULL;
Thomas Tsoufe269fe2013-10-14 23:56:51 -040053static int resamp_inrate = 0;
54static int resamp_inchunk = 0;
55static int resamp_outrate = 0;
56static int resamp_outchunk = 0;
57
Thomas Tsoucb69f082013-04-08 14:18:26 -040058RadioInterfaceResamp::RadioInterfaceResamp(RadioDevice *wRadio,
59 int wReceiveOffset,
Thomas Tsou204a9f12013-10-29 18:34:16 -040060 size_t sps, size_t chan,
Thomas Tsoucb69f082013-04-08 14:18:26 -040061 GSM::Time wStartTime)
Thomas Tsou204a9f12013-10-29 18:34:16 -040062 : RadioInterface(wRadio, wReceiveOffset, sps, chan, wStartTime),
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040063 innerSendBuffer(NULL), outerSendBuffer(NULL),
64 innerRecvBuffer(NULL), outerRecvBuffer(NULL)
Thomas Tsoucb69f082013-04-08 14:18:26 -040065{
66}
67
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040068RadioInterfaceResamp::~RadioInterfaceResamp()
69{
70 close();
71}
72
73void RadioInterfaceResamp::close()
74{
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040075 delete innerSendBuffer;
76 delete outerSendBuffer;
77 delete innerRecvBuffer;
78 delete outerRecvBuffer;
79
80 delete upsampler;
81 delete dnsampler;
82
83 innerSendBuffer = NULL;
84 outerSendBuffer = NULL;
85 innerRecvBuffer = NULL;
86 outerRecvBuffer = NULL;
87
88 upsampler = NULL;
89 dnsampler = NULL;
Thomas Tsoude1648c2013-10-15 16:18:58 -040090
Thomas Tsou204a9f12013-10-29 18:34:16 -040091 if (sendBuffer.size())
92 sendBuffer[0] = NULL;
93 if (recvBuffer.size())
94 recvBuffer[0] = NULL;
95
Thomas Tsoude1648c2013-10-15 16:18:58 -040096 RadioInterface::close();
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040097}
98
99/* Initialize I/O specific objects */
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400100bool RadioInterfaceResamp::init(int type)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400101{
102 float cutoff = 1.0f;
103
Thomas Tsou204a9f12013-10-29 18:34:16 -0400104 if (mChans != 1) {
105 LOG(ALERT) << "Unsupported channel configuration " << mChans;
106 return false;
107 }
108
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400109 close();
110
Thomas Tsou204a9f12013-10-29 18:34:16 -0400111 sendBuffer.resize(1);
112 recvBuffer.resize(1);
113 convertSendBuffer.resize(1);
114 convertRecvBuffer.resize(1);
115 mReceiveFIFO.resize(1);
116
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400117 switch (type) {
118 case RadioDevice::RESAMP_64M:
119 resamp_inrate = RESAMP_64M_INRATE;
120 resamp_outrate = RESAMP_64M_OUTRATE;
121 break;
122 case RadioDevice::RESAMP_100M:
123 resamp_inrate = RESAMP_100M_INRATE;
124 resamp_outrate = RESAMP_100M_OUTRATE;
125 break;
126 case RadioDevice::NORMAL:
127 default:
128 LOG(ALERT) << "Invalid device configuration";
129 return false;
130 }
131
132 resamp_inchunk = resamp_inrate * 4;
133 resamp_outchunk = resamp_outrate * 4;
134
Thomas Tsou3952d802013-10-15 15:12:24 -0400135 if (resamp_inchunk * NUMCHUNKS < 157 * mSPSTx * 2) {
136 LOG(ALERT) << "Invalid inner chunk size " << resamp_inchunk;
137 return false;
138 }
139
Thomas Tsou0e44ab32013-09-17 20:12:26 -0400140 if (mSPSTx == 4)
141 cutoff = RESAMP_TX4_FILTER;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400142
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400143 dnsampler = new Resampler(resamp_inrate, resamp_outrate);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400144 if (!dnsampler->init()) {
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400145 LOG(ALERT) << "Rx resampler failed to initialize";
146 return false;
147 }
148
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400149 upsampler = new Resampler(resamp_outrate, resamp_inrate);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400150 if (!upsampler->init(cutoff)) {
151 LOG(ALERT) << "Tx resampler failed to initialize";
152 return false;
153 }
154
155 /*
156 * Allocate high and low rate buffers. The high rate receive
157 * buffer and low rate transmit vectors feed into the resampler
158 * and requires headroom equivalent to the filter length. Low
159 * rate buffers are allocated in the main radio interface code.
160 */
Thomas Tsou3952d802013-10-15 15:12:24 -0400161 innerSendBuffer =
162 new signalVector(NUMCHUNKS * resamp_inchunk, upsampler->len());
163 outerSendBuffer =
164 new signalVector(NUMCHUNKS * resamp_outchunk);
165 outerRecvBuffer =
166 new signalVector(resamp_outchunk, dnsampler->len());
167 innerRecvBuffer =
168 new signalVector(NUMCHUNKS * resamp_inchunk / mSPSTx);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400169
Thomas Tsou204a9f12013-10-29 18:34:16 -0400170 convertSendBuffer[0] = new short[outerSendBuffer->size() * 2];
171 convertRecvBuffer[0] = new short[outerRecvBuffer->size() * 2];
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400172
Thomas Tsou204a9f12013-10-29 18:34:16 -0400173 sendBuffer[0] = innerSendBuffer;
174 recvBuffer[0] = innerRecvBuffer;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400175
176 return true;
177}
178
179/* Receive a timestamped chunk from the device */
Thomas Tsoucb69f082013-04-08 14:18:26 -0400180void RadioInterfaceResamp::pullBuffer()
kurtis.heimerlce317332011-11-26 03:18:39 +0000181{
kurtis.heimerlce317332011-11-26 03:18:39 +0000182 bool local_underrun;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400183 int rc, num_recv;
Thomas Tsou3952d802013-10-15 15:12:24 -0400184
185 if (recvCursor > innerRecvBuffer->size() - resamp_inchunk)
186 return;
kurtis.heimerlce317332011-11-26 03:18:39 +0000187
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400188 /* Outer buffer access size is fixed */
189 num_recv = mRadio->readSamples(convertRecvBuffer,
Thomas Tsou3952d802013-10-15 15:12:24 -0400190 resamp_outchunk,
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400191 &overrun,
192 readTimestamp,
193 &local_underrun);
Thomas Tsou3952d802013-10-15 15:12:24 -0400194 if (num_recv != resamp_outchunk) {
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400195 LOG(ALERT) << "Receive error " << num_recv;
196 return;
197 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000198
Thomas Tsou9471d762013-08-20 21:24:24 -0400199 convert_short_float((float *) outerRecvBuffer->begin(),
Thomas Tsou204a9f12013-10-29 18:34:16 -0400200 convertRecvBuffer[0], 2 * resamp_outchunk);
kurtis.heimerlce317332011-11-26 03:18:39 +0000201
202 underrun |= local_underrun;
Thomas Tsou3952d802013-10-15 15:12:24 -0400203 readTimestamp += (TIMESTAMP) resamp_outchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000204
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400205 /* Write to the end of the inner receive buffer */
Thomas Tsou3952d802013-10-15 15:12:24 -0400206 rc = dnsampler->rotate((float *) outerRecvBuffer->begin(),
207 resamp_outchunk,
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400208 (float *) (innerRecvBuffer->begin() + recvCursor),
Thomas Tsou3952d802013-10-15 15:12:24 -0400209 resamp_inchunk);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400210 if (rc < 0) {
211 LOG(ALERT) << "Sample rate upsampling error";
212 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000213
Thomas Tsou3952d802013-10-15 15:12:24 -0400214 recvCursor += resamp_inchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000215}
216
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400217/* Send a timestamped chunk to the device */
Thomas Tsoucb69f082013-04-08 14:18:26 -0400218void RadioInterfaceResamp::pushBuffer()
kurtis.heimerlce317332011-11-26 03:18:39 +0000219{
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400220 int rc, chunks, num_sent;
221 int inner_len, outer_len;
kurtis.heimerlce317332011-11-26 03:18:39 +0000222
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400223 if (sendCursor < resamp_inchunk)
kurtis.heimerlce317332011-11-26 03:18:39 +0000224 return;
225
Thomas Tsou3952d802013-10-15 15:12:24 -0400226 if (sendCursor > innerSendBuffer->size())
227 LOG(ALERT) << "Send buffer overflow";
228
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400229 chunks = sendCursor / resamp_inchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000230
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400231 inner_len = chunks * resamp_inchunk;
232 outer_len = chunks * resamp_outchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000233
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400234 /* Always send from the beginning of the buffer */
235 rc = upsampler->rotate((float *) innerSendBuffer->begin(), inner_len,
236 (float *) outerSendBuffer->begin(), outer_len);
237 if (rc < 0) {
238 LOG(ALERT) << "Sample rate downsampling error";
239 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000240
Thomas Tsou204a9f12013-10-29 18:34:16 -0400241 convert_float_short(convertSendBuffer[0],
Thomas Tsou9471d762013-08-20 21:24:24 -0400242 (float *) outerSendBuffer->begin(),
243 powerScaling, 2 * outer_len);
kurtis.heimerlce317332011-11-26 03:18:39 +0000244
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400245 num_sent = mRadio->writeSamples(convertSendBuffer,
246 outer_len,
247 &underrun,
248 writeTimestamp);
249 if (num_sent != outer_len) {
250 LOG(ALERT) << "Transmit error " << num_sent;
251 }
252
253 /* Shift remaining samples to beginning of buffer */
254 memmove(innerSendBuffer->begin(),
255 innerSendBuffer->begin() + inner_len,
256 (sendCursor - inner_len) * 2 * sizeof(float));
257
258 writeTimestamp += outer_len;
259 sendCursor -= inner_len;
260 assert(sendCursor >= 0);
kurtis.heimerlce317332011-11-26 03:18:39 +0000261}