diff --git a/Makefile b/Makefile index f9ae41d31..4b1b3b7f1 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ ENABLE_BOOT_BEEPS := 0 ENABLE_SHOW_CHARGE_LEVEL := 0 ENABLE_REVERSE_BAT_SYMBOL := 0 ENABLE_NO_CODE_SCAN_TIMEOUT := 1 -ENABLE_AM_FIX := 0 +ENABLE_AM_FIX := 1 ENABLE_SQUELCH_MORE_SENSITIVE := 1 ENABLE_FASTER_CHANNEL_SCAN := 1 ENABLE_RSSI_BAR := 1 diff --git a/am_fix.c b/am_fix.c index 016566a33..5e0fd83aa 100644 --- a/am_fix.c +++ b/am_fix.c @@ -29,6 +29,7 @@ #include "frequencies.h" #include "functions.h" #include "misc.h" +#include "settings.h" #ifdef ENABLE_AM_FIX @@ -186,7 +187,7 @@ static const t_gain_table gain_table[] = {0x03FF, 0}, // 91 .. 3 7 3 7 .. 0dB 0dB 0dB 0dB .. 0dB }; -static const unsigned int original_index = 85; +static const unsigned int original_index = ARRAY_SIZE(gain_table) - 7; #ifdef ENABLE_AM_FIX_SHOW_DATA // display update rate @@ -212,6 +213,9 @@ const unsigned max_index = ARRAY_SIZE(gain_table) - 1; // -89dBm, any higher and the AM demodulator starts to saturate/clip/distort const int16_t desired_rssi = (-89 + 160) * 2; +int8_t currentGainDiff; +bool enabled = true; + void AM_fix_init(void) { // called at boot-up for (int i = 0; i < 2; i++) { @@ -242,7 +246,7 @@ void AM_fix_reset(const unsigned vfo) // void AM_fix_10ms(const unsigned vfo, bool force) { - if(vfo > 1) + if(!gSetting_AM_fix || !enabled || vfo > 1 ) return; if(!force) switch (gCurrentFunction) @@ -254,7 +258,6 @@ void AM_fix_10ms(const unsigned vfo, bool force) #ifdef ENABLE_AM_FIX_SHOW_DATA counter = display_update_rate; // queue up a display update as soon as we switch to RX mode #endif - AM_fix_reset(vfo); return; // only adjust stuff if we're in one of these modes @@ -273,6 +276,12 @@ void AM_fix_10ms(const unsigned vfo, bool force) } #endif + static uint32_t lastFreq[2]; + if(gEeprom.VfoInfo[vfo].pRX->Frequency != lastFreq[vfo]) { + lastFreq[vfo] = gEeprom.VfoInfo[vfo].pRX->Frequency; + AM_fix_reset(vfo); + } + int16_t rssi; { // sample the current RSSI level // average it with the previous rssi (a bit of noise/spike immunity) @@ -348,7 +357,7 @@ void AM_fix_10ms(const unsigned vfo, bool force) // remember the new table index gain_table_index_prev[vfo] = index; - + currentGainDiff = gain_table[original_index].gain_dB - gain_table[index].gain_dB; BK4819_WriteRegister(BK4819_REG_13, gain_table[index].reg_val); } @@ -370,5 +379,13 @@ void AM_fix_print_data(const unsigned vfo, char *s) { } #endif +int8_t AM_fix_get_gain_diff() +{ + return currentGainDiff; +} +void AM_fix_enable(bool on) +{ + enabled = on; +} #endif diff --git a/am_fix.h b/am_fix.h index 569893703..562aec473 100644 --- a/am_fix.h +++ b/am_fix.h @@ -22,11 +22,14 @@ #ifdef ENABLE_AM_FIX void AM_fix_init(void); + void AM_fix_reset(const unsigned vfo); void AM_fix_10ms(const unsigned vfo, bool force); #ifdef ENABLE_AM_FIX_SHOW_DATA void AM_fix_print_data(const unsigned vfo, char *s); #endif - + int8_t AM_fix_get_gain_diff(); + void AM_fix_enable(bool on); + #endif #endif diff --git a/app/app.c b/app/app.c index 07f4c2431..e5378ecf0 100644 --- a/app/app.c +++ b/app/app.c @@ -1121,7 +1121,7 @@ void APP_TimeSlice10ms(void) #endif #ifdef ENABLE_AM_FIX - if (gRxVfo->Modulation == MODULATION_AM && gSetting_AM_fix) + if (gRxVfo->Modulation == MODULATION_AM) AM_fix_10ms(gEeprom.RX_VFO, false); #endif diff --git a/app/spectrum.c b/app/spectrum.c index b6cf931af..ca542f6ea 100644 --- a/app/spectrum.c +++ b/app/spectrum.c @@ -113,8 +113,7 @@ static uint16_t GetRegMenuValue(uint8_t st) { void LockAGC() { - if(!lockAGC) - BK4819_SetAGC(0); + RADIO_SetupAGC(true, lockAGC); lockAGC = true; } @@ -492,13 +491,6 @@ static void ToggleModulation() { } RADIO_SetModulation(settings.modulationType); -#ifdef ENABLE_AM_FIX - if(gSetting_AM_fix && settings.modulationType != MODULATION_AM) { - BK4819_InitAGC(false); - BK4819_SetAGC(1); - } -#endif - RelaunchScan(); redrawScreen = true; } @@ -1178,13 +1170,6 @@ void APP_RunSpectrum() { ToggleRX(true), ToggleRX(false); // hack to prevent noise when squelch off RADIO_SetModulation(settings.modulationType = gRxVfo->Modulation); -#ifdef ENABLE_AM_FIX - if(settings.modulationType != MODULATION_AM) { - BK4819_InitAGC(false); - BK4819_SetAGC(true); - } -#endif - BK4819_SetFilterBandwidth(settings.listenBw = BK4819_FILTER_BW_WIDE, false); RelaunchScan(); diff --git a/radio.c b/radio.c index be76635fb..eaacfa528 100644 --- a/radio.c +++ b/radio.c @@ -772,12 +772,7 @@ void RADIO_SetupRegisters(bool switchToForeground) } #endif -#ifdef ENABLE_AM_FIX - if(gSetting_AM_fix) { - BK4819_SetAGC(true); - BK4819_InitAGC(false); - } -#endif + RADIO_SetupAGC(false, false); // enable/disable BK4819 selected interrupts BK4819_WriteRegister(BK4819_REG_3F, InterruptMask); @@ -926,12 +921,36 @@ void RADIO_SetModulation(ModulationMode_t modulation) BK4819_SetRegValue(afDacGainRegSpec, 0xF); BK4819_WriteRegister(BK4819_REG_3D, modulation == MODULATION_USB ? 0 : 0x2AAB); BK4819_SetRegValue(afcDisableRegSpec, modulation != MODULATION_FM); -#ifdef ENABLE_AM_FIX - if(modulation == MODULATION_AM && gSetting_AM_fix) { - BK4819_SetAGC(0); + + RADIO_SetupAGC(modulation == MODULATION_AM, false); +} + +void RADIO_SetupAGC(bool listeningAM, bool disable) +{ + static uint8_t lastSettings; + uint8_t newSettings = (listeningAM << 1) | (disable << 1); + if(lastSettings == newSettings) + return; + lastSettings = newSettings; + + + if(!listeningAM) { // if not actively listening AM we don't need any AM specific regulation + BK4819_SetAGC(1); + BK4819_InitAGC(false); } + else { +#ifdef ENABLE_AM_FIX + if(gSetting_AM_fix) { // if AM fix active lock AGC so AM-fix can do it's job + BK4819_SetAGC(0); + AM_fix_enable(!disable); + } + else #endif - BK4819_InitAGC(modulation == MODULATION_AM); + { + BK4819_SetAGC(!disable); + BK4819_InitAGC(true); + } + } } void RADIO_SetVfoState(VfoState_t State) @@ -1149,4 +1168,4 @@ void RADIO_SendEndOfTransmission(void) } BK4819_ExitDTMF_TX(true); -} +} \ No newline at end of file diff --git a/radio.h b/radio.h index 9810d4bfc..64429c402 100644 --- a/radio.h +++ b/radio.h @@ -159,6 +159,7 @@ void RADIO_SetupRegisters(bool bSwitchToFunction0); void RADIO_ConfigureNOAA(void); #endif void RADIO_SetTxParameters(void); +void RADIO_SetupAGC(bool listeningAM, bool disable); void RADIO_SetModulation(ModulationMode_t modulation); void RADIO_SetVfoState(VfoState_t State); void RADIO_PrepareTX(void); diff --git a/ui/main.c b/ui/main.c index dbe8f2d1e..faf4d64bb 100644 --- a/ui/main.c +++ b/ui/main.c @@ -189,7 +189,13 @@ void DisplayRSSIBar(const bool now) const int16_t s0_dBm = -130; // S0 .. base level - const int16_t rssi_dBm = BK4819_GetRSSI_dBm() + dBmCorrTable[gRxVfo->Band]; + const int16_t rssi_dBm = + BK4819_GetRSSI_dBm() +#ifdef ENABLE_AM_FIX + + (gSetting_AM_fix ? AM_fix_get_gain_diff() : 0) +#endif + + dBmCorrTable[gRxVfo->Band]; + const uint8_t s_level = MIN(MAX((rssi_dBm - s0_dBm) / 6, 0), 9); // S0 - S9 uint8_t overS9dBm = MIN(MAX(rssi_dBm - (s0_dBm + 9*6), 0), 99);