SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
bladerf2outputthread.cpp
Go to the documentation of this file.
1 // Copyright (C) 2018 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <algorithm>
19 
20 #include "dsp/samplesourcefifo.h"
21 
22 #include "bladerf2outputthread.h"
23 
24 BladeRF2OutputThread::BladeRF2OutputThread(struct bladerf* dev, unsigned int nbTxChannels, QObject* parent) :
25  QThread(parent),
26  m_running(false),
27  m_dev(dev),
28  m_nbChannels(nbTxChannels)
29 {
30  qDebug("BladeRF2OutputThread::BladeRF2OutputThread");
31  m_channels = new Channel[nbTxChannels];
32  m_buf = new qint16[2*DeviceBladeRF2::blockSize*nbTxChannels];
33 }
34 
36 {
37  qDebug("BladeRF2OutputThread::~BladeRF2OutputThread");
38 
39  if (m_running) {
40  stopWork();
41  }
42 
43  delete[] m_buf;
44  delete[] m_channels;
45 }
46 
48 {
49  m_startWaitMutex.lock();
50  start();
51 
52  while(!m_running) {
53  m_startWaiter.wait(&m_startWaitMutex, 100);
54  }
55 
56  m_startWaitMutex.unlock();
57 }
58 
60 {
61  m_running = false;
62  wait();
63 }
64 
66 {
67  int res;
68 
69  m_running = true;
70  m_startWaiter.wakeAll();
71 
72  unsigned int nbFifos = getNbFifos();
73 
74  if ((m_nbChannels > 0) && (nbFifos > 0))
75  {
76  int status;
77 
78  if (m_nbChannels > 1) {
79  status = bladerf_sync_config(m_dev, BLADERF_TX_X2, BLADERF_FORMAT_SC16_Q11, 128, 16384, 32, 1500);
80  } else {
81  status = bladerf_sync_config(m_dev, BLADERF_TX_X1, BLADERF_FORMAT_SC16_Q11, 64, 8192, 32, 1500);
82  }
83 
84  if (status < 0)
85  {
86  qCritical("BladeRF2OutputThread::run: cannot configure streams: %s", bladerf_strerror(status));
87  }
88  else
89  {
90  qDebug("BladeRF2OutputThread::run: start running loop");
91 
92  while (m_running)
93  {
94  if (m_nbChannels > 1)
95  {
97  res = bladerf_sync_tx(m_dev, m_buf, DeviceBladeRF2::blockSize*m_nbChannels, 0, 1500);
98  }
99  else
100  {
102  res = bladerf_sync_tx(m_dev, m_buf, DeviceBladeRF2::blockSize, 0, 1500);
103  }
104 
105  if (res < 0)
106  {
107  qCritical("BladeRF2OutputThread::run sync Rx error: %s", bladerf_strerror(res));
108  break;
109  }
110  }
111 
112  qDebug("BladeRF2OutputThread::run: stop running loop");
113  }
114  }
115  else
116  {
117  qWarning("BladeRF2OutputThread::run: no channels or FIFO allocated. Aborting");
118  }
119 
120 
121  m_running = false;
122 }
123 
125 {
126  unsigned int fifoCount = 0;
127 
128  for (unsigned int i = 0; i < m_nbChannels; i++)
129  {
130  if (m_channels[i].m_sampleFifo) {
131  fifoCount++;
132  }
133  }
134 
135  return fifoCount;
136 }
137 
138 void BladeRF2OutputThread::setLog2Interpolation(unsigned int channel, unsigned int log2_interp)
139 {
140  if (channel < m_nbChannels) {
141  m_channels[channel].m_log2Interp = log2_interp;
142  }
143 }
144 
145 unsigned int BladeRF2OutputThread::getLog2Interpolation(unsigned int channel) const
146 {
147  if (channel < m_nbChannels) {
148  return m_channels[channel].m_log2Interp;
149  } else {
150  return 0;
151  }
152 }
153 
154 void BladeRF2OutputThread::setFifo(unsigned int channel, SampleSourceFifo *sampleFifo)
155 {
156  if (channel < m_nbChannels) {
157  m_channels[channel].m_sampleFifo = sampleFifo;
158  }
159 }
160 
162 {
163  if (channel < m_nbChannels) {
164  return m_channels[channel].m_sampleFifo;
165  } else {
166  return 0;
167  }
168 }
169 
170 void BladeRF2OutputThread::callbackMO(qint16* buf, qint32 samplesPerChannel)
171 {
172  for (unsigned int channel = 0; channel < m_nbChannels; channel++)
173  {
174  if (m_channels[channel].m_sampleFifo) {
175  callbackSO(&buf[2*samplesPerChannel*channel], samplesPerChannel, channel);
176  } else {
177  std::fill(&buf[2*samplesPerChannel*channel], &buf[2*samplesPerChannel*channel]+2*samplesPerChannel, 0); // fill with zero samples
178  }
179  }
180 
181  // TODO: write a set of interpolators that can write interleaved samples in output directly
182  int status = bladerf_interleave_stream_buffer(BLADERF_TX_X2, BLADERF_FORMAT_SC16_Q11 , samplesPerChannel*m_nbChannels, (void *) buf);
183 
184  if (status < 0)
185  {
186  qCritical("BladeRF2OutputThread::callbackMI: cannot interleave buffer: %s", bladerf_strerror(status));
187  return;
188  }
189 }
190 
191 // Interpolate according to specified log2 (ex: log2=4 => decim=16). len is a number of samples (not a number of I or Q)
192 void BladeRF2OutputThread::callbackSO(qint16* buf, qint32 len, unsigned int channel)
193 {
194  if (m_channels[channel].m_sampleFifo)
195  {
196  float bal = m_channels[channel].m_sampleFifo->getRWBalance();
197 
198  if (bal < -0.25) {
199  qDebug("BladeRF2OutputThread::callbackSO: read lags: %f", bal);
200  } else if (bal > 0.25) {
201  qDebug("BladeRF2OutputThread::callbackSO: read leads: %f", bal);
202  }
203 
204  SampleVector::iterator beginRead;
205  m_channels[channel].m_sampleFifo->readAdvance(beginRead, len/(1<<m_channels[channel].m_log2Interp));
206  beginRead -= len;
207 
208  if (m_channels[channel].m_log2Interp == 0)
209  {
210  m_channels[channel].m_interpolators.interpolate1(&beginRead, buf, len*2);
211  }
212  else
213  {
214  switch (m_channels[channel].m_log2Interp)
215  {
216  case 1:
217  m_channels[channel].m_interpolators.interpolate2_cen(&beginRead, buf, len*2);
218  break;
219  case 2:
220  m_channels[channel].m_interpolators.interpolate4_cen(&beginRead, buf, len*2);
221  break;
222  case 3:
223  m_channels[channel].m_interpolators.interpolate8_cen(&beginRead, buf, len*2);
224  break;
225  case 4:
226  m_channels[channel].m_interpolators.interpolate16_cen(&beginRead, buf, len*2);
227  break;
228  case 5:
229  m_channels[channel].m_interpolators.interpolate32_cen(&beginRead, buf, len*2);
230  break;
231  case 6:
232  m_channels[channel].m_interpolators.interpolate64_cen(&beginRead, buf, len*2);
233  break;
234  default:
235  break;
236  }
237  }
238  }
239  else
240  {
241  std::fill(buf, buf+2*len, 0);
242  }
243 }
244 
void callbackSO(qint16 *buf, qint32 len, unsigned int channel=0)
void callbackMO(qint16 *buf, qint32 samplesPerChannel)
void interpolate64_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
SampleSourceFifo * getFifo(unsigned int channel)
float getRWBalance() const
static const unsigned int blockSize
unsigned int getLog2Interpolation(unsigned int channel) const
Channel * m_channels
Array of channels dynamically allocated for the given number of Tx channels.
void interpolate32_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate8_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void setFifo(unsigned int channel, SampleSourceFifo *sampleFifo)
int32_t i
Definition: decimators.h:244
void interpolate2_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
BladeRF2OutputThread(struct bladerf *dev, unsigned int nbTxChannels, QObject *parent=0)
void setLog2Interpolation(unsigned int channel, unsigned int log2_interp)
QWaitCondition m_startWaiter
void readAdvance(SampleVector::iterator &readUntil, unsigned int nbSamples)
Interpolators< qint16, SDR_TX_SAMP_SZ, 12 > m_interpolators
void interpolate1(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate16_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate4_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
qint16 * m_buf
Full buffer for SISO or MIMO operation.