0
mirror of https://github.com/OneOfEleven/uv-k5-firmware-custom.git synced 2025-04-28 14:21:25 +03:00

FM radio updates

This commit is contained in:
OneOfEleven 2023-10-31 06:56:21 +00:00
parent b85ea562c7
commit 0a9765b2bd
36 changed files with 152 additions and 167 deletions

View File

@ -19,8 +19,6 @@
//
// that is until someone works out how to properly configure the BK chip !
#include <string.h>
#include "am_fix.h"
#include "app/main.h"
#include "board.h"

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/action.h"
#include "app/app.h"
#include "app/dtmf.h"
@ -183,7 +181,7 @@ void ACTION_Scan(bool bRestart)
}
g_fm_channel_position = 0;
BK1080_GetFrequencyDeviation(Frequency);
BK1080_get_freq_offset(Frequency);
FM_tune(Frequency, FM_SCAN_STATE_DIR_UP, bRestart);

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#ifndef ENABLE_OVERLAY
#include "ARMCM0.h"
#endif

View File

@ -14,9 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include <stdlib.h> // abs()
#ifdef ENABLE_AM_FIX
#include "am_fix.h"
#endif
@ -1911,8 +1908,8 @@ void APP_time_slice_500ms(void)
if (g_fm_radio_tick_500ms > 0)
g_fm_radio_tick_500ms--;
if (g_fm_radio_mode && g_current_display_screen == DISPLAY_FM && g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
g_update_display = true; // can't do this if not FM scanning, it causes audio clicks
// if (g_fm_radio_mode && g_current_display_screen == DISPLAY_FM && g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
// g_update_display = true; // can't do this if not FM scanning, it causes audio clicks
#endif
if (g_backlight_count_down > 0 &&
@ -2116,18 +2113,23 @@ void APP_time_slice_500ms(void)
{
if (g_fm_resume_tick_500ms > 0)
{
if (--g_fm_resume_tick_500ms == 0)
if (g_fm_radio_mode)
{
RADIO_set_vfo_state(VFO_STATE_NORMAL);
if (--g_fm_resume_tick_500ms == 0)
{
RADIO_set_vfo_state(VFO_STATE_NORMAL);
if (g_current_function != FUNCTION_RECEIVE &&
!g_monitor_enabled &&
g_fm_radio_mode)
{ // switch back to FM radio mode
FM_turn_on();
GUI_SelectNextDisplay(DISPLAY_FM);
if (g_current_function != FUNCTION_RECEIVE && g_fm_radio_mode)
{ // switch back to FM radio mode
if (g_current_display_screen != DISPLAY_FM)
FM_turn_on();
//GUI_SelectNextDisplay(DISPLAY_FM);
}
}
GUI_SelectNextDisplay(DISPLAY_FM);
}
else
FM_turn_off();
}
}

View File

@ -14,9 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include <stdio.h> // NULL
#ifdef ENABLE_FMRADIO
#include "app/fm.h"
#endif

139
app/fm.c
View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/action.h"
#include "app/fm.h"
#include "app/generic.h"
@ -45,7 +43,6 @@ fm_scan_state_dir_t g_fm_scan_state_dir;
bool g_fm_auto_scan;
uint8_t g_fm_channel_position;
bool g_fm_found_frequency;
bool g_fm_auto_scan;
uint8_t g_fm_resume_tick_500ms;
uint16_t g_fm_restore_tick_10ms;
uint8_t g_fm_radio_tick_500ms;
@ -138,20 +135,28 @@ void FM_tune(uint16_t frequency, const fm_scan_state_dir_t scan_state_dir, const
g_fm_scan_state_dir = scan_state_dir;
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
if (g_fm_resume_tick_500ms < 10)
g_fm_resume_tick_500ms = 10; // update display for next 5 seconds
}
void FM_stop_scan(void)
{
// stop scanning
if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF)
return;
g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
if (g_fm_auto_scan)
if (g_fm_auto_scan || g_eeprom.fm_channel_mode)
{ // switch to channel mode
g_eeprom.fm_channel_mode = true;
g_eeprom.fm_selected_channel = 0;
FM_configure_channel_state();
}
else
{
g_eeprom.fm_channel_mode = false;
}
FM_configure_channel_state();
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
@ -163,55 +168,41 @@ void FM_stop_scan(void)
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
if (g_fm_resume_tick_500ms < 10)
g_fm_resume_tick_500ms = 10; // update display for next 5 seconds
g_update_display = true;
}
int FM_check_frequency_lock(uint16_t Frequency, uint16_t LowerLimit)
int FM_check_frequency_lock(const uint16_t frequency, const uint16_t lower_limit)
{
int ret = -1;
const uint16_t Test2 = BK1080_ReadRegister(BK1080_REG_07);
const uint16_t rssi_status = BK1080_ReadRegister(BK1080_REG_10);
const uint16_t dev_snr = BK1080_ReadRegister(BK1080_REG_07);
// This is supposed to be a signed value, but above function is unsigned
const uint16_t Deviation = BK1080_REG_07_GET_FREQD(Test2);
const int16_t freq_offset = (int16_t)dev_snr / 16;
const uint8_t snr = dev_snr & 0x000f;
if (BK1080_REG_07_GET_SNR(Test2) >= 2)
{
const uint16_t Status = BK1080_ReadRegister(BK1080_REG_10);
// const uint8_t stc = (rssi_status >> 14) & 1u;
// const uint8_t sf_bl = (rssi_status >> 13) & 1u;
const uint8_t afc_railed = (rssi_status >> 12) & 1u;
// const uint8_t ste = (rssi_status >> 9) & 1u;
// const uint8_t st = (rssi_status >> 8) & 1u;
const uint8_t rssi = rssi_status & 0x00ff;
if ((Status & BK1080_REG_10_MASK_AFCRL) == BK1080_REG_10_AFCRL_NOT_RAILED &&
BK1080_REG_10_GET_RSSI(Status) >= 10)
{
//if (Deviation > -281 && Deviation < 280)
if (Deviation < 280 || Deviation > 3815)
{
if (Frequency > LowerLimit && (Frequency - BK1080_BaseFrequency) == 1)
{
if (BK1080_FrequencyDeviation & 0x800)
goto Bail;
if (afc_railed || snr < 2 || rssi < 10 || abs(freq_offset) > 250)
goto Bail;
if (BK1080_FrequencyDeviation < 20)
goto Bail;
}
if (frequency >= lower_limit && abs(((int)BK1080_freq_base - frequency)) == 1)
if (abs(BK1080_freq_offset) < 20)
goto Bail;
if (Frequency >= LowerLimit && (BK1080_BaseFrequency - Frequency) == 1)
{
if ((BK1080_FrequencyDeviation & 0x800) == 0)
goto Bail;
// if (BK1080_FrequencyDeviation > -21)
if (BK1080_FrequencyDeviation > 4075)
goto Bail;
}
ret = 0;
}
}
}
ret = 0;
Bail:
BK1080_FrequencyDeviation = Deviation;
BK1080_BaseFrequency = Frequency;
BK1080_freq_offset = freq_offset;
BK1080_freq_base = frequency;
return ret;
}
@ -255,13 +246,15 @@ void FM_scan(void)
void FM_turn_on(void)
{
// mute the audio from the other radio chip (the transceiver chip)
BK4819_SetAF(BK4819_AF_MUTE);
g_fm_radio_mode = true;
g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
g_fm_restore_tick_10ms = 0;
g_fm_resume_tick_500ms = fm_resume_500ms; // update display again in 'n' seconds
// enable the FM radio chip
// enable the FM radio chip/audio
BK1080_Init(g_eeprom.fm_frequency_playing, true);
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
@ -272,18 +265,35 @@ void FM_turn_on(void)
void FM_turn_off(void)
{
if (g_fm_radio_mode)
{
if (!g_squelch_open && !g_monitor_enabled)
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
// disable the FM chip
BK1080_Init(0, false);
g_update_display = true;
g_update_status = true;
}
g_fm_radio_mode = false;
g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
g_fm_restore_tick_10ms = 0;
g_fm_resume_tick_500ms = 0;
}
if (!g_squelch_open && !g_monitor_enabled)
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
void FM_toggle_chan_freq_mode(void)
{
g_eeprom.fm_channel_mode = !g_eeprom.fm_channel_mode;
// disable the FM chip
BK1080_Init(0, false);
FM_stop_scan();
g_update_display = true;
g_update_status = true;
if (!FM_configure_channel_state())
{
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
g_request_save_fm = true;
}
}
// ***************************************
@ -306,6 +316,18 @@ static void FM_Key_DIGITS(const key_code_t Key, const bool key_pressed, const bo
// long press key or short key release
if (g_input_box_index == 0)
{
if (Key == KEY_0 || (Key >= KEY_2 && Key <= KEY_5))
{ // can't start a frequency with those keys
if (Key == KEY_3)
{ // can't start a frequency with a '3', so just go straight to the function
FM_toggle_chan_freq_mode();
}
return;
}
}
if (!g_fkey_pressed && !key_held)
{ // short key release
uint8_t State;
@ -432,13 +454,7 @@ static void FM_Key_DIGITS(const key_code_t Key, const bool key_pressed, const bo
break;
case KEY_3:
g_eeprom.fm_channel_mode = !g_eeprom.fm_channel_mode;
if (!FM_configure_channel_state())
{
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
g_request_save_fm = true;
}
FM_toggle_chan_freq_mode();
break;
default:
@ -676,6 +692,15 @@ static void FM_Key_UP_DOWN(const bool key_pressed, const bool key_held, const fm
g_eeprom.fm_selected_frequency = g_eeprom.fm_frequency_playing;
}
if (g_current_display_screen == DISPLAY_FM && g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF)
{
// if (g_fm_resume_tick_500ms < fm_resume_500ms)
// g_fm_resume_tick_500ms = fm_resume_500ms; // update display for next 'n' seconds
if (g_fm_resume_tick_500ms < 10)
g_fm_resume_tick_500ms = 10; // update display for next 5 seconds
g_update_display = true;
}
g_request_save_fm = true;
Bail:

View File

@ -37,7 +37,6 @@ extern uint8_t g_fm_channel_position;
// Doubts about whether this should be signed or not
extern uint16_t g_fm_frequency_deviation;
extern bool g_fm_found_frequency;
extern bool g_fm_auto_scan;
extern uint8_t g_fm_resume_tick_500ms;
extern uint16_t g_fm_restore_tick_10ms;
extern uint8_t g_fm_radio_tick_500ms;
@ -50,7 +49,7 @@ int FM_configure_channel_state(void);
void FM_erase_channels(void);
void FM_tune(uint16_t frequency, const fm_scan_state_dir_t scan_state_dir, const bool flag);
void FM_stop_scan(void);
int FM_check_frequency_lock(uint16_t Frequency, uint16_t LowerLimit);
int FM_check_frequency_lock(const uint16_t frequency, const uint16_t lower_limit);
void FM_scan(void);
void FM_turn_on(void);
void FM_turn_off(void);

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/app.h"
#ifdef ENABLE_FMRADIO
#include "app/fm.h"

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/action.h"
#include "app/app.h"
#ifdef ENABLE_FMRADIO

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#if !defined(ENABLE_OVERLAY)
#include "ARMCM0.h"
#endif

View File

@ -16,8 +16,6 @@
#define INCLUDE_AES
#include <string.h>
#if !defined(ENABLE_OVERLAY)
#include "ARMCM0.h"
#endif

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/dtmf.h"
#ifdef ENABLE_FMRADIO
#include "app/fm.h"

View File

@ -23,8 +23,8 @@
#include "misc.h"
//#define CHAN_SPACING 0u // 200kHz
#define CHAN_SPACING 1u // 100kHz
//#define CHAN_SPACING 2u // 50kHz
//#define CHAN_SPACING 1u // 100kHz
#define CHAN_SPACING 2u // 50kHz
#define VOLUME 15u
@ -101,8 +101,8 @@ static const uint16_t BK1080_RegisterTable[] =
bool is_init;
uint16_t BK1080_freq_lower;
uint16_t BK1080_freq_upper;
uint16_t BK1080_BaseFrequency;
uint16_t BK1080_FrequencyDeviation;
uint16_t BK1080_freq_base;
int16_t BK1080_freq_offset;
void BK1080_Init(const uint16_t frequency, const bool initialise)
{
@ -115,6 +115,9 @@ void BK1080_Init(const uint16_t frequency, const bool initialise)
if (!is_init)
{
BK1080_freq_base = 0;
BK1080_freq_offset = 0;
BK1080_freq_lower = 0xffff;
BK1080_freq_upper = 0;
@ -201,14 +204,14 @@ void BK1080_WriteRegister(BK1080_Register_t Register, uint16_t Value)
I2C_Stop();
}
void BK1080_Mute(bool Mute)
void BK1080_Mute(const bool Mute)
{
BK1080_WriteRegister(BK1080_REG_02_POWER_CONFIGURATION, (1u << 9) | (1u << 0) | (Mute ? 1u << 14 : 0u));
}
void BK1080_SetFrequency(uint16_t Frequency)
{
uint16_t channel;
int channel;
uint16_t band = 0;
// #if (ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE) > 1) // compiler doesn't like this :(
@ -229,18 +232,20 @@ void BK1080_SetFrequency(uint16_t Frequency)
#endif
channel = Frequency - FM_RADIO_FREQ_BAND_TABLE[band].lower; // 100kHz channel spacing
// channel = (int)Frequency - FM_RADIO_FREQ_BAND_TABLE[band].lower; // 100kHz channel spacing
channel = ((int)Frequency - FM_RADIO_FREQ_BAND_TABLE[band].lower) * 2; // 50kHz channel spacing
channel = (channel < 0) ? 0 : (channel > 1023) ? 1023 : channel;
BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, (SEEK_THRESHOLD << 8) | (band << 6) | (CHAN_SPACING << 4) | (VOLUME << 0));
BK1080_WriteRegister(BK1080_REG_03_CHANNEL, channel);
BK1080_WriteRegister(BK1080_REG_03_CHANNEL, (uint16_t)channel);
SYSTEM_DelayMs(1);
BK1080_WriteRegister(BK1080_REG_03_CHANNEL, channel | (1u << 15));
BK1080_WriteRegister(BK1080_REG_03_CHANNEL, (uint16_t)channel | (1u << 15));
}
uint16_t BK1080_GetFrequencyDeviation(uint16_t Frequency)
int16_t BK1080_get_freq_offset(const uint16_t Frequency)
{
BK1080_BaseFrequency = Frequency;
BK1080_FrequencyDeviation = BK1080_ReadRegister(BK1080_REG_07) >> 4;
return BK1080_FrequencyDeviation;
BK1080_freq_base = Frequency;
BK1080_freq_offset = (int16_t)BK1080_ReadRegister(BK1080_REG_07) / 16;
return BK1080_freq_offset;
}

View File

@ -24,15 +24,15 @@
extern uint16_t BK1080_freq_lower;
extern uint16_t BK1080_freq_upper;
extern uint16_t BK1080_BaseFrequency;
extern uint16_t BK1080_FrequencyDeviation;
extern uint16_t BK1080_freq_base;
extern int16_t BK1080_freq_offset;
void BK1080_Init(const uint16_t frequency, const bool initialise);
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_Mute(const bool Mute);
void BK1080_SetFrequency(uint16_t Frequency);
uint16_t BK1080_GetFrequencyDeviation(uint16_t Frequency);
int16_t BK1080_get_freq_offset(const uint16_t Frequency);
#endif

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h> // NULL and memset
#include "bk4819.h"
#include "bsp/dp32g030/gpio.h"
#include "bsp/dp32g030/portcon.h"

View File

@ -14,14 +14,12 @@
* limitations under the License.
*/
#include <string.h>
#include <stdbool.h>
#include "bsp/dp32g030/dma.h"
#include "bsp/dp32g030/syscon.h"
#include "bsp/dp32g030/uart.h"
#include "driver/uart.h"
#include "external/printf/printf.h"
#include "misc.h"
static bool UART_IsLogEnabled;
uint8_t UART_DMA_Buffer[256];

Binary file not shown.

Binary file not shown.

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/dtmf.h"
#ifdef ENABLE_FMRADIO
#include "app/fm.h"

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#ifdef ENABLE_AIRCOPY
#include "app/aircopy.h"
#endif

3
main.c
View File

@ -14,9 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include <stdio.h> // NULL
#ifdef ENABLE_AM_FIX
#include "am_fix.h"
#endif

View File

@ -1,6 +1,4 @@
#include <string.h>
#include "driver/bk4819.h"
#include "driver/crc.h"
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)

2
misc.c
View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "misc.h"
#include "settings.h"

2
misc.h
View File

@ -17,6 +17,8 @@
#ifndef MISC_H
#define MISC_H
#include <string.h>
#include <stdlib.h> // abs()
#include <stdbool.h>
#include <stdint.h>

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/app.h"
#include "app/dtmf.h"
#ifdef ENABLE_FMRADIO
@ -965,10 +963,6 @@ void RADIO_set_vfo_state(vfo_state_t State)
{
g_vfo_state[0] = VFO_STATE_NORMAL;
g_vfo_state[1] = VFO_STATE_NORMAL;
#ifdef ENABLE_FMRADIO
g_fm_resume_tick_500ms = 0;
#endif
}
else
{
@ -984,7 +978,8 @@ void RADIO_set_vfo_state(vfo_state_t State)
}
#ifdef ENABLE_FMRADIO
g_fm_resume_tick_500ms = fm_resume_500ms;
if (g_fm_radio_mode && g_fm_resume_tick_500ms < fm_resume_500ms)
g_fm_resume_tick_500ms = fm_resume_500ms;
#endif
}

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#ifdef ENABLE_FMRADIO
#include "app/fm.h"
#endif

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/aircopy.h"
#include "driver/st7565.h"
#include "external/printf/printf.h"

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/fm.h"
#include "driver/backlight.h"
#include "driver/bk1080.h"
@ -144,16 +142,31 @@ void UI_DisplayFM(void)
// *************************************
// can't do this during FM radio - audio clicks else
if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF || g_fm_resume_tick_500ms > 0)
{
const uint16_t val_07 = BK1080_ReadRegister(0x07);
const uint16_t val_0A = BK1080_ReadRegister(0x0A);
sprintf(str, "%s %s %2udBuV %2u",
((val_0A >> 9) & 1u) ? "STE" : "ste",
((val_0A >> 8) & 1u) ? "ST" : "st",
(val_0A >> 0) & 0x00ff,
(val_07 >> 0) & 0x000f);
UI_PrintStringSmall(str, 0, LCD_WIDTH, 6);
const uint16_t rssi_status = BK1080_ReadRegister(BK1080_REG_10);
const uint16_t dev_snr = BK1080_ReadRegister(BK1080_REG_07);
const int16_t freq_offset = (int16_t)dev_snr / 16;
const uint8_t snr = dev_snr & 0x000f;
// const uint8_t stc = (rssi_status >> 14) & 1u;
// const uint8_t sf_bl = (rssi_status >> 13) & 1u;
const uint8_t afc_railed = (rssi_status >> 12) & 1u;
const uint8_t ste = (rssi_status >> 9) & 1u;
const uint8_t st = (rssi_status >> 8) & 1u;
const uint8_t rssi = rssi_status & 0x00ff;
sprintf(str, "%s %s %c %2udBuV %2u",
ste ? "STE" : "ste",
st ? "ST" : "st",
afc_railed ? 'R' : 'r',
rssi,
snr);
UI_PrintStringSmall(str, 0, 0, 6);
sprintf(str, "%c%d", (freq_offset > 0) ? '+' : (freq_offset < 0) ? '-' : ' ', abs(freq_offset));
UI_PrintStringSmall(str, 0, 0, 5);
}
// *************************************

View File

@ -14,11 +14,10 @@
* limitations under the License.
*/
#include <string.h>
#include "driver/st7565.h"
#include "external/printf/printf.h"
#include "font.h"
#include "misc.h"
#include "ui/helper.h"
#include "ui/inputbox.h"

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "misc.h"
#include "ui/inputbox.h"

View File

@ -16,8 +16,6 @@
#ifdef ENABLE_PWRON_PASSWORD
#include <string.h>
#include "ARMCM0.h"
#include "app/uart.h"
#include "audio.h"

View File

@ -14,9 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include <stdlib.h> // abs()
#include "app/dtmf.h"
#ifdef ENABLE_AM_FIX_SHOW_DATA
#include "am_fix.h"

View File

@ -14,9 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include <stdlib.h> // abs()
#include "app/dtmf.h"
#include "app/menu.h"
#include "bitmaps.h"

View File

@ -14,9 +14,6 @@
* limitations under the License.
*/
#include <stdbool.h>
#include <string.h>
#include "app/search.h"
#include "board.h"
#include "dcs.h"

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/search.h"
#ifdef ENABLE_FMRADIO
#include "app/fm.h"

View File

@ -14,8 +14,6 @@
* limitations under the License.
*/
#include <string.h>
#include "app/dtmf.h"
#ifdef ENABLE_FMRADIO
#include "app/fm.h"