Skip to content

Commit

Permalink
SSB, BYP, RAW demodulation modes added #64
Browse files Browse the repository at this point in the history
  • Loading branch information
egzumer committed Nov 2, 2023
1 parent 9777b5d commit 3705b7c
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 89 deletions.
19 changes: 10 additions & 9 deletions app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void UpdateRSSI(const int vfo)

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

Expand Down Expand Up @@ -526,7 +526,7 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix)
const uint8_t orig_pga = 6; // -3dB

#ifdef ENABLE_AM_FIX
if (gRxVfo->AM_mode && gSetting_AM_fix) { // AM RX mode
if (gRxVfo->Modulation != MODULATION_FM && 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);
Expand All @@ -540,10 +540,11 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix)
}

// AF gain - original QS values
if (gRxVfo->AM_mode){
BK4819_WriteRegister(BK4819_REG_48, 0xB3A8);
}
else {
// 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
Expand All @@ -554,7 +555,7 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix)
#ifdef ENABLE_VOICE
if (gVoiceWriteIndex == 0) // AM/FM RX mode will be set when the voice has finished
#endif
BK4819_SetAF(gRxVfo->AM_mode ? BK4819_AF_AM : BK4819_AF_FM); // no need, set it now
RADIO_SetModulation(gRxVfo->Modulation); // no need, set it now

FUNCTION_Select(Function);

Expand Down Expand Up @@ -1209,8 +1210,8 @@ void APP_TimeSlice10ms(void)
#endif

#ifdef ENABLE_AM_FIX
// if (gEeprom.VfoInfo[gEeprom.RX_VFO].AM_mode && gSetting_AM_fix)
if (gRxVfo->AM_mode && gSetting_AM_fix)
// if (gEeprom.VfoInfo[gEeprom.RX_VFO].Modulation != MODULATION_FM && gSetting_AM_fix)
if (gRxVfo->Modulation != MODULATION_FM && gSetting_AM_fix)
AM_fix_10ms(gEeprom.RX_VFO);
#endif

Expand Down
14 changes: 9 additions & 5 deletions app/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ int MENU_GetLimits(uint8_t menu_id, int32_t *pMin, int32_t *pMax)
case MENU_D_ST:
case MENU_D_DCD:
case MENU_D_LIVE_DEC:
case MENU_AM:
#ifdef ENABLE_NOAA
case MENU_NOAA_S:
#endif
Expand All @@ -246,6 +245,11 @@ int MENU_GetLimits(uint8_t menu_id, int32_t *pMin, int32_t *pMax)
*pMax = ARRAY_SIZE(gSubMenu_OFF_ON) - 1;
break;

case MENU_AM:
*pMin = 0;
*pMax = ARRAY_SIZE(gModulationStr) - 1;
break;

case MENU_SCR:
*pMin = 0;
*pMax = ARRAY_SIZE(gSubMenu_SCRAMBLER) - 1;
Expand Down Expand Up @@ -702,7 +706,7 @@ void MENU_AcceptSetting(void)
break;

case MENU_AM:
gTxVfo->AM_mode = gSubMenuSelection;
gTxVfo->Modulation = gSubMenuSelection;
gRequestSaveChannel = 1;
return;

Expand Down Expand Up @@ -1112,7 +1116,7 @@ void MENU_ShowCurrentSetting(void)
break;

case MENU_AM:
gSubMenuSelection = gTxVfo->AM_mode;
gSubMenuSelection = gTxVfo->Modulation;
break;

#ifdef ENABLE_AM_FIX
Expand Down Expand Up @@ -1603,9 +1607,9 @@ static void MENU_Key_STAR(const bool bKeyPressed, const bool bKeyHeld)
RADIO_SelectVfos();

