-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.cpp
More file actions
218 lines (185 loc) · 7.7 KB
/
Copy pathDevice.cpp
File metadata and controls
218 lines (185 loc) · 7.7 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
/*
* Copyright (C) 2025,2026 by Jonathan Naylor G4KLX
* Copyright (C) 2026 by Adrian Musceac YO8RZZ
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Device.h"
#include "Log.h"
#include <cassert>
const size_t RX_CHANNEL = 0;
const size_t TX_CHANNEL = 0;
Device::Device(std::string deviceType, std::string modemURI, double sampleRate, float rxFreq, float txFreq,
float rxGain, float txGain, std::string rxAntenna, std::string txAntenna,
unsigned int num_pfb_channels, bool debug) :
m_soapyDeviceType(deviceType),
m_soapyDeviceURI(modemURI),
m_type(SOAPY_TYPE::NONE),
m_device(nullptr),
m_rxStream(nullptr),
m_txStream(nullptr),
m_sampleRate(sampleRate),
m_soapyTXFreq(txFreq),
m_soapyRXFreq(rxFreq),
m_soapyTXGain(txGain),
m_soapyRXGain(rxGain),
m_rxAntenna(rxAntenna),
m_txAntenna(txAntenna),
m_soapyInit(false)
{
SoapySDR::Kwargs devArgs;
SoapySDR::Kwargs rxArgs;
SoapySDR::Kwargs txArgs;
if (debug)
SoapySDR::setLogLevel(SOAPY_SDR_DEBUG);
else
SoapySDR::setLogLevel(SOAPY_SDR_INFO);
const char* LIME_DEFAULT_URI = "index=0"; // eg: addr=1111:2222 or serial=xxxxxxxx
const char* PLUTO_DEFAULT_URI = "ip:pluto.local";
if (m_soapyDeviceType.compare("plutosdr") == 0 || m_soapyDeviceType.compare("pluto") == 0) {
const char* uri = m_soapyDeviceURI.empty() ? PLUTO_DEFAULT_URI : m_soapyDeviceURI.c_str();
std::string rxBufLen = std::to_string(RX_INTERP_IN_SIZE * num_pfb_channels);
std::string txBufLen = std::to_string(TX_INTERP_OUT_SIZE * num_pfb_channels);
devArgs["driver"] = "plutosdr";
rxArgs["uri"] = uri;
txArgs["uri"] = uri;
rxArgs["bufflen"] = rxBufLen.c_str();
txArgs["bufflen"] = txBufLen.c_str();
::LogInfo("Using Pluto SDR driver uri %s", uri);
m_type = SOAPY_TYPE::PlutoSDR;
} else if (m_soapyDeviceType.compare("limesdr") == 0 || m_soapyDeviceType.compare("lime") == 0 ||
m_soapyDeviceType.compare("limemini") == 0 || m_soapyDeviceType.compare("limenet-micro") == 0) {
const char* uri = m_soapyDeviceURI.empty() ? LIME_DEFAULT_URI : m_soapyDeviceURI.c_str();
devArgs["driver"] = "lime";
rxArgs["uri"] = uri;
txArgs["uri"] = uri;
rxArgs["latency"] = "0";
txArgs["latency"] = "0";
::LogInfo("Using Lime SDR driver uri %s", uri);
m_type = SOAPY_TYPE::LimeSDR;
} else if (m_soapyDeviceType.compare("limepsb") == 0) {
const char* uri = m_soapyDeviceURI.empty() ? LIME_DEFAULT_URI : m_soapyDeviceURI.c_str();
devArgs["driver"] = "limesuiteng";
rxArgs["uri"] = uri;
txArgs["uri"] = uri;
rxArgs["latency"] = "0";
txArgs["latency"] = "0";
::LogInfo("Using LimeSuiteNG driver uri %s", uri);
m_type = SOAPY_TYPE::LimeSDR;
} else if (m_soapyDeviceType.compare("usrp") == 0) {
const char* uri = m_soapyDeviceURI.c_str();
devArgs["driver"] = "uhd";
rxArgs["uri"] = uri;
txArgs["uri"] = uri;
rxArgs["recv_frame_size"] = "1024";
::LogInfo("Using Ettus USRP driver uri %s", uri);
m_type = SOAPY_TYPE::USRP;
} else if (m_soapyDeviceType.compare("mucell") == 0) {
devArgs["driver"] = "mucell";
::LogInfo("Using the muCell driver");
m_type = SOAPY_TYPE::MuCell;
} else {
devArgs["driver"] = "sx";
::LogInfo("Using the SXCeiver driver");
m_type = SOAPY_TYPE::SXceiver;
}
try {
m_device = SoapySDR::Device::make(devArgs);
assert(m_device != nullptr);
m_device->setSampleRate(SOAPY_SDR_RX, RX_CHANNEL, m_sampleRate);
m_device->setSampleRate(SOAPY_SDR_TX, TX_CHANNEL, m_sampleRate);
m_device->setFrequency(SOAPY_SDR_RX, RX_CHANNEL, m_soapyRXFreq);
m_device->setFrequency(SOAPY_SDR_TX, TX_CHANNEL, m_soapyTXFreq);
m_device->setAntenna(SOAPY_SDR_RX, RX_CHANNEL, m_rxAntenna);
m_device->setAntenna(SOAPY_SDR_TX, TX_CHANNEL, m_txAntenna);
// Device TX calibration routine requires normal gains for RF loopback
m_device->setGain(SOAPY_SDR_RX, RX_CHANNEL, m_soapyRXGain);
m_device->setGain(SOAPY_SDR_TX, TX_CHANNEL, m_soapyTXGain);
m_device->setBandwidth(SOAPY_SDR_RX, RX_CHANNEL, m_sampleRate);
m_device->setBandwidth(SOAPY_SDR_TX, TX_CHANNEL, m_sampleRate);
m_rxStream = m_device->setupStream(SOAPY_SDR_RX, "CF32", {RX_CHANNEL}, rxArgs);
m_txStream = m_device->setupStream(SOAPY_SDR_TX, "CF32", {TX_CHANNEL}, txArgs);
m_device->activateStream(m_txStream);
m_device->activateStream(m_rxStream);
assert(m_rxStream != nullptr);
assert(m_txStream != nullptr);
m_rxMTU = m_device->getStreamMTU(m_rxStream);
m_txMTU = m_device->getStreamMTU(m_txStream);
SoapySDR::Range txGainRange = m_device->getGainRange(SOAPY_SDR_TX, TX_CHANNEL);
SoapySDR::Range rxGainRange = m_device->getGainRange(SOAPY_SDR_RX, RX_CHANNEL);
m_minTxGain = float(txGainRange.minimum()) + float(txGainRange.step());
::LogMessage("Soapy device initialized");
::LogMessage(" RX stream MTU is %u", m_rxMTU);
::LogMessage(" TX stream MTU is %u", m_txMTU);
::LogMessage(" Minimum TX gain: %f dB", txGainRange.minimum());
::LogMessage(" Maximum TX gain: %f dB", txGainRange.maximum());
::LogMessage(" Minimum RX gain: %f dB", rxGainRange.minimum());
::LogMessage(" Maximum RX gain: %f dB", rxGainRange.maximum());
m_soapyInit = true;
} catch (std::runtime_error& e) {
::LogError("Soapy device failed to initialize");
}
}
Device::~Device()
{
if (m_device != nullptr) {
assert(m_rxStream != nullptr);
assert(m_txStream != nullptr);
if (m_soapyInit) {
m_device->deactivateStream(m_rxStream, 0, 0);
m_device->deactivateStream(m_txStream, 0, 0);
}
m_device->closeStream(m_rxStream);
m_device->closeStream(m_txStream);
SoapySDR::Device::unmake(m_device);
}
::LogMessage("Soapy device closed");
m_rxStream = nullptr;
m_txStream = nullptr;
m_device = nullptr;
}
bool Device::getSoapyInit() const
{
return m_soapyInit;
}
SoapySDR::Device* Device::getDevice()
{
return m_device;
}
unsigned int Device::getRxMTU() const
{
return m_rxMTU;
}
unsigned int Device::getTxMTU() const
{
return m_txMTU;
}
void Device::setTx(bool tx)
{
if (tx) {
::LogMessage("TX ON");
if ((m_type == SOAPY_TYPE::MuCell) || (m_type == SOAPY_TYPE::SXceiver))
m_device->setAntenna(SOAPY_SDR_TX, TX_CHANNEL, "TX");
else
m_device->setGain(SOAPY_SDR_TX, TX_CHANNEL, m_soapyTXGain);
} else {
::LogMessage("TX OFF");
if ((m_type == SOAPY_TYPE::MuCell) || (m_type == SOAPY_TYPE::SXceiver))
m_device->setAntenna(SOAPY_SDR_TX, TX_CHANNEL, "NONE");
else
m_device->setGain(SOAPY_SDR_TX, TX_CHANNEL, m_minTxGain);
}
// poke GPIO/relay code here
}