-
Notifications
You must be signed in to change notification settings - Fork 18
/
radio.c
1028 lines (903 loc) · 29 KB
/
radio.c
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "app/dtmf.h"
#include "helper/measurements.h"
#include <string.h>
#if defined(ENABLE_FMRADIO)
#include "app/fm.h"
#endif
#ifdef ENABLE_MESSENGER
#include "app/messenger.h"
#endif
#include "audio.h"
#include "bsp/dp32g030/gpio.h"
#include "dcs.h"
#include "driver/bk4819.h"
#include "driver/eeprom.h"
#include "driver/gpio.h"
#include "driver/system.h"
#include "frequencies.h"
#include "functions.h"
#include "helper/battery.h"
#include "misc.h"
#include "radio.h"
#include "settings.h"
VFO_Info_t *gTxVfo;
VFO_Info_t *gRxVfo;
VFO_Info_t *gCurrentVfo;
DCS_CodeType_t gCurrentCodeType;
DCS_CodeType_t gSelectedCodeType;
uint8_t gSelectedCode;
STEP_Setting_t gStepSetting;
UpconverterTypes gUpconverter;
VfoState_t VfoState[4];
#ifdef ENABLE_CW
const char *modulationTypeOptions[6] = {" FM", " AM", "SSB", "CW", "BYP", "RAW"};
#else
const char *modulationTypeOptions[5] = {" FM", " AM", "SSB", "BYP", "RAW"};
#endif
const char *vfoStateNames[] = {
"NORMAL", "BUSY", "BAT LOW", "DISABLE", "TIMEOUT", "ALARM", "VOL HIGH",
};
const char *powerNames[] = {"LOW", "MID", "HIGH"};
const char *bwNames[4] = {" 25k", "12.5k", "8.33k", "6.25k"};
const char *deviationNames[] = {"", "+", "-"};
bool RADIO_CheckValidChannel(uint16_t Channel, bool bCheckScanList,
uint8_t VFO) {
uint8_t Attributes;
uint8_t PriorityCh1;
uint8_t PriorityCh2;
if (!IS_MR_CHANNEL(Channel)) {
return false;
}
// Check channel is valid
Attributes = gMR_ChannelAttributes[Channel];
if ((Attributes & MR_CH_BAND_MASK) > BAND7_470MHz) {
return false;
}
if (bCheckScanList) {
switch (VFO) {
case 0:
if ((Attributes & MR_CH_SCANLIST1) == 0) {
return false;
}
PriorityCh1 = gEeprom.SCANLIST_PRIORITY_CH1[0];
PriorityCh2 = gEeprom.SCANLIST_PRIORITY_CH2[0];
break;
case 1:
if ((Attributes & MR_CH_SCANLIST2) == 0) {
return false;
}
PriorityCh1 = gEeprom.SCANLIST_PRIORITY_CH1[1];
PriorityCh2 = gEeprom.SCANLIST_PRIORITY_CH2[1];
break;
case 3:
if (((Attributes & MR_CH_SCANLIST1) == 0) && ((Attributes & MR_CH_SCANLIST2) == 0)) {
return false;
}
PriorityCh1 = gEeprom.SCANLIST_PRIORITY_CH1[0];
PriorityCh2 = gEeprom.SCANLIST_PRIORITY_CH2[0];
if (PriorityCh1 == Channel || PriorityCh2 == Channel) {
return false;
}
PriorityCh1 = gEeprom.SCANLIST_PRIORITY_CH1[1];
PriorityCh2 = gEeprom.SCANLIST_PRIORITY_CH2[1];
if (PriorityCh1 == Channel || PriorityCh2 == Channel) {
return false;
}
break;
default:
return true;
}
if (PriorityCh1 == Channel) {
return false;
}
if (PriorityCh2 == Channel) {
return false;
}
}
return true;
}
uint8_t RADIO_FindNextChannel(uint8_t Channel, int8_t Direction,
bool bCheckScanList, uint8_t VFO) {
uint8_t i;
for (i = 0; i <= MR_CHANNEL_LAST; i++) {
if (Channel == 0xFF) {
Channel = MR_CHANNEL_LAST;
} else if (Channel > MR_CHANNEL_LAST) {
Channel = MR_CHANNEL_FIRST;
}
if (RADIO_CheckValidChannel(Channel, bCheckScanList, VFO)) {
return Channel;
}
Channel += Direction;
}
return 0xFF;
}
void RADIO_InitInfo(VFO_Info_t *pInfo, uint8_t ChannelSave, uint8_t Band,
uint32_t Frequency) {
memset(pInfo, 0, sizeof(*pInfo));
pInfo->Band = Band;
pInfo->SCANLIST1_PARTICIPATION = true;
pInfo->SCANLIST2_PARTICIPATION = true;
pInfo->STEP_SETTING = STEP_25_0kHz;
pInfo->StepFrequency = 2500;
pInfo->CHANNEL_SAVE = ChannelSave;
pInfo->FrequencyReverse = false;
pInfo->OUTPUT_POWER = OUTPUT_POWER_HIGH;
pInfo->ConfigRX.Frequency = Frequency;
pInfo->ConfigTX.Frequency = Frequency;
pInfo->pRX = &pInfo->ConfigRX;
pInfo->pTX = &pInfo->ConfigTX;
pInfo->FREQUENCY_OF_DEVIATION = 1000000;
RADIO_ConfigureSquelchAndOutputPower(pInfo);
}
void RADIO_ConfigureChannel(uint8_t VFO, uint32_t Arg) {
VFO_Info_t *pRadio;
uint8_t Channel;
uint8_t Attributes;
uint8_t Band;
bool bParticipation2;
uint16_t Base;
uint8_t Data[8];
uint8_t Tmp;
uint32_t Frequency;
pRadio = &gEeprom.VfoInfo[VFO];
Channel = gEeprom.ScreenChannel[VFO];
if (IS_VALID_CHANNEL(Channel)) {
if (IS_MR_CHANNEL(Channel)) {
Channel = RADIO_FindNextChannel(Channel, RADIO_CHANNEL_UP, false, VFO);
if (Channel == 0xFF) {
Channel = gEeprom.FreqChannel[VFO];
gEeprom.ScreenChannel[VFO] = gEeprom.FreqChannel[VFO];
} else {
gEeprom.ScreenChannel[VFO] = Channel;
gEeprom.MrChannel[VFO] = Channel;
}
}
} else {
Channel = 205;
}
Attributes = gMR_ChannelAttributes[Channel];
if (Attributes == 0xFF) {
uint8_t Index;
if (IS_MR_CHANNEL(Channel)) {
Channel = gEeprom.FreqChannel[VFO];
gEeprom.ScreenChannel[VFO] = gEeprom.FreqChannel[VFO];
}
Index = Channel - FREQ_CHANNEL_FIRST;
RADIO_InitInfo(pRadio, Channel, Index, FrequencyBandTable[Index].lower);
return;
}
Band = Attributes & MR_CH_BAND_MASK;
if (Band > BAND7_470MHz) {
Band = BAND6_400MHz;
}
if (IS_MR_CHANNEL(Channel)) {
gEeprom.VfoInfo[VFO].Band = Band;
gEeprom.VfoInfo[VFO].SCANLIST1_PARTICIPATION =
!!(Attributes & MR_CH_SCANLIST1);
bParticipation2 = !!(Attributes & MR_CH_SCANLIST2);
} else {
Band = Channel - FREQ_CHANNEL_FIRST;
gEeprom.VfoInfo[VFO].Band = Band;
bParticipation2 = true;
gEeprom.VfoInfo[VFO].SCANLIST1_PARTICIPATION = true;
}
gEeprom.VfoInfo[VFO].SCANLIST2_PARTICIPATION = bParticipation2;
gEeprom.VfoInfo[VFO].CHANNEL_SAVE = Channel;
if (IS_MR_CHANNEL(Channel)) {
Base = Channel * 16;
} else {
Base = 0x0C80 + ((Channel - FREQ_CHANNEL_FIRST) * 32) + (VFO * 16);
}
if (Arg == 2 || Channel >= FREQ_CHANNEL_FIRST) {
EEPROM_ReadBuffer(Base + 8, Data, 8);
Tmp = Data[3] & 0x0F;
if (Tmp > 2) {
Tmp = 0;
}
gEeprom.VfoInfo[VFO].FREQUENCY_DEVIATION_SETTING = Tmp;
gEeprom.VfoInfo[VFO].AM_CHANNEL_MODE = (Data[3] >> 4) & 0b111;
Tmp = Data[6];
if (Tmp > STEP_100_0kHz) {
Tmp = STEP_0_01kHz;
}
gEeprom.VfoInfo[VFO].STEP_SETTING = Tmp;
gEeprom.VfoInfo[VFO].StepFrequency = StepFrequencyTable[Tmp];
Tmp = Data[7];
if (Tmp > 10) {
Tmp = 0;
}
gEeprom.VfoInfo[VFO].SCRAMBLING_TYPE = Tmp;
gEeprom.VfoInfo[VFO].ConfigRX.CodeType = (Data[2] >> 0) & 0x0F;
gEeprom.VfoInfo[VFO].ConfigTX.CodeType = (Data[2] >> 4) & 0x0F;
Tmp = Data[0];
switch (gEeprom.VfoInfo[VFO].ConfigRX.CodeType) {
case CODE_TYPE_CONTINUOUS_TONE:
if (Tmp >= 50) {
Tmp = 0;
}
break;
case CODE_TYPE_DIGITAL:
case CODE_TYPE_REVERSE_DIGITAL:
if (Tmp >= 104) {
Tmp = 0;
}
break;
default:
gEeprom.VfoInfo[VFO].ConfigRX.CodeType = CODE_TYPE_OFF;
Tmp = 0;
break;
}
gEeprom.VfoInfo[VFO].ConfigRX.Code = Tmp;
Tmp = Data[1];
switch (gEeprom.VfoInfo[VFO].ConfigTX.CodeType) {
case CODE_TYPE_CONTINUOUS_TONE:
if (Tmp >= 50) {
Tmp = 0;
}
break;
case CODE_TYPE_DIGITAL:
case CODE_TYPE_REVERSE_DIGITAL:
if (Tmp >= 104) {
Tmp = 0;
}
break;
default:
gEeprom.VfoInfo[VFO].ConfigTX.CodeType = CODE_TYPE_OFF;
Tmp = 0;
break;
}
gEeprom.VfoInfo[VFO].ConfigTX.Code = Tmp;
if (Data[4] == 0xFF) {
gEeprom.VfoInfo[VFO].FrequencyReverse = false;
gEeprom.VfoInfo[VFO].CHANNEL_BANDWIDTH = 0;
gEeprom.VfoInfo[VFO].OUTPUT_POWER = 2;
gEeprom.VfoInfo[VFO].BUSY_CHANNEL_LOCK = false;
} else {
gEeprom.VfoInfo[VFO].FrequencyReverse = !!(Data[4] & 0x01);
if(IS_MR_CHANNEL(Channel)){
gEeprom.VfoInfo[VFO].CHANNEL_BANDWIDTH = !!(Data[4] & 0x02);
}
gEeprom.VfoInfo[VFO].OUTPUT_POWER = (Data[4] >> 2) & 0x03;
gEeprom.VfoInfo[VFO].BUSY_CHANNEL_LOCK = !!(Data[4] & 0x10);
}
if (Data[5] == 0xFF) {
gEeprom.VfoInfo[VFO].DTMF_DECODING_ENABLE = false;
gEeprom.VfoInfo[VFO].DTMF_PTT_ID_TX_MODE = 0;
} else {
gEeprom.VfoInfo[VFO].DTMF_DECODING_ENABLE = !!(Data[5] & 1);
gEeprom.VfoInfo[VFO].DTMF_PTT_ID_TX_MODE = (Data[5] >> 0x01) & 0x03;
}
struct {
uint32_t Frequency;
uint32_t Offset;
} Info;
EEPROM_ReadBuffer(Base, &Info, 8);
pRadio->ConfigRX.Frequency = Info.Frequency;
if (Info.Offset >= 100000000) {
Info.Offset = 1000000;
}
gEeprom.VfoInfo[VFO].FREQUENCY_OF_DEVIATION = Info.Offset;
}
Frequency = pRadio->ConfigRX.Frequency;
Band = FREQUENCY_GetBand(Frequency);
if (Frequency < FrequencyBandTable[Band].lower) {
pRadio->ConfigRX.Frequency = FrequencyBandTable[Band].lower;
} else if (Frequency > FrequencyBandTable[Band].upper) {
pRadio->ConfigRX.Frequency = FrequencyBandTable[Band].upper;
} else if (Channel >= FREQ_CHANNEL_FIRST) {
pRadio->ConfigRX.Frequency = FREQUENCY_FloorToStep(
pRadio->ConfigRX.Frequency, gEeprom.VfoInfo[VFO].StepFrequency,
FrequencyBandTable[Band].lower);
}
if (Frequency >= 10800000 && Frequency <= 13599990) {
gEeprom.VfoInfo[VFO].FREQUENCY_DEVIATION_SETTING = FREQUENCY_DEVIATION_OFF;
} else if (!IS_MR_CHANNEL(Channel)) {
Frequency =
FREQUENCY_FloorToStep(gEeprom.VfoInfo[VFO].FREQUENCY_OF_DEVIATION,
gEeprom.VfoInfo[VFO].StepFrequency, 0);
gEeprom.VfoInfo[VFO].FREQUENCY_OF_DEVIATION = Frequency;
}
RADIO_ApplyOffset(pRadio);
memset(gEeprom.VfoInfo[VFO].Name, 0, sizeof(gEeprom.VfoInfo[VFO].Name));
if (IS_MR_CHANNEL(Channel)) {
// 16 bytes allocated but only 12 used
EEPROM_ReadBuffer(0x0F50 + (Channel * 0x10), gEeprom.VfoInfo[VFO].Name + 0,
8);
EEPROM_ReadBuffer(0x0F58 + (Channel * 0x10), gEeprom.VfoInfo[VFO].Name + 8,
2);
}
if (!gEeprom.VfoInfo[VFO].FrequencyReverse) {
gEeprom.VfoInfo[VFO].pRX = &gEeprom.VfoInfo[VFO].ConfigRX;
gEeprom.VfoInfo[VFO].pTX = &gEeprom.VfoInfo[VFO].ConfigTX;
} else {
gEeprom.VfoInfo[VFO].pRX = &gEeprom.VfoInfo[VFO].ConfigTX;
gEeprom.VfoInfo[VFO].pTX = &gEeprom.VfoInfo[VFO].ConfigRX;
}
if (gEeprom.VfoInfo[VFO].AM_CHANNEL_MODE) {
gEeprom.VfoInfo[VFO].ModulationType = gEeprom.VfoInfo[VFO].AM_CHANNEL_MODE;
gEeprom.VfoInfo[VFO].SCRAMBLING_TYPE = 0;
gEeprom.VfoInfo[VFO].DTMF_DECODING_ENABLE = false;
gEeprom.VfoInfo[VFO].ConfigRX.CodeType = CODE_TYPE_OFF;
gEeprom.VfoInfo[VFO].ConfigTX.CodeType = CODE_TYPE_OFF;
} else {
gEeprom.VfoInfo[VFO].ModulationType = false;
}
RADIO_ConfigureSquelchAndOutputPower(pRadio);
}
void RADIO_ConfigureSquelchAndOutputPower(VFO_Info_t *pInfo) {
uint8_t Txp[3];
// OLD CONFIG uint16_t Base;
FREQUENCY_Band_t Band;
Band = FREQUENCY_GetBand(pInfo->pRX->Frequency);
uint16_t Base = (Band < BAND4_174MHz) ? 0x1E60 : 0x1E00;
/* // OLD CONFIG
if (Band < BAND4_174MHz) {
Base = 0x1E60;
} else {
Base = 0x1E00;
}
*/
if (gEeprom.SQUELCH_LEVEL == 0) {
pInfo->SquelchOpenRSSIThresh = 0x00;
pInfo->SquelchOpenNoiseThresh = 0x7F;
pInfo->SquelchCloseGlitchThresh = 0xFF;
pInfo->SquelchCloseRSSIThresh = 0x00;
pInfo->SquelchCloseNoiseThresh = 0x7F;
pInfo->SquelchOpenGlitchThresh = 0xFF;
} else {
Base += gEeprom.SQUELCH_LEVEL;
EEPROM_ReadBuffer(Base + 0x00, &pInfo->SquelchOpenRSSIThresh, 1);
EEPROM_ReadBuffer(Base + 0x10, &pInfo->SquelchCloseRSSIThresh, 1);
EEPROM_ReadBuffer(Base + 0x20, &pInfo->SquelchOpenNoiseThresh, 1);
EEPROM_ReadBuffer(Base + 0x30, &pInfo->SquelchCloseNoiseThresh, 1);
EEPROM_ReadBuffer(Base + 0x40, &pInfo->SquelchCloseGlitchThresh, 1);
EEPROM_ReadBuffer(Base + 0x50, &pInfo->SquelchOpenGlitchThresh, 1);
uint16_t rssi_open = pInfo->SquelchOpenRSSIThresh;
uint16_t rssi_close = pInfo->SquelchCloseRSSIThresh;
uint16_t noise_open = pInfo->SquelchOpenNoiseThresh;
uint16_t noise_close = pInfo->SquelchCloseNoiseThresh;
uint16_t glitch_open = pInfo->SquelchOpenGlitchThresh;
uint16_t glitch_close = pInfo->SquelchCloseGlitchThresh;
#if ENABLE_SQUELCH_MORE_SENSITIVE
// make squelch a little more sensitive
//
// getting the best setting here is still experimental, bare with me
//
// note that 'noise' and 'glitch' values are inverted compared to 'rssi' values
#if 0
rssi_open = (rssi_open * 8) / 9;
noise_open = (noise_open * 9) / 8;
glitch_open = (glitch_open * 9) / 8;
#else
// even more sensitive .. use when RX bandwidths are fixed (no weak signal auto adjust)
rssi_open = (rssi_open * 1) / 2;
noise_open = (noise_open * 2) / 1;
glitch_open = (glitch_open * 2) / 1;
#endif
#else
// more sensitive .. use when RX bandwidths are fixed (no weak signal auto adjust)
rssi_open = (rssi_open * 3) / 4;
noise_open = (noise_open * 4) / 3;
glitch_open = (glitch_open * 4) / 3;
#endif
// make squelch more sensitive for HF bands
if(Band <= BAND1_50MHz) {
rssi_close = (rssi_open * 5) / 6;
noise_close = (noise_open * 6) / 5;
glitch_close = (glitch_open * 6) / 5;
}
else
{
rssi_close = (rssi_open * 9) / 10;
noise_close = (noise_open * 10) / 9;
glitch_close = (glitch_open * 10) / 9;
}
// ensure the 'close' threshold is lower than the 'open' threshold
if (rssi_close == rssi_open && rssi_close >= 2)
rssi_close -= 2;
if (noise_close == noise_open && noise_close <= 125)
noise_close += 2;
if (glitch_close == glitch_open && glitch_close <= 253)
glitch_close += 2;
pInfo->SquelchOpenRSSIThresh = (rssi_open > 255) ? 255 : rssi_open;
pInfo->SquelchCloseRSSIThresh = (rssi_close > 255) ? 255 : rssi_close;
pInfo->SquelchOpenNoiseThresh = (noise_open > 127) ? 127 : noise_open;
pInfo->SquelchCloseNoiseThresh = (noise_close > 127) ? 127 : noise_close;
pInfo->SquelchOpenGlitchThresh = (glitch_open > 255) ? 255 : glitch_open;
pInfo->SquelchCloseGlitchThresh = (glitch_close > 255) ? 255 : glitch_close;
}
/* // OLD STUFF
#else
if (pInfo->SquelchOpenNoiseThresh >= 0x80) {
pInfo->SquelchOpenNoiseThresh = 0x7F;
}
if (pInfo->SquelchCloseNoiseThresh >= 0x80) {
pInfo->SquelchCloseNoiseThresh = 0x7F;
}
//BK4819_WriteRegister(BK4819_REG_78, 0x1212);
BK4819_WriteRegister(BK4819_REG_78, 0x4848);
#endif
}
*/
Band = FREQUENCY_GetBand(pInfo->pTX->Frequency);
EEPROM_ReadBuffer(0x1ED0 + (Band * 0x10) + (pInfo->OUTPUT_POWER * 3), Txp, 3);
pInfo->TXP_CalculatedSetting = FREQUENCY_CalculateOutputPower(
Txp[0], Txp[1], Txp[2], FrequencyBandTable[Band].lower,
(FrequencyBandTable[Band].upper - FrequencyBandTable[Band].lower) / 2,
FrequencyBandTable[Band].upper, pInfo->pTX->Frequency);
}
uint32_t GetOffsetedF(VFO_Info_t *pInfo, uint32_t f) {
switch (pInfo->FREQUENCY_DEVIATION_SETTING) {
case FREQUENCY_DEVIATION_OFF:
break;
case FREQUENCY_DEVIATION_ADD:
f += pInfo->FREQUENCY_OF_DEVIATION;
break;
case FREQUENCY_DEVIATION_SUB:
f -= pInfo->FREQUENCY_OF_DEVIATION;
break;
}
return Clamp(f, FrequencyBandTable[0].lower,
FrequencyBandTable[ARRAY_SIZE(FrequencyBandTable) - 1].upper);
}
void RADIO_ApplyOffset(VFO_Info_t *pInfo) {
pInfo->ConfigTX.Frequency = GetOffsetedF(pInfo, pInfo->ConfigRX.Frequency);
}
static void RADIO_SelectCurrentVfo(void) {
if (gEeprom.CROSS_BAND_RX_TX == CROSS_BAND_OFF) {
gCurrentVfo = gRxVfo;
} else {
gCurrentVfo = &gEeprom.VfoInfo[gEeprom.TX_CHANNEL];
}
}
void RADIO_SelectVfos(void) {
if (gEeprom.CROSS_BAND_RX_TX == CROSS_BAND_CHAN_B) {
gEeprom.TX_CHANNEL = 1;
} else if (gEeprom.CROSS_BAND_RX_TX == CROSS_BAND_CHAN_A) {
gEeprom.TX_CHANNEL = 0;
} else if (gEeprom.DUAL_WATCH == DUAL_WATCH_CHAN_B) {
gEeprom.TX_CHANNEL = 1;
} else if (gEeprom.DUAL_WATCH == DUAL_WATCH_CHAN_A) {
gEeprom.TX_CHANNEL = 0;
}
gTxVfo = &gEeprom.VfoInfo[gEeprom.TX_CHANNEL];
if (gEeprom.CROSS_BAND_RX_TX == CROSS_BAND_OFF) {
gEeprom.RX_CHANNEL = gEeprom.TX_CHANNEL;
} else {
if (gEeprom.TX_CHANNEL == 0) {
gEeprom.RX_CHANNEL = 1;
} else {
gEeprom.RX_CHANNEL = 0;
}
}
gRxVfo = &gEeprom.VfoInfo[gEeprom.RX_CHANNEL];
RADIO_SelectCurrentVfo();
}
void RADIO_SetupRegisters(bool bSwitchToFunction0) {
BK4819_FilterBandwidth_t Bandwidth;
uint16_t Status;
uint16_t InterruptMask;
uint32_t Frequency;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
gEnableSpeaker = false;
BK4819_ToggleGpioOut(BK4819_GPIO0_PIN28_GREEN, false);
Bandwidth = gRxVfo->CHANNEL_BANDWIDTH;
/*if (Bandwidth != BK4819_FILTER_BW_WIDE) {
Bandwidth = BK4819_FILTER_BW_NARROWER;
}
BK4819_SetFilterBandwidth(Bandwidth);*/
switch (Bandwidth)
{
default:
Bandwidth = BK4819_FILTER_BW_WIDE;
[[fallthrough]];
case BK4819_FILTER_BW_WIDE:
case BK4819_FILTER_BW_NARROW:
case BK4819_FILTER_BW_NARROWAVIATION:
case BK4819_FILTER_BW_NARROWER:
BK4819_SetFilterBandwidth(Bandwidth);
break;
}
BK4819_ToggleGpioOut(BK4819_GPIO1_PIN29_RED, false);
BK4819_SetupPowerAmplifier(0, 0);
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1, false);
while (1) {
Status = BK4819_ReadRegister(BK4819_REG_0C);
if ((Status & 1U) == 0) { // INTERRUPT REQUEST
break;
}
BK4819_WriteRegister(BK4819_REG_02, 0);
SYSTEM_DelayMs(1);
}
BK4819_WriteRegister(BK4819_REG_3F, 0);
BK4819_WriteRegister(BK4819_REG_7D, gEeprom.MIC_SENSITIVITY_TUNING | 0xE94F);
Frequency = gRxVfo->pRX->Frequency;
BK4819_SetFrequency(Frequency);
BK4819_SetupSquelch(
gRxVfo->SquelchOpenRSSIThresh, gRxVfo->SquelchCloseRSSIThresh,
gRxVfo->SquelchOpenNoiseThresh, gRxVfo->SquelchCloseNoiseThresh,
gRxVfo->SquelchCloseGlitchThresh, gRxVfo->SquelchOpenGlitchThresh);
BK4819_PickRXFilterPathBasedOnFrequency(Frequency);
BK4819_ToggleGpioOut(BK4819_GPIO6_PIN2, true);
//OLDCONFIG BK4819_WriteRegister(BK4819_REG_48, 0xB3A8);
// AF RX Gain and DAC
//BK4819_WriteRegister(BK4819_REG_48, 0xB3A8); // 1011 00 111010 1000
BK4819_WriteRegister(BK4819_REG_48,
(11u << 12) | // ??? .. 0 ~ 15, doesn't seem to make any difference
( 0u << 10) | // AF Rx Gain-1
(gEeprom.VOLUME_GAIN << 4) | // AF Rx Gain-2
(gEeprom.DAC_GAIN << 0)); // AF DAC Gain (after Gain-1 and Gain-2)
InterruptMask = 0 | BK4819_REG_3F_SQUELCH_FOUND | BK4819_REG_3F_SQUELCH_LOST;
if (IS_NOT_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE)) {
if (!gRxVfo->ModulationType) {
uint8_t CodeType;
uint8_t Code;
CodeType = gSelectedCodeType;
Code = gSelectedCode;
if (gCssScanMode == CSS_SCAN_MODE_OFF) {
CodeType = gRxVfo->pRX->CodeType;
Code = gRxVfo->pRX->Code;
}
switch (CodeType) {
case CODE_TYPE_DIGITAL:
case CODE_TYPE_REVERSE_DIGITAL:
BK4819_SetCDCSSCodeWord(DCS_GetGolayCodeWord(CodeType, Code));
InterruptMask = 0 | BK4819_REG_3F_CxCSS_TAIL |
BK4819_REG_3F_CDCSS_FOUND | BK4819_REG_3F_CDCSS_LOST |
BK4819_REG_3F_SQUELCH_FOUND |
BK4819_REG_3F_SQUELCH_LOST;
break;
case CODE_TYPE_CONTINUOUS_TONE:
BK4819_SetCTCSSFrequency(CTCSS_Options[Code]);
BK4819_Set55HzTailDetection();
InterruptMask = 0 | BK4819_REG_3F_CxCSS_TAIL |
BK4819_REG_3F_CTCSS_FOUND | BK4819_REG_3F_CTCSS_LOST |
BK4819_REG_3F_SQUELCH_FOUND |
BK4819_REG_3F_SQUELCH_LOST;
break;
default:
BK4819_SetCTCSSFrequency(670);
BK4819_Set55HzTailDetection();
InterruptMask = 0 | BK4819_REG_3F_CxCSS_TAIL |
BK4819_REG_3F_SQUELCH_FOUND |
BK4819_REG_3F_SQUELCH_LOST;
break;
}
if (gRxVfo->SCRAMBLING_TYPE == 0 || !gSetting_ScrambleEnable) {
BK4819_DisableScramble();
} else {
BK4819_EnableScramble(gRxVfo->SCRAMBLING_TYPE - 1);
}
}
} else {
BK4819_SetCTCSSFrequency(2625);
InterruptMask = 0 | BK4819_REG_3F_CTCSS_FOUND | BK4819_REG_3F_CTCSS_LOST |
BK4819_REG_3F_SQUELCH_FOUND | BK4819_REG_3F_SQUELCH_LOST;
}
if (gEeprom.VOX_SWITCH
#if defined(ENABLE_FMRADIO)
&& !gFmRadioMode
#endif
&& IS_NOT_NOAA_CHANNEL(gCurrentVfo->CHANNEL_SAVE) &&
!gCurrentVfo->ModulationType) {
BK4819_EnableVox(gEeprom.VOX1_THRESHOLD, gEeprom.VOX0_THRESHOLD);
InterruptMask |= 0 | BK4819_REG_3F_VOX_FOUND | BK4819_REG_3F_VOX_LOST;
} else {
BK4819_DisableVox();
}
if (gRxVfo->ModulationType || !gRxVfo->DTMF_DECODING_ENABLE) {
BK4819_DisableDTMF();
} else {
BK4819_EnableDTMF();
InterruptMask |= BK4819_REG_3F_DTMF_5TONE_FOUND;
}
#ifdef ENABLE_MESSENGER
MSG_EnableRX(true);
InterruptMask |= BK4819_REG_3F_FSK_RX_SYNC | BK4819_REG_3F_FSK_RX_FINISHED | BK4819_REG_3F_FSK_FIFO_ALMOST_FULL | BK4819_REG_3F_FSK_TX_FINISHED;
#endif
BK4819_WriteRegister(BK4819_REG_3F, InterruptMask);
/*BK4819_WriteRegister(0x40, (BK4819_ReadRegister(0x40) & ~(0b11111111111)) |
0b10110101010);*/
FUNCTION_Init();
if (bSwitchToFunction0) {
FUNCTION_Select(FUNCTION_FOREGROUND);
}
}
void RADIO_SetTxParameters(void) {
BK4819_FilterBandwidth_t Bandwidth;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
gEnableSpeaker = false;
BK4819_ToggleGpioOut(BK4819_GPIO6_PIN2, false);
Bandwidth = gCurrentVfo->CHANNEL_BANDWIDTH;
/*if (Bandwidth != BK4819_FILTER_BW_WIDE) {
Bandwidth = BK4819_FILTER_BW_NARROWER;
}
BK4819_SetFilterBandwidth(Bandwidth);*/
switch (Bandwidth)
{
default:
Bandwidth = BK4819_FILTER_BW_WIDE;
[[fallthrough]];
case BK4819_FILTER_BW_WIDE:
case BK4819_FILTER_BW_NARROW:
case BK4819_FILTER_BW_NARROWAVIATION:
case BK4819_FILTER_BW_NARROWER:
BK4819_SetFilterBandwidth(Bandwidth);
break;
}
BK4819_SetFrequency(gCurrentVfo->pTX->Frequency);
BK4819_PrepareTransmit();
SYSTEM_DelayMs(10);
BK4819_PickRXFilterPathBasedOnFrequency(gCurrentVfo->pTX->Frequency);
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1, true);
SYSTEM_DelayMs(5);
BK4819_SetupPowerAmplifier(gCurrentVfo->TXP_CalculatedSetting,
gCurrentVfo->pTX->Frequency);
SYSTEM_DelayMs(10);
switch (gCurrentVfo->pTX->CodeType) {
case CODE_TYPE_CONTINUOUS_TONE:
BK4819_SetCTCSSFrequency(CTCSS_Options[gCurrentVfo->pTX->Code]);
break;
case CODE_TYPE_DIGITAL:
case CODE_TYPE_REVERSE_DIGITAL:
BK4819_SetCDCSSCodeWord(DCS_GetGolayCodeWord(gCurrentVfo->pTX->CodeType,
gCurrentVfo->pTX->Code));
break;
default:
BK4819_ExitSubAu();
break;
}
}
void RADIO_SetupAGC(bool listeningAM, bool disable)
{
static uint8_t lastSettings;
uint8_t newSettings = (listeningAM << 1) | (disable << 1);
if(lastSettings == newSettings)
return;
lastSettings = newSettings;
if(!listeningAM) { // if not actively listening AM we don't need any AM specific regulation
BK4819_SetAGC(!disable);
//BK4819_SetAGC(0);
BK4819_InitAGC();
}
else {
BK4819_SetAGC(!disable);
//BK4819_SetAGC(0);
BK4819_InitAGC();
}
}
void RADIO_SetVfoState(VfoState_t State) {
if (State == VFO_STATE_NORMAL) {
VfoState[0] = VFO_STATE_NORMAL;
VfoState[1] = VFO_STATE_NORMAL;
#if defined(ENABLE_FMRADIO)
gFM_ResumeCountdown = 0;
#endif
} else {
if (State == VFO_STATE_VOL_HIGH) {
VfoState[0] = VFO_STATE_VOL_HIGH;
VfoState[1] = VFO_STATE_TX_DISABLE;
} else {
uint8_t Channel;
if (gEeprom.CROSS_BAND_RX_TX == CROSS_BAND_OFF) {
Channel = gEeprom.RX_CHANNEL;
} else {
Channel = gEeprom.TX_CHANNEL;
}
VfoState[Channel] = State;
}
#if defined(ENABLE_FMRADIO)
gFM_ResumeCountdown = 5;
#endif
}
gUpdateDisplay = true;
}
void RADIO_PrepareTX(void) {
if (gEeprom.DUAL_WATCH != DUAL_WATCH_OFF) {
gDualWatchCountdown = 360;
gScheduleDualWatch = false;
if (!gRxVfoIsActive) {
gEeprom.RX_CHANNEL = gEeprom.TX_CHANNEL;
gRxVfo = &gEeprom.VfoInfo[gEeprom.TX_CHANNEL];
}
gRxVfoIsActive = true;
}
RADIO_SelectCurrentVfo();
#if defined(ENABLE_TX1750)
if (gAlarmState == ALARM_STATE_OFF || gAlarmState == ALARM_STATE_TX1750) {
#else
if (1) {
#endif
VfoState_t State;
if (FREQUENCY_Check(gCurrentVfo)) {
if (gCurrentVfo->BUSY_CHANNEL_LOCK &&
gCurrentFunction == FUNCTION_RECEIVE) {
State = VFO_STATE_BUSY;
} else if (gBatteryDisplayLevel == 0) {
State = VFO_STATE_BAT_LOW;
} else if (gBatteryDisplayLevel == 6) {
State = VFO_STATE_VOL_HIGH;
} else {
goto Skip;
}
} else {
State = VFO_STATE_TX_DISABLE;
}
RADIO_SetVfoState(State);
#if defined(ENABLE_TX1750)
gAlarmState = ALARM_STATE_OFF;
#endif
AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL);
//#ifdef ENABLE_DTMF_CALLING
gDTMF_ReplyState = DTMF_REPLY_NONE;
//#endif
return;
}
Skip:
#ifdef ENABLE_DTMF_CALLING
if (gDTMF_ReplyState == DTMF_REPLY_ANI) {
if (gDTMF_CallMode == DTMF_CALL_MODE_DTMF) {
gDTMF_IsTx = true;
gDTMF_CallState = DTMF_CALL_STATE_NONE;
gDTMF_TxStopCountdown = 6;
} else {
gDTMF_CallState = DTMF_CALL_STATE_CALL_OUT;
gDTMF_IsTx = false;
}
}
#endif
FUNCTION_Select(FUNCTION_TRANSMIT);
#if defined(ENABLE_TX1750)
if (gAlarmState == ALARM_STATE_OFF) {
gTxTimerCountdown = gEeprom.TX_TIMEOUT_TIMER * 120;
} else {
gTxTimerCountdown = 0;
}
#else
gTxTimerCountdown = gEeprom.TX_TIMEOUT_TIMER * 120;
#endif
gTxTimeoutReached = false;
gFlagEndTransmission = false;
gRTTECountdown = 0;
#ifdef ENABLE_DTMF_CALLING
gDTMF_ReplyState = DTMF_REPLY_NONE;
#endif
}
///////TESTING
/*
static bool tx_timeout_tone_on = false;
FUNCTION_Select(FUNCTION_TRANSMIT);
if (gNextTimeslice500ms == 1)
{ // <= 3 seconds TX time left .. start beeping
if (gNextTimeslice500ms & 1u)
{
if (!tx_timeout_tone_on)
{
tx_timeout_tone_on = true;
BK4819_PlayTone(880, true);
}
}
}
#if defined(ENABLE_TX1750)
if (gAlarmState == ALARM_STATE_OFF) {
gTxTimerCountdown = gEeprom.TX_TIMEOUT_TIMER * 120;
} else {
gTxTimerCountdown = 0;
}
#else
gTxTimerCountdown = gEeprom.TX_TIMEOUT_TIMER * 120;
#endif
gTxTimeoutReached = false;
gFlagEndTransmission = false;
gRTTECountdown = 0;
gDTMF_ReplyState = DTMF_REPLY_NONE;
}
*/
void RADIO_EnableCxCSS(void) {
switch (gCurrentVfo->pTX->CodeType) {
case CODE_TYPE_DIGITAL:
case CODE_TYPE_REVERSE_DIGITAL:
BK4819_EnableCDCSS();
SYSTEM_DelayMs(200);
break;
case CODE_TYPE_CONTINUOUS_TONE:
BK4819_EnableCTCSS();
SYSTEM_DelayMs(200);
break;
default:
break;
}
}
void RADIO_PrepareCssTX(void) {
RADIO_PrepareTX();
SYSTEM_DelayMs(200);
RADIO_EnableCxCSS();
RADIO_SetupRegisters(true);
}
void RADIO_SendEndOfTransmission(void) {
#ifdef ENABLE_ROGER_DEFAULT
if (gEeprom.ROGER == ROGER_MODE_DEFAULT) {
BK4819_PlayRoger(0);
}
#endif
#ifdef ENABLE_ROGER_MOTOTRBO
if (gEeprom.ROGER == ROGER_MODE_MOTOTRBO) {
BK4819_PlayRoger(1);
}
#endif
#ifdef ENABLE_ROGER_TPT
if (gEeprom.ROGER == ROGER_MODE_TPT) {
BK4819_PlayRoger(2);
}
#endif
#ifdef ENABLE_ROGER_MOTOTRBOT40
if (gEeprom.ROGER == ROGER_MODE_MOTOTRBOT40) {
BK4819_PlayRoger(3);
}
#endif
#ifdef ENABLE_ROGER_MOTOTRBOTLKRT80
if (gEeprom.ROGER == ROGER_MODE_MOTOTRBOTLKRT80) {
BK4819_PlayRoger(4);
}
#endif
#ifdef ENABLE_ROGER_ROGERCOBRAAM845
if (gEeprom.ROGER == ROGER_MODE_ROGERCOBRAAM845) {
BK4819_PlayRoger(5);
}
#endif
#ifdef ENABLE_ROGER_POLICE_ITA
if (gEeprom.ROGER == ROGER_MODE_POLICE_ITA) {
BK4819_PlayRoger(6);
}
#endif
#ifdef ENABLE_ROGER_UV5RC
if (gEeprom.ROGER == ROGER_MODE_UV5RC) {
BK4819_PlayRoger(7);
}
#endif
#ifdef ENABLE_ROGER_MARIO
if (gEeprom.ROGER == ROGER_MODE_MARIO) {
BK4819_PlayRoger(8);
}
#endif
#ifdef ENABLE_MDC