#ifdef ENABLE_NOAA
if (!IS_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE) && gRxVfo->AM_mode == 0)
if (!IS_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE) && gRxVfo->Modulation == MODULATION_FM)
#else
if (gRxVfo->AM_mode == 0)
if (gRxVfo->Modulation == MODULATION_FM)
#endif
{
if (GetCurrentMenuId() == MENU_R_CTCS || GetCurrentMenuId() == MENU_R_DCS)
Expand Down
29 changes: 8 additions & 21 deletions app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ ScanInfo scanInfo;
KeyboardState kbd = {KEY_INVALID, KEY_INVALID, 0};

const char *bwOptions[] = {" 25k", "12.5k", "6.25k"};
const char *modulationTypeOptions[] = {" FM", " AM", "USB"};
const uint8_t modulationTypeTuneSteps[] = {100, 50, 10};
const uint8_t modTypeReg47Values[] = {1, 7, 5};

Expand Down Expand Up @@ -103,22 +102,22 @@ static int Rssi2DBm(uint16_t rssi) { return (rssi >> 1) - 160; }

static uint16_t GetRegMenuValue(uint8_t st) {
RegisterSpec s = registerSpecs[st];
return (BK4819_ReadRegister(s.num) >> s.offset) & s.maxValue;
return (BK4819_ReadRegister(s.num) >> s.offset) & s.mask;
}

static void SetRegMenuValue(uint8_t st, bool add) {
uint16_t v = GetRegMenuValue(st);
RegisterSpec s = registerSpecs[st];

uint16_t reg = BK4819_ReadRegister(s.num);
if (add && v <= s.maxValue - s.inc) {
if (add && v <= s.mask - s.inc) {
v += s.inc;
} else if (!add && v >= 0 + s.inc) {
v -= s.inc;
}
// TODO: use max value for bits count in max value, or reset by additional
// mask in spec
reg &= ~(s.maxValue << s.offset);
reg &= ~(s.mask << s.offset);
BK4819_WriteRegister(s.num, reg | (v << s.offset));
redrawScreen = true;
}
Expand Down Expand Up @@ -225,18 +224,6 @@ static void RestoreRegisters() {
BK4819_WriteRegister(BK4819_REG_7E, R7E);
}

static void SetModulation(ModulationType type) {
RestoreRegisters();
uint16_t reg = BK4819_ReadRegister(BK4819_REG_47);
reg &= ~(0b111 << 8);
BK4819_WriteRegister(BK4819_REG_47, reg | (modTypeReg47Values[type] << 8));
if (type == MOD_USB) {
BK4819_WriteRegister(BK4819_REG_3D, 0b0010101101000101);
BK4819_WriteRegister(BK4819_REG_37, 0x160F);
BK4819_WriteRegister(BK4819_REG_48, 0b0000001110101000);
}
}

static void ToggleAFDAC(bool on) {
uint32_t Reg = BK4819_ReadRegister(BK4819_REG_30);
Reg &= ~(1 << 9);
Expand Down Expand Up @@ -489,12 +476,12 @@ static void UpdateFreqChangeStep(bool inc) {
}

static void ToggleModulation() {
if (settings.modulationType < MOD_USB) {
if (settings.modulationType < MODULATION_UKNOWN - 1) {
settings.modulationType++;
} else {
settings.modulationType = MOD_FM;
settings.modulationType = MODULATION_FM;
}
SetModulation(settings.modulationType);
RADIO_SetModulation(settings.modulationType);
redrawScreen = true;
}

Expand Down Expand Up @@ -669,7 +656,7 @@ static void DrawF(uint32_t f) {
sprintf(String, "%u.%05u", f / 100000, f % 100000);
UI_PrintStringSmall(String, 8, 127, 0);

sprintf(String, "%s", modulationTypeOptions[settings.modulationType]);
sprintf(String, "%3s", gModulationStr[settings.modulationType]);
GUI_DisplaySmallest(String, 116, 1, false, true);
sprintf(String, "%s", bwOptions[settings.listenBw]);
GUI_DisplaySmallest(String, 108, 7, false, true);
Expand Down Expand Up @@ -1175,7 +1162,7 @@ void APP_RunSpectrum() {
newScanStart = true;

ToggleRX(true), ToggleRX(false); // hack to prevent noise when squelch off
SetModulation(settings.modulationType = MOD_FM);
RADIO_SetModulation(settings.modulationType = MODULATION_FM);
BK4819_SetFilterBandwidth(settings.listenBw = BK4819_FILTER_BW_WIDE, false);

RelaunchScan();
Expand Down
16 changes: 1 addition & 15 deletions app/spectrum.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ typedef enum StepsCount {
STEPS_16,
} StepsCount;

typedef enum ModulationType {
MOD_FM,
MOD_AM,
MOD_USB,
} ModulationType;

typedef enum ScanStep {
S_STEP_0_01kHz,
S_STEP_0_1kHz,
Expand All @@ -131,7 +125,7 @@ typedef struct SpectrumSettings {
BK4819_FilterBandwidth_t listenBw;
int dbMin;
int dbMax;
ModulationType modulationType;
ModulationMode_t modulationType;
bool backlightState;
} SpectrumSettings;

Expand All @@ -149,14 +143,6 @@ typedef struct ScanInfo {
uint8_t measurementsCount;
} ScanInfo;

typedef struct RegisterSpec {
char *name;
uint8_t num;
uint8_t offset;
uint16_t maxValue;
uint16_t inc;
} RegisterSpec;

typedef struct PeakInfo {
uint16_t t;
uint16_t rssi;
Expand Down
4 changes: 2 additions & 2 deletions audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void AUDIO_PlayBeep(BEEP_Type_t Beep)
if (gCurrentFunction == FUNCTION_RECEIVE ||
gCurrentFunction == FUNCTION_MONITOR ||
gCurrentFunction == FUNCTION_INCOMING) // 1of11
BK4819_SetAF(gRxVfo->AM_mode ? BK4819_AF_AM : BK4819_AF_FM);
RADIO_SetModulation(gRxVfo->Modulation);

#ifdef ENABLE_FMRADIO
if (gFmRadioMode)
Expand Down Expand Up @@ -430,7 +430,7 @@ void AUDIO_PlayBeep(BEEP_Type_t Beep)
if (gCurrentFunction == FUNCTION_RECEIVE ||
gCurrentFunction == FUNCTION_MONITOR ||
gCurrentFunction == FUNCTION_INCOMING) // 1of11
BK4819_SetAF(gRxVfo->AM_mode ? BK4819_AF_AM : BK4819_AF_FM);
RADIO_SetModulation(gRxVfo->Modulation);

#ifdef ENABLE_FMRADIO
if (gFmRadioMode)
Expand Down
13 changes: 13 additions & 0 deletions driver/bk4819-regs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
#ifndef BK4819_REGS_H
#define BK4819_REGS_H


typedef struct {
const char *name;
uint8_t num;
uint8_t offset;
uint16_t mask;
uint16_t inc;
} RegisterSpec;

static const RegisterSpec afcDisableRegSpec = {"AFC Disable", 0x73, 4, 1, 1};
static const RegisterSpec afOutRegSpec = {"AF Output Select", 0x47, 8, 0xF, 1};
static const RegisterSpec afDacGainRegSpec = {"AF DAC Gain", 0x48, 0, 0xF, 1};

enum BK4819_REGISTER_t {
BK4819_REG_00 = 0x00U,
BK4819_REG_02 = 0x02U,
Expand Down
6 changes: 6 additions & 0 deletions driver/bk4819.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,12 @@ void BK4819_SetAF(BK4819_AF_Type_t AF)
BK4819_WriteRegister(BK4819_REG_47, (6u << 12) | (AF << 8) | (1u << 6));
}

void BK4819_SetRegValue(RegisterSpec s, uint16_t v) {
uint16_t reg = BK4819_ReadRegister(s.num);
reg &= ~(s.mask << s.offset);
BK4819_WriteRegister(s.num, reg | (v << s.offset));
}

void BK4819_RX_TurnOn(void)
{
// DSP Voltage Setting = 1
Expand Down
7 changes: 4 additions & 3 deletions driver/bk4819.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ enum BK4819_AF_Type_t
BK4819_AF_FM = 1u, // FM
BK4819_AF_ALAM = 2u, //
BK4819_AF_BEEP = 3u, //
BK4819_AF_BASEBAND1 = 4u, // SSB
BK4819_AF_BASEBAND2 = 5u, // SSB
BK4819_AF_BASEBAND1 = 4u, // RAW
BK4819_AF_BASEBAND2 = 5u, // USB
BK4819_AF_CTCO = 6u, // strange LF audio .. maybe the CTCSS LF line ?
BK4819_AF_AM = 7u, // AM
BK4819_AF_FSKO = 8u, // nothing
BK4819_AF_UNKNOWN3 = 9u, // distorted
BK4819_AF_UNKNOWN3 = 9u, // BYP
BK4819_AF_UNKNOWN4 = 10u, // nothing at all
BK4819_AF_UNKNOWN5 = 11u, // distorted
BK4819_AF_UNKNOWN6 = 12u, // distorted
Expand Down Expand Up @@ -68,6 +68,7 @@ extern bool gRxIdleMode;
void BK4819_Init(void);
uint16_t BK4819_ReadRegister(BK4819_REGISTER_t Register);
void BK4819_WriteRegister(BK4819_REGISTER_t Register, uint16_t Data);
void BK4819_SetRegValue(RegisterSpec s, uint16_t v);
void BK4819_WriteU8(uint8_t Data);
void BK4819_WriteU16(uint16_t Data);

Expand Down
2 changes: 1 addition & 1 deletion functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void FUNCTION_Init(void)
{
gCurrentCodeType = gSelectedCodeType;
if (gCssScanMode == CSS_SCAN_MODE_OFF)
gCurrentCodeType = gRxVfo->AM_mode ? CODE_TYPE_OFF : gRxVfo->pRX->CodeType;
gCurrentCodeType = (gRxVfo->Modulation != MODULATION_FM) ? CODE_TYPE_OFF : gRxVfo->pRX->CodeType;
}
#ifdef ENABLE_NOAA
else
Expand Down
Loading

0 comments on commit 3705b7c

Please sign in to comment.