Skip to content

Commit

Permalink
Real AM fix attempt, messy
Browse files Browse the repository at this point in the history
  • Loading branch information
egzumer committed Dec 5, 2023
1 parent 52bdd40 commit e95f577
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 158 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 := 1
ENABLE_AM_FIX := 0
ENABLE_SQUELCH_MORE_SENSITIVE := 1
ENABLE_FASTER_CHANNEL_SCAN := 1
ENABLE_RSSI_BAR := 1
Expand Down
27 changes: 5 additions & 22 deletions am_fix.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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 = 90;
static const unsigned int original_index = 85;

#ifdef ENABLE_AM_FIX_SHOW_DATA
// display update rate
Expand All @@ -206,9 +206,6 @@ int16_t prev_rssi[2] = {0, 0};
// to help reduce gain hunting, peak hold count down tick
unsigned int hold_counter[2] = {0, 0};

// used to correct the RSSI readings after our RF gain adjustments
int16_t rssi_gain_diff[2] = {0, 0};

// used to limit the max RF gain
const unsigned max_index = ARRAY_SIZE(gain_table) - 1;

Expand All @@ -233,7 +230,6 @@ void AM_fix_reset(const unsigned vfo)

prev_rssi[vfo] = 0;
hold_counter[vfo] = 0;
rssi_gain_diff[vfo] = 0;
gain_table_index_prev[vfo] = 0;
}

Expand Down Expand Up @@ -287,9 +283,10 @@ void AM_fix_10ms(const unsigned vfo, bool force)

#ifdef ENABLE_AM_FIX_SHOW_DATA
{
int16_t new_rssi = rssi - rssi_gain_diff[vfo];
if (gCurrentRSSI[vfo] != new_rssi) { // rssi changed
gCurrentRSSI[vfo] = new_rssi;
static int16_t lastRssi;

if (lastRssi != rssi) { // rssi changed
lastRssi = rssi;

if (counter == 0) {
counter = 1;
Expand Down Expand Up @@ -353,16 +350,8 @@ void AM_fix_10ms(const unsigned vfo, bool force)
gain_table_index_prev[vfo] = index;

BK4819_WriteRegister(BK4819_REG_13, gain_table[index].reg_val);

// offset the RSSI reading to the rest of the firmware to cancel out the gain adjustments we make

// RF gain difference from original QS setting
rssi_gain_diff[vfo] = ((int16_t)gain_table[index].gain_dB - gain_table[original_index].gain_dB) * 2;
}

// save the corrected RSSI level
gCurrentRSSI[vfo] = rssi - rssi_gain_diff[vfo];

#ifdef ENABLE_AM_FIX_SHOW_DATA
if (counter == 0) {
counter = 1;
Expand All @@ -381,11 +370,5 @@ void AM_fix_print_data(const unsigned vfo, char *s) {
}
#endif

int16_t AM_fix_get_rssi_gain_diff(const unsigned vfo)
{
if(vfo > 1)
return 0;
return rssi_gain_diff[vfo];
}

#endif
1 change: 0 additions & 1 deletion am_fix.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#ifdef ENABLE_AM_FIX_SHOW_DATA
void AM_fix_print_data(const unsigned vfo, char *s);
#endif
int16_t AM_fix_get_rssi_gain_diff(const unsigned vfo);

#endif

Expand Down
47 changes: 4 additions & 43 deletions app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,11 @@
#include "ui/status.h"
#include "ui/ui.h"

#include "debugging.h"

static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld);
static void FlashlightTimeSlice();

static void UpdateRSSI(const int vfo)
{
int16_t rssi = BK4819_GetRSSI();

#ifdef ENABLE_AM_FIX
// add RF gain adjust compensation
if (gEeprom.VfoInfo[vfo].Modulation == MODULATION_AM && gSetting_AM_fix)
rssi -= AM_fix_get_rssi_gain_diff(vfo);
#endif

if (gCurrentRSSI[vfo] == rssi)
return; // no change

gCurrentRSSI[vfo] = rssi;

UI_UpdateRSSI(rssi, vfo);
}

static void CheckForIncoming(void)
{
if (!g_SquelchLost)
Expand All @@ -108,9 +92,6 @@ static void CheckForIncoming(void)
{
FUNCTION_Select(FUNCTION_INCOMING);
//gUpdateDisplay = true;

UpdateRSSI(gEeprom.RX_VFO);
gUpdateRSSI = true;
}

return;
Expand All @@ -124,9 +105,6 @@ static void CheckForIncoming(void)
{
FUNCTION_Select(FUNCTION_INCOMING);
//gUpdateDisplay = true;

UpdateRSSI(gEeprom.RX_VFO);
gUpdateRSSI = true;
}
return;
}
Expand All @@ -146,9 +124,6 @@ static void CheckForIncoming(void)
{
FUNCTION_Select(FUNCTION_INCOMING);
//gUpdateDisplay = true;

UpdateRSSI(gEeprom.RX_VFO);
gUpdateRSSI = true;
}
return;
}
Expand All @@ -163,9 +138,6 @@ static void CheckForIncoming(void)
{
FUNCTION_Select(FUNCTION_INCOMING);
//gUpdateDisplay = true;

UpdateRSSI(gEeprom.RX_VFO);
gUpdateRSSI = true;
}
}

Expand Down Expand Up @@ -994,8 +966,6 @@ void APP_Update(void)
!gCssBackgroundScan)
{ // dual watch mode, toggle between the two VFO's
DualwatchAlternate();

gUpdateRSSI = false;
}

