Skip to content

Commit

Permalink
FM radio band selection #230
Browse files Browse the repository at this point in the history
  • Loading branch information
egzumer committed Jan 15, 2024
1 parent adbc466 commit 46e2d1a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 60 deletions.
2 changes: 1 addition & 1 deletion app/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ static void ACTION_Scan_FM(bool bRestart)
gFM_AutoScan = true;
gFM_ChannelPosition = 0;
FM_EraseChannels();
freq = gEeprom.FM_LowerLimit;
freq = BK1080_GetFreqLoLimit(gEeprom.FM_Band);
} else {
gFM_AutoScan = false;
gFM_ChannelPosition = 0;
Expand Down
2 changes: 1 addition & 1 deletion app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void APP_StartListening(FUNCTION_Type_t function)

#ifdef ENABLE_FMRADIO
if (gFmRadioMode)
BK1080_Init(0, false);
BK1080_Init0();
#endif

// clear the other vfo's rssi level (to hide the antenna symbol)
Expand Down
54 changes: 33 additions & 21 deletions app/fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ static void Key_FUNC(KEY_Code_t Key, uint8_t state);

bool FM_CheckValidChannel(uint8_t Channel)
{
return (Channel < ARRAY_SIZE(gFM_Channels) && (gFM_Channels[Channel] >= 760 && gFM_Channels[Channel] < 1080));
return Channel < ARRAY_SIZE(gFM_Channels) &&
gFM_Channels[Channel] >= BK1080_GetFreqLoLimit(gEeprom.FM_Band) &&
gFM_Channels[Channel] < BK1080_GetFreqHiLimit(gEeprom.FM_Band);
}

uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction)
Expand Down Expand Up @@ -106,7 +108,7 @@ void FM_TurnOff(void)
AUDIO_AudioPathOff();
gEnableSpeaker = false;

BK1080_Init(0, false);
BK1080_Init0();

gUpdateStatus = true;
}
Expand Down Expand Up @@ -138,17 +140,17 @@ void FM_Tune(uint16_t Frequency, int8_t Step, bool bFlag)

if (!bFlag) {
Frequency += Step;
if (Frequency < gEeprom.FM_LowerLimit)
Frequency = gEeprom.FM_UpperLimit;
else if (Frequency > gEeprom.FM_UpperLimit)
Frequency = gEeprom.FM_LowerLimit;
if (Frequency < BK1080_GetFreqLoLimit(gEeprom.FM_Band))
Frequency = BK1080_GetFreqHiLimit(gEeprom.FM_Band);
else if (Frequency > BK1080_GetFreqHiLimit(gEeprom.FM_Band))
Frequency = BK1080_GetFreqLoLimit(gEeprom.FM_Band);

gEeprom.FM_FrequencyPlaying = Frequency;
}

gFM_ScanState = Step;

BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);
}

void FM_PlayAndUpdate(void)
Expand All @@ -161,7 +163,7 @@ void FM_PlayAndUpdate(void)
}

FM_ConfigureChannelState();
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);
SETTINGS_SaveFM();

gFmPlayCountdown_10ms = 0;
Expand Down Expand Up @@ -261,7 +263,7 @@ static void Key_DIGITS(KEY_Code_t Key, uint8_t state)
gInputBoxIndex = 0;
Frequency = StrToUL(INPUTBOX_GetAscii());

if (Frequency < gEeprom.FM_LowerLimit || gEeprom.FM_UpperLimit < Frequency) {
if (Frequency < BK1080_GetFreqLoLimit(gEeprom.FM_Band) || BK1080_GetFreqHiLimit(gEeprom.FM_Band) < Frequency) {
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
gRequestDisplayScreen = DISPLAY_FM;
return;
Expand All @@ -272,7 +274,7 @@ static void Key_DIGITS(KEY_Code_t Key, uint8_t state)
gAnotherVoiceID = (VOICE_ID_t)Key;
#endif
gEeprom.FM_FrequencyPlaying = gEeprom.FM_SelectedFrequency;
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);
gRequestSaveFM = true;
return;
}
Expand All @@ -290,7 +292,7 @@ static void Key_DIGITS(KEY_Code_t Key, uint8_t state)
#endif
gEeprom.FM_SelectedChannel = Channel;
gEeprom.FM_FrequencyPlaying = gFM_Channels[Channel];
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);
gRequestSaveFM = true;
return;
}
Expand Down Expand Up @@ -332,11 +334,21 @@ static void Key_FUNC(KEY_Code_t Key, uint8_t state)
ACTION_FM();
break;

