-
Notifications
You must be signed in to change notification settings - Fork 4
/
IO.cpp
434 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Hotspot Firmware
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2015,2016,2020 Jonathan Naylor, G4KLX
* Copyright (C) 2016,2017,2018,2019,2020 Andy Uribe, CA6JAU
* Copyright (C) 2017 Danilo, DB4PLE
* Copyright (C) 2021,2024 Bryan Biedenkapp, N2PLL
*
*/
#include "Globals.h"
#include "ADF7021.h"
#include "IO.h"
// ---------------------------------------------------------------------------
// Public Class Members
// ---------------------------------------------------------------------------
/* Initializes a new instance of the IO class. */
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)
{
/* stub */
}
/* Initializes the air interface sampler. */
void IO::init()
{
initInt();
CE(HIGH);
setLEDInt(HIGH);
setPTTInt(LOW);
setDMRInt(LOW);
setP25Int(LOW);
setNXDNInt(LOW);
setCOSInt(LOW);
#if !defined(BIDIR_DATA_PIN)
setTXDInt(LOW);
#endif
SCLK(LOW);
SDATA(LOW);
SLE1(LOW);
#if defined(DUPLEX)
SLE2(LOW);
#endif
selfTest();
}
/* Starts air interface sampler. */
void IO::start()
{
if (m_started)
return;
startInt();
m_started = true;
}
/* Process samples from air interface. */
void IO::process()
{
uint8_t bit;
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_NXDN) {
#if defined(DUPLEX)
if (m_modemState == STATE_DMR && m_tx)
dmrTX.setStart(false);
#endif
}
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;
DEBUG2("IO::process() setting modem state", m_modemState);
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 && !m_forceDMO) {
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);
}
else if (m_modemState == STATE_NXDN) {
/** Next Generation Digital Narrowband */
nxdnRX.databit(bit);
}
}
}
/* Write bits to air interface. */
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;
}
}
/* Helper to get how much space the transmit ring buffer has for samples. */
uint16_t IO::getSpace() const
{
return m_txBuffer.getSpace();
}
/* */
void IO::setDecode(bool dcd)
{
if (dcd != m_dcd)
setCOSInt(dcd ? true : false);
m_dcd = dcd;
}
/* Helper to set the modem air interface state. */
void IO::setMode(DVM_STATE modemState)
{
DVM_STATE relativeState = modemState;
if (serial.isCalState(modemState)) {
relativeState = serial.calRelativeState(modemState);
}
DEBUG3("IO::setMode() setting modem state", modemState, relativeState);
rf1Conf(relativeState, true);
DEBUG4("IO::setMode() setting lights", relativeState == STATE_DMR, relativeState == STATE_P25, relativeState == STATE_NXDN);
setDMRInt(relativeState == STATE_DMR);
setP25Int(relativeState == STATE_P25);
setNXDNInt(relativeState == STATE_NXDN);
}
/* Sets the RF parameters. */
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;
}
/* Flag indicating the TX ring buffer has overflowed. */
bool IO::hasTXOverflow()
{
return m_txBuffer.hasOverflowed();
}
/* Flag indicating the RX ring buffer has overflowed. */
bool IO::hasRXOverflow()
{
return m_rxBuffer.hasOverflowed();
}
/* */
void IO::resetWatchdog()
{
m_watchdog = 0U;
}
/* */
uint32_t IO::getWatchdog()
{
return m_watchdog;
}
/* */
void IO::selfTest()
{
bool ledValue = false;
for (uint8_t i = 0; i < 6; i++) {
ledValue = !ledValue;
// We exclude PTT to avoid trigger the transmitter
setLEDInt(ledValue);
setCOSInt(ledValue);
setDMRInt(ledValue);
setP25Int(ledValue);
setNXDNInt(ledValue);
delayUS(125000);
}
// blinkin lights
setLEDInt(false);
setCOSInt(false);
setDMRInt(false);
setP25Int(false);
setNXDNInt(false);
delayUS(125000U);
setLEDInt(true);
setCOSInt(false);
setDMRInt(false);
setP25Int(false);
delayUS(125000U);
setLEDInt(false);
setCOSInt(true);
setDMRInt(false);
setP25Int(false);
delayUS(125000U);
setLEDInt(false);
setCOSInt(false);
setDMRInt(true);
setP25Int(false);
delayUS(125000U);
setLEDInt(false);
setCOSInt(false);
setDMRInt(false);
setP25Int(true);
delayUS(125000U);
setLEDInt(false);
setCOSInt(false);
setDMRInt(false);
setP25Int(false);
setNXDNInt(true);
delayUS(125000U);
setLEDInt(false);
setCOSInt(false);
setDMRInt(false);
setP25Int(true);
setNXDNInt(false);
delayUS(125000U);
setLEDInt(false);
setCOSInt(false);
setDMRInt(true);
setP25Int(false);
setNXDNInt(false);
delayUS(125000U);
setLEDInt(false);
setCOSInt(true);
setDMRInt(false);
setP25Int(false);
setNXDNInt(false);
delayUS(125000U);
setLEDInt(true);
setCOSInt(false);
setDMRInt(false);
setP25Int(false);
setNXDNInt(false);
delayUS(125000U);
setLEDInt(false);
setCOSInt(false);
setDMRInt(false);
setP25Int(false);
setNXDNInt(false);
delayUS(125000U);
}
/* */
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)
/* */
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