FUNCTION_Init();
Expand All @@ -1004,11 +974,8 @@ void APP_Update(void)
gRxIdleMode = false; // RX is awake
}
else
if (gEeprom.DUAL_WATCH == DUAL_WATCH_OFF || gScanStateDir != SCAN_OFF || gCssBackgroundScan || gUpdateRSSI)
if (gEeprom.DUAL_WATCH == DUAL_WATCH_OFF || gScanStateDir != SCAN_OFF || gCssBackgroundScan)
{ // dual watch mode off or scanning or rssi update request

UpdateRSSI(gEeprom.RX_VFO);

// go back to sleep

gPowerSave_10ms = gEeprom.BATTERY_SAVE * 10;
Expand All @@ -1021,12 +988,9 @@ void APP_Update(void)
// Authentic device checked removed

}
else
{
else {
// toggle between the two VFO's
DualwatchAlternate();

gUpdateRSSI = true;
gPowerSave_10ms = power_save1_10ms;
}

Expand Down Expand Up @@ -1530,9 +1494,6 @@ void APP_TimeSlice500ms(void)
}
}

if (gCurrentFunction != FUNCTION_POWER_SAVE && gCurrentFunction != FUNCTION_TRANSMIT)
UpdateRSSI(gEeprom.RX_VFO);

if (!gPttIsPressed && gVFOStateResumeCountdown_500ms > 0)
{
if (--gVFOStateResumeCountdown_500ms == 0)
Expand Down
18 changes: 9 additions & 9 deletions app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,7 @@ uint16_t GetRssi() {
SYSTICK_DelayUs(100);
}

return BK4819_GetRSSI()
#ifdef ENABLE_AM_FIX
- ((settings.modulationType == MODULATION_AM) ? AM_fix_get_rssi_gain_diff(vfo) : 0)
#endif
;
return BK4819_GetRSSI();
}

