-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoTx.cpp
More file actions
218 lines (169 loc) · 6.19 KB
/
AutoTx.cpp
File metadata and controls
218 lines (169 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <complex>
#include <stdexcept>
#include <iostream> // cerr
#include <string>
#include <algorithm> // min
#include <memory> // unique_ptr
#include <cstring> // memcpy
#include <unistd.h> // usleep
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <SoapySDR/Device.hpp>
#include <SoapySDR/Registry.hpp>
#include "AutoTx.hpp"
#include "SimpleSharedRingBuffer.hpp"
using namespace std;
#if 1
#define TX_DEBUG(x) cerr << x
#else
#define TX_DEBUG(x)
#endif
void transmitter_thread(std::shared_ptr<TransmitThreadDescription> info) {
const size_t n_channels = info->n_channels;
SoapySDR::Device* slave = info->slave;
SoapySDR::Stream* tx_stream;
boost::mutex& hw_mutex = *info->hw_mutex;
std::vector<size_t> channels; // TODO
unique_ptr<SharedRingBuffer> tx_buffer = SimpleSharedRingBuffer::create(info->shm,
SharedRingBuffer::BufferMode::ManyToOne, boost::interprocess::read_write,
info->format, info->buffer_size, n_channels);
const size_t buffer_read_size = 80000; // info->buffer_size / 4;
long long buffer_duration = (1000000 * buffer_read_size) / tx_buffer->getSampleRate(); // µs
bool activate_on_demand = false;
// Setup the tx stream ready on the slave devices
{
boost::mutex::scoped_lock lock(hw_mutex);
tx_stream = slave->setupStream(SOAPY_SDR_TX, info->format /*, channels, args*/);
slave->setFrequency(SOAPY_SDR_TX, 0, tx_buffer->getCenterFrequency());
slave->setSampleRate(SOAPY_SDR_TX, 0, tx_buffer->getSampleRate());
if (info->gain > 0)
slave->setGain(SOAPY_SDR_TX, 0, info->gain);
if (activate_on_demand == false) {
TX_DEBUG("Activating TX!" << endl);
slave->activateStream(tx_stream, /* flags = */ 0, /* timeNs = */ 0, /*numElems = */ 0);
}
}
unsigned int tx_active = false;
void* shmbuffs[n_channels];
long long timestamp = 0;
TX_DEBUG("TX thread running..." << endl);
tx_buffer->setState(SharedRingBuffer::Ready);
boost::posix_time::ptime t = boost::get_system_time();
while (info->shutdown == false)
{
// Update transmission settings
if (tx_buffer->settingsChanged() && tx_active == false) {
TX_DEBUG("New TX settings!" << endl);
{
boost::mutex::scoped_lock lock(hw_mutex);
slave->setFrequency(SOAPY_SDR_TX, 0, tx_buffer->getCenterFrequency());
slave->setSampleRate(SOAPY_SDR_TX, 0, tx_buffer->getSampleRate());
buffer_duration = (1000000 * buffer_read_size) / tx_buffer->getSampleRate(); // µs
}
usleep(50);
}
size_t samples_available = tx_buffer->getSamplesAvailable();
if (samples_available) {
/*
* New samples available!
*/
bool limited = false;
if (samples_available > buffer_read_size) {
samples_available = buffer_read_size;
limited = true;
}
// Activate TX stream if already active
if (tx_active == false) {
tx_active = true;
{
boost::mutex::scoped_lock lock(hw_mutex);
slave->setFrequency(SOAPY_SDR_TX, 0, tx_buffer->getCenterFrequency());
slave->setSampleRate(SOAPY_SDR_TX, 0, tx_buffer->getSampleRate());
buffer_duration = (1000000 * buffer_read_size) / tx_buffer->getSampleRate(); // µs
}
usleep(50);
if (activate_on_demand == true) {
TX_DEBUG("Activating TX!" << endl);
// Note: No hw lock needed for operating the stream.
slave->activateStream(tx_stream, /* flags = */ 0, /* timeNs = */ 0, /*numElems = */ 0);
}
else {
TX_DEBUG("Start of burst!" << endl);
}
}
int flags = 0;
bool end_of_burst = false;
//if (tx_buffer->isTimestamped())
// flags |= SOAPY_SDR_HAS_TIME;
if (limited == false && tx_buffer->getState() == SharedRingBuffer::EndOfBurst)
{
flags |= SOAPY_SDR_END_BURST;
end_of_burst = true;
}
// Write data to the actual
int samples_written;
tx_buffer->getReadPointers(shmbuffs);
/* Remark: hw_mutex is now used with stream operations! */
samples_written = slave->writeStream(tx_stream, shmbuffs, samples_available, flags, timestamp);
TX_DEBUG("writeStream " << tx_buffer->getSamplesAvailable() << " " << samples_available << " " << samples_written << " " << limited << endl);
if (samples_written < 0)
throw runtime_error("Write failed!");
t = boost::get_system_time();
tx_buffer->read(samples_written, timestamp);
#if 0
if ((size_t)samples_written != samples_available) { // Incomplete write?
// Sleep some time before next write
//usleep((750000 * (buffer_read_size - samples_written)) / tx_buffer->getSampleRate());
}
#endif
if (end_of_burst) {
tx_active = false;
TX_DEBUG("End of burst" << endl);
if (activate_on_demand == true) {
TX_DEBUG("Deactivating TX!" << endl);
// Note: No hw lock needed for operating the stream.
slave->deactivateStream(tx_stream, /* flags = */ 0, /* timeNs = */ 0);
}
tx_buffer->reset();
tx_buffer->setState(SharedRingBuffer::Ready);
}
}
else if (tx_active == true) {
/*
* TX running but no new samples
*/
// Wait for the ring buffer head to move forward.
// This timeout must be generous! If the code continues to call `readStreamStatus`
// timing of this loop is ruined tx buffer underflow quaranteed.
tx_buffer->wait_head(500 * 1000 /* us */);
if (tx_buffer->isEmpty() == false)
continue;
auto xxx = boost::get_system_time();
// Check if TX-buffer underflow has occured
int ret;
int flags = 0;
size_t channelMask = (1 << n_channels) - 1;
long long timeNs = 0;
ret = slave->readStreamStatus(tx_stream, channelMask, flags, timeNs, /* timeoutUs */ 0);
TX_DEBUG(" s " << (boost::get_system_time() - xxx).total_microseconds() << " " << boost::get_system_time() << endl);
if (ret == SOAPY_SDR_UNDERFLOW) {
TX_DEBUG("TX buffer underflow! " << channelMask << " " << flags << endl);
tx_active = false;
if (activate_on_demand == true) {
TX_DEBUG("Deactivating TX!" << endl);
boost::mutex::scoped_lock lock(hw_mutex);
slave->deactivateStream(tx_stream, /* flags = */ 0, /* timeNs = */ 0);
}
tx_buffer->reset();
tx_buffer->setState(SharedRingBuffer::Ready);
continue;
}
}
else {
// Nothing to do and TX is idling.
// Wait for ring buffer's head to move
tx_buffer->wait_head(1000 * 1000 /* us */);
}
}
TX_DEBUG("TX thread terminating..." << endl);
}