-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathTeensy_Convolution_SDR.ino
18660 lines (16845 loc) · 689 KB
/
Teensy_Convolution_SDR.ino
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
/*********************************************************************************************
(c) Frank DD4WH 2020_09_08
"TEENSY CONVOLUTION SDR"
SOFTWARE FOR A FAST CONVOLUTION-BASED RADIO
HARDWARE NEEDED:
- simple quadrature sampling detector board producing baseband IQ signals (Softrock, Elektor SDR etc.)
(IQ boards with up to 256kHz bandwidth supported --> which basically means nearly 100% of the existing boards on the market)
- Teensy audio board or ADC PCM1808 and DAC PCM5102a
- Teensy 3.6 or Teensy 4.0 or Teensy 4.1 (Teensy 3.1/3.2/3.5 not supported)
- has also been successfully used with Msi001 tuner chip (Esteban Bonito & tisho), but not yet incorporated in this sketch
HARDWARE OPTIONAL:
- Preselection: switchable RF lowpass or bandpass filter
- digital step attenuator: PE4306 used in my setup
- I2C adapter chip
SOFTWARE:
- FFT Fast Convolution = Digital Convolutionbuffer_spec_FFT
- with overlap - save = overlap-discard complex bandpass main filtering
- spectral NR uses FFT-iFFT overlap-add with 50% overlap
- in floating point 32bit
- with Teensy 3.6: compile with 180MHz F_CPU, other speeds not supported. Maybe with the newest fix in Teensyduino, higher speeds could work, but this is untested
- with Teensy 4.0: compile with "Optimize: Faster", never use "Optimize: smallest code", the latter will not work!
- with Teensy 4.1: tested in combination with SGTL5000 on D07JBH PCB board with ugly style adaptor board
- tested with Arduino 1.8.12 & Teensyduino 1.52
- the T4 versions of the code also work with toolchain ARM ver. 9
Part of the evolution of this project has been documented here:
https://forum.pjrc.com/threads/40188-Fast-Convolution-filtering-in-floating-point-with-Teensy-3-6/page2
https://forum.pjrc.com/threads/40590-Teensy-Convolution-SDR-(Software-Defined-Radio)/page10
HISTORY OF IMPLEMENTED FEATURES
- 12kHz to 30MHz Receive PLUS 76 - 108MHz: undersampling-by-3 with slightly reduced sensitivity (-9dB)
- I & Q - correction in software (manual correction or automatic correction)
- efficient frequency translation without multiplication
- efficient spectrum display using a 256 point FFT on the first 256 samples of every 4096 sample-cycle
- efficient AM demodulation with ARM functions
- efficient DC elimination after AM demodulation
- implemented nine different AM demodulation algorithms for comparison (only two could stand the test and one algorithm was finally left in the implementation)
- real SAM - synchronous AM demodulation with phase determination by atan2f implemented from the wdsp lib
- Stereo-SAM and sideband-selected SAM
- sample rate from 48k to 234k and decimation-by-8 for efficient realtime calculations
- spectrum Zoom function 1x, 2x, 4x, 512x, 1024x, 2048x, 4096x --> 4096x zoom with sub-Hz resolution
- Automatic gain control (high end algorithm by Warren Pratt, wdsp)
- plays MP3 and M4A (iTunes files) from SD card with the awesome lib by Frank Bösing (his old MP3 lib, not the new one)
- automatic IQ amplitude and phase imbalance correction
- dynamic frequency indicator figures and graticules on spectrum display x-axis
- kind of menu system now working with many variables that can be set by the encoders
- EEPROM save & load of important settings
- wideband FM demodulation with deemphasis
- automatic codec gain adjustment depending on the sample input level
- spectrum display AGC to allow display of very small signals
- spectrum display in WFM activated (alpha version . . .)
- optimized automatic test whether mirror rejection is working - if not, codec is restarted automatically until we have working mirror rejection
- display mirror rejection check ("IQtest" in red box)
- activated integrated codec 5-band graphic equalizer
- added digital attenuator PE4306 bit-banging SPI control [0 -31dB attenuation possible]
- added backlight control for TFT in the menu
- added analog gain display (analog codec gain AND attenuation displayed)
- fixed major bug associated with too small "string" variables for printing, leading to annoying audio clicks
- STEREO FM reception implemented and disabled spectrum display in WFM STEREO mode, because the digital noise of the refresh of the spectrum display does seriously distort audio
- manual notch filter implemented [in the frequency domain: simply deletes bins before the iFFT]
- bandwidth adjustment of manual notch filter implemented
- graphical display of manual notch filters in the frequency domain
- Michaels excellent noise blanker is working! Eliminates noise impulses very nicely and effectively!
- leaky LMS algorithm from the wdsp lib implemented (but not working as expected . . .)
- switched to complex filter coefficients for general filter in the fast convolution process
- freely adjustable bandpasses & passband tuning in AM/SAM/SSB . . .
- rebuilt convolution with more flexible choice of FFT size --> now default FFT size is 512, because of memory constraints when using 1024 . . .
- decimation and interpolation filters are calculated with new algorithm and are calculated on-the-fly when changing filter characteristics --> much less hiss and ringing of the filters
- Blackman-Harris four-term window for main FIR filter (as in PowerSDR)
- first test of a 110kHz lowpass filter in the WFM path for FM (stereo) reception on VHF --> does work properly but causes strange effects (button swaps) because of memory constraints when assigning the FIR instances
- changed default to 512tap FFT in order to have enough memory for MP3 playing and other things
- updated Arduino to version 1.8.5 and Teensyduino to version 1.40 and had to change some of the code
- implemented spectral noise reduction in the frequency domain by implementing another FFT-iFFT-overlap-add chain on the real audio output after the main filter
- spectral weighting algorithm Kim et al. 2002 implemented[working!]
- spectral weighting algorithm Romanin et al. 2009 / Schmitt et al. 2002 implemented (minimum statistics)[obsolete]
- spectral weighting algorithm Romanin et al. 2009 implemented (voice activity detector)[working, without VAD now]
- fixed bug in alias filter switching when changing bandpass filter coefficients
- adjustment in finer filter frequency steps when below 500Hz (switch to 50Hz steps instead of 100Hz)
- fixed several bugs in band switching and mode switching
- final tweak of spectral NR algorithms finished (many parameters eliminated from menu)
- for comparison added LMS and leaky LMS to NR menu choice (four NR algorithms to choose from: Kim, Romanin, leaky LMS, LMS)
- changed spectral NR Romanin to newest version by Michael DL2FW [the final UHSDR version, 22.2.2018]
- analog clock design
- spectrum display FFT windowing bug fixed (thanks, Bob Larkin!)
- ZoomFFT repaired and now fully functional for all magnifications (up to 2048x), additional IIR filters added, also added higher refresh rate!
- incorporated many good ideas by Bob Larkin, thanks!
- experimental new sample rates up to 353ksps . . . https://forum.pjrc.com/threads/42336-Reset-audio-board-codec-SGTL5000-in-realtime-processing/page3?highlight=sample+rate
- add possibility to use PCB hardware by DO7JBH https://github.com/do7jbh/SSR-2
- bugfix array out-of-bound, thanks bicycleguy for pointing me to this bug!
- atan2f approximation: https://www.mikrocontroller.net/topic/atan2-funktion-mit-lookup-table-fuer-arm --> thanks Frank B for the hint !
- bugfix band vs. bands --> cleanup and changed int band to int current_band
- integrated automatic crc check on eePROM load and save (by Mike / bicycleguy, thanks!) - no more need to uncomment/comment during first time use of the software
- added support for Bob Larkins RF Octave frontend filters http://www.janbob.com/electron/FilterBP1/FiltBP1.html
- bugfix: only use local loop variables
- bugfix: software now usable on different hardware versions: DO7JBH, DD4WH
- CW decoder (modified version of Lofturs excellent implementation) taken from UHSDR
- RTTY decoder: taken from UHSDR
- alternative RTTY decoder (Martin Ossmann)
- ERF time signal decoder (Martin Ossmann) with automatic adjustment of the real time clock
- now runs on Teensy 4.0
- bugfix runover audio buffers
- EEPROM runs fine on T4
- flexible T4 CPU frequency setting in menu, < 1 Watt power consumption is thus possible in every mode ! :-) [3.2" TFT + ADC + DAC + Teensy 4.1 + QSD hardware < 1 Watt !]
- T4: CPU temperature display
- T4: Hifi Stereo with PLL
- fixed RTC for T4
- T4 filter steepness doubled: now uses 1024-point-FFT, T3.6 uses 512-point-FFT
- T4: experimental: 2048-point-FFT --> filter after decimation equivalent to 16384 taps, only possible with modification of record_queue.h and record_queue.cpp --> substitute 53 with 83 blocks
- T4: tweak PLL clocks/switch off ADCs etc. to lower EMI in T4 (thanks FrankB !)
- float/double optimizations (FrankB)
- bugfix PLL for WFM Stereo (thanks, FrankB for pointing me to that!)
- automatic STEREO detection in WFM
- new debouncing of encoder (new lib by FrankB)
- audio volume encoder logarithmic feel (thanks to FrankB)
- bugfix Auto-IQ correction Moseley & Slump (2006) (thanks to FrankB)
- introduce ENCODER_FACTOR in order to be flexible with encoder library (DD4WH T4 setup does only work with the standard encoder lib and hardware debouncing with 4n7 caps at the encoder contacts)
- change ILI9341 screen update --> credit to FrankB [frees up CPU load considerably !]
- added hardware support for DO7JBH hardware with T3.6-to-T4.1. adapter
- added more convinient tuning steps, thanks tisho!
- menu assistant by tisho makes menu buttons obsolete and makes menu navigation MUCH easier ! Thanks tisho!
- Zoom FFT now correctly implemented for every zoom step up to 2048x --> now exclusively uses CMSIS decimation function and no more IIR filters, (formerly magnifications > 256x were spoiled, sample rate was fixed and not correctly taken into account for the lowpass filters)
- bugfix !? NoAudioInterrupts() - AudioInterrupts() should not be used with T4.x --> causes problems because of the spread spectrum adjustments !???
- experimental implementation of 9-band audio equalizer [Bob Larkins design]: variables line 1928, setup: line 3655
TODO:
- implement Bob Larkins´ FIR equalizer (https://forum.pjrc.com/threads/60928-Audio-Equalizer-using-FIR?highlight=equalizer) for every hardware and eliminate the SGTL5000 hardware-based equalizer (the latter does not work as it should, at least for the 5-band version)
- RDS decoding in wide FM reception mode ;-): very hard, but could be barely possible
- account for using the Si5351 with two clock outputs in 90 degrees difference
- fix bug in Zoom_FFT --> lowpass IIR filters run with different sample rates, but are calculated for a fixed sample rate of 48ksps
- implement separate interrupt to cope with UI (encoders, buttons, calculation of filter coefficients) in order to free audio interrupt
- SSB autotune algorithm taken from Robert Dick
- BPSK decoder
- UKW DX filters for WFM prior to FM demodulation (110kHz, 80kHz, 57kHz)
- test dBm measurement according to filter passband
- finetune AGC parameters and make AGC HANG TIME, AGC HANG THRESHOLD and AGC HANG DECAY user-adjustable
- record and playback IQ audio stream ;-)
- read stations´ frequencies from SD card and display station names when tuned to a frequency
- implement Motorola C-QUAM AM Stereo demodulation
- CW peak filter (independently adjustable from notch filter)
some parts of the code modified from and/or inspired by the following open sources:
Teensy SDR (rheslip & DD4WH): https://github.com/DD4WH/Teensy-SDR-Rx [GNU GPL]
UHSDR (M0NKA, KA7OEI, DF8OE, DB4PLE, DL2FW, DD4WH & other contributors): https://github.com/df8oe/UHSDR [GNU GPL]
libcsdr (András Retzler): https://github.com/simonyiszk/csdr [BSD / GPL]
wdsp (Warren Pratt): http://svn.tapr.org/repos_sdr_hpsdr/trunk/W5WC/PowerSDR_HPSDR_mRX_PS/Source/wdsp/ [GNU GPL]
Wheatley (2011): cuteSDR https://github.com/satrian/cutesdr-se [BSD]
Robert Dick (1999): Tune SSB Automatically. in QEX: http://www.arrl.org/files/file/QEX%20Binaries/1999/ssbtune.zip ["code is in the public domain . . .", thus I assume GNU GPL]
Martin Ossmann (2019): unpublished source code for decoders for RTTY and ERF time signals, thank you, Martin, for the permission to include your code here!
A great thank you and lots of credit go to Frank Bösing: from sample-rate-change-on-the-fly code to many many code snippets !
GREAT THANKS FOR ALL THE HELP AND INPUT BY WALTER, WMXZ !
Audio queue optimized by Pete El Supremo 2016_10_27, thanks Pete!
An important hint on the implementation came from Alberto I2PHD, thanks for that!
Thanks to Brian, bmillier for helping with codec restart code for the SGTL 5000 codec in the Teensy audio board!
Thanks a lot to Michael DL2FW - without you the spectral noise reduction would not have been possible! Also you contributed the state-of-the-art Noise Blanker
Bob Larkin, W7PUA, found a significant bug in the spectrum display FFT windowing and added lots of other very useful things, thanks a lot, Bob!
and of course a great Thank You to Paul Stoffregen @ pjrc.com for providing the Teensy platform and its excellent audio library !
Audio processing in float32_t with the NEW ARM CMSIS lib, --> https://forum.pjrc.com/threads/40590-Teensy-Convolution-SDR-(Software-Defined-Radio)?p=129081&viewfull=1#post129081
*********************************************************************************
**
** Project.........: Read Hand Sent Morse Code (tolerant of considerable jitter)
**
** Copyright (c) 2016 Loftur E. Jonasson (tf3lj [at] arrl [dot] net)
**
** https://sites.google.com/site/lofturj/cwreceive#TOC-Take-two-Fast-Fourier-Transform-and-Colour-Graphics
** Substantive portions of the methodology used here to decode Morse Code are found in:
**
** "MACHINE RECOGNITION OF HAND-SENT MORSE CODE USING THE PDP-12 COMPUTER"
** by Joel Arthur Guenther, Air Force Institute of Technology,
** Wright-Patterson Air Force Base, Ohio
** December 1973
** http://www.dtic.mil/dtic/tr/fulltext/u2/786492.pdf
**
** Platform........: Teensy 3.1 / 3.2 and the Teensy Audio Shield
**
** Initial version.: 0.00, 2016-01-25 Loftur Jonasson, TF3LJ / VE2LJX
**
*********************************************************************************
GNU GPL LICENSE v3
This program 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 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>
************************************************************************************************************************************/
/* If you use the hardware made by Frank DD4WH uncomment the next line */
//#define HARDWARE_DD4WH
/* If you use the hardware made by Frank DD4WH & the T4 uncomment the next line */
//#define HARDWARE_DD4WH_T4
/* If you use the hardware made by Frank DD4WH & the T4 uncomment the next line */
//#define HARDWARE_AD8331
/* If you use the hardware made by FrankB uncomment the next line */
//#define HARDWARE_FRANKB
//#define HARDWARE_FRANKB2
/* If you use the hardware made by Dante DO7JBH [https://github.com/do7jbh/SSR-2], uncomment the next line */
//#define HARDWARE_DO7JBH
/* If you use the hardware made by Dante DO7JBH with a Teensy 4.1 adapter [https://github.com/do7jbh/SSR-2], uncomment the next line */
#define HARDWARE_DO7JBH_T41
/* only for debugging */
//#define DEBUG
/* this prints out the ADC and DAC levels when NOT in SAM mode, primarily for debugging hardware
recommendation: leave this commented */
//#define USE_ADC_DAC_display
/* only for support of the hardware RF frontend filters designed by Bob Larkin, W7PUA
http://www.janbob.com/electron/FilterBP1/FiltBP1.html
adjust cutoff frequencies according to your needs in function setfreq */
#define USE_BOBS_FILTER
/* flag to indicate to use the changes introduced by Bob Larkin, W7PUA
recommendation: leave this uncommented */
#define USE_W7PUA
/* use faster log calculations
recommendation: leave this uncommented */
#define USE_LOG10FAST
/* use faster atan2f calculation
recommendation: leave this uncommented */
#define USE_ATAN2FAST
#define MP3
#if defined(__IMXRT1062__)
#define T4
#endif
// this allows simultaneous calculation of sin and cos to save processor time for SAM demodulation
extern "C"
{
void sincosf(float err, float *s, float *c);
void sincos(double err, double *s, double *c);
}
#if (defined(T4))
extern "C"
uint32_t set_arm_clock(uint32_t frequency);
// lowering this from 600MHz to 200MHz makes power consumption @5 Volts about 40mA less -> 200mWatts less
// should we make this available in the menu to adjust during runtime? --> DONE
//uint32_t T4_CPU_FREQUENCY = 444000000;
uint32_t T4_CPU_FREQUENCY = 512000000;
#endif
#include <Audio.h>
//#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Metro.h>
#include <Bounce.h>
#include <arm_math.h>
#include <arm_const_structs.h>
#include <si5351.h>
//#include <Encoder.h> // try empirically which lib works best for your encoders !
#include <EncoderBounce.h> // https://github.com/FrankBoesing/EncoderBounce, does not work with my cheap chinese encoders ... but works perfectly with Alps encoders (which cost 10 times more) DD4WH
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(T4)
#include <ILI9341_t3n.h>
#include <ili9341_t3n_font_Arial.h>
#else
#include <ILI9341_t3.h>
#include "font_Arial.h"
#endif
#if defined(MP3)
#include <play_sd_mp3.h> //mp3 decoder by Frank B
#include <play_sd_aac.h> // AAC decoder by Frank B
#define SDCARD_CS_PIN BUILTIN_SDCARD
#if defined(HARDWARE_DD4WH_T4)
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
#else
#endif
#endif
#include <util/crc16.h> //mdrhere
#if defined(T4)
#include <utility/imxrt_hw.h> // for setting I2S freq, Thanks, FrankB!
#include <EEPROM.h>
#define WFM_SAMPLE_RATE 256000.0f
//#define WFM_SAMPLE_RATE 234375.0f
#else
#include <EEPROM.h>
#define F_I2S ((((I2S0_MCR >> 24) & 0x03) == 3) ? F_PLL : F_CPU)
#define WFM_SAMPLE_RATE 234375.0f
#endif
// temperature stuff
#if defined(T4)
extern "C" uint8_t external_psram_size __attribute__((weak));
#define TEMPMON_ROOMTEMP 25.0f
static uint32_t s_hotTemp; /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at room temperature .*/
static uint32_t s_hotCount; /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at the hot temperature.*/
static uint32_t roomCount; /*!< The value of TEMPMON_TEMPSENSE0[TEMP_VALUE] at the hot temperature.*/
static float s_hotT_ROOM; /*!< The value of s_hotTemp minus room temperature(25¡æ).*/
static uint32_t s_roomC_hotC; /*!< The value of s_roomCount minus s_hotCount.*/
#define TMS0_POWER_DOWN_MASK (0x1U)
#define TMS0_POWER_DOWN_SHIFT (0U)
#define TMS1_MEASURE_FREQ(x) (((uint32_t)(((uint32_t)(x)) << 0U)) & 0xFFFFU)
#define TMS0_ALARM_VALUE(x) (((uint32_t)(((uint32_t)(x)) << 20U)) & 0xFFF00000U)
#define TMS02_LOW_ALARM_VALUE(x) (((uint32_t)(((uint32_t)(x)) << 0U)) & 0xFFFU)
#define TMS02_PANIC_ALARM_VALUE(x) (((uint32_t)(((uint32_t)(x)) << 16U)) & 0xFFF0000U)
uint16_t temp_check_frequency; /*!< The temperature measure frequency.*/
uint32_t highAlarmTemp; /*!< The high alarm temperature.*/
uint32_t panicAlarmTemp; /*!< The panic alarm temperature.*/
uint32_t lowAlarmTemp; /*!< The low alarm */
float CPU_temperature = 0.0;
#endif
// CW DECODER STUFF
#define CW_DECODER_BLOCKSIZE_MIN 8
#define CW_DECODER_BLOCKSIZE_MAX 256
#define CW_DECODER_BLOCKSIZE_DEFAULT 128 //88
//#define CW_DECODER_THRESH_MIN 1000
//#define CW_DECODER_THRESH_MAX 50000
//#define CW_DECODER_THRESH_DEFAULT 32000
//#define SIGNAL_TAU 0.01
#define SIGNAL_TAU 0.1
#define ONEM_SIGNAL_TAU (1.0 - SIGNAL_TAU)
#define CW_TIMEOUT 3 // Time, in seconds, to trigger display of last Character received
#define ONE_SECOND (12000 / cw_decoder_config.blocksize) // sample rate / decimation rate / block size
#define CW_SPIKECANCEL_MAX_DURATION 8 // Cancel transients/spikes/drops that have max duration of number chosen.
// Typically 4 or 8 to select at time periods of 4 or 8 times 2.9ms.
// 0 to deselect.
#define CW_SIG_BUFSIZE 256 // Size of a circular buffer of decoded input levels and durations
#define CW_DATA_BUFSIZE 40 // Size of a buffer of accumulated dot/dash information. Max is DATA_BUFSIZE-2
// Needs to be significantly longer than longest symbol 'sos'= ~30.
#define DIGIMODE_OFF 0
#define CW 1
#define RTTY 2
#define EFR 3
#define RTTY_OSSI 4
#define DCF77 5
#define PSK 6
#define DIGIMODE_LAST 5
uint8_t digimode = 0;
float lastII = 0;
float lastQQ = 0;
float RXbit = 0;
float bitSampleTimer=0;
float Tsample=1.0 / 12000.0;
float CP_buffer[256];
float CP_buffer_old = 0.0;
// for EFR
//float bitSamplePeriod=1.0/1000.0 ;
// for RTTY
float bitSamplePeriod=1.0/500.0;
// for DCF77
float dcfRefLevel;
#define withterm 1
// print stuff for text terminal
#define termChrXwidth 9
//#define termChrYwidth 9
#define termChrYwidth 10
//#define termNrows 20
//#define termNrows 16
#define termNrows 4 // 15
#define termNcols 28 // 34
#define CW_x_start spectrum_x + 2 // 1
#define CW_y_start spectrum_y - 1 // 55
//#define font Arial_6
int termCursorXpos = 0;
int termCursorYpos = 0;
uint16_t termColor = 0x10000;
char termCharStore[termNcols][termNrows] ;
int16_t termCharColorStore[termNcols][termNrows] ;
#define RTTYuartFullTime 10
#define RTTYuartHalfTime 6
#define LFcode 10
#define CRcode 13
#define UU 'y'
uint8_t Menu_1_Assistant = 0;
uint8_t Menu_2_Assistant = 0; //Flag for the menu assistant function (by long pressing the encode it is cold the menu with small description)
uint8_t Menu_1_Enc_Sub = 0; //Flag used by the Menu aasistant to show when the sub menu is selected
uint8_t Menu_2_Enc_Sub = 0; //Flag used by the Menu aasistant to show when the sub menu is selected
typedef struct
{
float32_t sampling_freq;
float32_t target_freq;
uint8_t speed;
float32_t thresh;
uint8_t blocksize;
// uint8_t AGC_enable;
uint8_t noisecancel_enable;
uint8_t spikecancel;
#define CW_SPIKECANCEL_MODE_OFF 0
#define CW_SPIKECANCEL_MODE_SPIKE 1
#define CW_SPIKECANCEL_MODE_SHORT 2
bool atc_enable;
bool snap_enable;
bool show_CW_LED; // menu choice whether the user wants the CW LED indicator to be working or not
} cw_config_t;
typedef struct
{
int a;
float32_t b;
float32_t sin;
float32_t cos;
float32_t r;
float32_t buf[3];
} Goertzel;
Goertzel cw_goertzel;
cw_config_t cw_decoder_config =
{ .sampling_freq = 12000.0, .target_freq = 700, //700.0,
.speed = 25,
// .average = 2,
.thresh = 2.8, //32000,
.blocksize = CW_DECODER_BLOCKSIZE_DEFAULT,
// .AGC_enable = 0,
.noisecancel_enable = 1,
.spikecancel = 0,
.atc_enable = false,
.snap_enable = false,
.show_CW_LED = false // menu choice whether the user wants the CW LED indicator to be working or not
};
typedef enum {
RTTY_STOP_1 = 0,
RTTY_STOP_1_5,
RTTY_STOP_2,
RTTY_STOP_NUM
} rtty_stop_t;
typedef struct
{
float32_t speed;
rtty_stop_t stopbits;
uint16_t shift;
float32_t samplerate;
} rtty_mode_config_t;
typedef enum {
RTTY_SPEED_45,
RTTY_SPEED_50,
RTTY_SPEED_200,
RTTY_SPEED_NUM
} rtty_speed_t;
typedef enum {
RTTY_SHIFT_85,
RTTY_SHIFT_170,
RTTY_SHIFT_200,
RTTY_SHIFT_340,
RTTY_SHIFT_425,
RTTY_SHIFT_450,
RTTY_SHIFT_850,
RTTY_SHIFT_NUM
} rtty_shift_t;
typedef struct
{
rtty_speed_t id;
float32_t value;
char* label;
} rtty_speed_item_t;
// TODO: Probably we should define just a few for the various value types and let
// the id be an uint32_t
typedef struct
{
rtty_shift_t id;
uint32_t value;
char* label;
} rtty_shift_item_t;
typedef struct
{
rtty_shift_t shift_idx;
rtty_speed_t speed_idx;
rtty_stop_t stopbits_idx;
bool atc_disable; // should the automatic level control be turned off?
} rtty_ctrl_t;
rtty_ctrl_t rtty_ctrl_config =
{
.shift_idx = RTTY_SHIFT_450,
.speed_idx = RTTY_SPEED_50,
.stopbits_idx = RTTY_STOP_1_5,
.atc_disable = false
};
// bits 0-4 -> baudot, bit 5 1 == LETTER, 0 == NUMBER/FIGURE
const uint8_t Ascii2Baudot[128] =
{
0,
0,
0,
0,
0,
0,
0,
0b001011, // BEL N
0,
0,
0b000010, // \n NL
0,
0,
0b001000, // \r NL
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0b100100, // N
0, // !
0, // "
0, // #
0, // $
0, // %
0, // &
0b000101, // ' N
0b001111, // ( N
0b010010, // ) N
0, // *
0b010001, // + N
0b001100, // , N
0b000011, // - N
0b011100, // . N
0b011101, // / N
0b010110, // 0 N
0b010111, // 1 N
0b010011, // 2 N
0b000001, // 3 N
0b001010, // 4 N
0b010000, // 5 N
0b010101, // 6 N
0b000111, // 7 N
0b000110, // 8 N
0b011000, // 9 N
0b001110, // : N
0, // ;
0, // <
0b011110, // =
0, // >
0b011001, // ? N
0, // @
0b100011, // A L
0b111001, // B L
0b101110, // C L
0b101001, // D L
0b100001, // E L
0b101101, // F L
0b111010, // G L
0b110100, // H L
0b100110, // I L
0b101011, // J L
0b101111, // K L
0b110010, // L L
0b111100, // M L
0b101100, // N L
0b111000, // O L
0b110110, // P L
0b110111, // Q L
0b101010, // R L
0b100101, // S L
0b110000, // T L
0b100111, // U L
0b111110, // V L
0b110011, // W L
0b111101, // X L
0b110101, // Y L
0b110001, // Z L
0,
0,
0,
0,
0,
0,
0b100011, // A L
0b111001, // B L
0b101110, // C L
0b101001, // D L
0b100001, // E L
0b101101, // F L
0b111010, // G L
0b110100, // H L
0b100110, // I L
0b101011, // J L
0b101111, // K L
0b110010, // L L
0b111100, // M L
0b101100, // N L
0b111000, // O L
0b110110, // P L
0b110111, // Q L
0b101010, // R L
0b100101, // S L
0b110000, // T L
0b100111, // U L
0b111110, // V L
0b110011, // W L
0b111101, // X L
0b110101, // Y L
0b110001, // Z L
0,
0,
0,
0,
0,
};
#define RTTY_SYMBOL_CODE (0b11011)
#define RTTY_LETTER_CODE (0b11111)
// RTTY Experiment based on code from the DSP Tutorial at http://dp.nonoo.hu/projects/ham-dsp-tutorial/18-rtty-decoder-using-iir-filters/
// Used with permission from Norbert Varga, HA2NON under GPLv3 license
const rtty_speed_item_t rtty_speeds[RTTY_SPEED_NUM] =
{
{ .id =RTTY_SPEED_45, .value = 45.45, .label = "45" },
{ .id =RTTY_SPEED_50, .value = 50, .label = "50" },
{ .id =RTTY_SPEED_200, .value = 200, .label = "200" },
};
const rtty_shift_item_t rtty_shifts[RTTY_SHIFT_NUM] =
{
{ RTTY_SHIFT_85, 85, " 85" },
{ RTTY_SHIFT_170, 170, "170" },
{ RTTY_SHIFT_200, 200, "200" },
{ RTTY_SHIFT_340, 340, "340" },
{ RTTY_SHIFT_425, 425, "425" },
{ RTTY_SHIFT_450, 450, "450" },
{ RTTY_SHIFT_850, 850, "850" },
};
typedef struct
{
float32_t gain;
float32_t coeffs[4];
uint16_t freq; // center freq
} rtty_bpf_config_t;
typedef struct
{
float32_t gain;
float32_t coeffs[2];
} rtty_lpf_config_t;
typedef struct
{
float32_t xv[5];
float32_t yv[5];
} rtty_bpf_data_t;
typedef struct
{
float32_t xv[3];
float32_t yv[3];
} rtty_lpf_data_t;
static float32_t RttyDecoder_bandPassFreq(float32_t sampleIn, const rtty_bpf_config_t* coeffs, rtty_bpf_data_t* data) {
data->xv[0] = data->xv[1]; data->xv[1] = data->xv[2]; data->xv[2] = data->xv[3]; data->xv[3] = data->xv[4];
data->xv[4] = sampleIn / coeffs->gain; // gain at centre
data->yv[0] = data->yv[1]; data->yv[1] = data->yv[2]; data->yv[2] = data->yv[3]; data->yv[3] = data->yv[4];
data->yv[4] = (data->xv[0] + data->xv[4]) - 2 * data->xv[2]
+ (coeffs->coeffs[0] * data->yv[0]) + (coeffs->coeffs[1] * data->yv[1])
+ (coeffs->coeffs[2] * data->yv[2]) + (coeffs->coeffs[3] * data->yv[3]);
return data->yv[4];
}
static float32_t RttyDecoder_lowPass(float32_t sampleIn, const rtty_lpf_config_t* coeffs, rtty_lpf_data_t* data) {
data->xv[0] = data->xv[1]; data->xv[1] = data->xv[2];
data->xv[2] = sampleIn / coeffs->gain; // gain at DC
data->yv[0] = data->yv[1]; data->yv[1] = data->yv[2];
data->yv[2] = (data->xv[0] + data->xv[2]) + 2 * data->xv[1]
+ (coeffs->coeffs[0] * data->yv[0]) + (coeffs->coeffs[1] * data->yv[1]);
return data->yv[2];
}
typedef enum {
RTTY_RUN_STATE_WAIT_START = 0,
RTTY_RUN_STATE_BIT,
} rtty_run_state_t;
typedef enum {
RTTY_MODE_LETTERS = 0,
RTTY_MODE_SYMBOLS
} rtty_charSetMode_t;
typedef struct {
rtty_bpf_data_t bpfSpaceData;
rtty_bpf_data_t bpfMarkData;
rtty_lpf_data_t lpfData;
rtty_bpf_config_t *bpfSpaceConfig;
rtty_bpf_config_t *bpfMarkConfig;
rtty_lpf_config_t *lpfConfig;
uint16_t oneBitSampleCount;
int32_t DPLLOldVal;
int32_t DPLLBitPhase;
uint8_t byteResult;
uint16_t byteResultp;
rtty_charSetMode_t charSetMode;
rtty_run_state_t state;
const rtty_mode_config_t* config_p;
} rtty_decoder_data_t;
rtty_decoder_data_t rttyDecoderData;
// this is for 12ksps sample rate
// for filter designing, see http://www-users.cs.york.ac.uk/~fisher/mkfilter/
// order 2 Butterworth, freqs: 865-965 Hz, centre: 915 Hz
static rtty_bpf_config_t rtty_bp_12khz_915 =
{
.gain = 1.513364755e+03,
.coeffs = { -0.9286270861, 3.3584472566, -4.9635817596, 3.4851652468 },
.freq = 915
};
// order 2 Butterworth, freqs: 1315-1415 Hz, centre 1365Hz
static rtty_bpf_config_t rtty_bp_12khz_1365 =
{
.gain = 1.513365019e+03,
.coeffs = { -0.9286270861, 2.8583904591, -4.1263569881, 2.9662407442 },
.freq = 1365
};
// order 2 Butterworth, freqs: 1035-1135 Hz, centre: 1085Hz
static rtty_bpf_config_t rtty_bp_12khz_1085 =
{
.gain = 1.513364927e+03,
.coeffs = { -0.9286270861, 3.1900687350, -4.6666321298, 3.3104336142 },
.freq = 1085
};
// order 2 Butterworth, freqs: 1065-1165 Hz, centre: 1115Hz
// for 200Hz shift
static rtty_bpf_config_t rtty_bp_12khz_1115 =
{
.gain = 1.513364944e+03,
.coeffs = { -0.9286270861, 3.1576917276, -4.6112830458, 3.2768349860 },
.freq = 1115
};
// for 340Hz shift --> 915 + 340 = 1255Hz
// order 2 Butterworth, freqs: 1205-1305 Hz, centre: 1255Hz
//
static rtty_bpf_config_t rtty_bp_12khz_1255 =
{
.gain = 1.513364944e+03,
.coeffs = { -0.9286270861, 2.9964316664, -4.3440155011, 3.1094904013 },
.freq = 1255
};
// for 85Hz shift --> 915 + 85Hz = space = 1000Hz
// 3dB bandwidth 50Hz
// order 2 Butterworth, freqs: 975-1025 Hz, centre: 1000Hz
static rtty_bpf_config_t rtty_bp_12khz_1000 =
{
.gain = 5.944465260e+03,
.coeffs = { -0.9636529842, 3.3693752166, -4.9084595657, 3.4323354886 },
.freq = 1000
};
// for 425Hz shift --> 915 + 425Hz = space = 1340Hz
// 3dB bandwidth 100Hz
// order 2 Butterworth, freqs: 1290 - 1390 Hz, centre: 1340Hz
static rtty_bpf_config_t rtty_bp_12khz_1340 =
{
.gain = 1.513365018e+03,
.coeffs = { -0.9286270862, 2.8906128091, -4.1762457780, 2.9996788796 },
.freq = 1340
};
// for 850Hz shift --> 915 + 850Hz = space = 1765Hz
// 3dB bandwidth 100Hz
// order 2 Butterworth, freqs: 1715 - 1815 Hz, centre: 1765Hz
static rtty_bpf_config_t rtty_bp_12khz_1765 =
{
.gain = 1.513365057e+03,
.coeffs = { -0.9286270862, 2.1190223173, -3.1352567157, 2.1989754113 },
.freq = 1765
};
static rtty_lpf_config_t rtty_lp_12khz_50 =
{
.gain = 5.944465310e+03,
.coeffs = { -0.9636529842, 1.9629800894 }
};
static rtty_mode_config_t rtty_mode_current_config;
int RTTY_marker_0 = 915; // RTTY_mark
int RTTY_marker_1 = RTTY_marker_0 + rtty_shifts[rtty_ctrl_config.shift_idx].value;
int is_usb_demod = 1;
float hz_per_pixel = 1.0;
float RTTY_marker_0_offset = 127;
float RTTY_marker_1_offset = 127;
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
// Settings for the hardware QSD
// Joris PCB uses a 27MHz crystal and CLOCK 2 output
// Elektor SDR PCB uses a 25MHz crystal and the CLOCK 1 output
//#define Si_5351_clock SI5351_CLK1
#if defined(HARDWARE_DO7JBH) || defined(HARDWARE_FRANKB) || defined(HARDWARE_DO7JBH_T41)
#define Si_5351_crystal 25000000
#else
#define Si_5351_crystal 27000000
#endif
#define Si_5351_clock SI5351_CLK2
// {SI5351_DRIVE_2MA, SI5351_DRIVE_4MA, SI5351_DRIVE_6MA, SI5351_DRIVE_8MA};
#define Si_5351_drive SI5351_DRIVE_4MA // default drive strength of the library is 2mA, which could be too low depending on your hardware!?
// Europe uses 9 kHz AM spacing, N.A. uses 10 (AM_SPACING_EU==0). Others??? <PUA>
#define AM_SPACING_EU 1
unsigned long long calibration_factor = 1000000000 ;// 10002285;
long calibration_constant = 0;
// this is for the Joris PCB !
//long calibration_constant = 108000; // this is for the Elektor PCB !
unsigned long long hilfsf = 1000000000;
uint8_t save_energy = 0;
uint8_t atan2_approx = 1;
#if defined (HARDWARE_DO7JBH) || defined (HARDWARE_DO7JBH_T41)
// Optical Encoder connections
Encoder tune (16, 17);
Encoder filter (4, 5);
Encoder encoder3 (1, 2); //(26, 28);
Si5351 si5351;
#define MASTER_CLK_MULT 4 // QSD frontend requires 4x clock
// pins for digital attenuator board PE4306
//#define ATT_LE 24
//#define ATT_DATA 25
//#define ATT_CLOCK 28
// dummy definitions for Dantes hardware
#define ATT_LE 40
#define ATT_DATA 50//41
#define ATT_CLOCK 42
// prop shield LC used for audio speaker amp
//#define AUDIO_AMP_ENABLE 39
#if (defined(T4))
#define BACKLIGHT_PIN 6 // cut PCB trace to 3V3 and new wire soldered from TFT backlight to pin6 on DO7JBHs PCB
#define TFT_DC 34 // 20
#define TFT_CS 10 //21
#define TFT_RST 35 // 255 = unused. connect to 3.3V
#define TFT_MOSI 11 //7
#define TFT_SCLK 13 //14
#define TFT_MISO 12
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
#else
#define BACKLIGHT_PIN 0 // unfortunately connected to 3V3 in DO7JBHs PCB
#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 35 // 255 = unused. connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
#endif
// push-buttons
#if defined (HARDWARE_DO7JBH)
#define BUTTON_1_PIN A22 // encoder2 button = button3SW
#define BUTTON_6_PIN 8 // this is the pushbutton pin of the filter encoder
#elif defined(HARDWARE_DO7JBH_T41)
#define BUTTON_1_PIN 41 // encoder2 button = button3SW
#define BUTTON_6_PIN 15 //8 // this is the pushbutton pin of the filter encoder
#endif
#define BUTTON_2_PIN 37 // BAND+ = button2SW
#define BUTTON_3_PIN 30 // ???
#define BUTTON_4_PIN 36 //
#define BUTTON_5_PIN 38 // this is the pushbutton pin of the tune encoder
#define BUTTON_7_PIN 39 // this is the menu button pin
#define BUTTON_8_PIN 33 //27 // this is the pushbutton pin of encoder 3
const int8_t On_set = 25; // hold switched on
Bounce button1 = Bounce(BUTTON_1_PIN, 50);
Bounce button2 = Bounce(BUTTON_2_PIN, 50);
Bounce button3 = Bounce(BUTTON_3_PIN, 50);
Bounce button4 = Bounce(BUTTON_4_PIN, 50);
Bounce button5 = Bounce(BUTTON_5_PIN, 50);
Bounce button6 = Bounce(BUTTON_6_PIN, 50);
Bounce button7 = Bounce(BUTTON_7_PIN, 50);
Bounce button8 = Bounce(BUTTON_8_PIN, 50);
#elif defined(HARDWARE_FRANKB)
Si5351 si5351;
// Optical Encoder connections
Encoder tune (2, 3);
Encoder filter (1, 2);
Encoder encoder3 (15, 16); //(26, 28);
#define MASTER_CLK_MULT 4 // QSD frontend requires 4x clock
#define PCF8574_ADR 0x20
#define PCF8574_INT 22
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_SENSE 24 //read 0: Card inserted, 1: no Card
#define ENCODER_1_A 2
#define ENCODER_1_B 3
#define ENCODER_2_A 4
#define ENCODER_2_B 14
#define ENCODER_3_A 15
#define ENCODER_3_B 16
#define TFT_DC 10 // only CS pin
#define TFT_CS 9 // using standard pin
#define TFT_RST 255 // no reset
#define TFT_TOUCH_IRQ 5
#define TFT_TOUCH_CS 6
#define LED_PIN 13
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST);
#elif defined(HARDWARE_FRANKB2)
#undef Si_5351_crystal
#undef Si_5351_clock
#define Si_5351_crystal 25000000
#define Si_5351_clock SI5351_CLK1
Si5351 si5351;
#define MASTER_CLK_MULT 4 // QSD frontend requires 4x clock
#define BACKLIGHT_PIN 6
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 255 // 255 = unused. connect to 3.3V
#define TFT_MOSI 11
#define TFT_SCLK 13
#define TFT_MISO 12
ILI9341_t3n tft = ILI9341_t3n(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
Encoder tune (16, 17);
Encoder filter (15, 14);