static void ToggleAudio(bool on) {
Expand Down Expand Up @@ -495,10 +491,14 @@ static void ToggleModulation() {
settings.modulationType = MODULATION_FM;
}
RADIO_SetModulation(settings.modulationType);
if(settings.modulationType != MODULATION_AM) {
BK4819_InitAGC();

#ifdef ENABLE_AM_FIX
if(gSetting_AM_fix && settings.modulationType != MODULATION_AM) {
BK4819_InitAGC(false);
BK4819_SetAGC(1);
}
#endif

RelaunchScan();
redrawScreen = true;
}
Expand Down Expand Up @@ -1180,8 +1180,8 @@ void APP_RunSpectrum() {

#ifdef ENABLE_AM_FIX
if(settings.modulationType != MODULATION_AM) {
BK4819_InitAGC();
BK4819_SetAGC(1);
BK4819_InitAGC(false);
BK4819_SetAGC(true);
}
#endif

Expand Down
4 changes: 3 additions & 1 deletion debugging.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "driver/bk4819.h"
#include "string.h"
#include "external/printf/printf.h"
#include "am_fix.h"


static inline void LogUart(char * str)
Expand All @@ -26,7 +27,8 @@ static inline void LogPrint()
uint16_t rssi = BK4819_GetRSSI();
uint16_t reg7e = BK4819_ReadRegister(0x7E);
char buf[32];
sprintf(buf, "reg7E: %d %2d %6d %2d %d rssi: %d\n", (reg7e >> 15), (reg7e >> 12) & 0b111, (reg7e >> 5) & 0b1111111, (reg7e >> 2) & 0b111, (reg7e >> 0) & 0b11, rssi);
sprintf(buf, "reg7E: %d %2d %6d %2d %d rssi: %d\n",
(reg7e >> 15), (reg7e >> 12) & 0b111, (reg7e >> 5) & 0b1111111, (reg7e >> 2) & 0b111, (reg7e >> 0) & 0b11, rssi);
LogUart(buf);
}

Expand Down
51 changes: 47 additions & 4 deletions driver/bk4819.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void BK4819_Init(void)
BK4819_WriteRegister(BK4819_REG_37, 0x1D0F);
BK4819_WriteRegister(BK4819_REG_36, 0x0022);

BK4819_InitAGC();
BK4819_InitAGC(false);
BK4819_SetAGC(true);

BK4819_WriteRegister(BK4819_REG_19, 0b0001000001000001); // <15> MIC AGC 1 = disable 0 = enable
Expand Down Expand Up @@ -263,7 +263,7 @@ void BK4819_SetAGC(bool enable)
// }
}

void BK4819_InitAGC()
void BK4819_InitAGC(bool amModulation)
{
// REG_10, REG_11, REG_12 REG_13, REG_14
//
Expand Down Expand Up @@ -308,13 +308,56 @@ void BK4819_InitAGC()
BK4819_WriteRegister(BK4819_REG_12, 0x037B); // 0x037B / 000000 11 011 11 011 / -24dB
BK4819_WriteRegister(BK4819_REG_11, 0x027B); // 0x027B / 000000 10 011 11 011 / -43dB
BK4819_WriteRegister(BK4819_REG_10, 0x007A); // 0x007A / 000000 00 011 11 010 / -58dB
BK4819_WriteRegister(BK4819_REG_14, 0x0019); // 0x0019 / 000000 00 000 11 001 / -79dB
BK4819_WriteRegister(BK4819_REG_49, (0 << 14) | (84 << 7) | (56 << 0)); //0x2A38 / 00 1010100 0111000 / 84, 56
if(amModulation) {
BK4819_WriteRegister(BK4819_REG_14, 0x0000);
BK4819_WriteRegister(BK4819_REG_49, (0 << 14) | (50 << 7) | (32 << 0));
}
else{
BK4819_WriteRegister(BK4819_REG_14, 0x0019); // 0x0019 / 000000 00 000 11 001 / -79dB
BK4819_WriteRegister(BK4819_REG_49, (0 << 14) | (84 << 7) | (56 << 0)); //0x2A38 / 00 1010100 0111000 / 84, 56
}

BK4819_WriteRegister(BK4819_REG_7B, 0x8420);

}

