Refactor cleanup

This commit is contained in:
Krzysiek Egzmont
2023-11-30 23:54:26 +01:00
parent 292e2e7e7e
commit c341570838
10 changed files with 358 additions and 472 deletions

View File

@@ -319,9 +319,6 @@ endif
ifeq ($(ENABLE_AM_FIX_SHOW_DATA),1)
CFLAGS += -DENABLE_AM_FIX_SHOW_DATA
endif
ifeq ($(ENABLE_AM_FIX_TEST1),1)
CFLAGS += -DENABLE_AM_FIX_TEST1
endif
ifeq ($(ENABLE_SQUELCH_MORE_SENSITIVE),1)
CFLAGS += -DENABLE_SQUELCH_MORE_SENSITIVE
endif

312
am_fix.c
View File

@@ -32,87 +32,72 @@
#ifdef ENABLE_AM_FIX
typedef struct
{
typedef struct
{
uint16_t reg_val;
int8_t gain_dB;
} __attribute__((packed)) t_gain_table;
} __attribute__((packed)) t_gain_table;
// REG_10 AGC gain table
//
// <15:10> ???
//
// <9:8> = LNA Gain Short
// 3 = 0dB < original value
// 2 = -24dB // was -11
// 1 = -30dB // was -16
// 0 = -33dB // was -19
//
// <7:5> = LNA Gain
// 7 = 0dB
// 6 = -2dB
// 5 = -4dB < original value
// 4 = -6dB
// 3 = -9dB
// 2 = -14dB
// 1 = -19dB
// 0 = -24dB
//
// <4:3> = MIXER Gain
// 3 = 0dB < original value
// 2 = -3dB
// 1 = -6dB
// 0 = -8dB
//
// <2:0> = PGA Gain
// 7 = 0dB
// 6 = -3dB < original value
// 5 = -6dB
// 4 = -9dB
// 3 = -15dB
// 2 = -21dB
// 1 = -27dB
// 0 = -33dB
// REG_10 AGC gain table
//
// <15:10> ???
//
// <9:8> = LNA Gain Short
// 3 = 0dB < original value
// 2 = -24dB // was -11
// 1 = -30dB // was -16
// 0 = -33dB // was -19
//
// <7:5> = LNA Gain
// 7 = 0dB
// 6 = -2dB
// 5 = -4dB < original value
// 4 = -6dB
// 3 = -9dB
// 2 = -14dB
// 1 = -19dB
// 0 = -24dB
//
// <4:3> = MIXER Gain
// 3 = 0dB < original value
// 2 = -3dB
// 1 = -6dB
// 0 = -8dB
//
// <2:0> = PGA Gain
// 7 = 0dB
// 6 = -3dB < original value
// 5 = -6dB
// 4 = -9dB
// 3 = -15dB
// 2 = -21dB
// 1 = -27dB
// 0 = -33dB
// front end register dB values
//
// these values need to be accurate for the code to properly/reliably switch
// between table entries when adjusting the front end registers.
//
// these 4 tables need a measuring/calibration update
//
//
// QUESTION: why do I have to surround the negative numbers in brackets ???
// if I don't add the brackets, reading the table returns unexpected/different values !!!
//
//
// front end register dB values
//
// these values need to be accurate for the code to properly/reliably switch
// between table entries when adjusting the front end registers.
//
// these 4 tables need a measuring/calibration update
//
//
// QUESTION: why do I have to surround the negative numbers in brackets ???
// if I don't add the brackets, reading the table returns unexpected/different values !!!
//
//
//// static const int16_t lna_short_dB[] = { -19, -16, -11, 0}; // was (but wrong)
// static const int16_t lna_short_dB[] = { (-33), (-30), (-24), 0}; // corrected'ish
// static const int16_t lna_dB[] = { (-24), (-19), (-14), ( -9), (-6), (-4), (-2), 0};
// static const int16_t mixer_dB[] = { ( -8), ( -6), ( -3), 0};
// static const int16_t pga_dB[] = { (-33), (-27), (-21), (-15), (-9), (-6), (-3), 0};
// lookup table is hugely easier than writing code to do the same
//
static const t_gain_table gain_table[] =
{
// lookup table is hugely easier than writing code to do the same
//
static const t_gain_table gain_table[] =
{
{0x035E, -17}, // 0 .. 3 2 3 6 .. 0dB -14dB 0dB -3dB .. -17dB original
#ifdef ENABLE_AM_FIX_TEST1
// test table that lets me manually set the lna-short register
// to measure it's actual dB change using an RF signal generator
{0x005E, -50}, // 1 .. 0 2 3 6 .. -33dB -14dB 0dB -3dB .. -50dB
{0x015E, -47}, // 2 .. 1 2 3 6 .. -30dB -14dB 0dB -3dB .. -47dB
{0x025E, -41}, // 3 .. 2 2 3 6 .. -24dB -14dB 0dB -3dB .. -41dB
{0x035E, -17} // 4 .. 3 2 3 6 .. 0dB -14dB 0dB -3dB .. -17dB original
};
static const unsigned int original_index = 1;
#else
{0x0000, -98}, // 1 .. 0 0 0 0 .. -33dB -24dB -8dB -33dB .. -98dB
{0x0008, -96}, // 2 .. 0 0 1 0 .. -33dB -24dB -6dB -33dB .. -96dB
{0x0100, -95}, // 3 .. 1 0 0 0 .. -30dB -24dB -8dB -33dB .. -95dB
@@ -209,107 +194,68 @@
{0x03F7, -3 }, // 94 .. 3 7 2 7 .. 0dB 0dB -3dB 0dB .. -3dB
{0x03DF, -2 }, // 95 .. 3 6 3 7 .. 0dB -2dB 0dB 0dB .. -2dB
{0x03FF, 0 }, // 96 .. 3 7 3 7 .. 0dB 0dB 0dB 0dB .. 0dB
};
};
static const unsigned int original_index = 90;
static const unsigned int original_index = 90;
#endif
#ifdef ENABLE_AM_FIX_SHOW_DATA
#ifdef ENABLE_AM_FIX_SHOW_DATA
// display update rate
static const unsigned int display_update_rate = 250 / 10; // max 250ms display update rate
unsigned int counter = 0;
#endif
#endif
#ifdef ENABLE_AM_FIX_TEST1
// user manually sets the table index .. used to calibrate the desired dB gain table
unsigned int gain_table_index[2] = {1 + gSetting_AM_fix_test1, 1 + gSetting_AM_fix_test1};
#else
unsigned int gain_table_index[2] = {original_index, original_index};
#endif
unsigned int gain_table_index[2] = {original_index, original_index};
// used simply to detect a changed gain setting
unsigned int gain_table_index_prev[2] = {0, 0};
// holds the previous RSSI level .. we do an average of old + new RSSI reading
int16_t prev_rssi[2] = {0, 0};
// used simply to detect a changed gain setting
unsigned int gain_table_index_prev[2] = {0, 0};
// to help reduce gain hunting, peak hold count down tick
unsigned int hold_counter[2] = {0, 0};
// holds the previous RSSI level .. we do an average of old + new RSSI reading
int16_t prev_rssi[2] = {0, 0};
// used to correct the RSSI readings after our RF gain adjustments
int16_t rssi_gain_diff[2] = {0, 0};
// to help reduce gain hunting, peak hold count down tick
unsigned int hold_counter[2] = {0, 0};
// used to limit the max RF gain
unsigned int max_index = ARRAY_SIZE(gain_table) - 1;
// used to correct the RSSI readings after our RF gain adjustments
int16_t rssi_gain_diff[2] = {0, 0};
#ifndef ENABLE_AM_FIX_TEST1
// -89dBm, any higher and the AM demodulator starts to saturate/clip/distort
const int16_t desired_rssi = (-89 + 160) * 2;
#endif
// used to limit the max RF gain
unsigned int max_index = ARRAY_SIZE(gain_table) - 1;
void AM_fix_init(void)
{ // called at boot-up
// -89dBm, any higher and the AM demodulator starts to saturate/clip/distort
const int16_t desired_rssi = (-89 + 160) * 2;
unsigned int i;
for (i = 0; i < 2; i++)
{
#ifdef ENABLE_AM_FIX_TEST1
gain_table_index[i] = 1 + gSetting_AM_fix_test1;
#else
void AM_fix_init(void)
{ // called at boot-up
for (int i = 0; i < 2; i++) {
gain_table_index[i] = original_index; // re-start with original QS setting
#endif
}
#if 0
{ // set a maximum gain to use
// const int16_t max_gain_dB = gain_dB[original_index];
const int16_t max_gain_dB = -10;
max_index = ARRAY_SIZE(gain_table);
while (--max_index > 1)
// if (gain_dB[max_index] <= max_gain_dB)
if (gain_table[max_index].gain_dB <= max_gain_dB)
break;
}
#else
// use the full range of available gains
max_index = ARRAY_SIZE(gain_table) - 1;
#endif
}
void AM_fix_reset(const int vfo)
{ // reset the AM fixer upper
}
void AM_fix_reset(const int vfo)
{ // reset the AM fixer upper
#ifdef ENABLE_AM_FIX_SHOW_DATA
counter = 0;
#endif
prev_rssi[vfo] = 0;
hold_counter[vfo] = 0;
rssi_gain_diff[vfo] = 0;
#ifdef ENABLE_AM_FIX_TEST1
// gain_table_index[vfo] = 1 + gSetting_AM_fix_test1;
#else
// gain_table_index[vfo] = original_index; // re-start with original QS setting
#endif
gain_table_index_prev[vfo] = 0;
}
}
// adjust the RX gain to try and prevent the AM demodulator from
// saturating/overloading/clipping (distorted AM audio)
//
// we're actually doing the BK4819's job for it here, but as the chip
// won't/don't do it for itself, we're left to bodging it ourself by
// playing with the RF front end gain setting
//
void AM_fix_10ms(const int vfo)
{
// adjust the RX gain to try and prevent the AM demodulator from
// saturating/overloading/clipping (distorted AM audio)
//
// we're actually doing the BK4819's job for it here, but as the chip
// won't/don't do it for itself, we're left to bodging it ourself by
// playing with the RF front end gain setting
//
void AM_fix_10ms(const int vfo)
{
int16_t diff_dB;
int16_t rssi;
@@ -331,16 +277,14 @@
break;
}
#ifdef ENABLE_AM_FIX_SHOW_DATA
if (counter > 0)
{
if (++counter >= display_update_rate)
{ // trigger a display update
#ifdef ENABLE_AM_FIX_SHOW_DATA
if (counter > 0) {
if (++counter >= display_update_rate) { // trigger a display update
counter = 0;
gUpdateDisplay = true;
}
}
#endif
#endif
{ // sample the current RSSI level
// average it with the previous rssi (a bit of noise/spike immunity)
@@ -350,38 +294,23 @@
}
// save the corrected RSSI level
#ifdef ENABLE_AM_FIX_SHOW_DATA
#ifdef ENABLE_AM_FIX_SHOW_DATA
{
const int16_t new_rssi = rssi - rssi_gain_diff[vfo];
if (gCurrentRSSI[vfo] != new_rssi)
{
if (gCurrentRSSI[vfo] != new_rssi) {
gCurrentRSSI[vfo] = new_rssi;
if (counter == 0)
{ // trigger a display update
if (counter == 0) {
counter = 1;
gUpdateDisplay = true;
gUpdateDisplay = true; // trigger a display update
}
}
}
#else
gCurrentRSSI[vfo] = rssi - rssi_gain_diff[vfo];
#endif
#ifdef ENABLE_AM_FIX_TEST1
// user is manually adjusting a gain register - don't do anything automatically
{
int i = 1 + (int)gSetting_AM_fix_test1;
i = (i < 1) ? 1 : (i > ((int)ARRAY_SIZE(gain_table) - 1) ? ARRAY_SIZE(gain_table) - 1 : i;
if (gain_table_index[vfo] == i)
return; // no change
gain_table_index[vfo] = i;
}
#else
gCurrentRSSI[vfo] = rssi - rssi_gain_diff[vfo];
#endif
// automatically adjust the RF RX gain
// update the gain hold counter
@@ -391,13 +320,10 @@
// dB difference between actual and desired RSSI level
diff_dB = (rssi - desired_rssi) / 2;
if (diff_dB > 0)
{ // decrease gain
if (diff_dB > 0) { // decrease gain
unsigned int index = gain_table_index[vfo]; // current position we're at
if (diff_dB >= 10)
{ // jump immediately to a new gain setting
if (diff_dB >= 10) { // jump immediately to a new gain setting
// this greatly speeds up initial gain reduction (but reduces noise/spike immunity)
const int16_t desired_gain_dB = (int16_t)gain_table[index].gain_dB - diff_dB + 8; // get no closer than 8dB (bit of noise/spike immunity)
@@ -406,15 +332,9 @@
while (index > 1)
if (gain_table[--index].gain_dB <= desired_gain_dB)
break;
//index = (gain_table_index[vfo] + index) / 2; // easy does it
}
else
{ // incrementally reduce the gain .. taking it slow improves noise/spike immunity
// if (index >= (1 + 3) && diff_dB >= 3)
// index -= 3; // faster gain reduction
// else
if (index > 1)
index--; // slow step-by-step gain reduction
}
@@ -437,12 +357,6 @@
gain_table_index[vfo] = (index <= max_index) ? index : max_index; // limit the gain index
}
#if 0
if (gain_table_index[vfo] == gain_table_index_prev[vfo])
return; // no gain change - this is to reduce writing to the BK chip on ever call
#endif
#endif
{ // apply the new settings to the front end registers
@@ -462,28 +376,24 @@
// save the corrected RSSI level
gCurrentRSSI[vfo] = rssi - rssi_gain_diff[vfo];
#ifdef ENABLE_AM_FIX_SHOW_DATA
if (counter == 0)
{
#ifdef ENABLE_AM_FIX_SHOW_DATA
if (counter == 0) {
counter = 1;
gUpdateDisplay = true;
}
#endif
}
#endif
}
#ifdef ENABLE_AM_FIX_SHOW_DATA
#ifdef ENABLE_AM_FIX_SHOW_DATA
void AM_fix_print_data(const int vfo, char *s)
{
if (s != NULL && vfo >= 0 && vfo < (int)ARRAY_SIZE(gain_table_index))
{
void AM_fix_print_data(const int vfo, char *s) {
if (s != NULL && vfo >= 0 && vfo < (int)ARRAY_SIZE(gain_table_index)) {
const unsigned int index = gain_table_index[vfo];
// sprintf(s, "%2u.%u %4ddB %3u", index, ARRAY_SIZE(gain_table) - 1, gain_table[index].gain_dB, prev_rssi[vfo]);
sprintf(s, "%2u %4ddB %3u", index, gain_table[index].gain_dB, prev_rssi[vfo]);
counter = 0;
}
}
#endif
}
#endif
#endif

View File

@@ -224,13 +224,6 @@ int MENU_GetLimits(uint8_t menu_id, int32_t *pMin, int32_t *pMax)
*pMax = ARRAY_SIZE(gSubMenu_RX_TX) - 1;
break;
#ifdef ENABLE_AM_FIX_TEST1
case MENU_AM_FIX_TEST1:
*pMin = 0;
*pMax = ARRAY_SIZE(gSubMenu_AM_fix_test1) - 1;
break;
#endif
#ifdef ENABLE_AM_FIX
case MENU_AM_FIX:
#endif
@@ -740,14 +733,6 @@ void MENU_AcceptSetting(void)
break;
#endif
#ifdef ENABLE_AM_FIX_TEST1
case MENU_AM_FIX_TEST1:
gSetting_AM_fix_test1 = gSubMenuSelection;
gVfoConfigureMode = VFO_CONFIGURE_RELOAD;
gFlagResetVfos = true;
break;
#endif
#ifdef ENABLE_NOAA
case MENU_NOAA_S:
gEeprom.NOAA_AUTO_SCAN = gSubMenuSelection;
@@ -1124,13 +1109,6 @@ void MENU_ShowCurrentSetting(void)
gSubMenuSelection = gSetting_AM_fix;
break;
#endif
#ifdef ENABLE_AM_FIX_TEST1
case MENU_AM_FIX_TEST1:
gSubMenuSelection = gSetting_AM_fix_test1;
break;
#endif
#ifdef ENABLE_NOAA
case MENU_NOAA_S:
gSubMenuSelection = gEeprom.NOAA_AUTO_SCAN;

View File

@@ -2,6 +2,7 @@
#define DEBUGGING_H
#include "driver/uart.h"
#include "driver/bk4819.h"
#include "string.h"
#include "external/printf/printf.h"
@@ -11,6 +12,13 @@ static inline void LogUart(char * str)
UART_Send(str, strlen(str));
}
static inline void LogRegUart(uint16_t reg)
{
uint16_t regVal = BK4819_ReadRegister(reg);
char buf[32];
sprintf(buf, "reg%02X: %04X\n", reg, regVal);
LogUart(buf);
}

View File

@@ -41,10 +41,15 @@ enum BK4819_REGISTER_t {
BK4819_REG_0C = 0x0CU,
BK4819_REG_0D = 0x0DU,
BK4819_REG_0E = 0x0EU,
// RX AGC Gain Table[0]
BK4819_REG_10 = 0x10U,
// RX AGC Gain Table[1]
BK4819_REG_11 = 0x11U,
// RX AGC Gain Table[2]
BK4819_REG_12 = 0x12U,
// RX AGC Gain Table[3]
BK4819_REG_13 = 0x13U,
// RX AGC Gain Table[-1]
BK4819_REG_14 = 0x14U,
BK4819_REG_19 = 0x19U,
BK4819_REG_1F = 0x1FU,
@@ -72,6 +77,12 @@ enum BK4819_REGISTER_t {
BK4819_REG_46 = 0x46U,
BK4819_REG_47 = 0x47U,
BK4819_REG_48 = 0x48U,
// REG_49<15:14> 0b00; High/Low Lo selection:
// 0X: Auto High/Low Lo
// 10: Low Lo
// 11: High Lo
// REG_49<13:7> 0x50; RF AGC high threshold, 1 dB/LSB
// REG_49<6:0> 0x30; RF AGC low threshold, 1 dB/LSB
BK4819_REG_49 = 0x49U,
BK4819_REG_4D = 0x4DU,
BK4819_REG_4E = 0x4EU,
@@ -100,9 +111,22 @@ enum BK4819_REGISTER_t {
BK4819_REG_78 = 0x78U,
BK4819_REG_79 = 0x79U,
BK4819_REG_7A = 0x7AU,
// REG_7B<15:0> 0xae34 RSSI table
BK4819_REG_7B = 0x7BU,
// REG_7C<15:0> 0x8000 RSSI table
BK4819_REG_7C = 0x7CU,
BK4819_REG_7D = 0x7DU,
// REG_7E<15> 0; AGC fix mode:
// 1: Fix
// 0: Auto
// REG_7E<14:12> 0b011; AGC fix index:
// 011: Max.
// …
// 100: Min.
// REG_7E<5:3> 0b101; DC filter bandwidth for TX (MIC in):
// 000: Bypass DC filter
// REG_7E<2:0> 0b110; DC filter bandwidth for RX (IF in):
// 000: Bypass DC filter
BK4819_REG_7E = 0x7EU,
};

4
misc.c
View File

@@ -94,9 +94,7 @@ uint8_t gSetting_backlight_on_tx_rx;
#ifdef ENABLE_AM_FIX
bool gSetting_AM_fix;
#endif
#ifdef ENABLE_AM_FIX_TEST1
uint8_t gSetting_AM_fix_test1 = 0;
#endif
#ifdef ENABLE_AUDIO_BAR
bool gSetting_mic_bar;
#endif

4
misc.h
View File

@@ -158,9 +158,7 @@ extern uint8_t gSetting_backlight_on_tx_rx;
#ifdef ENABLE_AM_FIX
extern bool gSetting_AM_fix;
#endif
#ifdef ENABLE_AM_FIX_TEST1
extern uint8_t gSetting_AM_fix_test1;
#endif
#ifdef ENABLE_AUDIO_BAR
extern bool gSetting_mic_bar;
#endif

View File

@@ -585,8 +585,6 @@ void RADIO_SelectVfos(void)
void RADIO_SetupRegisters(bool switchToForeground)
{
BK4819_FilterBandwidth_t Bandwidth = gRxVfo->CHANNEL_BANDWIDTH;
uint16_t InterruptMask;
uint32_t Frequency;
AUDIO_AudioPathOff();
@@ -630,6 +628,7 @@ void RADIO_SetupRegisters(bool switchToForeground)
// mic gain 0.5dB/step 0 to 31
BK4819_WriteRegister(BK4819_REG_7D, 0xE940 | (gEeprom.MIC_SENSITIVITY_TUNING & 0x1f));
uint32_t Frequency;
#ifdef ENABLE_NOAA
if (!IS_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE) || !gIsNoaaMode)
Frequency = gRxVfo->pRX->Frequency;
@@ -659,7 +658,7 @@ void RADIO_SetupRegisters(bool switchToForeground)
(gEeprom.DAC_GAIN << 0)); // AF DAC Gain (after Gain-1 and Gain-2)
InterruptMask = BK4819_REG_3F_SQUELCH_FOUND | BK4819_REG_3F_SQUELCH_LOST;
uint16_t InterruptMask = BK4819_REG_3F_SQUELCH_FOUND | BK4819_REG_3F_SQUELCH_LOST;
#ifdef ENABLE_NOAA
if (!IS_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE))

View File

@@ -117,9 +117,6 @@ const t_menu_item MenuList[] =
#ifdef ENABLE_AM_FIX
{"AM Fix", VOICE_ID_INVALID, MENU_AM_FIX },
#endif
#ifdef ENABLE_AM_FIX_TEST1
{"AM FT1", VOICE_ID_INVALID, MENU_AM_FIX_TEST1 },
#endif
#ifdef ENABLE_VOX
{"VOX", VOICE_ID_VOX, MENU_VOX },
#endif
@@ -309,16 +306,6 @@ const char gSubMenu_RX_TX[][6] =
"TX/RX"
};
#ifdef ENABLE_AM_FIX_TEST1
const char gSubMenu_AM_fix_test1[][8] =
{
"LNA-S 0",
"LNA-S 1",
"LNA-S 2",
"LNA-S 3"
};
#endif
const char gSubMenu_BAT_TXT[][8] =
{
"NONE",
@@ -611,13 +598,6 @@ void UI_DisplayMenu(void)
strcpy(String, gModulationStr[gSubMenuSelection]);
break;
#ifdef ENABLE_AM_FIX_TEST1
case MENU_AM_FIX_TEST1:
strcpy(String, gSubMenu_AM_fix_test1[gSubMenuSelection]);
// gSetting_AM_fix = gSubMenuSelection;
break;
#endif
case MENU_AUTOLK:
strcpy(String, (gSubMenuSelection == 0) ? "OFF" : "AUTO");
break;

View File

@@ -104,9 +104,6 @@ enum
#ifdef ENABLE_AM_FIX
MENU_AM_FIX,
#endif
#ifdef ENABLE_AM_FIX_TEST1
MENU_AM_FIX_TEST1,
#endif
#ifdef ENABLE_NOAA
MENU_NOAA_S,
#endif
@@ -158,9 +155,6 @@ extern const char gSubMenu_RESET[2][4];
extern const char* gSubMenu_F_LOCK[F_LOCK_LEN];
extern const char gSubMenu_BACKLIGHT[8][7];
extern const char gSubMenu_RX_TX[4][6];
#ifdef ENABLE_AM_FIX_TEST1
extern const char gSubMenu_AM_fix_test1[4][8];
#endif
extern const char gSubMenu_BAT_TXT[3][8];
extern const char gSubMenu_BATTYP[2][9];
extern const char gSubMenu_SCRAMBLER[11][7];