forked from g4klx/MMDVM_HS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.cpp
462 lines (407 loc) · 10.7 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX
* Copyright (C) 2016,2017,2018,2019,2020 by Andy Uribe CA6JAU
* Copyright (C) 2017 by Danilo DB4PLE
* 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 "Config.h"
#include "Globals.h"
#include "IO.h"
uint32_t m_frequency_rx;
uint32_t m_frequency_tx;
uint32_t m_pocsag_freq_tx;
uint8_t m_power;
CIO::CIO():
m_started(false),
m_rxBuffer(1024U),
m_txBuffer(1024U),
m_LoDevYSF(false),
m_ledCount(0U),
m_scanEnable(false),
m_scanPauseCnt(0U),
m_scanPos(0U),
m_ledValue(true),
m_watchdog(0U),
m_int1counter(0U),
m_int2counter(0U)
{
Init();
CE_pin(HIGH);
LED_pin(HIGH);
PTT_pin(LOW);
DSTAR_pin(LOW);
DMR_pin(LOW);
YSF_pin(LOW);
P25_pin(LOW);
NXDN_pin(LOW);
POCSAG_pin(LOW);
COS_pin(LOW);
DEB_pin(LOW);
#if !defined(BIDIR_DATA_PIN)
TXD_pin(LOW);
#endif
SCLK_pin(LOW);
SDATA_pin(LOW);
SLE_pin(LOW);
selfTest();
m_modeTimerCnt = 0U;
}
void CIO::selfTest()
{
bool ledValue = false;
uint32_t ledCount = 0U;
uint32_t blinks = 0U;
while(true) {
ledCount++;
delay_us(1000U);
if(ledCount >= 125U) {
ledCount = 0U;
ledValue = !ledValue;
LED_pin(!ledValue);
PTT_pin(ledValue);
DSTAR_pin(ledValue);
DMR_pin(ledValue);
YSF_pin(ledValue);
P25_pin(ledValue);
NXDN_pin(ledValue);
POCSAG_pin(ledValue);
COS_pin(ledValue);
blinks++;
if(blinks > 5U)
break;
}
}
}
void CIO::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_DSTAR || m_modemState == STATE_DMR || m_modemState == STATE_YSF || m_modemState == STATE_P25 || m_modemState == STATE_NXDN) {
m_modemState = STATE_IDLE;
setMode(m_modemState);
}
m_watchdog = 0U;
}
#if defined(CONSTANT_SRV_LED)
LED_pin(HIGH);
#elif defined(CONSTANT_SRV_LED_INVERTED)
LED_pin(LOW);
#elif defined(DISCREET_SRV_LED)
if (m_ledCount == 10000U) LED_pin(LOW);
if (m_ledCount >= 480000U) {
m_ledCount = 0U;
LED_pin(HIGH);
};
#elif defined(DISCREET_SRV_LED_INVERTED)
if (m_ledCount == 10000U) LED_pin(HIGH);
if (m_ledCount >= 480000U) {
m_ledCount = 0U;
LED_pin(LOW);
};
#else
if (m_ledCount >= 24000U) {
m_ledCount = 0U;
m_ledValue = !m_ledValue;
LED_pin(m_ledValue);
}
#endif
} else {
if (m_ledCount >= 240000U) {
m_ledCount = 0U;
m_ledValue = !m_ledValue;
LED_pin(m_ledValue);
}
return;
}
// Switch off the transmitter if needed
if (m_txBuffer.getData() == 0U && m_tx) {
if(m_cwid_state) { // check for CW ID end of transmission
m_cwid_state = false;
// Restoring previous mode
if (m_TotalModes)
io.ifConf(m_modemState_prev, true);
}
if(m_pocsag_state) { // check for POCSAG end of transmission
m_pocsag_state = false;
// Restoring previous mode
if (m_TotalModes)
io.ifConf(m_modemState_prev, true);
}
setRX(false);
}
if(m_modemState_prev == STATE_DSTAR)
scantime = SCAN_TIME;
else if(m_modemState_prev == STATE_DMR)
scantime = SCAN_TIME * 2U;
else if(m_modemState_prev == STATE_YSF)
scantime = SCAN_TIME;
else if(m_modemState_prev == STATE_P25)
scantime = SCAN_TIME;
else if(m_modemState_prev == STATE_NXDN)
scantime = SCAN_TIME;
else
scantime = SCAN_TIME;
if(m_modeTimerCnt >= scantime) {
m_modeTimerCnt = 0U;
if( (m_modemState == STATE_IDLE) && (m_scanPauseCnt == 0U) && m_scanEnable && !m_cwid_state && !m_pocsag_state) {
m_scanPos = (m_scanPos + 1U) % m_TotalModes;
#if !defined(QUIET_MODE_LEDS)
setMode(m_Modes[m_scanPos]);
#endif
io.ifConf(m_Modes[m_scanPos], true);
}
}
if (m_rxBuffer.getData() >= 1U) {
m_rxBuffer.get(bit, control);
switch (m_modemState_prev) {
case STATE_DSTAR:
dstarRX.databit(bit);
break;
case STATE_DMR:
#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
break;
case STATE_YSF:
ysfRX.databit(bit);
break;
case STATE_P25:
p25RX.databit(bit);
break;
case STATE_NXDN:
nxdnRX.databit(bit);
break;
default:
break;
}
}
}
void CIO::start()
{
m_TotalModes = 0U;
if(m_dstarEnable) {
m_Modes[m_TotalModes] = STATE_DSTAR;
m_TotalModes++;
}
if(m_dmrEnable) {
m_Modes[m_TotalModes] = STATE_DMR;
m_TotalModes++;
}
if(m_ysfEnable) {
m_Modes[m_TotalModes] = STATE_YSF;
m_TotalModes++;
}
if(m_p25Enable) {
m_Modes[m_TotalModes] = STATE_P25;
m_TotalModes++;
}
if(m_nxdnEnable) {
m_Modes[m_TotalModes] = STATE_NXDN;
m_TotalModes++;
}
#if defined(ENABLE_SCAN_MODE)
if(m_TotalModes > 1U)
m_scanEnable = true;
else {
m_scanEnable = false;
setMode(m_modemState);
}
#else
m_scanEnable = false;
setMode(m_modemState);
#endif
if (m_started)
return;
startInt();
m_started = true;
}
void CIO::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;
}
}
uint16_t CIO::getSpace() const
{
return m_txBuffer.getSpace();
}
bool CIO::hasTXOverflow()
{
return m_txBuffer.hasOverflowed();
}
bool CIO::hasRXOverflow()
{
return m_rxBuffer.hasOverflowed();
}
#if defined(ZUMSPOT_ADF7021) || defined(SKYBRIDGE_HS)
void CIO::checkBand(uint32_t frequency_rx, uint32_t frequency_tx) {
if (!(io.hasSingleADF7021())) {
// There are two ADF7021s on the board
if (io.isDualBand()) {
// Dual band
if ((frequency_tx <= VHF2_MAX) && (frequency_rx <= VHF2_MAX)) {
// Turn on VHF side
io.setBandVHF(true);
} else if ((frequency_tx >= UHF1_MIN) && (frequency_rx >= UHF1_MIN)) {
// Turn on UHF side
io.setBandVHF(false);
}
}
}
}
uint8_t CIO::checkZUMspot(uint32_t frequency_rx, uint32_t frequency_tx) {
if (!(io.hasSingleADF7021())) {
// There are two ADF7021s on the board
if (io.isDualBand()) {
// Dual band
if ((frequency_tx <= VHF2_MAX) && (frequency_rx <= VHF2_MAX)) {
// Turn on VHF side
io.setBandVHF(true);
} else if ((frequency_tx >= UHF1_MIN) && (frequency_rx >= UHF1_MIN)) {
// Turn on UHF side
io.setBandVHF(false);
}
} else if (!io.isDualBand()) {
// Duplex board
if ((frequency_tx < UHF1_MIN) || (frequency_rx < UHF1_MIN)) {
// Reject VHF frequencies
return 4U;
}
}
}
return 0U;
}
#endif
uint8_t CIO::setFreq(uint32_t frequency_rx, uint32_t frequency_tx, uint8_t rf_power, uint32_t pocsag_freq_tx)
{
// Configure power level
setPower(rf_power);
#if !defined(DISABLE_FREQ_CHECK)
// Check frequency ranges
if( !( ((frequency_rx >= VHF1_MIN)&&(frequency_rx < VHF1_MAX)) || ((frequency_tx >= VHF1_MIN)&&(frequency_tx < VHF1_MAX)) || \
((frequency_rx >= UHF1_MIN)&&(frequency_rx < UHF1_MAX)) || ((frequency_tx >= UHF1_MIN)&&(frequency_tx < UHF1_MAX)) || \
((frequency_rx >= VHF2_MIN)&&(frequency_rx < VHF2_MAX)) || ((frequency_tx >= VHF2_MIN)&&(frequency_tx < VHF2_MAX)) || \
((frequency_rx >= UHF2_MIN)&&(frequency_rx < UHF2_MAX)) || ((frequency_tx >= UHF2_MIN)&&(frequency_tx < UHF2_MAX)) ) )
return 4U;
if( !( ((pocsag_freq_tx >= VHF1_MIN)&&(pocsag_freq_tx < VHF1_MAX)) || \
((pocsag_freq_tx >= UHF1_MIN)&&(pocsag_freq_tx < UHF1_MAX)) || \
((pocsag_freq_tx >= VHF2_MIN)&&(pocsag_freq_tx < VHF2_MAX)) || \
((pocsag_freq_tx >= UHF2_MIN)&&(pocsag_freq_tx < UHF2_MAX)) ) )
return 4U;
#endif
#if !defined(DISABLE_FREQ_BAN)
// Check banned frequency ranges
if( ((frequency_rx >= BAN1_MIN)&&(frequency_rx <= BAN1_MAX)) || ((frequency_tx >= BAN1_MIN)&&(frequency_tx <= BAN1_MAX)) || \
((frequency_rx >= BAN2_MIN)&&(frequency_rx <= BAN2_MAX)) || ((frequency_tx >= BAN2_MIN)&&(frequency_tx <= BAN2_MAX)) )
return 4U;
if( ((pocsag_freq_tx >= BAN1_MIN)&&(pocsag_freq_tx <= BAN1_MAX)) || \
((pocsag_freq_tx >= BAN2_MIN)&&(pocsag_freq_tx <= BAN2_MAX)) )
return 4U;
#endif
// Check if we have a single, dualband or duplex board
#if defined(ZUMSPOT_ADF7021) || defined(SKYBRIDGE_HS)
if (checkZUMspot(frequency_rx, frequency_tx) > 0) {
return 4U;
}
#endif
// Configure frequency
m_frequency_rx = frequency_rx;
m_frequency_tx = frequency_tx;
m_pocsag_freq_tx = pocsag_freq_tx;
return 0U;
}
void CIO::setMode(MMDVM_STATE modemState)
{
#if defined(USE_ALTERNATE_POCSAG_LEDS)
if (modemState != STATE_POCSAG) {
#endif
DSTAR_pin(modemState == STATE_DSTAR);
DMR_pin(modemState == STATE_DMR);
#if defined(USE_ALTERNATE_POCSAG_LEDS)
}
#endif
#if defined(USE_ALTERNATE_NXDN_LEDS)
if (modemState != STATE_NXDN) {
#endif
YSF_pin(modemState == STATE_YSF);
P25_pin(modemState == STATE_P25);
#if defined(USE_ALTERNATE_NXDN_LEDS)
}
#endif
#if defined(USE_ALTERNATE_NXDN_LEDS)
if (modemState != STATE_YSF && modemState != STATE_P25) {
#endif
NXDN_pin(modemState == STATE_NXDN);
#if defined(USE_ALTERNATE_NXDN_LEDS)
}
#endif
#if defined(USE_ALTERNATE_POCSAG_LEDS)
if (modemState != STATE_DSTAR && modemState != STATE_DMR) {
#endif
POCSAG_pin(modemState == STATE_POCSAG);
#if defined(USE_ALTERNATE_POCSAG_LEDS)
}
#endif
}
void CIO::setDecode(bool dcd)
{
if (dcd != m_dcd) {
m_scanPauseCnt = 1U;
COS_pin(dcd ? true : false);
}
m_dcd = dcd;
}
void CIO::setLoDevYSF(bool on)
{
m_LoDevYSF = on;
}
void CIO::resetWatchdog()
{
m_watchdog = 0U;
}
uint32_t CIO::getWatchdog()
{
return m_watchdog;
}
void CIO::getIntCounter(uint16_t &int1, uint16_t &int2)
{
int1 = m_int1counter;
int2 = m_int2counter;
m_int1counter = 0U;
m_int2counter = 0U;
}