case KEY_1:
gEeprom.FM_Band++;
gRequestSaveFM = true;
break;

// case KEY_2:
// gEeprom.FM_Space = (gEeprom.FM_Space + 1) % 3;
// gRequestSaveFM = true;
// break;

case KEY_3:
gEeprom.FM_IsMrMode = !gEeprom.FM_IsMrMode;

if (!FM_ConfigureChannelState()) {
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);
gRequestSaveFM = true;
}
else
Expand Down Expand Up @@ -424,7 +436,7 @@ static void Key_MENU(uint8_t state)
gFM_Channels[gEeprom.FM_SelectedChannel] = 0xFFFF;

FM_ConfigureChannelState();
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);

gRequestSaveFM = true;
}
Expand Down Expand Up @@ -487,10 +499,10 @@ static void Key_UP_DOWN(uint8_t state, int8_t Step)
else {
uint16_t Frequency = gEeprom.FM_SelectedFrequency + Step;

if (Frequency < gEeprom.FM_LowerLimit)
Frequency = gEeprom.FM_UpperLimit;
else if (Frequency > gEeprom.FM_UpperLimit)
Frequency = gEeprom.FM_LowerLimit;
if (Frequency < BK1080_GetFreqLoLimit(gEeprom.FM_Band))
Frequency = BK1080_GetFreqHiLimit(gEeprom.FM_Band);
else if (Frequency > BK1080_GetFreqHiLimit(gEeprom.FM_Band))
Frequency = BK1080_GetFreqLoLimit(gEeprom.FM_Band);

gEeprom.FM_FrequencyPlaying = Frequency;
gEeprom.FM_SelectedFrequency = gEeprom.FM_FrequencyPlaying;
Expand All @@ -499,7 +511,7 @@ static void Key_UP_DOWN(uint8_t state, int8_t Step)
gRequestSaveFM = true;

Bail:
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);

gRequestDisplayScreen = DISPLAY_FM;
}
Expand Down Expand Up @@ -542,7 +554,7 @@ void FM_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)