int8_t BK4819_GetRxGain_dB(void)
{
union {
struct {
uint16_t pga:3;
uint16_t mixer:2;
uint16_t lna:3;
uint16_t lnaS:2;
};
uint16_t __raw;
} agcGainReg;

union {
struct {
uint16_t _ : 5;
uint16_t agcSigStrength : 7;
int16_t gainIdx : 3;
uint16_t agcEnab : 1;
};
uint16_t __raw;
} reg7e;

reg7e.__raw = BK4819_ReadRegister(BK4819_REG_7E);
uint8_t gainAddr = reg7e.gainIdx < 0 ? BK4819_REG_14 : BK4819_REG_10 + reg7e.gainIdx;
agcGainReg.__raw = BK4819_ReadRegister(gainAddr);
int8_t lnaShortTab[] = {-28, -24, -19, 0};
int8_t lnaTab[] = {-24, -19, -14, -9, -6, -4, -2, 0};
int8_t mixerTab[] = {-8, -6, -3, 0};
int8_t pgaTab[] = {-33, -27, -21, -15, -9, -6, -3, 0};
return lnaShortTab[agcGainReg.lnaS] + lnaTab[agcGainReg.lna] + mixerTab[agcGainReg.mixer] + pgaTab[agcGainReg.pga];
}

int16_t BK4819_GetRSSI_dBm(void)
{
uint16_t rssi = BK4819_GetRSSI();
return (rssi / 2) - 160;// - BK4819_GetRxGain_dB();
}

void BK4819_ToggleGpioOut(BK4819_GPIO_PIN_t Pin, bool bSet)
{
Expand Down
4 changes: 3 additions & 1 deletion driver/bk4819.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void BK4819_WriteU8(uint8_t Data);
void BK4819_WriteU16(uint16_t Data);

void BK4819_SetAGC(bool enable);
void BK4819_InitAGC();
void BK4819_InitAGC(bool amModulation);

void BK4819_ToggleGpioOut(BK4819_GPIO_PIN_t Pin, bool bSet);

Expand Down Expand Up @@ -136,6 +136,8 @@ void BK4819_EnableCDCSS(void);
void BK4819_EnableCTCSS(void);

uint16_t BK4819_GetRSSI(void);
int8_t BK4819_GetRxGain_dB(void);
int16_t BK4819_GetRSSI_dBm(void);
uint8_t BK4819_GetGlitchIndicator(void);
uint8_t BK4819_GetExNoiceIndicator(void);
uint16_t BK4819_GetVoiceAmplitudeOut(void);
Expand Down
3 changes: 0 additions & 3 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ bool gCssBackgroundScan;
volatile bool gScheduleScanListen = true;
volatile uint16_t gScanPauseDelayIn_10ms;

bool gUpdateRSSI;
#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750)
AlarmState_t gAlarmState;
#endif
Expand Down Expand Up @@ -250,8 +249,6 @@ volatile bool gFlagTailNoteEliminationComplete;

volatile uint8_t boot_counter_10ms;

int16_t gCurrentRSSI[2] = {0, 0}; // now one per VFO

uint8_t gIsLocked = 0xFF;


Expand Down
2 changes: 0 additions & 2 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ enum
extern volatile bool gScheduleScanListen;
extern volatile uint16_t gScanPauseDelayIn_10ms;

extern bool gUpdateRSSI;
extern AlarmState_t gAlarmState;
extern uint16_t gMenuCountdown;
extern bool gPttWasReleased;
Expand Down Expand Up @@ -332,7 +331,6 @@ extern volatile uint8_t gVFOStateResumeCountdown_500ms;
#ifdef ENABLE_FMRADIO
extern volatile bool gScheduleFM;
#endif
extern int16_t gCurrentRSSI[2]; // now one per VFO
extern uint8_t gIsLocked;
extern volatile uint8_t boot_counter_10ms;

Expand Down
Loading

0 comments on commit e95f577

Please sign in to comment.