forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 52
/
sa_core.c
8580 lines (8007 loc) · 278 KB
/
sa_core.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
/*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
//#ifdef __SI4432__
#include "si4432.h" // comment out for simulation
//#endif
#include "stdlib.h"
//#define TINYSA4
#pragma GCC push_options
#ifdef TINYSA4
#pragma GCC optimize ("Os")
#else
#pragma GCC optimize ("Os")
#endif
#if 0
#define TRACE(X) { DAC->DHR12R1 =(uint32_t) ((X)*400); } // Enable for realtime tracing
#else
#define TRACE(X) // { DAC->DHR12R1 = (X*400); } // Enable for realtime tracing
#endif
#ifdef __FFT_DECONV__
void FFT(float *real, float *imag, int length, bool inverse);
float *real = (float *) &spi_buffer[0];
float *imag = (float *) &spi_buffer[512];
float *real2 = (float *) &spi_buffer[1024];
float *imag2 = (float *) &spi_buffer[1536];
#endif
#ifdef __FFT_VBW__
void FFT(float *real, float *imag, int length, bool inverse);
float *real = (float *) &spi_buffer[0];
float *imag = (float *) &spi_buffer[512];
#endif
//#define __DEBUG_AGC__ If set the AGC value will be shown in the stored trace and FAST_SWEEP rmmode will be disabled
#ifdef __DEBUG_AGC__
#ifdef __FAST_SWEEP__
#undef __FAST_SWEEP__
#endif
#endif
// uint8_t dirty = true;
uint8_t scandirty = true;
bool debug_avoid = false;
bool debug_avoid_second = false;
bool progress_bar = true;
#ifdef __ULTRA__
bool debug_spur = false;
bool debug_level = false;
#endif
int current_index = -1;
setting_t setting;
uint16_t actual_rbw_x10 = 0;
freq_t frequency_step_x10 = 0;
uint16_t vbwSteps = 1;
freq_t minFreq = 0;
freq_t maxFreq = 520000000;
static float old_a = -150; // cached value to reduce writes to level registers
int spur_gate = 100;
#ifdef __BANDS__
uint16_t current_band = 0;
#endif
#ifdef __ULTRA__
freq_t ultra_start;
//bool ultra;
int LO_harmonic;
#endif
#ifdef TINYSA4
bool direct_test;
int noise_level;
float log_averaging_correction;
//uint32_t old_CFGR; // Not used??
//uint32_t orig_CFGR; // Not used??
int debug_frequencies = false;
int linear_averaging = true;
static freq_t old_freq[5] = { 0, 0, 0, 0,0};
static freq_t real_old_freq[5] = { 0, 0, 0, 0,0};
static long real_offset = 0;
static float old_temp = 0.0;
static int LO_spur_shifted;
static int LO_mirrored;
static int LO_shifting;
void clear_frequency_cache(void)
{
for (unsigned int i = 0; i < sizeof(old_freq)/sizeof(freq_t) ; i++) {
old_freq[i] = 0;
real_old_freq[i] = 0;
}
ADF4351_force_refresh();
}
#else
static freq_t old_freq[4] = { 0, 0, 0, 0};
static freq_t real_old_freq[4] = { 0, 0, 0, 0};
#endif
#ifdef __ULTRA__
int old_drive = -1;
int actual_drive = -1;
#endif
#define BELOW_MAX_DRIVE(X) (drive_dBm[X] - drive_dBm[MAX_DRIVE])
#ifdef TINYSA4
const float si_drive_dBm [] = {-44.1, -30, -21.6, -17, -14, -11.7, -9.9, -8.4, -7.1, -6, -5, -4.2, -3.4, -2.7 , -2.1, -1.5, -1, -0.47, 0};
//const float adf_drive_dBm[] = {-13,-7.5,-4.2, 0};
//const float adf_drive_dBm[] = {-9, -4, 0, 0};
const float adf_drive_dBm[] = {0, 0, 0, 0}; // Only use drive 0
const uint8_t drive_register[] = {0, 1, 2, 3, 4, 5, 6, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
float *drive_dBm = (float *) si_drive_dBm;
const int min_drive = 0;
int max_drive = 18;
#ifdef __NEW_SWITCHES__
#define SWITCH_ATTENUATION ((setting.mode == M_GENHIGH) ? 40 : 25.0 + config.out_switch_offset)
#define MAX_DRIVE max_drive // ((setting.mode == M_GENHIGH) ? 3 : 18)
#define MIN_DRIVE min_drive // ((setting.mode == M_GENHIGH) ? 0: 0)
#define SL_GENHIGH_LEVEL_MIN (drive_dBm[MIN_DRIVE])
#else
#define SWITCH_ATTENUATION ((setting.mode == M_GENHIGH && config.high_out_adf4350) ? 40 : 25.0 + config.out_switch_offset)
#define MAX_DRIVE ((setting.mode == M_GENHIGH && config.high_out_adf4350 ) ? 3 : 18)
#define MIN_DRIVE ((setting.mode == M_GENHIGH && config.high_out_adf4350 ) ? 0: 0)
#define SL_GENHIGH_LEVEL_MIN (drive_dBm[MIN_DRIVE] - (config.high_out_adf4350 ? 0: 37 - config.switch_offset))
#endif
#define RECEIVE_SWITCH_ATTENUATION (29 + config.receive_switch_offset)
//#define POWER_OFFSET -18 // Max level with all enabled
//#define POWER_RANGE 70
//#define SL_GENHIGH_LEVEL_MIN -15
//#define SL_GENHIGH_LEVEL_RANGE 9
#define SL_GENHIGH_LEVEL_MAX drive_dBm[MAX_DRIVE]
#define SL_GENLOW_LEVEL_MIN -115
#define SL_GENLOW_LEVEL_MAX -18.5
#ifdef TINYSA4_4
#define MAX_ATTENUATE (setting.extra_lna ? 0 : 31.5)
#else
#define MAX_ATTENUATE 31.5
#endif
// 0 1 2 3 4 5
// enum {PATH_OFF, PATH_LOW, PATH_DIRECT, PATH_LEAKAGE, PATH_ULTRA, PATH_HIGH};
int signal_path = PATH_OFF;
#ifdef TINYSA4
const char *const path_text[]=PATH_TEXT;
void set_output_drive(int d)
{
if (signal_path == PATH_LEAKAGE) {
if (max2871 && d == 0)
d = 3;
ADF4351_drive(d);
} else {
#ifdef __SI4432__
SI4432_Sel = SI4432_RX ;
SI4432_Drive(d);
#endif
#ifdef __SI4463__
SI4463_set_output_level(d);
#endif
setting.rx_drive = d;
}
}
void set_output_step_atten(int s)
{
setting.atten_step = s;
if (signal_path == PATH_LEAKAGE)
// enable_ultra(!s);
enable_direct(s);
else {
#ifdef TINYSA3
SI4432_Sel = SI4432_RX ;
if (s)
set_switch_receive();
else
set_switch_transmit();
#else
enable_rx_output(!s);
#endif
}
}
void set_output_path(freq_t f, float level)
{
LO_harmonic = false;
if (depth_error) { depth_error = false; redraw_request |= REDRAW_CAL_STATUS; draw_all(true);}
if (force_signal_path) {
signal_path = test_path;
setting.mixer_output = (signal_path == PATH_ULTRA);
if (test_output_drive >=0) {
setting.atten_step = test_output_switch;
set_output_drive(test_output_drive);
PE4302_Write_Byte(test_output_attenuate);
if (debug_level && SDU1.config->usbp->state == USB_ACTIVE) {
shell_printf ("d=%d, a=%d, s=%d\r\n",test_output_drive, test_output_attenuate, (setting.atten_step ? 1 : 0));
osalThreadSleepMilliseconds(100);
}
goto set_path;
}
} else if (MODE_HIGH(setting.mode))
signal_path = PATH_HIGH;
else if (setting.mixer_output && (f >= MAX_LOW_OUTPUT_FREQ || (config.ultra_start != ULTRA_AUTO && f > config.ultra_start))) {
signal_path = PATH_ULTRA;
LO_harmonic = (f > ULTRA_MAX_FREQ);
} else if (!setting.mixer_output && (f >= MAX_LOW_OUTPUT_FREQ || (config.ultra_start != ULTRA_AUTO && f > config.ultra_start))) {
signal_path = PATH_LEAKAGE;
LO_harmonic = (f > MAX_LO_FREQ);
} else if (f > MINIMUM_DIRECT_FREQ)
signal_path = PATH_DIRECT;
else
signal_path = PATH_LOW;
level += PURE_TO_float(get_frequency_correction(f));
switch (signal_path) {
case PATH_LEAKAGE:
drive_dBm = (float *)adf_drive_dBm;
max_drive = 0;
break;
default:
drive_dBm = (float *)si_drive_dBm;
max_drive = 18;
}
if (signal_path != PATH_LEAKAGE) {
float dt = Si446x_get_temp() - CENTER_TEMPERATURE;
if (dt > 0)
level += dt * DB_PER_DEGREE_ABOVE; // Temperature correction
else
level += dt * DB_PER_DEGREE_BELOW; // Temperature correction
}
if (signal_path == PATH_HIGH) {
return; //TODO setup high path
}
float ATTENUATION_RESERVE = 0.0;
if (signal_path == PATH_LEAKAGE) {
ATTENUATION_RESERVE = 0;
}
else {
if (setting.modulation == MO_AM)
ATTENUATION_RESERVE = 6.0;
}
level += ATTENUATION_RESERVE;
float switch_atten = SWITCH_ATTENUATION;
if (signal_path == PATH_LEAKAGE)
#ifdef TINYSA4
switch_atten = 32.0;
#else
switch_atten = 31.0;
#endif
float a = level - level_max(); // convert to all settings maximum power output equals a = zero
if (a < -switch_atten + BELOW_MAX_DRIVE(0)) { // Switch needed
a = a + switch_atten;
setting.atten_step = true;
} else
setting.atten_step = false;
// set_output_step_atten(setting.atten_step);
#ifdef TINYSA4
#define LOWEST_LEVEL (setting.atten_step ? 0 : MIN_DRIVE)
#else
#define LOWEST_LEVEL MIN_DRIVE
#endif
int d;
#ifdef TINYSA4
if (signal_path == PATH_LEAKAGE)
d = 0;
else
d = MAX_DRIVE-8; // Start in the middle
#else
d = MAX_DRIVE-3; // Start in the middle
#endif
float blw = BELOW_MAX_DRIVE(d);
while (a > blw && d < MAX_DRIVE) { // Increase if needed
d++;
blw = BELOW_MAX_DRIVE(d);
}
int ar = 31 - ATTENUATION_RESERVE;
if (setting.modulation == MO_AM) // reserve attenuator range for AM modulation
ar = 6;
while (a + ar < blw && d > LOWEST_LEVEL) { // reduce till it fits attenuator ((ar+ATTENUATION_RESERVE) .. ATTENUATION_RESERVE)
d--;
blw = BELOW_MAX_DRIVE(d);
}
a -= blw;
set_output_drive(d);
// if (signal_path != PATH_LEAKAGE)
a -= ATTENUATION_RESERVE;
if (a > 0) {
if (!level_error) { level_error = true; redraw_request |= REDRAW_CAL_STATUS; draw_all(true);}
a += 0.49;
a = (float)((int)(a*2))/2.0;
setting.level -= a;
a = 0;
// } else if (setting.modulation == MO_AM && a < -10) { // Insufficient headroom for modulation
// if (!level_error) { level_error = true; redraw_request |= REDRAW_CAL_STATUS; draw_all(true); }
} else {
if (level_error) { level_error = false; redraw_request |= REDRAW_CAL_STATUS; draw_all(true); }
}
if (a < -31.5)
a = -31.5;
a = -a - 0.25; // Rounding
setting.attenuate_x2 = (int)(a * 2);
PE4302_Write_Byte(setting.attenuate_x2);
if (debug_level && SDU1.config->usbp->state == USB_ACTIVE)
shell_printf ("level=%.2f, d=%d, a=%d, s=%d, f=%.2f\r\n", level, d, setting.attenuate_x2, (setting.atten_step ? 1 : 0), PURE_TO_float(get_frequency_correction(f)));
enable_extra_lna(false);
if (setting.mute)
signal_path = PATH_OFF;
set_path:
switch(signal_path) {
case PATH_OFF:
if (SI4463_is_in_tx_mode())
SI4463_init_rx();
enable_ADF_output(false,false);
break;
case PATH_LEAKAGE:
if (SI4463_is_in_tx_mode())
SI4463_init_rx();
enable_ADF_output(true, setting.tracking_output);
#if 0
enable_direct(false);
enable_ultra(!setting.atten_step);
#else
enable_direct(setting.atten_step);
enable_ultra(true);
#endif
enable_high(false);
break;
case PATH_LOW:
enable_ultra(false);
enable_high(false);
enable_direct(false);
goto common;
case PATH_DIRECT:
enable_ultra(true);
enable_direct(true);
enable_high(true);
enable_ADF_output(false, false);
goto common2;
case PATH_ULTRA:
enable_ultra(true);
enable_direct(false);
enable_high(false);
common:
enable_ADF_output(true, setting.tracking_output);
common2:
enable_rx_output(!setting.atten_step);
if (!SI4463_is_in_tx_mode())
SI4463_init_tx();
break;
}
setting.spur_removal &= 0xFE;
}
static void calculate_static_correction(void);
void set_input_path(freq_t f)
{
LO_harmonic = false;
if (force_signal_path) {
setting.extra_lna = test_path & 0x01;
switch ((test_path & 0xFE)>>1) {
case 0: signal_path = PATH_LOW; break;
case 1: signal_path = PATH_ULTRA; break;
case 2: signal_path = PATH_DIRECT; break;
case 3: signal_path = PATH_ULTRA; LO_harmonic = true; break;
}
}
else if (MODE_HIGH(setting.mode))
signal_path = PATH_HIGH;
else if (direct_test && f >= 830000000 && f < 1130000000)
signal_path = PATH_DIRECT;
else if (config.direct && f >= config.direct_start && f < config.direct_stop)
signal_path = PATH_DIRECT;
else if(config.ultra && ((config.ultra_start == ULTRA_AUTO && f > ultra_start) || (config.ultra_start != ULTRA_AUTO && f >config.ultra_start))) {
LO_harmonic = (f > (config.harmonic_start?config.harmonic_start:ULTRA_MAX_FREQ));
signal_path = PATH_ULTRA;
} else {
signal_path = PATH_LOW;
}
if (signal_path == PATH_HIGH) {
return; // TODO setup high input path
}
enable_rx_output(setting.atten_step);
bool mirrors = false;
switch(signal_path) {
case PATH_LOW:
enable_ultra(false);
enable_direct(false);
enable_high(false);
enable_extra_lna(setting.extra_lna);
goto common;
case PATH_DIRECT:
enable_ultra(true);
enable_direct(true);
enable_high(true);
enable_extra_lna(setting.extra_lna);
if (setting.tracking_output)
enable_ADF_output(true, true);
else
enable_ADF_output(false, false);
TRACE(1);
goto common2;
case PATH_ULTRA:
mirrors = true;
enable_ultra(true);
enable_high(false);
enable_direct(false);
enable_extra_lna(setting.extra_lna);
common:
enable_ADF_output(true, setting.tracking_output);
common2:
if (SI4463_is_in_tx_mode())
SI4463_init_rx();
break;
}
if (force_signal_path)
calculate_static_correction();
if (S_IS_AUTO(setting.spur_removal)) {
if (mirrors)
setting.spur_removal |= 0x01;
else
setting.spur_removal &= 0xFE;
}
}
#endif
#else
const int8_t drive_dBm [16] = {-38, -32, -30, -27, -24, -19, -15, -12, -5, -2, 0, 3, 6, 9, 12, 16};
#define SWITCH_ATTENUATION (29 - config.switch_offset)
#define RECEIVE_SWITCH_ATTENUATION (24 + config.receive_switch_offset)
//#define POWER_OFFSET 15
#define MAX_DRIVE (setting.mode == M_GENHIGH ? 13 : 11) // The value of 13 is linked to the SL_GENHIGH_LEVEL_MAX of 9
#define MIN_DRIVE 8
#define SL_GENHIGH_LEVEL_MIN -38
#define SL_GENHIGH_LEVEL_MAX 9
#define SL_GENLOW_LEVEL_MIN -76
#define SL_GENLOW_LEVEL_MAX -7
#define MAX_ATTENUATE 31.5
#endif
//float level_min;
//float level_max;
//float level_range;
float channel_power[3];
float channel_power_watt[3];
volatile float flatness;
//int setting.refer = -1; // Off by default
const uint32_t reffer_freq[] = {30000000, 15000000, 10000000, 4000000, 3000000, 2000000, 1000000};
#ifdef TINYSA3
const freq_t fh_low[] = { 240000000, 480000000, 720000000, 960000000, 1200000000 };
const freq_t fh_high[] = { 480000000, 960000000, 1920000000, 2880000000, 3840000000 };
#endif
uint8_t in_selftest = false;
uint8_t ignore_stored = false;
uint8_t in_step_test = false;
uint8_t in_calibration = false;
uint8_t calibration_stage;
void update_min_max_freq(void)
{
switch(setting.mode) {
case M_LOW:
minFreq = 0;
#ifdef __ULTRA__
if (config.ultra)
#ifdef TINYSA4
if (setting.harmonic)
maxFreq = setting.harmonic * MAX_LO_FREQ - DEFAULT_IF; // ULTRA_MAX_FREQ; // make use of harmonic mode above ULTRA_MAX_FREQ
else
maxFreq = ULTRA_MAX_FREQ;
#else
maxFreq = 3000000000; // ULTRA_MAX_FREQ; // make use of harmonic mode above ULTRA_MAX_FREQ
#endif
else
#endif
maxFreq = NORMAL_MAX_FREQ;
#ifdef TINYSA4
plot_printf(range_text, sizeof range_text, "%QHz to %QHz", minFreq, maxFreq);
#endif
break;
case M_GENLOW:
minFreq = 0;
#ifdef TINYSA4
#ifdef __ULTRA_OUT__
if (setting.mixer_output) {
if (setting.harmonic)
maxFreq = setting.harmonic * MAX_LO_FREQ - DEFAULT_IF; // ULTRA_MAX_FREQ; // make use of harmonic mode above ULTRA_MAX_FREQ
else
maxFreq = ULTRA_MAX_FREQ+60000000; // Add 60MHz to go to 5.40GHz
} else {
if (setting.harmonic)
maxFreq = setting.harmonic * (MAX_LO_FREQ+config.overclock); // ULTRA_MAX_FREQ; // make use of harmonic mode above ULTRA_MAX_FREQ
else
maxFreq = MAX_LO_FREQ+config.overclock; // 4.4GHz
}
#else
maxFreq = MAX_LOW_OUTPUT_FREQ;
#endif
#else
maxFreq = DEFAULT_MAX_FREQ;
#endif
break;
case M_HIGH:
minFreq = HIGH_MIN_FREQ_MHZ * 1000000;
maxFreq = HIGH_MAX_FREQ_MHZ * 1000000;
#ifdef __HARMONIC__
#ifdef TINYSA3 // different haemonics processing
if (setting.harmonic) {
minFreq = setting.harmonic * HIGH_MIN_FREQ_MHZ * 1000000;
if (setting.harmonic < 4)
maxFreq = setting.harmonic * HIGH_MAX_FREQ_MHZ * 1000000;
else
maxFreq = 2880000000;
}
if (get_sweep_frequency(ST_START) < minFreq)
set_sweep_frequency(ST_START, minFreq);
if (get_sweep_frequency(ST_STOP) > maxFreq)
set_sweep_frequency(ST_STOP, maxFreq);
#endif
#endif
break;
case M_GENHIGH:
#ifdef TINYSA4
#ifndef __NEW_SWITCHES__
if (!config.high_out_adf4350) {
minFreq = 136000000;
maxFreq = 1150000000U;
} else
#endif
{
minFreq = 136000000;
maxFreq = MAX_LO_FREQ;
}
#else
minFreq = 240000000;
maxFreq = 960000000;
#endif
break;
}
#ifdef TINYSA4
plot_printf(range_text, sizeof range_text, "%.3QHz to %.3QHz", minFreq, maxFreq);
#endif
}
void reset_settings(int m)
{
// strcpy((char *)spi_buffer, dummy);
setting.mode = m;
setting.sweep = false;
disable_waterfall();
setting.level_meter = false;
setting.pulse = false;
#ifdef __BANDS__
setting.multi_band = false;
setting.multi_trace = false;
for (int i = 0; i<BANDS_MAX;i++) {
setting.bands[i].start = 0;
setting.bands[i].end = 0;
setting.bands[i].enabled = false;
setting.bands[i].level = 0;
}
#endif
#ifdef __ULTRA__
ultra_start = (config.ultra_start == ULTRA_AUTO ? ULTRA_THRESHOLD : config.ultra_start);
#endif
#ifdef TINYSA4
drive_dBm = (float *) (setting.mode == M_GENHIGH ? adf_drive_dBm : si_drive_dBm);
setting.exp_aver = 1;
setting.increased_R = false;
setting.mixer_output = true;
#endif
update_min_max_freq();
force_signal_path = false;
setting.frequency_var = 0;
sweep_mode |= SWEEP_ENABLE;
setting.unit_scale_index = 0;
setting.unit_scale = 1;
setting.unit = U_DBM;
set_scale(10);
set_reflevel(-10);
setting.level_sweep = 0.0;
setting.attenuate_x2 = 0; // These should be initialized consistently
setting.rx_drive=MAX_DRIVE; // And this
setting.atten_step = 0; // And this, only used in low output mode
setting.rbw_x10 = 0;
for (int t=0;t<TRACES_MAX;t++) {
setting.average[t] = 0;
setting.stored[t] = false;
setting.subtract[t] = 0; // Disabled
setting.normalized[t] = false; // Disabled
}
for (int r=0;r<REFERENCE_MAX;r++)
for (int l=0;l<LIMITS_MAX;l++)
setting.limits[r][l].enabled = false;
if (in_selftest) {
setting.stored[TRACE_STORED] = true;
TRACE_ENABLE(TRACE_STORED_FLAG);
} else
#ifdef TINYSA4
TRACE_DISABLE(TRACE_STORED_FLAG|TRACE_TEMP_FLAG|TRACE_STORED2_FLAG);
#else
TRACE_DISABLE(TRACE_STORED_FLAG|TRACE_TEMP_FLAG);
#endif
#ifdef TINYSA4
setting.harmonic = 3; // Automatically used when above ULTRA_MAX_FREQ
#else
#ifdef __ULTRA__
setting.harmonic = 3;
#else
setting.harmonic = 0;
#endif
#endif
#ifdef __DRAW_LINE__
setting.draw_line = false;
#endif
// setting.show_stored = 0;
setting.auto_attenuation = false;
setting.normalize_level = 0.0;
setting.normalized_trace = -1;
#ifdef TINYSA4
setting.lo_drive=5;
#else
setting.lo_drive=13;
// setting.rx_drive=8; moved to top
// setting.atten_step = 0; moved to top
#endif
setting.agc = S_AUTO_ON;
setting.lna = S_AUTO_OFF;
setting.tracking = false;
setting.modulation = MO_NONE;
setting.modulation_frequency = 1000;
#ifdef TINYSA4
setting.modulation_depth_x100 = 80; // %80
setting.modulation_deviation_div100 = 30; // 3kHz
#endif
setting.step_delay = 0;
setting.offset_delay = 0;
setting.step_delay_mode = SD_NORMAL;
setting.vbw_x100 = 0; // Auto mode
setting.repeat = 1;
setting.auto_reflevel = true; // Must be after SetReflevel
setting.decay=20;
setting.attack=1;
setting.noise=5;
setting.below_IF = S_AUTO_OFF;
setting.tracking_output = false;
setting.measurement = M_OFF;
#ifdef __ULTRA__
// setting.ultra = S_AUTO_OFF;
#endif
#ifdef TINYSA4
setting.frequency_IF = config.frequency_IF1; ;
#else
setting.frequency_IF = DEFAULT_IF;
#endif
setting.frequency_offset = FREQUENCY_SHIFT;
setting.auto_IF = true;
//set_external_gain(0.0); // This also updates the help text!!!!!
//setting.external_gain = 0.0;
setting.trigger = T_AUTO;
setting.trigger_direction = T_UP;
setting.trigger_mode = T_MID;
setting.fast_speedup = 0;
setting.faster_speedup = 0;
setting.trigger_level = -150.0;
#ifdef __TRIGGER_TRACE__
setting.trigger_trace = 255;
#endif
setting.linearity_step = 0;
// setting.refer = -1; // do not reset reffer when switching modes
setting.mute = true;
#ifdef __SPUR__
#ifdef __ULTRA__
if (m == M_LOW)
setting.spur_removal = S_AUTO_OFF;
else
setting.spur_removal = S_OFF;
#else
setting.spur_removal = S_OFF;
#endif
setting.mirror_masking = false;
setting.slider_position = 0;
setting.slider_span = 100000;
#endif // __SPUR__
switch(m) {
case M_LOW:
set_sweep_frequency(ST_START, minFreq);
set_sweep_frequency(ST_STOP, maxFreq);
#ifdef TINYSA4
set_sweep_frequency(ST_STOP, NORMAL_MAX_FREQ); // TODO <----------------- temp ----------------------
setting.attenuate_x2 = 0;
#else
setting.attenuate_x2 = 60;
#endif
setting.auto_attenuation = true;
setting.sweep_time_us = 0;
#ifdef TINYSA4
setting.lo_drive=5;
setting.extra_lna = false;
#endif
// setting.correction_frequency = config.correction_frequency[CORRECTION_LOW_IN];
// setting.correction_value = config.correction_value[CORRECTION_LOW_IN];
break;
case M_GENLOW:
#ifdef TINYSA4
setting.rx_drive= MAX_DRIVE;
if (max2871)
setting.lo_drive= 2;
else
setting.lo_drive= 1;
#else
// setting.rx_drive=8;
setting.lo_drive=13;
#endif
set_sweep_frequency(ST_CENTER, 10000000);
set_sweep_frequency(ST_SPAN, 0);
setting.sweep_time_us = 2*ONE_SECOND_TIME;
setting.step_delay_mode = SD_FAST;
#ifdef TINYSA4
setting.extra_lna = false;
// setting.correction_frequency = config.correction_frequency[CORRECTION_LOW_OUT];
// setting.correction_value = config.correction_value[CORRECTION_LOW_OUT];
#else
// setting.correction_frequency = config.correction_frequency[CORRECTION_LOW_IN];
// setting.correction_value = config.correction_value[CORRECTION_LOW_IN];
#endif
// level_min = SL_GENLOW_LEVEL_MIN + LOW_OUT_OFFSET;
// level_max = SL_GENLOW_LEVEL_MAX + LOW_OUT_OFFSET;
// level_range = level_max - level_min;
break;
case M_HIGH:
set_sweep_frequency(ST_START, minFreq);
set_sweep_frequency(ST_STOP, maxFreq);
setting.sweep_time_us = 0;
#ifdef TINYSA4
setting.extra_lna = false;
#endif
// setting.correction_frequency = config.correction_frequency[CORRECTION_HIGH_IN];
// setting.correction_value = config.correction_value[CORRECTION_HIGH_IN];
break;
case M_GENHIGH:
#ifdef TINYSA4
setting.lo_drive = MIN_DRIVE;
setting.level = drive_dBm[setting.lo_drive]+ config.high_level_output_offset;
set_sweep_frequency(ST_CENTER, (minFreq + maxFreq)/2 );
setting.extra_lna = false;
#else
setting.lo_drive=8;
set_sweep_frequency(ST_CENTER, 300000000);
#endif
set_sweep_frequency(ST_SPAN, 0);
setting.sweep_time_us = 2*ONE_SECOND_TIME;
setting.step_delay_mode = SD_FAST;
// setting.correction_frequency = config.correction_frequency[CORRECTION_HIGH_IN];
// setting.correction_value = config.correction_value[CORRECTION_HIGH_IN];
// level_min = SL_GENHIGH_LEVEL_MIN + config.high_level_output_offset;
// level_max = SL_GENHIGH_LEVEL_MAX + config.high_level_output_offset;
// level_range = level_max - level_min;
break;
}
setting.level = level_max(); // This is the level with above settings.
markers_reset();
setting._active_marker = 0;
//set_external_gain(0.0); // This also updates the help text!!!!! Must be below level_min and level_max being set
set_sweep_points(POINTS_COUNT);
dirty = true;
}
uint32_t calc_min_sweep_time_us(void) // Estimate minimum sweep time in uS, needed to calculate the initial delays for the RSSI before first sweep
{
uint32_t t;
if (MODE_OUTPUT(setting.mode))
t = 200*sweep_points; // 200 microseconds is the delay set in perform when sweeping in output mode
else {
uint32_t bare_sweep_time=0;
bare_sweep_time = (SI4432_step_delay + MEASURE_TIME) * (sweep_points); // Single RSSI delay and measurement time in uS while scanning
if (FREQ_IS_CW()) {
bare_sweep_time = MINIMUM_SWEEP_TIME; // minimum sweep time in fast CW mode
if (setting.repeat != 1 || setting.sweep_time_us >= 100*ONE_MS_TIME || S_STATE(setting.spur_removal)) // if no fast CW sweep possible
bare_sweep_time = 15000; // minimum CW sweep time when not in fast CW mode
}
t = vbwSteps * (S_STATE(setting.spur_removal) ? 2 : 1) * bare_sweep_time ; // factor in vbwSteps and spur impact
t += (setting.repeat - 1)* REPEAT_TIME * (sweep_points); // Add time required for repeats
}
return t;
}
void set_refer_output(int v)
{
setting.refer = v;
set_calibration_freq(setting.refer);
dirty = true;
}
void set_decay(int d)
{
if (d < 0 || d > 1000000)
return;
if (setting.frequency_step == 0) { // decay in ms
d = (float)d * 500.0 * (float)sweep_points / (float)setting.actual_sweep_time_us;
}
setting.decay = d;
dirty = true;
}
#ifdef __QUASI_PEAK__
void set_attack(int d)
{
if (d < 0 || d > 20000)
return;
if (setting.frequency_step == 0 && d>0) { // decay in ms
d = (float)d * 500.0 * (float)sweep_points / (float)setting.actual_sweep_time_us;
}
setting.attack = d;
dirty = true;
}
#endif
void set_noise(int d)
{
if (d < 2 || d > 120)
return;
setting.noise = d;
dirty = true;
}
void set_gridlines(int d)
{
if (d != 0 && (d < 3 || d > 20))
return;
config.gridlines = d;
config_save();
dirty = true;
update_grid();
}
#ifdef TINYSA4
int set_freq_corr(int f)
{
if (f < - 100000 || f > +100000)
return -1;
config.setting_frequency_30mhz = 3000000000 + f * 3 ;
ADF4351_recalculate_PFDRFout();
config_save();
dirty = true;
update_grid();
return 0;
}
int set_actual_freq(freq_t f)
{
if (f < 3000000000 - 30000 || f > 3000000000 + 30000)
return -1;
config.setting_frequency_30mhz =f;
ADF4351_recalculate_PFDRFout();
config_save();
dirty = true;
update_grid();
return 0;
}
#else
void set_10mhz(freq_t f)
{
if (f < 9000000 || f > 11000000)
return;
config.setting_frequency_10mhz = f;
config_save();
dirty = true;
update_grid();
}
#endif
#if 0
static setting_t saved_setting;
#endif
void set_measurement(int m)
{
#ifdef __LINEARITY__
setting.stored[TRACE_STORED] = true;
if (m == M_LINEARITY) {
for (int j = 0; j < setting._sweep_points; j++)
stored_t[j] = -150;
setting.linearity_step = 0;
setting.attenuate_x2 = 29*2;
setting.auto_attenuation = false;
}
#endif
#ifdef __FFT_DECONV__
if (m == M_DECONV && sweep_points == 256) {
set_storage();
set_reflevel(-20);
} else
return;
#endif
setting.measurement = m;
dirty = true;
}
void set_lo_drive(int d)
{
setting.lo_drive = d;
dirty = true;
}
void set_rx_drive(int d)
{
setting.rx_drive = d;
dirty = true;
}
void set_level_sweep(float l)
{
setting.level_sweep = l;
dirty = true;
}
void set_sweep_time_us(uint32_t t) // Set the sweep time as the user wants it to be.
{
// if (t < MINIMUM_SWEEP_TIME) // Sweep time of zero means sweep as fast as possible
// t = MINIMUM_SWEEP_TIME;
if (t > MAXIMUM_SWEEP_TIME)
t = MAXIMUM_SWEEP_TIME;
setting.sweep_time_us = t;
// if (MODE_OUTPUT(setting.mode))
// setting.actual_sweep_time_us = t; // To ensure time displayed is correct before first sweep is completed
#if 0
uint32_t ta = calc_min_sweep_time_us(); // Can not be faster than minimum sweep time
if (ta < t)
ta = t;
setting.actual_sweep_time_us = ta;
if (FREQ_IS_CW())
update_grid(); // Really only needed in zero span mode
redraw_request |= REDRAW_FREQUENCY;
#endif
dirty = true;
}
void set_tracking_output(int t)
{
setting.tracking_output = t;
dirty = true;
}
//#ifndef TINYSA4
void toggle_tracking_output(void)
{
setting.tracking_output = !setting.tracking_output;
dirty = true;
}
//#endif