void FM_Play(void)
{
if (!FM_CheckFrequencyLock(gEeprom.FM_FrequencyPlaying, gEeprom.FM_LowerLimit)) {
if (!FM_CheckFrequencyLock(gEeprom.FM_FrequencyPlaying, BK1080_GetFreqLoLimit(gEeprom.FM_Band))) {
if (!gFM_AutoScan) {
gFmPlayCountdown_10ms = 0;
gFM_FoundFrequency = true;
Expand All @@ -567,7 +579,7 @@ void FM_Play(void)
}
}

if (gFM_AutoScan && gEeprom.FM_FrequencyPlaying >= gEeprom.FM_UpperLimit)
if (gFM_AutoScan && gEeprom.FM_FrequencyPlaying >= BK1080_GetFreqHiLimit(1))
FM_PlayAndUpdate();
else
FM_Tune(gEeprom.FM_FrequencyPlaying, gFM_ScanState, false);
Expand All @@ -582,7 +594,7 @@ void FM_Start(void)
gFM_ScanState = FM_SCAN_OFF;
gFM_RestoreCountdown_10ms = 0;

BK1080_Init(gEeprom.FM_FrequencyPlaying, true);
BK1080_Init(gEeprom.FM_FrequencyPlaying, gEeprom.FM_Band/*, gEeprom.FM_Space*/);

AUDIO_AudioPathOn();

Expand Down
2 changes: 1 addition & 1 deletion board.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void BOARD_Init(void)
BOARD_ADC_Init();
ST7565_Init();
#ifdef ENABLE_FMRADIO
BK1080_Init(0, false);
BK1080_Init0();
#endif

#if defined(ENABLE_UART) || defined(ENABLED_AIRCOPY)
Expand Down
41 changes: 35 additions & 6 deletions driver/bk1080.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ static bool gIsInitBK1080;
uint16_t BK1080_BaseFrequency;
uint16_t BK1080_FrequencyDeviation;

void BK1080_Init(uint16_t Frequency, bool bDoScan)
void BK1080_Init0(void)
{
BK1080_Init(0,0/*,0*/);
}

void BK1080_Init(uint16_t freq, uint8_t band/*, uint8_t space*/)
{
unsigned int i;

if (bDoScan) {
if (freq) {
GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_BK1080);

if (!gIsInitBK1080) {
Expand All @@ -63,8 +68,8 @@ void BK1080_Init(uint16_t Frequency, bool bDoScan)
BK1080_WriteRegister(BK1080_REG_02_POWER_CONFIGURATION, 0x0201);
}

BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, 0x0A5F);
BK1080_SetFrequency(Frequency);
BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, 0x0A1F);
BK1080_SetFrequency(freq, band/*, space*/);
}
else {
BK1080_WriteRegister(BK1080_REG_02_POWER_CONFIGURATION, 0x0241);
Expand Down Expand Up @@ -100,9 +105,19 @@ void BK1080_Mute(bool Mute)
BK1080_WriteRegister(BK1080_REG_02_POWER_CONFIGURATION, Mute ? 0x4201 : 0x0201);
}

void BK1080_SetFrequency(uint16_t frequency)
void BK1080_SetFrequency(uint16_t frequency, uint8_t band/*, uint8_t space*/)
{
uint16_t channel = frequency - 760;
//uint8_t spacings[] = {20,10,5};
//space %= 3;

uint16_t channel = (frequency - BK1080_GetFreqLoLimit(band))/* * 10 / spacings[space]*/;

uint16_t regval = BK1080_ReadRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2);
regval = (regval & ~(0b11 << 6)) | ((band & 0b11) << 6);
//regval = (regval & ~(0b11 << 4)) | ((space & 0b11) << 4);

BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, regval);

BK1080_WriteRegister(BK1080_REG_03_CHANNEL, channel);
SYSTEM_DelayMs(10);
BK1080_WriteRegister(BK1080_REG_03_CHANNEL, channel | 0x8000);
Expand All @@ -113,3 +128,17 @@ void BK1080_GetFrequencyDeviation(uint16_t Frequency)
BK1080_BaseFrequency = Frequency;
BK1080_FrequencyDeviation = BK1080_ReadRegister(BK1080_REG_07) / 16;
}

uint16_t BK1080_GetFreqLoLimit(uint8_t band)
{
uint16_t lim[] = {875, 760, 760, 640};
return lim[band % 4];
}

uint16_t BK1080_GetFreqHiLimit(uint8_t band)
{
band %= 4;
uint16_t lim[] = {1080, 1080, 900, 760};
return lim[band % 4];
}

7 changes: 5 additions & 2 deletions driver/bk1080.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
extern uint16_t BK1080_BaseFrequency;
extern uint16_t BK1080_FrequencyDeviation;

void BK1080_Init(uint16_t Frequency, bool bDoScan);
void BK1080_Init0(void);
void BK1080_Init(uint16_t Frequency, uint8_t band/*, uint8_t space*/);
uint16_t BK1080_ReadRegister(BK1080_Register_t Register);
void BK1080_WriteRegister(BK1080_Register_t Register, uint16_t Value);
void BK1080_Mute(bool Mute);
void BK1080_SetFrequency(uint16_t frequency);
uint16_t BK1080_GetFreqLoLimit(uint8_t band);
uint16_t BK1080_GetFreqHiLimit(uint8_t band);
void BK1080_SetFrequency(uint16_t frequency, uint8_t band/*, uint8_t space*/);
void BK1080_GetFrequencyDeviation(uint16_t Frequency);

#endif
Expand Down
2 changes: 1 addition & 1 deletion functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void FUNCTION_Transmit()

