forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 52
/
nanovna.h
1921 lines (1714 loc) · 56.5 KB
/
nanovna.h
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 (c) 2014-2015, TAKAHASHI Tomohiro (TTRFTECH) [email protected]
* All rights reserved.
*
* This 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 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.
*/
#include "ch.h"
//#ifdef TINYSA_F303
#ifdef TINYSA_F072
#error "Remove comment for #ifdef TINYSA_F303"
#endif
#ifndef TINYSA4
#define TINYSA4
#endif
#define TINYSA4_PROTO
//#endif
#ifdef TINYSA_F072
#ifdef TINYSA_F303
#error "Remove comment for #ifdef TINYSA_F072"
#endif
#ifndef TINYSA3
#define TINYSA3
#endif
#endif
// Need enable HAL_USE_SPI in halconf.h
#define __USE_DISPLAY_DMA__
//#define __DEBUG_SPUR__
#define __SA__
#ifdef TINYSA3
#define __SI4432__
#endif
#ifdef TINYSA4
//#define __DISABLE_HOT_INSERT__
#define __SI4463__
#define __SI4468__
#define __ADF4351__
#define __NEW_SWITCHES__
#define __ULTRA_OUT__ // Use ADF output over LOW out
#define __SI5351__
#endif
#define __PE4302__
//#define __SIMULATION__
#define __SCROLL__ // Add waterfall option
#define __LEVEL_METER__
#define __DRAW_LINE__
#define __ICONS__
#define __MEASURE__
#define __LINEARITY__ // Not available
#define __SELFTEST__ // Add selftest option (not fully disable it)
#define __CALIBRATE__ // Add calibration menu and functions
//#define __WATCHDOG__
//#ifdef __WATCHDOG__ // still resets when entering the Ultra code
//#if (HAL_USE_WDG != TRUE)
//#error "HAL_USE_WDG must be set to true"
//#endif
//#if (STM32_WDG_USE_IWDG != TRUE)
//#error "STM32_WDG_USE_IWDG must be set to true"
//#endif
//#endif
#define __FAST_SWEEP__ // Pre-fill SI4432 RSSI buffer to get fastest sweep in zero span mode
// #define __AUDIO__
#define __SPUR__ // Does spur reduction by shifting IF
#define __USE_SERIAL_CONSOLE__ // Enable serial I/O connection (need enable HAL_USE_SERIAL as TRUE in halconf.h)
#ifdef __USE_SERIAL_CONSOLE__
//#if (HAL_USE_SERIAL != TRUE)
//#error "HAL_USE_SERIAL must be set to true"
//#endif
#endif
#define __SINGLE_LETTER__ // Add fast console commands
#define __NICE_BIG_FONT__ // Add not scaled big font for menus
#define __QUASI_PEAK__ // Add quasi peak average option
#define __REMOTE_DESKTOP__ // Add remote desktop option
#define __LISTEN__
#define __CHANNEL_POWER__
#define __LIMITS__
#define __CURVE_EDIT__
#ifdef TINYSA3
#define __HAS_DFU__
#define __MCU_CLOCK_SHIFT__
#define __HAM_BAND__
#endif
#ifdef TINYSA4
#define __BANDS__
#define __BEEP__
#define __MULTI_BAND__
#define __HAM_BAND__
#define __TRIGGER_TRACE__
#define __MCU_CLOCK_SHIFT__
#define __ULTRA__
#define __USE_RTC__ // Enable RTC clock
#define __USE_SD_CARD__ // Enable SD card support
//#define __SD_CARD_LOAD__ // Allow run commands from SD card (config.ini in root), if enabled __SD_FILE_BROWSER__ scripts run from *.cmd in it
#define __SD_CARD_DUMP_FIRMWARE__ // Allow dump firmware to SD card
#define __SD_FILE_BROWSER__
#define __LCD_BRIGHTNESS__ // LCD or hardware allow change brightness, add menu item for this
#define __HARMONIC__
#define __NOISE_FIGURE__
#define __VBW__
#define __SWEEP_RESTART__
// #define DIRECT_CORRECTION // Not enough space for config in one flash page.
#define DB_PER_DEGREE_BELOW 0.056
#define DB_PER_DEGREE_ABOVE 0.069
#define CENTER_TEMPERATURE 34.0
#define __WAIT_CTS_WHILE_SLEEPING__
#define __MARKER_CACHE__
#define TINYSA4_4
#ifdef TINYSA4_4
//#define __SI5351__
#endif
//#define __FFT_VBW__
//#define __FFT_DECONV__
#else
//#define __ULTRA__
//#define __HARMONIC__
//#define __USE_FREQ_TABLE__ // Enable use table for frequency list
#endif
#ifdef __BANDS__
#define __PWM__
#endif
#ifdef TINYSA3
typedef uint32_t freq_t;
#define VARIANT(X,Y) (X)
#define DEFAULT_IF 433800000
#define DEFAULT_SPUR_IF 434000000
#define DEFAULT_MAX_FREQ 350000000
#define NORMAL_MAX_FREQ DEFAULT_MAX_FREQ
#define MAX_LO_FREQ 959800000UL
#define MIN_LO_FREQ 240000000UL
#define MIN_BELOW_LO 550000000UL
#define ULTRA_MAX_FREQ 1390000000UL
//#define DEFAULT_MAX_FREQ 527000000
#define HIGH_MIN_FREQ_MHZ 240
#define HIGH_MAX_FREQ_MHZ 959
#endif
#ifdef TINYSA4
typedef uint64_t freq_t;
#define FREQ_MULTIPLIER 100 // Multiplier of the 30MHz reference to get accurate frequency correction
#define VARIANT(X,Y) (Y)
#define DEFAULT_IF ((freq_t)977400000)
#define DEFAULT_IF_PLUS ((freq_t)1070100000)
extern uint16_t hw_if;
#define DEFAULT_SPUR_OFFSET ((freq_t)(actual_rbw_x10 > 3000 ? 1500000 : 1000000))
#define STATIC_DEFAULT_SPUR_OFFSET ((freq_t) 1500000)
#define MAX_LOW_OUTPUT_FREQ ((freq_t)1130000000)
#define HIGH_MIN_FREQ_MHZ 136// 825
#define HIGH_MAX_FREQ_MHZ 1130
#define MINIMUM_DIRECT_FREQ 823000000ULL
#define ULTRA_AUTO 10000000000ULL // 10GHz
//#define LOW_MAX_FREQ 800000000ULL
//#define MIN_BELOW_LO 550000000ULL // not used????
#define DRIVE0_MAX_FREQ 600000000ULL // LO drive 0
#define DRIVE1_MAX_FREQ 1200000000ULL // LO drive 1
#define DRIVE2_MAX_FREQ 2100000000ULL // LO drive 2
#define LOW_SHIFT_FREQ 2000000ULL // shift IF to avoid zero Hz within IF
#define USE_SHIFT2_RBW 4000 // use shift2_level_offset if actual_rbw_x10 is larger then this.
#ifdef __NEW_SWITCHES__
#define DIRECT_START config.direct_start
#define DIRECT_STOP config.direct_stop
#endif
#endif
/*
* main.c
*/
#ifdef __SA__
#ifdef TINYSA4
#define POINTS_COUNT 450
#else
#define POINTS_COUNT 290
#endif
#ifdef TINYSA4
#define MARKER_COUNT 8
#define TRACES_MAX 4
#else
#define MARKER_COUNT 4
#define TRACES_MAX 3
#endif
#define TRACE_ACTUAL 0 // order linked to colors in palette!!!!!
#if TRACES_MAX == 3
#define TRACE_TEMP (LCD_TRACE_3_COLOR - LCD_TRACE_1_COLOR)
#else
#define TRACE_TEMP (LCD_TRACE_4_COLOR - LCD_TRACE_1_COLOR)
#define TRACE_STORED2 (LCD_TRACE_3_COLOR - LCD_TRACE_1_COLOR)
#endif
#define TRACE_STORED (LCD_TRACE_2_COLOR - LCD_TRACE_1_COLOR)
//#define TRACE_AGE 3
#define TRACE_INVALID -1
#define actual_t measured[TRACE_ACTUAL]
#define stored_t measured[TRACE_STORED]
#if TRACES_MAX == 4
#define stored2_t measured[TRACE_STORED2]
#endif
#define temp_t measured[TRACE_TEMP]
// #define age_t measured[TRACE_AGE]
extern const char * const trc_channel_name[];
#ifdef TINYSA3
#define HALF_FREQ 0x80000000UL
typedef int32_t long_t;
extern bool has_esd;
#define CORRECTION_POINTS 10 // Frequency dependent level correction table entries
#define CORRECTION_LOW_IN 0
#define CORRECTION_HIGH_IN 1
#define CORRECTION_LOW_OUT 2
#define CORRECTION_HIGH_OUT 3
// #define CORRECTION_LOW_OUT 2 // Must be same order as output path options!!!!!
#define CORRECTION_SIZE 3
#endif
#ifdef TINYSA4
typedef int64_t long_t;
#define HALF_FREQ 0x800000000000000ULL
#define CORRECTION_POINTS 20 // Frequency dependent level correction table entries
#define CORRECTION_LOW_IN 0
#define CORRECTION_LNA 1
#define CORRECTION_LOW_ULTRA 2
#define CORRECTION_LNA_ULTRA 3
#define CORRECTION_DIRECT 4
#define CORRECTION_LNA_DIRECT 5
#define CORRECTION_HARM 6
#define CORRECTION_LNA_HARM 7
#define CORRECTION_LOW_OUT 8 // Must be same order as output path options!!!!!
#define CORRECTION_LOW_OUT_DIRECT 9
#define CORRECTION_LOW_OUT_ADF 10
#define CORRECTION_LOW_OUT_MIXER 11
#define CORRECTION_SIZE 12
extern freq_t ULTRA_MAX_FREQ; // Start of harmonic mode
extern freq_t MAX_LO_FREQ;
extern freq_t MAX_ABOVE_IF_FREQ; // Range to use for below IF
extern freq_t MIN_BELOW_IF_FREQ; // Range to use for below IF
extern freq_t ULTRA_THRESHOLD;
extern freq_t NORMAL_MAX_FREQ;
extern int max2871;
extern void set_freq_boundaries(void);
#endif
typedef float measurement_t[TRACES_MAX][POINTS_COUNT];
extern measurement_t measured;
#endif
extern freq_t minFreq;
extern freq_t maxFreq;
#define START_MIN minFreq
#define STOP_MAX maxFreq
extern const char TINYSA_VERSION[];
#ifdef TINYSA4
extern uint16_t hwid;
#endif
#define MAX_FREQ_TYPE 5
enum stimulus_type {
ST_START=0, ST_STOP, ST_CENTER, ST_SPAN, ST_CW, ST_DUMMY // Last is used in marker ops
};
void set_sweep_points(uint16_t points);
void update_frequencies(void);
void update_bands(void);
void set_sweep_frequency(int type, freq_t frequency);
freq_t get_sweep_frequency(int type);
void my_microsecond_delay(int t);
void my_veryfast_delay(int t);
float my_atof(const char *p);
freq_t my_atoui(const char *p);
int shell_printf(const char *fmt, ...);
int usage_printf(const char *fmt, ...);
void clear_backup(void);
const char *get_hw_version_text(void);
#ifdef __REMOTE_DESKTOP__
extern uint8_t remote_mouse_down;
extern uint8_t auto_capture;
typedef struct {
char new_str[6];
int16_t x;
int16_t y;
int16_t w;
int16_t h;
} remote_region_t;
void send_region(remote_region_t *rd, uint8_t * buf, uint16_t size);
#endif
void set_marker_frequency(int m, freq_t f);
void set_marker_time(int m, float f);
void set_marker_index(int m, int16_t idx);
void toggle_sweep(void);
void resume_once(uint16_t c);
#ifdef TINYSA4
void set_deviation(int d);
void set_depth(int d);
extern int LO_harmonic;
#endif
void toggle_mute(void);
void toggle_pulse(void);
void toggle_draw_line(void);
void load_default_properties(void);
enum {
AV_OFF, AV_MIN, AV_MAX_HOLD, AV_MAX_DECAY, AV_4, AV_16, AV_100, AV_QUASI, AV_TABLE, AV_DECONV
};
enum {
M_LOW, M_HIGH, M_GENLOW, M_GENHIGH, M_ULTRA
};
enum {
MO_NONE, MO_AM, MO_NFM,
#ifdef TINYSA4
MO_NFM2, MO_NFM3,
#endif
MO_WFM, MO_EXTERNAL, MO_MAX
};
#define MODE_OUTPUT(x) ((x) == M_GENLOW || (x) == M_GENHIGH )
#ifdef __ULTRA__
#define MODE_INPUT(x) ((x) == M_LOW || (x) == M_HIGH || (x) == M_ULTRA )
#else
#define MODE_INPUT(x) ((x) == M_LOW || (x) == M_HIGH )
#endif
#define MODE_HIGH(x) ((x) == M_HIGH || (x) == M_GENHIGH )
#define MODE_LOW(x) ((x) == M_LOW || (x) == M_GENLOW )
#ifdef __SI4432__
#define SI4432_RX 0
#define SI4432_LO 1
#define MODE_SELECT(x) (MODE_HIGH(x) ? SI4432_LO : SI4432_RX)
#endif
#ifdef __SI4468__
// Not use mode
#define MODE_SELECT(x) (MODE_HIGH(x) ? 1 : 0)
#endif
#define SWEEP_ENABLE 0x01
#define SWEEP_ONCE 0x02
#define SWEEP_CALIBRATE 0x04
#define SWEEP_SELFTEST 0x08
#define SWEEP_REMOTE 0x10
#ifdef __LISTEN__
//#define SWEEP_LISTEN 0x20
//#define SWEEP_FACTORY 0x20
#endif
#define SWEEP_CALIBRATE_HARMONIC 0x40
#define SWEEP_UI_MODE 0x80
extern uint8_t sweep_mode;
extern uint8_t completed;
extern const char * const info_about[];
#ifdef TINYSA4
void toggle_extra_lna(void);
void set_extra_lna(int t);
enum { A_DAC, A_PWM };
void set_audio_mode(uint16_t new_mode);
void pwm_start(int f);
void pwm_stop(void);
#ifdef __BANDS__
void reset_band(void);
#endif
#endif
// ------------------------------- sa_core.c ----------------------------------
extern float level_min(void);
extern float level_max(void);
extern float level_range(void);
extern float channel_power[3];
extern float channel_power_watt[3];
extern const char * const unit_string[];
extern uint16_t vbwSteps;
#ifdef __ULTRA__
extern freq_t ultra_start;
//extern bool ultra;
#endif
#ifdef TINYSA4
extern float measured_noise_figure;
extern float *drive_dBm;
extern bool level_error;
extern bool depth_error;
#else
extern const int8_t drive_dBm [];
#endif
extern int force_signal_path;
extern int test_output_switch;
extern int test_output_drive;
extern int test_output_attenuate;
extern int test_path;
extern uint8_t signal_is_AM;
extern const uint32_t reffer_freq[];
extern freq_t minFreq;
extern freq_t maxFreq;
int level_is_calibrated(void);
void reset_settings(int);
void update_min_max_freq(void);
//void ui_process_touch(void);
void SetPowerGrid(int);
void SetRefLevel(float);
void set_refer_output(int);
void toggle_below_IF(void);
int get_refer_output(void);
void set_attenuation(float);
float get_attenuation(void);
float get_level(void);
void set_harmonic(int);
void store_trace(int f, int t);
void subtract_trace(int t, int f);
//extern int setting.harmonic;
int search_is_greater(void);
void set_auto_attenuation(void);
void set_auto_reflevel(bool);
int is_paused(void);
float set_actual_power(float);
void set_actual_correction_value(int current_curve,int current_curve_index, float local_actual_level);
extern const int to_calibrate[6];
void SetGenerate(int);
void set_RBW(uint32_t rbw_x10);
#ifdef __VBW__
void set_VBW(uint32_t vbw_x100);
#endif
void set_lo_drive(int d);
void set_rx_drive(int d);
void set_IF(int f);
void set_step_delay(int t);
void set_offset_delay(int t);
void set_repeat(int);
void set_level_sweep(float);
void set_level(float);
void set_sweep_time_us(uint32_t);
//extern int setting.repeat;
//extern int setting.rbw;
#ifdef __SPUR__
//extern int setting.spur;
void set_spur(int v);
void toggle_spur(void);
void toggle_mirror_masking(void);
#endif
void set_average(int t, int);
//extern int setting.average;
void set_storage(void);
void set_clear_storage(void);
void set_subtract_storage(void);
void toggle_normalize(int);
void set_level_meter_or_waterfall(void);
void disable_waterfall(void);
#ifdef __LEVEL_METER__
void disable_level_meter(void);
#endif
void set_mode(int);
int GetMode(void);
void set_reflevel(float);
void user_set_reflevel(float);
#define REFLEVEL_MAX 9999.0
#define REFLEVEL_MIN 1.0e-12
void set_scale(float);
void user_set_scale(float);
void AllDirty(void);
void MenuDirty(void);
void toggle_LNA(void);
void toggle_AGC(void);
void redrawHisto(void);
void selftest(int);
void set_decay(int);
void set_attack(int);
void set_noise(int);
void toggle_tracking_output(void);
extern int32_t frequencyExtra;
void set_modulation(int);
void set_modulation_frequency(float);
int search_maximum(int m, freq_t center, int span);
//extern int setting.modulation;
void set_measurement(int);
// extern int settingSpeed;
//extern int setting.step_delay;
void sweep_remote(void);
void calculate_step_delay(void);
extern int generic_option_cmd( const char *cmd, const char *cmd_list, int argc, char *argv);
extern bool global_abort;
#ifdef __ULTRA__
void toggle_ultra(void);
void enable_ultra(int);
#endif
#ifdef TINYSA4
void clear_frequency_cache(void);
#ifndef __NEW_SWITCHES__
void toggle_high_out_adf4350(void);
extern int high_out_adf4350;
#endif
int set_actual_freq(freq_t);
void set_jump_freq(freq_t a, freq_t b, freq_t c);
int set_freq_corr(int);
void set_IF2(int f);
void set_R(int f);
extern void set_modulo(uint32_t f);
extern uint32_t local_modulo;
extern void fill_spur_table(void);
extern float low_out_offset(void);
extern float high_out_offset(void);
#define LOW_OUT_OFFSET low_out_offset()
#define HIGH_OUT_OFFSET high_out_offset()
extern bool debug_avoid;
extern bool progress_bar;
extern void toggle_debug_avoid(void);
extern float log_averaging_correction;
#else
void set_10mhz(freq_t);
#define LOW_OUT_OFFSET config.low_level_output_offset
#define HIGH_OUT_OFFSET config.high_level_output_offset
#endif
#ifdef __ULTRA__
extern bool debug_level;
extern void toggle_debug_level(void);
extern bool debug_spur;
extern void toggle_debug_spur(void);
#endif
#ifdef __AUDIO__
/*
* dsp.c
*/
// 5ms @ 48kHz
#define AUDIO_BUFFER_LEN 96
extern int16_t rx_buffer[AUDIO_BUFFER_LEN * 2];
#define STATE_LEN 32
#define SAMPLE_LEN 48
#ifdef ENABLED_DUMP
extern int16_t ref_buf[];
extern int16_t samp_buf[];
#endif
#endif
#ifdef __VNA__
void dsp_process(int16_t *src, size_t len);
void reset_dsp_accumerator(void);
void calculate_gamma(float *gamma);
void fetch_amplitude(float *gamma);
void fetch_amplitude_ref(float *gamma);
#endif
#ifdef __AUDIO__
/*
* tlv320aic3204.c
*/
extern void tlv320aic3204_init(void);
extern void tlv320aic3204_set_gain(int lgain, int rgain);
extern void tlv320aic3204_select(int channel);
#endif
/*
* plot.c
*/
// Offset of plot area
#define OFFSETX 30
#define OFFSETY 0
#define NGRIDY 10
// GRIDX calculated depends from frequency span
#ifdef __SCROLL__
extern uint16_t _grid_y;
#define GRIDY _grid_y
extern uint16_t graph_bottom;
#ifdef TINYSA4
#define SUPER_WATERFALL 90
#define BIG_WATERFALL 180
#define SMALL_WATERFALL 240
#define BIG_NUMBER_SPACE BIG_WATERFALL
#else
#define BIG_WATERFALL 90
#define SMALL_WATERFALL 180
#define BIG_NUMBER_SPACE SMALL_WATERFALL
#endif
#define NO_WATERFALL CHART_BOTTOM
#define CHART_BOTTOM (LCD_HEIGHT-10)
#define SCROLL_GRIDY (HEIGHT_SCROLL / NGRIDY)
#define NOSCROLL_GRIDY (CHART_BOTTOM / NGRIDY)
#else
#define GRIDY (CHART_BOTTOM / NGRIDY)
#endif
#define SD_CARD_START (LCD_HEIGHT-40-20)
#define BATTERY_START (LCD_HEIGHT-40)
#define WIDTH (LCD_WIDTH - OFFSETX)
#define HEIGHT (GRIDY*NGRIDY)
#define FREQUENCIES_XPOS1 OFFSETX
#define FREQUENCIES_XPOS2 (LCD_WIDTH-120)
#define FREQUENCIES_YPOS (LCD_HEIGHT-8)
//
#define CELLOFFSETX 0
#define AREA_WIDTH_NORMAL (CELLOFFSETX + WIDTH)
#define AREA_HEIGHT_NORMAL ( HEIGHT)
#define GRID_X_TEXT (AREA_WIDTH_NORMAL - 7*5)
// Marker start drag distance (can be bigger for various display resolution)
#define MARKER_PICKUP_DISTANCE 20
// Smith/polar chart
//#define P_CENTER_X (CELLOFFSETX + WIDTH/2)
//#define P_CENTER_Y (HEIGHT/2)
//#define P_RADIUS (HEIGHT/2)
// Menu Button
// Maximum menu buttons count
#ifdef TINYSA4
#define MENU_BUTTON_MAX 16
#define MENU_BUTTON_MIN 9
#else
#define MENU_BUTTON_MAX 16
#define MENU_BUTTON_MIN 7
#endif
#define MENU_BUTTON_WIDTH 80
#define MENU_BUTTON_BORDER 1
#define KEYBOARD_BUTTON_BORDER 2
#define FORM_BUTTON_BORDER 2
#define MENU_BUTTON_HEIGHT_N(n) (LCD_HEIGHT/(n)-1)
#define BROWSER_BUTTON_BORDER 1
// Browser window settings
#define FILES_COLUMNS (LCD_WIDTH/160) // columns in browser
#define FILES_ROWS 10 // rows in browser
#define FILES_PER_PAGE (FILES_COLUMNS*FILES_ROWS) // FILES_ROWS * FILES_COLUMNS
#define FILE_BOTTOM_HEIGHT 20 // Height of bottom buttons (< > X)
#define FILE_BUTTON_HEIGHT ((LCD_HEIGHT - FILE_BOTTOM_HEIGHT)/FILES_ROWS) // Height of file buttons
// Define message box width
#ifdef TINYSA4
#define MESSAGE_BOX_WIDTH 300
#else
#define MESSAGE_BOX_WIDTH 180
#endif
// Form button (at center screen better be less LCD_WIDTH - 2*OFFSETX)
#define MENU_FORM_WIDTH (LCD_WIDTH - 2*OFFSETX)
// Num Input height at bottom
#define NUM_INPUT_HEIGHT 32
extern uint16_t area_width;
extern uint16_t area_height;
// Define marker size (can be 0 or 1)
#ifdef TINYSA3
#define _MARKER_SIZE_ 0
#endif
#ifdef TINYSA4
#define _MARKER_SIZE_ 1
#endif
// font
extern const uint8_t x5x7_bits [];
extern const uint8_t x7x11b_bits [];
extern const uint8_t x10x14_bits[];
extern const uint8_t numfont16x22[];
#define FONT_SMALL 0
#define FONT_NORMAL 1
#define FONT_START_CHAR 0x16
#define FONT_MAX_WIDTH 7
#define FONT_WIDTH 5
#define FONT_GET_HEIGHT 7
#define FONT_STR_HEIGHT 8
#define FONT_GET_DATA(ch) ( &x5x7_bits[(ch-FONT_START_CHAR)*FONT_GET_HEIGHT])
#define FONT_GET_WIDTH(ch) (8-(x5x7_bits[(ch-FONT_START_CHAR)*FONT_GET_HEIGHT]&7))
#define bFONT_START_CHAR 0x16
#define bFONT_MAX_WIDTH 8
#define bFONT_WIDTH 7
#define bFONT_GET_HEIGHT 11
#define bFONT_STR_HEIGHT 11
#define bFONT_GET_DATA(ch) ( &x7x11b_bits[(ch-bFONT_START_CHAR)*bFONT_GET_HEIGHT])
#define bFONT_GET_WIDTH(ch) (8-(x7x11b_bits[(ch-bFONT_START_CHAR)*bFONT_GET_HEIGHT]&7))
#ifdef __NICE_BIG_FONT__
#define wFONT_START_CHAR 0x16
#define wFONT_MAX_WIDTH 12
#define wFONT_GET_HEIGHT 14
#define wFONT_STR_HEIGHT 16
#define wFONT_GET_DATA(ch) ( &x10x14_bits[(ch-wFONT_START_CHAR)*2*wFONT_GET_HEIGHT ])
#define wFONT_GET_WIDTH(ch) (14-(x10x14_bits[(ch-wFONT_START_CHAR)*2*wFONT_GET_HEIGHT+1]&0x7))
#else
#define wFONT_MAX_WIDTH 12
#define wFONT_GET_HEIGHT 14
#endif
#define NUM_FONT_GET_WIDTH 16
#define NUM_FONT_GET_HEIGHT 22
#define NUM_FONT_GET_DATA(ch) (&numfont16x22[ch*2*NUM_FONT_GET_HEIGHT])
#define KP_WIDTH (LCD_WIDTH / 4) // numeric keypad button width
#define KP_HEIGHT ((LCD_HEIGHT - NUM_INPUT_HEIGHT) / 4) // numeric keypad button height
#define KP_X_OFFSET 0 // numeric keypad X offset
#define KP_Y_OFFSET 0 // numeric keypad Y offset
#define KPF_WIDTH (LCD_WIDTH / 10) // text keypad button width
#define KPF_HEIGHT KPF_WIDTH // text keypad button height
#define KPF_X_OFFSET 0 // text keypad X offset
#define KPF_Y_OFFSET (LCD_HEIGHT - NUM_INPUT_HEIGHT - 4 * KPF_HEIGHT) // text keypad Y offset
#define S_ENTER "\026" // 0x16
#define S_DELTA "\027" // 0x17
#define S_SARROW "\030" // 0x18
#define S_INFINITY "\031" // 0x19
#define S_LARROW "\032" // 0x1A
#define S_RARROW "\033" // 0x1B
#define S_PI "\034" // 0x1C
#define S_MICRO "\035" // 0x1D
#define S_OHM "\036" // 0x1E
#define S_DEGREE "\037" // 0x1F
#define C_ENTER 0x16 // 0x16
#define C_LARROW 0x1A // 0x1A
#define C_RARROW 0x1B // 0x1B
// String prefix for select font size (use not printable chars)
#define FONT_s "\001"
#define _FONT_s 1
// bold as default
#define FONT_b ""
#define _FONT_b 2
#define FONT_w "\003"
#define _FONT_w 3
// Max palette indexes in config
#define MAX_PALETTE 32
// trace
#define MAX_TRACE_TYPE 12
enum trace_type {
TRC_LOGMAG=0, TRC_PHASE, TRC_DELAY, TRC_SMITH, TRC_POLAR, TRC_LINEAR, TRC_SWR, TRC_REAL, TRC_IMAG, TRC_R, TRC_X, TRC_OFF
};
// Mask for define rectangular plot
#define RECTANGULAR_GRID_MASK ((1<<TRC_LOGMAG)|(1<<TRC_PHASE)|(1<<TRC_DELAY)|(1<<TRC_LINEAR)|(1<<TRC_SWR)|(1<<TRC_REAL)|(1<<TRC_IMAG)|(1<<TRC_R)|(1<<TRC_X))
// LOGMAG: SCALE, REFPOS, REFVAL
// PHASE: SCALE, REFPOS, REFVAL
// DELAY: SCALE, REFPOS, REFVAL
// SMITH: SCALE, <REFPOS>, <REFVAL>
// LINMAG: SCALE, REFPOS, REFVAL
// SWR: SCALE, REFPOS, REFVAL
// Electrical Delay
// Phase
#define MAX_UNIT_TYPE 7 // Index of U_DBC
enum unit_type {
U_DBM=0, U_DBMV, U_DBUV, U_RAW, U_VOLT, U_VPP, U_WATT, U_DBC // dBc only for displaying delta marker info
};
#define UNIT_IS_LINEAR(T) ( T >= U_VOLT ? true : false)
#define UNIT_IS_LOG(T) ( T >= U_VOLT ? false : true)
float value(float);
float index_to_value(const int i);
float marker_to_value(const int i);
#define FREQ_MODE_START_STOP 0x0
#define FREQ_MODE_CENTER_SPAN 0x1
//#define FREQ_MODE_DOTTED_GRID 0x2
// Connection flag
#define _MODE_CONNECTION_MASK 0x04
#define _MODE_SERIAL 0x04
#define _MODE_USB 0x00
// don't save state
#define _MODE_DONT_SAVE_STATE 0x08
// auto name
#define _MODE_AUTO_FILENAME 0x10
#define _MODE_MHZ_CSV 0x20
#pragma pack(push, 4)
typedef struct config {
int32_t magic;
uint32_t deviceid;
uint16_t lcd_palette[MAX_PALETTE];
int16_t touch_cal[4];
uint32_t _serial_speed;
uint16_t dac_value;
uint16_t vbat_offset;
int16_t cor_am;
float low_level_offset;
float high_level_offset;
float low_level_output_offset;
float high_level_output_offset;
float receive_switch_offset;
#ifdef TINYSA4
float out_switch_offset;
float lna_level_offset;
float harmonic_level_offset;
float harmonic_lna_level_offset;
float shift_level_offset;
float shift1_level_offset;
float shift2_level_offset;
float shift3_level_offset;
float drive1_level_offset;
float drive2_level_offset;
float drive3_level_offset;
float direct_level_offset;
float ultra_level_offset;
float direct_lna_level_offset;
float ultra_lna_level_offset;
float adf_level_offset;
float direct_level_output_offset;
#endif
#ifdef __NOISE_FIGURE__
float noise_figure;
#endif
float correction_value[CORRECTION_SIZE][CORRECTION_POINTS];
freq_t correction_frequency[CORRECTION_SIZE][CORRECTION_POINTS];
#ifdef TINYSA4
freq_t setting_frequency_30mhz;
#else
freq_t setting_frequency_10mhz;
#endif
uint16_t gridlines;
uint16_t hambands;
#ifdef TINYSA4
freq_t frequency_IF1;
freq_t frequency_IF2;
#endif
#ifdef __ULTRA__
freq_t ultra_start;
freq_t harmonic_start;
freq_t direct_start;
freq_t direct_stop;
freq_t overclock;
int8_t ultra;
#endif
uint8_t input_is_calibrated;
uint8_t output_is_calibrated;
uint8_t _mode;
int8_t cor_wfm;
int8_t cor_nfm;
#ifdef TINYSA4
int8_t cor_nfm2;
int8_t cor_nfm3;
#endif
uint8_t _brightness;
#ifndef __NEW_SWITCHES__
uint8_t high_out_adf4350;
#endif
uint8_t flip;
#ifdef __ULTRA__
uint8_t direct;
#endif
#ifdef TINYSA4
uint8_t hide_21MHz;
#endif
float sweep_voltage;
float switch_offset;
int16_t ext_zero_level;
uint32_t dummy;
// uint8_t _reserved[22];
freq_t checksum;
} config_t;
#pragma pack(pop)
extern config_t config;
//#define settingLevelOffset config.level_offset
float get_level_offset(void);
extern uint8_t in_selftest;
extern int display_test(void);
extern void clear_marker_cache(void);
//
// Shell config functions and macros
// Serial connect definitions not used if Serial mode disabled
void shell_update_speed(void);
void shell_reset_console(void);
int shell_serial_printf(const char *fmt, ...);
void shell_executeCMDLine(char *line);
// marker
enum {
M_NORMAL=0,M_REFERENCE=1, M_DELTA=2, M_NOISE=4, M_STORED=8, M_AVER=16, M_TRACKING=32, M_DELETE=64 // Tracking must be last.
};
enum {
M_DISABLED = 0, M_ENABLED = 1
};
// Flags/macros for enable/disable traces
#define TRACE_ACTUAL_FLAG (1<<(TRACE_ACTUAL))
#define TRACE_STORED_FLAG (1<<(TRACE_STORED))
#define TRACE_TEMP_FLAG (1<<(TRACE_TEMP))
#ifdef TINYSA4
#define TRACE_STORED2_FLAG (1<<(TRACE_STORED2))
#endif
#define TRACE_ENABLE(t_mask) {setting._traces|= (t_mask);}
#define TRACE_DISABLE(t_mask) {setting._traces&=~(t_mask);}
#define IS_TRACES_ENABLED(t_mask) (setting._traces&(t_mask))
#define IS_TRACE_ENABLE(t) (setting._traces&(1<<(t)))
#define IS_TRACE_DISABLE(t) !(setting._traces&(1<<(t)))
// Enable trace for show only after sweep complete (disable it at call)
void enableTracesAtComplete(uint8_t mask);
typedef struct {
uint8_t mtype;
uint8_t enabled;
uint8_t ref;
uint8_t trace;
int16_t index;
freq_t frequency;
} marker_t;
#ifdef __LIMITS__
#ifdef TINYSA4
#define LIMITS_MAX 8
#else
#define LIMITS_MAX 6
#endif
#define REFERENCE_MAX TRACES_MAX
typedef struct {
uint8_t enabled;
float level;
freq_t frequency;
int16_t index;
} limit_t;
extern uint8_t active_limit;
extern void limits_update(void);
#endif
#define MARKERS_MAX MARKER_COUNT
#define MARKER_INVALID -1
extern int8_t previous_marker;
extern int8_t marker_tracking;
void plot_init(void);
void update_grid(void);
void request_to_redraw_grid(void);
void redraw_frame(void);
//void redraw_all(void);
void request_to_draw_cells_behind_menu(void);
void request_to_draw_cells_behind_numeric_input(void);
void redraw_marker(int marker);
void markmap_all_markers(void);
void plot_into_index(measurement_t measured);
void draw_frequencies(void);
void draw_all(bool flush);
void draw_cal_status(void);
//void markmap_all_markers(void);
int distance_to_index(int8_t t, uint16_t idx, int16_t x, int16_t y);
int search_nearest_index(int x, int y, int t);
int marker_search_max(int m);
int marker_search_left_max(int m);
int marker_search_right_max(int m);
int marker_search_left_min(int m);
int marker_search_right_min(int m);
void markers_reset(void);
// _request flag for update screen
#define REDRAW_CELLS (1<<0)
#define REDRAW_FREQUENCY (1<<1)
#define REDRAW_CAL_STATUS (1<<2)
#define REDRAW_MARKER (1<<3)
#define REDRAW_BATTERY (1<<4)
#define REDRAW_AREA (1<<5)
#define REDRAW_TRIGGER (1<<6)
#define REDRAW_INBETWEEN (1<<7)
extern uint16_t redraw_request;