forked from erikkaashoek/tinySA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
3099 lines (2845 loc) · 81 KB
/
main.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
/*
*
* 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.
*/
//#define HAL_USE_SERIAL 1
//#define STM32_SERIAL_USE_USART1 1
#include "ch.h"
#include "hal.h"
//#include "hal_serial.h"
#include "usbcfg.h"
#ifdef __VNA__
#include "si5351.h"
#endif
#include "nanovna.h"
#ifdef __VNA__
#include "fft.h"
#endif
#include <chprintf.h>
#include <string.h>
#include <math.h>
extern freq_t minFreq;
extern freq_t maxFreq;
freq_t frequencyStart;
freq_t frequencyStop;
int32_t frequencyExtra;
#define START_MIN minFreq
#define STOP_MAX maxFreq
/*
* Shell settings
*/
// If need run shell as thread (use more amount of memory fore stack), after
// enable this need reduce spi_buffer size, by default shell run in main thread
// #define VNA_SHELL_THREAD
static BaseSequentialStream *shell_stream;
// Shell new line
#define VNA_SHELL_NEWLINE_STR "\r\n"
// Shell command promt
#define VNA_SHELL_PROMPT_STR "ch> "
// Shell max arguments
#define VNA_SHELL_MAX_ARGUMENTS 4
// Shell max command line size
#define VNA_SHELL_MAX_LENGTH 48
// Shell command functions prototypes
typedef void (*vna_shellcmd_t)(int argc, char *argv[]);
#define VNA_SHELL_FUNCTION(command_name) \
static void command_name(int argc, char *argv[])
// Shell command line buffer, args, nargs, and function ptr
static char shell_line[VNA_SHELL_MAX_LENGTH];
static char *shell_args[VNA_SHELL_MAX_ARGUMENTS + 1];
static uint16_t shell_nargs;
static volatile vna_shellcmd_t shell_function = 0;
//#define ENABLED_DUMP
// Allow get threads debug info
#define ENABLE_THREADS_COMMAND
// RTC time not used
//#define ENABLE_TIME_COMMAND
// Enable vbat_offset command, allow change battery voltage correction in config
#define ENABLE_VBAT_OFFSET_COMMAND
// Info about NanoVNA, need fore soft
#define ENABLE_INFO_COMMAND
// Enable color command, allow change config color for traces, grid, menu
#define ENABLE_COLOR_COMMAND
#ifdef __USE_SERIAL_CONSOLE__
#define ENABLE_USART_COMMAND
#endif
#ifdef __VNA__
static void apply_error_term_at(int i);
static void apply_edelay_at(int i);
static void cal_interpolate(int s);
#endif
void update_frequencies(void);
static void set_frequencies(freq_t start, freq_t stop, uint16_t points);
static bool sweep(bool break_on_operation);
#ifdef __VNA__
static void transform_domain(void);
#define DRIVE_STRENGTH_AUTO (-1)
#define FREQ_HARMONICS (config.harmonic_freq_threshold)
#define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS)
// Obsolete, always use interpolate
#define cal_auto_interpolate TRUE
static int8_t drive_strength = DRIVE_STRENGTH_AUTO;
#endif
uint8_t sweep_mode = SWEEP_ENABLE;
volatile uint8_t redraw_request = 0; // contains REDRAW_XXX flags
volatile int auto_capture = false;
// Version text, displayed in Config->Version menu, also send by info command
const char *info_about[]={
BOARD_NAME,
"2019-2020 Copyright @Erik Kaashoek",
"2016-2020 Copyright @edy555",
"SW licensed under GPL. See: https://github.com/erikkaashoek/tinySA",
"Version: " VERSION,
"Build Time: " __DATE__ " - " __TIME__,
"Kernel: " CH_KERNEL_VERSION,
"Compiler: " PORT_COMPILER_NAME,
"Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME,
"Port Info: " PORT_INFO,
"Platform: " PLATFORM_NAME,
0 // sentinel
};
uint16_t dirty = true;
bool completed = false;
static THD_WORKING_AREA(waThread1, 768);
static THD_FUNCTION(Thread1, arg)
{
(void)arg;
chRegSetThreadName("sweep");
ui_process();
while (1) {
// START_PROFILE
if (sweep_mode&(SWEEP_ENABLE|SWEEP_ONCE)) {
// if (dirty)
completed = sweep(true);
sweep_mode&=~SWEEP_ONCE;
} else if (sweep_mode & SWEEP_SELFTEST) {
// call from lowest level to save stack space
self_test(setting.test);
// sweep_mode = SWEEP_ENABLE;
#ifdef __SINGLE_LETTER__
} else if (sweep_mode & SWEEP_REMOTE) {
sweep_remote();
#endif
} else if (sweep_mode & SWEEP_CALIBRATE) {
// call from lowest level to save stack space
calibrate();
sweep_mode = SWEEP_ENABLE;
} else {
// if (setting.mode != -1)
__WFI();
}
// STOP_PROFILE
// Run Shell command in sweep thread
if (shell_function) {
operation_requested = OP_NONE; // otherwise commands will be aborted
shell_function(shell_nargs - 1, &shell_args[1]);
shell_function = 0;
osalThreadSleepMilliseconds(10);
if (dirty) {
if (MODE_OUTPUT(setting.mode))
draw_menu(); // update screen if in output mode and dirty
else
redraw_request |= REDRAW_CAL_STATUS | REDRAW_AREA | REDRAW_FREQUENCY;
}
// continue;
}
// START_PROFILE
// Process UI inputs
if (!(sweep_mode & SWEEP_SELFTEST))
ui_process();
// Process collected data, calculate trace coordinates and plot only if scan
// completed
if (/* sweep_mode & SWEEP_ENABLE && */ completed) {
#ifdef __VNA__
if ((domain_mode & DOMAIN_MODE) == DOMAIN_TIME) transform_domain();
#endif
// Prepare draw graphics, cache all lines, mark screen cells for redraw
plot_into_index(measured);
redraw_request |= REDRAW_CELLS | REDRAW_BATTERY;
if (uistat.marker_tracking) {
int i = marker_search();
if (i != -1 && active_marker != -1) {
markers[active_marker].index = i;
markers[active_marker].frequency = frequencies[i];
redraw_request |= REDRAW_MARKER;
}
}
}
// plot trace and other indications as raster
draw_all(completed); // flush markmap only if scan completed to prevent
// remaining traces
// STOP_PROFILE
}
}
#pragma GCC push_options
#pragma GCC optimize ("Os")
int
is_paused(void)
{
return !(sweep_mode & SWEEP_ENABLE);
}
static inline void
pause_sweep(void)
{
sweep_mode &= ~SWEEP_ENABLE;
}
static inline void
resume_sweep(void)
{
sweep_mode |= SWEEP_ENABLE;
}
void
toggle_sweep(void)
{
sweep_mode ^= SWEEP_ENABLE;
}
#ifdef __VNA__
static float
bessel0(float x)
{
const float eps = 0.0001;
float ret = 0;
float term = 1;
float m = 0;
while (term > eps * ret) {
ret += term;
++m;
term *= (x*x) / (4*m*m);
}
return ret;
}
static float
kaiser_window(float k, float n, float beta)
{
if (beta == 0.0) return 1.0;
float r = (2 * k) / (n - 1) - 1;
return bessel0(beta * sqrt(1 - r * r)) / bessel0(beta);
}
static void
transform_domain(void)
{
// use spi_buffer as temporary buffer
// and calculate ifft for time domain
float* tmp = (float*)spi_buffer;
uint8_t window_size = POINTS_COUNT, offset = 0;
uint8_t is_lowpass = FALSE;
switch (domain_mode & TD_FUNC) {
case TD_FUNC_BANDPASS:
offset = 0;
window_size = POINTS_COUNT;
break;
case TD_FUNC_LOWPASS_IMPULSE:
case TD_FUNC_LOWPASS_STEP:
is_lowpass = TRUE;
offset = POINTS_COUNT;
window_size = POINTS_COUNT * 2;
break;
}
float beta = 0.0;
switch (domain_mode & TD_WINDOW) {
case TD_WINDOW_MINIMUM:
beta = 0.0; // this is rectangular
break;
case TD_WINDOW_NORMAL:
beta = 6.0;
break;
case TD_WINDOW_MAXIMUM:
beta = 13;
break;
}
for (int ch = 0; ch < 2; ch++) {
memcpy(tmp, measured[ch], sizeof(measured[0]));
for (int i = 0; i < POINTS_COUNT; i++) {
float w = kaiser_window(i + offset, window_size, beta);
tmp[i * 2 + 0] *= w;
tmp[i * 2 + 1] *= w;
}
for (int i = POINTS_COUNT; i < FFT_SIZE; i++) {
tmp[i * 2 + 0] = 0.0;
tmp[i * 2 + 1] = 0.0;
}
if (is_lowpass) {
for (int i = 1; i < POINTS_COUNT; i++) {
tmp[(FFT_SIZE - i) * 2 + 0] = tmp[i * 2 + 0];
tmp[(FFT_SIZE - i) * 2 + 1] = -tmp[i * 2 + 1];
}
}
fft256_inverse((float(*)[2])tmp);
memcpy(measured[ch], tmp, sizeof(measured[0]));
for (int i = 0; i < POINTS_COUNT; i++) {
measured[ch][i][0] /= (float)FFT_SIZE;
if (is_lowpass) {
measured[ch][i][1] = 0.0;
} else {
measured[ch][i][1] /= (float)FFT_SIZE;
}
}
if ((domain_mode & TD_FUNC) == TD_FUNC_LOWPASS_STEP) {
for (int i = 1; i < POINTS_COUNT; i++) {
measured[ch][i][0] += measured[ch][i - 1][0];
}
}
}
}
#endif
// Shell commands output
int shell_printf(const char *fmt, ...)
{
va_list ap;
int formatted_bytes;
va_start(ap, fmt);
formatted_bytes = chvprintf(shell_stream, fmt, ap);
va_end(ap);
return formatted_bytes;
}
#ifdef __USE_SERIAL_CONSOLE__
// Serial Shell commands output
int shell_serial_printf(const char *fmt, ...)
{
va_list ap;
int formatted_bytes;
va_start(ap, fmt);
formatted_bytes = chvprintf(&SD1, fmt, ap);
va_end(ap);
return formatted_bytes;
}
#endif
VNA_SHELL_FUNCTION(cmd_pause)
{
(void)argc;
(void)argv;
pause_sweep();
draw_cal_status();
}
VNA_SHELL_FUNCTION(cmd_resume)
{
(void)argc;
(void)argv;
// restore frequencies array and cal
update_frequencies();
#ifdef __VNA__
if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY))
cal_interpolate(lastsaveid);
#endif
resume_sweep();
}
VNA_SHELL_FUNCTION(cmd_reset)
{
(void)argc;
(void)argv;
if (argc == 1) {
if (strcmp(argv[0], "dfu") == 0) {
shell_printf("Performing reset to DFU mode\r\n");
enter_dfu();
return;
}
}
shell_printf("Performing reset\r\n");
rccEnableWWDG(FALSE);
WWDG->CFR = 0x60;
WWDG->CR = 0xff;
/* wait forever */
while (1)
;
}
#ifdef __VNA__
const int8_t gain_table[] = {
0, // 0 ~ 300MHz
40, // 300 ~ 600MHz
50, // 600 ~ 900MHz
75, // 900 ~ 1200MHz
85, // 1200 ~ 1500MHz
95, // 1500MHz ~
95, // 1800MHz ~
95, // 2100MHz ~
95 // 2400MHz ~
};
#define DELAY_GAIN_CHANGE 2
static int
adjust_gain(uint32_t newfreq)
{
int new_order = newfreq / FREQ_HARMONICS;
int old_order = si5351_get_frequency() / FREQ_HARMONICS;
if (new_order != old_order) {
tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]);
return DELAY_GAIN_CHANGE;
}
return 0;
}
#endif
int set_frequency(freq_t freq)
{
(void) freq;
#ifdef __VNA__
int delay = adjust_gain(freq);
int8_t ds = drive_strength;
if (ds == DRIVE_STRENGTH_AUTO) {
ds = freq > FREQ_HARMONICS ? SI5351_CLK_DRIVE_STRENGTH_8MA : SI5351_CLK_DRIVE_STRENGTH_2MA;
}
delay += si5351_set_frequency(freq, ds);
return delay;
#endif
return 1;
}
// Use macro, std isdigit more big
#define _isdigit(c) (c >= '0' && c <= '9')
// Rewrite universal standart str to value functions to more compact
//
// Convert string to int32
static int32_t my_atoi(const char *p)
{
int32_t value = 0;
uint32_t c;
bool neg = false;
if (*p == '-') {neg = true; p++;}
if (*p == '+') p++;
while ((c = *p++ - '0') < 10)
value = value * 10 + c;
switch (*(--p)) {
case 'k': value *= 1000; break;
case 'M': value *= 1000000; break;
case 'G': value *= 1000000000; break;
}
return neg ? -value : value;
}
// Convert string to uint32
// 0x - for hex radix
// 0o - for oct radix
// 0b - for bin radix
// default dec radix
uint32_t my_atoui(const char *p)
{
uint32_t value = 0, radix = 10, c;
if (*p == '+') p++;
if (*p == '0') {
switch (p[1]) {
case 'x': radix = 16; break;
case 'o': radix = 8; break;
case 'b': radix = 2; break;
default: goto calculate;
}
p+=2;
}
calculate:
while (1) {
c = *p++ - '0';
// c = to_upper(*p) - 'A' + 10
if (c >= 'A' - '0') c = (c&(~0x20)) - ('A' - '0') + 10;
if (c >= radix) break;
value = value * radix + c;
}
switch (*(--p)) {
case 'k': value *= 1000; break;
case 'M': value *= 1000000; break;
case 'G': value *= 1000000000; break;
}
return value;
}
float
my_atof(const char *p)
{
int neg = FALSE;
if (*p == '-')
neg = TRUE;
if (*p == '-' || *p == '+')
p++;
float x = my_atoi(p);
while (_isdigit((int)*p))
p++;
if (*p == '.') {
float d = 1.0f;
p++;
while (_isdigit((int)*p)) {
d /= 10;
x += d * (*p - '0');
p++;
}
}
if (*p == 'e' || *p == 'E') {
p++;
int exp = my_atoi(p);
while (exp > 0) {
x *= 10;
exp--;
}
while (exp < 0) {
x /= 10;
exp++;
}
}
switch (*p) {
case 'k': x *= 1e+3; break;
case 'M': x *= 1e+6; break;
case 'G': x *= 1e+9; break;
case 'm': x /= 1e+3; break;
case 'u': x /= 1e+6; break;
case 'n': x /= 1e+9; break;
case 'p': x /= 1e+12; break;
}
if (neg)
x = -x;
return x;
}
//
// Function used for search substring v in list
// Example need search parameter "center" in "start|stop|center|span|cw" getStringIndex return 2
// If not found return -1
// Used for easy parse command arguments
static int get_str_index(char *v, const char *list)
{
int i = 0;
while (1) {
char *p = v;
while (1) {
char c = *list;
if (c == '|') c = 0;
if (c == *p++) {
// Found, return index
if (c == 0) return i;
list++; // Compare next symbol
continue;
}
break; // Not equal, break
}
// Set new substring ptr
while (1) {
// End of string, not found
if (*list == 0) return -1;
if (*list++ == '|') break;
}
i++;
}
return -1;
}
#ifdef __VNA__
VNA_SHELL_FUNCTION(cmd_offset)
{
if (argc != 1) {
shell_printf("usage: offset {frequency offset(Hz)}\r\n");
return;
}
si5351_set_frequency_offset(my_atoi(argv[0]));
}
#endif
VNA_SHELL_FUNCTION(cmd_freq)
{
if (argc != 1) {
goto usage;
}
freq_t freq = my_atoui(argv[0]);
pause_sweep();
set_frequency(freq);
return;
usage:
shell_printf("usage: freq {frequency(Hz)}\r\n");
}
#ifdef __VNA__
VNA_SHELL_FUNCTION(cmd_power)
{
if (argc != 1) {
shell_printf("usage: power {0-3|-1}\r\n");
return;
}
(void)argv;
drive_strength = my_atoi(argv[0]);
// set_frequency(frequency);
}
#endif
#ifdef ENABLE_TIME_COMMAND
#if HAL_USE_RTC == FALSE
#error "Error cmd_time require define HAL_USE_RTC = TRUE in halconf.h"
#endif
VNA_SHELL_FUNCTION(cmd_time)
{
RTCDateTime timespec;
(void)argc;
(void)argv;
rtcGetTime(&RTCD1, ×pec);
shell_printf("%d/%d/%d %d\r\n", timespec.year+1980, timespec.month, timespec.day, timespec.millisecond);
}
#endif
VNA_SHELL_FUNCTION(cmd_dac)
{
int value;
if (argc != 1) {
shell_printf("usage: dac {value(0-4095)}\r\n"\
"current value: %d\r\n", config.dac_value);
return;
}
value = my_atoui(argv[0]);
config.dac_value = value;
dacPutChannelX(&DACD2, 0, value);
}
#ifdef __VNA__
VNA_SHELL_FUNCTION(cmd_threshold)
{
uint32_t value;
if (argc != 1) {
shell_printf("usage: threshold {frequency in harmonic mode}\r\n"\
"current: %d\r\n", config.harmonic_freq_threshold);
return;
}
value = my_atoui(argv[0]);
config.harmonic_freq_threshold = value;
}
#endif
VNA_SHELL_FUNCTION(cmd_saveconfig)
{
(void)argc;
(void)argv;
config_save();
shell_printf("Config saved.\r\n");
}
VNA_SHELL_FUNCTION(cmd_clearconfig)
{
if (argc != 1) {
shell_printf("usage: clearconfig {protection key}\r\n");
return;
}
if (strcmp(argv[0], "1234") != 0) {
shell_printf("Key unmatched.\r\n");
return;
}
clear_all_config_prop_data();
shell_printf("Config and all cal data cleared.\r\n"\
"Do reset manually to take effect. Then do touch cal and save.\r\n");
}
#ifdef __AUDIO__
static struct {
int16_t rms[2];
int16_t ave[2];
int callback_count;
#if 1
int32_t last_counter_value;
int32_t interval_cycles;
int32_t busy_cycles;
#endif
} stat;
int16_t rx_buffer[AUDIO_BUFFER_LEN * 2];
#ifdef ENABLED_DUMP
int16_t dump_buffer[AUDIO_BUFFER_LEN];
int16_t dump_selection = 0;
#endif
volatile uint8_t wait_count = 0;
volatile uint8_t accumerate_count = 0;
#endif
#ifdef __VNA__
const int8_t bandwidth_accumerate_count[] = {
1, // 1kHz
3, // 300Hz
10, // 100Hz
33, // 30Hz
100 // 10Hz
};
float measured[2][POINTS_COUNT][2];
#endif
measurement_t measured;
#ifdef __AUDIO__
#ifdef ENABLED_DUMP
static void
duplicate_buffer_to_dump(int16_t *p)
{
if (dump_selection == 1)
p = samp_buf;
else if (dump_selection == 2)
p = ref_buf;
memcpy(dump_buffer, p, sizeof dump_buffer);
}
#endif
#ifdef __AUDIO__
void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n)
{
#if PORT_SUPPORTS_RT
int32_t cnt_s = port_rt_get_counter_value();
int32_t cnt_e;
#endif
int16_t *p = &rx_buffer[offset];
(void)i2sp;
(void)n;
if (wait_count > 1) {
--wait_count;
} else if (wait_count > 0) {
if (accumerate_count > 0) {
dsp_process(p, n);
accumerate_count--;
}
#ifdef ENABLED_DUMP
duplicate_buffer_to_dump(p);
#endif
}
#if PORT_SUPPORTS_RT
cnt_e = port_rt_get_counter_value();
stat.interval_cycles = cnt_s - stat.last_counter_value;
stat.busy_cycles = cnt_e - cnt_s;
stat.last_counter_value = cnt_s;
#endif
stat.callback_count++;
}
static const I2SConfig i2sconfig = {
NULL, // TX Buffer
rx_buffer, // RX Buffer
AUDIO_BUFFER_LEN * 2,
NULL, // tx callback
i2s_end_callback, // rx callback
0, // i2scfgr
2 // i2spr
};
#endif
#endif
#define MAX_DATA 2
VNA_SHELL_FUNCTION(cmd_data)
{
int i;
int sel = 0;
if (argc == 1)
sel = my_atoi(argv[0]);
if (sel >= 0 && sel <= MAX_DATA) {
for (i = 0; i < sweep_points; i++)
shell_printf("%f\r\n", value(measured[sel][i]));
return;
}
shell_printf("usage: data [0-2]\r\n");
}
#ifdef ENABLED_DUMP
VNA_SHELL_FUNCTION(cmd_dump)
{
int i, j;
int len;
if (argc == 1)
dump_selection = my_atoi(argv[0]);
wait_dsp(3);
len = AUDIO_BUFFER_LEN;
if (dump_selection == 1 || dump_selection == 2)
len /= 2;
for (i = 0; i < len; ) {
for (j = 0; j < 16; j++, i++) {
shell_printf("%04x ", 0xffff & (int)dump_buffer[i]);
}
shell_printf("\r\n");
}
}
#endif
#ifdef __REMOTE_DESKTOP__
VNA_SHELL_FUNCTION(cmd_refresh)
{
// read pixel count at one time (PART*2 bytes required for read buffer)
int m = generic_option_cmd("refresh", "off|on", argc, argv[0]);
if (m>=0) {
auto_capture = m;
}
}
volatile int mouse_x = 0;
volatile int mouse_y = 0;
volatile int mouse_down = false;
VNA_SHELL_FUNCTION(cmd_touch)
{
if (argc == 2){
mouse_x = (uint32_t)my_atoi(argv[0]);
mouse_y = (uint32_t)my_atoi(argv[1]);
mouse_down = true;
handle_touch_interrupt();
}
}
VNA_SHELL_FUNCTION(cmd_release)
{
if (argc==2) {
mouse_x = (uint32_t)my_atoi(argv[0]);
mouse_y = (uint32_t)my_atoi(argv[1]);
}
mouse_down = false;
handle_touch_interrupt();
}
#endif
VNA_SHELL_FUNCTION(cmd_capture)
{
// read pixel count at one time (PART*2 bytes required for read buffer)
(void)argc;
(void)argv;
int i, y;
#if SPI_BUFFER_SIZE < (3*LCD_WIDTH + 1)
#error "Low size of spi_buffer for cmd_capture"
#endif
// read 2 row pixel time (read buffer limit by 2/3 + 1 from spi_buffer size)
for (y = 0; y < LCD_HEIGHT; y += 2) {
// use uint16_t spi_buffer[2048] (defined in ili9341) for read buffer
uint8_t *buf = (uint8_t *)spi_buffer;
ili9341_read_memory(0, y, LCD_WIDTH, 2, 2 * LCD_WIDTH, spi_buffer);
for (i = 0; i < 4 * LCD_WIDTH; i++) {
streamPut(shell_stream, *buf++);
}
}
}
void send_region(const char *t, int x, int y, int w, int h)
{
shell_printf("%s\r\n", t);
streamPut(shell_stream, (((uint16_t) x) & 0xff));
streamPut(shell_stream, (((uint16_t)x>>8) & 0xff));
streamPut(shell_stream, (((uint16_t) y) & 0xff));
streamPut(shell_stream, (((uint16_t)y>>8) & 0xff));
streamPut(shell_stream, (((uint16_t) w) & 0xff));
streamPut(shell_stream, (((uint16_t)w>>8) & 0xff));
streamPut(shell_stream, (((uint16_t) h) & 0xff));
streamPut(shell_stream, (((uint16_t)h>>8) & 0xff));
}
void send_buffer(uint8_t * buf, int s)
{
for (int i = 0; i < s; i++) {
streamPut(shell_stream, *buf++);
}
shell_printf("ch> \r\n");
}
#if 0
VNA_SHELL_FUNCTION(cmd_gamma)
{
float gamma[2];
(void)argc;
(void)argv;
pause_sweep();
chMtxLock(&mutex);
wait_dsp(4);
calculate_gamma(gamma);
chMtxUnlock(&mutex);
shell_printf("%d %d\r\n", gamma[0], gamma[1]);
}
#endif
#ifdef __VNA__
static void (*sample_func)(float *gamma) = calculate_gamma;
VNA_SHELL_FUNCTION(cmd_sample)
{
if (argc != 1) goto usage;
// 0 1 2
static const char cmd_sample_list[] = "gamma|ampl|ref";
switch (get_str_index(argv[0], cmd_sample_list)) {
case 0:
sample_func = calculate_gamma;
return;
case 1:
sample_func = fetch_amplitude;
return;
case 2:
sample_func = fetch_amplitude_ref;
return;
default:
break;
}
usage:
shell_printf("usage: sample {%s}\r\n", cmd_sample_list);
}
#endif
config_t config = {
.magic = CONFIG_MAGIC,
.dac_value = 1922,
// .touch_cal = { 693, 605, 124, 171 }, // 2.4 inch LCD panel
#ifdef TINYSA3
.touch_cal = { 347, 495, 160, 205 }, // 2.8 inch LCD panel
#endif
#ifdef TINYSA4
.touch_cal = { 261, 605, 115, 146 }, // 4 inch panel
#endif
._mode = _MODE_USB,
._serial_speed = USART_SPEED_SETTING(SERIAL_DEFAULT_BITRATE),
#ifdef __VNA__
.harmonic_freq_threshold = 300000000,
#endif
.lcd_palette = LCD_DEFAULT_PALETTE,
.vbat_offset = 500,
#ifdef TINYSA4
.frequency_IF2 = 0,
#endif
.low_level_offset = 100, // Uncalibrated
.high_level_offset = 100, // Uncalibrated
#ifdef TINYSA3
.low_correction_frequency = { 10000, 100000, 200000, 500000, 30000000, 140000000, 200000000, 300000000, 330000000, 350000000 },
.low_correction_value = { +6.0, +2.8, +1.6, -0.4, 0.0, -0.4, +0.4, +3.0, +4.0, +8.1 },
.high_correction_frequency = { 240000000, 280000000, 300000000, 400000000, 500000000, 600000000, 700000000, 800000000, 900000000, 960000000 },
.high_correction_value = { 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0 },
#endif
#ifdef TINYSA4
.low_correction_frequency = { 10000, 100000, 200000, 500000, 30000000, 140000000, 200000000, 300000000, 330000000, 350000000 },
.low_correction_value = { 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0 },
.high_correction_frequency = { 10000, 100000, 200000, 500000, 50000000, 140000000, 200000000, 300000000, 330000000, 350000000 },
.high_correction_value = { 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0 },
#endif
.setting_frequency_10mhz = 10000000,
.cor_am = -14,
.cor_wfm = -17,
.cor_nfm = -17,
.sweep_voltage = 3.3,
};
//properties_t current_props;
//properties_t *active_props = ¤t_props;
// NanoVNA Default settings
static const trace_t def_trace[TRACES_MAX] = {//enable, type, channel, reserved, scale, refpos
{ 0, TRC_LOGMAG, 0, 0, 10.0, (float) NGRIDY+1 }, //Temp
{ 0, TRC_LOGMAG, 1, 0, 10.0, (float) NGRIDY+1 }, //Stored
{ 1, TRC_LOGMAG, 2, 0, 10.0, (float) NGRIDY+1 } //Actual
};
static const marker_t def_markers[MARKERS_MAX] = {
{ M_ENABLED, M_REFERENCE | M_TRACKING, 30, 0 },
{ M_DISABLED, M_NORMAL, 40, 0 },
{ M_DISABLED, M_NORMAL, 60, 0 },
{ M_DISABLED, M_NORMAL, 80, 0 }
};
// Load propeties default settings
void load_LCD_properties(void)
{
//Magic add on caldata_save
//current_props.magic = CONFIG_MAGIC;
// current_props._setting.frequency0 = 0; // start = 0Hz
// current_props._setting.frequency1 = 350000000; // end = 350MHz
// current_props._setting.frequency_IF= 433800000,
setting._sweep_points = POINTS_COUNT;
#ifdef VNA__
setting._cal_status = 0;
//This data not loaded by default
//setting._frequencies[POINTS_COUNT];