From 8981ea1af7d8d310b4748c84eb99aaaa8d299c4e Mon Sep 17 00:00:00 2001 From: wu58430 Date: Tue, 5 Dec 2023 17:52:23 +0800 Subject: [PATCH] am-fix --- Makefile | 60 ++-- am_fix.c | 732 ++++++++++++++++++++-------------------------- am_fix.h | 14 +- app/action.c | 4 +- app/app.c | 55 ++-- app/app.h | 2 +- app/chFrScanner.c | 4 +- app/main.c | 232 +++++++-------- app/spectrum.c | 127 ++++---- board.c | 2 +- driver/st7565.c | 262 ++++++----------- driver/st7565.h | 13 +- frequencies.c | 261 ++++++++--------- frequencies.h | 77 ++--- radio.c | 25 +- settings.c | 30 +- ui/battery.c | 2 +- ui/main.c | 62 +++- ui/main.h | 1 + 19 files changed, 910 insertions(+), 1055 deletions(-) diff --git a/Makefile b/Makefile index eccad5b..ee52ace 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,6 @@ ENABLE_SHOW_CHARGE_LEVEL := 0 ENABLE_REVERSE_BAT_SYMBOL := 0 ENABLE_NO_CODE_SCAN_TIMEOUT := 1 ENABLE_AM_FIX := 1 -ENABLE_AM_FIX_SHOW_DATA := 0 ENABLE_SQUELCH_MORE_SENSITIVE := 1 ENABLE_FASTER_CHANNEL_SCAN := 1 ENABLE_RSSI_BAR := 1 @@ -48,7 +47,9 @@ ENABLE_SCAN_RANGES := 1 ENABLE_MDC1200 := 1 ENABLE_MDC1200_SHOW_OP_ARG := 0 ENABLE_MDC1200_SIDE_BEEP := 0 - +# ---- DEBUGGING ---- +ENABLE_AM_FIX_SHOW_DATA := 0 +ENABLE_AGC_SHOW_DATA := 0 ############################################################# @@ -76,9 +77,7 @@ ifeq ($(ENABLE_OVERLAY),1) OBJS += sram-overlay.o endif OBJS += external/printf/printf.o -ifeq ($(ENABLE_MDC1200),1) - OBJS += app/mdc1200.o -endif + # Drivers OBJS += driver/adc.o ifeq ($(ENABLE_UART),1) @@ -157,6 +156,9 @@ OBJS += ui/inputbox.o ifeq ($(ENABLE_PWRON_PASSWORD),1) OBJS += ui/lock.o endif +ifeq ($(ENABLE_MDC1200),1) + OBJS += app/mdc1200.o +endif OBJS += ui/main.o OBJS += ui/menu.o OBJS += ui/scanner.o @@ -187,17 +189,15 @@ else # unix endif AS = arm-none-eabi-gcc - -CC = LD = arm-none-eabi-gcc ifeq ($(ENABLE_CLANG),0) - CC += arm-none-eabi-gcc + CC = arm-none-eabi-gcc # Use GCC's linker to avoid undefined symbol errors # LD += arm-none-eabi-gcc else # May need to adjust this to match your system - CC += clang --sysroot=/usr/arm-none-eabi --target=arm-none-eabi + CC = clang --sysroot=/usr/arm-none-eabi --target=arm-none-eabi # Bloats binaries to 512MB # LD = ld.lld endif @@ -224,30 +224,23 @@ endif CFLAGS = ifeq ($(ENABLE_CLANG),0) - CFLAGS += -Os -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=c11 -MMD + CFLAGS += -Os -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=c2x -MMD + #CFLAGS += -Os -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=c11 -MMD #CFLAGS += -Os -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=c99 -MMD #CFLAGS += -Os -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=gnu99 -MMD #CFLAGS += -Os -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=gnu11 -MMD else # Oz needed to make it fit on flash - CFLAGS += -Oz -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=c11 -MMD + CFLAGS += -Oz -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=c2x -MMD endif ifeq ($(ENABLE_LTO),1) - CFLAGS += -flto=2 + CFLAGS += -flto=auto else # We get most of the space savings if LTO creates problems CFLAGS += -ffunction-sections -fdata-sections endif -ifeq ($(ENABLE_MDC1200),1) - CFLAGS += -DENABLE_MDC1200 -endif -ifeq ($(ENABLE_MDC1200_SHOW_OP_ARG),1) - CFLAGS += -DENABLE_MDC1200_SHOW_OP_ARG -endif -ifeq ($(ENABLE_MDC1200_SIDE_BEEP),1) - CFLAGS += -DENABLE_MDC1200_SIDE_BEEP -endif + # May cause unhelpful build failures #CFLAGS += -Wpadded @@ -372,23 +365,24 @@ endif ifeq ($(ENABLE_DTMF_CALLING),1) CFLAGS += -DENABLE_DTMF_CALLING endif - -LDFLAGS = -ifeq ($(ENABLE_CLANG),0) - LDFLAGS += -mcpu=cortex-m0 -nostartfiles -Wl,-T,firmware.ld -else -# Fix warning about implied executable stack - LDFLAGS += -z noexecstack -mcpu=cortex-m0 -nostartfiles -Wl,-T,firmware.ld +ifeq ($(ENABLE_AGC_SHOW_DATA),1) + CFLAGS += -DENABLE_AGC_SHOW_DATA endif +ifeq ($(ENABLE_MDC1200),1) + CFLAGS += -DENABLE_MDC1200 +endif +ifeq ($(ENABLE_MDC1200_SHOW_OP_ARG),1) + CFLAGS += -DENABLE_MDC1200_SHOW_OP_ARG +endif +ifeq ($(ENABLE_MDC1200_SIDE_BEEP),1) + CFLAGS += -DENABLE_MDC1200_SIDE_BEEP +endif +LDFLAGS = +LDFLAGS += -z noexecstack -mcpu=cortex-m0 -nostartfiles -Wl,-T,firmware.ld -Wl,--gc-sections # Use newlib-nano instead of newlib LDFLAGS += --specs=nano.specs -ifeq ($(ENABLE_LTO),0) - # Throw away unneeded func/data sections like LTO does - LDFLAGS += -Wl,--gc-sections -endif - ifeq ($(DEBUG),1) ASFLAGS += -g CFLAGS += -g diff --git a/am_fix.c b/am_fix.c index 18f6ee9..3f44735 100644 --- a/am_fix.c +++ b/am_fix.c @@ -32,458 +32,360 @@ #ifdef ENABLE_AM_FIX - typedef struct - { - uint16_t reg_val; - int8_t gain_dB; - } __attribute__((packed)) t_gain_table; +typedef struct +{ + uint16_t reg_val; + int8_t gain_dB; +} __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 = -19dB // was -11 +// 1 = -24dB // was -16 +// 0 = -28dB // 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 +// //// 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_short_dB[] = { (-28), (-24), (-19), 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[] = - { - {0x035E, -17}, // 0 .. 3 2 3 6 .. 0dB -14dB 0dB -3dB .. -17dB original +// lookup table is hugely easier than writing code to do the same +// +static const t_gain_table gain_table[] = +{ + {0x03BE, -7}, // 0 .. 3 5 3 6 .. 0dB -4dB 0dB -3dB .. -7dB original -#ifdef ENABLE_AM_FIX_TEST1 + {0x0000,-93}, // 1 .. 0 0 0 0 .. -28dB -24dB -8dB -33dB .. -93dB + {0x0008,-91}, // 2 .. 0 0 1 0 .. -28dB -24dB -6dB -33dB .. -91dB + {0x0100,-89}, // 3 .. 1 0 0 0 .. -24dB -24dB -8dB -33dB .. -89dB + {0x0020,-88}, // 4 .. 0 1 0 0 .. -28dB -19dB -8dB -33dB .. -88dB + {0x0108,-87}, // 5 .. 1 0 1 0 .. -24dB -24dB -6dB -33dB .. -87dB + {0x0028,-86}, // 6 .. 0 1 1 0 .. -28dB -19dB -6dB -33dB .. -86dB + {0x0018,-85}, // 7 .. 0 0 3 0 .. -28dB -24dB 0dB -33dB .. -85dB + {0x0200,-84}, // 8 .. 2 0 0 0 .. -19dB -24dB -8dB -33dB .. -84dB + {0x0101,-83}, // 9 .. 1 0 0 1 .. -24dB -24dB -8dB -27dB .. -83dB + {0x0208,-82}, // 10 .. 2 0 1 0 .. -19dB -24dB -6dB -33dB .. -82dB + {0x0118,-81}, // 11 .. 1 0 3 0 .. -24dB -24dB 0dB -33dB .. -81dB + {0x0038,-80}, // 12 .. 0 1 3 0 .. -28dB -19dB 0dB -33dB .. -80dB + {0x0220,-79}, // 13 .. 2 1 0 0 .. -19dB -19dB -8dB -33dB .. -79dB + {0x0201,-78}, // 14 .. 2 0 0 1 .. -19dB -24dB -8dB -27dB .. -78dB + {0x0228,-77}, // 15 .. 2 1 1 0 .. -19dB -19dB -6dB -33dB .. -77dB + {0x0218,-76}, // 16 .. 2 0 3 0 .. -19dB -24dB 0dB -33dB .. -76dB + {0x0119,-75}, // 17 .. 1 0 3 1 .. -24dB -24dB 0dB -27dB .. -75dB + {0x0240,-74}, // 18 .. 2 2 0 0 .. -19dB -14dB -8dB -33dB .. -74dB + {0x0221,-73}, // 19 .. 2 1 0 1 .. -19dB -19dB -8dB -27dB .. -73dB + {0x0248,-72}, // 20 .. 2 2 1 0 .. -19dB -14dB -6dB -33dB .. -72dB + {0x0238,-71}, // 21 .. 2 1 3 0 .. -19dB -19dB 0dB -33dB .. -71dB + {0x0219,-70}, // 22 .. 2 0 3 1 .. -19dB -24dB 0dB -27dB .. -70dB + {0x0260,-69}, // 23 .. 2 3 0 0 .. -19dB -9dB -8dB -33dB .. -69dB + {0x0241,-68}, // 24 .. 2 2 0 1 .. -19dB -14dB -8dB -27dB .. -68dB + {0x0268,-67}, // 25 .. 2 3 1 0 .. -19dB -9dB -6dB -33dB .. -67dB + {0x0280,-66}, // 26 .. 2 4 0 0 .. -19dB -6dB -8dB -33dB .. -66dB + {0x0300,-65}, // 27 .. 3 0 0 0 .. 0dB -24dB -8dB -33dB .. -65dB + {0x02A0,-64}, // 28 .. 2 5 0 0 .. -19dB -4dB -8dB -33dB .. -64dB + {0x0308,-63}, // 29 .. 3 0 1 0 .. 0dB -24dB -6dB -33dB .. -63dB + {0x02C0,-62}, // 30 .. 2 6 0 0 .. -19dB -2dB -8dB -33dB .. -62dB + {0x0290,-61}, // 31 .. 2 4 2 0 .. -19dB -6dB -3dB -33dB .. -61dB + {0x0320,-60}, // 32 .. 3 1 0 0 .. 0dB -19dB -8dB -33dB .. -60dB + {0x0301,-59}, // 33 .. 3 0 0 1 .. 0dB -24dB -8dB -27dB .. -59dB + {0x0328,-58}, // 34 .. 3 1 1 0 .. 0dB -19dB -6dB -33dB .. -58dB + {0x0318,-57}, // 35 .. 3 0 3 0 .. 0dB -24dB 0dB -33dB .. -57dB + {0x02C1,-56}, // 36 .. 2 6 0 1 .. -19dB -2dB -8dB -27dB .. -56dB + {0x0340,-55}, // 37 .. 3 2 0 0 .. 0dB -14dB -8dB -33dB .. -55dB + {0x0321,-54}, // 38 .. 3 1 0 1 .. 0dB -19dB -8dB -27dB .. -54dB + {0x0348,-53}, // 39 .. 3 2 1 0 .. 0dB -14dB -6dB -33dB .. -53dB + {0x0338,-52}, // 40 .. 3 1 3 0 .. 0dB -19dB 0dB -33dB .. -52dB + {0x0319,-51}, // 41 .. 3 0 3 1 .. 0dB -24dB 0dB -27dB .. -51dB + {0x0360,-50}, // 42 .. 3 3 0 0 .. 0dB -9dB -8dB -33dB .. -50dB + {0x0341,-49}, // 43 .. 3 2 0 1 .. 0dB -14dB -8dB -27dB .. -49dB + {0x0368,-48}, // 44 .. 3 3 1 0 .. 0dB -9dB -6dB -33dB .. -48dB + {0x0380,-47}, // 45 .. 3 4 0 0 .. 0dB -6dB -8dB -33dB .. -47dB + {0x0339,-46}, // 46 .. 3 1 3 1 .. 0dB -19dB 0dB -27dB .. -46dB + {0x03A0,-45}, // 47 .. 3 5 0 0 .. 0dB -4dB -8dB -33dB .. -45dB + {0x0361,-44}, // 48 .. 3 3 0 1 .. 0dB -9dB -8dB -27dB .. -44dB + {0x03C0,-43}, // 49 .. 3 6 0 0 .. 0dB -2dB -8dB -33dB .. -43dB + {0x0390,-42}, // 50 .. 3 4 2 0 .. 0dB -6dB -3dB -33dB .. -42dB + {0x03E0,-41}, // 51 .. 3 7 0 0 .. 0dB 0dB -8dB -33dB .. -41dB + {0x03B0,-40}, // 52 .. 3 5 2 0 .. 0dB -4dB -3dB -33dB .. -40dB + {0x03E8,-39}, // 53 .. 3 7 1 0 .. 0dB 0dB -6dB -33dB .. -39dB + {0x03D0,-38}, // 54 .. 3 6 2 0 .. 0dB -2dB -3dB -33dB .. -38dB + {0x03C1,-37}, // 55 .. 3 6 0 1 .. 0dB -2dB -8dB -27dB .. -37dB + {0x03F0,-36}, // 56 .. 3 7 2 0 .. 0dB 0dB -3dB -33dB .. -36dB + {0x03E1,-35}, // 57 .. 3 7 0 1 .. 0dB 0dB -8dB -27dB .. -35dB + {0x03B1,-34}, // 58 .. 3 5 2 1 .. 0dB -4dB -3dB -27dB .. -34dB + {0x03F8,-33}, // 59 .. 3 7 3 0 .. 0dB 0dB 0dB -33dB .. -33dB + {0x03D1,-32}, // 60 .. 3 6 2 1 .. 0dB -2dB -3dB -27dB .. -32dB + {0x03C2,-31}, // 61 .. 3 6 0 2 .. 0dB -2dB -8dB -21dB .. -31dB + {0x03F1,-30}, // 62 .. 3 7 2 1 .. 0dB 0dB -3dB -27dB .. -30dB + {0x03E2,-29}, // 63 .. 3 7 0 2 .. 0dB 0dB -8dB -21dB .. -29dB + {0x03B2,-28}, // 64 .. 3 5 2 2 .. 0dB -4dB -3dB -21dB .. -28dB + {0x03F9,-27}, // 65 .. 3 7 3 1 .. 0dB 0dB 0dB -27dB .. -27dB + {0x03D2,-26}, // 66 .. 3 6 2 2 .. 0dB -2dB -3dB -21dB .. -26dB + {0x03C3,-25}, // 67 .. 3 6 0 3 .. 0dB -2dB -8dB -15dB .. -25dB + {0x03F2,-24}, // 68 .. 3 7 2 2 .. 0dB 0dB -3dB -21dB .. -24dB + {0x03E3,-23}, // 69 .. 3 7 0 3 .. 0dB 0dB -8dB -15dB .. -23dB + {0x03B3,-22}, // 70 .. 3 5 2 3 .. 0dB -4dB -3dB -15dB .. -22dB + {0x03FA,-21}, // 71 .. 3 7 3 2 .. 0dB 0dB 0dB -21dB .. -21dB + {0x03D3,-20}, // 72 .. 3 6 2 3 .. 0dB -2dB -3dB -15dB .. -20dB + {0x03C4,-19}, // 73 .. 3 6 0 4 .. 0dB -2dB -8dB -9dB .. -19dB + {0x03F3,-18}, // 74 .. 3 7 2 3 .. 0dB 0dB -3dB -15dB .. -18dB + {0x03E4,-17}, // 75 .. 3 7 0 4 .. 0dB 0dB -8dB -9dB .. -17dB + {0x03C5,-16}, // 76 .. 3 6 0 5 .. 0dB -2dB -8dB -6dB .. -16dB + {0x03FB,-15}, // 77 .. 3 7 3 3 .. 0dB 0dB 0dB -15dB .. -15dB + {0x03E5,-14}, // 78 .. 3 7 0 5 .. 0dB 0dB -8dB -6dB .. -14dB + {0x03C6,-13}, // 79 .. 3 6 0 6 .. 0dB -2dB -8dB -3dB .. -13dB + {0x03F4,-12}, // 80 .. 3 7 2 4 .. 0dB 0dB -3dB -9dB .. -12dB + {0x03E6,-11}, // 81 .. 3 7 0 6 .. 0dB 0dB -8dB -3dB .. -11dB + {0x03C7,-10}, // 82 .. 3 6 0 7 .. 0dB -2dB -8dB 0dB .. -10dB + {0x03FC, -9}, // 83 .. 3 7 3 4 .. 0dB 0dB 0dB -9dB .. -9dB + {0x03E7, -8}, // 84 .. 3 7 0 7 .. 0dB 0dB -8dB 0dB .. -8dB + {0x03BE, -7}, // 85 .. 3 5 3 6 .. 0dB -4dB 0dB -3dB .. -7dB original + {0x03FD, -6}, // 86 .. 3 7 3 5 .. 0dB 0dB 0dB -6dB .. -6dB + {0x03DE, -5}, // 87 .. 3 6 3 6 .. 0dB -2dB 0dB -3dB .. -5dB + {0x03BF, -4}, // 88 .. 3 5 3 7 .. 0dB -4dB 0dB 0dB .. -4dB + {0x03FE, -3}, // 89 .. 3 7 3 6 .. 0dB 0dB 0dB -3dB .. -3dB + {0x03DF, -2}, // 90 .. 3 6 3 7 .. 0dB -2dB 0dB 0dB .. -2dB + {0x03FF, 0}, // 91 .. 3 7 3 7 .. 0dB 0dB 0dB 0dB .. 0dB +}; - // 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 - {0x0020, -93}, // 4 .. 0 1 0 0 .. -33dB -19dB -8dB -33dB .. -93dB - {0x0001, -92}, // 5 .. 0 0 0 1 .. -33dB -24dB -8dB -27dB .. -92dB - {0x0028, -91}, // 6 .. 0 1 1 0 .. -33dB -19dB -6dB -33dB .. -91dB - {0x0009, -90}, // 7 .. 0 0 1 1 .. -33dB -24dB -6dB -27dB .. -90dB - {0x0101, -89}, // 8 .. 1 0 0 1 .. -30dB -24dB -8dB -27dB .. -89dB - {0x0030, -88}, // 9 .. 0 1 2 0 .. -33dB -19dB -3dB -33dB .. -88dB - {0x0118, -87}, // 10 .. 1 0 3 0 .. -30dB -24dB 0dB -33dB .. -87dB - {0x0002, -86}, // 11 .. 0 0 0 2 .. -33dB -24dB -8dB -21dB .. -86dB - {0x0130, -85}, // 12 .. 1 1 2 0 .. -30dB -19dB -3dB -33dB .. -85dB - {0x0019, -84}, // 13 .. 0 0 3 1 .. -33dB -24dB 0dB -27dB .. -84dB - {0x0060, -83}, // 14 .. 0 3 0 0 .. -33dB -9dB -8dB -33dB .. -83dB - {0x0138, -82}, // 15 .. 1 1 3 0 .. -30dB -19dB 0dB -33dB .. -82dB - {0x0119, -81}, // 16 .. 1 0 3 1 .. -30dB -24dB 0dB -27dB .. -81dB - {0x0058, -80}, // 17 .. 0 2 3 0 .. -33dB -14dB 0dB -33dB .. -80dB - {0x0141, -79}, // 18 .. 1 2 0 1 .. -30dB -14dB -8dB -27dB .. -79dB - {0x0070, -78}, // 19 .. 0 3 2 0 .. -33dB -9dB -3dB -33dB .. -78dB - {0x0180, -77}, // 20 .. 1 4 0 0 .. -30dB -6dB -8dB -33dB .. -77dB - {0x0139, -76}, // 21 .. 1 1 3 1 .. -30dB -19dB 0dB -27dB .. -76dB - {0x0013, -75}, // 22 .. 0 0 2 3 .. -33dB -24dB -3dB -15dB .. -75dB - {0x0161, -74}, // 23 .. 1 3 0 1 .. -30dB -9dB -8dB -27dB .. -74dB - {0x01C0, -73}, // 24 .. 1 6 0 0 .. -30dB -2dB -8dB -33dB .. -73dB - {0x00E8, -72}, // 25 .. 0 7 1 0 .. -33dB 0dB -6dB -33dB .. -72dB - {0x00D0, -71}, // 26 .. 0 6 2 0 .. -33dB -2dB -3dB -33dB .. -71dB - {0x0239, -70}, // 27 .. 2 1 3 1 .. -24dB -19dB 0dB -27dB .. -70dB - {0x006A, -69}, // 28 .. 0 3 1 2 .. -33dB -9dB -6dB -21dB .. -69dB - {0x0006, -68}, // 29 .. 0 0 0 6 .. -33dB -24dB -8dB -3dB .. -68dB - {0x00B1, -67}, // 30 .. 0 5 2 1 .. -33dB -4dB -3dB -27dB .. -67dB - {0x000E, -66}, // 31 .. 0 0 1 6 .. -33dB -24dB -6dB -3dB .. -66dB - {0x015A, -65}, // 32 .. 1 2 3 2 .. -30dB -14dB 0dB -21dB .. -65dB - {0x022B, -64}, // 33 .. 2 1 1 3 .. -24dB -19dB -6dB -15dB .. -64dB - {0x01F8, -63}, // 34 .. 1 7 3 0 .. -30dB 0dB 0dB -33dB .. -63dB - {0x0163, -62}, // 35 .. 1 3 0 3 .. -30dB -9dB -8dB -15dB .. -62dB - {0x0035, -61}, // 36 .. 0 1 2 5 .. -33dB -19dB -3dB -6dB .. -61dB - {0x0214, -60}, // 37 .. 2 0 2 4 .. -24dB -24dB -3dB -9dB .. -60dB - {0x01D9, -59}, // 38 .. 1 6 3 1 .. -30dB -2dB 0dB -27dB .. -59dB - {0x0145, -58}, // 39 .. 1 2 0 5 .. -30dB -14dB -8dB -6dB .. -58dB - {0x02A2, -57}, // 40 .. 2 5 0 2 .. -24dB -4dB -8dB -21dB .. -57dB - {0x02D1, -56}, // 41 .. 2 6 2 1 .. -24dB -2dB -3dB -27dB .. -56dB - {0x00B3, -55}, // 42 .. 0 5 2 3 .. -33dB -4dB -3dB -15dB .. -55dB - {0x0216, -54}, // 43 .. 2 0 2 6 .. -24dB -24dB -3dB -3dB .. -54dB - {0x0066, -53}, // 44 .. 0 3 0 6 .. -33dB -9dB -8dB -3dB .. -53dB - {0x00C4, -52}, // 45 .. 0 6 0 4 .. -33dB -2dB -8dB -9dB .. -52dB - {0x006E, -51}, // 46 .. 0 3 1 6 .. -33dB -9dB -6dB -3dB .. -51dB - {0x015D, -50}, // 47 .. 1 2 3 5 .. -30dB -14dB 0dB -6dB .. -50dB - {0x00AD, -49}, // 48 .. 0 5 1 5 .. -33dB -4dB -6dB -6dB .. -49dB - {0x007D, -48}, // 49 .. 0 3 3 5 .. -33dB -9dB 0dB -6dB .. -48dB - {0x00D4, -47}, // 50 .. 0 6 2 4 .. -33dB -2dB -3dB -9dB .. -47dB - {0x01B4, -46}, // 51 .. 1 5 2 4 .. -30dB -4dB -3dB -9dB .. -46dB - {0x030B, -45}, // 52 .. 3 0 1 3 .. 0dB -24dB -6dB -15dB .. -45dB - {0x00CE, -44}, // 53 .. 0 6 1 6 .. -33dB -2dB -6dB -3dB .. -44dB - {0x01B5, -43}, // 54 .. 1 5 2 5 .. -30dB -4dB -3dB -6dB .. -43dB - {0x0097, -42}, // 55 .. 0 4 2 7 .. -33dB -6dB -3dB 0dB .. -42dB - {0x0257, -41}, // 56 .. 2 2 2 7 .. -24dB -14dB -3dB 0dB .. -41dB - {0x02B4, -40}, // 57 .. 2 5 2 4 .. -24dB -4dB -3dB -9dB .. -40dB - {0x027D, -39}, // 58 .. 2 3 3 5 .. -24dB -9dB 0dB -6dB .. -39dB - {0x01DD, -38}, // 59 .. 1 6 3 5 .. -30dB -2dB 0dB -6dB .. -38dB - {0x02AE, -37}, // 60 .. 2 5 1 6 .. -24dB -4dB -6dB -3dB .. -37dB - {0x0379, -36}, // 61 .. 3 3 3 1 .. 0dB -9dB 0dB -27dB .. -36dB - {0x035A, -35}, // 62 .. 3 2 3 2 .. 0dB -14dB 0dB -21dB .. -35dB - {0x02B6, -34}, // 63 .. 2 5 2 6 .. -24dB -4dB -3dB -3dB .. -34dB - {0x030E, -33}, // 64 .. 3 0 1 6 .. 0dB -24dB -6dB -3dB .. -33dB - {0x0307, -32}, // 65 .. 3 0 0 7 .. 0dB -24dB -8dB 0dB .. -32dB - {0x02BE, -31}, // 66 .. 2 5 3 6 .. -24dB -4dB 0dB -3dB .. -31dB - {0x037A, -30}, // 67 .. 3 3 3 2 .. 0dB -9dB 0dB -21dB .. -30dB - {0x02DE, -29}, // 68 .. 2 6 3 6 .. -24dB -2dB 0dB -3dB .. -29dB - {0x0345, -28}, // 69 .. 3 2 0 5 .. 0dB -14dB -8dB -6dB .. -28dB - {0x03A3, -27}, // 70 .. 3 5 0 3 .. 0dB -4dB -8dB -15dB .. -27dB - {0x0364, -26}, // 71 .. 3 3 0 4 .. 0dB -9dB -8dB -9dB .. -26dB - {0x032F, -25}, // 72 .. 3 1 1 7 .. 0dB -19dB -6dB 0dB .. -25dB - {0x0393, -24}, // 73 .. 3 4 2 3 .. 0dB -6dB -3dB -15dB .. -24dB - {0x0384, -23}, // 74 .. 3 4 0 4 .. 0dB -6dB -8dB -9dB .. -23dB - {0x0347, -22}, // 75 .. 3 2 0 7 .. 0dB -14dB -8dB 0dB .. -22dB - {0x03EB, -21}, // 76 .. 3 7 1 3 .. 0dB 0dB -6dB -15dB .. -21dB - {0x03D3, -20}, // 77 .. 3 6 2 3 .. 0dB -2dB -3dB -15dB .. -20dB - {0x03BB, -19}, // 78 .. 3 5 3 3 .. 0dB -4dB 0dB -15dB .. -19dB - {0x037C, -18}, // 79 .. 3 3 3 4 .. 0dB -9dB 0dB -9dB .. -18dB - {0x03CC, -17}, // 80 .. 3 6 1 4 .. 0dB -2dB -6dB -9dB .. -17dB - {0x03C5, -16}, // 81 .. 3 6 0 5 .. 0dB -2dB -8dB -6dB .. -16dB - {0x03EC, -15}, // 82 .. 3 7 1 4 .. 0dB 0dB -6dB -9dB .. -15dB - {0x035F, -14}, // 83 .. 3 2 3 7 .. 0dB -14dB 0dB 0dB .. -14dB - {0x03BC, -13}, // 84 .. 3 5 3 4 .. 0dB -4dB 0dB -9dB .. -13dB - {0x038F, -12}, // 85 .. 3 4 1 7 .. 0dB -6dB -6dB 0dB .. -12dB - {0x03E6, -11}, // 86 .. 3 7 0 6 .. 0dB 0dB -8dB -3dB .. -11dB - {0x03AF, -10}, // 87 .. 3 5 1 7 .. 0dB -4dB -6dB 0dB .. -10dB - {0x03F5, -9 }, // 88 .. 3 7 2 5 .. 0dB 0dB -3dB -6dB .. -9dB - {0x03D6, -8 }, // 89 .. 3 6 2 6 .. 0dB -2dB -3dB -3dB .. -8dB - {0x03BE, -7 }, // 90 .. 3 5 3 6 .. 0dB -4dB 0dB -3dB .. -7dB original - {0x03F6, -6 }, // 91 .. 3 7 2 6 .. 0dB 0dB -3dB -3dB .. -6dB - {0x03DE, -5 }, // 92 .. 3 6 3 6 .. 0dB -2dB 0dB -3dB .. -5dB - {0x03BF, -4 }, // 93 .. 3 5 3 7 .. 0dB -4dB 0dB 0dB .. -4dB - {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; +#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 +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}; + +// 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; + +// -89dBm, any higher and the AM demodulator starts to saturate/clip/distort +const int16_t desired_rssi = (-89 + 160) * 2; + +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 + } +} + +void AM_fix_reset(const unsigned vfo) +{ // reset the AM fixer upper + if (vfo > 1) + return; + #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; + counter = 0; #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 + prev_rssi[vfo] = 0; + hold_counter[vfo] = 0; + rssi_gain_diff[vfo] = 0; + gain_table_index_prev[vfo] = 0; +} - // used simply to detect a changed gain setting - unsigned int gain_table_index_prev[2] = {0, 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 unsigned vfo, bool force) +{ + if(vfo > 1) + return; - // holds the previous RSSI level .. we do an average of old + new RSSI reading - int16_t prev_rssi[2] = {0, 0}; + if(!force) switch (gCurrentFunction) + { + case FUNCTION_TRANSMIT: + case FUNCTION_BAND_SCOPE: + case FUNCTION_POWER_SAVE: + case FUNCTION_FOREGROUND: +#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; - // to help reduce gain hunting, peak hold count down tick - unsigned int hold_counter[2] = {0, 0}; + // only adjust stuff if we're in one of these modes + case FUNCTION_RECEIVE: + case FUNCTION_MONITOR: + case FUNCTION_INCOMING: + break; + } - // 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 - unsigned int max_index = ARRAY_SIZE(gain_table) - 1; - - #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 - - void AM_fix_init(void) - { // called at boot-up - - unsigned int i; - - for (i = 0; i < 2; i++) - { - #ifdef ENABLE_AM_FIX_TEST1 - gain_table_index[i] = 1 + gSetting_AM_fix_test1; - #else - gain_table_index[i] = original_index; // re-start with original QS setting - #endif +#ifdef ENABLE_AM_FIX_SHOW_DATA + if (counter > 0) { + if (++counter >= display_update_rate) { // trigger a display update + counter = 0; + gUpdateDisplay = true; } + } +#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; + int16_t rssi; + { // sample the current RSSI level + // average it with the previous rssi (a bit of noise/spike immunity) + const int16_t new_rssi = BK4819_GetRSSI(); + rssi = (prev_rssi[vfo] > 0) ? (prev_rssi[vfo] + new_rssi) / 2 : new_rssi; + prev_rssi[vfo] = new_rssi; + } - 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) +#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; + + if (counter == 0) { + counter = 1; + gUpdateDisplay = true; // trigger a display update + } + } + } +#endif + + // automatically adjust the RF RX gain + + // update the gain hold counter + if (hold_counter[vfo] > 0) + hold_counter[vfo]--; + + // dB difference between actual and desired RSSI level + int16_t diff_dB = (rssi - desired_rssi) / 2; + + 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 + // 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) + + // scan the table to see what index to jump straight too + while (index > 1) + if (gain_table[--index].gain_dB <= desired_gain_dB) break; } - #else - // use the full range of available gains - max_index = ARRAY_SIZE(gain_table) - 1; - #endif + else + { // incrementally reduce the gain .. taking it slow improves noise/spike immunity + if (index > 1) + index--; // slow step-by-step gain reduction + } + + index = (index < 1) ? 1 : (index > max_index) ? max_index : index; + + if (gain_table_index[vfo] != index) + { + gain_table_index[vfo] = index; + hold_counter[vfo] = 30; // 300ms hold + } } - void AM_fix_reset(const int vfo) - { // reset the AM fixer upper + if (diff_dB >= -6) // 6dB hysterisis (help reduce gain hunting) + hold_counter[vfo] = 30; // 300ms hold - #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; + if (hold_counter[vfo] == 0) + { // hold has been released, we're free to increase gain + const unsigned int index = gain_table_index[vfo] + 1; // move up to next gain index + gain_table_index[vfo] = (index <= max_index) ? index : max_index; // limit the gain index } - // 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; - switch (gCurrentFunction) - { - case FUNCTION_TRANSMIT: - case FUNCTION_BAND_SCOPE: - case FUNCTION_POWER_SAVE: - #ifdef ENABLE_AM_FIX_SHOW_DATA - counter = display_update_rate; // queue up a display update as soon as we switch to RX mode - #endif - return; + { // apply the new settings to the front end registers + const unsigned int index = gain_table_index[vfo]; - // only adjust stuff if we're in one of these modes - case FUNCTION_FOREGROUND: - case FUNCTION_RECEIVE: - case FUNCTION_MONITOR: - case FUNCTION_INCOMING: - break; - } + // remember the new table index + gain_table_index_prev[vfo] = index; - #ifdef ENABLE_AM_FIX_SHOW_DATA - if (counter > 0) - { - if (++counter >= display_update_rate) - { // trigger a display update - counter = 0; - gUpdateDisplay = true; - } - } - #endif + BK4819_WriteRegister(BK4819_REG_13, gain_table[index].reg_val); - { // sample the current RSSI level - // average it with the previous rssi (a bit of noise/spike immunity) - const int16_t new_rssi = BK4819_GetRSSI(); - rssi = (prev_rssi[vfo] > 0) ? (prev_rssi[vfo] + new_rssi) / 2 : new_rssi; - prev_rssi[vfo] = new_rssi; - } + // offset the RSSI reading to the rest of the firmware to cancel out the gain adjustments we make - // save the corrected RSSI level - #ifdef ENABLE_AM_FIX_SHOW_DATA - { - const int16_t new_rssi = rssi - rssi_gain_diff[vfo]; - if (gCurrentRSSI[vfo] != new_rssi) - { - gCurrentRSSI[vfo] = new_rssi; - - if (counter == 0) - { // trigger a display update - counter = 1; - gUpdateDisplay = true; - } - } - } - #else - gCurrentRSSI[vfo] = rssi - rssi_gain_diff[vfo]; - #endif + // 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; + } -#ifdef ENABLE_AM_FIX_TEST1 - // user is manually adjusting a gain register - don't do anything automatically + // save the corrected RSSI level + gCurrentRSSI[vfo] = rssi - rssi_gain_diff[vfo]; - { - 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 - // automatically adjust the RF RX gain - - // update the gain hold counter - if (hold_counter[vfo] > 0) - hold_counter[vfo]--; - - // dB difference between actual and desired RSSI level - diff_dB = (rssi - desired_rssi) / 2; - - 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 - // 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) - - // scan the table to see what index to jump straight too - 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 - } - - index = (index < 1) ? 1 : (index > max_index) ? max_index : index; - - if (gain_table_index[vfo] != index) - { - gain_table_index[vfo] = index; - hold_counter[vfo] = 30; // 300ms hold - } - } - - if (diff_dB >= -6) // 6dB hysterisis (help reduce gain hunting) - hold_counter[vfo] = 30; // 300ms hold - - if (hold_counter[vfo] == 0) - { // hold has been released, we're free to increase gain - const unsigned int index = gain_table_index[vfo] + 1; // move up to next gain index - 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 +#ifdef ENABLE_AM_FIX_SHOW_DATA + if (counter == 0) { + counter = 1; + gUpdateDisplay = true; + } +#endif +} +#ifdef ENABLE_AM_FIX_SHOW_DATA +void AM_fix_print_data(const unsigned vfo, char *s) { + if (s != NULL && vfo < ARRAY_SIZE(gain_table_index)) { + const unsigned int index = gain_table_index[vfo]; + sprintf(s, "%2u %4ddB %3u", index, gain_table[index].gain_dB, prev_rssi[vfo]); + counter = 0; + } +} #endif - { // apply the new settings to the front end registers +int16_t AM_fix_get_rssi_gain_diff(const unsigned vfo) +{ + if(vfo > 1) + return 0; + return rssi_gain_diff[vfo]; +} - const unsigned int index = gain_table_index[vfo]; - - // remember the new table index - 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; - gUpdateDisplay = true; - } - #endif - } - - #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)) - { - 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 \ No newline at end of file diff --git a/am_fix.h b/am_fix.h index 3c94d17..60d1595 100644 --- a/am_fix.h +++ b/am_fix.h @@ -21,15 +21,13 @@ #include #ifdef ENABLE_AM_FIX - extern int16_t rssi_gain_diff[2]; - - void AM_fix_init(void); - void AM_fix_reset(const int vfo); - void AM_fix_10ms(const int vfo); +void AM_fix_init(void); +void AM_fix_10ms(const unsigned vfo, bool force); #ifdef ENABLE_AM_FIX_SHOW_DATA - void AM_fix_print_data(const int vfo, char *s); + void AM_fix_print_data(const unsigned vfo, char *s); #endif - -#endif + int16_t AM_fix_get_rssi_gain_diff(const unsigned vfo); #endif + +#endif \ No newline at end of file diff --git a/app/action.c b/app/action.c index 75b4406..d0808f5 100644 --- a/app/action.c +++ b/app/action.c @@ -81,8 +81,8 @@ void ACTION_Monitor(void) gNoaaChannel = gRxVfo->CHANNEL_SAVE - NOAA_CHANNEL_FIRST; #endif RADIO_SetupRegisters(true); - APP_StartListening(FUNCTION_MONITOR, false); - return; + APP_StartListening(FUNCTION_MONITOR); + return; } gMonitor = false; diff --git a/app/app.c b/app/app.c index d08e5c0..1ddd494 100644 --- a/app/app.c +++ b/app/app.c @@ -74,7 +74,7 @@ static void UpdateRSSI(const int vfo) #ifdef ENABLE_AM_FIX // add RF gain adjust compensation if (gEeprom.VfoInfo[vfo].Modulation == MODULATION_AM && gSetting_AM_fix) - rssi -= rssi_gain_diff[vfo]; + rssi -= AM_fix_get_rssi_gain_diff(vfo); #endif if (gCurrentRSSI[vfo] == rssi) @@ -172,7 +172,7 @@ static void CheckForIncoming(void) static void HandleIncoming(void) { - bool bFlag; + if (!g_SquelchLost) { // squelch is closed #ifdef ENABLE_DTMF_CALLING @@ -186,8 +186,7 @@ static void HandleIncoming(void) return; } - bFlag = (gScanStateDir == SCAN_OFF && gCurrentCodeType == CODE_TYPE_OFF); - + bool bFlag = (gScanStateDir == SCAN_OFF && gCurrentCodeType == CODE_TYPE_OFF); #ifdef ENABLE_NOAA if (IS_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE) && gNOAACountdown_10ms > 0) { gNOAACountdown_10ms = 0; @@ -200,8 +199,9 @@ static void HandleIncoming(void) gFoundCTCSS = false; } - if (g_CDCSS_Lost && gCDCSSCodeType == CDCSS_POSITIVE_CODE && (gCurrentCodeType == CODE_TYPE_DIGITAL || gCurrentCodeType == CODE_TYPE_REVERSE_DIGITAL)) { - gFoundCDCSS = false; + if (g_CDCSS_Lost && gCDCSSCodeType == CDCSS_POSITIVE_CODE + && (gCurrentCodeType == CODE_TYPE_DIGITAL || gCurrentCodeType == CODE_TYPE_REVERSE_DIGITAL)) + { gFoundCDCSS = false; } else if (!bFlag) return; @@ -231,7 +231,7 @@ static void HandleIncoming(void) } #endif - APP_StartListening(gMonitor ? FUNCTION_MONITOR : FUNCTION_RECEIVE, false); + APP_StartListening(gMonitor ? FUNCTION_MONITOR : FUNCTION_RECEIVE); } static void HandleReceive(void) @@ -445,11 +445,9 @@ static void HandleFunction(void) } } -void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) -{ - (void)reset_am_fix; - const unsigned int chan = gEeprom.RX_VFO; -// const unsigned int chan = gRxVfo->CHANNEL_SAVE; +void APP_StartListening(FUNCTION_Type_t function){ + const unsigned int vfo = gEeprom.RX_VFO; + // const unsigned int chan = gRxVfo->CHANNEL_SAVE; #ifdef ENABLE_DTMF_CALLING if (gSetting_KILLED) @@ -462,8 +460,7 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) #endif // clear the other vfo's rssi level (to hide the antenna symbol) - gVFO_RSSI_bar_level[(chan + 1) & 1u] = 0; - + gVFO_RSSI_bar_level[!vfo] = 0; AUDIO_AudioPathOn(); gEnableSpeaker = true; @@ -477,8 +474,7 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) gRxVfo->CHANNEL_SAVE = gNoaaChannel + NOAA_CHANNEL_FIRST; gRxVfo->pRX->Frequency = NoaaFrequencyTable[gNoaaChannel]; gRxVfo->pTX->Frequency = NoaaFrequencyTable[gNoaaChannel]; - gEeprom.ScreenChannel[chan] = gRxVfo->CHANNEL_SAVE; - +gEeprom.ScreenChannel[vfo] = gRxVfo->CHANNEL_SAVE; gNOAA_Countdown_10ms = 500; // 5 sec gScheduleNOAA = false; } @@ -500,38 +496,25 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) gUpdateStatus = true; } -#ifdef ENABLE_AM_FIX - if (gRxVfo->Modulation == MODULATION_AM && gSetting_AM_fix) { // AM RX mode - if (reset_am_fix) - AM_fix_reset(chan); // TODO: only reset it when moving channel/frequency - AM_fix_10ms(chan); - } -#endif - // AF gain - original QS values - // if (gRxVfo->Modulation != MODULATION_FM){ - // BK4819_WriteRegister(BK4819_REG_48, 0xB3A8); - // } - // else - { + BK4819_WriteRegister(BK4819_REG_48, (11u << 12) | // ??? .. 0 to 15, doesn't seem to make any difference ( 0u << 10) | // AF Rx Gain-1 (gEeprom.VOLUME_GAIN << 4) | // AF Rx Gain-2 (gEeprom.DAC_GAIN << 0)); // AF DAC Gain (after Gain-1 and Gain-2) - } + #ifdef ENABLE_VOICE if (gVoiceWriteIndex == 0) // AM/FM RX mode will be set when the voice has finished #endif RADIO_SetModulation(gRxVfo->Modulation); // no need, set it now - FUNCTION_Select(Function); - + FUNCTION_Select(function); #ifdef ENABLE_FMRADIO - if (Function == FUNCTION_MONITOR || gFmRadioMode) + if (function == FUNCTION_MONITOR || gFmRadioMode) #else - if (Function == FUNCTION_MONITOR) + if (function == FUNCTION_MONITOR) #endif { // squelch is disabled if (gScreenToDisplay != DISPLAY_MENU) // 1of11 .. don't close the menu @@ -1180,7 +1163,7 @@ void APP_TimeSlice10ms(void) #ifdef ENABLE_AM_FIX if (gRxVfo->Modulation == MODULATION_AM && gSetting_AM_fix) - AM_fix_10ms(gEeprom.RX_VFO); +AM_fix_10ms(gEeprom.RX_VFO, false); #endif if (UART_IsCommandAvailable()) @@ -1581,7 +1564,7 @@ void APP_TimeSlice500ms(void) BATTERY_TimeSlice500ms(); SCANNER_TimeSlice500ms(); - + UI_MAIN_TimeSlice500ms(); #ifdef ENABLE_DTMF_CALLING if (gCurrentFunction != FUNCTION_TRANSMIT) { diff --git a/app/app.h b/app/app.h index ef3b706..5ca2696 100644 --- a/app/app.h +++ b/app/app.h @@ -24,7 +24,7 @@ #include "radio.h" void APP_EndTransmission(void); -void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix); +void APP_StartListening(FUNCTION_Type_t function); uint32_t APP_SetFreqByStepAndLimits(VFO_Info_t *pInfo, int8_t direction, uint32_t lower, uint32_t upper); uint32_t APP_SetFrequencyByStep(VFO_Info_t *pInfo, int8_t direction); void APP_Update(void); diff --git a/app/chFrScanner.c b/app/chFrScanner.c index 5c99173..f1083c3 100644 --- a/app/chFrScanner.c +++ b/app/chFrScanner.c @@ -71,14 +71,14 @@ void CHFRSCANNER_ContinueScanning(void) if (IS_FREQ_CHANNEL(gNextMrChannel)) { if (gCurrentFunction == FUNCTION_INCOMING) - APP_StartListening(gMonitor ? FUNCTION_MONITOR : FUNCTION_RECEIVE, true); + APP_StartListening(gMonitor ? FUNCTION_MONITOR : FUNCTION_RECEIVE); else NextFreqChannel(); // switch to next frequency } else { if (gCurrentCodeType == CODE_TYPE_OFF && gCurrentFunction == FUNCTION_INCOMING) - APP_StartListening(gMonitor ? FUNCTION_MONITOR : FUNCTION_RECEIVE, true); + APP_StartListening(gMonitor ? FUNCTION_MONITOR : FUNCTION_RECEIVE); else NextMemChannel(); // switch to next channel } diff --git a/app/main.c b/app/main.c index c7dbecc..5ee21b4 100644 --- a/app/main.c +++ b/app/main.c @@ -46,24 +46,19 @@ #include void toggle_chan_scanlist(void) { // toggle the selected channels scanlist setting - if ( SCANNER_IsScanning()) + if (SCANNER_IsScanning()) return; - if(!IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) { + if (!IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) { #ifdef ENABLE_SCAN_RANGES gScanRangeStart = gScanRangeStart ? 0 : gTxVfo->pRX->Frequency; #endif return; } - if (gTxVfo->SCANLIST1_PARTICIPATION) { - if (gTxVfo->SCANLIST2_PARTICIPATION) - gTxVfo->SCANLIST1_PARTICIPATION = 0; - else - gTxVfo->SCANLIST2_PARTICIPATION = 1; + + if (gTxVfo->SCANLIST1_PARTICIPATION ^ gTxVfo->SCANLIST2_PARTICIPATION) { + gTxVfo->SCANLIST2_PARTICIPATION = gTxVfo->SCANLIST1_PARTICIPATION; } else { - if (gTxVfo->SCANLIST2_PARTICIPATION) - gTxVfo->SCANLIST2_PARTICIPATION = 0; - else - gTxVfo->SCANLIST1_PARTICIPATION = 1; + gTxVfo->SCANLIST1_PARTICIPATION = !gTxVfo->SCANLIST1_PARTICIPATION; } SETTINGS_UpdateChannel(gTxVfo->CHANNEL_SAVE, gTxVfo, true); @@ -73,7 +68,6 @@ void toggle_chan_scanlist(void) { // toggle the selected channels scanlist se } static void processFKeyFunction(const KEY_Code_t Key, const bool beep) { - uint8_t Band; uint8_t Vfo = gEeprom.TX_VFO; if (gScreenToDisplay == DISPLAY_MENU) { @@ -81,8 +75,6 @@ static void processFKeyFunction(const KEY_Code_t Key, const bool beep) { gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; return; } - -// if (beep) gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL; switch (Key) { @@ -106,63 +98,59 @@ static void processFKeyFunction(const KEY_Code_t Key, const bool beep) { gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL; #ifdef ENABLE_COPY_CHAN_TO_VFO - if (gEeprom.VFO_OPEN && !gCssBackgroundScan) + if (!gEeprom.VFO_OPEN || gCssBackgroundScan) { - - if (gScanStateDir != SCAN_OFF) - { - if (gCurrentFunction != FUNCTION_INCOMING || - gRxReceptionMode == RX_MODE_NONE || - gScanPauseDelayIn_10ms == 0) - { // scan is running (not paused) - return; - } - } - - const uint8_t vfo = gEeprom.TX_VFO; - - if (IS_MR_CHANNEL(gEeprom.ScreenChannel[vfo])) - { // copy channel to VFO, then swap to the VFO - - const unsigned int channel = FREQ_CHANNEL_FIRST + gEeprom.VfoInfo[vfo].Band; - - gEeprom.ScreenChannel[vfo] = channel; - gEeprom.VfoInfo[vfo].CHANNEL_SAVE = channel; - - RADIO_SelectVfos(); - RADIO_ApplyOffset(gRxVfo); - RADIO_ConfigureSquelchAndOutputPower(gRxVfo); - RADIO_SetupRegisters(true); - - //SETTINGS_SaveChannel(channel, gEeprom.RX_VFO, gRxVfo, 1); - - gUpdateDisplay = true; - } +gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; + return; } - else + if (gScanStateDir != SCAN_OFF) { - gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; + if (gCurrentFunction != FUNCTION_INCOMING || + gRxReceptionMode == RX_MODE_NONE || + gScanPauseDelayIn_10ms == 0) + { // scan is running (not paused) + return; + } +} + const uint8_t vfo = gEeprom.TX_VFO; + if (IS_MR_CHANNEL(gEeprom.ScreenChannel[vfo])) + { // copy channel to VFO, then swap to the VFO + + gEeprom.ScreenChannel[vfo] = FREQ_CHANNEL_FIRST + gEeprom.VfoInfo[vfo].Band; + gEeprom.VfoInfo[vfo].CHANNEL_SAVE = gEeprom.ScreenChannel[vfo]; + + RADIO_SelectVfos(); + RADIO_ApplyOffset(gRxVfo); + RADIO_ConfigureSquelchAndOutputPower(gRxVfo); + RADIO_SetupRegisters(true); + //SETTINGS_SaveChannel(channel, gEeprom.RX_VFO, gRxVfo, 1); + + gUpdateDisplay = true; + } #endif return; } - if (gTxVfo->Band == 6 && gTxVfo->pRX->Frequency < 100000000) { - gTxVfo->pRX->Frequency = 100000000; - return; - } else { - Band = gTxVfo->Band + 1; -// if (gSetting_350EN || Band != BAND5_350MHz) { - if (Band > BAND7_470MHz) - Band = BAND1_50MHz; -// } else -// Band = BAND6_400MHz; - - gTxVfo->Band = Band; - - gEeprom.ScreenChannel[Vfo] = FREQ_CHANNEL_FIRST + Band; - gEeprom.FreqChannel[Vfo] = FREQ_CHANNEL_FIRST + Band; +#ifdef ENABLE_WIDE_RX + if(gTxVfo->Band == BAND7_470MHz && gTxVfo->pRX->Frequency < _1GHz_in_KHz) { + gTxVfo->pRX->Frequency = _1GHz_in_KHz; + return; } +#endif + + gTxVfo->Band += 1; + + if (gTxVfo->Band == BAND5_350MHz && gSetting_F_LOCK != F_LOCK_NONE) { + // skip if not enabled + gTxVfo->Band += 1; + } else if (gTxVfo->Band >= BAND_LAST_ELEMENT) { + // go arround if overflowed + gTxVfo->Band = BAND1_50MHz; + } + + gEeprom.ScreenChannel[Vfo] = FREQ_CHANNEL_FIRST + gTxVfo->Band; + gEeprom.FreqChannel[Vfo] = FREQ_CHANNEL_FIRST + gTxVfo->Band; gRequestSaveVFO = true; gVfoConfigureMode = VFO_CONFIGURE_RELOAD; @@ -315,8 +303,6 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) { if (IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) { // user is entering channel number - uint16_t Channel; - if (gInputBoxIndex != 3) { #ifdef ENABLE_VOICE gAnotherVoiceID = (VOICE_ID_t)Key; @@ -327,7 +313,7 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) { gInputBoxIndex = 0; - Channel = ((gInputBox[0] * 100) + (gInputBox[1] * 10) + gInputBox[2]) - 1; + const uint16_t Channel = ((gInputBox[0] * 100) + (gInputBox[1] * 10) + gInputBox[2]) - 1; if (!RADIO_CheckValidChannel(Channel, false, 0)) { gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; @@ -351,18 +337,16 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) { // #endif if (IS_FREQ_CHANNEL(gTxVfo->CHANNEL_SAVE)) { // user is entering a frequency - uint32_t Frequency; - bool isGigaF = gTxVfo->pRX->Frequency >= 100000000; - if (gInputBoxIndex < 6 + isGigaF) { #ifdef ENABLE_VOICE - gAnotherVoiceID = (VOICE_ID_t)Key; + gAnotherVoiceID = (VOICE_ID_t)Key; #endif - + bool isGigaF = gTxVfo->pRX->Frequency >= _1GHz_in_KHz; + if (gInputBoxIndex < 6 + isGigaF) { return; } gInputBoxIndex = 0; - Frequency = StrToUL(INPUTBOX_GetAscii()) * 100; + uint32_t Frequency = StrToUL(INPUTBOX_GetAscii()) * 100; // clamp the frequency entered to some valid value if (Frequency < frequencyBandTable[0].lower) { @@ -374,70 +358,61 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) { Frequency = frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper; } - { - const FREQUENCY_Band_t band = FREQUENCY_GetBand(Frequency); + const FREQUENCY_Band_t band = FREQUENCY_GetBand(Frequency); -#ifdef ENABLE_VOICE - gAnotherVoiceID = (VOICE_ID_t)Key; -#endif + if (gTxVfo->Band != band) { + gTxVfo->Band = band; + gEeprom.ScreenChannel[Vfo] = band + FREQ_CHANNEL_FIRST; + gEeprom.FreqChannel[Vfo] = band + FREQ_CHANNEL_FIRST; - if (gTxVfo->Band != band) { - gTxVfo->Band = band; - gEeprom.ScreenChannel[Vfo] = band + FREQ_CHANNEL_FIRST; - gEeprom.FreqChannel[Vfo] = band + FREQ_CHANNEL_FIRST; + SETTINGS_SaveVfoIndices(); - SETTINGS_SaveVfoIndices(); - - RADIO_ConfigureChannel(Vfo, VFO_CONFIGURE_RELOAD); - } - - Frequency = FREQUENCY_RoundToStep(Frequency, gTxVfo->StepFrequency); - - if (Frequency >= BX4819_band1.upper && - Frequency < BX4819_band2.lower) { // clamp the frequency to the limit - const uint32_t center = (BX4819_band1.upper + BX4819_band2.lower) / 2; - Frequency = (Frequency < center) ? BX4819_band1.upper - gTxVfo->StepFrequency : BX4819_band2.lower; - } - - gTxVfo->freq_config_RX.Frequency = Frequency; - - gRequestSaveChannel = 1; - return; + RADIO_ConfigureChannel(Vfo, VFO_CONFIGURE_RELOAD); } + Frequency = FREQUENCY_RoundToStep(Frequency, gTxVfo->StepFrequency); + + if (Frequency >= BX4819_band1.upper && + Frequency < BX4819_band2.lower) { // clamp the frequency to the limit + const uint32_t center = (BX4819_band1.upper + BX4819_band2.lower) / 2; + Frequency = (Frequency < center) ? BX4819_band1.upper - gTxVfo->StepFrequency : BX4819_band2.lower; + } + + gTxVfo->freq_config_RX.Frequency = Frequency; + + gRequestSaveChannel = 1; + return; + } #ifdef ENABLE_NOAA else - if (IS_NOAA_CHANNEL(gTxVfo->CHANNEL_SAVE)) - { // user is entering NOAA channel - - uint8_t Channel; - - if (gInputBoxIndex != 2) - { + if (IS_NOAA_CHANNEL(gTxVfo->CHANNEL_SAVE)) + { // user is entering NOAA channel + if (gInputBoxIndex != 2) + { #ifdef ENABLE_VOICE - gAnotherVoiceID = (VOICE_ID_t)Key; + gAnotherVoiceID = (VOICE_ID_t)Key; #endif - gRequestDisplayScreen = DISPLAY_MAIN; - return; - } + gRequestDisplayScreen = DISPLAY_MAIN; + return; + } - gInputBoxIndex = 0; + gInputBoxIndex = 0; - Channel = (gInputBox[0] * 10) + gInputBox[1]; - if (Channel >= 1 && Channel <= ARRAY_SIZE(NoaaFrequencyTable)) - { - Channel += NOAA_CHANNEL_FIRST; + uint8_t Channel = (gInputBox[0] * 10) + gInputBox[1]; + if (Channel >= 1 && Channel <= ARRAY_SIZE(NoaaFrequencyTable)) + { + Channel += NOAA_CHANNEL_FIRST; #ifdef ENABLE_VOICE - gAnotherVoiceID = (VOICE_ID_t)Key; + gAnotherVoiceID = (VOICE_ID_t)Key; #endif - gEeprom.NoaaChannel[Vfo] = Channel; - gEeprom.ScreenChannel[Vfo] = Channel; - gRequestSaveVFO = true; - gVfoConfigureMode = VFO_CONFIGURE_RELOAD; - return; + gEeprom.NoaaChannel[Vfo] = Channel; + gEeprom.ScreenChannel[Vfo] = Channel; + gRequestSaveVFO = true; + gVfoConfigureMode = VFO_CONFIGURE_RELOAD; + return; + } } - } #endif gRequestDisplayScreen = DISPLAY_MAIN; @@ -540,15 +515,14 @@ static void MAIN_Key_MENU(const bool bKeyPressed, const bool bKeyHeld) { } if (!bKeyPressed && !gDTMF_InputMode) { // menu key released - if(gWasFKeyPressed) - { - gWasFKeyPressed=false; - gEeprom.BEEP_CONTROL=!gEeprom.BEEP_CONTROL; - gRequestSaveSettings = 1; + if (gWasFKeyPressed) { + gWasFKeyPressed = false; + gEeprom.BEEP_CONTROL = !gEeprom.BEEP_CONTROL; + gRequestSaveSettings = 1; return; } - const bool bFlag = (gInputBoxIndex == 0); + const bool bFlag = !gInputBoxIndex; gInputBoxIndex = 0; if (bFlag) { @@ -607,8 +581,7 @@ static void MAIN_Key_STAR(bool bKeyPressed, bool bKeyHeld) { #ifdef ENABLE_SCAN_RANGES && gScanRangeStart == 0 #endif - ) - { // start entering a DTMF string + ) { // start entering a DTMF string gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL; memmove(gDTMF_InputBox, gDTMF_String, MIN(sizeof(gDTMF_InputBox), sizeof(gDTMF_String) - 1)); gDTMF_InputBox_Index = 0; @@ -617,8 +590,7 @@ static void MAIN_Key_STAR(bool bKeyPressed, bool bKeyHeld) { gKeyInputCountdown = key_input_timeout_500ms; gRequestDisplayScreen = DISPLAY_MAIN; - } - else + } else gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; } else { // with the F-key gWasFKeyPressed = false; diff --git a/app/spectrum.c b/app/spectrum.c index d8ac734..34645a5 100644 --- a/app/spectrum.c +++ b/app/spectrum.c @@ -15,10 +15,10 @@ */ #include "app/spectrum.h" -#include "driver/backlight.h" -#include "audio.h" -#include "ui/helper.h" #include "am_fix.h" +#include "audio.h" +#include "driver/backlight.h" +#include "ui/helper.h" #include "ui/main.h" struct FrequencyBandInfo { @@ -32,7 +32,6 @@ struct FrequencyBandInfo { const uint16_t RSSI_MAX_VALUE = 65535; - static uint16_t R30, R37, R3D, R43, R47, R48, R7E; static uint32_t initialFreq; static char String[32]; @@ -45,6 +44,7 @@ bool redrawScreen = false; bool newScanStart = true; bool preventKeypress = true; bool audioState = true; +bool lockAGC = false; State currentState = SPECTRUM, previousState = SPECTRUM; @@ -56,17 +56,17 @@ const char *bwOptions[] = {" 25k", "12.5k", "6.25k"}; const uint8_t modulationTypeTuneSteps[] = {100, 50, 10}; const uint8_t modTypeReg47Values[] = {1, 7, 5}; -SpectrumSettings settings = {stepsCount: STEPS_64, - scanStepIndex: S_STEP_25_0kHz, - frequencyChangeStep: 80000, - scanDelay: 3200, - rssiTriggerLevel: 150, - backlightState: true, - bw: BK4819_FILTER_BW_WIDE, - listenBw: BK4819_FILTER_BW_WIDE, - modulationType: false, - dbMin: -130, - dbMax: -50}; +SpectrumSettings settings = {.stepsCount = STEPS_64, + .scanStepIndex = S_STEP_25_0kHz, + .frequencyChangeStep = 80000, + .scanDelay = 3200, + .rssiTriggerLevel = 150, + .backlightState = true, + .bw = BK4819_FILTER_BW_WIDE, + .listenBw = BK4819_FILTER_BW_WIDE, + .modulationType = false, + .dbMin = -130, + .dbMax = -50}; uint32_t fMeasure = 0; uint32_t currentFreq, tempFreq; @@ -102,9 +102,8 @@ static uint8_t DBm2S(int dbm) { return i; } -static int Rssi2DBm(uint16_t rssi) -{ - return (rssi / 2) - 160 + dBmCorrTable[gRxVfo->Band]; +static int Rssi2DBm(uint16_t rssi) { + return (rssi / 2) - 160 + dBmCorrTable[gRxVfo->Band]; } static uint16_t GetRegMenuValue(uint8_t st) { @@ -112,10 +111,20 @@ static uint16_t GetRegMenuValue(uint8_t st) { return (BK4819_ReadRegister(s.num) >> s.offset) & s.mask; } +void LockAGC() +{ + if(!lockAGC) + BK4819_SetAGC(0); + lockAGC = true; +} + static void SetRegMenuValue(uint8_t st, bool add) { uint16_t v = GetRegMenuValue(st); RegisterSpec s = registerSpecs[st]; + if(s.num == BK4819_REG_13) + LockAGC(); + uint16_t reg = BK4819_ReadRegister(s.num); if (add && v <= s.mask - s.inc) { v += s.inc; @@ -282,15 +291,12 @@ uint16_t GetRssi() { while ((BK4819_ReadRegister(0x63) & 0b11111111) >= 255) { SYSTICK_DelayUs(100); } - if(settings.modulationType == MODULATION_AM) - { - return BK4819_GetRSSI() - rssi_gain_diff[vfo]; //return the corrected RSSI to allow for AM_Fixs AGC action. - } - else - { - return BK4819_GetRSSI(); - } + return BK4819_GetRSSI() +#ifdef ENABLE_AM_FIX + - ((settings.modulationType == MODULATION_AM) ? AM_fix_get_rssi_gain_diff(vfo) : 0) +#endif + ; } static void ToggleAudio(bool on) { @@ -395,14 +401,14 @@ static void Measure() { rssiHistory[scanInfo.i] = scanInfo.rssi = GetRssi(); } // Update things by keypress -static uint16_t dbm2rssi(int dBm) -{ - return (dBm + 160 - dBmCorrTable[gRxVfo->Band])*2; +static uint16_t dbm2rssi(int dBm) { + return (dBm + 160 - dBmCorrTable[gRxVfo->Band]) * 2; } -static void ClampRssiTriggerLevel() -{ - settings.rssiTriggerLevel = clamp(settings.rssiTriggerLevel, dbm2rssi(settings.dbMin), dbm2rssi(settings.dbMax)); +static void ClampRssiTriggerLevel() { + settings.rssiTriggerLevel = + clamp(settings.rssiTriggerLevel, dbm2rssi(settings.dbMin), + dbm2rssi(settings.dbMax)); } static void UpdateRssiTriggerLevel(bool inc) { @@ -489,6 +495,10 @@ static void ToggleModulation() { settings.modulationType = MODULATION_FM; } RADIO_SetModulation(settings.modulationType); + if(settings.modulationType != MODULATION_AM) { + BK4819_InitAGC(); + BK4819_SetAGC(1); + } RelaunchScan(); redrawScreen = true; } @@ -549,7 +559,7 @@ static void UpdateFreqInput(KEY_Code_t key) { } if (key == KEY_EXIT) { freqInputIndex--; - if(freqInputDotIndex==freqInputIndex) + if (freqInputDotIndex == freqInputIndex) freqInputDotIndex = 0; } else { freqInputArr[freqInputIndex++] = key; @@ -564,7 +574,7 @@ static void UpdateFreqInput(KEY_Code_t key) { for (int i = 0; i < 10; ++i) { if (i < freqInputIndex) { digitKey = freqInputArr[i]; - freqInputString[i] = digitKey <= KEY_9 ? '0' + digitKey-KEY_0 : '.'; + freqInputString[i] = digitKey <= KEY_9 ? '0' + digitKey - KEY_0 : '.'; } else { freqInputString[i] = '-'; } @@ -572,14 +582,14 @@ static void UpdateFreqInput(KEY_Code_t key) { uint32_t base = 100000; // 1MHz in BK units for (int i = dotIndex - 1; i >= 0; --i) { - tempFreq += (freqInputArr[i]-KEY_0) * base; + tempFreq += (freqInputArr[i] - KEY_0) * base; base *= 10; } base = 10000; // 0.1MHz in BK units if (dotIndex < freqInputIndex) { for (int i = dotIndex + 1; i < freqInputIndex; ++i) { - tempFreq += (freqInputArr[i]-KEY_0) * base; + tempFreq += (freqInputArr[i] - KEY_0) * base; base /= 10; } } @@ -630,10 +640,11 @@ static void DrawStatus() { #endif GUI_DisplaySmallest(String, 0, 1, true, true); - BOARD_ADC_GetBatteryInfo(&gBatteryVoltages[gBatteryCheckCounter++ % 4], &gBatteryCurrent); + BOARD_ADC_GetBatteryInfo(&gBatteryVoltages[gBatteryCheckCounter++ % 4], + &gBatteryCurrent); - uint16_t voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + gBatteryVoltages[2] + - gBatteryVoltages[3]) / + uint16_t voltage = (gBatteryVoltages[0] + gBatteryVoltages[1] + + gBatteryVoltages[2] + gBatteryVoltages[3]) / 4 * 760 / gBatteryCalibration[3]; unsigned perc = BATTERY_VoltsToPercent(voltage); @@ -648,7 +659,7 @@ static void DrawStatus() { } for (unsigned i = 127; i >= 118; i--) { - if (127 - i <= (perc+5)*9/100) { + if (127 - i <= (perc + 5) * 9 / 100) { gStatusLine[i] = 0b00111110; } } @@ -901,6 +912,7 @@ void OnKeyDownStill(KEY_Code_t key) { case KEY_EXIT: if (!menuState) { SetState(SPECTRUM); + lockAGC = false; monitorMode = false; RelaunchScan(); break; @@ -912,9 +924,7 @@ void OnKeyDownStill(KEY_Code_t key) { } } -static void RenderFreqInput() { - UI_PrintStringSmall(freqInputString, 2, 127, 0); -} +static void RenderFreqInput() { UI_PrintStringSmall(freqInputString, 2, 127, 0); } static void RenderStatus() { memset(gStatusLine, 0, sizeof(gStatusLine)); @@ -1014,13 +1024,12 @@ bool HandleUserInput() { kbd.current = GetKey(); if (kbd.current != KEY_INVALID && kbd.current == kbd.prev) { - if(kbd.counter < 16) + if (kbd.counter < 16) kbd.counter++; else - kbd.counter-=3; + kbd.counter -= 3; SYSTEM_DelayMs(20); - } - else { + } else { kbd.counter = 0; } @@ -1119,11 +1128,12 @@ static void UpdateListening() { } static void Tick() { +#ifdef ENABLE_AM_FIX + if(settings.modulationType == MODULATION_AM) { + AM_fix_10ms(vfo, !lockAGC); //allow AM_Fix to apply its AGC action + } +#endif - if(settings.modulationType == MODULATION_AM) - { - AM_fix_10ms(vfo); //allow AM_Fix to apply its AGC action - } if (!preventKeypress) { HandleUserInput(); } @@ -1153,9 +1163,10 @@ static void Tick() { void APP_RunSpectrum() { // TX here coz it always? set to active VFO - vfo = gEeprom.TX_VFO; - //set the current frequency in the middle of the display - currentFreq = initialFreq = gEeprom.VfoInfo[vfo].pRX->Frequency - ((GetStepsCount()/2) * GetScanStep()); + vfo = gEeprom.TX_VFO; + // set the current frequency in the middle of the display + currentFreq = initialFreq = gEeprom.VfoInfo[vfo].pRX->Frequency - + ((GetStepsCount() / 2) * GetScanStep()); BackupRegisters(); @@ -1166,6 +1177,14 @@ 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(); + BK4819_SetAGC(1); + } +#endif + BK4819_SetFilterBandwidth(settings.listenBw = BK4819_FILTER_BW_WIDE, false); RelaunchScan(); diff --git a/board.c b/board.c index a774edd..5fcffaa 100644 --- a/board.c +++ b/board.c @@ -493,7 +493,7 @@ void BOARD_Init(void) BOARD_GPIO_Init(); BACKLIGHT_InitHardware(); BOARD_ADC_Init(); - ST7565_Init(true); + ST7565_Init(); #ifdef ENABLE_FMRADIO BK1080_Init(0, false); #endif diff --git a/driver/st7565.c b/driver/st7565.c index 212d188..177d8aa 100644 --- a/driver/st7565.c +++ b/driver/st7565.c @@ -25,119 +25,58 @@ #include "driver/system.h" #include "misc.h" -uint8_t gStatusLine[128]; -uint8_t gFrameBuffer[7][128]; +uint8_t gStatusLine[LCD_WIDTH]; +uint8_t gFrameBuffer[FRAME_LINES][LCD_WIDTH]; -void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const unsigned int Size, const uint8_t *pBitmap) +static void DrawLine(uint8_t column, uint8_t line, const uint8_t * lineBuffer, unsigned size_defVal) { - unsigned int i; + ST7565_SelectColumnAndLine(column + 4, line); + GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); + for (unsigned i = 0; i < size_defVal; i++) { + while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} + SPI0->WDR = lineBuffer ? lineBuffer[i] : size_defVal; + } + SPI_WaitForUndocumentedTxFifoStatusBit(); +} - SPI_ToggleMasterMode(&SPI0->CR, false); - - ST7565_SelectColumnAndLine(Column + 4U, Line); - - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - - if (pBitmap != NULL) - { - for (i = 0; i < Size; i++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = pBitmap[i]; - } - } - else - { - for (i = 0; i < Size; i++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = 0; - } - } - - SPI_WaitForUndocumentedTxFifoStatusBit(); - - SPI_ToggleMasterMode(&SPI0->CR, true); +void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const uint8_t *pBitmap, const unsigned int Size) +{ + SPI_ToggleMasterMode(&SPI0->CR, false); + DrawLine(Column, Line, pBitmap, Size); + SPI_ToggleMasterMode(&SPI0->CR, true); } void ST7565_BlitFullScreen(void) { - unsigned int Line; - - SPI_ToggleMasterMode(&SPI0->CR, false); - - ST7565_WriteByte(0x40); - - for (Line = 0; Line < ARRAY_SIZE(gFrameBuffer); Line++) - { - unsigned int Column; - ST7565_SelectColumnAndLine(4, Line + 1); - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - for (Column = 0; Column < ARRAY_SIZE(gFrameBuffer[0]); Column++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = gFrameBuffer[Line][Column]; - } - SPI_WaitForUndocumentedTxFifoStatusBit(); - } - - #if 0 - // whats the delay for I wonder, it holds things up :( - SYSTEM_DelayMs(20); - #else -// SYSTEM_DelayMs(1); - #endif - - SPI_ToggleMasterMode(&SPI0->CR, true); + SPI_ToggleMasterMode(&SPI0->CR, false); + ST7565_WriteByte(0x40); + for (unsigned line = 0; line < FRAME_LINES; line++) { + DrawLine(0, line+1, gFrameBuffer[line], LCD_WIDTH); + } + SPI_ToggleMasterMode(&SPI0->CR, true); +} +void ST7565_BlitLine(unsigned line) +{ + SPI_ToggleMasterMode(&SPI0->CR, false); + ST7565_WriteByte(0x40); // start line ? + DrawLine(0, line+1, gFrameBuffer[line], LCD_WIDTH); + SPI_ToggleMasterMode(&SPI0->CR, true); } - void ST7565_BlitStatusLine(void) { // the top small text line on the display - - unsigned int i; - - SPI_ToggleMasterMode(&SPI0->CR, false); - - ST7565_WriteByte(0x40); // start line ? - - ST7565_SelectColumnAndLine(4, 0); - - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - - for (i = 0; i < ARRAY_SIZE(gStatusLine); i++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = gStatusLine[i]; - } - - SPI_WaitForUndocumentedTxFifoStatusBit(); - - SPI_ToggleMasterMode(&SPI0->CR, true); + SPI_ToggleMasterMode(&SPI0->CR, false); + ST7565_WriteByte(0x40); // start line ? + DrawLine(0, 0, gStatusLine, LCD_WIDTH); + SPI_ToggleMasterMode(&SPI0->CR, true); } -void ST7565_FillScreen(uint8_t Value) +void ST7565_FillScreen(uint8_t value) { - unsigned int i; - - // reset some of the displays settings to try and overcome the radios hardware problem - RF corrupting the display - ST7565_Init(false); - - SPI_ToggleMasterMode(&SPI0->CR, false); - - for (i = 0; i < 8; i++) - { - unsigned int j; - ST7565_SelectColumnAndLine(0, i); - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - for (j = 0; j < 132; j++) - { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = Value; - } - SPI_WaitForUndocumentedTxFifoStatusBit(); - } - - SPI_ToggleMasterMode(&SPI0->CR, true); + SPI_ToggleMasterMode(&SPI0->CR, false); + for (unsigned i = 0; i < 8; i++) { + DrawLine(0, i, NULL, value); + } + SPI_ToggleMasterMode(&SPI0->CR, true); } // Software reset @@ -181,100 +120,93 @@ const uint8_t ST7565_CMD_SET_EV = 0x81; // VF: Built-in Follower const uint8_t ST7565_CMD_POWER_CIRCUIT = 0x28; // Set display start line 0-63 -// 0 0 0 1 S5 S4 S3 S2 S1 S0 +// 0 0 0 1 S5 S4 S3 S2 S1 S0 const uint8_t ST7565_CMD_SET_START_LINE = 0x40; -// Display ON/OFF -// 0 0 1 0 1 0 1 1 1 D +// Display ON/OFF +// 0 0 1 0 1 0 1 1 1 D // D=1, display ON // D=0, display OFF const uint8_t ST7565_CMD_DISPLAY_ON_OFF = 0xAE; uint8_t cmds[] = { - ST7565_CMD_BIAS_SELECT | 0, // Select bias setting: 1/9 - ST7565_CMD_COM_DIRECTION | (0 << 3), // Set output direction of COM: normal - ST7565_CMD_SEG_DIRECTION | 1, // Set scan direction of SEG: reverse - ST7565_CMD_INVERSE_DISPLAY | 0, // Inverse Display: false - ST7565_CMD_ALL_PIXEL_ON | 0, // All Pixel ON: false - normal display - ST7565_CMD_REGULATION_RATIO | (4 << 0), // Regulation Ratio 5.0 + ST7565_CMD_BIAS_SELECT | 0, // Select bias setting: 1/9 + ST7565_CMD_COM_DIRECTION | (0 << 3), // Set output direction of COM: normal + ST7565_CMD_SEG_DIRECTION | 1, // Set scan direction of SEG: reverse + ST7565_CMD_INVERSE_DISPLAY | 0, // Inverse Display: false + ST7565_CMD_ALL_PIXEL_ON | 0, // All Pixel ON: false - normal display + ST7565_CMD_REGULATION_RATIO | (4 << 0), // Regulation Ratio 5.0 - ST7565_CMD_SET_EV, // Set contrast - 31, + ST7565_CMD_SET_EV, // Set contrast + 31, - ST7565_CMD_POWER_CIRCUIT | 0b111, // Built-in power circuit ON/OFF: VB=1 VR=1 VF=1 - ST7565_CMD_SET_START_LINE | 0, // Set Start Line: 0 - ST7565_CMD_DISPLAY_ON_OFF | 1, // Display ON/OFF: ON + ST7565_CMD_POWER_CIRCUIT | 0b111, // Built-in power circuit ON/OFF: VB=1 VR=1 VF=1 + ST7565_CMD_SET_START_LINE | 0, // Set Start Line: 0 + ST7565_CMD_DISPLAY_ON_OFF | 1, // Display ON/OFF: ON }; -void ST7565_Init(const bool full) +void ST7565_Init(void) { - if (full) { - SPI0_Init(); - ST7565_HardwareReset(); - SPI_ToggleMasterMode(&SPI0->CR, false); - ST7565_WriteByte(ST7565_CMD_SOFTWARE_RESET); // software reset - SYSTEM_DelayMs(120); - } - else - SPI_ToggleMasterMode(&SPI0->CR, false); + SPI0_Init(); + ST7565_HardwareReset(); + SPI_ToggleMasterMode(&SPI0->CR, false); + ST7565_WriteByte(ST7565_CMD_SOFTWARE_RESET); // software reset + SYSTEM_DelayMs(120); - for(uint8_t i = 0; i < 8; i++) - ST7565_WriteByte(cmds[i]); + for(uint8_t i = 0; i < 8; i++) + ST7565_WriteByte(cmds[i]); - if (full) { - ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b011); // VB=0 VR=1 VF=1 - SYSTEM_DelayMs(1); - ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b110); // VB=1 VR=1 VF=0 - SYSTEM_DelayMs(1); - - for(uint8_t i = 0; i < 4; i++) // why 4 times? - ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b111); // VB=1 VR=1 VF=1 + ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b011); // VB=0 VR=1 VF=1 + SYSTEM_DelayMs(1); + ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b110); // VB=1 VR=1 VF=0 + SYSTEM_DelayMs(1); - SYSTEM_DelayMs(40); - } + for(uint8_t i = 0; i < 4; i++) // why 4 times? + ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b111); // VB=1 VR=1 VF=1 - ST7565_WriteByte(ST7565_CMD_SET_START_LINE | 0); // line 0 - ST7565_WriteByte(ST7565_CMD_DISPLAY_ON_OFF | 1); // D=1 - SPI_WaitForUndocumentedTxFifoStatusBit(); - SPI_ToggleMasterMode(&SPI0->CR, true); + SYSTEM_DelayMs(40); - if (full) - ST7565_FillScreen(0x00); + ST7565_WriteByte(ST7565_CMD_SET_START_LINE | 0); // line 0 + ST7565_WriteByte(ST7565_CMD_DISPLAY_ON_OFF | 1); // D=1 + SPI_WaitForUndocumentedTxFifoStatusBit(); + SPI_ToggleMasterMode(&SPI0->CR, true); + + ST7565_FillScreen(0x00); } void ST7565_FixInterfGlitch(void) { - SPI_ToggleMasterMode(&SPI0->CR, false); - for(uint8_t i = 0; i < ARRAY_SIZE(cmds); i++) - ST7565_WriteByte(cmds[i]); - SPI_WaitForUndocumentedTxFifoStatusBit(); - SPI_ToggleMasterMode(&SPI0->CR, true); + SPI_ToggleMasterMode(&SPI0->CR, false); + for(uint8_t i = 0; i < ARRAY_SIZE(cmds); i++) + ST7565_WriteByte(cmds[i]); + SPI_WaitForUndocumentedTxFifoStatusBit(); + SPI_ToggleMasterMode(&SPI0->CR, true); } void ST7565_HardwareReset(void) { - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES); - SYSTEM_DelayMs(1); - GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES); - SYSTEM_DelayMs(20); - GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES); - SYSTEM_DelayMs(120); + GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES); + SYSTEM_DelayMs(1); + GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES); + SYSTEM_DelayMs(20); + GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES); + SYSTEM_DelayMs(120); } void ST7565_SelectColumnAndLine(uint8_t Column, uint8_t Line) { - GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = Line + 176; - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = ((Column >> 4) & 0x0F) | 0x10; - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = ((Column >> 0) & 0x0F); - SPI_WaitForUndocumentedTxFifoStatusBit(); + GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); + while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} + SPI0->WDR = Line + 176; + while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} + SPI0->WDR = ((Column >> 4) & 0x0F) | 0x10; + while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} + SPI0->WDR = ((Column >> 0) & 0x0F); + SPI_WaitForUndocumentedTxFifoStatusBit(); } void ST7565_WriteByte(uint8_t Value) { - GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = Value; -} + GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); + while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} + SPI0->WDR = Value; +} \ No newline at end of file diff --git a/driver/st7565.h b/driver/st7565.h index 6d49fe6..1bffa7c 100644 --- a/driver/st7565.h +++ b/driver/st7565.h @@ -22,19 +22,20 @@ #define LCD_WIDTH 128 #define LCD_HEIGHT 64 +#define FRAME_LINES 7 -extern uint8_t gStatusLine[128]; -extern uint8_t gFrameBuffer[7][128]; +extern uint8_t gStatusLine[LCD_WIDTH]; +extern uint8_t gFrameBuffer[FRAME_LINES][LCD_WIDTH]; -void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const unsigned int Size, const uint8_t *pBitmap); +void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const uint8_t *pBitmap, const unsigned int Size); void ST7565_BlitFullScreen(void); +void ST7565_BlitLine(unsigned line); void ST7565_BlitStatusLine(void); void ST7565_FillScreen(uint8_t Value); -void ST7565_Init(const bool full); +void ST7565_Init(void); void ST7565_FixInterfGlitch(void); void ST7565_HardwareReset(void); void ST7565_SelectColumnAndLine(uint8_t Column, uint8_t Line); void ST7565_WriteByte(uint8_t Value); -#endif - +#endif \ No newline at end of file diff --git a/frequencies.c b/frequencies.c index 34b5c28..ab56c5b 100644 --- a/frequencies.c +++ b/frequencies.c @@ -19,34 +19,33 @@ #include "settings.h" // the BK4819 has 2 bands it covers, 18MHz ~ 630MHz and 760MHz ~ 1300MHz -const freq_band_table_t BX4819_band1 = { 1800000, 63000000}; -const freq_band_table_t BX4819_band2 = {84000000, 130000000}; -const freq_band_table_t frequencyBandTable[7] = -{ - #ifndef ENABLE_WIDE_RX - // QS original - {.lower = 5000000, .upper = 7600000}, - {.lower = 10800000, .upper = 13700000}, - {.lower = 13700000, .upper = 17400000}, - {.lower = 17400000, .upper = 35000000}, - {.lower = 35000000, .upper = 40000000}, - {.lower = 40000000, .upper = 47000000}, - {.lower = 47000000, .upper = 60000000} - #else - // extended range - {.lower = BX4819_band1.lower, .upper = 10800000}, - {.lower = 10800000, .upper = 13700000}, - {.lower = 13700000, .upper = 17400000}, - {.lower = 17400000, .upper = 35000000}, - {.lower = 35000000, .upper = 40000000}, - {.lower = 40000000, .upper = 47000000}, - {.lower = 47000000, .upper = BX4819_band2.upper} - #endif -}; +#define BX4819_band1_lower 1800000 +#define BX4819_band2_upper 130000000 + +const freq_band_table_t BX4819_band1 = {BX4819_band1_lower, 63000000}; +const freq_band_table_t BX4819_band2 = {84000000, BX4819_band2_upper}; + +const freq_band_table_t frequencyBandTable[] = + { +#ifndef ENABLE_WIDE_RX + // QS original + [BAND1_50MHz ]={.lower = 5000000, .upper = 7600000}, + [BAND7_470MHz]={.lower = 47000000, .upper = 60000000}, +#else + // extended range + [BAND1_50MHz ]={.lower = BX4819_band1_lower, .upper = 10800000}, + [BAND7_470MHz]={.lower = 47000000, .upper = BX4819_band2_upper}, +#endif + [BAND2_108MHz]={.lower = 10800000, .upper = 13700000}, + [BAND3_137MHz]={.lower = 13700000, .upper = 17400000}, + [BAND4_174MHz]={.lower = 17400000, .upper = 35000000}, + [BAND5_350MHz]={.lower = 35000000, .upper = 40000000}, + [BAND6_400MHz]={.lower = 40000000, .upper = 47000000} + }; #ifdef ENABLE_NOAA - const uint32_t NoaaFrequencyTable[10] = +const uint32_t NoaaFrequencyTable[10] = { 16255000, 16240000, @@ -63,159 +62,159 @@ const freq_band_table_t frequencyBandTable[7] = const uint16_t gStepFrequencyTable[] = { - 250, 500, 625, 1000, 1250, 2500, 833, - 1, 5, 10, 25, 50, 100, 125, 1500, 3000, 5000, 10000, 12500, 25000, 50000 - }; - - -const uint8_t StepSortedIndexes[] = { - STEP_0_01kHz, STEP_0_05kHz, STEP_0_1kHz, STEP_0_25kHz, STEP_0_5kHz, STEP_1kHz, STEP_1_25kHz, STEP_2_5kHz, STEP_5kHz, STEP_6_25kHz, - STEP_8_33kHz, STEP_10kHz, STEP_12_5kHz, STEP_15kHz, STEP_25kHz, STEP_30kHz, STEP_50kHz, STEP_100kHz, - STEP_125kHz, STEP_250kHz, STEP_500kHz + 250, 500, 625, 1000, 1250, 2500, 833, + 1, 5, 10, 25, 50, 100, 125, 1500, 3000, 5000, 10000, 12500, 25000, 50000 }; -uint8_t FREQUENCY_GetStepIdxFromSortedIdx(uint8_t sortedIdx) + + +const STEP_Setting_t StepSortedIndexes[] = { + STEP_0_01kHz, STEP_0_05kHz, STEP_0_1kHz, STEP_0_25kHz, STEP_0_5kHz, STEP_1kHz, STEP_1_25kHz, STEP_2_5kHz, STEP_5kHz, STEP_6_25kHz, + STEP_8_33kHz, STEP_10kHz, STEP_12_5kHz, STEP_15kHz, STEP_25kHz, STEP_30kHz, STEP_50kHz, STEP_100kHz, + STEP_125kHz, STEP_250kHz, STEP_500kHz +}; + +STEP_Setting_t FREQUENCY_GetStepIdxFromSortedIdx(uint8_t sortedIdx) { - return StepSortedIndexes[sortedIdx]; + return StepSortedIndexes[sortedIdx]; } -uint8_t FREQUENCY_GetSortedIdxFromStepIdx(uint8_t stepIdx) + +uint32_t FREQUENCY_GetSortedIdxFromStepIdx(uint8_t stepIdx) { - for(uint8_t i = 0; i < ARRAY_SIZE(gStepFrequencyTable); i++) - if(StepSortedIndexes[i] == stepIdx) - return i; - return 0; + for(uint8_t i = 0; i < ARRAY_SIZE(gStepFrequencyTable); i++) + if(StepSortedIndexes[i] == stepIdx) + return i; + return 0; } FREQUENCY_Band_t FREQUENCY_GetBand(uint32_t Frequency) { - int band; - for (band = ARRAY_SIZE(frequencyBandTable) - 1; band >= 0; band--) - if (Frequency >= frequencyBandTable[band].lower) + for (int band = ARRAY_SIZE(frequencyBandTable) - 1; band >= 0; band--) + if (Frequency >= frequencyBandTable[band].lower) // if (Frequency < frequencyBandTable[band].upper) - return (FREQUENCY_Band_t)band; + return (FREQUENCY_Band_t)band; - return BAND1_50MHz; + return BAND1_50MHz; // return BAND_NONE; } uint8_t FREQUENCY_CalculateOutputPower(uint8_t TxpLow, uint8_t TxpMid, uint8_t TxpHigh, int32_t LowerLimit, int32_t Middle, int32_t UpperLimit, int32_t Frequency) { - if (Frequency <= LowerLimit) - return TxpLow; + if (Frequency <= LowerLimit) + return TxpLow; - if (UpperLimit <= Frequency) - return TxpHigh; + if (UpperLimit <= Frequency) + return TxpHigh; - if (Frequency <= Middle) - { - TxpMid += ((TxpMid - TxpLow) * (Frequency - LowerLimit)) / (Middle - LowerLimit); - return TxpMid; - } + if (Frequency <= Middle) + { + TxpMid += ((TxpMid - TxpLow) * (Frequency - LowerLimit)) / (Middle - LowerLimit); + return TxpMid; + } - TxpMid += ((TxpHigh - TxpMid) * (Frequency - Middle)) / (UpperLimit - Middle); + TxpMid += ((TxpHigh - TxpMid) * (Frequency - Middle)) / (UpperLimit - Middle); - return TxpMid; + return TxpMid; } - uint32_t FREQUENCY_RoundToStep(uint32_t freq, uint16_t step) { - if(step == 833) { + if(step == 833) { uint32_t base = freq/2500*2500; int chno = (freq - base) / 700; // convert entered aviation 8.33Khz channel number scheme to actual frequency. return base + (chno * 833) + (chno == 3); - } + } if(step == 1) return freq; return (freq + (step + 1) / 2) / step * step; } -int TX_freq_check(const uint32_t Frequency) +int32_t TX_freq_check(const uint32_t Frequency) { // return '0' if TX frequency is allowed - // otherwise return '-1' + // otherwise return '-1' - if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) - return -1; // not allowed outside this range + if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) + return -1; // not allowed outside this range - if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) - return -1; // BX chip does not work in this range + if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) + return -1; // BX chip does not work in this range - switch (gSetting_F_LOCK) - { - case F_LOCK_DEF: - if (Frequency >= frequencyBandTable[BAND3_137MHz].lower && Frequency < frequencyBandTable[BAND3_137MHz].upper) - return 0; -// if (Frequency >= frequencyBandTable[BAND4_174MHz].lower && Frequency < frequencyBandTable[BAND4_174MHz].upper) -// if (gSetting_F_LOCK==F_LOCK_ALL) -// return 0; -// if (Frequency >= frequencyBandTable[BAND5_350MHz].lower && Frequency < frequencyBandTable[BAND5_350MHz].upper) -// if (gSetting_F_LOCK==F_LOCK_ALL) -// return 0; - if (Frequency >= frequencyBandTable[BAND6_400MHz].lower && Frequency < frequencyBandTable[BAND6_400MHz].upper) - return 0; -// if (Frequency >= frequencyBandTable[BAND7_470MHz].lower && Frequency <= 60000000) -// if (gSetting_F_LOCK==F_LOCK_ALL) -// return 0; - break; + switch (gSetting_F_LOCK) + { + case F_LOCK_DEF: + if (Frequency >= frequencyBandTable[BAND3_137MHz].lower && Frequency < frequencyBandTable[BAND3_137MHz].upper) + return 0; +// if (Frequency >= frequencyBandTable[BAND4_174MHz].lower && Frequency < frequencyBandTable[BAND4_174MHz].upper) +// if (gSetting_200TX) +// return 0; +// if (Frequency >= frequencyBandTable[BAND5_350MHz].lower && Frequency < frequencyBandTable[BAND5_350MHz].upper) +// if (gSetting_350TX && gSetting_350EN) +// return 0; + if (Frequency >= frequencyBandTable[BAND6_400MHz].lower && Frequency < frequencyBandTable[BAND6_400MHz].upper) + return 0; +// if (Frequency >= frequencyBandTable[BAND7_470MHz].lower && Frequency <= 60000000) +// if (gSetting_500TX) +// return 0; + break; - case F_LOCK_FCC: - if (Frequency >= 14400000 && Frequency < 14800000) - return 0; - if (Frequency >= 42000000 && Frequency < 45000000) - return 0; - break; + case F_LOCK_FCC: + if (Frequency >= 14400000 && Frequency < 14800000) + return 0; + if (Frequency >= 42000000 && Frequency < 45000000) + return 0; + break; - case F_LOCK_CE: - if (Frequency >= 14400000 && Frequency < 14600000) - return 0; - if (Frequency >= 43000000 && Frequency < 44000000) - return 0; - break; + case F_LOCK_CE: + if (Frequency >= 14400000 && Frequency < 14600000) + return 0; + if (Frequency >= 43000000 && Frequency < 44000000) + return 0; + break; - case F_LOCK_GB: - if (Frequency >= 14400000 && Frequency < 14800000) - return 0; - if (Frequency >= 43000000 && Frequency < 44000000) - return 0; - break; + case F_LOCK_GB: + if (Frequency >= 14400000 && Frequency < 14800000) + return 0; + if (Frequency >= 43000000 && Frequency < 44000000) + return 0; + break; - case F_LOCK_430: - if (Frequency >= frequencyBandTable[BAND3_137MHz].lower && Frequency < 17400000) - return 0; - if (Frequency >= 40000000 && Frequency < 43000000) - return 0; - break; + case F_LOCK_430: + if (Frequency >= frequencyBandTable[BAND3_137MHz].lower && Frequency < 17400000) + return 0; + if (Frequency >= 40000000 && Frequency < 43000000) + return 0; + break; - case F_LOCK_438: - if (Frequency >= frequencyBandTable[BAND3_137MHz].lower && Frequency < 17400000) - return 0; - if (Frequency >= 40000000 && Frequency < 43800000) - return 0; - break; + case F_LOCK_438: + if (Frequency >= frequencyBandTable[BAND3_137MHz].lower && Frequency < 17400000) + return 0; + if (Frequency >= 40000000 && Frequency < 43800000) + return 0; + break; - case F_LOCK_ALL: - break; + case F_LOCK_ALL: + break; - case F_LOCK_NONE: - for (uint8_t i = 0; i < ARRAY_SIZE(frequencyBandTable); i++) - if (Frequency >= frequencyBandTable[i].lower && Frequency < frequencyBandTable[i].upper) - return 0; - break; - } + case F_LOCK_NONE: + for (uint8_t i = 0; i < ARRAY_SIZE(frequencyBandTable); i++) + if (Frequency >= frequencyBandTable[i].lower && Frequency < frequencyBandTable[i].upper) + return 0; + break; + } - // dis-allowed TX frequency - return -1; + // dis-allowed TX frequency + return -1; } -int RX_freq_check(const uint32_t Frequency) +int32_t RX_freq_check(const uint32_t Frequency) { // return '0' if RX frequency is allowed - // otherwise return '-1' + // otherwise return '-1' - if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) - return -1; + if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) + return -1; - if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) - return -1; + if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) + return -1; - return 0; // OK frequency -} + return 0; // OK frequency +} \ No newline at end of file diff --git a/frequencies.h b/frequencies.h index e721452..b970de8 100644 --- a/frequencies.h +++ b/frequencies.h @@ -19,9 +19,11 @@ #include +#define _1GHz_in_KHz 100000000 + typedef struct { - const uint32_t lower; - const uint32_t upper; + const uint32_t lower; + const uint32_t upper; } freq_band_table_t; extern const freq_band_table_t BX4819_band1; @@ -30,56 +32,57 @@ extern const freq_band_table_t BX4819_band2; extern const freq_band_table_t frequencyBandTable[7]; typedef enum { - BAND_NONE = -1, - BAND1_50MHz = 0, - BAND2_108MHz, - BAND3_137MHz, - BAND4_174MHz, - BAND5_350MHz, - BAND6_400MHz, - BAND7_470MHz + BAND_NONE = -1, + BAND1_50MHz = 0, + BAND2_108MHz, + BAND3_137MHz, + BAND4_174MHz, + BAND5_350MHz, + BAND6_400MHz, + BAND7_470MHz, + BAND_LAST_ELEMENT //keep this guard as last element } FREQUENCY_Band_t; typedef enum { - STEP_2_5kHz, - STEP_5kHz, - STEP_6_25kHz, - STEP_10kHz, - STEP_12_5kHz, - STEP_25kHz, - STEP_8_33kHz, - STEP_0_01kHz, - STEP_0_05kHz, - STEP_0_1kHz, - STEP_0_25kHz, - STEP_0_5kHz, - STEP_1kHz, - STEP_1_25kHz, - STEP_15kHz, - STEP_30kHz, - STEP_50kHz, - STEP_100kHz, - STEP_125kHz, - STEP_250kHz, - STEP_500kHz, + STEP_2_5kHz, + STEP_5kHz, + STEP_6_25kHz, + STEP_10kHz, + STEP_12_5kHz, + STEP_25kHz, + STEP_8_33kHz, + STEP_0_01kHz, + STEP_0_05kHz, + STEP_0_1kHz, + STEP_0_25kHz, + STEP_0_5kHz, + STEP_1kHz, + STEP_1_25kHz, + STEP_15kHz, + STEP_30kHz, + STEP_50kHz, + STEP_100kHz, + STEP_125kHz, + STEP_250kHz, + STEP_500kHz, } STEP_Setting_t; extern const uint16_t gStepFrequencyTable[21]; #ifdef ENABLE_NOAA - extern const uint32_t NoaaFrequencyTable[10]; +extern const uint32_t NoaaFrequencyTable[10]; #endif FREQUENCY_Band_t FREQUENCY_GetBand(uint32_t Frequency); uint8_t FREQUENCY_CalculateOutputPower(uint8_t TxpLow, uint8_t TxpMid, uint8_t TxpHigh, int32_t LowerLimit, int32_t Middle, int32_t UpperLimit, int32_t Frequency); uint32_t FREQUENCY_RoundToStep(uint32_t freq, uint16_t step); -uint8_t FREQUENCY_GetStepIdxFromSortedIdx(uint8_t sortedIdx); -uint8_t FREQUENCY_GetSortedIdxFromStepIdx(uint8_t step); +STEP_Setting_t FREQUENCY_GetStepIdxFromSortedIdx(uint8_t sortedIdx); +uint32_t FREQUENCY_GetSortedIdxFromStepIdx(uint8_t step); -int TX_freq_check(const uint32_t Frequency); -int RX_freq_check(const uint32_t Frequency); +int32_t TX_freq_check(const uint32_t Frequency); +int32_t RX_freq_check(const uint32_t Frequency); -#endif +#endif \ No newline at end of file diff --git a/radio.c b/radio.c index a43d7ed..5b47daa 100644 --- a/radio.c +++ b/radio.c @@ -17,7 +17,7 @@ #include #include "app/mdc1200.h" #include - +#include "am_fix.h" #include "app/dtmf.h" #ifdef ENABLE_FMRADIO #include "app/fm.h" @@ -353,8 +353,8 @@ void RADIO_ConfigureChannel(const unsigned int VFO, const unsigned int configure else pVfo->freq_config_RX.Frequency = info.Frequency; - if (info.Offset >= 100000000) - info.Offset = 1000000; + if (info.Offset >= _1GHz_in_KHz) + info.Offset = _1GHz_in_KHz / 100; pVfo->TX_OFFSET_FREQUENCY = info.Offset; // *************** @@ -792,7 +792,8 @@ void RADIO_SetupRegisters(bool switchToForeground) BK4819_DisableDTMF(); } #endif - + BK4819_SetAGC(1); + BK4819_InitAGC(); // enable/disable BK4819 selected interrupts BK4819_WriteRegister(BK4819_REG_3F, InterruptMask); @@ -912,7 +913,6 @@ void RADIO_SetTxParameters(void) void RADIO_SetModulation(ModulationMode_t modulation) { - static ModulationMode_t m = MODULATION_UKNOWN; BK4819_AF_Type_t mod; switch(modulation) { default: @@ -937,16 +937,15 @@ void RADIO_SetModulation(ModulationMode_t modulation) } BK4819_SetAF(mod); - if(m != modulation) { - m = modulation; - BK4819_SetRegValue(afDacGainRegSpec, 0xF); - BK4819_WriteRegister(BK4819_REG_3D, modulation == MODULATION_USB ? 0 : 0x2AAB); - BK4819_SetRegValue(afcDisableRegSpec, modulation != MODULATION_FM); + + + BK4819_SetRegValue(afDacGainRegSpec, 0xF); + BK4819_WriteRegister(BK4819_REG_3D, modulation == MODULATION_USB ? 0 : 0x2AAB); + BK4819_SetRegValue(afcDisableRegSpec, modulation != MODULATION_FM); #ifdef ENABLE_AM_FIX - BK4819_SetAGC(gRxVfo->Modulation != MODULATION_AM || !gSetting_AM_fix); - BK4819_InitAGC(); + if(modulation == MODULATION_AM && gSetting_AM_fix) + BK4819_SetAGC(0); #endif - } } void RADIO_SetVfoState(VfoState_t State) diff --git a/settings.c b/settings.c index 2c3adc0..1a93a42 100644 --- a/settings.c +++ b/settings.c @@ -614,29 +614,33 @@ void SETTINGS_SaveChannel(uint8_t Channel, uint8_t VFO, const VFO_Info_t *pVFO, if (Mode >= 2 || !IS_MR_CHANNEL(Channel)) { // copy VFO to a channel - uint8_t State[8]; - ((uint32_t *)State)[0] = pVFO->freq_config_RX.Frequency; - ((uint32_t *)State)[1] = pVFO->TX_OFFSET_FREQUENCY; - EEPROM_WriteBuffer(OffsetVFO + 0, State); + union { + uint8_t _8[8]; + uint32_t _32[2]; + } State; - State[0] = pVFO->freq_config_RX.Code; - State[1] = pVFO->freq_config_TX.Code; - State[2] = (pVFO->freq_config_TX.CodeType << 4) | pVFO->freq_config_RX.CodeType; - State[3] = (pVFO->Modulation << 4) | pVFO->TX_OFFSET_FREQUENCY_DIRECTION; - State[4] = 0 + State._32[0] = pVFO->freq_config_RX.Frequency; + State._32[1] = pVFO->TX_OFFSET_FREQUENCY; + EEPROM_WriteBuffer(OffsetVFO + 0, State._32); + + State._8[0] = pVFO->freq_config_RX.Code; + State._8[1] = pVFO->freq_config_TX.Code; + State._8[2] = (pVFO->freq_config_TX.CodeType << 4) | pVFO->freq_config_RX.CodeType; + State._8[3] = (pVFO->Modulation << 4) | pVFO->TX_OFFSET_FREQUENCY_DIRECTION; + State._8[4] = 0 | (pVFO->BUSY_CHANNEL_LOCK << 4) | (pVFO->OUTPUT_POWER << 2) | (pVFO->CHANNEL_BANDWIDTH << 1) | (pVFO->FrequencyReverse << 0); - State[5] = ((pVFO->DTMF_PTT_ID_TX_MODE & 7u) << 1) + State._8[5] = ((pVFO->DTMF_PTT_ID_TX_MODE & 7u) << 1) #ifdef ENABLE_DTMF_CALLING | ((pVFO->DTMF_DECODING_ENABLE & 1u) << 0) #endif ; - State[6] = pVFO->STEP_SETTING; - State[7] = pVFO->SCRAMBLING_TYPE; - EEPROM_WriteBuffer(OffsetVFO + 8, State); + State._8[6] = pVFO->STEP_SETTING; + State._8[7] = pVFO->SCRAMBLING_TYPE; + EEPROM_WriteBuffer(OffsetVFO + 8, State._8); SETTINGS_UpdateChannel(Channel, pVFO, true); diff --git a/ui/battery.c b/ui/battery.c index 0441994..a5e5423 100644 --- a/ui/battery.c +++ b/ui/battery.c @@ -52,5 +52,5 @@ void UI_DisplayBattery(uint8_t level, uint8_t blink) { uint8_t bitmap[sizeof(BITMAP_BatteryLevel1)]; UI_DrawBattery(bitmap, level, blink); - ST7565_DrawLine(LCD_WIDTH - sizeof(bitmap), 0, sizeof(bitmap), bitmap); + ST7565_DrawLine(LCD_WIDTH - sizeof(bitmap), 0, bitmap, sizeof(bitmap)); } diff --git a/ui/main.c b/ui/main.c index 86a2064..eccfae0 100644 --- a/ui/main.c +++ b/ui/main.c @@ -246,7 +246,52 @@ void UI_UpdateRSSI(const int16_t rssi, const int vfo) } } +#ifdef ENABLE_AGC_SHOW_DATA +static void PrintAGC(bool now) +{ + char buf[20]; + memset(gFrameBuffer[3], 0, 128); + 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(0x7E); + uint8_t gainAddr = reg7e.gainIdx < 0 ? 0x14 : 0x10 + reg7e.gainIdx; + union { + struct { + uint16_t pga:3; + uint16_t mixer:2; + uint16_t lna:3; + uint16_t lnaS:2; + }; + uint16_t __raw; + } agcGainReg; + 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}; + int16_t agcGain = lnaShortTab[agcGainReg.lnaS] + lnaTab[agcGainReg.lna] + mixerTab[agcGainReg.mixer] + pgaTab[agcGainReg.pga]; + sprintf(buf, "%d%2d %2d %2d %3d", reg7e.agcEnab, reg7e.gainIdx, -agcGain, reg7e.agcSigStrength, BK4819_GetRSSI()); + UI_PrintStringSmall(buf, 2, 0, 3); + if(now) + ST7565_BlitLine(3); +} +#endif + +void UI_MAIN_TimeSlice500ms(void) +{ +#ifdef ENABLE_AGC_SHOW_DATA + if(gScreenToDisplay==DISPLAY_MAIN) + PrintAGC(true); +#endif +} // *************************************************************************** void UI_DisplayMain(void) @@ -428,7 +473,7 @@ void UI_DisplayMain(void) { // frequency mode // show the frequency band number const unsigned int x = 2; - char * buf = gEeprom.VfoInfo[vfo_num].pRX->Frequency < 100000000 ? "" : "+"; + char * buf = gEeprom.VfoInfo[vfo_num].pRX->Frequency < _1GHz_in_KHz ? "" : "+"; sprintf(String, "F%u%s", 1 + gEeprom.ScreenChannel[vfo_num] - FREQ_CHANNEL_FIRST, buf); UI_PrintStringSmall(String, x, 0, line + 1); } @@ -473,7 +518,7 @@ void UI_DisplayMain(void) const char * ascii = INPUTBOX_GetAscii(); - bool isGigaF = frequency>=100000000; + bool isGigaF = frequency>=_1GHz_in_KHz; sprintf(String, "%.*s.%.3s", 3 + isGigaF, ascii, ascii + 3 + isGigaF); #ifdef ENABLE_BIG_FREQ if(!isGigaF) { @@ -528,8 +573,8 @@ void UI_DisplayMain(void) case MDF_FREQUENCY: // show the channel frequency sprintf(String, "%3u.%05u", frequency / 100000, frequency % 100000); #ifdef ENABLE_BIG_FREQ - if(frequency < 100000000) { - // show the remaining 2 small frequency digits + if(frequency < _1GHz_in_KHz) { + // show the remaining 2 small frequency digits UI_PrintStringSmall(String + 7, 113, 0, line + 1); String[7] = 0; // show the main large frequency digits @@ -583,8 +628,8 @@ void UI_DisplayMain(void) sprintf(String, "%3u.%05u", frequency / 100000, frequency % 100000); #ifdef ENABLE_BIG_FREQ - if(frequency < 100000000) { - // show the remaining 2 small frequency digits + if(frequency < _1GHz_in_KHz) { + // show the remaining 2 small frequency digits UI_PrintStringSmall(String + 7, 113, 0, line + 1); String[7] = 0; // show the main large frequency digits @@ -702,7 +747,10 @@ void UI_DisplayMain(void) UI_PrintStringSmall("SCR", LCD_WIDTH + 106, 0, line + 1); } - +#ifdef ENABLE_AGC_SHOW_DATA + center_line = CENTER_LINE_IN_USE; + PrintAGC(false); +#endif if (center_line == CENTER_LINE_NONE) { // we're free to use the middle line diff --git a/ui/main.h b/ui/main.h index 69c1eab..4fa982b 100644 --- a/ui/main.h +++ b/ui/main.h @@ -34,6 +34,7 @@ extern center_line_t center_line; void UI_DisplayAudioBar(void); void UI_UpdateRSSI(const int16_t rssi, const int vfo); void UI_DisplayMain(void); +void UI_MAIN_TimeSlice500ms(void); extern const int8_t dBmCorrTable[7]; #endif