forked from DVMProject/dvmfirmware-hs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.cpp
405 lines (360 loc) · 10.3 KB
/
IO.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Digital Voice Modem - DSP Firmware (Hotspot)
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / DSP Firmware (Hotspot)
*
*/
//
// Based on code from the MMDVM_HS project. (https://github.com/juribeparada/MMDVM_HS)
// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0)
//
/*
* Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX
* Copyright (C) 2016,2017,2018,2019,2020 by Andy Uribe CA6JAU
* Copyright (C) 2017 by Danilo DB4PLE
* Copyright (C) 2021 Bryan Biedenkapp N2PLL
*
* 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 "Globals.h"
#include "ADF7021.h"
#include "IO.h"
// ---------------------------------------------------------------------------
// Public Class Members
// ---------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the IO class.
/// </summary>
IO::IO():
m_started(false),
m_rxBuffer(1024U),
m_txBuffer(1024U),
m_ledCount(0U),
m_ledValue(true),
m_watchdog(0U),
m_int1Counter(0U),
m_int2Counter(0U),
m_rxFrequency(DEFAULT_FREQUENCY),
m_txFrequency(DEFAULT_FREQUENCY),
m_rfPower(0U),
m_gainMode(ADF_GAIN_AUTO)
{
initInt();
CE(HIGH);
setLEDInt(HIGH);
setPTTInt(LOW);
setDMRInt(LOW);
setP25Int(LOW);
setCOSInt(LOW);
#if !defined(BIDIR_DATA_PIN)
setTXDInt(LOW);
#endif
SCLK(LOW);
SDATA(LOW);
SLE1(LOW);
#if defined(DUPLEX)
SLE2(LOW);
#endif
selfTest();
}
/// <summary>
/// Starts air interface sampler.
/// </summary>
void IO::start()
{
if (m_started)
return;
startInt();
m_started = true;
}
/// <summary>
/// Process samples from air interface.
/// </summary>
void IO::process()
{
uint8_t bit;
uint32_t scanTime;
uint8_t control;
m_ledCount++;
if (m_started) {
// Two seconds timeout
if (m_watchdog >= 19200U) {
/*
if (m_modemState == STATE_DMR || m_modemState == STATE_P25) {
m_modemState = STATE_IDLE;
setMode(m_modemState);
}
*/
m_watchdog = 0U;
}
if (m_ledCount >= 48000U) {
m_ledCount = 0U;
m_ledValue = !m_ledValue;
setLEDInt(m_ledValue);
}
}
else {
if (m_ledCount >= 480000U) {
m_ledCount = 0U;
m_ledValue = !m_ledValue;
setLEDInt(m_ledValue);
}
return;
}
// switch off the transmitter if needed
if (m_txBuffer.getData() == 0U && m_tx) {
if (m_cwIdState) {
// check for CW ID end of transmission
m_cwIdState = false;
io.rf1Conf(m_modemState, true);
}
setRX(false);
}
if (m_rxBuffer.getData() >= 1U) {
m_rxBuffer.get(bit, control);
if (m_modemState == STATE_DMR) {
/** Digital Mobile Radio */
#if defined(DUPLEX)
if (m_duplex) {
if (m_tx)
dmrRX.databit(bit, control);
else
dmrIdleRX.databit(bit);
}
else
dmrDMORX.databit(bit);
#else
dmrDMORX.databit(bit);
#endif
}
else if (m_modemState == STATE_P25) {
/** Project 25 */
p25RX.databit(bit);
}
}
}
/// <summary>
/// Write bits to air interface.
/// </summary>
/// <param name="mode"></param>
/// <param name="samples"></param>
/// <param name="length"></param>
/// <param name="control"></param>
void IO::write(uint8_t* data, uint16_t length, const uint8_t* control)
{
if (!m_started)
return;
for (uint16_t i = 0U; i < length; i++) {
if (control == NULL)
m_txBuffer.put(data[i], MARK_NONE);
else
m_txBuffer.put(data[i], control[i]);
}
// switch the transmitter on if needed
if (!m_tx) {
setTX();
m_tx = true;
}
}
/// <summary>
/// Helper to get how much space the transmit ring buffer has for samples.
/// </summary>
/// <returns></returns>
uint16_t IO::getSpace() const
{
return m_txBuffer.getSpace();
}
/// <summary>
///
/// </summary>
/// <param name="dcd"></param>
void IO::setDecode(bool dcd)
{
if (dcd != m_dcd)
setCOSInt(dcd ? true : false);
m_dcd = dcd;
}
/// <summary>
/// Helper to set the modem air interface state.
/// </summary>
void IO::setMode(DVM_STATE modemState)
{
DVM_STATE relativeState = modemState;
if (serial.isCalState(modemState)) {
relativeState = serial.calRelativeState(modemState);
}
DEBUG3("IO::setMode(): setting RF hardware", modemState, relativeState);
rf1Conf(relativeState, true);
setDMRInt(relativeState == STATE_DMR);
setP25Int(relativeState == STATE_P25);
}
/// <summary>
/// Sets the RF parameters.
/// </summary>
/// <param name="rxFreq"></param>
/// <param name="txFreq"></param>
/// <param name="rfPower"></param>
/// <param name="gainMode"></param>
uint8_t IO::setRFParams(uint32_t rxFreq, uint32_t txFreq, uint8_t rfPower, ADF_GAIN_MODE gainMode)
{
m_rfPower = rfPower >> 2;
m_gainMode = gainMode;
// check frequency ranges
if (!(
/** 136 - 174 mhz */
((rxFreq >= VHF_MIN) && (rxFreq < VHF_MAX)) || ((txFreq >= VHF_MIN) && (txFreq < VHF_MAX)) ||
/** 216 - 225 mhz */
((rxFreq >= VHF_220_MIN) && (rxFreq < VHF_220_MAX)) || ((txFreq >= VHF_220_MIN) && (txFreq < VHF_220_MAX)) ||
/** 380 - 431 mhz */
((rxFreq >= UHF_380_MIN) && (rxFreq < UHF_380_MAX)) || ((txFreq >= UHF_380_MIN) && (txFreq < UHF_380_MAX)) ||
/** 431 - 450 mhz */
((rxFreq >= UHF_1_MIN) && (rxFreq < UHF_1_MAX)) || ((txFreq >= UHF_1_MIN) && (txFreq < UHF_1_MAX)) ||
/** 450 - 470 mhz */
((rxFreq >= UHF_2_MIN) && (rxFreq < UHF_2_MAX)) || ((txFreq >= UHF_2_MIN) && (txFreq < UHF_2_MAX)) ||
/** 470 - 520 mhz */
((rxFreq >= UHF_T_MIN) && (rxFreq < UHF_T_MAX)) || ((txFreq >= UHF_T_MIN) && (txFreq < UHF_T_MAX)) ||
/** 842 - 900 mhz */
((rxFreq >= UHF_800_MIN) && (rxFreq < UHF_800_MAX)) || ((txFreq >= UHF_800_MIN) && (txFreq < UHF_800_MAX)) ||
/** 900 - 950 mhz */
((rxFreq >= UHF_900_MIN) && (rxFreq < UHF_900_MAX)) || ((txFreq >= UHF_900_MIN) && (txFreq < UHF_900_MAX))
))
return RSN_INVALID_REQUEST;
#if defined(ZUMSPOT_ADF7021) || defined(LONESTAR_USB) || defined(SKYBRIDGE_HS)
// check hotspot configuration for single or dual ADF7021
if (!(io.hasSingleADF7021())) {
// there are two ADF7021s on the board -- are they dual-band?
if (io.isDualBand()) {
// dual band
if ((txFreq <= VHF_220_MAX) && (rxFreq <= VHF_220_MAX)) {
// turn on VHF side
io.setBandVHF(true);
}
else if ((txFreq >= UHF_1_MIN) && (rxFreq >= UHF_1_MIN)) {
// turn on UHF side
io.setBandVHF(false);
}
}
else if (!io.isDualBand()) {
// duplex board
if ((txFreq < UHF_1_MIN) || (rxFreq < UHF_1_MIN)) {
// Reject VHF frequencies
return RSN_INVALID_REQUEST;
}
}
}
#endif
m_rxFrequency = rxFreq;
m_txFrequency = txFreq;
DEBUG5("IO::setRFParams(): setting RF params", m_rxFrequency, m_txFrequency, m_rfPower, m_gainMode);
return RSN_OK;
}
/// <summary>
/// Flag indicating the TX ring buffer has overflowed.
/// </summary>
/// <returns></returns>
bool IO::hasTXOverflow()
{
return m_txBuffer.hasOverflowed();
}
/// <summary>
/// Flag indicating the RX ring buffer has overflowed.
/// </summary>
/// <returns></returns>
bool IO::hasRXOverflow()
{
return m_rxBuffer.hasOverflowed();
}
/// <summary>
///
/// </summary>
void IO::resetWatchdog()
{
m_watchdog = 0U;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
uint32_t IO::getWatchdog()
{
return m_watchdog;
}
/// <summary>
///
/// </summary>
void IO::selfTest()
{
bool ledValue = false;
uint32_t ledCount = 0U;
uint32_t blinks = 0U;
while (true) {
ledCount++;
delayUS(1000U);
if (ledCount >= 125U) {
ledCount = 0U;
ledValue = !ledValue;
setLEDInt(!ledValue);
setPTTInt(ledValue);
setDMRInt(ledValue);
setP25Int(ledValue);
setCOSInt(ledValue);
blinks++;
if (blinks > 5U)
break;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="int1"></param>
/// <param name="int2"></param>
void IO::getIntCounter(uint16_t& int1, uint16_t& int2)
{
int1 = m_int1Counter;
int2 = m_int2Counter;
m_int1Counter = 0U;
m_int2Counter = 0U;
}
// ---------------------------------------------------------------------------
// Private Class Members
// ---------------------------------------------------------------------------
#if defined(ZUMSPOT_ADF7021) || defined(LONESTAR_USB) || defined(SKYBRIDGE_HS)
/// <summary>
///
/// </summary>
/// <param name="rxFreq"></param>
/// <param name="txFreq"></param>
void IO::checkBand(uint32_t rxFreq, uint32_t txFreq)
{
// check hotspot configuration for single or dual ADF7021
if (!(io.hasSingleADF7021())) {
// there are two ADF7021s on the board -- are they dual-band?
if (io.isDualBand()) {
// dual band
if ((txFreq <= VHF_220_MAX) && (rxFreq <= VHF_220_MAX)) {
// turn on VHF side
io.setBandVHF(true);
}
else if ((txFreq >= UHF_1_MIN) && (rxFreq >= UHF_1_MIN)) {
// turn on UHF side
io.setBandVHF(false);
}
}
}
}
#endif