-
Notifications
You must be signed in to change notification settings - Fork 4
/
ADF7021.cpp
1379 lines (1158 loc) · 46.1 KB
/
ADF7021.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
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
// 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) 2020,2021 Jonathan Naylor, G4KLX
* Copyright (C) 2016 Jim McLaughlin, KI6ZUM
* Copyright (C) 2016,2017,2018,2019,2020 Andy Uribe, CA6JAU
* Copyright (C) 2017 Danilo, DB4PLE
* Copyright (C) 2021 Bryan Biedenkapp, N2PLL
*
*/
#include <math.h>
#include "Globals.h"
#include "ADF7021.h"
/*
* Some of the code is based on work of Guus Van Dooren PE1PLM:
* https://github.com/ki6zum/gmsk-dstar/blob/master/firmware/dvmega/dvmega.ino
*/
#if defined(ENABLE_ADF7021)
// ---------------------------------------------------------------------------
// Globals
// ---------------------------------------------------------------------------
volatile bool toTxRequest = false;
volatile bool toRxRequest = false;
volatile bool even = true;
static uint32_t lastClk = 2U;
volatile uint32_t AD7021_CONTROL;
uint32_t ADF7021_RX_REG0;
uint32_t ADF7021_TX_REG0;
uint32_t ADF7021_REG1;
uint32_t ADF7021_REG2;
uint32_t ADF7021_REG3;
uint32_t ADF7021_REG4;
uint32_t ADF7021_REG10;
uint32_t ADF7021_REG13;
uint32_t div2;
uint32_t f_div;
uint8_t RX_N_Divider; // Rx - 8-bit Integer_N
uint16_t RX_F_Divider; // Rx - 15-bit Frational_N
uint8_t TX_N_Divider; // Tx - 8-bit Integer_N
uint16_t TX_F_Divider; // Tx - 15-bit Frational_N
uint16_t dmrDev;
uint16_t p25Dev;
uint16_t nxdnDev;
int8_t m_dmrDiscBWAdj;
int8_t m_p25DiscBWAdj;
int8_t m_nxdnDiscBWAdj;
int8_t m_dmrPostBWAdj;
int8_t m_p25PostBWAdj;
int8_t m_nxdnPostBWAdj;
bool m_afcEnable;
uint8_t m_afcKI;
uint8_t m_afcKP;
uint8_t m_afcRange;
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
/* */
static void AD7021_IOCTL_Shift()
{
for (int i = 31; i >= 0; i--) {
if (ADF_BIT_READ(AD7021_CONTROL, i) == HIGH)
io.SDATA(HIGH);
else
io.SDATA(LOW);
io.delayBit();
io.SCLK(HIGH);
io.delayBit();
io.SCLK(LOW);
}
// to keep SDATA signal at defined level when idle (not required)
io.SDATA(LOW);
}
/* */
static void AD7021_IOCTL_SLEPulse()
{
io.SLE1(HIGH);
io.delayBit();
io.SLE1(LOW);
}
/* */
static void AD7021_1_IOCTL(bool doSle = true)
{
AD7021_IOCTL_Shift();
if (doSle)
AD7021_IOCTL_SLEPulse();
}
#if defined(DUPLEX)
/* */
static void AD7021_2_IOCTL_SLEPulse()
{
io.SLE2(HIGH);
io.delayBit();
io.SLE2(LOW);
}
/* */
static void AD7021_2_IOCTL(bool doSle = true)
{
AD7021_IOCTL_Shift();
if (doSle)
AD7021_2_IOCTL_SLEPulse();
}
#endif
// ---------------------------------------------------------------------------
// Public Class Members
// ---------------------------------------------------------------------------
/* Hardware interrupt handler. */
void IO::interrupt1()
{
uint8_t bit = 0U;
if (!m_started)
return;
uint8_t clk = CLK();
// this is to prevent activation by spurious interrupts
// which seem to happen if you send out an control word
// needs investigation
// this workaround will fail if only rising or falling edge
// is used to trigger the interrupt !!!!
// TODO: figure out why sending the control word seems to issue interrupts
// possibly this is a design problem of the RF7021 board or too long wires
// on the breadboard build
// but normally this will not hurt too much
if (clk == lastClk)
return;
else
lastClk = clk;
// we set the TX bit at TXD low, sampling of ADF7021 happens at rising clock
if (m_tx && clk == 0U) {
m_txBuffer.get(bit, m_control);
even = !even;
#if defined(BIDIR_DATA_PIN)
if (bit)
setRXDInt(HIGH);
else
setRXDInt(LOW);
#else
if (bit)
setTXDInt(HIGH);
else
setTXDInt(LOW);
#endif
// wait a brief period before raising SLE
if (toTxRequest == true) {
asm volatile(
"nop \n\t"
"nop \n\t"
"nop \n\t"
);
// SLE Pulse, should be moved out of here into class method
// according to datasheet in 4FSK we have to deliver this before 1/4 tbit == 26uS
SLE1(HIGH);
asm volatile(
"nop \n\t"
"nop \n\t"
"nop \n\t"
);
SLE1(LOW);
SDATA(LOW);
// now do housekeeping
toTxRequest = false;
// first tranmittted bit is always the odd bit
even = ADF7021_EVEN_BIT;
}
}
// we sample the RX bit at rising TXD clock edge, so TXD must be 1 and we are not in tx mode
if (!m_tx && clk == 1U && !m_duplex) {
if (RXD1())
bit = 1U;
else
bit = 0U;
m_rxBuffer.put(bit, m_control);
}
if (toRxRequest && even == ADF7021_EVEN_BIT && m_tx && clk == 0U) {
// that is absolutely crucial in 4FSK, see datasheet:
// enable sle after 1/4 tBit == 26uS when sending MSB (even == false) and clock is low
delayUS(26U);
// SLE Pulse, should be moved out of here into class method
SLE1(HIGH);
asm volatile(
"nop \n\t"
"nop \n\t"
"nop \n\t"
);
SLE1(LOW);
SDATA(LOW);
// now do housekeeping
m_tx = false;
toRxRequest = false;
// last tranmittted bit is always the even bit
// since the current bit is a transitional "don't care" bit, never transmitted
even = !ADF7021_EVEN_BIT;
}
m_watchdog++;
m_int1Counter++;
}
#if defined(DUPLEX)
/* Hardware interrupt handler. */
void IO::interrupt2()
{
uint8_t bit = 0U;
if (m_duplex) {
if (RXD2())
bit = 1U;
else
bit = 0U;
m_rxBuffer.put(bit, m_control);
}
m_int2Counter++;
}
#endif
/* Sets the ADF7021 RF configuration. */
void IO::rf1Conf(DVM_STATE modemState, bool reset)
{
uint32_t txFrequencyTmp, rxFrequencyTmp;
DEBUG4("IO::rf1Conf() ADF1 (Tx/Rx); modemState/reset/rxGain", modemState, reset, m_gainMode);
#if defined (ZUMSPOT_ADF7021) || defined(SKYBRIDGE_HS)
io.checkBand(m_rxFrequency, m_txFrequency);
#endif
// Toggle CE pin for ADF7021 reset
if (reset) {
CE(LOW);
delayReset();
CE(HIGH);
delayReset();
}
/*
** VCO/Oscillator (Register 1)
*/
configureBand();
/*
** Fractional-N Synthesizer (Register 0)
*/
float divider = 0.0f;
if (div2 == 1U)
divider = (m_rxFrequency - 100000) / (ADF7021_PFD / 2U);
else
divider = (m_rxFrequency - 100000) / ADF7021_PFD;
// calculate Integer_N and Fractional_N divider values for Rx
RX_N_Divider = floor(divider);
divider = (divider - RX_N_Divider) * 32768;
RX_F_Divider = floor(divider + 0.5);
// setup rx register 0
ADF7021_RX_REG0 = (uint32_t)ADF7021_REG0_ADDR; // Register Address 0
#if defined(BIDIR_DATA_PIN)
ADF7021_RX_REG0 |= (uint32_t)0b01001 << 27; // Mux regulator/receive
#else
ADF7021_RX_REG0 |= (uint32_t)0b01011 << 27; // Mux regulator/uart-spi enabled/receive
#endif
ADF7021_RX_REG0 |= (uint32_t)RX_N_Divider << 19; // Frequency - 8-bit Int_N
ADF7021_RX_REG0 |= (uint32_t)RX_F_Divider << 4; // Frequency - 15-bit Frac_N
if (div2 == 1U)
divider = m_txFrequency / (ADF7021_PFD / 2U);
else
divider = m_txFrequency / ADF7021_PFD;
// calculate Integer_N and Fractional_N divider values for Tx
TX_N_Divider = floor(divider);
divider = (divider - TX_N_Divider) * 32768;
TX_F_Divider = floor(divider + 0.5);
// setup tx register 0
ADF7021_TX_REG0 = (uint32_t)ADF7021_REG0_ADDR; // Register Address 0
#if defined(BIDIR_DATA_PIN)
ADF7021_TX_REG0 |= (uint32_t)0b01000 << 27; // Mux regulator/transmit
#else
ADF7021_TX_REG0 |= (uint32_t)0b01010 << 27; // Mux regulator/uart-spi enabled/transmit
#endif
ADF7021_TX_REG0 |= (uint32_t)TX_N_Divider << 19; // Frequency - 8-bit Int_N
ADF7021_TX_REG0 |= (uint32_t)TX_F_Divider << 4; // Frequency - 15-bit Frac_N
// configure ADF Tx/RX
configureTxRx(modemState);
// write registers
/*
** VCO/Oscillator (Register 1)
*/
AD7021_CONTROL = ADF7021_REG1;
AD7021_1_IOCTL();
/*
** Tx/Rx Clock (Register 3)
*/
AD7021_CONTROL = ADF7021_REG3;
AD7021_1_IOCTL();
DEBUG3("IO::rf1Conf() ADF1 REG3 =", (ADF7021_REG3 >> 16 & 0xFFFFU), (ADF7021_REG3 & 0xFFFFU));
/*
** Demodulator Setup (Register 4)
*/
AD7021_CONTROL = ADF7021_REG4;
AD7021_1_IOCTL();
DEBUG3("IO::rf1Conf() ADF1 REG4 =", (ADF7021_REG4 >> 16 & 0xFFFFU), (ADF7021_REG4 & 0xFFFFU));
/*
** IF Fine Cal Setup (Register 6)
*/
AD7021_CONTROL = ADF7021_REG6;
AD7021_1_IOCTL();
/*
** IF Coarse Cal Setup (Register 5)
*/
AD7021_CONTROL = ADF7021_REG5;
AD7021_1_IOCTL();
// delay for filter calibration
delayIfCal();
/*
** N Register (Frequency) (Register 0)
*/
setRX();
/*
** Transmit Modulation (Register 2)
*/
AD7021_CONTROL = ADF7021_REG2;
AD7021_1_IOCTL();
DEBUG3("IO::rf1Conf() ADF1 REG2 =", (ADF7021_REG2 >> 16 & 0xFFFFU), (ADF7021_REG3 & 0xFFFFU));
/*
** Test DAC (Register 14)
*/
#if defined(TEST_DAC)
AD7021_CONTROL = 0x0000001E;
#else
AD7021_CONTROL = 0x0000000E;
#endif
AD7021_1_IOCTL();
/*
** AGC (Register 9)
*/
switch (m_gainMode) {
case ADF_GAIN_AUTO_LIN:
AD7021_CONTROL = 0x100231E9; // AGC ON, LNA high linearity
break;
case ADF_GAIN_LOW:
AD7021_CONTROL = 0x120631E9; // AGC OFF, low gain, LNA high linearity
break;
case ADF_GAIN_HIGH:
AD7021_CONTROL = 0x00A631E9; // AGC OFF, high gain
break;
case ADF_GAIN_AUTO:
default:
AD7021_CONTROL = 0x000231E9; // AGC ON, normal operation
break;
}
AD7021_1_IOCTL();
/*
** AFC (Register 10)
*/
AD7021_CONTROL = ADF7021_REG10;
AD7021_1_IOCTL();
DEBUG3("IO::rf1Conf() ADF1 REG10 =", (ADF7021_REG10 >> 16 & 0xFFFFU), (ADF7021_REG10 & 0xFFFFU));
/*
** Sync Word Detect (Register 11)
*/
AD7021_CONTROL = 0x0000003B;
AD7021_1_IOCTL();
/*
** SWD/Threshold Setup (Register 12)
*/
AD7021_CONTROL = 0x0000010C;
AD7021_1_IOCTL();
/*
** 3FSK/4FSK Demod (Register 13)
*/
AD7021_CONTROL = ADF7021_REG13;
AD7021_1_IOCTL();
DEBUG3("IO::rf1Conf() ADF1 REG13 =", (ADF7021_REG13 >> 16 & 0xFFFFU), (ADF7021_REG13 & 0xFFFFU));
/*
** Test Mode (Register 15)
*/
#if defined(TEST_TX)
PTT(HIGH);
AD7021_CONTROL = ADF7021_TX_REG0;
AD7021_1_IOCTL();
AD7021_CONTROL = 0x000E010F;
#else
AD7021_CONTROL = 0x000E000F;
#endif
AD7021_1_IOCTL();
#if defined(DUPLEX)
// if duplex -- auto setup the second ADF7021
if (m_duplex && (modemState != STATE_CW))
rf2Conf(modemState);
#endif
}
#if defined(DUPLEX)
/* Sets the ADF7021 RF configuration. */
void IO::rf2Conf(DVM_STATE modemState)
{
DEBUG3("IO::rf2Conf() ADF2 (Rx); modemState/rxGain", modemState, m_gainMode);
// configure ADF Tx/RX
configureTxRx(modemState);
// write registers
/*
** VCO/Oscillator (Register 1)
*/
AD7021_CONTROL = ADF7021_REG1;
AD7021_2_IOCTL();
/*
** Tx/Rx Clock (Register 3)
*/
AD7021_CONTROL = ADF7021_REG3;
AD7021_2_IOCTL();
DEBUG3("IO::rf2Conf() ADF2 REG3 =", (ADF7021_REG3 >> 16 & 0xFFFFU), (ADF7021_REG3 & 0xFFFFU));
/*
** Demodulator Setup (Register 4)
*/
AD7021_CONTROL = ADF7021_REG4;
AD7021_2_IOCTL();
DEBUG3("IO::rf2Conf() ADF2 REG4 =", (ADF7021_REG4 >> 16 & 0xFFFFU), (ADF7021_REG4 & 0xFFFFU));
/*
** IF Fine Cal Setup (Register 6)
*/
AD7021_CONTROL = ADF7021_REG6;
AD7021_2_IOCTL();
/*
** IF Coarse Cal Setup (Register 5)
*/
AD7021_CONTROL = ADF7021_REG5;
AD7021_2_IOCTL();
// delay for filter calibration
delayIfCal();
/*
** N Register (Frequency) (Register 0)
*/
// set to RX only
AD7021_CONTROL = ADF7021_RX_REG0;
AD7021_2_IOCTL();
/*
** Transmit Modulation (Register 2)
*/
AD7021_CONTROL = ADF7021_REG2;
AD7021_2_IOCTL();
DEBUG3("IO::rf2Conf() ADF2 REG2 =", (ADF7021_REG2 >> 16 & 0xFFFFU), (ADF7021_REG3 & 0xFFFFU));
/*
** Test DAC (Register 14)
*/
AD7021_CONTROL = 0x0000000E;
AD7021_2_IOCTL();
/*
** AGC (Register 9)
*/
switch (m_gainMode) {
case ADF_GAIN_AUTO_LIN:
AD7021_CONTROL = 0x100231E9; // AGC ON, LNA high linearity
break;
case ADF_GAIN_LOW:
AD7021_CONTROL = 0x120631E9; // AGC OFF, low gain, LNA high linearity
break;
case ADF_GAIN_HIGH:
AD7021_CONTROL = 0x00A631E9; // AGC OFF, high gain
break;
case ADF_GAIN_AUTO:
default:
AD7021_CONTROL = 0x000231E9; // AGC ON, normal operation
break;
}
AD7021_2_IOCTL();
/*
** AFC (Register 10)
*/
AD7021_CONTROL = ADF7021_REG10;
AD7021_2_IOCTL();
DEBUG3("IO::rf2Conf() ADF2 REG10 =", (ADF7021_REG10 >> 16 & 0xFFFFU), (ADF7021_REG10 & 0xFFFFU));
/*
** Sync Word Detect (Register 11)
*/
AD7021_CONTROL = 0x0000003B;
AD7021_2_IOCTL();
/*
** SWD/Threshold Setup (Register 12)
*/
AD7021_CONTROL = 0x0000010C;
AD7021_2_IOCTL();
/*
** 3FSK/4FSK Demod (Register 13)
*/
AD7021_CONTROL = ADF7021_REG13;
AD7021_2_IOCTL();
DEBUG3("IO::rf2Conf() ADF2 REG13 =", (ADF7021_REG13 >> 16 & 0xFFFFU), (ADF7021_REG13 & 0xFFFFU));
/*
** Test Mode (Register 15)
*/
AD7021_CONTROL = 0x000E000F;
AD7021_2_IOCTL();
}
#endif // DUPLEX
/* Sets the deviation levels. */
void IO::setDeviations(uint8_t dmrTXLevel, uint8_t p25TXLevel, uint8_t nxdnTXLevel)
{
dmrDev = uint16_t((ADF7021_DEV_DMR * uint16_t(dmrTXLevel)) / 128U);
p25Dev = uint16_t((ADF7021_DEV_P25 * uint16_t(p25TXLevel)) / 128U);
nxdnDev = uint16_t((ADF7021_DEV_NXDN * uint16_t(nxdnTXLevel)) / 128U);
}
/* Sets the RF adjustment parameters. */
void IO::setRFAdjust(int8_t dmrDiscBWAdj, int8_t p25DiscBWAdj, int8_t nxdnDiscBWAdj, int8_t dmrPostBWAdj, int8_t p25PostBWAdj, int8_t nxdnPostBWADJ)
{
m_dmrDiscBWAdj = dmrDiscBWAdj;
m_p25DiscBWAdj = p25DiscBWAdj;
m_nxdnDiscBWAdj = nxdnDiscBWAdj;
m_dmrPostBWAdj = dmrPostBWAdj;
m_p25PostBWAdj = p25PostBWAdj;
m_nxdnPostBWAdj = nxdnPostBWADJ;
DEBUG4("IO::setRFAdjust() RF adjustment, discBW", dmrDiscBWAdj, p25DiscBWAdj, nxdnDiscBWAdj);
DEBUG4("IO::setRFAdjust() RF adjustment, postBW", dmrPostBWAdj, p25PostBWAdj, nxdnPostBWADJ);
}
/* Sets the RF AFC parameters. */
void IO::setAFCParams(bool afcEnable, uint8_t afcKI, uint8_t afcKP, uint8_t afcRange)
{
m_afcEnable = afcEnable;
m_afcKI = afcKI;
m_afcKP = afcKP;
m_afcRange = afcRange;
DEBUG5("IO::setAFCParams() AFC params", afcEnable, afcKI, afcKP, afcRange);
}
/* */
void IO::updateCal(DVM_STATE modemState)
{
uint32_t ADF7021_REG2;
float divider;
/*
** VCO/Oscillator (Register 1)
*/
configureBand();
AD7021_CONTROL = ADF7021_REG1;
AD7021_1_IOCTL();
// configure ADF Tx/Rx
configureTxRx(modemState);
/*
** Demodulator Setup (Register 4)
*/
AD7021_CONTROL = ADF7021_REG4;
AD7021_1_IOCTL();
/*
** Fractional-N Synthesizer (Register 0)
*/
if (div2 == 1U)
divider = m_txFrequency / (ADF7021_PFD / 2U);
else
divider = m_txFrequency / ADF7021_PFD;
// calculate Integer_N and Fractional_N divider values for Rx
TX_N_Divider = floor(divider);
divider = (divider - TX_N_Divider) * 32768;
TX_F_Divider = floor(divider + 0.5);
// setup tx register 0
ADF7021_TX_REG0 = (uint32_t)0b0000; // register 0
#if defined(BIDIR_DATA_PIN)
ADF7021_TX_REG0 |= (uint32_t)0b01000 << 27; // mux regulator/transmit
#else
ADF7021_TX_REG0 |= (uint32_t)0b01010 << 27; // mux regulator/uart-spi enabled/transmit
#endif
ADF7021_TX_REG0 |= (uint32_t)TX_N_Divider << 19; // frequency - 15-bit Frac_N
ADF7021_TX_REG0 |= (uint32_t)TX_F_Divider << 4; // frequency - 8-bit Int_N
/*
** Transmit Modulation (Register 2)
*/
AD7021_CONTROL = ADF7021_REG2;
AD7021_1_IOCTL();
DEBUG2("IO::updateCal() ADF calibration; modemState", modemState);
if (m_tx)
setTX();
else
setRX();
}
/* */
uint16_t IO::readRSSI()
{
uint32_t AD7021_RB;
uint16_t RB_word = 0U;
uint8_t RB_code, gainCode, gainCorr;
// Register 7, readback enable, ADC RSSI mode
AD7021_RB = 0x0147;
// Send control register
for (int i = 8; i >= 0; i--) {
if (ADF_BIT_READ(AD7021_RB, i) == HIGH)
SDATA(HIGH);
else
SDATA(LOW);
delayBit();
SCLK(HIGH);
delayBit();
SCLK(LOW);
}
SDATA(LOW);
#if defined(DUPLEX)
if (m_duplex || m_modemState == STATE_RSSI_CAL)
SLE2(HIGH);
else
SLE1(HIGH);
#else
SLE1(HIGH);
#endif
delayBit();
// Read SREAD pin
for (int i = 17; i >= 0; i--) {
SCLK(HIGH);
delayBit();
if ((i != 17) && (i != 0))
RB_word |= ((SREAD() & 0x01) << (i - 1));
SCLK(LOW);
delayBit();
}
#if defined(DUPLEX)
if (m_duplex || m_modemState == STATE_RSSI_CAL)
SLE2(LOW);
else
SLE1(LOW);
#else
SLE1(LOW);
#endif
// Process RSSI code
RB_code = RB_word & 0x7f;
gainCode = (RB_word >> 7) & 0x0f;
switch (gainCode) {
case 0b1010:
gainCorr = 0U;
break;
case 0b0110:
gainCorr = 24U;
break;
case 0b0101:
gainCorr = 38U;
break;
case 0b0100:
gainCorr = 58U;
break;
case 0b0000:
gainCorr = 86U;
break;
default:
gainCorr = 0U;
break;
}
return (130 - (RB_code + gainCorr) / 2);
}
// ---------------------------------------------------------------------------
// Private Class Members
// ---------------------------------------------------------------------------
/* */
void IO::configureBand()
{
/*
** VCO/Oscillator (Register 1)
*/
/** 136 - 174mhz */
if ((m_txFrequency >= VHF_MIN) && (m_txFrequency < VHF_MAX)) {
ADF7021_REG1 = ADF7021_REG1_VHF; // VHF (80 - 200), external VCO
div2 = 1U;
}
/** 216 - 225mhz */
if ((m_txFrequency >= VHF_220_MIN) && (m_txFrequency < VHF_220_MAX)) {
ADF7021_REG1 = ADF7021_REG1_VHF_220; // VHF (200 - 450), external VCO
div2 = 1U; // should be 2U?
}
/** 380 - 431mhz */
if ((m_txFrequency >= UHF_380_MIN) && (m_txFrequency < UHF_380_MAX)) {
// NOTE: I've included support for this band, but, this could be problematic due to
// the external VCO control on most (if not all) hotspots
ADF7021_REG1 = ADF7021_REG1_UHF_380; // UHF (200 - 450), external VCO
div2 = 1U;
}
/** 431 - 450mhz */
if ((m_txFrequency >= UHF_1_MIN) && (m_txFrequency < UHF_1_MAX)) {
ADF7021_REG1 = ADF7021_REG1_UHF_1; // UHF (431 - 450), internal VCO
div2 = 1U;
}
/** 450 - 470mhz */
if ((m_txFrequency >= UHF_2_MIN) && (m_txFrequency < UHF_2_MAX)) {
ADF7021_REG1 = ADF7021_REG1_UHF_2; // UHF (450 - 470), internal VCO
div2 = 1U;
}
/** 470 - 520mhz */
if ((m_txFrequency >= UHF_T_MIN) && (m_txFrequency < UHF_T_MAX)) {
// NOTE: I've included support for this band, but, this could be problematic due to
// the external VCO control on most (if not all) hotspots
ADF7021_REG1 = ADF7021_REG1_UHF_T; // UHF (470 - 520), external VCO
#if defined(FORCE_UHF_INTERAL_L)
div2 = 2U;
#else
div2 = 1U;
#endif
}
/** 842 - 900mhz */
if ((m_txFrequency >= UHF_800_MIN) && (m_txFrequency < UHF_800_MAX)) {
ADF7021_REG1 = ADF7021_REG1_800; // UHF (862 - 900), internal VCO
div2 = 2U;
}
/** 900 - 950mhz */
if ((m_txFrequency >= UHF_900_MIN) && (m_txFrequency < UHF_900_MAX)) {
ADF7021_REG1 = ADF7021_REG1_900; // UHF (900 - 950), internal VCO
div2 = 2U;
}
if (div2 == 1U)
f_div = 2U;
else
f_div = 1U;
DEBUG3("IO::configureBand() ADF freq band; reg1/f_div", ADF7021_REG1, f_div);
}
/* */
void IO::configureTxRx(DVM_STATE modemState)
{
uint16_t dmrDiscBW = ADF7021_DISC_BW_DMR, dmrPostBW = ADF7021_POST_BW_DMR;
uint16_t p25DiscBW = ADF7021_DISC_BW_P25, p25PostBW = ADF7021_POST_BW_P25;
uint16_t nxdnDiscBW = ADF7021_DISC_BW_NXDN, nxdnPostBW = ADF7021_POST_BW_NXDN;
// configure DMR discriminator and post demodulator BW
if (m_dmrDiscBWAdj != 0) {
if (dmrDiscBW + m_dmrDiscBWAdj < 0)
dmrDiscBW = 0U;
else
dmrDiscBW = ADF7021_DISC_BW_DMR + m_dmrDiscBWAdj;
if (dmrDiscBW > ADF7021_DISC_BW_MAX)
dmrDiscBW = ADF7021_DISC_BW_MAX;
}
if (m_dmrPostBWAdj != 0) {
if (dmrPostBW + m_dmrPostBWAdj < 0)
dmrPostBW = 0U;
else
dmrPostBW = ADF7021_POST_BW_DMR + m_dmrPostBWAdj;
if (dmrPostBW > ADF7021_POST_BW_MAX)
dmrPostBW = ADF7021_POST_BW_MAX;
}
// configure P25 discriminator and post demodulator BW
if (m_p25DiscBWAdj != 0) {
if (p25DiscBW + m_p25DiscBWAdj < 0)
p25DiscBW = 0U;
else
p25DiscBW = ADF7021_DISC_BW_P25 + m_p25DiscBWAdj;
if (p25DiscBW > ADF7021_DISC_BW_MAX)
p25DiscBW = ADF7021_DISC_BW_MAX;
}
if (m_p25PostBWAdj != 0) {
if (p25PostBW + m_p25PostBWAdj < 0)
p25PostBW = 0U;
else
p25PostBW = ADF7021_POST_BW_P25 + m_p25PostBWAdj;
if (p25PostBW > ADF7021_POST_BW_MAX)
p25PostBW = ADF7021_POST_BW_MAX;
}
// configure NXDN discriminator and post demodulator BW
if (m_nxdnDiscBWAdj != 0) {
if (nxdnDiscBW + m_nxdnDiscBWAdj < 0)
nxdnDiscBW = 0U;
else
nxdnDiscBW = ADF7021_DISC_BW_NXDN + m_nxdnDiscBWAdj;
if (nxdnDiscBW > ADF7021_DISC_BW_MAX)
nxdnDiscBW = ADF7021_DISC_BW_MAX;
}
if (m_nxdnPostBWAdj != 0) {
if (nxdnPostBW + m_nxdnPostBWAdj < 0)
nxdnPostBW = 0U;
else
nxdnPostBW = ADF7021_POST_BW_NXDN + m_nxdnPostBWAdj;
if (nxdnPostBW > ADF7021_POST_BW_MAX)
nxdnPostBW = ADF7021_POST_BW_MAX;
}
/*
** Configure the remaining registers based on modem state.
*/
switch (modemState) {
case STATE_CW: // 4FSK
{
// Dev: +1 symb (variable), symb rate = 4800
/*
** Tx/Rx Clock (Register 3)
*/
/** Support for 14.7456 MHz TCXO (modified RF7021SE boards) */
#if defined(ADF7021_14_7456)
ADF7021_REG3 = (uint32_t)ADF7021_REG3_ADDR; // Register Address 3
ADF7021_REG3 |= (uint32_t)ADF7021_REG3_BBOS_DIV_8 << 4; // Base Band Clock Divider
ADF7021_REG3 |= (uint32_t)(3 & 0xFU) << 6; // Demodulator Clock Divider
ADF7021_REG3 |= (uint32_t)(32 & 0xFFU) << 10; // Data/Clock Recovery Divider (CDR)
ADF7021_REG3 |= (uint32_t)(147 & 0xFFU) << 18; // Sequencer Clock Divider
ADF7021_REG3 |= (uint32_t)(10 & 0x3FU) << 26; // AGC Clock Divider
/** Support for 12.2880 MHz TCXO */
#elif defined(ADF7021_12_2880)
ADF7021_REG3 = (uint32_t)ADF7021_REG3_ADDR; // Register Address 3
ADF7021_REG3 |= (uint32_t)ADF7021_REG3_BBOS_DIV_8 << 4; // Base Band Clock Divider
ADF7021_REG3 |= (uint32_t)(2 & 0xFU) << 6; // Demodulator Clock Divider
ADF7021_REG3 |= (uint32_t)(40 & 0xFFU) << 10; // Data/Clock Recovery Divider (CDR)
ADF7021_REG3 |= (uint32_t)(123 & 0xFFU) << 18; // Sequencer Clock Divider
ADF7021_REG3 |= (uint32_t)(10 & 0x3FU) << 26; // AGC Clock Divider
#endif
/*
** AFC (Register 10)
*/
ADF7021_REG10 = (uint32_t)ADF7021_REG10_ADDR; // Register Address 10
if (m_afcEnable) {
ADF7021_REG10 |= (uint32_t)ADF7021_REG10_AFC_ENABLE << 4; // AFC Enable/Disable
} else {
ADF7021_REG10 |= (uint32_t)ADF7021_REG10_AFC_DISABLE << 4; // AFC Enable/Disable
}
/** Support for 14.7456 MHz TCXO (modified RF7021SE boards) */
#if defined(ADF7021_14_7456)
ADF7021_REG10 |= (uint32_t)(569 & 0xFFFU) << 5; // AFC Scaling Factor
/** Support for 12.2880 MHz TCXO */
#elif defined(ADF7021_12_2880)
ADF7021_REG10 |= (uint32_t)(683 & 0xFFFU) << 5; // AFC Scaling Factor
#endif
ADF7021_REG10 |= (uint32_t)(m_afcKI & 0xFU) << 17; // KI
ADF7021_REG10 |= (uint32_t)(m_afcKP & 0x7U) << 21; // KP
ADF7021_REG10 |= (uint32_t)(m_afcRange & 0xFFU) << 24; // Maximum AFC Range
/*
** Demodulator Setup (Register 4)
*/
// K=32
ADF7021_REG4 = (uint32_t)ADF7021_REG4_ADDR; // Register Address 4
ADF7021_REG4 |= (uint32_t)ADF7021_REG4_MODE_4FSK << 4; // Demodulation Scheme
ADF7021_REG4 |= (uint32_t)ADF7021_REG4_CROSS_PROD << 7; // Dot Product
ADF7021_REG4 |= (uint32_t)ADF7021_REG4_INV_CLKDAT << 8; // Clock/Data Inversion
ADF7021_REG4 |= (uint32_t)(dmrDiscBW & 0x3FFU) << 10; // Discriminator BW
ADF7021_REG4 |= (uint32_t)(dmrPostBW & 0xFFFU) << 20; // Post Demod BW
ADF7021_REG4 |= (uint32_t)ADF7021_REG4_IF_25K << 30; // IF Filter
/*
** 3FSK/4FSK Demod (Register 13)
*/
ADF7021_REG13 = (uint32_t)ADF7021_REG13_ADDR; // Register Address 13
ADF7021_REG13 |= (uint32_t)ADF7021_SLICER_TH_DMR << 4; // Slicer Threshold
/*
** Transmit Modulation (Register 2)
*/
ADF7021_REG2 = (uint32_t)ADF7021_REG2_ADDR; // Register Address 2
ADF7021_REG2 |= (uint32_t)ADF7021_REG2_MOD_4FSKRC << 4; // Modulation Scheme
ADF7021_REG2 |= (uint32_t)ADF7021_REG2_PA_DEF << 7; // PA Enable & PA Bias
ADF7021_REG2 |= (uint32_t)(m_rfPower & 0x3FU) << 13; // PA Level (0 - Off, 63 - 13 dBm)
ADF7021_REG2 |= (uint32_t)(m_cwIdTXLevel / div2) << 19; // Freq. Deviation
ADF7021_REG2 |= (uint32_t)ADF7021_REG2_INV_CLKDAT << 28; // Clock/Data Inversion
ADF7021_REG2 |= (uint32_t)ADF7021_REG2_RC_5 << 30; // R-Cosine Alpha
}
break;