blob: 6c3839a11325d4f651a4ccfe417db29b93bdbf44 [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 Tsou03e6ecf2013-08-20 20:54:54 -040058short *convertRecvBuffer = NULL;
59short *convertSendBuffer = NULL;
kurtis.heimerlce317332011-11-26 03:18:39 +000060
Thomas Tsoucb69f082013-04-08 14:18:26 -040061RadioInterfaceResamp::RadioInterfaceResamp(RadioDevice *wRadio,
62 int wReceiveOffset,
63 int wSPS,
64 GSM::Time wStartTime)
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040065 : RadioInterface(wRadio, wReceiveOffset, wSPS, wStartTime),
66 innerSendBuffer(NULL), outerSendBuffer(NULL),
67 innerRecvBuffer(NULL), outerRecvBuffer(NULL)
Thomas Tsoucb69f082013-04-08 14:18:26 -040068{
69}
70
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040071RadioInterfaceResamp::~RadioInterfaceResamp()
72{
73 close();
74}
75
76void RadioInterfaceResamp::close()
77{
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040078 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 Tsoude1648c2013-10-15 16:18:58 -040090 sendBuffer = NULL;
91 recvBuffer = NULL;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -040092
93 upsampler = NULL;
94 dnsampler = NULL;
Thomas Tsoude1648c2013-10-15 16:18:58 -040095
96 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
104 close();
105
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400106 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 Tsou3952d802013-10-15 15:12:24 -0400124 if (resamp_inchunk * NUMCHUNKS < 157 * mSPSTx * 2) {
125 LOG(ALERT) << "Invalid inner chunk size " << resamp_inchunk;
126 return false;
127 }
128
Thomas Tsou0e44ab32013-09-17 20:12:26 -0400129 if (mSPSTx == 4)
130 cutoff = RESAMP_TX4_FILTER;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400131
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400132 dnsampler = new Resampler(resamp_inrate, resamp_outrate);
Thomas Tsouc1f7c422013-10-11 13:49:55 -0400133 if (!dnsampler->init()) {
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400134 LOG(ALERT) << "Rx resampler failed to initialize";
135 return false;
136 }
137
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400138 upsampler = new Resampler(resamp_outrate, resamp_inrate);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400139 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 Tsou3952d802013-10-15 15:12:24 -0400150 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 Tsou03e6ecf2013-08-20 20:54:54 -0400158
Thomas Tsou3952d802013-10-15 15:12:24 -0400159 convertSendBuffer = new short[outerSendBuffer->size() * 2];
160 convertRecvBuffer = new short[outerRecvBuffer->size() * 2];
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400161
162 sendBuffer = innerSendBuffer;
163 recvBuffer = innerRecvBuffer;
164
165 return true;
166}
167
168/* Receive a timestamped chunk from the device */
Thomas Tsoucb69f082013-04-08 14:18:26 -0400169void RadioInterfaceResamp::pullBuffer()
kurtis.heimerlce317332011-11-26 03:18:39 +0000170{
kurtis.heimerlce317332011-11-26 03:18:39 +0000171 bool local_underrun;
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400172 int rc, num_recv;
Thomas Tsou3952d802013-10-15 15:12:24 -0400173
174 if (recvCursor > innerRecvBuffer->size() - resamp_inchunk)
175 return;
kurtis.heimerlce317332011-11-26 03:18:39 +0000176
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400177 /* Outer buffer access size is fixed */
178 num_recv = mRadio->readSamples(convertRecvBuffer,
Thomas Tsou3952d802013-10-15 15:12:24 -0400179 resamp_outchunk,
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400180 &overrun,
181 readTimestamp,
182 &local_underrun);
Thomas Tsou3952d802013-10-15 15:12:24 -0400183 if (num_recv != resamp_outchunk) {
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400184 LOG(ALERT) << "Receive error " << num_recv;
185 return;
186 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000187
Thomas Tsou9471d762013-08-20 21:24:24 -0400188 convert_short_float((float *) outerRecvBuffer->begin(),
Thomas Tsou3952d802013-10-15 15:12:24 -0400189 convertRecvBuffer, 2 * resamp_outchunk);
kurtis.heimerlce317332011-11-26 03:18:39 +0000190
191 underrun |= local_underrun;
Thomas Tsou3952d802013-10-15 15:12:24 -0400192 readTimestamp += (TIMESTAMP) resamp_outchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000193
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400194 /* Write to the end of the inner receive buffer */
Thomas Tsou3952d802013-10-15 15:12:24 -0400195 rc = dnsampler->rotate((float *) outerRecvBuffer->begin(),
196 resamp_outchunk,
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400197 (float *) (innerRecvBuffer->begin() + recvCursor),
Thomas Tsou3952d802013-10-15 15:12:24 -0400198 resamp_inchunk);
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400199 if (rc < 0) {
200 LOG(ALERT) << "Sample rate upsampling error";
201 }
kurtis.heimerlce317332011-11-26 03:18:39 +0000202
Thomas Tsou3952d802013-10-15 15:12:24 -0400203 recvCursor += resamp_inchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000204}
205
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400206/* Send a timestamped chunk to the device */
Thomas Tsoucb69f082013-04-08 14:18:26 -0400207void RadioInterfaceResamp::pushBuffer()
kurtis.heimerlce317332011-11-26 03:18:39 +0000208{
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400209 int rc, chunks, num_sent;
210 int inner_len, outer_len;
kurtis.heimerlce317332011-11-26 03:18:39 +0000211
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400212 if (sendCursor < resamp_inchunk)
kurtis.heimerlce317332011-11-26 03:18:39 +0000213 return;
214
Thomas Tsou3952d802013-10-15 15:12:24 -0400215 if (sendCursor > innerSendBuffer->size())
216 LOG(ALERT) << "Send buffer overflow";
217
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400218 chunks = sendCursor / resamp_inchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000219
Thomas Tsoufe269fe2013-10-14 23:56:51 -0400220 inner_len = chunks * resamp_inchunk;
221 outer_len = chunks * resamp_outchunk;
kurtis.heimerlce317332011-11-26 03:18:39 +0000222
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400223 /* 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.heimerlce317332011-11-26 03:18:39 +0000229
Thomas Tsou9471d762013-08-20 21:24:24 -0400230 convert_float_short(convertSendBuffer,
231 (float *) outerSendBuffer->begin(),
232 powerScaling, 2 * outer_len);
kurtis.heimerlce317332011-11-26 03:18:39 +0000233
Thomas Tsou03e6ecf2013-08-20 20:54:54 -0400234 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.heimerlce317332011-11-26 03:18:39 +0000250}