forked from erikkaashoek/tinySA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanovna.h
1142 lines (1007 loc) · 32.4 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
#include "adc_F303.h"
#define TINYSA4
#else
#define TINYSA3
#endif
// Need enable HAL_USE_SPI in halconf.h
#define __USE_DISPLAY_DMA__
#define __SA__
#ifdef TINYSA3
#define __SI4432__
#endif
#ifdef TINYSA4
#define __SI4463__
#define __SI4468__
#define __ADF4351__
#endif
#define __PE4302__
//#define __SIMULATION__
//#define __PIPELINE__
#define __SCROLL__
#define __ICONS__
#define __MEASURE__
#define __LINEARITY__ // Not available
#define __SELFTEST__
#define __CALIBRATE__
#define __FAST_SWEEP__ // Pre-fill SI4432 RSSI buffer to get fastest sweep in zero span mode
//#define __HAM_BAND__
//#define __ULTRA__ // Add harmonics mode on low input.
#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)
#define __SINGLE_LETTER__
#define __NICE_BIG_FONT__
#define __QUASI_PEAK__
//#define __REMOTE_DESKTOP__
#ifdef TINYSA3
#define DEFAULT_IF 433800000
#define DEFAULT_SPUR_IF 434000000
#define DEFAULT_MAX_FREQ 350000000
#define HIGH_MIN_FREQ_MHZ 240
#define HIGH_MAX_FREQ_MHZ 960
#endif
#ifdef TINYSA4
#define DEFAULT_IF 978000000
#define DEFAULT_SPUR_IF 979000000
#define DEFAULT_MAX_FREQ 800000000
#define HIGH_MIN_FREQ_MHZ 850
#define HIGH_MAX_FREQ_MHZ 1150
#endif
/*
* main.c
*/
#ifdef __SA__
#define POINTS_COUNT 290
#define MARKER_COUNT 4
#define TRACES_MAX 3
//#define TRACE_AGE 3
#define TRACE_ACTUAL 2
#define TRACE_STORED 1
#define TRACE_TEMP 0
// #define age_t measured[TRACE_AGE]
#define stored_t measured[TRACE_STORED]
#define actual_t measured[TRACE_ACTUAL]
#define temp_t measured[TRACE_TEMP]
typedef uint32_t freq_t;
#define CORRECTION_POINTS 10 // Frequency dependent level correction table entries
typedef float measurement_t[TRACES_MAX][POINTS_COUNT];
extern measurement_t measured;
#endif
#ifdef __REMOTE_DESKTOP__
extern volatile int auto_capture;
extern volatile int mouse_x;
extern volatile int mouse_y;
extern volatile int mouse_down;
#endif
#ifdef __VNA__
// Minimum frequency set
#define START_MIN 50000
// Maximum frequency set
#define STOP_MAX 2700000000U
// Frequency offset (sin_cos table in dsp.c generated for this offset, if change need create new table)
#define FREQUENCY_OFFSET 5000
// Speed of light const
#define SPEED_OF_LIGHT 299792458
// pi const
#define VNA_PI 3.14159265358979323846
#define POINTS_COUNT 101
extern float measured[2][POINTS_COUNT][2];
#define CAL_LOAD 0
#define CAL_OPEN 1
#define CAL_SHORT 2
#define CAL_THRU 3
#define CAL_ISOLN 4
#define CALSTAT_LOAD (1<<0)
#define CALSTAT_OPEN (1<<1)
#define CALSTAT_SHORT (1<<2)
#define CALSTAT_THRU (1<<3)
#define CALSTAT_ISOLN (1<<4)
#define CALSTAT_ES (1<<5)
#define CALSTAT_ER (1<<6)
#define CALSTAT_ET (1<<7)
#define CALSTAT_ED CALSTAT_LOAD
#define CALSTAT_EX CALSTAT_ISOLN
#define CALSTAT_APPLY (1<<8)
#define CALSTAT_INTERPOLATED (1<<9)
#define ETERM_ED 0 /* error term directivity */
#define ETERM_ES 1 /* error term source match */
#define ETERM_ER 2 /* error term refrection tracking */
#define ETERM_ET 3 /* error term transmission tracking */
#define ETERM_EX 4 /* error term isolation */
#define DOMAIN_MODE (1<<0)
#define DOMAIN_FREQ (0<<0)
#define DOMAIN_TIME (1<<0)
#define TD_FUNC (0b11<<1)
#define TD_FUNC_BANDPASS (0b00<<1)
#define TD_FUNC_LOWPASS_IMPULSE (0b01<<1)
#define TD_FUNC_LOWPASS_STEP (0b10<<1)
#define TD_WINDOW (0b11<<3)
#define TD_WINDOW_NORMAL (0b00<<3)
#define TD_WINDOW_MINIMUM (0b01<<3)
#define TD_WINDOW_MAXIMUM (0b10<<3)
#define FFT_SIZE 256
void cal_collect(int type);
void cal_done(void);
#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 set_sweep_frequency(int type, freq_t frequency);
freq_t get_sweep_frequency(int type);
void my_microsecond_delay(int t);
float my_atof(const char *p);
int shell_printf(const char *fmt, ...);
#ifdef __REMOTE_DESKTOP__
void send_region(const char *t, int x, int y, int w, int h);
void send_buffer(uint8_t * buf, int s);
#endif
void set_marker_frequency(int m, freq_t f);
void toggle_sweep(void);
void toggle_mute(void);
void load_default_properties(void);
enum {
AV_OFF, AV_MIN, AV_MAX_HOLD, AV_MAX_DECAY, AV_4, AV_16, AV_QUASI
};
enum {
M_LOW, M_HIGH, M_GENLOW, M_GENHIGH, M_ULTRA
};
enum {
MO_NONE, MO_AM, MO_NFM, MO_WFM, MO_EXTERNAL,
};
#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 )
#define MODE_SELECT(x) (MODE_HIGH(x) ? 1 : 0)
#define SWEEP_ENABLE 0x01
#define SWEEP_ONCE 0x02
#define SWEEP_CALIBRATE 0x04
#define SWEEP_SELFTEST 0x08
#define SWEEP_REMOTE 0x10
//#define SWEEP_FACTORY 0x20
extern uint8_t sweep_mode;
extern bool completed;
extern const char *info_about[];
// ------------------------------- sa_core.c ----------------------------------
extern const char * const unit_string[];
extern uint8_t signal_is_AM;
extern const int 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);
//extern int setting.harmonic;
int search_is_greater(void);
void set_auto_attenuation(void);
void set_auto_reflevel(int);
int is_paused(void);
void set_actual_power(float);
void SetGenerate(int);
void set_RBW(uint32_t rbw_x10);
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);
int GetAverage(void);
//extern int setting.average;
void set_storage(void);
void set_clear_storage(void);
void set_subtract_storage(void);
void toggle_normalize(void);
void toggle_waterfall(void);
void disable_waterfall(void);
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 self_test(int);
void set_decay(int);
void set_attack(int);
void set_noise(int);
void toggle_tracking_output(void);
extern int32_t frequencyExtra;
void set_10mhz(freq_t);
void set_modulation(int);
void set_modulation_frequency(int);
int search_maximum(int m, int center, int span);
//extern int setting.modulation;
void set_measurement(int);
// extern int settingSpeed;
//extern int setting.step_delay;
void sweep_remote(void);
extern int generic_option_cmd( const char *cmd, const char *cmd_list, int argc, char *argv);
#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;
#define BIG_WATERFALL 90
#define SMALL_WATERFALL 180
#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 BATTERY_START (LCD_HEIGHT-40)
#define WIDTH (LCD_WIDTH - 1 - OFFSETX)
#define HEIGHT (GRIDY*NGRIDY)
#define CELLWIDTH (32)
#define CELLHEIGHT (32)
#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 + 1)
#define AREA_HEIGHT_NORMAL ( HEIGHT + 1)
#define GRID_X_TEXT (AREA_WIDTH_NORMAL - 7*5)
// 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
#define MENU_BUTTON_MAX 8
#define MENU_BUTTON_WIDTH 80
#define MENU_BUTTON_HEIGHT (LCD_HEIGHT/8-1)
#define MENU_BUTTON_BORDER 1
#define KEYBOARD_BUTTON_BORDER 2
#define FORM_BUTTON_BORDER 2
// 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 int16_t area_width;
extern int16_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_START_CHAR 0x17
#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 0x17
#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 0x17
#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])
#if 1
#define KP_WIDTH ((LCD_WIDTH) / 4)// numeric keypad button width
#define KP_HEIGHT ((LCD_HEIGHT - NUM_INPUT_HEIGHT) / 4) // numeric keypad button height
// Key x, y position (0 - 15) on screen
#define KP_GET_X(posx) ((posx) * KP_WIDTH) // numeric keypad left
#define KP_GET_Y(posy) ((posy) * KP_HEIGHT) // numeric keypad top
#else
#define KP_WIDTH (LCD_HEIGHT/5)
#define KP_HEIGHT (LCD_HEIGHT/5)
// Key x, y position (0 - 15) on screen
#define KP_GET_X(posx) ((posx)*KP_WIDTH + (LCD_WIDTH-MENU_BUTTON_WIDTH-5-KP_WIDTH*4))
#define KP_GET_Y(posy) ((posy)*KP_HEIGHT + 12 )
#endif
#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
// 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 4
enum unit_type {
U_DBM=0, U_DBMV, U_DBUV, U_VOLT, 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);
typedef struct trace {
uint8_t enabled;
uint8_t type;
uint8_t channel;
uint8_t reserved;
float scale;
float refpos;
} trace_t;
#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
typedef struct config {
int32_t magic;
uint16_t lcd_palette[MAX_PALETTE];
int16_t touch_cal[4];
uint32_t _serial_speed;
#ifdef __VNA__
freq_t harmonic_freq_threshold;
#endif
uint16_t dac_value;
uint16_t vbat_offset;
float low_level_offset;
float high_level_offset;
freq_t low_correction_frequency[CORRECTION_POINTS];
float low_correction_value[CORRECTION_POINTS];
freq_t high_correction_frequency[CORRECTION_POINTS];
float high_correction_value[CORRECTION_POINTS];
uint32_t deviceid;
freq_t setting_frequency_10mhz;
uint16_t gridlines;
uint16_t hambands;
#ifdef TINYSA4
freq_t frequency_IF2;
#endif
int8_t _mode;
int8_t cor_am;
int8_t cor_wfm;
int8_t cor_nfm;
float sweep_voltage;
uint32_t dummy;
// uint8_t _reserved[22];
freq_t checksum;
} config_t;
extern config_t config;
//#define settingLevelOffset config.level_offset
float get_level_offset(void);
void set_trace_type(int t, int type);
void set_trace_channel(int t, int channel);
void set_trace_scale(float scale);
void set_trace_refpos(float refpos);
float get_trace_scale(int t);
float get_trace_refpos(int t);
const char *get_trace_typename(int t);
extern int in_selftest;
extern int display_test(void);
//
// Shell config functions and macros
// Serial connect definitions not used if Serial mode disabled
// Minimum speed - USART_SPEED_MULTIPLIER
// Maximum speed - USART_SPEED_MULTIPLIER * 256
// Can be: 19200, 38400, 57600, 76800, 115200, 230400, 460800, 921600, 1843200, 3686400
#define USART_SPEED_MULTIPLIER 19200
#define USART_SPEED_SETTING(speed) ((speed)/USART_SPEED_MULTIPLIER - 1)
#define USART_GET_SPEED(idx) (((idx) + 1) * USART_SPEED_MULTIPLIER)
void shell_update_speed(void);
void shell_reset_console(void);
int shell_serial_printf(const char *fmt, ...);
#ifdef __VNA
void set_electrical_delay(float picoseconds);
float get_electrical_delay(void);
float groupdelay_from_array(int i, float array[POINTS_COUNT][2]);
#endif
// marker
enum {
M_NORMAL=0,M_REFERENCE=1, M_DELTA=2, M_NOISE=4, M_TRACKING=8, M_DELETE=16 // Tracking must be last.
};
enum {
M_DISABLED = false, M_ENABLED = true
};
typedef struct {
int8_t enabled;
int8_t mtype;
int16_t index;
freq_t frequency;
} marker_t;
#define MARKERS_MAX 4
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 force_set_markmap(void);
void draw_frequencies(void);
void draw_all(bool flush);
void draw_cal_status(void);
//void markmap_all_markers(void);
void marker_position(int m, int t, int *x, int *y);
int search_nearest_index(int x, int y, int t);
void set_marker_search(int mode);
int marker_search(void);
int marker_search_max(void);
int marker_search_left(int from);
int marker_search_right(int from);
int marker_search_left_max(int from);
int marker_search_right_max(int from);
int marker_search_left_min(int from);
int marker_search_right_min(int from);
// _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)
extern volatile uint8_t redraw_request;
/*
* ili9341.c
*/
// SPI bus revert byte order
// 16-bit gggBBBbb RRRrrGGG
#define RGB565(r,g,b) ( (((g)&0x1c)<<11) | (((b)&0xf8)<<5) | ((r)&0xf8) | (((g)&0xe0)>>5) )
#define RGBHEX(hex) ( (((hex)&0x001c00)<<3) | (((hex)&0x0000f8)<<5) | (((hex)&0xf80000)>>16) | (((hex)&0x00e000)>>13) )
#define HEXRGB(hex) ( (((hex)>>3)&0x001c00) | (((hex)>>5)&0x0000f8) | (((hex)<<16)&0xf80000) | (((hex)<<13)&0x00e000) )
// Define size of screen buffer in pixels (one pixel 16bit size)
#define SPI_BUFFER_SIZE (CELLWIDTH*CELLHEIGHT)
#define LCD_WIDTH 320
#define LCD_HEIGHT 240
#define LCD_BG_COLOR 0
#define LCD_FG_COLOR 1
#define LCD_GRID_COLOR 2
#define LCD_MENU_COLOR 3
#define LCD_MENU_TEXT_COLOR 4
#define LCD_MENU_ACTIVE_COLOR 5
#define LCD_TRACE_1_COLOR 6
#define LCD_TRACE_2_COLOR 7
#define LCD_TRACE_3_COLOR 8
#define LCD_TRACE_4_COLOR 9
#define LCD_NORMAL_BAT_COLOR 10
#define LCD_LOW_BAT_COLOR 11
#define LCD_TRIGGER_COLOR 12
#define LCD_RISE_EDGE_COLOR 13
#define LCD_FALLEN_EDGE_COLOR 14
#define LCD_SWEEP_LINE_COLOR 15
#define LCD_BW_TEXT_COLOR 16
#define LCD_INPUT_TEXT_COLOR 17
#define LCD_INPUT_BG_COLOR 18
#define LCD_BRIGHT_COLOR_BLUE 19
#define LCD_BRIGHT_COLOR_RED 20
#define LCD_BRIGHT_COLOR_GREEN 21
#define LCD_DARK_GREY 22
#define LCD_LIGHT_GREY 23
#define LCD_HAM_COLOR 24
#define LCD_GRID_VALUE_COLOR 25
#define LCD_M_REFERENCE 26
#define LCD_M_DELTA 27
#define LCD_M_NOISE 28
#define LCD_M_DEFAULT 29
#define LCD_DEFAULT_PALETTE {\
[LCD_BG_COLOR ] = RGB565( 0, 0, 0), \
[LCD_FG_COLOR ] = RGB565(255,255,255), \
[LCD_GRID_COLOR ] = RGB565(128,128,128), \
[LCD_MENU_COLOR ] = RGB565(230,230,230), \
[LCD_MENU_TEXT_COLOR ] = RGB565( 0, 0, 0), \
[LCD_MENU_ACTIVE_COLOR] = RGB565(210,210,210), \
[LCD_TRACE_1_COLOR ] = RGB565(255, 0, 0), \
[LCD_TRACE_2_COLOR ] = RGB565( 0,255, 0), \
[LCD_TRACE_3_COLOR ] = RGB565(255,255, 0), \
[LCD_TRACE_4_COLOR ] = RGB565(255, 0,255), \
[LCD_NORMAL_BAT_COLOR ] = RGB565( 31,227, 0), \
[LCD_LOW_BAT_COLOR ] = RGB565(255, 0, 0), \
[LCD_TRIGGER_COLOR ] = RGB565( 0, 0,255), \
[LCD_RISE_EDGE_COLOR ] = RGB565(255,255,255), \
[LCD_FALLEN_EDGE_COLOR] = RGB565(128,128,128), \
[LCD_SWEEP_LINE_COLOR ] = RGB565( 0,255, 0), \
[LCD_BW_TEXT_COLOR ] = RGB565(128,128,128), \
[LCD_INPUT_TEXT_COLOR ] = RGB565( 0, 0, 0), \
[LCD_INPUT_BG_COLOR ] = RGB565(255,255,255), \
[LCD_BRIGHT_COLOR_BLUE] = RGB565( 0, 0,255), \
[LCD_BRIGHT_COLOR_RED ] = RGB565(255,128,128), \
[LCD_BRIGHT_COLOR_GREEN]= RGB565( 0,255, 0), \
[LCD_DARK_GREY ] = RGB565(140,140,140), \
[LCD_LIGHT_GREY ] = RGB565(220,220,220), \
[LCD_HAM_COLOR ] = RGB565( 80, 80, 80), \
[LCD_GRID_VALUE_COLOR ] = RGB565(196,196,196), \
[LCD_M_REFERENCE ] = RGB565(255,255,255), \
[LCD_M_DELTA ] = RGB565( 0,255, 0), \
[LCD_M_NOISE ] = RGB565( 0,255,255), \
[LCD_M_DEFAULT ] = RGB565(255,255, 0), \
}
#define GET_PALTETTE_COLOR(idx) config.lcd_palette[idx]
extern uint16_t foreground_color;
extern uint16_t background_color;
extern uint16_t spi_buffer[SPI_BUFFER_SIZE];
// Used for easy define big Bitmap as 0bXXXXXXXXX image
#define _BMP8(d) ((d)&0xFF)
#define _BMP16(d) (((d)>>8)&0xFF), ((d)&0xFF)
#define _BMP24(d) (((d)>>16)&0xFF), (((d)>>8)&0xFF), ((d)&0xFF)
#define _BMP32(d) (((d)>>24)&0xFF), (((d)>>16)&0xFF), (((d)>>8)&0xFF), ((d)&0xFF)
void ili9341_init(void);
void ili9341_test(int mode);
void ili9341_bulk(int x, int y, int w, int h);
void ili9341_fill(int x, int y, int w, int h);
void ili9341_set_foreground(uint16_t fg_idx);
void ili9341_set_background(uint16_t bg_idx);
void ili9341_clear_screen(void);
void blit8BitWidthBitmap(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *bitmap);
void blit16BitWidthBitmap(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint16_t *bitmap);
void ili9341_drawchar(uint8_t ch, int x, int y);
void ili9341_drawstring(const char *str, int x, int y);
void ili9341_drawstring_7x13(const char *str, int x, int y);
void ili9341_drawstring_10x14(const char *str, int x, int y);
void ili9341_drawstringV(const char *str, int x, int y);
int ili9341_drawchar_size(uint8_t ch, int x, int y, uint8_t size);
void ili9341_drawstring_size(const char *str, int x, int y, uint8_t size);
void ili9341_drawfont(uint8_t ch, int x, int y);
void ili9341_read_memory(int x, int y, int w, int h, int len, uint16_t* out);
void ili9341_line(int x0, int y0, int x1, int y1);
void show_version(void);
/*
* flash.c
*/
typedef struct setting
{
uint32_t magic;
// freq_t _frequency0;
// freq_t _frequency1;
int mode;
uint16_t _sweep_points;
int16_t attenuate_x2;
int auto_attenuation;
int atten_step;
uint32_t rbw_x10;
int below_IF;
int average;
int show_stored;
int subtract_stored;
int lo_drive; // 0-3 , 3dB steps
int rx_drive; // 0-15 , 7=+20dBm, 3dB steps
int agc;
int lna;
int auto_reflevel;
float reflevel;
float scale;
int tracking;
int modulation;
int step_delay;
freq_t frequency_step;
int test;
int harmonic;
int decay;
int attack;
int noise;
uint32_t vbw_x10;
int tracking_output;
int repeat;
freq_t frequency0;
freq_t frequency1;
freq_t frequency_IF;
int freq_mode;
int measurement;
int refer;
int spur_removal;
int mirror_masking;
trace_t _trace[TRACES_MAX];
marker_t _markers[MARKERS_MAX];
int8_t _active_marker;
int8_t unit;
float offset;
float trigger_level;
int trigger_direction;
int trigger;
int linearity_step;
float level;
float level_sweep;
uint32_t sweep_time_us;
systime_t measure_sweep_time_us;
uint32_t actual_sweep_time_us;
uint32_t additional_step_delay_us;
int test_argument;
int auto_IF;
unsigned int unit_scale_index;
float unit_scale;
int mute;
int step_delay_mode;
int offset_delay;
int fast_speedup;
float normalize_level; // Level to set normalize to, zero if not doing anything
int modulation_frequency;
int trigger_mode;
int slider_position;
int32_t slider_span;
freq_t *correction_frequency;
float *correction_value;
uint32_t checksum; // must be last
}setting_t;
extern setting_t setting;
extern int setting_frequency_10mhz;
void reset_settings(int m);
#define S_IS_AUTO(x) ((x)&2)
#define S_STATE(X) ((X)&1)
enum { S_OFF=0, S_ON=1, S_AUTO_OFF=2, S_AUTO_ON=3 };
enum { SD_NORMAL, SD_PRECISE, SD_FAST, SD_MANUAL };
#ifdef __FAST_SWEEP__
#define MINIMUM_SWEEP_TIME 1800U // Minimum sweep time on zero span in uS
#else
#define MINIMUM_SWEEP_TIME 15000U // Minimum sweep time on zero span in uS
#endif
#define MAXIMUM_SWEEP_TIME 600000000U // Maximum sweep time uS
#define ONE_SECOND_TIME 1000000U // One second uS
#define ONE_MS_TIME 1000U // One ms uS
#define REPEAT_TIME 111 // Time per extra repeat in uS
#define MEASURE_TIME 127 // Time per single point measurement with vbwstep =1 without step delay in uS
extern freq_t frequencies[POINTS_COUNT];
extern const float unit_scale_value[];
extern const char * const unit_scale_text[];
#if 1 // Still sufficient flash
// Flash save area - flash7 : org = 0x0801B000, len = 20k in *.ld file
// 2k - for config save
// 9 * 2k for setting_t + stored trace
#define SAVEAREA_MAX 9
// STM32 minimum page size for write
#define FLASH_PAGESIZE 0x800
// config save area (flash7 addr)
#define SAVE_CONFIG_ADDR 0x0801B000
#define SAVE_CONFIG_SIZE 0x00000800
// setting_t save area (save area + config size)
#define SAVE_PROP_CONFIG_ADDR (SAVE_CONFIG_ADDR + SAVE_CONFIG_SIZE)
#define SAVE_PROP_CONFIG_SIZE 0x00000800
// Should include all save slots
#define SAVE_CONFIG_AREA_SIZE (SAVE_CONFIG_SIZE + SAVEAREA_MAX * SAVE_PROP_CONFIG_SIZE)
#else // Just in case flash runs out
// Flash save area - flash7 : org = 0x0801D000, len = 12k in *.ld file
// 2k - for config save
// 9 * 2k for setting_t + stored trace
#define SAVEAREA_MAX 5
// STM32 minimum page size for write
#define FLASH_PAGESIZE 0x800
// config save area (flash7 addr)
#define SAVE_CONFIG_ADDR 0x0801D000
#define SAVE_CONFIG_SIZE 0x00000800
// setting_t save area (save area + config size)
#define SAVE_PROP_CONFIG_ADDR (SAVE_CONFIG_ADDR + SAVE_CONFIG_SIZE)
#define SAVE_PROP_CONFIG_SIZE 0x00000800
// Should include all save slots
#define SAVE_CONFIG_AREA_SIZE (SAVE_CONFIG_SIZE + SAVEAREA_MAX * SAVE_PROP_CONFIG_SIZE)
#endif
#if 0
typedef struct properties {
uint32_t magic;
preset_t setting;
// freq_t _frequency0;
// freq_t _frequency1;
uint16_t _sweep_points;
#ifdef __VNA__
uint16_t _cal_status;
#endif
#ifdef __SA__
// freq_t _frequency_IF; //IF frequency
#endif
// freq_t _frequencies[POINTS_COUNT];
#ifdef __VNA__
float _cal_data[5][POINTS_COUNT][2];
float _electrical_delay; // picoseconds
#endif
trace_t _trace[TRACES_MAX];
marker_t _markers[MARKERS_MAX];
int8_t _active_marker;
#ifdef __VNA__
float _velocity_factor; // %
uint8_t _domain_mode; /* 0bxxxxxffm : where ff: TD_FUNC m: DOMAIN_MODE */
uint8_t _marker_smith_format;
uint8_t _bandwidth;
#endif
uint8_t _reserved[2];
uint32_t checksum;
} properties_t;
#endif
//sizeof(properties_t) == 0x1200
#define CONFIG_MAGIC 0x434f4e47 /* 'CONF' */
extern int16_t lastsaveid;
//extern properties_t *active_props;
//extern properties_t current_props;
//#define frequency0 current_props._frequency0
//#define frequency1 current_props._frequency1
#define sweep_points setting._sweep_points
#ifdef __VNA__
#define cal_status current_props._cal_status
#endif
#ifdef __SA__
//#define frequency_IF current_props._frequency_IF
#endif
//#define frequencies current_props._frequencies
#ifdef __VNA__
#define cal_data active_props->_cal_data
#define electrical_delay current_props._electrical_delay
#endif
#define trace setting._trace
#define markers setting._markers
#define active_marker setting._active_marker
#ifdef __VNA__
#define domain_mode current_props._domain_mode
#define velocity_factor current_props._velocity_factor
#define marker_smith_format current_props._marker_smith_format
#define bandwidth current_props._bandwidth
#endif
#define FREQ_IS_STARTSTOP() (!(setting.freq_mode&FREQ_MODE_CENTER_SPAN))
#define FREQ_IS_CENTERSPAN() (setting.freq_mode&FREQ_MODE_CENTER_SPAN)
#define FREQ_IS_CW() (setting.frequency0 == setting.frequency1)
int caldata_recall(uint16_t id);
int caldata_save(uint16_t id);
//const properties_t *caldata_ref(int id);
int config_save(void);
int config_recall(void);
void clear_all_config_prop_data(void);
/*
* ui.c
*/
extern void ui_init(void);
extern void ui_process(void);
int current_menu_is_form(void);
void ui_mode_normal(void);
void ui_mode_menu(void);
void menu_push_lowoutput(void);
void menu_push_highoutput(void);
void menu_move_top(void);
void draw_menu(void);
int check_touched(void);
int invoke_quick_menu(int);
// Irq operation process set
#define OP_NONE 0x00
#define OP_LEVER 0x01
#define OP_TOUCH 0x02
#define OP_CONSOLE 0x04
//#define OP_FREQCHANGE 0x04
extern volatile uint8_t operation_requested;