#if defined(ENABLE_FMRADIO)
if (gFmRadioMode)
BK1080_Init(0, false);
BK1080_Init0();
#endif

#ifdef ENABLE_ALARM
Expand Down
49 changes: 28 additions & 21 deletions settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifdef ENABLE_FMRADIO
#include "app/fm.h"
#endif
#include "driver/bk1080.h"
#include "driver/bk4819.h"
#include "driver/eeprom.h"
#include "misc.h"
Expand Down Expand Up @@ -89,17 +90,20 @@ void SETTINGS_InitEEPROM(void)
{
uint16_t selFreq;
uint8_t selChn;
uint8_t isMrMode;
uint8_t isMrMode:1;
uint8_t band:2;
//uint8_t space:2;
} __attribute__((packed)) fmCfg;
EEPROM_ReadBuffer(0x0E88, &fmCfg, 4);

gEeprom.FM_LowerLimit = 760;
gEeprom.FM_UpperLimit = 1080;
gEeprom.FM_Band = fmCfg.band;
//gEeprom.FM_Space = fmCfg.space;
gEeprom.FM_SelectedFrequency =
(fmCfg.selFreq >= gEeprom.FM_LowerLimit && fmCfg.selFreq <= gEeprom.FM_UpperLimit) ? fmCfg.selFreq : 960;
(fmCfg.selFreq >= BK1080_GetFreqLoLimit(gEeprom.FM_Band) && fmCfg.selFreq <= BK1080_GetFreqHiLimit(gEeprom.FM_Band)) ?
fmCfg.selFreq : BK1080_GetFreqLoLimit(gEeprom.FM_Band);

gEeprom.FM_SelectedChannel = fmCfg.selChn;
gEeprom.FM_IsMrMode = (fmCfg.isMrMode < 2) ? fmCfg.isMrMode : false;
gEeprom.FM_IsMrMode = fmCfg.isMrMode;
}

// 0E40..0E67
Expand Down Expand Up @@ -415,25 +419,28 @@ void SETTINGS_FactoryReset(bool bIsAll)
}

#ifdef ENABLE_FMRADIO
void SETTINGS_SaveFM(void)
void SETTINGS_SaveFM(void)
{
unsigned int i;

struct
{
uint16_t Frequency;
uint8_t Channel;
bool IsChannelSelected;
uint8_t Padding[4];
} State;
union {
struct {
uint16_t selFreq;
uint8_t selChn;
uint8_t isMrMode:1;
uint8_t band:2;
//uint8_t space:2;
};
uint8_t __raw[8];
} __attribute__((packed)) fmCfg;

memset(&State, 0xFF, sizeof(State));
State.Channel = gEeprom.FM_SelectedChannel;
State.Frequency = gEeprom.FM_SelectedFrequency;
State.IsChannelSelected = gEeprom.FM_IsMrMode;
memset(fmCfg.__raw, 0xFF, sizeof(fmCfg.__raw));
fmCfg.selChn = gEeprom.FM_SelectedChannel;
fmCfg.selFreq = gEeprom.FM_SelectedFrequency;
fmCfg.isMrMode = gEeprom.FM_IsMrMode;
fmCfg.band = gEeprom.FM_Band;
//fmCfg.space = gEeprom.FM_Space;
EEPROM_WriteBuffer(0x0E88, fmCfg.__raw);

EEPROM_WriteBuffer(0x0E88, &State);
for (i = 0; i < 5; i++)
for (unsigned i = 0; i < 5; i++)
EEPROM_WriteBuffer(0x0E40 + (i * 8), &gFM_Channels[i * 4]);
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ typedef struct {
uint8_t FM_SelectedChannel;
bool FM_IsMrMode;
uint16_t FM_FrequencyPlaying;
uint16_t FM_LowerLimit;
uint16_t FM_UpperLimit;
uint8_t FM_Band : 2;
//uint8_t FM_Space : 2;
#endif

uint8_t SQUELCH_LEVEL;
Expand Down
Loading

0 comments on commit 46e2d1a

Please sign in to comment.