-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathradio_management.c
1889 lines (1643 loc) · 63.8 KB
/
radio_management.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
/* -*- mode: c; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4; coding: utf-8 -*- */
/************************************************************************************
** **
** UHSDR **
** a powerful firmware for STM32 based SDR transceivers **
** **
**---------------------------------------------------------------------------------**
** **
** File name: **
** Description: **
** Last Modified: **
** Licence: GNU GPLv3 **
************************************************************************************/
// Common
#include <assert.h>
#include "radio_management.h"
#include "profiling.h"
#include "adc.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <soft_tcxo.h>
#include "uhsdr_hw_i2c.h"
#include "freedv_uhsdr.h"
// SI570 control
#include "osc_interface.h"
#include "codec.h"
#include "audio_driver.h"
#include "audio_management.h"
#include "cat_driver.h"
#include "ui_spectrum.h"
#include "ui_configuration.h"
#include "ui_menu.h" // for CONFIG_160M_FULL_POWER_ADJUST and Co.
#include "config_storage.h"
#include "cw_gen.h"
#include "cw_decoder.h"
#include "psk.h"
#include "rtty.h"
#include "uhsdr_digi_buffer.h"
#include "audio_nr.h"
#define SWR_SAMPLES_SKP 1 //5000
#define SWR_SAMPLES_CNT 5//10
#define SWR_ADC_FULL_SCALE 4095 // full scale of A/D converter (4095 = 10 bits)
#define SWR_ADC_VOLT_REFERENCE 3.3 // NOMINAL A/D reference voltage. The PRECISE value is calibrated by a menu item! (Probably "FWD/REV ADC Cal.")
//
// coefficients for very low power (<75 milliwatt) power levels. Do NOT use this above approx. 0.07 volts input!
//
#define LOW_RF_PWR_COEFF_A -0.0338205168744131 // constant (offset)
#define LOW_RF_PWR_COEFF_B 5.02584652062682 // "b" coefficient (for x)
#define LOW_RF_PWR_COEFF_C -106.610490958242 // "c" coefficient (for x^2)
#define LOW_RF_PWR_COEFF_D 853.156505329744 // "d" coefficient (for x^3)
//
// coefficients for higher power levels (>50 milliwatts). This is actually good down to 25 milliwatts or so.
//
#define HIGH_RF_PWR_COEFF_A 0.01209 //0.0120972709513557 // constant (offset)
#define HIGH_RF_PWR_COEFF_B 0.8334 //0.833438917330908 // "b" coefficient (for x)
#define HIGH_RF_PWR_COEFF_C 1.569 //1.56930042559198 // "c" coefficient (for x^2)
#define LOW_POWER_CALC_THRESHOLD 0.05 // voltage from sensor below which we use the "low power" calculations, above
// SWR/Power meter
SWRMeter swrm;
// ------------------------------------------------
// Frequency public
DialFrequency df;
//
// Bands definition
//
//
// We first define all individual band definitions
static const BandInfo bi_2200m_all = { .tune = 135700, .size = 2100, .name = "2200m", BAND_MODE_2200};
static const BandInfo bi_630m_all = { .tune = 472000, .size = 7000, .name = "630m", BAND_MODE_630};
static const BandInfo bi_160m_all = { .tune = 1810000, .size = 190000, .name = "160m", BAND_MODE_160};
static const BandInfo bi_80m_r1 = { .tune = 3500000, .size = 300000, .name = "80m", BAND_MODE_80};
static const BandInfo bi_80m_r2 = { .tune = 3500000, .size = 500000, .name = "80m", BAND_MODE_80};
static const BandInfo bi_80m_r3 = { .tune = 3500000, .size = 400000, .name = "80m", BAND_MODE_80};
static const BandInfo bi_60m_gen = { .tune = 5250000, .size = 200000, .name = "60m", BAND_MODE_60};
static const BandInfo bi_40m_r1 = { .tune = 7000000, .size = 200000, .name = "40m", BAND_MODE_40};
static const BandInfo bi_40m_r2_3 = { .tune = 7000000, .size = 300000, .name = "40m", BAND_MODE_40};
static const BandInfo bi_30m_all = { .tune = 10100000, .size = 50000, .name = "30m", BAND_MODE_30};
static const BandInfo bi_20m_all = { .tune = 14000000, .size = 350000, .name = "20m", BAND_MODE_20};
static const BandInfo bi_17m_all = { .tune = 18068000, .size = 100000, .name = "17m", BAND_MODE_17};
static const BandInfo bi_15m_all = { .tune = 21000000, .size = 450000, .name = "15m", BAND_MODE_15};
static const BandInfo bi_12m_all = { .tune = 24890000, .size = 100000, .name = "12m", BAND_MODE_12};
static const BandInfo bi_10m_all = { .tune = 28000000, .size = 1700000, .name = "10m", BAND_MODE_10};
static const BandInfo bi_6m_r2_3 = { .tune = 50000000, .size = 4000000, .name = "6m", BAND_MODE_6};
static const BandInfo bi_4m_gen = { .tune = 70000000, .size = 500000, .name = "4m", BAND_MODE_4};
static const BandInfo bi_2m_r1 = { .tune = 144000000, .size = 2000000, .name = "2m", BAND_MODE_2};
static const BandInfo bi_2m_r2_3 = { .tune = 144000000, .size = 4000000, .name = "2m", BAND_MODE_2};
static const BandInfo bi_70cm_r1 = { .tune = 430000000, .size = 10000000, .name = "70cm", BAND_MODE_70};
static const BandInfo bi_70cm_r2_3 = { .tune = 430000000, .size = 20000000, .name = "70cm", BAND_MODE_70};
static const BandInfo bi_23cm_all = { .tune = 1240000000, .size = 60000000, .name = "23cm", BAND_MODE_23};
static const BandInfo bi_gen_all = { .tune = 0, .size = 0, .name = "Gen", BAND_MODE_GEN};
// now we combine from the above defined bands the set of bands for each region
// to be backwards compatible we provide the original set of bands which are
// the maximum band size from the 3 different IARU regions
// IMPORTANT: Right now the order of bands in the list is fixed to the order of BAND_MODE_xxx (low to high)
// TODO: Make this list ordered by frequency
static const BandInfo *bandInfo_combined[MAX_BAND_NUM] =
{
&bi_80m_r2,
&bi_60m_gen,
&bi_40m_r2_3,
&bi_30m_all,
&bi_20m_all,
&bi_17m_all,
&bi_15m_all,
&bi_12m_all,
&bi_10m_all,
&bi_6m_r2_3,
&bi_4m_gen,
&bi_2m_r2_3,
&bi_70cm_r2_3,
&bi_23cm_all,
&bi_2200m_all,
&bi_630m_all,
&bi_160m_all,
&bi_gen_all,
};
static const BandInfo* bandInfo_region1[MAX_BAND_NUM] =
{
&bi_80m_r1,
&bi_60m_gen, // should cover all regions
&bi_40m_r1,
&bi_30m_all,
&bi_20m_all,
&bi_17m_all,
&bi_15m_all,
&bi_12m_all,
&bi_10m_all,
&bi_6m_r2_3,
&bi_4m_gen,
&bi_2m_r1,
&bi_70cm_r1,
&bi_23cm_all,
&bi_2200m_all,
&bi_630m_all,
&bi_160m_all,
&bi_gen_all,
};
static const BandInfo* bandInfo_region2[MAX_BAND_NUM] =
{
&bi_80m_r2,
&bi_60m_gen, // should cover all regions
&bi_40m_r2_3,
&bi_30m_all,
&bi_20m_all,
&bi_17m_all,
&bi_15m_all,
&bi_12m_all,
&bi_10m_all,
&bi_6m_r2_3,
&bi_4m_gen,
&bi_2m_r2_3,
&bi_70cm_r2_3,
&bi_23cm_all,
&bi_2200m_all,
&bi_630m_all,
&bi_160m_all,
&bi_gen_all,
};
static const BandInfo* bandInfo_region3[MAX_BAND_NUM] =
{
&bi_80m_r3,
&bi_60m_gen, // should cover all regions
&bi_40m_r2_3,
&bi_30m_all,
&bi_20m_all,
&bi_17m_all,
&bi_15m_all,
&bi_12m_all,
&bi_10m_all,
&bi_6m_r2_3,
&bi_4m_gen,
&bi_2m_r2_3,
&bi_70cm_r2_3,
&bi_23cm_all,
&bi_2200m_all,
&bi_630m_all,
&bi_160m_all,
&bi_gen_all,
};
// finally we list all of them in a table and give them names for the menu
const BandInfoSet bandInfos[] =
{
{ bandInfo_combined, "R1+2+3" },
{ bandInfo_region1, "Region 1" },
{ bandInfo_region2, "Region 2" },
{ bandInfo_region3, "Region 3" },
};
const int BAND_INFO_SET_NUM = sizeof(bandInfos)/sizeof(BandInfoSet);
BandInfo_c **bandInfo = bandInfo_combined;
uint8_t bandinfo_idx; // default init with 0 is fine
/**
* Searches the band info for a given band memory index
* This relieves us from ordering the vfo band memories exactly like the
* band infos.
*
* @param new_band_index
* @return the band with the provided index or if this is not found, the current bandInfo
*/
const BandInfo* RadioManagement_GetBandInfo(uint8_t new_band_index)
{
const BandInfo* bi = ts.band;
for (int idx = 0; idx < MAX_BANDS; idx ++)
{
if (bandInfo[idx]->band_mode == new_band_index)
{
bi = bandInfo[idx];
break;
}
}
return bi;
}
// this structure MUST match the order of entries in power_level_t !
static const power_level_desc_t mchf_rf_power_levels[] =
{
{ .id = PA_LEVEL_FULL, .name = "FULL", .power_factor = 1.0 , .mW = 0, }, // we use 0 to indicate max power
{ .id = PA_LEVEL_5W, .name = "5W" , .power_factor = 1.0 , .mW = 5000, },
{ .id = PA_LEVEL_2W, .name = "2W" , .power_factor = 0.6324, .mW = 2000, },
{ .id = PA_LEVEL_1W, .name = "1W" , .power_factor = 0.447 , .mW = 1000, },
{ .id = PA_LEVEL_0_5W, .name = "0.5W" , .power_factor = 0.316 , .mW = 500, },
};
const pa_power_levels_info_t mchf_power_levelsInfo =
{
.levels = mchf_rf_power_levels,
.count = sizeof(mchf_rf_power_levels)/sizeof(*mchf_rf_power_levels),
};
typedef struct
{
char* name;
float32_t reference_power;
int32_t max_freq;
int32_t min_freq;
int32_t max_am_power;
} pa_info_t;
static const pa_info_t mchf_pa =
{
.name = "mcHF PA",
.reference_power = 5000.0,
.max_freq = 32000000,
.min_freq = 1800000,
.max_am_power = 2000.0,
};
// The following descriptor table has to be in the order of the enum digital_modes_t in radio_management.h
// This table is stored in flash (due to const) and cannot be written to
// for operational data per mode [r/w], use a different table with order of modes
const digital_mode_desc_t digimodes[DigitalMode_Num_Modes] =
{
{ "DIGITAL" , true },
#ifdef USE_FREEDV
{ "FreeDV" , true },
#endif
{ "RTTY" , true },
{ "BPSK" , true },
};
static void RadioManagement_SetCouplingForFrequency(uint32_t freq);
static void RadioManagement_SetHWFiltersForFrequency(uint32_t freq);
/**
* @brief returns the "real" frequency translation mode for a given transceiver state. This may differ from the configured one due to modulation demands
*
*/
uint32_t RadioManagement_GetRealFreqTranslationMode(uint32_t txrx_mode, uint32_t dmod_mode, uint32_t iq_freq_mode)
{
uint32_t retval = iq_freq_mode;
if (dmod_mode == DEMOD_CW && txrx_mode == TRX_MODE_TX)
{
retval = FREQ_IQ_CONV_MODE_OFF;
}
return retval;
}
/**
* @brief permits to dis/enable a digital codec (or get back to analog)
*/
void RadioManagement_ChangeCodec(uint32_t codec, bool enableCodec)
{
// codec == 0 -> Analog Sound
// all other codecs -> digital codec
if (codec == 0)
{
ts.dvmode = false;
}
else
{
ts.dvmode = enableCodec;
}
ts.digital_mode = codec;
}
/**
* Returns the scaling which needs to be applied to the standard signal levl (which delivers the PA_REFERENCE_POWER)
* in order to output the request power.
* @param powerMw requested power in mW. mW =< 0.0 returns scale 1
* @return scaling (gain)
*/
float32_t RadioManagement_CalculatePowerFactorScale(float32_t powerMw)
{
float32_t retval = 1.0;
if (powerMw > 0)
{
retval = sqrtf(powerMw / mchf_pa.reference_power);
}
return retval;
}
/**
*
* Depends on globals: ts.pwr_adj, ts.power_level, df.tune_old, ts.flags2 & FLAGS2_LOW_BAND_BIAS_REDUCE
* Impacts globals: ts.tx_power_factor
* @param band
* @return true if the power factor value differs from previous
*/
static bool RadioManagement_SetBandPowerFactor(const BandInfo* band, int32_t power)
{
float32_t pf_bandvalue; // used as a holder for percentage of power output scaling
// FIXME: This code needs fixing, the hack for TX Outside should at least reduce power factor for lower bands
if (RadioManagement_IsGenericBand(band)) // we are outside a TX band
{
// TX outside bands **very dirty hack**
// FIXME: calculate based on 2 frequency points close the selected frequency, should be inter-/extrapolated
uint32_t freq_min = RadioManagement_GetBandInfo(BAND_MODE_80)->tune;
float32_t adj_min = ts.pwr_adj[ADJ_REF_PWR][BAND_MODE_80] / (RadioManagement_IsPowerFactorReduce(freq_min)? 400: 100);
uint32_t freq_max = RadioManagement_GetBandInfo(BAND_MODE_10)->tune;
float32_t adj_max = ts.pwr_adj[ADJ_REF_PWR][BAND_MODE_10] / (RadioManagement_IsPowerFactorReduce(freq_max)? 400: 100);
float32_t delta_f = (float32_t)df.tune_old - (float32_t)freq_min; // we must convert to a signed type
float32_t delta_points = freq_max - freq_min;
float32_t freq_mult = delta_f / delta_points;
pf_bandvalue = freq_mult * (adj_max - adj_min) + adj_min;
}
else
{
pf_bandvalue = ts.pwr_adj[power == 0?ADJ_FULL_POWER:ADJ_REF_PWR][band->band_mode];
pf_bandvalue /= RadioManagement_IsPowerFactorReduce(df.tune_old)? 400: 100;
}
float32_t power_factor_scale = 1.0 ;
// now rescale to power levels below reference power (i.e for mcHF <5 watts) if necessary.
if (power != 0)
{
power_factor_scale = RadioManagement_CalculatePowerFactorScale(power);
}
float32_t power_factor = pf_bandvalue * power_factor_scale;
// limit hard limit for power factor since it otherwise may overdrive the PA section
const float32_t old_pf = ts.tx_power_factor;
ts.tx_power_factor =
(power_factor > TX_POWER_FACTOR_MAX_INTERNAL) ?
TX_POWER_FACTOR_MAX_INTERNAL : power_factor;
ts.power_modified |= (power_factor == 0 || ts.tx_power_factor != power_factor);
return ts.tx_power_factor != old_pf;
}
/**
* Is the currently active modulation / transceiver mode able to generate a side tone during transmit
* Can be called during RX and TX
*
* @return true if the selected modulation mode permits the use of a side tone
*/
bool RadioManagement_UsesTxSidetone()
{
return ts.dmod_mode == DEMOD_CW || is_demod_rtty() || is_demod_psk() || (ts.tune && !ts.iq_freq_mode);
}
/**
* @brief API Function, implements application logic for changing the power level including filter changes
*
*
* @param power_level The requested power level (as PA_LEVEL constants). Invalid values are not accepted
* @returns true if power level has been changed, false otherwise
*/
bool RadioManagement_SetPowerLevel(const BandInfo* band, power_level_t power_level)
{
bool retval = false;
bool power_modified = false;
int32_t power = power_level < mchf_power_levelsInfo.count ? mchf_power_levelsInfo.levels[power_level].mW : -1;
if (power != -1 && band != NULL)
{
if (RadioManagement_IsGenericBand(band))
{
if(ts.flags1 & FLAGS1_TX_OUTSIDE_BANDS)
{
power = 50; // ~50 mW limit;
power_modified = true;
// I never will use this function (DF8OE)
}
else
{
power = 5; // 5mW, use very low value in case of wrong call to this function
power_modified = true;
}
}
if(ts.dmod_mode == DEMOD_AM) // in AM mode?
{
if(power > mchf_pa.max_am_power || power == 0) // yes, power over am limits?
{
power = mchf_pa.max_am_power; // force to keep am limits
power_modified = true;
}
}
else if(power > mchf_pa.reference_power)
{
power = 0; // 0 == full power
power_level = PA_LEVEL_FULL;
}
// Calculate TX power factor - see if power changed
bool pf_change = RadioManagement_SetBandPowerFactor(band, power);
if (pf_change == true || power != ts.power || ts.power_level != power_level || ts.power_modified != power_modified)
{
retval = true;
ts.power_level = power_level;
ts.power = power;
ts.power_modified = power_modified;
if (RadioManagement_UsesTxSidetone())
{
Codec_TxSidetoneSetgain(ts.txrx_mode);
}
}
}
return retval;
}
bool RadioManagement_Tune(bool tune)
{
bool retval = tune;
if(RadioManagement_IsTxDisabled() == false && (ts.dmod_mode != DEMOD_SAM))
{
if(tune)
{
if(ts.tune_power_level != PA_LEVEL_TUNE_KEEP_CURRENT)
{
ts.power_temp = ts.power_level; //store tx level and set tune level
ts.power_level = ts.tune_power_level;
}
RadioManagement_SwitchTxRx(TRX_MODE_TX,true);
// tune ON, this will also update the power to use our
// to our temporarily changed level
retval = (ts.txrx_mode == TRX_MODE_TX);
}
else
{
RadioManagement_SwitchTxRx(TRX_MODE_RX,true); // tune OFF
if(ts.tune_power_level != PA_LEVEL_TUNE_KEEP_CURRENT)
{
RadioManagement_SetPowerLevel(RadioManagement_GetBand(df.tune_new), ts.power_temp);
}
retval = (ts.txrx_mode == TRX_MODE_TX); // no longer tuning
}
}
else
{
retval = false; // no TUNE mode in AM or FM or with disabled TX!
}
return retval;
}
/**
* @returns offset of tuned frequency to dial frequency in CW mode, in Hz
*/
int32_t RadioManagement_GetCWDialOffset()
{
int32_t retval = 0;
switch(ts.cw_offset_mode)
{
case CW_OFFSET_USB_SHIFT: // Yes - USB?
case CW_OFFSET_LSB_SHIFT: // LSB?
case CW_OFFSET_AUTO_SHIFT: // Auto mode? Check flag
if(ts.cw_lsb)
{
retval += ts.cw_sidetone_freq; // it was LSB - raise by sidetone amount
}
else
{
retval -= ts.cw_sidetone_freq; // it was USB - lower by sidetone amount
}
}
return retval;
}
/**
* @brief returns true if the mode parameters tell us we will TX at zero if as opposed to offset frequency
*/
bool RadioManagement_IsTxAtZeroIF(uint8_t dmod_mode, uint8_t digital_mode)
{
return (
dmod_mode == DEMOD_CW ||
(dmod_mode == DEMOD_DIGI &&
(
#ifdef USE_FREEDV
digital_mode == DigitalMode_FreeDV
#else
false
#endif
|| is_demod_psk()
#ifdef USE_RTTY_PROCESSOR
|| is_demod_rtty()
#endif
)
)
);
}
uint32_t RadioManagement_Dial2TuneFrequency(const uint32_t dial_freq, uint8_t txrx_mode)
{
uint32_t tune_freq = dial_freq;
//
// Do "Icom" style frequency offset of the LO if in "CW OFFSET" mode. (Display freq. is also offset!)
if(ts.dmod_mode == DEMOD_CW) // In CW mode?
{
tune_freq += RadioManagement_GetCWDialOffset();
}
// Offset dial frequency if the RX/TX frequency translation is active and we are not transmitting in mode which
// does not use frequency translation, this permits to use the generated I or Q channel as audio sidetone
// that is right now RTTY, FreeDV, CW, BPSK
if(txrx_mode != TRX_MODE_TX || RadioManagement_IsTxAtZeroIF(ts.dmod_mode, ts.digital_mode) == false)
{
tune_freq += AudioDriver_GetTranslateFreq();
}
// Extra tuning actions
if(txrx_mode == TRX_MODE_RX)
{
tune_freq += (ts.rit_value*20); // Add RIT on receive
}
return tune_freq;
}
/**
* @brief switch off the PA Bias to mute HF output ( even if PTT is on )
* Using this method the PA will be effectively muted no matter what setting
* the main bias switch has (which directly connected to the PTT HW Signal)
* Used to suppress signal path reconfiguration noise during rx/tx and tx/rx switching
*/
void RadioManagement_DisablePaBias()
{
Board_SetPaBiasValue(0);
}
/**
* @brief recalculate and set the PA Bias according to requested value
*
* Please note that at the mcHF BIAS is only applied if the PTT HW Signal
* is active (which is controlled using MchfBoard_EnableTXSignalPath())
*/
void RadioManagement_SetPaBias()
{
uint32_t calc_var;
if((ts.pa_cw_bias) && (ts.dmod_mode == DEMOD_CW)) // is CW PA bias non-zero AND are we in CW mode?
{
calc_var = ts.pa_cw_bias; // use special CW-mode bias setting
}
else
{
calc_var = ts.pa_bias; // use "default" bias setting
}
calc_var = calc_var *2 + BIAS_OFFSET;
if(calc_var > 255)
{
calc_var = 255;
}
Board_SetPaBiasValue(calc_var);
}
bool RadioManagement_ChangeFrequency(bool force_update, uint32_t dial_freq,uint8_t txrx_mode)
{
// everything else uses main VFO frequency
// uint32_t tune_freq;
bool lo_change_pending = false;
// Calculate actual tune frequency
ts.tune_freq_req = RadioManagement_Dial2TuneFrequency(dial_freq, txrx_mode);
if((ts.tune_freq != ts.tune_freq_req) || (ts.refresh_freq_disp) || df.temp_factor_changed || force_update ) // did the frequency NOT change and display refresh NOT requested??
{
if(ts.sysclock-ts.last_tuning > 5 || ts.last_tuning == 0) // prevention for SI570 crash due too fast frequency changes
{
Oscillator_ResultCodes_t lo_prep_result = osc->prepareNextFrequency(ts.tune_freq_req, df.temp_factor);
// first check and mute output if a large step is to be done
if(osc->isNextStepLarge() == true) // did the tuning require that a large tuning step occur?
{
// 18 is a little more than 10ms (15 ==10ms) which is max for the Si570 to change the frequency
if (ts.audio_dac_muting_buffer_count < 18)
{
ts.audio_dac_muting_buffer_count = 18;
}
if (ts.audio_processor_input_mute_counter < 18)
{
ts.audio_processor_input_mute_counter = 18;
}
}
ts.last_tuning = ts.sysclock;
ts.last_lo_result = lo_prep_result;
Oscillator_ResultCodes_t lo_exec_result = OSC_TUNE_IMPOSSIBLE;
if (lo_prep_result != OSC_TUNE_IMPOSSIBLE)
{
// Set frequency
lo_exec_result = osc->changeToNextFrequency();
}
// if i2c error or verify error, there is a chance that we can fix that, so we mark this
// as NOT executed, in all other cases we assume the change has happened (but may prevent TX)
if (lo_exec_result != OSC_COMM_ERROR && lo_exec_result != OSC_ERROR_VERIFY)
{
df.temp_factor_changed = false;
ts.tune_freq = ts.tune_freq_req; // frequency change required - update change detector
// Save current freq
df.tune_old = dial_freq;
}
else
{
ts.last_lo_result = lo_exec_result;
}
if (ts.last_lo_result == OSC_OK || ts.last_lo_result == OSC_TUNE_LIMITED)
{
ts.tx_disable &= ~TX_DISABLE_OUTOFRANGE;
}
else
{
ts.tx_disable |= TX_DISABLE_OUTOFRANGE;
}
uint32_t tune_freq_real = ts.tune_freq;
RadioManagement_SetCouplingForFrequency(tune_freq_real); // adjust wattmeter coupling factor
RadioManagement_SetHWFiltersForFrequency(tune_freq_real); // check the filter status with the new frequency update
AudioManagement_CalcIqPhaseGainAdjust(tune_freq_real);
// Inform Spectrum Display code that a frequency change has happened
ts.dial_moved = 1;
}
else
{
lo_change_pending = true;
}
}
// successfully executed the change
return lo_change_pending == false;
}
/**
* @brief temporary muting of the receiver when making changes which may cause audible pops etc., unmuting happens some 10s of milliseconds automatically later
*
*/
void RadioManagement_MuteTemporarilyRxAudio()
{
// save us from the loud "POP" that will occur when we change bands
ts.audio_processor_input_mute_counter = 5 * 15;
ts.audio_dac_muting_buffer_count = 13 * 15;
}
Oscillator_ResultCodes_t RadioManagement_ValidateFrequencyForTX(uint32_t dial_freq)
{
// we check with the si570 code if the frequency is tunable, we do not tune to it.
Oscillator_ResultCodes_t osc_res = osc->prepareNextFrequency(RadioManagement_Dial2TuneFrequency(dial_freq, TRX_MODE_TX), df.temp_factor);
bool osc_ok = osc_res == OSC_OK || osc_res == OSC_TUNE_LIMITED;
// we also check if our PA is able to support this frequency
bool pa_ok = dial_freq >= mchf_pa.min_freq && dial_freq <= mchf_pa.max_freq;
return pa_ok && osc_ok ? osc_res: OSC_TUNE_IMPOSSIBLE;
}
/**
* @brief returns the current LO Tune Frequency for TX
* @returns LO Frequency in Hz
*/
uint32_t RadioManagement_GetTXDialFrequency()
{
uint32_t retval;
if (ts.txrx_mode != TRX_MODE_TX)
{
if(is_splitmode()) // is SPLIT mode active and?
{
uint8_t vfo_tx;
if (is_vfo_b())
{
vfo_tx = VFO_A;
}
else
{
vfo_tx = VFO_B;
}
retval = vfo[vfo_tx].band[ts.band->band_mode].dial_value; // load with VFO-A frequency
}
else
{
retval = df.tune_new;
}
}
else
{
retval = df.tune_new;
}
return retval;
}
/**
* @brief returns the current LO Dial Frequency for RX
* @returns LO Frequency in Hz
*/
uint32_t RadioManagement_GetRXDialFrequency()
{
uint32_t baseval;
if (ts.txrx_mode != TRX_MODE_RX)
{
if(is_splitmode()) // is SPLIT mode active?
{
uint8_t vfo_rx;
if (is_vfo_b())
{
vfo_rx = VFO_B;
}
else
{
vfo_rx = VFO_A;
}
baseval = vfo[vfo_rx].band[ts.band->band_mode].dial_value; // load with VFO-A frequency
}
else
{
baseval = df.tune_new;
}
}
else
{
baseval = df.tune_new;
}
return baseval + (ts.rit_value*20);
}
// globals used:
// ts.trx_mode -> R / W
// ts.band -> R
// vfo[] -> r / w
// df.tune_new -> r
// ts.dmod_mode -> r
// ts.cw_text_entry -> r
// functions called:
// RadioManagement_ValidateFrequencyForTX
// RadioManagement_IsTxDisabled()
// ts.audio_dac_muting_buffer_count -> r/w
// ts.audio_dac_muting_flag = true; // let the audio being muted initially as long as we need it
// ads.agc_holder -> w
// ads.agc_val -> r
// RadioManagement_DisablePaBias(); // kill bias to mute the HF output quickly
// Board_RedLed(LED_STATE_ON); // TX
// Board_GreenLed(LED_STATE_OFF);
// Board_EnableTXSignalPath(true); // switch antenna to output and codec output to QSE mixer
// RadioManagement_ChangeFrequency(false,df.tune_new, txrx_mode_final);
// uint8_t tx_band = RadioManagement_GetBand(tune_new);
// RadioManagement_PowerLevelChange(tx_band,ts.power_level);
// RadioManagement_SetBandPowerFactor(tx_band);
// AudioManagement_SetSidetoneForDemodMode(ts.dmod_mode,txrx_mode_final == TRX_MODE_RX?false:tune_mode);
// Codec_SwitchTxRxMode(txrx_mode_final);
// RadioManagement_SetPaBias();
// ts.txrx_switch_audio_muting_timing -> r;
// ts.audio_processor_input_mute_counter -> w;
// ts.audio_dac_muting_buffer_count -> w
// ts.audio_dac_muting_flag -> w;
// ts.tx_audio_source -> r
// ts.power_level -> r
/**
* @brief check if all resources are available to switch tx/rx mode in an interrupt
* @return true if all resources are available to switch tx/rx mode in an interrupt
*/
static __IO bool radioManagement_SwitchTxRx_running;
/**
* This function should only return true if it is absolutely safe to switch between Tx and Rx in an interrupt. Better safe than sorry.
* @return true if we can switch because no code is running in a critical section
*/
bool RadioManagement_SwitchTxRx_Possible()
{
// we check all resources which may be locked by a user mode (non-interrupt activity)
return RadioManagement_TxRxSwitching_IsEnabled() && osc->readyForIrqCall() && Codec_ReadyForIrqCall() && radioManagement_SwitchTxRx_running == false;
}
void RadioManagement_SwitchTxRx(uint8_t txrx_mode, bool tune_mode)
{
radioManagement_SwitchTxRx_running = true;
uint32_t tune_new;
bool tx_ok = false;
bool tx_pa_disabled = false;
// ts.last_tuning = 0; // prevents transmitting on wrong frequency during "RX bk phases"
if(is_splitmode()) // is SPLIT mode active?
{
uint8_t vfo_tx,vfo_rx;
if (is_vfo_b())
{
vfo_rx = VFO_B;
vfo_tx = VFO_A;
}
else
{
vfo_rx = VFO_A;
vfo_tx = VFO_B;
}
if(txrx_mode == TRX_MODE_TX) // are we in TX mode?
{
if(ts.txrx_mode == TRX_MODE_RX) // did we want to enter TX mode?
{
vfo[vfo_rx].band[ts.band->band_mode].dial_value = df.tune_new; // yes - save current RX frequency in RX VFO location
}
tune_new = vfo[vfo_tx].band[ts.band->band_mode].dial_value; // load with TX VFO frequency
}
else // we are in RX mode
{
tune_new = vfo[vfo_rx].band[ts.band->band_mode].dial_value; // load with RX VFO frequency
}
}
else
{
// we just take the current one if not in split mode
tune_new = df.tune_new;
}
if(txrx_mode == TRX_MODE_TX)
{
// FIXME: Not very robust code, make sure Validate always returns TUNE_IMPOSSIBLE in case of issues
tx_ok = RadioManagement_ValidateFrequencyForTX(tune_new) != OSC_TUNE_IMPOSSIBLE;
// this code handles the ts.tx_disable
// even if ts.tx_disble is set in CW and only in CW we still switch to TX
// but leave the PA disabled. This is for support of CW training right with the mcHF.
if (RadioManagement_IsTxDisabled() || ts.cw_text_entry)
{
if ((tx_ok == true && ts.dmod_mode == DEMOD_CW) || ts.cw_text_entry)
{
tx_pa_disabled = true;
}
else
{
// in any other case, it is not okay to transmit with ts.tx_disable == true
tx_ok = false;
}
}
if (is_demod_psk())
{
Psk_Modulator_PrepareTx();
}
}
uint8_t txrx_mode_final = tx_ok?txrx_mode:TRX_MODE_RX;
// only switch mode if tx was permitted or rx was requested
if (txrx_mode_final != ts.txrx_mode || txrx_mode_final == TRX_MODE_RX)
{
// there is in fact a switch happening
// which may cause audio issues
if (txrx_mode_final != ts.txrx_mode)
{
ts.audio_dac_muting_buffer_count = 2; // wait at least 2 buffer cycles
ts.audio_dac_muting_flag = true; // let the audio being muted initially as long as we need it
RadioManagement_DisablePaBias(); // kill bias to mute the HF output quickly
}
if(txrx_mode_final == TRX_MODE_TX)
{
// We mute the audio BEFORE we activate the PTT.
// This is necessary since U3 is switched the instant that we do so,
// rerouting audio paths and causing all sorts of disruption including CLICKs and squeaks.
Codec_PrepareTx(ts.txrx_mode); // 5ms
while (ts.audio_dac_muting_buffer_count >0)
{
// TODO: Find a better solution here
asm("nop"); // just wait a little for the silence to come out of the audio path
// this can take up to 1.2ms (time for processing two audio buffer dma requests
}
// this is here to allow CW training
// with ts.tx_disabled on nothing will be transmitted but you can hear the sidetone
if (tx_pa_disabled == false)
{
Board_RedLed(LED_STATE_ON); // TX
Board_GreenLed(LED_STATE_OFF);
Board_EnableTXSignalPath(true); // switch antenna to output and codec output to QSE mixer
}
}
df.tune_new = tune_new;
RadioManagement_ChangeFrequency(false,df.tune_new, txrx_mode_final);
// ts.audio_dac_muting_flag = true; // let the audio being muted initially as long as we need it
// there might have been a band change between the modes, make sure to have the power settings fitting the mode
if (txrx_mode_final == TRX_MODE_TX)
{
RadioManagement_SetPowerLevel(RadioManagement_GetBand(tune_new),ts.power_level);
}
AudioManagement_SetSidetoneForDemodMode(ts.dmod_mode,txrx_mode_final == TRX_MODE_RX?false:tune_mode);
// make sure the audio is set properly according to txrx and tune modes
if (txrx_mode_final == TRX_MODE_RX)
{
while (ts.audio_dac_muting_buffer_count >0)
{
// TODO: Find a better solution here
asm("nop"); // just wait a little for the silence to come out of the audio path
// this can take up to 1.2ms (time for processing two audio buffer dma requests
}
Board_EnableTXSignalPath(false); // switch antenna to input and codec output to lineout
Board_RedLed(LED_STATE_OFF); // TX led off
Board_GreenLed(LED_STATE_ON); // TX led off
ts.audio_dac_muting_flag = false; // unmute audio output
//CwGen_PrepareTx(); // make sure the keyer is set correctly for next round